Debian Codegraph @

converter/other/pngtopam.c

raw symbols SCIP
1 /*
2 ** Copyright (C) 1995,1998 by Alexander Lehmann <[email protected]>
3 ** and Willem van Schaik <[email protected]>
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 **
12 ** modeled after giftopnm by David Koblas and
13 ** with lots of bits pasted from libpng.txt by Guy Eric Schalnat
14 */
15
16 #ifndef PNMTOPNG_WARNING_LEVEL
17 # define PNMTOPNG_WARNING_LEVEL 0 /* use 0 for backward compatibility, */
18 #endif /* 2 for warnings (1 == error) */
19
20
21 #include <assert.h>
22 #include <math.h>
23 #include <float.h>
24 #include <png.h>
25 #include <zlib.h>
26
27
28 #include "pm_c_util.h"
29 #include "mallocvar.h"
30 #include "nstring.h"
31 #include "shhopt.h"
32 #include "pam.h"
33 #include "pngx.h"
34
35 enum AlphaHandling {ALPHA_NONE, ALPHA_ONLY, ALPHA_MIX, ALPHA_IN};
36
37 typedef struct {
38 bool needCorrection;
39 double gamma;
40 } GammaCorrection;
41
42 struct CmdlineInfo {
43 /* All the information the user supplied in the command line,
44 in a form easy for the program to use.
45 */
46 const char * inputFileName; /* '-' if stdin */
47 unsigned int verbose;
48 enum AlphaHandling alpha;
49 const char * background;
50 unsigned int gammaSpec;
51 float gamma;
52 const char * text;
53 unsigned int time;
54 unsigned int byrow;
55 };
56
57
58 static bool verbose;
59
60
61
62 static void
63 parseCommandLine(int argc,
64 const char ** argv,
65 struct CmdlineInfo * cmdlineP ) {
66 /*----------------------------------------------------------------------------
67 Parse program command line described in Unix standard form by argc
68 and argv. Return the information in the options as *cmdlineP.
69
70 If command line is internally inconsistent (invalid options, etc.),
71 issue error message to stderr and abort program.
72
73 Note that the strings we return are stored in the storage that
74 was passed to us as the argv array. We also trash *argv.
75 -----------------------------------------------------------------------------*/
76 optEntry * option_def; /* Used by OPTENT3 */
77 optStruct3 opt;
78
79 unsigned int option_def_index;
80
81 unsigned int alphaSpec, alphapamSpec, mixSpec,
82 backgroundSpec, textSpec;
83
84 MALLOCARRAY(option_def, 100);
85
86 option_def_index = 0; /* incremented by OPTENT3 */
87 OPTENT3(0, "verbose", OPT_FLAG, NULL,
88 &cmdlineP->verbose, 0);
89 OPTENT3(0, "alpha", OPT_FLAG, NULL,
90 &alphaSpec, 0);
91 OPTENT3(0, "alphapam", OPT_FLAG, NULL,
92 &alphapamSpec, 0);
93 OPTENT3(0, "mix", OPT_FLAG, NULL,
94 &mixSpec, 0);
95 OPTENT3(0, "background", OPT_STRING, &cmdlineP->background,
96 &backgroundSpec, 0);
97 OPTENT3(0, "gamma", OPT_FLOAT, &cmdlineP->gamma,
98 &cmdlineP->gammaSpec, 0);
99 OPTENT3(0, "text", OPT_STRING, &cmdlineP->text,
100 &textSpec, 0);
101 OPTENT3(0, "time", OPT_FLAG, NULL,
102 &cmdlineP->time, 0);
103 OPTENT3(0, "byrow", OPT_FLAG, NULL,
104 &cmdlineP->byrow, 0);
105
106 opt.opt_table = option_def;
107 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
108 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
109
110 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
111 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
112
113
114 if (alphaSpec + mixSpec + alphapamSpec > 1)
115 pm_error("You cannot specify more than one of -alpha -alphapam -mix");
116 else if (alphaSpec)
117 cmdlineP->alpha = ALPHA_ONLY;
118 else if (mixSpec)
119 cmdlineP->alpha = ALPHA_MIX;
120 else if (alphapamSpec)
121 cmdlineP->alpha = ALPHA_IN;
122 else
123 cmdlineP->alpha = ALPHA_NONE;
124
125 if (backgroundSpec && !mixSpec)
126 pm_error("-background is useless without -mix");
127
128 if (!backgroundSpec)
129 cmdlineP->background = NULL;
130
131 if (!textSpec)
132 cmdlineP->text = NULL;
133
134 if (argc-1 < 1)
135 cmdlineP->inputFileName = "-";
136 else if (argc-1 == 1)
137 cmdlineP->inputFileName = argv[1];
138 else
139 pm_error("Program takes at most one argument: input file name. "
140 "you specified %d", argc-1);
141 }
142
143
144
145 typedef struct {
146 /*----------------------------------------------------------------------------
147 A color in a format compatible with the PNG library.
148
149 Note that the PNG library declares types png_color and png_color_16
150 which are similar.
151 -----------------------------------------------------------------------------*/
152 png_uint_16 r;
153 png_uint_16 g;
154 png_uint_16 b;
155 } pngcolor;
156
157
158
159 static pngcolor
160 pngcolorFrom16(png_color_16 const arg) {
161
162 pngcolor retval;
163
164 retval.r = arg.red;
165 retval.g = arg.green;
166 retval.b = arg.blue;
167
168 return retval;
169 }
170
171
172
173 static pngcolor
174 pngcolorFromByte(png_color const arg) {
175
176 pngcolor retval;
177
178 retval.r = arg.red;
179 retval.g = arg.green;
180 retval.b = arg.blue;
181
182 return retval;
183 }
184
185
186
187 static bool
188 pngColorEqual(pngcolor const comparand,
189 pngcolor const comparator) {
190
191 return (comparand.r == comparator.r
192 && comparand.g == comparator.g
193 && comparand.b == comparator.b);
194 }
195
196
197
198 static png_uint_16
199 gammaCorrect(png_uint_16 const uncorrected,
200 GammaCorrection const gamma,
201 png_uint_16 const maxval) {
202
203 if (gamma.needCorrection) {
204 double const uncorrectedN = (double) uncorrected / maxval;
205 return (png_uint_16)
206 ROUNDU(pow(uncorrectedN, (1.0 / gamma.gamma)) * maxval);
207 } else
208 return uncorrected;
209 }
210
211
212
213 static pngcolor
214 gammaCorrectColor(pngcolor const color,
215 GammaCorrection const gamma,
216 png_uint_16 const maxval) {
217
218 pngcolor retval;
219
220 retval.r = gammaCorrect(color.r, gamma, maxval);
221
222 return retval;
223 }
224
225
226
227 static unsigned int
228 computePngLineSize(struct pngx * const pngxP) {
229
230 unsigned int const bytesPerSample =
231 pngx_bitDepth(pngxP) == 16 ? 2 : 1;
232
233 unsigned int samplesPerPixel;
234
235 switch (pngx_colorType(pngxP)) {
236 case PNG_COLOR_TYPE_GRAY_ALPHA: samplesPerPixel = 2; break;
237 case PNG_COLOR_TYPE_RGB: samplesPerPixel = 3; break;
238 case PNG_COLOR_TYPE_RGB_ALPHA: samplesPerPixel = 4; break;
239 default: samplesPerPixel = 1;
240 }
241
242 if (UINT_MAX / bytesPerSample / samplesPerPixel < pngx_imageWidth(pngxP))
243 pm_error("Width %u of PNG is uncomputably large",
244 pngx_imageWidth(pngxP));
245
246 return pngx_imageWidth(pngxP) * bytesPerSample * samplesPerPixel;
247 }
248
249
250
251 static void
252 allocPngRaster(struct pngx * const pngxP,
253 png_byte *** const pngImageP) {
254
255 unsigned int const lineSize = computePngLineSize(pngxP);
256
257 png_byte ** pngImage;
258 unsigned int row;
259
260 MALLOCARRAY(pngImage, pngx_imageHeight(pngxP));
261
262 if (pngImage == NULL)
263 pm_error("couldn't allocate index space for %u PNG raster rows. "
264 "Try -byrow, which needs only 1 row of buffer space. ",
265 pngx_imageHeight(pngxP));
266
267 for (row = 0; row < pngx_imageHeight(pngxP); ++row) {
268 MALLOCARRAY(pngImage[row], lineSize);
269 if (pngImage[row] == NULL)
270 pm_error("couldn't allocate space for %uth row of PNG raster. "
271 "Try -byrow, which needs only 1 row of buffer space. ",
272 row);
273 }
274 *pngImageP = pngImage;
275 }
276
277
278
279 static void
280 freePngRaster(png_byte ** const pngRaster,
281 struct pngx * const pngxP) {
282
283 unsigned int row;
284
285 for (row = 0; row < pngx_imageHeight(pngxP); ++row)
286 free(pngRaster[row]);
287
288 free(pngRaster);
289 }
290
291
292
293 typedef struct {
294 /*----------------------------------------------------------------------------
295 This is an object for reading the raster of the PNG, a row at a time.
296 -----------------------------------------------------------------------------*/
297 struct pngx * pngxP;
298 png_byte ** pngRaster;
299 /* The entire raster of the PNG. Null if this is a
300 row-at-a-time object. Constant.
301
302 We give a pointer into this to the user.
303 */
304 png_byte * rowBuf;
305 /* The buffer in which we put the most recently read row.
306 Null if this is an all-at-once object. Constant.
307
308 We give a pointer into this to the user.
309 */
310 unsigned int nextRowNum;
311 /* The number of the next row to be read from this object. */
312
313 } Reader;
314
315
316
317 static Reader *
318 reader_createAllAtOnce(struct pngx * const pngxP,
319 FILE * const ifP) {
320 /*----------------------------------------------------------------------------
321 Create a Reader object that uses libpng's all-at-once raster reading
322 interface (libpng calls this the "high level" interface).
323
324 The Reader object reads the PNG at construction time, stores the entire
325 raster, and hands it out as you call reader_read().
326
327 It is essential that *pngxP be already fully set up to read the image
328 (all options set).
329 -----------------------------------------------------------------------------*/
330 Reader * readerP;
331
332 MALLOCVAR_NOFAIL(readerP);
333
334 readerP->pngxP = pngxP;
335
336 allocPngRaster(pngxP, &readerP->pngRaster);
337
338 readerP->rowBuf = NULL;
339
340 pngx_readImage(pngxP, readerP->pngRaster);
341
342 readerP->nextRowNum = 0;
343
344 return readerP;
345 }
346
347
348
349 static Reader *
350 reader_createRowByRow(struct pngx * const pngxP,
351 FILE * const ifP) {
352 /*----------------------------------------------------------------------------
353 Create a Reader object that uses libpng's one-row-at-a-time raster reading
354 interface (libpng calls this the "low level" interface).
355
356 The Reader object reads from the PNG file, via libpng, as its client
357 requests the rows.
358 -----------------------------------------------------------------------------*/
359 Reader * readerP;
360
361 MALLOCVAR_NOFAIL(readerP);
362
363 readerP->pngxP = pngxP;
364
365 readerP->pngRaster = NULL;
366
367 MALLOCARRAY(readerP->rowBuf, computePngLineSize(pngxP));
368
369 if (!readerP->rowBuf)
370 pm_error("Could not allocate %u bytes for a PNG row buffer",
371 computePngLineSize(pngxP));
372
373 readerP->nextRowNum = 0;
374
375 if (pngx_interlaceType(pngxP) != PNG_INTERLACE_NONE)
376 pm_message("WARNING: this is an interlaced PNG. The PAM output "
377 "will be interlaced. To get proper output, "
378 "don't use -byrow");
379
380 return readerP;
381 }
382
383
384
385 static void
386 reader_destroy(Reader * const readerP) {
387
388 if (readerP->pngRaster)
389 freePngRaster(readerP->pngRaster, readerP->pngxP);
390
391 if (readerP->rowBuf)
392 free(readerP->rowBuf);
393
394 free(readerP);
395 }
396
397
398
399 static png_byte *
400 reader_read(Reader * const readerP) {
401 /*----------------------------------------------------------------------------
402 Return a pointer to the next row of the raster.
403
404 The pointer is into storage owned by this object. It is good until
405 the next read from the object, while the object exists.
406 -----------------------------------------------------------------------------*/
407 png_byte * retval;
408
409 if (readerP->pngRaster) {
410 if (readerP->nextRowNum >= pngx_imageHeight(readerP->pngxP))
411 retval = NULL;
412 else
413 retval = readerP->pngRaster[readerP->nextRowNum];
414 } else {
415 pngx_readRow(readerP->pngxP, readerP->rowBuf, NULL);
416 retval = readerP->rowBuf;
417 }
418
419 ++readerP->nextRowNum;
420
421 return retval;
422 }
423
424
425
426 static png_uint_16
427 getPngVal(const png_byte ** const pp,
428 int const bitDepth) {
429
430 png_uint_16 c;
431
432 if (bitDepth == 16)
433 c = *(*pp)++ << 8;
434 else
435 c = 0;
436
437 c |= *(*pp)++;
438
439 return c;
440 }
441
442
443
444 static bool
445 isGrayscale(pngcolor const color) {
446
447 return color.r == color.g && color.r == color.b;
448 }
449
450
451
452 static sample
453 alphaMix(png_uint_16 const foreground,
454 png_uint_16 const background,
455 png_uint_16 const alpha,
456 sample const maxval) {
457
458 double const opacity = (double)alpha / maxval;
459 double const transparency = 1.0 - opacity;
460
461 return ROUNDU(foreground * opacity + background * transparency);
462 }
463
464
465
466 static void
467 setTuple(const struct pam * const pamP,
468 tuple const tuple,
469 pngcolor const foreground,
470 pngcolor const background,
471 enum AlphaHandling const alphaHandling,
472 const struct pngx * const pngxP,
473 png_uint_16 const alpha) {
474
475 if (alphaHandling == ALPHA_ONLY)
476 tuple[0] = alpha;
477 else if (alphaHandling == ALPHA_NONE ||
478 (alphaHandling == ALPHA_MIX && alpha == pngxP->maxval)) {
479 if (pamP->depth < 3)
480 tuple[0] = foreground.r;
481 else {
482 tuple[PAM_RED_PLANE] = foreground.r;
483 tuple[PAM_GRN_PLANE] = foreground.g;
484 tuple[PAM_BLU_PLANE] = foreground.b;
485 }
486 } else if (alphaHandling == ALPHA_IN) {
487 if (pamP->depth < 4) {
488 tuple[0] = foreground.r;
489 tuple[PAM_GRAY_TRN_PLANE] = alpha;
490 } else {
491 tuple[PAM_RED_PLANE] = foreground.r;
492 tuple[PAM_GRN_PLANE] = foreground.g;
493 tuple[PAM_BLU_PLANE] = foreground.b;
494 tuple[PAM_TRN_PLANE] = alpha;
495 }
496 } else {
497 assert(alphaHandling == ALPHA_MIX);
498
499 if (pamP->depth < 3)
500 tuple[0] =
501 alphaMix(foreground.r, background.r, alpha, pngxP->maxval);
502 else {
503 tuple[PAM_RED_PLANE] =
504 alphaMix(foreground.r, background.r, alpha, pngxP->maxval);
505 tuple[PAM_GRN_PLANE] =
506 alphaMix(foreground.g, background.g, alpha, pngxP->maxval);
507 tuple[PAM_BLU_PLANE] =
508 alphaMix(foreground.b, background.b, alpha, pngxP->maxval);
509 }
510 }
511 }
512
513
514
515 static bool
516 isColor(png_color const c) {
517
518 return c.red != c.green || c.green != c.blue;
519 }
520
521
522
523 static void
524 saveText(struct pngx * const pngxP,
525 FILE * const tfP) {
526
527 struct pngx_text const text = pngx_text(pngxP);
528
529 unsigned int i;
530
531 for (i = 0 ; i < text.size; ++i) {
532 unsigned int j;
533 j = 0;
534
535 while (text.line[i].key[j] != '\0' &&
536 text.line[i].key[j] != ' ')
537 ++j;
538
539 if (text.line[i].key[j] != ' ') {
540 fprintf(tfP, "%s", text.line[i].key);
541 for (j = strlen (text.line[i].key); j < 15; ++j)
542 putc(' ', tfP);
543 } else {
544 fprintf(tfP, "\"%s\"", text.line[i].key);
545 for (j = strlen (text.line[i].key); j < 13; ++j)
546 putc(' ', tfP);
547 }
548 putc(' ', tfP); /* at least one space between key and text */
549
550 for (j = 0; j < text.line[i].text_length; ++j) {
551 putc(text.line[i].text[j], tfP);
552 if (text.line[i].text[j] == '\n') {
553 unsigned int k;
554 for (k = 0; k < 16; ++k)
555 putc(' ', tfP);
556 }
557 }
558 putc('\n', tfP);
559 }
560 }
561
562
563
564 static void
565 showTime(struct pngx * const pngxP) {
566
567 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tIME)) {
568 png_time const modTime = pngx_time(pngxP);
569
570 static const char * const month[] = {
571 "", "January", "February", "March", "April", "May", "June",
572 "July", "August", "September", "October", "November", "December"
573 };
574
575 if (modTime.month < 1 || modTime.month >= ARRAY_SIZE(month)) {
576 pm_message("tIME chunk in PNG input is invalid; "
577 "modification time of image is unknown. "
578 "The month value, which should be in the range "
579 "1-12, is %u", modTime.month);
580 } else {
581 pm_message("modification time: %02d %s %d %02d:%02d:%02d",
582 modTime.day,
583 month[modTime.month],
584 modTime.year,
585 modTime.hour,
586 modTime.minute,
587 modTime.second);
588 }
589 }
590 }
591
592
593
594 static void
595 dumpTypeAndFilter(struct pngx * const pngxP) {
596
597 const char * typeString;
598 const char * filterString;
599
600 switch (pngx_colorType(pngxP)) {
601 case PNG_COLOR_TYPE_GRAY:
602 typeString = "gray";
603 break;
604
605 case PNG_COLOR_TYPE_GRAY_ALPHA:
606 typeString = "gray+alpha";
607 break;
608
609 case PNG_COLOR_TYPE_PALETTE:
610 typeString = "palette";
611 break;
612
613 case PNG_COLOR_TYPE_RGB:
614 typeString = "truecolor";
615 break;
616
617 case PNG_COLOR_TYPE_RGB_ALPHA:
618 typeString = "truecolor+alpha";
619 break;
620 }
621
622 switch (pngx_filterType(pngxP)) {
623 case PNG_FILTER_TYPE_BASE:
624 pm_asprintf(&filterString, "base filter");
625 break;
626 default:
627 pm_asprintf(&filterString, "unknown filter type %d",
628 pngx_filterType(pngxP));
629 }
630
631 pm_message("%s, %s, %s",
632 typeString,
633 pngx_interlaceType(pngxP) ?
634 "Adam7 interlaced" : "not interlaced",
635 filterString);
636
637 pm_strfree(filterString);
638 }
639
640
641
642 static void
643 dumpPngInfo(struct pngx * const pngxP) {
644
645 pm_message("reading a %u x %u image, %u bit%s",
646 pngx_imageWidth(pngxP),
647 pngx_imageHeight(pngxP),
648 pngx_bitDepth(pngxP),
649 pngx_bitDepth(pngxP) > 1 ? "s" : "");
650
651 dumpTypeAndFilter(pngxP);
652
653 if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD)) {
654 png_color_16 const background = pngx_bkgd(pngxP);
655
656 pm_message("background {index, gray, red, green, blue} = "
657 "{%d, %d, %d, %d, %d}",
658 background.index,
659 background.gray,
660 background.red,
661 background.green,
662 background.blue);
663 }
664
665 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
666 struct pngx_trns const trns = pngx_trns(pngxP);
667
668 pm_message("tRNS chunk (transparency):");
669 pm_message(" %u palette entries", trns.numTrans);
670 pm_message(" transparent color = (%u,%u,%u)",
671 trns.transColor.red,
672 trns.transColor.green,
673 trns.transColor.blue);
674 } else
675 pm_message("tRNS chunk (transparency): not present");
676
677 if (pngx_chunkIsPresent(pngxP, PNG_INFO_gAMA))
678 pm_message("gAMA chunk (image gamma): gamma = %4.2f",
679 pngx_gama(pngxP));
680 else
681 pm_message("gAMA chunk (image gamma): not present");
682
683 if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT))
684 pm_message("sBIT chunk: present");
685 else
686 pm_message("sBIT chunk: not present");
687
688 if (pngx_chunkIsPresent(pngxP, PNG_INFO_cHRM))
689 pm_message("cHRM chunk: present");
690 else
691 pm_message("cHRM chunk: not present");
692
693 if (pngx_chunkIsPresent(pngxP, PNG_INFO_PLTE))
694 pm_message("PLTE chunk: %d entries", pngx_plte(pngxP).size);
695 else
696 pm_message("PLTE chunk: not present");
697
698 if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD))
699 pm_message("bKGD chunk: present");
700 else
701 pm_message("bKGD chunk: not present");
702
703 if (pngx_chunkIsPresent(pngxP, PNG_INFO_hIST))
704 pm_message("hIST chunk: present");
705 else
706 pm_message("hIST chunk: not present");
707
708 if (pngx_chunkIsPresent(pngxP, PNG_INFO_pHYs))
709 pm_message("pHYs chunk: present");
710 else
711 pm_message("pHYs chunk: not present");
712
713 if (pngx_chunkIsPresent(pngxP, PNG_INFO_oFFs))
714 pm_message("oFFs chunk: present");
715 else
716 pm_message("oFFs chunk: not present");
717
718 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tIME))
719 pm_message("tIME chunk: present");
720 else
721 pm_message("tIME chunk: not present");
722
723 if (pngx_chunkIsPresent(pngxP, PNG_INFO_pCAL))
724 pm_message("pCAL chunk: present");
725 else
726 pm_message("pCAL chunk: not present");
727
728 if (pngx_chunkIsPresent(pngxP, PNG_INFO_sRGB))
729 pm_message("sRGB chunk: present");
730 else
731 pm_message("sRGB chunk: not present");
732 }
733
734
735
736 static const png_color_16
737 transColor(struct pngx * const pngxP) {
738
739 struct pngx_trns const trans = pngx_trns(pngxP);
740
741 assert(pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS));
742
743 return trans.transColor;
744 }
745
746
747
748 static bool
749 isTransparentColor(pngcolor const color,
750 struct pngx * const pngxP,
751 GammaCorrection const gamma) {
752 /*----------------------------------------------------------------------------
753 Return TRUE iff pixels of color 'color' are supposed to be transparent
754 everywhere they occur. Assume it's an RGB image.
755
756 'gamma' indicats what gamma correction has been applied to 'color' (we need
757 to know that because *pngxP identifies the color that is supposed to be
758 transparent in _not_ gamma-corrected form!).
759 -----------------------------------------------------------------------------*/
760 bool retval;
761
762 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
763 png_color_16 const transColor16 = transColor(pngxP);
764
765 /* It seems odd that libpng lets you get gamma-corrected pixel
766 values, but not gamma-corrected transparency or background
767 values. But as that is the case, we have to gamma-correct
768 the transparency values.
769
770 Note that because we compare the gamma-corrected values and
771 there may be many-to-one mapping of uncorrected to corrected
772 values, more pixels may be transparent than what the user
773 intended.
774
775 We could fix this by not letting libpng gamma-correct the
776 pixels, and just do it ourselves.
777 */
778
779 switch (pngx_colorType(pngxP)) {
780 case PNG_COLOR_TYPE_GRAY:
781 retval = color.r == gammaCorrect(transColor16.gray, gamma,
782 pngxP->maxval);
783 break;
784 default: {
785 pngcolor const transColor = pngcolorFrom16(transColor16);
786 retval = pngColorEqual(color,
787 gammaCorrectColor(transColor, gamma,
788 pngxP->maxval));
789 }
790 }
791 } else
792 retval = FALSE;
793
794 return retval;
795 }
796
797
798
799 static void
800 setupGammaCorrection(struct pngx * const pngxP,
801 bool const screenGammaIsKnown,
802 float const screenGamma,
803 GammaCorrection * const gammaCorrectionP) {
804 /*----------------------------------------------------------------------------
805 Set up to have values from the PNG gamma-corrected.
806
807 Return as *gammaCorrectionP the correction necessary and tell the
808 libpng image reader *pngxP to do that same correction - on the pixels
809 only, as it can't do it on anything else (hence, Caller will have to
810 use *gammaCorrectionP to do it).
811
812 'screenGammaIsKnown' means we know what the screen gamma is, and it is
813 'screenGamma'. If we don't know what the screen gamma is, gamma
814 correction is not possible, so we set up for no gamma correction.
815
816 The gamma correction we ordain is a combination of the image gamma,
817 recorded in the PNG input and represented by *pngxP, and the screen gamma.
818
819 Note that "screen gamma" is a characteristic of both the display and the
820 viewing conditions. There are also "display gamma" and "viewing gamma,"
821 respectively, but we don't care about the breakdown. In a dimly lit room,
822 viewing gamma is 1 and screen gamma is the same as the display gamma.
823 -----------------------------------------------------------------------------*/
824 if (!screenGammaIsKnown)
825 gammaCorrectionP->needCorrection = false;
826 else {
827 float imageGamma;
828 if (pngx_chunkIsPresent(pngxP, PNG_INFO_gAMA))
829 imageGamma = pngx_gama(pngxP);
830 else {
831 if (verbose)
832 pm_message("PNG doesn't specify image gamma. Assuming 1.0");
833 imageGamma = 1.0;
834 }
835
836 if (fabs(screenGamma * imageGamma - 1.0) < .01) {
837 gammaCorrectionP->needCorrection = false;
838 if (verbose)
839 pm_message("image gamma %4.2f matches "
840 "screen gamma %4.2f. No conversion.",
841 imageGamma, screenGamma);
842 } else {
843 pngx_setGamma(pngxP, screenGamma, imageGamma);
844
845 gammaCorrectionP->needCorrection = true;
846 gammaCorrectionP->gamma = imageGamma * screenGamma;
847 /* In case of gamma-corrections, sBIT's as in the
848 PNG-file are not valid anymore
849 */
850 pngx_removeChunk(pngxP, PNG_INFO_sBIT);
851 if (verbose)
852 pm_message("image gamma is %4.2f, "
853 "display gamma is %4.2f; "
854 "combined gamma is %4.2f",
855 imageGamma, screenGamma, gammaCorrectionP->gamma);
856 }
857 }
858 }
859
860
861
862 static bool
863 paletteHasPartialTransparency(struct pngx * const pngxP) {
864
865 bool retval;
866
867 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
868 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
869 struct pngx_trns const trans = pngx_trns(pngxP);
870
871 bool foundPartial;
872 unsigned int i;
873
874 for (i = 0, foundPartial = FALSE;
875 i < trans.numTrans && !foundPartial;
876 ++i) {
877 if (trans.trans[i] != 0 && trans.trans[i] != pngxP->maxval) {
878 foundPartial = TRUE;
879 }
880 }
881 retval = foundPartial;
882 } else
883 retval = FALSE;
884 } else
885 retval = FALSE;
886
887 return retval;
888 }
889
890
891
892 static void
893 getComponentSbitFg(struct pngx * const pngxP,
894 png_byte * const fgSbitP,
895 bool * const notUniformP) {
896
897 png_color_8 const sigBit = pngx_sbit(pngxP);
898
899 assert(pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT));
900
901 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB ||
902 pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA ||
903 pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
904
905 if (sigBit.red == sigBit.blue &&
906 sigBit.red == sigBit.green) {
907 *notUniformP = false;
908 *fgSbitP = sigBit.red;
909 } else
910 *notUniformP = true;
911 } else {
912 /* It has only a gray channel so it's obviously uniform */
913 *notUniformP = false;
914 *fgSbitP = sigBit.gray;
915 }
916 }
917
918
919
920 static void
921 getComponentSbit(struct pngx * const pngxP,
922 enum AlphaHandling const alphaHandling,
923 png_byte * const componentSbitP,
924 bool * const notUniformP) {
925
926 assert(pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT));
927
928 switch (alphaHandling) {
929
930 case ALPHA_ONLY: {
931 /* We care only about the alpha channel, so the uniform Sbit is
932 the alpha Sbit
933 */
934 *notUniformP = false;
935 *componentSbitP = pngx_sbit(pngxP).alpha;
936 } break;
937 case ALPHA_NONE:
938 case ALPHA_MIX:
939 /* We aren't going to produce an alpha channel, so we care only
940 about the uniformity of the foreground channels.
941 */
942 getComponentSbitFg(pngxP, componentSbitP, notUniformP);
943 break;
944 case ALPHA_IN: {
945 /* We care about both the foreground and the alpha */
946 bool fgNotUniform;
947 png_byte fgSbit;
948
949 getComponentSbitFg(pngxP, &fgSbit, &fgNotUniform);
950
951 if (fgNotUniform)
952 *notUniformP = true;
953 else {
954 if (fgSbit == pngx_sbit(pngxP).alpha) {
955 *notUniformP = false;
956 *componentSbitP = fgSbit;
957 } else
958 *notUniformP = true;
959 }
960 } break;
961 }
962 }
963
964
965
966 static void
967 shiftPalette(struct pngx * const pngxP,
968 unsigned int const shift) {
969 /*----------------------------------------------------------------------------
970 Shift every component of every color in the PNG palette right by
971 'shift' bits because sBIT chunk says only those are significant.
972 -----------------------------------------------------------------------------*/
973 if (shift > 7)
974 pm_error("Invalid PNG: paletted image can't have "
975 "more than 8 significant bits per component, "
976 "but sBIT chunk says %u bits",
977 shift);
978 else {
979 struct pngx_plte const palette = pngx_plte(pngxP);
980
981 unsigned int i;
982
983 for (i = 0; i < palette.size; ++i) {
984 palette.palette[i].red >>= (8 - shift);
985 palette.palette[i].green >>= (8 - shift);
986 palette.palette[i].blue >>= (8 - shift);
987 }
988 }
989 }
990
991
992
993 static void
994 computeMaxvalFromSbit(struct pngx * const pngxP,
995 enum AlphaHandling const alphaHandling,
996 png_uint_16 * const maxvalP,
997 bool * const succeededP,
998 int * const errorLevelP) {
999
1000 /* sBIT handling is very tricky. If we are extracting only the
1001 image, we can use the sBIT info for grayscale and color images,
1002 if the three values agree. If we extract the transparency/alpha
1003 mask, sBIT is irrelevant for trans and valid for alpha. If we
1004 mix both, the multiplication may result in values that require
1005 the normal bit depth, so we will use the sBIT info only for
1006 transparency, if we know that only solid and fully transparent
1007 is used
1008 */
1009
1010 bool notUniform;
1011 /* The sBIT chunk says the number of significant high-order bits
1012 in each component varies among the components we care about.
1013 */
1014 png_byte componentSigBit;
1015 /* The number of high-order significant bits in each RGB component.
1016 Meaningless if they aren't all the same (i.e. 'notUniform')
1017 */
1018
1019 getComponentSbit(pngxP, alphaHandling, &componentSigBit, &notUniform);
1020
1021 if (notUniform) {
1022 pm_message("This program cannot handle "
1023 "different bit depths for color channels");
1024 pm_message("writing file with %u bit resolution",
1025 pngx_bitDepth(pngxP));
1026 *succeededP = false;
1027 *errorLevelP = PNMTOPNG_WARNING_LEVEL;
1028 } else if (componentSigBit > 15) {
1029 pm_message("Invalid PNG: says %u significant bits for a component; "
1030 "max possible is 16. Ignoring sBIT chunk.",
1031 componentSigBit);
1032 *succeededP = false;
1033 *errorLevelP = PNMTOPNG_WARNING_LEVEL;
1034 } else {
1035 if (alphaHandling == ALPHA_MIX &&
1036 (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA ||
1037 pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA ||
1038 paletteHasPartialTransparency(pngxP)))
1039 *succeededP = false;
1040 else {
1041 if (componentSigBit < pngx_bitDepth(pngxP)) {
1042 pm_message("Image has fewer significant bits, "
1043 "writing file with %u bits", componentSigBit);
1044 *maxvalP = (1l << componentSigBit) - 1;
1045 *succeededP = true;
1046
1047 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE)
1048 shiftPalette(pngxP, componentSigBit);
1049 else
1050 pngx_setShift(pngxP, pngx_sbit(pngxP));
1051 } else
1052 *succeededP = false;
1053 }
1054 }
1055 }
1056
1057
1058
1059 static void
1060 setupSignificantBits(struct pngx * const pngxP,
1061 enum AlphaHandling const alphaHandling,
1062 int * const errorLevelP) {
1063 /*----------------------------------------------------------------------------
1064 Figure out what maxval is used in the PNG described by *pngxP, with 'alpha'
1065 telling which information in the PNG we care about (image or alpha mask).
1066 Update *pngxP with that information.
1067
1068 Return the result as *maxvalP.
1069
1070 Also set up *pngxP for the corresponding significant bits.
1071 -----------------------------------------------------------------------------*/
1072 bool gotItFromSbit;
1073
1074 if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT))
1075 computeMaxvalFromSbit(pngxP, alphaHandling,
1076 &pngxP->maxval, &gotItFromSbit, errorLevelP);
1077 else
1078 gotItFromSbit = false;
1079
1080 if (!gotItFromSbit) {
1081 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
1082 if (alphaHandling == ALPHA_ONLY) {
1083 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY ||
1084 pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB)
1085 /* The alpha mask will be all opaque, so maxval 1
1086 is plenty
1087 */
1088 pngxP->maxval = 1;
1089 else if (paletteHasPartialTransparency(pngxP))
1090 /* Use same maxval as PNG transparency palette for
1091 simplicity
1092 */
1093 pngxP->maxval = 255;
1094 else
1095 /* A common case, so we conserve bits */
1096 pngxP->maxval = 1;
1097 } else
1098 /* Use same maxval as PNG palette for simplicity */
1099 pngxP->maxval = 255;
1100 } else {
1101 pngxP->maxval = (1l << pngx_bitDepth(pngxP)) - 1;
1102 }
1103 }
1104 }
1105
1106
1107
1108 static bool
1109 imageHasColor(struct pngx * const pngxP) {
1110
1111 bool retval;
1112
1113 if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY ||
1114 pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA)
1115
1116 retval = FALSE;
1117 else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
1118 struct pngx_plte const palette = pngx_plte(pngxP);
1119
1120 bool foundColor;
1121 unsigned int i;
1122
1123 for (i = 0, foundColor = FALSE;
1124 i < palette.size && !foundColor;
1125 ++i) {
1126 if (isColor(palette.palette[i]))
1127 foundColor = TRUE;
1128 }
1129 retval = foundColor;
1130 } else
1131 retval = TRUE;
1132
1133 return retval;
1134 }
1135
1136
1137
1138 static void
1139 determineOutputType(struct pngx * const pngxP,
1140 enum AlphaHandling const alphaHandling,
1141 pngcolor const bgColor,
1142 xelval const maxval,
1143 int * const formatP,
1144 unsigned int * const depthP,
1145 char * const tupleType) {
1146
1147 if (alphaHandling == ALPHA_ONLY) {
1148 /* The output is a old style pseudo-PNM transparency image */
1149 *depthP = 1;
1150 *formatP = maxval > 1 ? PGM_FORMAT : PBM_FORMAT;
1151 } else {
1152 /* The output is a normal Netpbm image */
1153 bool const outputIsColor =
1154 imageHasColor(pngxP) || !isGrayscale(bgColor);
1155
1156 if (alphaHandling == ALPHA_IN) {
1157 *formatP = PAM_FORMAT;
1158 if (outputIsColor) {
1159 *depthP = 4;
1160 strcpy(tupleType, "RGB_ALPHA");
1161 } else {
1162 *depthP = 2;
1163 strcpy(tupleType, "GRAYSCALE_ALPHA");
1164 }
1165 } else {
1166 if (outputIsColor) {
1167 *formatP = PPM_FORMAT;
1168 *depthP = 3;
1169 } else {
1170 *depthP = 1;
1171 *formatP = maxval > 1 ? PGM_FORMAT : PBM_FORMAT;
1172 }
1173 }
1174 }
1175 }
1176
1177
1178
1179 static void
1180 getBackgroundColor(struct pngx * const pngxP,
1181 const char * const requestedColor,
1182 GammaCorrection const gamma,
1183 xelval const maxval,
1184 pngcolor * const bgColorP) {
1185 /*----------------------------------------------------------------------------
1186 Figure out what the background color should be. If the user requested
1187 a particular color ('requestedColor' not null), that's the one.
1188 Otherwise, if the PNG specifies a background color, that's the one.
1189 And otherwise, it's white.
1190 -----------------------------------------------------------------------------*/
1191 if (requestedColor) {
1192 /* Background was specified from the command-line; we always
1193 use that. I chose to do no gamma-correction in this case;
1194 which is a bit arbitrary.
1195 */
1196 pixel const backcolor = ppm_parsecolor(requestedColor, maxval);
1197
1198 bgColorP->r = PPM_GETR(backcolor);
1199 bgColorP->g = PPM_GETG(backcolor);
1200 bgColorP->b = PPM_GETB(backcolor);
1201
1202 } else if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD)) {
1203 /* Didn't manage to get libpng to work (bugs?) concerning background
1204 processing, therefore we do our own.
1205 */
1206 png_color_16 const background = pngx_bkgd(pngxP);
1207 switch (pngx_colorType(pngxP)) {
1208 case PNG_COLOR_TYPE_GRAY:
1209 case PNG_COLOR_TYPE_GRAY_ALPHA:
1210 bgColorP->r = bgColorP->g = bgColorP->b =
1211 gammaCorrect(background.gray, gamma, pngxP->maxval);
1212 break;
1213 case PNG_COLOR_TYPE_PALETTE: {
1214 struct pngx_plte const palette = pngx_plte(pngxP);
1215 png_color const rawBgcolor =
1216 palette.palette[background.index];
1217 *bgColorP = gammaCorrectColor(pngcolorFromByte(rawBgcolor),
1218 gamma, pngxP->maxval);
1219 }
1220 break;
1221 case PNG_COLOR_TYPE_RGB:
1222 case PNG_COLOR_TYPE_RGB_ALPHA: {
1223 png_color_16 const rawBgcolor = background;
1224
1225 *bgColorP = gammaCorrectColor(pngcolorFrom16(rawBgcolor),
1226 gamma, pngxP->maxval);
1227 }
1228 break;
1229 }
1230 } else
1231 /* when no background given, we use white [from version 2.37] */
1232 bgColorP->r = bgColorP->g = bgColorP->b = maxval;
1233 }
1234
1235
1236
1237 static void
1238 warnNonsquarePixels(struct pngx * const pngxP,
1239 int * const errorLevelP) {
1240
1241 if (pngx_pixelAspectRatioIsKnown(pngxP)) {
1242 float const r = pngx_pixelAspectRatio(pngxP);
1243
1244 if (r != 1.0) {
1245 const char * const baseMsg = "warning - non-square pixels";
1246
1247 if (pm_have_float_format())
1248 pm_message("%s; to fix do a 'pamscale -%cscale %g'",
1249 baseMsg,
1250 r < 1.0 ? 'x' : 'y',
1251 r < 1.0 ? 1.0 / r : r);
1252 else
1253 pm_message("%s", baseMsg);
1254
1255 *errorLevelP = PNMTOPNG_WARNING_LEVEL;
1256 }
1257 }
1258 }
1259
1260
1261
1262 static png_uint_16
1263 paletteAlpha(struct pngx * const pngxP,
1264 png_uint_16 const index,
1265 sample const maxval) {
1266
1267 png_uint_16 retval;
1268
1269 if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
1270 struct pngx_trns const trans = pngx_trns(pngxP);
1271
1272 if (index < trans.numTrans)
1273 retval = trans.trans[index];
1274 else
1275 retval = maxval;
1276 } else
1277 retval = maxval;
1278
1279 return retval;
1280 }
1281
1282
1283
1284 #define GET_PNG_VAL(p) getPngVal(&(p), pngx_bitDepth(pngxP))
1285
1286
1287
1288 static void
1289 makeTupleRow(const struct pam * const pamP,
1290 const tuple * const tuplerow,
1291 struct pngx * const pngxP,
1292 const png_byte * const pngRasterRow,
1293 pngcolor const bgColor,
1294 enum AlphaHandling const alphaHandling,
1295 GammaCorrection const gamma) {
1296 /*----------------------------------------------------------------------------
1297 Convert a raster row as supplied by libpng, at 'pngRasterRow' and
1298 described by *pngxP, to a libpam-style tuple row at 'tupleRow'.
1299
1300 Where the raster says the pixel isn't opaque, we either include that
1301 opacity information in the output pixel or we mix the pixel with background
1302 color 'bgColor', as directed by 'alphaHandling'. Or, if 'alphaHandling'
1303 says so, we may produce an output row of _only_ the transparency
1304 information.
1305 -----------------------------------------------------------------------------*/
1306 const png_byte * pngPixelP;
1307 unsigned int col;
1308
1309 pngPixelP = &pngRasterRow[0]; /* initial value */
1310 for (col = 0; col < pngx_imageWidth(pngxP); ++col) {
1311 switch (pngx_colorType(pngxP)) {
1312 case PNG_COLOR_TYPE_GRAY: {
1313 pngcolor fgColor;
1314 fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP);
1315 setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling,
1316 pngxP,
1317 isTransparentColor(fgColor, pngxP, gamma) ?
1318 0 : pngxP->maxval);
1319 }
1320 break;
1321
1322 case PNG_COLOR_TYPE_GRAY_ALPHA: {
1323 pngcolor fgColor;
1324 png_uint_16 alpha;
1325
1326 fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP);
1327 alpha = GET_PNG_VAL(pngPixelP);
1328 setTuple(pamP, tuplerow[col], fgColor, bgColor,
1329 alphaHandling, pngxP, alpha);
1330 }
1331 break;
1332
1333 case PNG_COLOR_TYPE_PALETTE: {
1334 png_uint_16 const index = GET_PNG_VAL(pngPixelP);
1335 struct pngx_plte const palette = pngx_plte(pngxP);
1336 png_color const paletteColor = palette.palette[index];
1337
1338 pngcolor fgColor;
1339
1340 fgColor.r = paletteColor.red;
1341 fgColor.g = paletteColor.green;
1342 fgColor.b = paletteColor.blue;
1343
1344 setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling,
1345 pngxP, paletteAlpha(pngxP, index, pngxP->maxval));
1346 }
1347 break;
1348
1349 case PNG_COLOR_TYPE_RGB: {
1350 pngcolor fgColor;
1351
1352 fgColor.r = GET_PNG_VAL(pngPixelP);
1353 fgColor.g = GET_PNG_VAL(pngPixelP);
1354 fgColor.b = GET_PNG_VAL(pngPixelP);
1355 setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling,
1356 pngxP,
1357 isTransparentColor(fgColor, pngxP, gamma) ?
1358 0 : pngxP->maxval);
1359 }
1360 break;
1361
1362 case PNG_COLOR_TYPE_RGB_ALPHA: {
1363 pngcolor fgColor;
1364 png_uint_16 alpha;
1365
1366 fgColor.r = GET_PNG_VAL(pngPixelP);
1367 fgColor.g = GET_PNG_VAL(pngPixelP);
1368 fgColor.b = GET_PNG_VAL(pngPixelP);
1369 alpha = GET_PNG_VAL(pngPixelP);
1370 setTuple(pamP, tuplerow[col], fgColor, bgColor,
1371 alphaHandling, pngxP, alpha);
1372 }
1373 break;
1374
1375 default:
1376 pm_error("unknown PNG color type: %d",
1377 pngx_colorType(pngxP));
1378 }
1379 }
1380 }
1381
1382
1383
1384 static void
1385 reportOutputFormat(const struct pam * const pamP) {
1386
1387 switch (pamP->format) {
1388
1389 case PBM_FORMAT:
1390 pm_message("Writing a PBM file");
1391 break;
1392 case PGM_FORMAT:
1393 pm_message("Writing a PGM file with maxval %lu", pamP->maxval);
1394 break;
1395 case PPM_FORMAT:
1396 pm_message("Writing a PPM file with maxval %lu", pamP->maxval);
1397 break;
1398 case PAM_FORMAT:
1399 pm_message("Writing a PAM file with tuple type %s, maxval %lu",
1400 pamP->tuple_type, pamP->maxval);
1401 break;
1402 default:
1403 assert(false); /* Every possible value handled above */
1404 }
1405 }
1406
1407
1408
1409 static void
1410 writeNetpbm(struct pam * const pamP,
1411 struct pngx * const pngxP,
1412 Reader * const rasterReaderP,
1413 pngcolor const bgColor,
1414 enum AlphaHandling const alphaHandling,
1415 GammaCorrection const gamma) {
1416 /*----------------------------------------------------------------------------
1417 Write a Netpbm image of either the image or the alpha mask, according to
1418 'alphaHandling' that is in the PNG image described by *pngxP, reading
1419 its raster with the raster reader object *rasterReaderP.
1420
1421 *pamP describes the required output image and is consistent with
1422 *pngInfoP.
1423
1424 Use background color 'bgColor' in the output if the PNG is such that a
1425 background color is needed.
1426 -----------------------------------------------------------------------------*/
1427 tuple * tuplerow;
1428 unsigned int row;
1429
1430 if (verbose)
1431 reportOutputFormat(pamP);
1432
1433 pnm_writepaminit(pamP);
1434
1435 tuplerow = pnm_allocpamrow(pamP);
1436
1437 for (row = 0; row < pngx_imageHeight(pngxP); ++row) {
1438 png_byte * const pngRow = reader_read(rasterReaderP);
1439
1440 assert(pngRow);
1441
1442 makeTupleRow(pamP, tuplerow, pngxP, pngRow, bgColor,
1443 alphaHandling, gamma);
1444
1445 pnm_writepamrow(pamP, tuplerow);
1446 }
1447 pnm_freepamrow(tuplerow);
1448 }
1449
1450
1451
1452 static void
1453 convertpng(FILE * const ifP,
1454 FILE * const tfP,
1455 struct CmdlineInfo const cmdline,
1456 int * const errorLevelP) {
1457
1458 Reader * rasterReaderP;
1459 pngcolor bgColor;
1460 GammaCorrection gamma;
1461 struct pam pam;
1462 struct pngx * pngxP;
1463
1464 *errorLevelP = 0;
1465
1466 pngx_create(&pngxP, PNGX_READ, NULL);
1467
1468 pngx_readStart(pngxP, ifP);
1469
1470 if (verbose)
1471 dumpPngInfo(pngxP);
1472
1473 if (cmdline.time)
1474 showTime(pngxP);
1475 if (tfP)
1476 saveText(pngxP, tfP);
1477
1478 warnNonsquarePixels(pngxP, errorLevelP);
1479
1480 setupGammaCorrection(pngxP, cmdline.gammaSpec, cmdline.gamma, &gamma);
1481
1482 setupSignificantBits(pngxP, cmdline.alpha, errorLevelP);
1483
1484 getBackgroundColor(pngxP, cmdline.background, gamma, pngxP->maxval,
1485 &bgColor);
1486
1487 pam.size = sizeof(pam);
1488 pam.len = PAM_STRUCT_SIZE(tuple_type);
1489 pam.file = stdout;
1490 pam.plainformat = 0;
1491 pam.height = pngx_imageHeight(pngxP);
1492 pam.width = pngx_imageWidth(pngxP);
1493 pam.maxval = pngxP->maxval;
1494
1495 determineOutputType(pngxP, cmdline.alpha, bgColor, pngxP->maxval,
1496 &pam.format, &pam.depth, pam.tuple_type);
1497
1498 rasterReaderP = cmdline.byrow ?
1499 reader_createRowByRow(pngxP, ifP) : reader_createAllAtOnce(pngxP, ifP);
1500
1501 writeNetpbm(&pam, pngxP, rasterReaderP, bgColor,
1502 cmdline.alpha, gamma);
1503
1504 reader_destroy(rasterReaderP);
1505
1506 pngx_readEnd(pngxP);
1507
1508 fflush(stdout);
1509
1510 pngx_destroy(pngxP);
1511 }
1512
1513
1514
1515 int
1516 main(int argc, const char *argv[]) {
1517
1518 struct CmdlineInfo cmdline;
1519 FILE * ifP;
1520 FILE * tfP;
1521 int errorLevel;
1522
1523 pm_proginit(&argc, argv);
1524
1525 parseCommandLine(argc, argv, &cmdline);
1526
1527 verbose = cmdline.verbose;
1528
1529 ifP = pm_openr(cmdline.inputFileName);
1530
1531 if (cmdline.text)
1532 tfP = pm_openw(cmdline.text);
1533 else
1534 tfP = NULL;
1535
1536 convertpng(ifP, tfP, cmdline, &errorLevel);
1537
1538 if (tfP)
1539 pm_close(tfP);
1540
1541 pm_close(ifP);
1542 pm_close(stdout);
1543
1544 return errorLevel;
1545 }
1546
1547
1548