Debian Codegraph @

converter/pbm/pbmtomacp.c

raw
1 /*=============================================================================
2 pbmtomacp
3 ===============================================================================
4 Read a PBM file and produce a MacPaint bitmap file
5
6 Copyright (C) 2015 by Akira Urushibata ("douso").
7
8 Replacement of a previous program of the same name written in 1988
9 by Douwe van der Schaaf (...!mcvax!uvapsy!vdschaaf).
10
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted, provided
13 that the above copyright notice appear in all copies and that both that
14 copyright notice and this permission notice appear in supporting
15 documentation. This software is provided "as is" without express or implied
16 warranty.
17 =============================================================================*/
18
19 /*
20
21 Implementation notes
22
23 Header size is 512 bytes. There is no MacBinary header.
24
25 White margin which is added for input files with small dimensions
26 is treated separately from the active image raster. The margins
27 are directly coded based on the number of rows/columns.
28
29 Output file size never exceeds 53072 bytes. When -norle is specified,
30 output is always 53072 bytes. It is conceivable that decoders which
31 examine the size of Macpaint files (for general validation or for
32 determination of header type and size) do exist.
33
34 The uncompressed output (-norle case) fully conforms to Macpaint
35 specifications. No special treatment by the decoder is required.
36 */
37
38 #include <assert.h>
39
40 #include "pm_c_util.h"
41 #include "pbm.h"
42 #include "shhopt.h"
43 #include "mallocvar.h"
44 #include "runlength.h"
45 #include "macp.h"
46
47 #define MIN3(a,b,c) (MIN((MIN((a),(b))),(c)))
48
49 struct CmdlineInfo {
50 /* All the information the user supplied in the command line, in a form
51 easy for the program to use.
52 */
53 const char * inputFileName; /* File name of input file */
54 unsigned int left;
55 unsigned int right;
56 unsigned int top;
57 unsigned int bottom;
58 unsigned int leftSpec;
59 unsigned int rightSpec;
60 unsigned int topSpec;
61 unsigned int bottomSpec;
62 bool norle;
63 };
64
65
66
67 static void
68 parseCommandLine(int argc,
69 const char ** const argv,
70 struct CmdlineInfo * const cmdlineP) {
71 /*----------------------------------------------------------------------------
72 Parse program command line described in Unix standard form by argc
73 and argv. Return the information in the options as *cmdlineP.
74 -----------------------------------------------------------------------------*/
75 optEntry * option_def; /* malloc'ed */
76 optStruct3 opt;
77
78 unsigned int norleSpec;
79
80 unsigned int option_def_index;
81
82 MALLOCARRAY_NOFAIL(option_def, 100);
83
84 option_def_index = 0; /* incremented by OPTENTRY */
85 OPTENT3(0, "left", OPT_UINT, &cmdlineP->left,
86 &cmdlineP->leftSpec, 0);
87 OPTENT3(0, "right", OPT_UINT, &cmdlineP->right,
88 &cmdlineP->rightSpec, 0);
89 OPTENT3(0, "top", OPT_UINT, &cmdlineP->top,
90 &cmdlineP->topSpec, 0);
91 OPTENT3(0, "bottom", OPT_UINT, &cmdlineP->bottom,
92 &cmdlineP->bottomSpec, 0);
93 OPTENT3(0, "norle", OPT_FLAG, NULL,
94 &norleSpec, 0);
95
96 opt.opt_table = option_def;
97 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
98 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
99
100 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
101 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
102
103 cmdlineP->norle = norleSpec;
104
105 if (argc-1 < 1)
106 cmdlineP->inputFileName = "-";
107 else {
108 cmdlineP->inputFileName = argv[1];
109 if (argc-1 > 1)
110 pm_error("Program takes zero or one argument (filename). You "
111 "specified %d", argc-1);
112 }
113
114 free(option_def);
115 }
116
117
118
119 struct CropPadDimensions {
120 unsigned int imageWidth; /* Active image content */
121 unsigned int imageHeight;
122 unsigned int leftCrop; /* Cols cropped off from input */
123 unsigned int topCrop; /* Rows cropped off from input */
124 unsigned int topMargin; /* White padding for output */
125 unsigned int bottomMargin;
126 unsigned int leftMargin;
127 };
128
129
130
131 static void
132 calculateCropPad(struct CmdlineInfo const cmdline,
133 unsigned int const cols,
134 unsigned int const rows,
135 struct CropPadDimensions * const cropPadP) {
136 /*--------------------------------------------------------------------------
137 Validate -left -right -top -bottom from command line.
138
139 Determine what rows, columns to take from input if any of these are
140 specified and return it as *cropPadP.
141
142 'cols and 'rows' are the dimensions of the input image.
143
144 Center image if it is smaller than the fixed Macpaint format size.
145 ----------------------------------------------------------------------------*/
146 unsigned int const left = cmdline.leftSpec ? cmdline.left : 0;
147 unsigned int const top = cmdline.topSpec ? cmdline.top : 0;
148
149 unsigned int right, bottom, width, height;
150
151 if (cmdline.leftSpec) {
152 if (cmdline.rightSpec && left >= cmdline.right)
153 pm_error("-left value must be smaller than -right value");
154 else if (left + 1 > cols)
155 pm_error("Specified -left value is beyond right edge "
156 "of input image");
157 }
158 if (cmdline.topSpec) {
159 if (cmdline.bottomSpec && top >= cmdline.bottom)
160 pm_error("-top value must be smaller than -bottom value");
161 else if (top + 1 > rows)
162 pm_error("Specified -top value is beyond bottom edge "
163 "of input image");
164 }
165 if (cmdline.rightSpec) {
166 if (cmdline.right + 1 > cols)
167 pm_message("Specified -right value %u is beyond edge of "
168 "input image", cmdline.right);
169
170 right = MIN3(cmdline.right, cols - 1, left + MACP_COLS - 1);
171 } else
172 right = MIN(cols - 1, left + MACP_COLS - 1);
173
174 if (cmdline.bottomSpec) {
175 if (cmdline.bottom + 1 > rows)
176 pm_message("Specified -bottom value %u is beyond edge of "
177 "input image", cmdline.bottom);
178
179 bottom = MIN3(cmdline.bottom, rows - 1, top + MACP_ROWS - 1);
180 } else
181 bottom = MIN(rows - 1, top + MACP_ROWS - 1);
182
183 cropPadP->leftCrop = left;
184 cropPadP->topCrop = top;
185
186 assert(right >= left);
187
188 width = right - left + 1;
189 assert(width > 0 && width <= MACP_COLS);
190
191 cropPadP->leftMargin = (MACP_COLS - width) / 2;
192
193 if (width < cols)
194 pm_message("%u of %u input columns will be output", width, cols);
195
196 height = bottom - top + 1;
197 assert(height > 0 && height <= MACP_ROWS);
198
199 cropPadP->topMargin = (MACP_ROWS - height) / 2;
200 cropPadP->bottomMargin = cropPadP->topMargin + height - 1;
201
202 if (height < rows)
203 pm_message("%u out of %u input rows will be output", height, rows);
204
205 cropPadP->imageWidth = width;
206 cropPadP->imageHeight = height;
207 }
208
209
210
211 static void
212 writeMacpHeader(FILE * const ofP) {
213
214 char const ch = 0x00; /* header contains nothing */
215
216 unsigned int i;
217
218 for (i = 0; i < MACP_HEAD_LEN; ++i)
219 fputc(ch, ofP);
220 }
221
222
223
224 static void
225 writeMacpRowUnpacked(const bit * const imageBits,
226 unsigned int const leftMarginCharCt,
227 unsigned int const imageColCharCt,
228 FILE * const ofP) {
229 /*--------------------------------------------------------------------------
230 Encode (without compression) and output one row. The row comes divided into
231 three parts: left margin, image, right margin.
232 ----------------------------------------------------------------------------*/
233 char const marginByte = 0x00; /* White bits for margin */
234 unsigned int const rightMarginCharCt =
235 MACP_COLCHARS - leftMarginCharCt - imageColCharCt;
236
237 unsigned int i;
238
239 fputc(MACP_COLCHARS - 1, ofP);
240
241 for (i = 0; i < leftMarginCharCt; ++i)
242 fputc(marginByte, ofP);
243
244 if (imageColCharCt > 0)
245 fwrite(imageBits, 1, imageColCharCt, ofP);
246
247 for (i = 0; i < rightMarginCharCt; ++i)
248 fputc(marginByte, ofP);
249 }
250
251
252
253 static void
254 writeMacpRowPacked(const bit * const packedBits,
255 unsigned int const leftMarginCharCt,
256 unsigned int const imageColCharCt,
257 unsigned int const rightMarginCharCt,
258 FILE * const ofP) {
259 /*--------------------------------------------------------------------------
260 Encode one row and write it to *ofP.
261
262 As in the unpacked case, the row comes divided into three parts: left
263 margin, image, right margin. Unlike the unpacked case we need to know both
264 the size of the packed data and the size of the right margin.
265 ----------------------------------------------------------------------------*/
266 char const marginByte = 0x00; /* White bits for margin */
267
268 if (leftMarginCharCt > 0) {
269 fputc(257 - leftMarginCharCt, ofP);
270 fputc(marginByte, ofP);
271 }
272
273 if (imageColCharCt > 0)
274 fwrite(packedBits, 1, imageColCharCt, ofP);
275
276 if (rightMarginCharCt > 0) {
277 fputc(257 - rightMarginCharCt, ofP);
278 fputc(marginByte, ofP);
279 }
280 }
281
282
283
284 static void
285 writeMacpRow(bit * const imageBits,
286 unsigned int const leftMarginCharCt,
287 unsigned int const imageColCharCt,
288 bool const norle,
289 FILE * const ofP) {
290 /*--------------------------------------------------------------------------
291 Write the row 'imageBits' to Standard Output.
292
293 Write it packed, unless packing would lead to unnecessary bloat or 'norle'
294 is true.
295 ----------------------------------------------------------------------------*/
296 if (norle)
297 writeMacpRowUnpacked(imageBits, leftMarginCharCt, imageColCharCt, ofP);
298 else {
299 unsigned int const rightMarginCharCt =
300 MACP_COLCHARS - leftMarginCharCt - imageColCharCt;
301 unsigned char packedBits[MACP_COLCHARS+1];
302 size_t packedImageLength;
303
304 if (pm_rlenc_maxbytes(MACP_COLCHARS, PM_RLE_PACKBITS)
305 > MACP_COLCHARS + 1)
306 pm_error("INTERNAL ERROR: RLE buffer too small");
307
308 pm_rlenc_compressbyte(imageBits, packedBits, PM_RLE_PACKBITS,
309 imageColCharCt, &packedImageLength);
310
311 if (packedImageLength +
312 (leftMarginCharCt > 0 ? 1 : 0) * 2 +
313 (rightMarginCharCt > 0 ? 1 : 0) * 2
314 < MACP_COLCHARS) {
315 /* It's smaller compressed, so do that */
316 writeMacpRowPacked(packedBits, leftMarginCharCt,
317 packedImageLength, rightMarginCharCt, ofP);
318 } else { /* Extremely rare */
319 /* It's larger compressed, so do it uncompressed. See note
320 at top of file.
321 */
322 writeMacpRowUnpacked(imageBits, leftMarginCharCt, imageColCharCt,
323 ofP);
324 }
325 }
326 }
327
328
329
330 static void
331 encodeRowsWithShift(bit * const bitrow,
332 FILE * const ifP,
333 int const inCols,
334 int const format,
335 bool const norle,
336 struct CropPadDimensions const cropPad,
337 FILE * const ofP) {
338 /*--------------------------------------------------------------------------
339 Shift input rows to put only specified columns to output. Add padding on
340 left and right if necessary.
341
342 No shift if the input image is the exact size (576 columns) of the Macpaint
343 format. If the input image is too wide and -left was not specified, extra
344 content on the right is discarded.
345 ----------------------------------------------------------------------------*/
346 unsigned int const offset =
347 (cropPad.leftMargin + 8 - cropPad.leftCrop % 8) % 8;
348 unsigned int const leftTrim =
349 cropPad.leftMargin % 8;
350 unsigned int const rightTrim =
351 (8 - (leftTrim + cropPad.imageWidth) % 8 ) % 8;
352 unsigned int const startChar =
353 (cropPad.leftCrop + offset) / 8;
354 unsigned int const imageCharCt =
355 pbm_packed_bytes(leftTrim + cropPad.imageWidth);
356 unsigned int const leftMarginCharCt =
357 cropPad.leftMargin / 8;
358
359 unsigned int row;
360
361 for (row = 0; row < cropPad.imageHeight; ++row) {
362 pbm_readpbmrow_bitoffset(ifP, bitrow, inCols, format, offset);
363
364 /* Trim off fractional margin portion in first byte of image data */
365 if (leftTrim > 0) {
366 bitrow[startChar] <<= leftTrim;
367 bitrow[startChar] >>= leftTrim;
368 }
369 /* Do the same with bits in last byte of relevant image data */
370 if (rightTrim > 0) {
371 bitrow[startChar + imageCharCt - 1] >>= rightTrim;
372 bitrow[startChar + imageCharCt - 1] <<= rightTrim;
373 }
374
375 writeMacpRow(&bitrow[startChar], leftMarginCharCt,
376 imageCharCt, norle, ofP);
377 }
378 }
379
380
381
382 static void
383 writeMacp(unsigned int const cols,
384 unsigned int const rows,
385 int const format,
386 FILE * const ifP,
387 bool const norle,
388 struct CropPadDimensions const cropPad,
389 FILE * const ofP) {
390
391 unsigned int row, skipRow;
392 bit * bitrow;
393
394 writeMacpHeader(ofP);
395
396 /* Write top padding */
397 for (row = 0; row < cropPad.topMargin; ++row)
398 writeMacpRow(NULL, MACP_COLCHARS, 0, norle, ofP);
399
400 /* Allocate PBM row with one extra byte for the shift case. */
401 bitrow = pbm_allocrow_packed(cols + 8);
402
403 for (skipRow = 0; skipRow < cropPad.topCrop; ++skipRow)
404 pbm_readpbmrow_packed(ifP, bitrow, cols, format);
405
406 encodeRowsWithShift(bitrow, ifP, cols, format, norle, cropPad, ofP);
407
408 pbm_freerow_packed(bitrow);
409
410 /* Add bottom padding */
411 for (row = cropPad.bottomMargin + 1; row < MACP_ROWS; ++row)
412 writeMacpRow(NULL, MACP_COLCHARS, 0, norle, ofP);
413 }
414
415
416
417 int
418 main(int argc, const char *argv[]) {
419
420 FILE * ifP;
421 int rows, cols;
422 int format;
423 struct CmdlineInfo cmdline;
424 struct CropPadDimensions cropPad;
425
426 pm_proginit(&argc, argv);
427
428 parseCommandLine(argc, argv, &cmdline);
429
430 ifP = pm_openr(cmdline.inputFileName);
431
432 pbm_readpbminit(ifP, &cols, &rows, &format);
433
434 calculateCropPad(cmdline, cols, rows, &cropPad);
435
436 writeMacp(cols, rows, format, ifP, cmdline.norle, cropPad, stdout);
437
438 pm_close(ifP);
439
440 return 0;
441 }
442
443
444