Debian Codegraph @

converter/other/pngx.c

raw
1 #include <stdbool.h>
2 #include <assert.h>
3 #include <png.h>
4 /* Because of a design error in png.h, you must not #include <setjmp.h> before
5 <png.h>. If you do, png.h won't compile.
6 */
7 #include <setjmp.h>
8 #include "pm_c_util.h"
9 #include "mallocvar.h"
10 #include "nstring.h"
11 #include "pm.h"
12
13 /* <png.h> defines (or doesn't) PNG_iTXt_SUPPORTED to tell us whether libpng
14 has facilities related to PNG iTXt chunks.
15 */
16 #ifdef PNG_iTXt_SUPPORTED
17 #define HAVE_PNGLIB_WITH_ITXT 1
18 #else
19 #define HAVE_PNGLIB_WITH_ITXT 0
20 #endif
21
22 #include "pngx.h"
23
24
25 static void
26 errorHandler(png_structp const png_ptr,
27 png_const_charp const msg) {
28
29 jmp_buf * jmpbufP;
30
31 /* This function, aside from the extra step of retrieving the "error
32 pointer" (below) and the fact that it exists within the application
33 rather than within libpng, is essentially identical to libpng's
34 default error handler. The second point is critical: since both
35 setjmp() and longjmp() are called from the same code, they are
36 guaranteed to have compatible notions of how big a jmp_buf is,
37 regardless of whether _BSD_SOURCE or anything else has (or has not)
38 been defined.
39 */
40
41 pm_errormsg("fatal libpng error: %s", msg);
42
43 jmpbufP = png_get_error_ptr(png_ptr);
44
45 if (!jmpbufP)
46 pm_longjmp();
47 else
48 longjmp(*jmpbufP, 1);
49 }
50
51
52
53 void
54 pngx_create(struct pngx ** const pngxPP,
55 pngx_rw const rw,
56 jmp_buf * const jmpbufP) {
57
58 struct pngx * pngxP;
59
60 MALLOCVAR(pngxP);
61
62 if (!pngxP)
63 pm_error("Failed to allocate memory for PNG object");
64 else {
65 pngxP->infoPrepared = false;
66 pngxP->numPassesRequired = 1;
67
68 switch(rw) {
69 case PNGX_READ:
70 pngxP->png_ptr = png_create_read_struct(
71 PNG_LIBPNG_VER_STRING,
72 jmpbufP, errorHandler, NULL);
73 break;
74 case PNGX_WRITE:
75 pngxP->png_ptr = png_create_write_struct(
76 PNG_LIBPNG_VER_STRING,
77 jmpbufP, errorHandler, NULL);
78 break;
79 }
80 if (!pngxP->png_ptr)
81 pm_error("cannot allocate main libpng structure (png_ptr)");
82 else {
83 pngxP->info_ptr = png_create_info_struct(pngxP->png_ptr);
84
85 if (!pngxP->info_ptr)
86 pm_error("cannot allocate libpng info structure (info_ptr)");
87 else
88 *pngxPP = pngxP;
89 }
90 pngxP->rw = rw;
91 }
92 }
93
94
95
96 void
97 pngx_destroy(struct pngx * const pngxP) {
98
99 switch(pngxP->rw) {
100 case PNGX_READ:
101 png_destroy_read_struct(&pngxP->png_ptr, &pngxP->info_ptr, NULL);
102 break;
103 case PNGX_WRITE:
104 png_destroy_write_struct(&pngxP->png_ptr, &pngxP->info_ptr);
105 break;
106 }
107
108 free(pngxP);
109 }
110
111
112
113 bool
114 pngx_chunkIsPresent(struct pngx * const pngxP,
115 uint32_t const chunkType) {
116
117 return png_get_valid(pngxP->png_ptr, pngxP->info_ptr, chunkType);
118 }
119
120
121
122 unsigned int
123 pngx_bitDepth(struct pngx * const pngxP) {
124
125 return png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr);
126 }
127
128
129
130 png_color_16
131 pngx_bkgd(struct pngx * const pngxP) {
132
133 png_color_16 * colorP;
134
135 png_get_bKGD(pngxP->png_ptr, pngxP->info_ptr, &colorP);
136
137 return *colorP;
138 }
139
140
141
142 png_byte
143 pngx_colorType(struct pngx * const pngxP) {
144
145 return png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
146 }
147
148
149
150 png_byte
151 pngx_filterType(struct pngx * const pngxP) {
152
153 return png_get_filter_type(pngxP->png_ptr, pngxP->info_ptr);
154 }
155
156
157
158 double
159 pngx_gama(struct pngx * const pngxP) {
160
161 double retval;
162
163 png_get_gAMA(pngxP->png_ptr, pngxP->info_ptr, &retval);
164
165 return retval;
166 }
167
168
169
170 uint32_t
171 pngx_imageHeight(struct pngx * const pngxP) {
172
173 return png_get_image_height(pngxP->png_ptr, pngxP->info_ptr);
174 }
175
176
177
178 uint32_t
179 pngx_imageWidth(struct pngx * const pngxP) {
180
181 return png_get_image_width(pngxP->png_ptr, pngxP->info_ptr);
182 }
183
184
185
186 png_byte
187 pngx_interlaceType(struct pngx * const pngxP) {
188
189 return png_get_interlace_type(pngxP->png_ptr, pngxP->info_ptr);
190 }
191
192
193
194 struct pngx_plte
195 pngx_plte(struct pngx * const pngxP) {
196
197 struct pngx_plte retval;
198
199 int size;
200
201 png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &retval.palette, &size);
202
203 assert(size >= 0);
204
205 retval.size = size;
206
207 return retval;
208 }
209
210
211
212 png_color_8
213 pngx_sbit(struct pngx * const pngxP) {
214
215 png_color_8 * sbitP;
216
217 png_get_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sbitP);
218
219 return *sbitP;
220 }
221
222
223
224 struct pngx_text
225 pngx_text(struct pngx * const pngxP) {
226
227 struct pngx_text retval;
228
229 int size;
230
231 png_get_text(pngxP->png_ptr, pngxP->info_ptr, &retval.line, &size);
232
233 assert(size >= 0);
234
235 retval.size = size;
236
237 return retval;
238 }
239
240
241
242 png_time
243 pngx_time(struct pngx * const pngxP) {
244
245 png_time * timeP;
246
247 png_get_tIME(pngxP->png_ptr, pngxP->info_ptr, &timeP);
248
249 return *timeP;
250 }
251
252
253
254 struct pngx_trns
255 pngx_trns(struct pngx * const pngxP) {
256
257 struct pngx_trns retval;
258
259 int numTrans;
260 png_color_16 * transColorP;
261
262 png_get_tRNS(pngxP->png_ptr, pngxP->info_ptr,
263 &retval.trans, &numTrans, &transColorP);
264
265 assert(numTrans >= 0);
266
267 retval.numTrans = numTrans;
268 retval.transColor = *transColorP;
269
270 return retval;
271 }
272
273
274
275 uint32_t
276 pngx_xPixelsPerMeter(struct pngx * const pngxP) {
277 /*----------------------------------------------------------------------------
278 Horizontal pixel density in pixel per meter; 0 if unknown.
279 -----------------------------------------------------------------------------*/
280 return png_get_x_pixels_per_meter(pngxP->png_ptr, pngxP->info_ptr);
281 }
282
283
284
285 float
286 pngx_pixelAspectRatio(struct pngx * const pngxP) {
287 /*----------------------------------------------------------------------------
288 Aspect ratio - y/x. 0.0 if unknown
289 -----------------------------------------------------------------------------*/
290 return png_get_pixel_aspect_ratio(pngxP->png_ptr, pngxP->info_ptr);
291 }
292
293
294
295 bool
296 pngx_pixelAspectRatioIsKnown(struct pngx * const pngxP) {
297 /*----------------------------------------------------------------------------
298 There is pixel aspect ratio information in the PNG image.
299 -----------------------------------------------------------------------------*/
300 return png_get_pixel_aspect_ratio(pngxP->png_ptr, pngxP->info_ptr) != 0.0;
301 }
302
303
304
305 uint32_t
306 pngx_yPixelsPerMeter(struct pngx * const pngxP) {
307 /*----------------------------------------------------------------------------
308 Vertical pixel density in pixel per meter; 0 if unknown.
309 -----------------------------------------------------------------------------*/
310 return png_get_y_pixels_per_meter(pngxP->png_ptr, pngxP->info_ptr);
311 }
312
313
314
315 void
316 pngx_removeChunk(struct pngx * const pngxP,
317 uint32_t const chunkType) {
318
319 png_set_invalid(pngxP->png_ptr, pngxP->info_ptr, chunkType);
320 }
321
322
323
324 void
325 pngx_setBkgdPalette(struct pngx * const pngxP,
326 unsigned int const backgroundIndex) {
327
328 png_color_16 background;
329
330 background.index = backgroundIndex;
331
332 png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
333 }
334
335
336
337 void
338 pngx_setBkgdRgb(struct pngx * const pngxP,
339 png_color_16 const backgroundArg) {
340
341 png_color_16 background;
342
343 background = backgroundArg;
344
345 png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
346 }
347
348
349
350 void
351 pngx_setChrm(struct pngx * const pngxP,
352 struct pngx_chroma const chroma) {
353
354 png_set_cHRM(pngxP->png_ptr, pngxP->info_ptr,
355 chroma.wx, chroma.wy,
356 chroma.rx, chroma.ry,
357 chroma.gx, chroma.gy,
358 chroma.bx, chroma.by);
359 }
360
361
362
363 const char *
364 pngx_srgbIntentDesc(pngx_srgbIntent const srgbIntent) {
365
366 switch (srgbIntent) {
367 case PNGX_PERCEPTUAL: return "PERCEPTUAL";
368 case PNGX_RELATIVE_COLORIMETRIC: return "RELATIVE COLORIMETRIC";
369 case PNGX_SATURATION: return "SATURATION";
370 case PNGX_ABSOLUTE_COLORIMETRIC: return "ABSOLUTE_COLORIMETRIC";
371 }
372 assert(false);
373 }
374
375
376
377 static int
378 const libpngSrgbIntentCode(pngx_srgbIntent const srgbIntent) {
379
380 switch (srgbIntent) {
381 case PNGX_PERCEPTUAL: return 0;
382 case PNGX_RELATIVE_COLORIMETRIC: return 1;
383 case PNGX_SATURATION: return 2;
384 case PNGX_ABSOLUTE_COLORIMETRIC: return 3;
385 }
386
387 assert(false); /* All cases above return */
388 }
389
390
391
392 void
393 pngx_setSrgb(struct pngx * const pngxP,
394 pngx_srgbIntent const srgbIntent) {
395
396 png_set_sRGB(pngxP->png_ptr, pngxP->info_ptr,
397 libpngSrgbIntentCode(srgbIntent));
398 }
399
400
401
402 void
403 pngx_setCompressionSize(struct pngx * const pngxP,
404 unsigned int const bufferSize) {
405
406 #if PNG_LIBPNG_VER >= 10009
407 png_set_compression_buffer_size(pngxP->png_ptr, bufferSize);
408 #else
409 pm_error("Your PNG library cannot set the compression buffer size. "
410 "You need at least Version 1.0.9 of Libpng; you have Version %s",
411 PNG_LIBPNG_VER_STRING);
412 #endif
413 }
414
415
416
417 void
418 pngx_setFilter(struct pngx * const pngxP,
419 int const filterSet) {
420
421 /* This sets the allowed filters in the compressor. The filters, and thus
422 the interpretation of 'filterSet', is specific to a filter method (aka
423 filter type), which you set with pngx_setIhdr. There is only one
424 filter method defined today, though (PNG_FILTER_TYPE_BASE).
425
426 For filter method Base, 'filterSet' is the OR of the following masks,
427 each one allowing the compressor to use one filter. Not that the
428 compressor decides on a row-by-row basis what filter to use.
429
430 PNG_FILTER_NONE
431 PNG_FILTER_SUB
432 PNG_FILTER_UP
433 PNG_FILTER_AVG
434 PNG_FILTER_PAETH
435
436 There are also
437
438 PNG_NO_FILTERS
439 PNG_ALL_FILTERS
440 */
441 png_set_filter(pngxP->png_ptr, 0, filterSet);
442 }
443
444
445
446 void
447 pngx_setGama(struct pngx * const pngxP,
448 float const fileGamma) {
449
450 png_set_gAMA(pngxP->png_ptr, pngxP->info_ptr, fileGamma);
451 }
452
453
454
455 void
456 pngx_setGamma(struct pngx * const pngxP,
457 float const screenGamma,
458 float const imageGamma) {
459
460 png_set_gamma(pngxP->png_ptr, screenGamma, imageGamma);
461 }
462
463
464
465 void
466 pngx_setHist(struct pngx * const pngxP,
467 png_uint_16 * const histogram) {
468
469 png_set_hIST(pngxP->png_ptr, pngxP->info_ptr, histogram);
470 }
471
472
473
474 void
475 pngx_setIhdr(struct pngx * const pngxP,
476 unsigned int const width,
477 unsigned int const height,
478 unsigned int const bitDepth,
479 int const colorType,
480 int const interlaceMethod,
481 int const compressionMethod,
482 int const filterMethod) {
483
484 png_set_IHDR(pngxP->png_ptr, pngxP->info_ptr, width, height,
485 bitDepth, colorType, interlaceMethod, compressionMethod,
486 filterMethod);
487 }
488
489
490
491 void
492 pngx_setInterlaceHandling(struct pngx * const pngxP) {
493 /* The documentation is vague and contradictory on what this does, but
494 what it appears from reasoning and experimentation to do is the
495 following.
496
497 It applies to reading and writing by rows (png_write_row, png_read_row)
498 as opposed to whole image (png_write_image, png_read_image). It has
499 no effect on whole image read and write.
500
501 This is not what makes an image interlaced or tells the decompressor
502 that it is interlaced. All it does is control how you you read and
503 write the raster when the image is interlaced. It has no effect if the
504 image is not interlaced. (You make an image interlaced by setting the
505 IHDR; the decompressor finds out from the IHDR that it is interlaced).
506
507 In the write case, it controls whether you construct the subimages
508 yourself and feed them to libpng in sequence or you feed libpng the
509 entire image multiple times and libpng picks out the pixels appropriate
510 for each subimage in each pass.
511
512 In the read case, it controls whether you get the raw subimages and you
513 assemble them into the full image or you read the whole image multiple
514 times into the same buffer, with the pixels that belong to each
515 subimage being filled in on each pass.
516
517 Note that the only kind of interlacing that exists today is ADAM7 and
518 consequently, the number of passes is always 1 (for no interlacing) or
519 7 (for interlacing).
520 */
521 if (!pngxP->infoPrepared)
522 pm_error("pngx_setInterlaceHandling must not be called before "
523 "pngx_writeInfo or pngx_readInfo");
524
525 pngxP->numPassesRequired = png_set_interlace_handling(pngxP->png_ptr);
526 }
527
528
529
530 void
531 pngx_setPacking(struct pngx * const pngxP) {
532
533 if (!pngxP->infoPrepared)
534 pm_error("pngx_setPacking must not be called before "
535 "pngx_writeInfo or pngx_readInfo");
536
537 png_set_packing(pngxP->png_ptr);
538 }
539
540
541
542 void
543 pngx_setPhys(struct pngx * const pngxP,
544 struct pngx_phys const phys) {
545
546 png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr,
547 phys.x, phys.y, phys.unit);
548 }
549
550
551
552 void
553 pngx_setPlte(struct pngx * const pngxP,
554 png_color * const palette,
555 unsigned int const paletteSize) {
556
557 png_set_PLTE(pngxP->png_ptr, pngxP->info_ptr, palette, paletteSize);
558 }
559
560
561
562 void
563 pngx_setSbit(struct pngx * const pngxP,
564 png_color_8 const sbitArg) {
565
566 png_color_8 sbit;
567
568 sbit = sbitArg;
569
570 png_set_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sbit);
571 }
572
573
574
575 void
576 pngx_setShift(struct pngx * const pngxP,
577 png_color_8 const sigBitArg) {
578 /*----------------------------------------------------------------------------
579 Tell the number of significant bits in the row buffers that will be given
580 to the compressor. Those bits are the least significant of the 8 bits of
581 space in the row buffer for each sample. For example, if red sample values
582 are in the range 0-7, only the lower 3 bits of the 8-bit byte for each
583 red sample will be used, so one would call this with sigBitArg.red == 3.
584
585 The name alludes to the fact that to normalize the sample to 8 bits, one
586 shifts it left, and this function tells how much shift has to happen. In
587 the example above, each red sample has to be shifted left 5 bits (so that
588 the upper 3 bits are significant and the lower 5 bits are always zero) to
589 create an 8 bit sample out of the 3 bit samples.
590 -----------------------------------------------------------------------------*/
591 png_color_8 sigBit;
592
593 sigBit = sigBitArg;
594
595 png_set_shift(pngxP->png_ptr, &sigBit);
596 }
597
598
599
600 void
601 pngx_setSigBytes(struct pngx * const pngxP,
602 unsigned int const sigByteCt) {
603
604 assert(sigByteCt <= INT_MAX);
605
606 png_set_sig_bytes(pngxP->png_ptr, sigByteCt);
607 }
608
609
610
611 void
612 pngx_setTextKey(png_text * const textP,
613 const char * const key) {
614
615 /* textP->key is improperly declared in libpng as char *; should
616 be const char *
617 */
618 textP->key = (char *)pm_strdup(key);
619 }
620
621
622
623 void
624 pngx_setTextLang(png_text * const textP,
625 const char * const language) {
626
627 #if HAVE_PNGLIB_WITH_ITXT
628 if (language)
629 textP->lang = (char *)pm_strdup(language);
630 else
631 textP->lang = NULL;
632 #else
633 if (language)
634 pm_error("PNG library does not have ability to create an iTXT "
635 "chunk (i.e. to create a PNG with text strings in a language "
636 "other than English)");
637 #endif
638 }
639
640
641
642 void
643 pngx_setTextLangKey(png_text * const textP,
644 const char * const key) {
645
646 #if HAVE_PNGLIB_WITH_ITXT
647 textP->lang_key = (char *)pm_strdup(key);
648 #else
649 pm_error("PNG library does not have ability to create an iTXT "
650 "chunk (i.e. to create a PNG with text strings in a language "
651 "other than English)");
652 #endif
653 }
654
655
656
657 void
658 pngx_termText(png_text * const textP) {
659
660 pm_strfree(textP->key);
661
662 #if HAVE_PNGLIB_WITH_ITXT
663 if (textP->lang) {
664 pm_strfree(textP->lang);
665 pm_strfree(textP->lang_key);
666 }
667 #endif
668
669 free(textP->text);
670 }
671
672
673
674 void
675 pngx_setText(struct pngx * const pngxP,
676 png_text * const textP,
677 unsigned int const count) {
678
679 png_set_text(pngxP->png_ptr, pngxP->info_ptr, textP, count);
680 }
681
682
683
684 void
685 pngx_setTime(struct pngx * const pngxP,
686 time_t const timeArg) {
687
688 png_time pngTime;
689
690 png_convert_from_time_t(&pngTime, timeArg);
691
692 png_set_tIME(pngxP->png_ptr, pngxP->info_ptr, &pngTime);
693 }
694
695
696
697 void
698 pngx_setTrnsPalette(struct pngx * const pngxP,
699 const png_byte * const transPalette,
700 unsigned int const paletteSize) {
701
702 png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
703 (png_byte *)transPalette, paletteSize, NULL);
704 }
705
706
707
708 void
709 pngx_setTrnsValue(struct pngx * const pngxP,
710 png_color_16 const transColorArg) {
711
712 png_color_16 transColor;
713
714 transColor = transColorArg;
715
716 png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
717 NULL, 0, &transColor);
718 }
719
720
721
722 void
723 pngx_readInfo(struct pngx * const pngxP) {
724
725 png_read_info(pngxP->png_ptr, pngxP->info_ptr);
726
727 pngxP->infoPrepared = true;
728 }
729
730
731
732 void
733 pngx_writeInfo(struct pngx * const pngxP) {
734
735 png_write_info(pngxP->png_ptr, pngxP->info_ptr);
736
737 pngxP->infoPrepared = true;
738 }
739
740
741
742 static void
743 verifyFileIsPng(FILE * const ifP,
744 size_t * const consumedByteCtP) {
745
746 unsigned char buffer[4];
747 size_t bytesRead;
748
749 bytesRead = fread(buffer, 1, sizeof(buffer), ifP);
750 if (bytesRead != sizeof(buffer))
751 pm_error("input file is empty or too short");
752
753 if (png_sig_cmp(buffer, (png_size_t) 0, (png_size_t) sizeof(buffer)) != 0)
754 pm_error("input file is not a PNG file "
755 "(does not have the PNG signature in its first 4 bytes)");
756 else
757 *consumedByteCtP = bytesRead;
758 }
759
760
761
762 void
763 pngx_readStart(struct pngx * const pngxP,
764 FILE * const ifP) {
765
766 size_t sigByteCt;
767
768 verifyFileIsPng(ifP, &sigByteCt);
769
770 /* Declare that we already read the signature bytes */
771 pngx_setSigBytes(pngxP, (unsigned int)sigByteCt);
772
773 png_init_io(pngxP->png_ptr, ifP);
774
775 pngx_readInfo(pngxP);
776
777 if (pngx_bitDepth(pngxP) < 8)
778 pngx_setPacking(pngxP);
779 }
780
781
782
783 void
784 pngx_readRow(struct pngx * const pngxP,
785 png_byte * const rowBuf,
786 png_byte * const displayRow) {
787
788 png_read_row(pngxP->png_ptr, rowBuf, displayRow);
789 }
790
791
792
793 void
794 pngx_readImage(struct pngx * const pngxP,
795 png_byte ** const image) {
796
797 png_read_image(pngxP->png_ptr, image);
798 }
799
800
801
802 void
803 pngx_writeRow(struct pngx * const pngxP,
804 const png_byte * const line) {
805
806 png_write_row(pngxP->png_ptr, (png_byte *)line);
807 }
808
809
810
811 void
812 pngx_writeImage(struct pngx * const pngxP,
813 png_byte ** const raster) {
814
815 png_write_image(pngxP->png_ptr, (png_byte **)raster);
816 }
817
818
819
820 void
821 pngx_readEnd(struct pngx * const pngxP) {
822
823 /* Note that some of info_ptr is not defined until png_read_end()
824 completes. That's because it comes from chunks that are at the
825 end of the stream. In particular, text and time chunks may
826 be at the end. Furthermore, they may be in both places, in
827 which case info_ptr contains different information before and
828 after png_read_end().
829 */
830 png_read_end(pngxP->png_ptr, pngxP->info_ptr);
831 }
832
833
834
835 void
836 pngx_writeEnd(struct pngx * const pngxP) {
837
838 png_write_end(pngxP->png_ptr, pngxP->info_ptr);
839 }
840
841
842