Debian Codegraph @

converter/other/pnmtopalm/pnmtopalm.c

raw symbols SCIP
1 /* pnmtopalm.c - read a PNM image and write a Palm Bitmap file
2 *
3 * Inspired by and using methods from ppmtoTbmp.c by Ian Goldberg
4 * <[email protected]>, which was based on ppmtopuzz.c by Jef
5 * Poskanzer, from the netpbm-1mar1994 package.
6 *
7 * Mods for multiple bits per pixel were added to ppmtoTbmp.c by
8 * George Caswell <[email protected]> and Bill Janssen
9 * <[email protected]>.
10 *
11 * Major fixes and new capability added by Paul Bolle <[email protected]>
12 * in late 2004 / early 2005.
13 *
14 * See LICENSE file for licensing information.
15 *
16 * References for the Palm Bitmap format:
17 *
18 * https://web.archive.org/web/20030621112139/http://www.palmos.com:80/dev/support/docs/
19 * https://web.archive.org/web/20030413080018/http://www.palmos.com:80/dev/support/docs/palmos/ReferenceTOC.html
20 *
21 * http://www.trantor.de/kawt/doc/palmimages.html
22 * (above retrieved August 2017)
23 */
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <limits.h>
29
30 #include "pm_c_util.h"
31 #include "pnm.h"
32 #include "shhopt.h"
33 #include "mallocvar.h"
34 #include "runlength.h"
35
36 #include "palm.h"
37 #include "palmcolormap.h"
38
39 enum CompressionType {COMP_NONE, COMP_SCANLINE, COMP_RLE, COMP_PACKBITS};
40
41 struct CmdlineInfo {
42 /* All the information the user supplied in the command line,
43 in a form easy for the program to use.
44 */
45 const char * inputFilespec; /* Filespecs of input files */
46 const char * transparent;
47 unsigned int depthSpec;
48 unsigned int depth;
49 unsigned int maxdepthSpec;
50 unsigned int maxdepth;
51 enum CompressionType compression;
52 unsigned int verbose;
53 unsigned int colormap;
54 unsigned int offset;
55 unsigned int density;
56 unsigned int withdummy;
57 };
58
59
60
61 static void
62 parseCommandLine(int argc, const char ** argv, struct CmdlineInfo *cmdlineP) {
63 /*----------------------------------------------------------------------------
64 Note that the file spec array we return is stored in the storage that
65 was passed to us as the argv array.
66 -----------------------------------------------------------------------------*/
67 optStruct3 opt; /* set by OPTENT3 */
68 optEntry *option_def;
69 unsigned int option_def_index;
70
71 unsigned int transSpec, densitySpec;
72 unsigned int scanline_compression, rle_compression, packbits_compression;
73
74 MALLOCARRAY_NOFAIL(option_def, 100);
75
76 option_def_index = 0; /* incremented by OPTENTRY */
77 OPTENT3(0, "transparent", OPT_STRING,
78 &cmdlineP->transparent, &transSpec, 0);
79 OPTENT3(0, "depth", OPT_UINT,
80 &cmdlineP->depth, &cmdlineP->depthSpec, 0);
81 OPTENT3(0, "maxdepth", OPT_UINT,
82 &cmdlineP->maxdepth, &cmdlineP->maxdepthSpec, 0);
83 OPTENT3(0, "scanline_compression", OPT_FLAG,
84 NULL, &scanline_compression, 0);
85 OPTENT3(0, "rle_compression", OPT_FLAG,
86 NULL, &rle_compression, 0);
87 OPTENT3(0, "packbits_compression", OPT_FLAG,
88 NULL, &packbits_compression, 0);
89 OPTENT3(0, "verbose", OPT_FLAG,
90 NULL, &cmdlineP->verbose, 0);
91 OPTENT3(0, "colormap", OPT_FLAG,
92 NULL, &cmdlineP->colormap, 0);
93 OPTENT3(0, "offset", OPT_FLAG,
94 NULL, &cmdlineP->offset, 0);
95 OPTENT3(0, "density", OPT_UINT,
96 &cmdlineP->density, &densitySpec, 0);
97 OPTENT3(0, "withdummy", OPT_FLAG,
98 NULL, &cmdlineP->withdummy, 0);
99
100 opt.opt_table = option_def;
101 opt.short_allowed = FALSE; /* We have some short (old-fashioned) options */
102 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
103
104 pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
105 /* Uses and sets argc, argv, and some of *cmdline_p and others. */
106
107 if (cmdlineP->depthSpec) {
108 if (cmdlineP->depth != 1 && cmdlineP->depth != 2
109 && cmdlineP->depth != 4 && cmdlineP->depth != 8
110 && cmdlineP->depth != 16)
111 pm_error("invalid value for -depth: %u. Valid values are "
112 "1, 2, 4, 8, and 16", cmdlineP->depth);
113 }
114
115 if (cmdlineP->maxdepthSpec) {
116 if (cmdlineP->maxdepth != 1 && cmdlineP->maxdepth != 2
117 && cmdlineP->maxdepth != 4 && cmdlineP->maxdepth != 8
118 && cmdlineP->maxdepth != 16)
119 pm_error("invalid value for -maxdepth: %u. Valid values are "
120 "1, 2, 4, 8, and 16", cmdlineP->maxdepth);
121 }
122
123 if (cmdlineP->depthSpec && cmdlineP->maxdepthSpec &&
124 cmdlineP->depth > cmdlineP->maxdepth)
125 pm_error("-depth value (%u) is greater than -maxdepth (%u) value.",
126 cmdlineP->depth, cmdlineP->maxdepth);
127
128 if (!transSpec)
129 cmdlineP->transparent = NULL;
130
131 if (densitySpec) {
132 if (cmdlineP->density != PALM_DENSITY_LOW &&
133 cmdlineP->density != PALM_DENSITY_ONEANDAHALF &&
134 cmdlineP->density != PALM_DENSITY_DOUBLE &&
135 cmdlineP->density != PALM_DENSITY_TRIPLE &&
136 cmdlineP->density != PALM_DENSITY_QUADRUPLE)
137 pm_error("Invalid value for -density: %u. Valid values are "
138 "%u, %u, %u, %u and %u.", cmdlineP->density,
139 PALM_DENSITY_LOW, PALM_DENSITY_ONEANDAHALF,
140 PALM_DENSITY_DOUBLE, PALM_DENSITY_TRIPLE,
141 PALM_DENSITY_QUADRUPLE);
142 } else
143 cmdlineP->density = PALM_DENSITY_LOW;
144
145 if (cmdlineP->density != PALM_DENSITY_LOW && cmdlineP->withdummy)
146 pm_error("You can't specify -withdummy with -density value %u. "
147 "It is valid only with low density (%u)",
148 cmdlineP->density, PALM_DENSITY_LOW);
149
150 if (cmdlineP->withdummy && !cmdlineP->offset)
151 pm_error("-withdummy does not make sense without -offset");
152
153 if (scanline_compression + rle_compression + packbits_compression > 1)
154 pm_error("You may specify only one of -scanline_compression, "
155 "-rle_compression, and -packbits_compression");
156 else {
157 if (scanline_compression)
158 cmdlineP->compression = COMP_SCANLINE;
159 else if (rle_compression)
160 cmdlineP->compression = COMP_RLE;
161 else if (packbits_compression)
162 cmdlineP->compression = COMP_PACKBITS;
163 else
164 cmdlineP->compression = COMP_NONE;
165 }
166
167 if (argc-1 > 1)
168 pm_error("This program takes at most 1 argument: the file name. "
169 "You specified %u", argc-1);
170 else if (argc-1 > 0)
171 cmdlineP->inputFilespec = argv[1];
172 else
173 cmdlineP->inputFilespec = "-";
174 }
175
176
177
178 static xelval
179 scaleSample(pixval const arg,
180 pixval const oldMaxval,
181 pixval const newMaxval) {
182
183 return (arg * newMaxval + oldMaxval/2) / oldMaxval;
184 }
185
186
187
188 static void
189 determinePalmFormatPgm(xelval const maxval,
190 bool const bppSpecified,
191 unsigned int const bpp,
192 bool const maxBppSpecified,
193 unsigned int const maxBpp,
194 bool const wantCustomColormap,
195 enum CompressionType const compression,
196 bool const verbose,
197 unsigned int * const bppP) {
198
199 /* We can usually handle this one, but may not have enough pixels. So
200 check.
201 */
202
203 if (wantCustomColormap)
204 pm_error("You specified -colormap with a black and white input"
205 "image. -colormap is valid only with color.");
206 if (bppSpecified)
207 *bppP = bpp;
208 else if (maxBppSpecified && (maxval >= (1 << maxBpp)))
209 *bppP = maxBpp;
210 else if (compression != COMP_NONE && maxval > 255)
211 *bppP = 8;
212 else if (maxval > 16)
213 *bppP = 4;
214 else {
215 /* scale to minimum number of bpp needed */
216 unsigned int bpp;
217 for (bpp = 1; (1 << bpp) < maxval; bpp *= 2)
218 ;
219 *bppP = bpp;
220 }
221 if (verbose)
222 pm_message("output is grayscale %u bits-per-pixel", *bppP);
223 }
224
225
226
227 static void
228 validateImageAgainstStandardColormap(const Colormap * const colormapP,
229 xel ** const xels,
230 unsigned int const cols,
231 unsigned int const rows,
232 xelval const maxval) {
233 /*----------------------------------------------------------------------------
234 Abort program if the image xels[][] (which is 'cols' x 'rows') contains a
235 color not in the colormap *colormapP, giving an error message assuming the
236 user chose the standard Palm colormap.
237 -----------------------------------------------------------------------------*/
238 unsigned int row;
239
240 for (row = 0; row < rows; ++row) {
241 unsigned int col;
242
243 for (col = 0; col < cols; ++col) {
244 ColormapEntry const searchTarget =
245 palmcolor_mapEntryColorFmPixel(xels[row][col], maxval, 255);
246
247 ColormapEntry * const foundEntryP =
248 (bsearch(&searchTarget,
249 colormapP->color_entries, colormapP->ncolors,
250 sizeof(ColormapEntry), palmcolor_compare_colors));
251 if (!foundEntryP)
252 pm_error(
253 "A color in the input image is not in the standard Palm "
254 "8-bit color palette. Either adjust the colors in the "
255 "input with 'pnmremap' and the 'palmcolor8.map' file "
256 "(see manual) or specify -colormap or -depth=16");
257 }
258 }
259 }
260
261
262
263 static void
264 determinePalmFormatPpm(unsigned int const cols,
265 unsigned int const rows,
266 xelval const maxval,
267 xel ** const xels,
268 bool const bppSpecified,
269 unsigned int const bpp,
270 bool const maxBppSpecified,
271 unsigned int const maxBpp,
272 bool const wantCustomColormap,
273 enum CompressionType const compression,
274 bool const verbose,
275 unsigned int * const bppP,
276 bool * const directColorP,
277 Colormap ** const colormapPP) {
278
279 /* We don't attempt to identify PPM files that are actually
280 monochrome. So there are two options here: either 8-bit with a
281 colormap, either the standard one or a custom one, or 16-bit direct
282 color. In the colormap case, if 'wantCustomColormap' is true (not
283 recommended by Palm) we will put in our own colormap that has the
284 colors of the input image; otherwise we will select the default
285 Palm colormap and will fail if the input image has any colors that
286 are not in that map (user should use Pnmremap and the
287 palmcolor8.map file that comes with Netpbm to avoid this). We try
288 for colormapped first, since it works on more PalmOS devices.
289 */
290 if ((bppSpecified && bpp == 16) ||
291 (!bppSpecified && maxBppSpecified && maxBpp == 16)) {
292 /* we do the 16-bit direct color */
293 *directColorP = TRUE;
294 *colormapPP = NULL;
295 *bppP = 16;
296 } else if (!wantCustomColormap) {
297 /* colormapped with the standard colormap */
298 Colormap * colormapP;
299
300 if ((bppSpecified && bpp != 8) || (maxBppSpecified && maxBpp < 8))
301 pm_error("Must use depth of 8 for color Palm Bitmap without "
302 "custom color table.");
303 colormapP = palmcolor_build_default_8bit_colormap();
304 validateImageAgainstStandardColormap(colormapP,
305 xels, cols, rows, maxval);
306
307 *colormapPP = colormapP;
308 *bppP = 8;
309 *directColorP = FALSE;
310 if (verbose)
311 pm_message("Output is color with default colormap at 8 bpp");
312 } else {
313 /* colormapped with a custom colormap */
314 *colormapPP =
315 palmcolor_build_custom_8bit_colormap(xels, rows, cols, maxval);
316 for (*bppP = 1; (1 << *bppP) < (*colormapPP)->ncolors; *bppP *= 2);
317 if (bppSpecified) {
318 if (bpp >= *bppP)
319 *bppP = bpp;
320 else
321 pm_error("Too many colors for specified depth. "
322 "Specified depth is %u bits; would need %u to "
323 "represent the %u colors in the image. "
324 "Use pnmquant to reduce.",
325 maxBpp, *bppP, (*colormapPP)->ncolors);
326 } else if (maxBppSpecified && maxBpp < *bppP) {
327 pm_error("Too many colors for specified max depth. "
328 "Specified maximum is %u bits; would need %u to "
329 "represent the %u colors in the image. "
330 "Use pnmquant to reduce.",
331 maxBpp, *bppP, (*colormapPP)->ncolors);
332 } else if (compression != COMP_NONE && *bppP > 8) {
333 pm_error("Too many colors for a compressed image. "
334 "Maximum is 256; the image has %u",
335 (*colormapPP)->ncolors);
336 }
337 *directColorP = FALSE;
338 if (verbose)
339 pm_message("Output is color with custom colormap "
340 "with %u colors at %u bpp",
341 (*colormapPP)->ncolors, *bppP);
342 }
343 }
344
345
346
347 static void
348 determinePalmFormat(unsigned int const cols,
349 unsigned int const rows,
350 xelval const maxval,
351 int const format,
352 xel ** const xels,
353 bool const bppSpecified,
354 unsigned int const bpp,
355 bool const maxBppSpecified,
356 unsigned int const maxBpp,
357 bool const wantCustomColormap,
358 enum CompressionType const compression,
359 bool const verbose,
360 unsigned int * const bppP,
361 bool * const directColorP,
362 Colormap ** const colormapPP) {
363 /*----------------------------------------------------------------------------
364 Determine what kind of Palm output file to make.
365
366 Also compute the colormap, if there is to be one. This could be either one
367 we make up, that needs to go into the image, or a standard one.
368 -----------------------------------------------------------------------------*/
369 if (compression != COMP_NONE) {
370 if (bppSpecified && bpp > 8)
371 pm_error("You requested %u bits per pixel and compression. "
372 "This program does not know how to generate a "
373 "compressed image with more than 8 bits per pixel",
374 bpp);
375 if (maxBppSpecified && maxBpp > 8)
376 pm_error("You requested %u max bits per pixel and compression. "
377 "This program does not know how to generate a "
378 "compressed image with more than 8 bits per pixel",
379 maxBpp);
380 }
381 if (PNM_FORMAT_TYPE(format) == PBM_TYPE) {
382 if (wantCustomColormap)
383 pm_error("You specified -colormap with a black and white input "
384 "image. -colormap is valid only with color.");
385 if (bppSpecified)
386 *bppP = bpp;
387 else
388 *bppP = 1; /* no point in wasting bits */
389 *directColorP = FALSE;
390 *colormapPP = NULL;
391 if (verbose)
392 pm_message("output is black and white");
393 } else if (PNM_FORMAT_TYPE(format) == PGM_TYPE) {
394 determinePalmFormatPgm(maxval,
395 bppSpecified, bpp, maxBppSpecified, maxBpp,
396 wantCustomColormap, compression,
397 verbose,
398 bppP);
399
400 *directColorP = FALSE;
401 *colormapPP = NULL;
402 } else if (PNM_FORMAT_TYPE(format) == PPM_TYPE) {
403 determinePalmFormatPpm(cols, rows, maxval, xels, bppSpecified, bpp,
404 maxBppSpecified, maxBpp,
405 wantCustomColormap, compression, verbose,
406 bppP, directColorP, colormapPP);
407 } else {
408 pm_error("unknown format 0x%x on input file", (unsigned) format);
409 }
410
411 if (compression != COMP_NONE)
412 assert(*bppP <= 8);
413 }
414
415
416
417 static const char *
418 formatName(int const format) {
419
420 const char * retval;
421
422 switch(PNM_FORMAT_TYPE(format)) {
423 case PBM_TYPE: retval = "black and white"; break;
424 case PGM_TYPE: retval = "grayscale"; break;
425 case PPM_TYPE: retval = "color"; break;
426 default: retval = "???"; break;
427 }
428 return retval;
429 }
430
431
432
433 static void
434 findTransparentColor(const char * const colorSpec,
435 pixval const newMaxval,
436 bool const directColor,
437 pixval const maxval,
438 Colormap * const colormapP,
439 xel * const transcolorP,
440 unsigned int * const transindexP) {
441
442 *transcolorP = ppm_parsecolor(colorSpec, maxval);
443 if (!directColor) {
444 ColormapEntry const searchTarget =
445 palmcolor_mapEntryColorFmPixel(*transcolorP, maxval, newMaxval);
446 ColormapEntry * const foundEntryP =
447 (bsearch(&searchTarget,
448 colormapP->color_entries, colormapP->ncolors,
449 sizeof(ColormapEntry), palmcolor_compare_colors));
450 if (!foundEntryP) {
451 pm_error("Specified transparent color %s not found "
452 "in colormap.", colorSpec);
453 } else
454 *transindexP = (*foundEntryP >> 24) & 0xFF;
455 }
456 }
457
458
459
460 static unsigned int
461 bitmapVersion(unsigned int const bpp,
462 bool const colormapped,
463 bool const transparent,
464 enum CompressionType const compression,
465 unsigned int const density) {
466 /*----------------------------------------------------------------------------
467 Return the version number of the oldest version that can represent
468 the specified attributes.
469 -----------------------------------------------------------------------------*/
470 unsigned int version;
471 /* we need Version 1 if we use more than 1 bpp,
472 Version 2 if we use compression or transparency,
473 Version 3 if density is 108 or higher
474 */
475 if (density > PALM_DENSITY_LOW)
476 version = 3;
477 else if (transparent || compression != COMP_NONE)
478 version = 2;
479 else if (bpp > 1 || colormapped)
480 version = 1;
481 else
482 version = 0;
483
484 return version;
485 }
486
487
488
489 static void
490 writeCommonHeader(unsigned int const cols,
491 unsigned int const rows,
492 unsigned int const rowbytes,
493 enum CompressionType const compression,
494 bool const colormapped,
495 bool const transparent,
496 bool const directColor,
497 unsigned int const bpp,
498 unsigned int const version) {
499 /*----------------------------------------------------------------------------
500 Write the first 10 bytes of the Palm Bitmap header.
501 These are common to all encodings (versions 0, 1, 2 and 3).
502 -----------------------------------------------------------------------------*/
503 unsigned short flags;
504
505 if (cols > USHRT_MAX)
506 pm_error("Too many columns for Palm Bitmap: %u", cols);
507 pm_writebigshort(stdout, cols); /* width */
508 if (rows > USHRT_MAX)
509 pm_error("Too many rows for Palm Bitmap: %u", rows);
510 pm_writebigshort(stdout, rows); /* height */
511 if (rowbytes > USHRT_MAX)
512 pm_error("Too many bytes per row for Palm Bitmap: %u", rowbytes);
513 pm_writebigshort(stdout, rowbytes);
514
515 flags = 0; /* initial value */
516 if (compression != COMP_NONE)
517 flags |= PALM_IS_COMPRESSED_FLAG;
518 if (colormapped)
519 flags |= PALM_HAS_COLORMAP_FLAG;
520 if (transparent)
521 flags |= PALM_HAS_TRANSPARENCY_FLAG;
522 if (directColor)
523 flags |= PALM_DIRECT_COLOR_FLAG;
524 pm_writebigshort(stdout, flags);
525 assert(bpp <= UCHAR_MAX);
526 fputc(bpp, stdout);
527
528 fputc(version, stdout);
529 }
530
531
532
533 static unsigned char
534 compressionFieldValue(enum CompressionType const compression) {
535
536 unsigned char retval;
537
538 switch (compression) {
539 case COMP_SCANLINE:
540 retval = PALM_COMPRESSION_SCANLINE;
541 break;
542 case COMP_RLE:
543 retval = PALM_COMPRESSION_RLE;
544 break;
545 case COMP_PACKBITS:
546 retval = PALM_COMPRESSION_PACKBITS;
547 break;
548 case COMP_NONE:
549 retval = 0x00; /* empty */
550 break;
551 }
552 return retval;
553 }
554
555
556
557 static void
558 writeRemainingHeaderLow(unsigned int const nextDepthOffset,
559 unsigned int const transindex,
560 enum CompressionType const compression,
561 unsigned int const bpp) {
562 /*----------------------------------------------------------------------------
563 Write last 6 bytes of a low density Palm Bitmap header.
564 -----------------------------------------------------------------------------*/
565 if (nextDepthOffset > USHRT_MAX)
566 pm_error("Image too large for Palm Bitmap");
567
568 pm_writebigshort(stdout, nextDepthOffset);
569
570 if (bpp != 16) {
571 assert(transindex <= UCHAR_MAX);
572 fputc(transindex, stdout); /* transparent index */
573 } else
574 fputc(0, stdout); /* the DirectInfoType will hold this info */
575
576 fputc(compressionFieldValue(compression), stdout);
577
578 pm_writebigshort(stdout, 0); /* reserved by Palm */
579 }
580
581
582
583 static void
584 writeRemainingHeaderHigh(unsigned int const bpp,
585 enum CompressionType const compression,
586 unsigned int const density,
587 xelval const maxval,
588 bool const transparent,
589 xel const transcolor,
590 unsigned int const transindex,
591 unsigned int const nextBitmapOffset) {
592 /*----------------------------------------------------------------------------
593 Write last 16 bytes of a high density Palm Bitmap header.
594 -----------------------------------------------------------------------------*/
595 if ((nextBitmapOffset >> 31) > 1)
596 pm_error("Image too large for Palm Bitmap. nextBitmapOffset "
597 "value doesn't fit in 4 bytes");
598
599 fputc(0x18, stdout); /* size of this high density header */
600
601 if (bpp != 16)
602 fputc(PALM_FORMAT_INDEXED, stdout);
603 else
604 fputc(PALM_FORMAT_565, stdout);
605
606 fputc(0x00, stdout); /* unused */
607
608 fputc(compressionFieldValue(compression), stdout);
609
610 pm_writebigshort(stdout, density);
611
612 if (transparent) {
613 if (bpp == 16) {
614 /* Blind guess here */
615 fputc(0, stdout);
616 fputc(scaleSample(PPM_GETR(transcolor), maxval, 255), stdout);
617 fputc(scaleSample(PPM_GETG(transcolor), maxval, 255), stdout);
618 fputc(scaleSample(PPM_GETB(transcolor), maxval, 255), stdout);
619 } else {
620 assert(transindex <= UCHAR_MAX);
621 fputc(0, stdout);
622 fputc(0, stdout);
623 fputc(0, stdout);
624 fputc(transindex, stdout); /* transparent index */
625 }
626 } else
627 pm_writebiglong(stdout, 0);
628
629 pm_writebiglong(stdout, nextBitmapOffset);
630 }
631
632
633
634 static void
635 writeDummy() {
636 /*----------------------------------------------------------------------------
637 Write a dummy Palm Bitmap header. This is a 16 byte header, of
638 type version 1 and with (only) pixelSize set to 0xFF.
639
640 An old viewer will see this as invalid because of the pixelSize, and stop
641 reading the stream. A new viewer will recognize this for what it is
642 (a dummy header designed to stop old viewers from reading further in
643 the stream) and continue reading the stream. Presumably, what follows
644 in the stream is understandable by a new viewer, but would confuse an
645 old one.
646 -----------------------------------------------------------------------------*/
647 pm_writebiglong(stdout, 0x00);
648 pm_writebiglong(stdout, 0x00);
649 fputc(0xFF, stdout); /* pixelSize */
650 fputc(0x01, stdout); /* version */
651 pm_writebigshort(stdout, 0x00);
652 pm_writebiglong(stdout, 0x00);
653 }
654
655
656
657 static void
658 writeColormap(bool const explicitColormap,
659 Colormap * const colormapP,
660 bool const directColor,
661 unsigned int const bpp,
662 bool const transparent,
663 xel const transcolor,
664 xelval const maxval,
665 unsigned int const version) {
666
667 /* if there's a colormap, write it out */
668 if (explicitColormap) {
669 unsigned int row;
670 if (!colormapP)
671 pm_error("Internal error: user specified -colormap, but we did "
672 "not generate a colormap.");
673 qsort(colormapP->color_entries, colormapP->ncolors,
674 sizeof(ColormapEntry), palmcolor_compare_indices);
675 pm_writebigshort( stdout, colormapP->ncolors );
676 for (row = 0; row < colormapP->ncolors; ++row)
677 pm_writebiglong (stdout, colormapP->color_entries[row]);
678 }
679
680 if (directColor && (version < 3)) {
681 /* write the DirectInfoType (8 bytes) */
682 if (bpp == 16) {
683 fputc(5, stdout); /* # of bits of red */
684 fputc(6, stdout); /* # of bits of green */
685 fputc(5, stdout); /* # of bits of blue */
686 fputc(0, stdout); /* reserved by Palm */
687 } else
688 pm_error("Don't know how to create %u bit DirectColor bitmaps.",
689 bpp);
690 if (transparent) {
691 fputc(0, stdout);
692 fputc(scaleSample(PPM_GETR(transcolor) , maxval, 255), stdout);
693 fputc(scaleSample(PPM_GETG(transcolor) , maxval, 255), stdout);
694 fputc(scaleSample(PPM_GETB(transcolor) , maxval, 255), stdout);
695 } else
696 pm_writebiglong(stdout, 0); /* no transparent color */
697 }
698 }
699
700
701
702 static void
703 computeRawRowDirectColor(const xel * const xelrow,
704 unsigned int const cols,
705 xelval const maxval,
706 unsigned char * const rowdata) {
707 /*----------------------------------------------------------------------------
708 Compute a row of Palm data in raw (uncompressed) form for an image that
709 uses direct color (really, true color: each pixel contains RGB intensities
710 as distinct R, G, and B numbers).
711
712 In this format, each pixel is 16 bits: 5 red, 6 green, 5 blue.
713
714 'xelrow' is the image contents of row. It is 'cols' columns wide and
715 samples are based on maxval 'maxval'.
716
717 Put the output data at 'rowdata'.
718 -----------------------------------------------------------------------------*/
719 unsigned int col;
720 unsigned char * outCursor;
721
722 for (col = 0, outCursor = &rowdata[0]; col < cols; ++col) {
723 unsigned int const color =
724 (scaleSample(PPM_GETR(xelrow[col]), maxval, 31) << 11) |
725 (scaleSample(PPM_GETG(xelrow[col]), maxval, 63) << 5) |
726 (scaleSample(PPM_GETB(xelrow[col]), maxval, 31) << 0);
727
728 *outCursor++ = (color >> 8) & 0xFF;
729 *outCursor++ = color & 0xFF;
730 }
731 }
732
733
734
735 static void
736 computeRawRowNonDirect(const xel * const xelrow,
737 unsigned int const cols,
738 xelval const maxval,
739 unsigned int const bpp,
740 Colormap * const colormapP,
741 unsigned int const newMaxval,
742 unsigned char * const rowdata) {
743 /*----------------------------------------------------------------------------
744 Compute a row of Palm data in raw (uncompressed) form for an image that
745 does not have a raster whose elements are explicit R, G, and B
746 intensities.
747
748 If 'colormapP' is non-null, the pixel is an index into that colormap.
749 'newMaxval' is meaningless.
750
751 If 'colormapP' is null, the pixel is a grayscale intensity, on a scale with
752 maximum value 'newMaxval'. (N.B. this is really direct color, but for some
753 reason it's historically lumped in with the paletted formats).
754
755 'xelrow' is the image contents of row. It is 'cols' columns wide and
756 samples are based on maxval 'maxval'.
757
758 Put the output data at 'rowdata', using 'bpp' bits per pixel.
759 -----------------------------------------------------------------------------*/
760 unsigned int col;
761 unsigned char * outCursor;
762 /* Points to next slot in 'rowdata' we will fill */
763 unsigned char outbyte;
764 /* Accumulated bits to be output */
765 unsigned char outbit;
766 /* The lowest bit number we want to access for this pixel */
767
768 outbyte = 0x00; /* initial value */
769 outCursor = &rowdata[0]; /* Start at the beginning of the row */
770
771 for (outbit = 8 - bpp, col = 0; col < cols; ++col) {
772 unsigned int color;
773 if (!colormapP) {
774 /* we assume grayscale, and use simple scaling */
775 color = (PNM_GET1(xelrow[col]) * newMaxval)/maxval;
776 if (color > newMaxval)
777 pm_error("oops. Bug in color re-calculation code. "
778 "color of %u.", color);
779 color = newMaxval - color; /* note grayscale maps are inverted */
780 } else {
781 ColormapEntry const searchTarget =
782 palmcolor_mapEntryColorFmPixel(xelrow[col], maxval, 255);
783 ColormapEntry * const foundEntryP =
784 bsearch(&searchTarget,
785 colormapP->color_entries,
786 colormapP->ncolors,
787 sizeof(ColormapEntry),
788 palmcolor_compare_colors);
789 if (!foundEntryP) {
790 pm_error("INTERNAL ERROR: "
791 "Color (%u,%u,%u) not found in colormap, "
792 "though it was supposedly there before",
793 PPM_GETR(xelrow[col]),
794 PPM_GETG(xelrow[col]),
795 PPM_GETB(xelrow[col]));
796 }
797 color = (*foundEntryP >> 24) & 0xFF;
798 }
799
800 if (color > newMaxval)
801 pm_error("oops. Bug in color re-calculation code. "
802 "color of %u.", color);
803 outbyte |= (color << outbit);
804 if (outbit == 0) {
805 /* Bit buffer is full. Flush to to rowdata. */
806 *outCursor++ = outbyte;
807 outbyte = 0x00;
808 outbit = 8 - bpp;
809 } else
810 outbit -= bpp;
811 }
812 if ((cols % (8 / bpp)) != 0) {
813 /* Flush bits remaining in the bit buffer to rowdata */
814 *outCursor++ = outbyte;
815 }
816 }
817
818
819
820 typedef struct {
821 /*----------------------------------------------------------------------------
822 A buffer to which one can write bytes sequentially.
823 -----------------------------------------------------------------------------*/
824 char * buffer;
825 unsigned int allocatedSize;
826 unsigned int occupiedSize;
827 } SeqBuffer;
828
829
830 static void
831 seqBuffer_create(SeqBuffer ** const bufferPP) {
832
833 SeqBuffer * bufferP;
834
835 MALLOCVAR_NOFAIL(bufferP);
836
837 bufferP->allocatedSize = 4096;
838 MALLOCARRAY(bufferP->buffer, bufferP->allocatedSize);
839 if (bufferP == NULL)
840 pm_error("Unable to allocate %u bytes of buffer",
841 bufferP->allocatedSize);
842 bufferP->occupiedSize = 0;
843
844 *bufferPP = bufferP;
845 }
846
847
848
849 static void
850 seqBuffer_destroy(SeqBuffer * const bufferP) {
851
852 free(bufferP->buffer);
853 free(bufferP);
854 }
855
856
857
858 static void
859 seqBuffer_addByte(SeqBuffer * const bufferP,
860 unsigned char const newByte) {
861 /*-----------------------------------------------------------------------------
862 Append one byte to buffer, expanding with realloc() whenever necessary.
863
864 Buffer is initially 4096 bytes. It is doubled with each expansion.
865 A combination of large image size (maximum 65535 x 65535), high
866 resolution (each pixel can occupy more than one byte) and poor
867 compression can lead to an arithmetic overflow.
868 Abort with error if an arithmetic overflow is detected during doubling.
869 -----------------------------------------------------------------------------*/
870 assert(bufferP->allocatedSize >= bufferP->occupiedSize);
871
872 if (bufferP->allocatedSize == bufferP->occupiedSize) {
873 unsigned int const newSize = bufferP->allocatedSize * 2;
874
875 if (newSize <= bufferP->allocatedSize)
876 pm_error("Image too large. Arithmetic overflow trying to "
877 "expand buffer beyond %u bytes.",
878 bufferP->allocatedSize);
879
880 REALLOCARRAY(bufferP->buffer, newSize);
881 if (bufferP->buffer == NULL)
882 pm_error("Couldn't (re)allocate %u bytes of memory "
883 "for buffer.", newSize);
884
885 bufferP->allocatedSize = newSize;
886 }
887 bufferP->buffer[bufferP->occupiedSize++] = newByte;
888 }
889
890
891
892 static unsigned int
893 seqBuffer_length(SeqBuffer * const bufferP) {
894 return bufferP->occupiedSize;
895 }
896
897
898
899 static void
900 seqBuffer_writeOut(SeqBuffer * const bufferP,
901 FILE * const fileP) {
902
903 size_t bytesWritten;
904
905 bytesWritten = fwrite(bufferP->buffer, sizeof(char),
906 bufferP->occupiedSize, fileP);
907
908 if (bytesWritten != bufferP->occupiedSize)
909 pm_error("fwrite() failed to write out the buffer.");
910 }
911
912
913
914 static void
915 copyRowToBuffer(const unsigned char * const rowdata,
916 unsigned int const rowbytes,
917 SeqBuffer * const rasterBufferP) {
918
919 unsigned int pos;
920 for (pos = 0; pos < rowbytes; ++pos)
921 seqBuffer_addByte(rasterBufferP, rowdata[pos]);
922 }
923
924
925
926 static void
927 scanlineCompressAndBufferRow(const unsigned char * const rowdata,
928 unsigned int const rowbytes,
929 SeqBuffer * const rasterBufferP,
930 const unsigned char * const lastrow) {
931 /*----------------------------------------------------------------------------
932 Take the raw Palm Bitmap row 'rowdata', which is 'rowbytes'
933 columns, and add the scanline-compressed representation of it to
934 the buffer with handle 'rasterBufferP'.
935
936 'lastrow' is the raw contents of the row immediately before the one
937 we're compressing -- i.e. we compress with respect to that row. This
938 function does not work on the first row of an image.
939 -----------------------------------------------------------------------------*/
940 unsigned int pos;
941
942 for (pos = 0; pos < rowbytes; pos += 8) {
943 unsigned int const limit = MIN(rowbytes - pos, 8);
944
945 unsigned char map;
946 /* mask indicating which of the next 8 pixels are
947 different from the previous row, and therefore present
948 in the file immediately following the map byte.
949 */
950 unsigned char differentPixels[8];
951 unsigned char *outptr;
952 unsigned char outbit;
953
954 for (outbit = 0, map = 0x00, outptr = differentPixels;
955 outbit < limit;
956 ++outbit) {
957 if (!lastrow
958 || (lastrow[pos + outbit] != rowdata[pos + outbit])) {
959 map |= (1 << (7 - outbit));
960 *outptr++ = rowdata[pos + outbit];
961 }
962 }
963
964 seqBuffer_addByte(rasterBufferP, map);
965 {
966 unsigned int j;
967 for (j = 0; j < (outptr - differentPixels); ++j)
968 seqBuffer_addByte(rasterBufferP, differentPixels[j]);
969 }
970 }
971 }
972
973
974
975 static void
976 rleCompressAndBufferRow(const unsigned char * const rowdata,
977 unsigned int const rowbytes,
978 SeqBuffer * const rasterBufferP) {
979 /*----------------------------------------------------------------------------
980 Take the raw Palm Bitmap row 'rowdata', which is 'rowbytes' bytes,
981 and add the rle-compressed representation of it to the buffer with
982 handle 'rasterBufferP'.
983 -----------------------------------------------------------------------------*/
984 unsigned int pos;
985
986 /* we output a count of the number of bytes a value is
987 repeated, followed by that byte value
988 */
989 pos = 0;
990 while (pos < rowbytes) {
991 unsigned int repeatcount;
992 for (repeatcount = 1;
993 repeatcount < (rowbytes - pos) && repeatcount < 255;
994 ++repeatcount)
995 if (rowdata[pos + repeatcount] != rowdata[pos])
996 break;
997
998 seqBuffer_addByte(rasterBufferP, repeatcount);
999 seqBuffer_addByte(rasterBufferP, rowdata[pos]);
1000 pos += repeatcount;
1001 }
1002 }
1003
1004
1005
1006 static void
1007 packbitsCompressAndBufferRow(const unsigned char * const rowdata,
1008 unsigned int const rowbytes,
1009 SeqBuffer * const rasterBufferP) {
1010 /*----------------------------------------------------------------------------
1011 Take the raw Palm Bitmap row 'rowdata', which is 'rowbytes' bytes, and
1012 add the packbits-compressed representation of it to the buffer
1013 with handle 'rasterBufferP'.
1014 -----------------------------------------------------------------------------*/
1015 unsigned char * compressedData;
1016 size_t compressedDataCt;
1017 unsigned int byteCt;
1018
1019 pm_rlenc_allocoutbuf(&compressedData, rowbytes, PM_RLE_PACKBITS);
1020 pm_rlenc_compressbyte(rowdata, compressedData, PM_RLE_PACKBITS,
1021 rowbytes, &compressedDataCt);
1022
1023 for (byteCt = 0; byteCt < compressedDataCt; ++byteCt)
1024 seqBuffer_addByte(rasterBufferP, compressedData[byteCt]);
1025
1026 free(compressedData);
1027 }
1028
1029
1030
1031 static void
1032 bufferRowFromRawRowdata(const unsigned char * const rowdata,
1033 unsigned int const rowbytes,
1034 enum CompressionType const compression,
1035 const unsigned char * const lastrow,
1036 SeqBuffer * const rasterBufferP) {
1037 /*----------------------------------------------------------------------------
1038 Starting with a raw (uncompressed) Palm raster line, do the
1039 compression identified by 'compression' and add the compressed row
1040 to the buffer with handle 'rasterBufferP'.
1041
1042 If 'compression' indicates scanline compression, 'lastrow' is the
1043 row immediately preceding this one in the image (and this function
1044 doesn't work on the first row of an image). Otherwise, 'lastrow'
1045 is meaningless.
1046 -----------------------------------------------------------------------------*/
1047 switch (compression) {
1048 case COMP_NONE:
1049 copyRowToBuffer(rowdata, rowbytes, rasterBufferP);
1050 break;
1051 case COMP_SCANLINE:
1052 scanlineCompressAndBufferRow(rowdata, rowbytes, rasterBufferP,
1053 lastrow);
1054 break;
1055 case COMP_RLE:
1056 rleCompressAndBufferRow(rowdata, rowbytes, rasterBufferP);
1057 break;
1058 case COMP_PACKBITS:
1059 packbitsCompressAndBufferRow(rowdata, rowbytes, rasterBufferP);
1060 break;
1061 }
1062 }
1063
1064
1065
1066 static void
1067 bufferRow(const xel * const xelrow,
1068 unsigned int const cols,
1069 xelval const maxval,
1070 unsigned int const rowbytes,
1071 unsigned int const bpp,
1072 unsigned int const newMaxval,
1073 enum CompressionType const compression,
1074 bool const directColor,
1075 Colormap * const colormapP,
1076 unsigned char * const rowdata,
1077 unsigned char * const lastrow,
1078 SeqBuffer * const rasterBufferP) {
1079 /*----------------------------------------------------------------------------
1080 Add a row of the Palm Bitmap raster to buffer 'rasterBufferP'.
1081
1082 'xelrow' is the image contents of row. It is 'cols' columns wide and
1083 samples are based on maxval 'maxval'.
1084
1085 If 'compression' indicates scanline compression, 'lastrow' is the
1086 row immediately preceding this one in the image (and this function
1087 doesn't work on the first row of an image). Otherwise, 'lastrow'
1088 is meaningless.
1089
1090 'rowdata' is a work buffer 'rowbytes' in size.
1091 -----------------------------------------------------------------------------*/
1092 if (directColor)
1093 computeRawRowDirectColor(xelrow, cols, maxval, rowdata);
1094 else
1095 computeRawRowNonDirect(xelrow, cols, maxval, bpp, colormapP, newMaxval,
1096 rowdata);
1097
1098 bufferRowFromRawRowdata(rowdata, rowbytes, compression,
1099 lastrow, rasterBufferP);
1100 }
1101
1102
1103
1104 static void
1105 bufferRaster(xel ** const xels,
1106 unsigned int const cols,
1107 unsigned int const rows,
1108 xelval const maxval,
1109 unsigned int const rowbytes,
1110 unsigned int const bpp,
1111 unsigned int const newMaxval,
1112 enum CompressionType const compression,
1113 bool const directColor,
1114 Colormap * const colormapP,
1115 SeqBuffer ** const rasterBufferPP) {
1116
1117 unsigned char * rowdata;
1118 unsigned char * lastrow;
1119 unsigned int row;
1120
1121 seqBuffer_create(rasterBufferPP);
1122
1123 MALLOCARRAY_NOFAIL(rowdata, rowbytes);
1124 if (compression == COMP_SCANLINE)
1125 MALLOCARRAY_NOFAIL(lastrow, rowbytes);
1126 else
1127 lastrow = NULL;
1128
1129 /* clear pad bytes to suppress valgrind error */
1130 rowdata[rowbytes - 1] = rowdata[rowbytes - 2] = 0x00;
1131
1132 /* And write out the data. */
1133 for (row = 0; row < rows; ++row) {
1134 bufferRow(xels[row], cols, maxval, rowbytes, bpp, newMaxval,
1135 compression,
1136 directColor, colormapP, rowdata, row > 0 ? lastrow : NULL,
1137 *rasterBufferPP);
1138
1139 if (compression == COMP_SCANLINE)
1140 memcpy(lastrow, rowdata, rowbytes);
1141 }
1142 free(rowdata);
1143 if (compression == COMP_SCANLINE)
1144 free(lastrow);
1145 }
1146
1147
1148
1149 static void
1150 computeOffsetStuff(bool const offsetWanted,
1151 unsigned int const version,
1152 bool const directColor,
1153 enum CompressionType const compression,
1154 bool const colormapped,
1155 unsigned int const colormapColorCount,
1156 unsigned int const sizePlusRasterSize,
1157 unsigned int * const nextDepthOffsetP,
1158 unsigned int * const nextBitmapOffsetP,
1159 unsigned int * const padBytesRequiredP) {
1160
1161 if (offsetWanted) {
1162 /* Offset is measured in 4-byte words (double words in
1163 Intel/Microsoft terminology). Account for header,
1164 colormap, and raster size and round up
1165 */
1166 unsigned int const headerSize = ((version < 3) ? 16 : 24);
1167 unsigned int const colormapSize =
1168 (colormapped ? (2 + colormapColorCount * 4) : 0);
1169 if (version < 3) {
1170 unsigned int const directSize =
1171 (directColor && version < 3) ? 8 : 0;
1172 if (compression != COMP_NONE && sizePlusRasterSize > USHRT_MAX)
1173 pm_error("Oversized compressed bitmap: %u bytes",
1174 sizePlusRasterSize);
1175 *padBytesRequiredP = 4 - (sizePlusRasterSize + headerSize +
1176 directSize + colormapSize) % 4;
1177 *nextDepthOffsetP =
1178 (sizePlusRasterSize + headerSize +
1179 directSize + colormapSize + *padBytesRequiredP) / 4;
1180 } else {
1181 if (compression != COMP_NONE && (sizePlusRasterSize >> 31) > 1)
1182 pm_error("Oversized compressed bitmap: %u bytes",
1183 sizePlusRasterSize);
1184 /* Does version 3 need padding? Probably won't hurt */
1185 *padBytesRequiredP = 4 - (sizePlusRasterSize + headerSize +
1186 colormapSize) % 4;
1187 *nextBitmapOffsetP = sizePlusRasterSize + headerSize +
1188 colormapSize + *padBytesRequiredP;
1189 }
1190 } else {
1191 *padBytesRequiredP = 0;
1192 *nextDepthOffsetP = 0;
1193 *nextBitmapOffsetP = 0;
1194 }
1195 }
1196
1197
1198
1199 static void
1200 writeRasterSize(unsigned int const sizePlusRasterSize,
1201 unsigned int const version,
1202 FILE * const fileP) {
1203 /*----------------------------------------------------------------------------
1204 Write to file 'fileP' a raster size field for a Palm Bitmap version
1205 'version' header, indicating 'sizePlusRasterSize' bytes.
1206 -----------------------------------------------------------------------------*/
1207 if (version < 3)
1208 pm_writebigshort(fileP, sizePlusRasterSize);
1209 else
1210 pm_writebiglong(fileP, sizePlusRasterSize);
1211 }
1212
1213
1214
1215 static void
1216 writeBitmap(xel ** const xels,
1217 unsigned int const cols,
1218 unsigned int const rows,
1219 xelval const maxval,
1220 unsigned int const rowbytes,
1221 unsigned int const bpp,
1222 unsigned int const newMaxval,
1223 enum CompressionType const compression,
1224 bool const transparent,
1225 bool const directColor,
1226 bool const offsetWanted,
1227 bool const colormapped,
1228 Colormap * const colormapP,
1229 unsigned int const transindex,
1230 xel const transcolor,
1231 unsigned int const version,
1232 unsigned int const density,
1233 bool const withdummy) {
1234
1235 unsigned int sizePlusRasterSize;
1236 unsigned int nextDepthOffset;
1237 unsigned int nextBitmapOffset;
1238 /* Offset from the beginning of the image we write to the beginning
1239 of the next one, assuming user writes another one following this
1240 one.
1241 nextDepthOffset is used in encodings 1, 2 and is in 4 byte words
1242 nextBitmapOffset is used in encoding 3, is in 4 bytes
1243 */
1244 unsigned int padBytesRequired;
1245 /* Number of bytes of padding we need to put after the image in
1246 order to align properly for User to add the next image to the
1247 stream.
1248 */
1249 SeqBuffer * rasterBufferP;
1250
1251 writeCommonHeader(cols, rows, rowbytes, compression, colormapped,
1252 transparent, directColor, bpp, version);
1253
1254 bufferRaster(xels, cols, rows, maxval, rowbytes, bpp, newMaxval,
1255 compression, directColor, colormapP, &rasterBufferP);
1256
1257 /* rasterSize itself takes 2 or 4 bytes */
1258 if (version < 3)
1259 sizePlusRasterSize = 2 + seqBuffer_length(rasterBufferP);
1260 else
1261 sizePlusRasterSize = 4 + seqBuffer_length(rasterBufferP);
1262
1263 computeOffsetStuff(offsetWanted, version, directColor, compression,
1264 colormapped, colormapped ? colormapP->ncolors : 0,
1265 sizePlusRasterSize,
1266 &nextDepthOffset, &nextBitmapOffset,
1267 &padBytesRequired);
1268
1269 if (version < 3)
1270 writeRemainingHeaderLow(nextDepthOffset, transindex, compression, bpp);
1271 else
1272 writeRemainingHeaderHigh(bpp, compression, density,
1273 maxval, transparent, transcolor,
1274 transindex, nextBitmapOffset);
1275
1276 writeColormap(colormapped, colormapP, directColor, bpp,
1277 transparent, transcolor, maxval, version);
1278
1279 if (compression != COMP_NONE)
1280 writeRasterSize(sizePlusRasterSize, version, stdout);
1281
1282 seqBuffer_writeOut(rasterBufferP, stdout);
1283
1284 seqBuffer_destroy(rasterBufferP);
1285
1286 {
1287 unsigned int i;
1288 for (i = 0; i < padBytesRequired; ++i)
1289 fputc(0x00, stdout);
1290 }
1291
1292 if (withdummy)
1293 writeDummy();
1294 }
1295
1296
1297
1298 int
1299 main( int argc, const char **argv ) {
1300 struct CmdlineInfo cmdline;
1301 unsigned int version;
1302 FILE* ifP;
1303 xel** xels;
1304 xel transcolor;
1305 unsigned int transindex;
1306 int rows, cols;
1307 unsigned int rowbytes;
1308 xelval maxval;
1309 int format;
1310 unsigned int bpp;
1311 bool directColor;
1312 unsigned int newMaxval;
1313 Colormap * colormapP;
1314
1315 pm_proginit(&argc, argv);
1316
1317 parseCommandLine(argc, argv, &cmdline);
1318
1319 ifP = pm_openr(cmdline.inputFilespec);
1320
1321 xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);
1322 pm_close(ifP);
1323
1324 if (cmdline.verbose)
1325 pm_message("Input is %ux%u %s, maxval %u",
1326 cols, rows, formatName(format), maxval);
1327
1328 determinePalmFormat(cols, rows, maxval, format, xels,
1329 cmdline.depthSpec, cmdline.depth,
1330 cmdline.maxdepthSpec, cmdline.maxdepth,
1331 cmdline.colormap, cmdline.compression, cmdline.verbose,
1332 &bpp, &directColor, &colormapP);
1333
1334 newMaxval = (1 << bpp) - 1;
1335
1336 if (cmdline.transparent)
1337 findTransparentColor(cmdline.transparent, newMaxval, directColor,
1338 maxval, colormapP, &transcolor, &transindex);
1339 else
1340 transindex = 0;
1341
1342 rowbytes = ((cols + (16 / bpp -1)) / (16 / bpp)) * 2;
1343 /* bytes per row - always a word boundary */
1344
1345 version = bitmapVersion(bpp, cmdline.colormap, !!cmdline.transparent,
1346 cmdline.compression, cmdline.density);
1347
1348 writeBitmap(xels, cols, rows, maxval,
1349 rowbytes, bpp, newMaxval, cmdline.compression,
1350 !!cmdline.transparent, directColor, cmdline.offset,
1351 cmdline.colormap, colormapP, transindex, transcolor,
1352 version, cmdline.density, cmdline.withdummy);
1353
1354 return 0;
1355 }
1356
1357
1358