| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
. |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <string.h> |
| 48 |
|
| 49 |
#include "pm_c_util.h" |
| 50 |
#include "pbm.h" |
| 51 |
#include "shhopt.h" |
| 52 |
#include "mallocvar.h" |
| 53 |
|
| 54 |
struct cmdlineInfo { |
| 55 |
unsigned char graph_mode; |
| 56 |
unsigned int passes; |
| 57 |
unsigned int nFiles; |
| 58 |
const char ** inputFile; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
bool sent_xon; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static void |
| 68 |
parseCommandLine(int argc, char ** const argv, |
| 69 |
struct cmdlineInfo * const cmdlineP) { |
| 70 |
|
| 71 |
optStruct3 opt; |
| 72 |
optEntry option_def[100]; |
| 73 |
|
| 74 |
unsigned int option_def_index = 0; |
| 75 |
unsigned int xresSpec, yresSpec; |
| 76 |
unsigned int xres, yres; |
| 77 |
unsigned int slowMode; |
| 78 |
|
| 79 |
OPTENT3(0, "xres", OPT_UINT, &xres, &xresSpec, 0); |
| 80 |
OPTENT3(0, "yres", OPT_UINT, &yres, &yresSpec, 0); |
| 81 |
OPTENT3(0, "slow", OPT_FLAG, NULL, &slowMode, 0); |
| 82 |
|
| 83 |
opt.opt_table = option_def; |
| 84 |
opt.short_allowed = 0; |
| 85 |
opt.allowNegNum = 0; |
| 86 |
|
| 87 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 88 |
|
| 89 |
|
| 90 |
if (!xresSpec) |
| 91 |
pm_error("You must specify the -xres option"); |
| 92 |
if (!yresSpec) |
| 93 |
pm_error("You must specify the -yres option"); |
| 94 |
|
| 95 |
switch (xres) { |
| 96 |
case 60: cmdlineP->graph_mode = 'K'; break; |
| 97 |
case 120: cmdlineP->graph_mode = slowMode ? 'L' : 'Y'; break; |
| 98 |
case 240: cmdlineP->graph_mode = 'Z'; break; |
| 99 |
default: |
| 100 |
pm_error("Please specify 60, 120, or 240 for -xres"); |
| 101 |
} |
| 102 |
|
| 103 |
if (yres != 60 && yres != 120 && yres != 240) |
| 104 |
pm_error("Please specify 60, 120, or 240 for -yres"); |
| 105 |
|
| 106 |
cmdlineP->passes = yres / 60; |
| 107 |
|
| 108 |
cmdlineP->nFiles = MAX(argc-1, 1); |
| 109 |
MALLOCARRAY_NOFAIL(cmdlineP->inputFile, cmdlineP->nFiles); |
| 110 |
|
| 111 |
if (argc-1 < 1) |
| 112 |
cmdlineP->inputFile[0] = "-"; |
| 113 |
else { |
| 114 |
unsigned int i; |
| 115 |
for (i = 0; i < argc-1; ++i) |
| 116 |
cmdlineP->inputFile[i] = argv[i+1]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static void |
| 124 |
process_handle(FILE * const fh, |
| 125 |
unsigned char const graph_mode, |
| 126 |
unsigned int const passes) { |
| 127 |
int eof; |
| 128 |
|
| 129 |
while(pbm_nextimage(fh, &eof), eof == 0) { |
| 130 |
|
| 131 |
int cols, rows, format; |
| 132 |
|
| 133 |
unsigned int x, y; |
| 134 |
unsigned int bitline; |
| 135 |
unsigned int pass; |
| 136 |
|
| 137 |
unsigned char *output; |
| 138 |
bit *row; |
| 139 |
|
| 140 |
|
| 141 |
if(!sent_xon) { |
| 142 |
putchar(0x11); |
| 143 |
sent_xon = TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
pbm_readpbminit(fh, &cols, &rows, &format); |
| 147 |
|
| 148 |
output = malloc(sizeof(*output) * cols * passes); |
| 149 |
if(output == NULL) |
| 150 |
pm_error("Out of memory"); |
| 151 |
row = pbm_allocrow(cols); |
| 152 |
|
| 153 |
for(y = 0; y < rows; y += 8 * passes) { |
| 154 |
memset(output, 0, sizeof(*output) * cols * passes); |
| 155 |
for(bitline = 0; bitline < 8; ++bitline) |
| 156 |
for(pass = 0; pass < passes; ++pass) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if(y + bitline * passes + pass < rows) { |
| 161 |
pbm_readpbmrow(fh, row, cols, format); |
| 162 |
for(x = 0; x < cols; ++x) |
| 163 |
if(row[x] == PBM_BLACK) |
| 164 |
output[cols * pass + x] |= 1 << (7 - bitline); |
| 165 |
} |
| 166 |
for(pass = 0; pass < passes; ++pass){ |
| 167 |
|
| 168 |
putchar(0x1b); putchar(graph_mode); |
| 169 |
putchar(cols & 0xff); putchar((cols >> 8) & 0xff); |
| 170 |
fwrite(output + pass * cols, sizeof(*output), cols, stdout); |
| 171 |
|
| 172 |
|
| 173 |
putchar('\r'); |
| 174 |
|
| 175 |
|
| 176 |
putchar(0x1b); putchar('J'); putchar(4 / passes); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
putchar(0x1b); putchar('J'); putchar(24 - 4); |
| 181 |
} |
| 182 |
putchar(0x0c); |
| 183 |
|
| 184 |
pbm_freerow(row); |
| 185 |
free(output); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
int |
| 192 |
main(int argc,char **argv) { |
| 193 |
|
| 194 |
struct cmdlineInfo cmdline; |
| 195 |
unsigned int i; |
| 196 |
|
| 197 |
pbm_init(&argc, argv); |
| 198 |
|
| 199 |
parseCommandLine(argc, argv, &cmdline); |
| 200 |
|
| 201 |
sent_xon = FALSE; |
| 202 |
|
| 203 |
for (i = 0; i < cmdline.nFiles; ++i) { |
| 204 |
FILE *ifP; |
| 205 |
pm_message("opening '%s'", cmdline.inputFile[i]); |
| 206 |
ifP = pm_openr(cmdline.inputFile[i]); |
| 207 |
process_handle(ifP, cmdline.graph_mode, cmdline.passes); |
| 208 |
pm_close(ifP); |
| 209 |
} |
| 210 |
|
| 211 |
free(cmdline.inputFile); |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|