| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <stdbool.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "pbm.h" |
| 28 |
#include "mallocvar.h" |
| 29 |
#include "shhopt.h" |
| 30 |
|
| 31 |
struct CmdlineInfo { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
const char * inputFileNm; |
| 36 |
unsigned int dscale; |
| 37 |
unsigned int invert; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
static void |
| 42 |
parseCommandLine(int argc, const char ** argv, |
| 43 |
struct CmdlineInfo * const cmdlineP) { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
optEntry * option_def; |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
|
| 53 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 54 |
|
| 55 |
option_def_index = 0; |
| 56 |
OPTENT3(0, "dscale", OPT_FLAG, NULL, &cmdlineP->dscale, 0); |
| 57 |
OPTENT3(0, "invert", OPT_FLAG, NULL, &cmdlineP->invert, 0); |
| 58 |
|
| 59 |
opt.opt_table = option_def; |
| 60 |
opt.short_allowed = false; |
| 61 |
opt.allowNegNum = false; |
| 62 |
|
| 63 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 64 |
|
| 65 |
|
| 66 |
if (argc-1 < 1) |
| 67 |
cmdlineP->inputFileNm = "-"; |
| 68 |
else if (argc-1 == 1) |
| 69 |
cmdlineP->inputFileNm = argv[1]; |
| 70 |
else |
| 71 |
pm_error("Program takes at most one argument: input file name"); |
| 72 |
|
| 73 |
free(option_def); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
typedef unsigned char Mdbyte; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static Mdbyte |
| 87 |
encode(bit ** const bits, |
| 88 |
int const row, |
| 89 |
int const col) { |
| 90 |
|
| 91 |
int n; |
| 92 |
int mask; |
| 93 |
Mdbyte b; |
| 94 |
|
| 95 |
mask = 0x80; |
| 96 |
b = 0; |
| 97 |
|
| 98 |
for (n = 0; n < 8; n++) { |
| 99 |
if (bits[row][col+n] == PBM_BLACK) b |= mask; |
| 100 |
mask = mask >> 1; |
| 101 |
} |
| 102 |
return b; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void |
| 108 |
doTranslation(bit ** const bits, |
| 109 |
unsigned int const nOutCols, |
| 110 |
unsigned int const nOutRows, |
| 111 |
unsigned int const nInRows, |
| 112 |
bool const mustInvert, |
| 113 |
bool const mustScale) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
unsigned int const step = mustScale ? 2 : 1; |
| 118 |
|
| 119 |
unsigned int row; |
| 120 |
Mdbyte * mdrow; |
| 121 |
|
| 122 |
MALLOCARRAY(mdrow, nOutCols); |
| 123 |
|
| 124 |
if (mdrow == NULL) |
| 125 |
pm_error("Unable to allocate memory for %u columns", nOutCols); |
| 126 |
|
| 127 |
for (row = 0; row < nOutRows; row += step) { |
| 128 |
unsigned int col; |
| 129 |
|
| 130 |
|
| 131 |
for (col = 0; col < nOutCols; ++col) { |
| 132 |
Mdbyte b; |
| 133 |
|
| 134 |
if (row < nInRows) |
| 135 |
b = encode(bits, row, col * 8); |
| 136 |
else |
| 137 |
b = 0xff; |
| 138 |
|
| 139 |
mdrow[col] = mustInvert ? b : ~b; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
for (col = 0; col < nOutCols; ) { |
| 144 |
Mdbyte const b = mdrow[col]; |
| 145 |
|
| 146 |
if (b != 0xFF && b != 0) { |
| 147 |
|
| 148 |
putchar(b); |
| 149 |
++col; |
| 150 |
} else { |
| 151 |
|
| 152 |
|
| 153 |
unsigned int x1; |
| 154 |
|
| 155 |
for (x1 = col; x1 < nOutCols; ++x1) { |
| 156 |
if (mdrow[x1] != b) break; |
| 157 |
assert(x1 >= col); |
| 158 |
if (x1 - col > 256) break; |
| 159 |
} |
| 160 |
assert(x1 >= col); |
| 161 |
x1 -= col; |
| 162 |
if (x1 == 256) x1 = 0; |
| 163 |
putchar(b); |
| 164 |
putchar(x1); |
| 165 |
col += x1; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
free(mdrow); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int |
| 175 |
main(int argc, const char ** argv) { |
| 176 |
|
| 177 |
const char * const headerValue = ".MDAMicroDesignPCWv1.00\r\npbm2mda\r\n"; |
| 178 |
|
| 179 |
struct CmdlineInfo cmdline; |
| 180 |
FILE * ifP; |
| 181 |
unsigned int nOutRowsUnrounded; |
| 182 |
unsigned int nOutCols, nOutRows; |
| 183 |
int nInCols, nInRows; |
| 184 |
bit ** bits; |
| 185 |
Mdbyte header[128]; |
| 186 |
int rc; |
| 187 |
|
| 188 |
pm_proginit(&argc, argv); |
| 189 |
|
| 190 |
parseCommandLine(argc, argv, &cmdline); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
memcpy(header + 0, headerValue, strlen(headerValue)); |
| 197 |
memset(header + strlen(headerValue), |
| 198 |
0x00, |
| 199 |
sizeof(header)-strlen(headerValue)); |
| 200 |
|
| 201 |
ifP = pm_openr(cmdline.inputFileNm); |
| 202 |
|
| 203 |
bits = pbm_readpbm(ifP, &nInCols, &nInRows); |
| 204 |
|
| 205 |
nOutRowsUnrounded = cmdline.dscale ? nInRows/2 : nInRows; |
| 206 |
|
| •207 |
overflow_add(nOutRowsUnrounded, 3); |
| 208 |
nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4; |
| 209 |
|
| 210 |
nOutCols = nInCols / 8; |
| 211 |
|
| 212 |
rc = fwrite(header, 1, 128, stdout); |
| 213 |
if (rc < 128) |
| 214 |
pm_error("Unable to write header to output file. errno=%d (%s)", |
| 215 |
errno, strerror(errno)); |
| 216 |
|
| 217 |
pm_writelittleshort(stdout, nOutRows); |
| 218 |
pm_writelittleshort(stdout, nOutCols); |
| 219 |
|
| 220 |
doTranslation(bits, nOutCols, nOutRows, nInRows, |
| 221 |
!!cmdline.invert, !!cmdline.dscale); |
| 222 |
|
| 223 |
pm_close(ifP); |
| 224 |
fflush(stdout); |
| 225 |
pbm_freearray(bits, nInRows); |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|