about summary refs log tree commit diff
path: root/converter/other/pngx.c
blob: 7840b2da51eaa196e85006c6a16129dda7675762 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#include <stdbool.h>
#include <assert.h>
#include <png.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "pm.h"

/* <png.h> defines (or doesn't) PNG_iTXt_SUPPORTED to tell us whether libpng
   has facilities related to PNG iTXt chunks.
*/
#ifdef PNG_iTXt_SUPPORTED
  #define HAVE_PNGLIB_WITH_ITXT 1
#else
  #define HAVE_PNGLIB_WITH_ITXT 0
#endif

#include "pngx.h"


static void
errorHandler(png_structp     const png_ptr,
             png_const_charp const msg) {

    jmp_buf * jmpbufP;

    /* This function, aside from the extra step of retrieving the "error
       pointer" (below) and the fact that it exists within the application
       rather than within libpng, is essentially identical to libpng's
       default error handler.  The second point is critical:  since both
       setjmp() and longjmp() are called from the same code, they are
       guaranteed to have compatible notions of how big a jmp_buf is,
       regardless of whether _BSD_SOURCE or anything else has (or has not)
       been defined.
    */

    pm_message("fatal libpng error: %s", msg);

    jmpbufP = png_get_error_ptr(png_ptr);

    if (!jmpbufP) {
        /* we are completely hosed now */
        pm_error("EXTREMELY fatal error: jmpbuf unrecoverable; terminating.");
    }

    longjmp(*jmpbufP, 1);
}



void
pngx_create(struct pngx ** const pngxPP,
            pngx_rw        const rw,
            jmp_buf *      const jmpbufP) {

    struct pngx * pngxP;

    MALLOCVAR(pngxP);

    if (!pngxP)
        pm_error("Failed to allocate memory for PNG object");
    else {
        pngxP->infoPrepared = false;
        pngxP->numPassesRequired = 1;

        switch(rw) {
        case PNGX_READ:
            pngxP->png_ptr = png_create_read_struct(
                PNG_LIBPNG_VER_STRING,
                jmpbufP, errorHandler, NULL);
            break;
        case PNGX_WRITE:
            pngxP->png_ptr = png_create_write_struct(
                PNG_LIBPNG_VER_STRING,
                jmpbufP, errorHandler, NULL);
            break;
        }
        if (!pngxP->png_ptr)
            pm_error("cannot allocate main libpng structure (png_ptr)");
        else {
            pngxP->info_ptr = png_create_info_struct(pngxP->png_ptr);

            if (!pngxP->info_ptr)
                pm_error("cannot allocate libpng info structure (info_ptr)");
            else
                *pngxPP = pngxP;
        }
        pngxP->rw = rw;
    }
}



void
pngx_destroy(struct pngx * const pngxP) {

    switch(pngxP->rw) {
    case PNGX_READ:
        png_destroy_read_struct(&pngxP->png_ptr, &pngxP->info_ptr, NULL);
        break;
    case PNGX_WRITE:
        png_destroy_write_struct(&pngxP->png_ptr, &pngxP->info_ptr);
        break;
    }

    free(pngxP);
}



bool
pngx_chunkIsPresent(struct pngx * const pngxP,
                    uint32_t      const chunkType) {

    return png_get_valid(pngxP->png_ptr, pngxP->info_ptr, chunkType);
}



unsigned int
pngx_bitDepth(struct pngx * const pngxP) {

    return png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr);
}



png_color_16
pngx_bkgd(struct pngx * const pngxP) {

    png_color_16 * colorP;

    png_get_bKGD(pngxP->png_ptr, pngxP->info_ptr, &colorP);

    return *colorP;
}



png_byte
pngx_colorType(struct pngx * const pngxP) {

    return png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
}



png_byte
pngx_filterType(struct pngx * const pngxP) {

    return png_get_filter_type(pngxP->png_ptr, pngxP->info_ptr);
}



double
pngx_gama(struct pngx * const pngxP) {

    double retval;

    png_get_gAMA(pngxP->png_ptr, pngxP->info_ptr, &retval);

    return retval;
}



uint32_t
pngx_imageHeight(struct pngx * const pngxP) {

    return png_get_image_height(pngxP->png_ptr, pngxP->info_ptr);
}



uint32_t
pngx_imageWidth(struct pngx * const pngxP) {

    return png_get_image_width(pngxP->png_ptr, pngxP->info_ptr);
}



png_byte
pngx_interlaceType(struct pngx * const pngxP) {

    return png_get_interlace_type(pngxP->png_ptr, pngxP->info_ptr);
}



struct pngx_plte
pngx_plte(struct pngx * const pngxP) {

    struct pngx_plte retval;

    int size;

    png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &retval.palette, &size);

    assert(size >= 0);

    retval.size = size;

    return retval;
}



png_color_8
pngx_sbit(struct pngx *   const pngxP) {

    png_color_8 * sbitP;

    png_get_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sbitP);

    return *sbitP;
}



struct pngx_text
pngx_text(struct pngx * const pngxP) {

    struct pngx_text retval;

    int size;

    png_get_text(pngxP->png_ptr, pngxP->info_ptr, &retval.line, &size);

    assert(size >= 0);

    retval.size = size;

    return retval;
}



png_time
pngx_time(struct pngx * const pngxP) {

    png_time * timeP;

    png_get_tIME(pngxP->png_ptr, pngxP->info_ptr, &timeP);

    return *timeP;
}



struct pngx_trns
pngx_trns(struct pngx * const pngxP) {

    struct pngx_trns retval;

    int numTrans;
    png_color_16 * transColorP;

    png_get_tRNS(pngxP->png_ptr, pngxP->info_ptr,
                 &retval.trans, &numTrans, &transColorP);

    assert(numTrans >= 0);

    retval.numTrans = numTrans;
    retval.transColor = *transColorP;

    return retval;
}



uint32_t
pngx_xPixelsPerMeter(struct pngx * const pngxP) {
/*----------------------------------------------------------------------------
  Horizontal pixel density in pixel per meter; 0 if unknown.
-----------------------------------------------------------------------------*/
    return png_get_x_pixels_per_meter(pngxP->png_ptr, pngxP->info_ptr);
}



float
pngx_pixelAspectRatio(struct pngx * const pngxP) {
/*----------------------------------------------------------------------------
  Aspect ratio - y/x.  0.0 if unknown
-----------------------------------------------------------------------------*/
    return png_get_pixel_aspect_ratio(pngxP->png_ptr, pngxP->info_ptr);
}



bool
pngx_pixelAspectRatioIsKnown(struct pngx * const pngxP) {
/*----------------------------------------------------------------------------
  There is pixel aspect ratio information in the PNG image.
-----------------------------------------------------------------------------*/
    return png_get_pixel_aspect_ratio(pngxP->png_ptr, pngxP->info_ptr) != 0.0;
}



uint32_t
pngx_yPixelsPerMeter(struct pngx * const pngxP) {
/*----------------------------------------------------------------------------
  Vertical pixel density in pixel per meter; 0 if unknown.
-----------------------------------------------------------------------------*/
    return png_get_y_pixels_per_meter(pngxP->png_ptr, pngxP->info_ptr);
}



void
pngx_removeChunk(struct pngx * const pngxP,
                 uint32_t      const chunkType) {

    png_set_invalid(pngxP->png_ptr, pngxP->info_ptr, chunkType);
}



void
pngx_setBkgdPalette(struct pngx * const pngxP,
                    unsigned int  const backgroundIndex) {

    png_color_16 background;

    background.index = backgroundIndex;

    png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
}



void
pngx_setBkgdRgb(struct pngx * const pngxP,
                png_color_16  const backgroundArg) {

    png_color_16 background;

    background = backgroundArg;

    png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
}



void
pngx_setChrm(struct pngx *      const pngxP,
             struct pngx_chroma const chroma) {

    png_set_cHRM(pngxP->png_ptr, pngxP->info_ptr,
                 chroma.wx, chroma.wy,
                 chroma.rx, chroma.ry,
                 chroma.gx, chroma.gy,
                 chroma.bx, chroma.by);
}



const char *
pngx_srgbIntentDesc(pngx_srgbIntent const srgbIntent) {

    switch (srgbIntent) {
    case PNGX_PERCEPTUAL:            return "PERCEPTUAL";
    case PNGX_RELATIVE_COLORIMETRIC: return "RELATIVE COLORIMETRIC";
    case PNGX_SATURATION:            return "SATURATION";
    case PNGX_ABSOLUTE_COLORIMETRIC: return "ABSOLUTE_COLORIMETRIC";
    }
    assert(false);
}



static int
const libpngSrgbIntentCode(pngx_srgbIntent const srgbIntent) {

    switch (srgbIntent) {
    case PNGX_PERCEPTUAL:            return 0;
    case PNGX_RELATIVE_COLORIMETRIC: return 1;
    case PNGX_SATURATION:            return 2;
    case PNGX_ABSOLUTE_COLORIMETRIC: return 3;
    }

    assert(false);  /* All cases above return */
}



void
pngx_setSrgb(struct pngx *   const pngxP,
             pngx_srgbIntent const srgbIntent) {

    png_set_sRGB(pngxP->png_ptr, pngxP->info_ptr,
                 libpngSrgbIntentCode(srgbIntent));
}



void
pngx_setCompressionSize(struct pngx * const pngxP,
                        unsigned int  const bufferSize) {

#if PNG_LIBPNG_VER >= 10009
    png_set_compression_buffer_size(pngxP->png_ptr, bufferSize);
#else
    pm_error("Your PNG library cannot set the compression buffer size.  "
             "You need at least Version 1.0.9 of Libpng; you have Version %s",
             PNG_LIBPNG_VER_STRING);
#endif
}



void
pngx_setFilter(struct pngx * const pngxP,
               int           const filterSet) {

    /* This sets the allowed filters in the compressor.  The filters, and thus
       the interpretation of 'filterSet', is specific to a filter method (aka
       filter type), which you set with pngx_setIhdr.  There is only one
       filter method defined today, though (PNG_FILTER_TYPE_BASE).

       For filter method Base, 'filterSet' is the OR of the following masks,
       each one allowing the compressor to use one filter.  Not that  the
       compressor decides on a row-by-row basis what filter to use.

         PNG_FILTER_NONE
         PNG_FILTER_SUB
         PNG_FILTER_UP
         PNG_FILTER_AVG
         PNG_FILTER_PAETH

       There are also

         PNG_NO_FILTERS
         PNG_ALL_FILTERS
    */
    png_set_filter(pngxP->png_ptr, 0, filterSet);
}



void
pngx_setGama(struct pngx * const pngxP,
             float         const fileGamma) {

    png_set_gAMA(pngxP->png_ptr, pngxP->info_ptr, fileGamma);
}



void
pngx_setGamma(struct pngx * const pngxP,
              float         const screenGamma,
              float         const imageGamma) {

    png_set_gamma(pngxP->png_ptr, screenGamma, imageGamma);
}



void
pngx_setHist(struct pngx * const pngxP,
             png_uint_16 * const histogram) {

    png_set_hIST(pngxP->png_ptr, pngxP->info_ptr, histogram);
}



void
pngx_setIhdr(struct pngx * const pngxP,
             unsigned int  const width,
             unsigned int  const height,
             unsigned int  const bitDepth,
             int           const colorType,
             int           const interlaceMethod,
             int           const compressionMethod,
             int           const filterMethod) {

    png_set_IHDR(pngxP->png_ptr, pngxP->info_ptr, width, height,
                 bitDepth, colorType, interlaceMethod, compressionMethod,
                 filterMethod);
}



void
pngx_setInterlaceHandling(struct pngx * const pngxP) {
    /* The documentation is vague and contradictory on what this does, but
       what it appears from reasoning and experimentation to do is the
       following.

       It applies to reading and writing by rows (png_write_row, png_read_row)
       as opposed to whole image (png_write_image, png_read_image).  It has
       no effect on whole image read and write.

       This is not what makes an image interlaced or tells the decompressor
       that it is interlaced.  All it does is control how you you read and
       write the raster when the image is interlaced.  It has no effect if the
       image is not interlaced.  (You make an image interlaced by setting the
       IHDR; the decompressor finds out from the IHDR that it is interlaced).

       In the write case, it controls whether you construct the subimages
       yourself and feed them to libpng in sequence or you feed libpng the
       entire image multiple times and libpng picks out the pixels appropriate
       for each subimage in each pass.

       In the read case, it controls whether you get the raw subimages and you
       assemble them into the full image or you read the whole image multiple
       times into the same buffer, with the pixels that belong to each
       subimage being filled in on each pass.

       Note that the only kind of interlacing that exists today is ADAM7 and
       consequently, the number of passes is always 1 (for no interlacing) or
       7 (for interlacing).
    */
    if (!pngxP->infoPrepared)
        pm_error("pngx_setInterlaceHandling must not be called before "
                 "pngx_writeInfo or pngx_readInfo");

    pngxP->numPassesRequired = png_set_interlace_handling(pngxP->png_ptr);
}



void
pngx_setPacking(struct pngx * const pngxP) {

    if (!pngxP->infoPrepared)
        pm_error("pngx_setPacking must not be called before "
                 "pngx_writeInfo or pngx_readInfo");

    png_set_packing(pngxP->png_ptr);
}



void
pngx_setPhys(struct pngx *    const pngxP,
             struct pngx_phys const phys) {

    png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr,
                 phys.x, phys.y, phys.unit);
}



void
pngx_setPlte(struct pngx * const pngxP,
             png_color *   const palette,
             unsigned int  const paletteSize) {

    png_set_PLTE(pngxP->png_ptr, pngxP->info_ptr, palette, paletteSize);
}



void
pngx_setSbit(struct pngx * const pngxP,
             png_color_8   const sbitArg) {

    png_color_8 sbit;

    sbit = sbitArg;

    png_set_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sbit);
}



void
pngx_setShift(struct pngx * const pngxP,
              png_color_8   const sigBitArg) {
/*----------------------------------------------------------------------------
   Tell the number of significant bits in the row buffers that will be given
   to the compressor.  Those bits are the least significant of the 8 bits of
   space in the row buffer for each sample.  For example, if red sample values
   are in the range 0-7, only the lower 3 bits of the 8-bit byte for each
   red sample will be used, so one would call this with sigBitArg.red == 3.

   The name alludes to the fact that to normalize the sample to 8 bits, one
   shifts it left, and this function tells how much shift has to happen.  In
   the example above, each red sample has to be shifted left 5 bits (so that
   the upper 3 bits are significant and the lower 5 bits are always zero) to
   create an 8 bit sample out of the 3 bit samples.
-----------------------------------------------------------------------------*/
    png_color_8 sigBit;

    sigBit = sigBitArg;

    png_set_shift(pngxP->png_ptr, &sigBit);
}



void
pngx_setSigBytes(struct pngx * const pngxP,
                 unsigned int  const sigByteCt) {

    assert(sigByteCt <= INT_MAX);

    png_set_sig_bytes(pngxP->png_ptr, sigByteCt);
}



void
pngx_setTextKey(png_text *   const textP,
                const char * const key) {

    /* textP->key is improperly declared in libpng as char *; should
       be const char *
    */
    textP->key = (char *)pm_strdup(key);
}



void
pngx_setTextLang(png_text *   const textP,
                 const char * const language) {

#if HAVE_PNGLIB_WITH_ITXT
    if (language)
        textP->lang = (char *)pm_strdup(language);
    else
        textP->lang = NULL;
#else
    if (language)
        pm_error("PNG library does not have ability to create an iTXT "
                 "chunk (i.e. to create a PNG with text strings in a language "
                 "other than English)");
#endif
}



void
pngx_setTextLangKey(png_text *   const textP,
                    const char * const key) {

#if HAVE_PNGLIB_WITH_ITXT
    textP->lang_key = (char *)pm_strdup(key);
#else
    pm_error("PNG library does not have ability to create an iTXT "
             "chunk (i.e. to create a PNG with text strings in a language "
             "other than English)");
#endif
}


void
pngx_termText(png_text * const textP) {

    pm_strfree(textP->key);

#if HAVE_PNGLIB_WITH_ITXT
    if (textP->lang) {
        pm_strfree(textP->lang);
        pm_strfree(textP->lang_key);
    }
#endif

    free(textP->text);
}



void
pngx_setText(struct pngx * const pngxP,
             png_text *    const textP,
             unsigned int  const count) {

    png_set_text(pngxP->png_ptr, pngxP->info_ptr, textP, count);
}



void
pngx_setTime(struct pngx * const pngxP,
             time_t        const timeArg) {

    png_time pngTime;

    png_convert_from_time_t(&pngTime, timeArg);

    png_set_tIME(pngxP->png_ptr, pngxP->info_ptr, &pngTime);
}



void
pngx_setTrnsPalette(struct pngx *    const pngxP,
                    const png_byte * const transPalette,
                    unsigned int     const paletteSize) {

    png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
                 (png_byte *)transPalette, paletteSize, NULL);
}



void
pngx_setTrnsValue(struct pngx * const pngxP,
                  png_color_16  const transColorArg) {

    png_color_16 transColor;

    transColor = transColorArg;

    png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
                 NULL, 0, &transColor);
}



void
pngx_readInfo(struct pngx * const pngxP) {

    png_read_info(pngxP->png_ptr, pngxP->info_ptr);

    pngxP->infoPrepared = true;
}



void
pngx_writeInfo(struct pngx * const pngxP) {

    png_write_info(pngxP->png_ptr, pngxP->info_ptr);

    pngxP->infoPrepared = true;
}



static void
verifyFileIsPng(FILE *   const ifP,
                size_t * const consumedByteCtP) {

    unsigned char buffer[4];
    size_t bytesRead;

    bytesRead = fread(buffer, 1, sizeof(buffer), ifP);
    if (bytesRead != sizeof(buffer))
        pm_error("input file is empty or too short");

    if (png_sig_cmp(buffer, (png_size_t) 0, (png_size_t) sizeof(buffer)) != 0)
        pm_error("input file is not a PNG file "
                 "(does not have the PNG signature in its first 4 bytes)");
    else
        *consumedByteCtP = bytesRead;
}



void
pngx_readStart(struct pngx * const pngxP,
               FILE *        const ifP) {

    size_t sigByteCt;

    verifyFileIsPng(ifP, &sigByteCt);

    /* Declare that we already read the signature bytes */
    pngx_setSigBytes(pngxP, (unsigned int)sigByteCt);

    png_init_io(pngxP->png_ptr, ifP);

    pngx_readInfo(pngxP);

    if (pngx_bitDepth(pngxP) < 8)
        pngx_setPacking(pngxP);
}



void
pngx_readRow(struct pngx * const pngxP,
             png_byte *    const rowBuf,
             png_byte *    const displayRow) {

    png_read_row(pngxP->png_ptr, rowBuf, displayRow);
}



void
pngx_readImage(struct pngx * const pngxP,
               png_byte **   const image) {

    png_read_image(pngxP->png_ptr, image);
}



void
pngx_writeRow(struct pngx *    const pngxP,
              const png_byte * const line) {

    png_write_row(pngxP->png_ptr, (png_byte *)line);
}



void
pngx_writeImage(struct pngx * const pngxP,
                png_byte **   const raster) {

    png_write_image(pngxP->png_ptr, (png_byte **)raster);
}



void
pngx_readEnd(struct pngx * const pngxP) {

    /* Note that some of info_ptr is not defined until png_read_end()
       completes.  That's because it comes from chunks that are at the
       end of the stream.  In particular, text and time chunks may
       be at the end.  Furthermore, they may be in both places, in
       which case info_ptr contains different information before and
       after png_read_end().
    */
    png_read_end(pngxP->png_ptr, pngxP->info_ptr);
}



void
pngx_writeEnd(struct pngx * const pngxP) {

    png_write_end(pngxP->png_ptr, pngxP->info_ptr);
}