OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libpng / contrib / libtests / pngvalid.c
1
2 /* pngvalid.c - validate libpng by constructing then reading png files.
3  *
4  * Last changed in libpng 1.7.0 [(PENDING RELEASE)]
5  * Copyright (c) 2014 Glenn Randers-Pehrson
6  * Written by John Cunningham Bowler
7  *
8  * This code is released under the libpng license.
9  * For conditions of distribution and use, see the disclaimer
10  * and license in png.h
11  *
12  * NOTES:
13  *   This is a C program that is intended to be linked against libpng.  It
14  *   generates bitmaps internally, stores them as PNG files (using the
15  *   sequential write code) then reads them back (using the sequential
16  *   read code) and validates that the result has the correct data.
17  *
18  *   The program can be modified and extended to test the correctness of
19  *   transformations performed by libpng.
20  */
21
22 #define _POSIX_SOURCE 1
23 #define _ISOC99_SOURCE 1 /* For floating point */
24 #define _GNU_SOURCE 1 /* For the floating point exception extension */
25
26 #include <signal.h>
27 #include <stdio.h>
28
29 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_FEENABLEEXCEPT /* from config.h, if included */
34 #  include <fenv.h>
35 #endif
36
37 /* Define the following to use this test against your installed libpng, rather
38  * than the one being built here:
39  */
40 #ifdef PNG_FREESTANDING_TESTS
41 #  include <png.h>
42 #else
43 #  include "../../png.h"
44 #endif
45
46 #ifdef PNG_ZLIB_HEADER
47 #  include PNG_ZLIB_HEADER
48 #else
49 #  include <zlib.h>   /* For crc32 */
50 #endif
51
52 /* 1.6.1 added support for the configure test harness, which uses 77 to indicate
53  * a skipped test, in earlier versions we need to succeed on a skipped test, so:
54  */
55 #if PNG_LIBPNG_VER < 10601
56 #  define SKIP 0
57 #else
58 #  define SKIP 77
59 #endif
60
61 /* pngvalid requires write support and one of the fixed or floating point APIs.
62  */
63 #if defined(PNG_WRITE_SUPPORTED) &&\
64    (defined(PNG_FIXED_POINT_SUPPORTED) || defined(PNG_FLOATING_POINT_SUPPORTED))
65
66 #if PNG_LIBPNG_VER < 10500
67 /* This deliberately lacks the PNG_CONST. */
68 typedef png_byte *png_const_bytep;
69
70 /* This is copied from 1.5.1 png.h: */
71 #define PNG_INTERLACE_ADAM7_PASSES 7
72 #define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
73 #define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
74 #define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
75 #define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
76 #define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
77    -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
78 #define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
79    -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
80 #define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
81    (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
82 #define PNG_COL_FROM_PASS_COL(xIn, pass) \
83    (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
84 #define PNG_PASS_MASK(pass,off) ( \
85    ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
86    ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
87 #define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
88    ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
89 #define PNG_COL_IN_INTERLACE_PASS(x, pass) \
90    ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
91
92 /* These are needed too for the default build: */
93 #define PNG_WRITE_16BIT_SUPPORTED
94 #define PNG_READ_16BIT_SUPPORTED
95
96 /* This comes from pnglibconf.h afer 1.5: */
97 #define PNG_FP_1 100000
98 #define PNG_GAMMA_THRESHOLD_FIXED\
99    ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
100 #endif
101
102 #if PNG_LIBPNG_VER < 10600
103    /* 1.6.0 constifies many APIs, the following exists to allow pngvalid to be
104     * compiled against earlier versions.
105     */
106 #  define png_const_structp png_structp
107 #endif
108
109 #include <float.h>  /* For floating point constants */
110 #include <stdlib.h> /* For malloc */
111 #include <string.h> /* For memcpy, memset */
112 #include <math.h>   /* For floor */
113
114 /* Unused formal parameter errors are removed using the following macro which is
115  * expected to have no bad effects on performance.
116  */
117 #ifndef UNUSED
118 #  if defined(__GNUC__) || defined(_MSC_VER)
119 #     define UNUSED(param) (void)param;
120 #  else
121 #     define UNUSED(param)
122 #  endif
123 #endif
124
125 /* Fixups for various minimal builds */
126 #ifndef PNG_ERROR_TEXT_SUPPORTED
127 #  define png_error(a,b) png_err(a)
128 #endif
129
130 /***************************** EXCEPTION HANDLING *****************************/
131 #ifdef PNG_FREESTANDING_TESTS
132 #  include <cexcept.h>
133 #else
134 #  include "../visupng/cexcept.h"
135 #endif
136
137 #ifdef __cplusplus
138 #  define this not_the_cpp_this
139 #  define new not_the_cpp_new
140 #  define voidcast(type, value) static_cast<type>(value)
141 #else
142 #  define voidcast(type, value) (value)
143 #endif /* __cplusplus */
144
145 struct png_store;
146 define_exception_type(struct png_store*);
147
148 /* The following are macros to reduce typing everywhere where the well known
149  * name 'the_exception_context' must be defined.
150  */
151 #define anon_context(ps) struct exception_context *the_exception_context = \
152    &(ps)->exception_context
153 #define context(ps,fault) anon_context(ps); png_store *fault
154
155 /******************************* UTILITIES ************************************/
156 /* Error handling is particularly problematic in production code - error
157  * handlers often themselves have bugs which lead to programs that detect
158  * minor errors crashing.  The following functions deal with one very
159  * common class of errors in error handlers - attempting to format error or
160  * warning messages into buffers that are too small.
161  */
162 static size_t safecat(char *buffer, size_t bufsize, size_t pos,
163    PNG_CONST char *cat)
164 {
165    while (pos < bufsize && cat != NULL && *cat != 0)
166       buffer[pos++] = *cat++;
167
168    if (pos >= bufsize)
169       pos = bufsize-1;
170
171    buffer[pos] = 0;
172    return pos;
173 }
174
175 static size_t safecatn(char *buffer, size_t bufsize, size_t pos, int n)
176 {
177    char number[64];
178    sprintf(number, "%d", n);
179    return safecat(buffer, bufsize, pos, number);
180 }
181
182 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
183 static size_t safecatd(char *buffer, size_t bufsize, size_t pos, double d,
184     int precision)
185 {
186    char number[64];
187    sprintf(number, "%.*f", precision, d);
188    return safecat(buffer, bufsize, pos, number);
189 }
190 #endif
191
192 static PNG_CONST char invalid[] = "invalid";
193 static PNG_CONST char sep[] = ": ";
194
195 static PNG_CONST char *colour_types[8] =
196 {
197    "grayscale", invalid, "truecolour", "indexed-colour",
198    "grayscale with alpha", invalid, "truecolour with alpha", invalid
199 };
200
201 #ifdef PNG_READ_SUPPORTED
202 /* Convert a double precision value to fixed point. */
203 static png_fixed_point
204 fix(double d)
205 {
206    d = floor(d * PNG_FP_1 + .5);
207    return (png_fixed_point)d;
208 }
209 #endif /* PNG_READ_SUPPORTED */
210
211 /* Generate random bytes.  This uses a boring repeatable algorithm and it
212  * is implemented here so that it gives the same set of numbers on every
213  * architecture.  It's a linear congruential generator (Knuth or Sedgewick
214  * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
215  * Hill, "The Art of Electronics" (Pseudo-Random Bit Sequences and Noise
216  * Generation.)
217  */
218 static void
219 make_random_bytes(png_uint_32* seed, void* pv, size_t size)
220 {
221    png_uint_32 u0 = seed[0], u1 = seed[1];
222    png_bytep bytes = voidcast(png_bytep, pv);
223
224    /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
225     * bit-20.  The top 1 bit is in u1, the bottom 32 are in u0.
226     */
227    size_t i;
228    for (i=0; i<size; ++i)
229    {
230       /* First generate 8 new bits then shift them in at the end. */
231       png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff;
232       u1 <<= 8;
233       u1 |= u0 >> 24;
234       u0 <<= 8;
235       u0 |= u;
236       *bytes++ = (png_byte)u;
237    }
238
239    seed[0] = u0;
240    seed[1] = u1;
241 }
242
243 static void
244 make_four_random_bytes(png_uint_32* seed, png_bytep bytes)
245 {
246    make_random_bytes(seed, bytes, 4);
247 }
248
249 #ifdef PNG_READ_SUPPORTED
250 static void
251 randomize(void *pv, size_t size)
252 {
253    static png_uint_32 random_seed[2] = {0x56789abc, 0xd};
254    make_random_bytes(random_seed, pv, size);
255 }
256
257 #define RANDOMIZE(this) randomize(&(this), sizeof (this))
258
259 static unsigned int
260 random_mod(unsigned int max)
261 {
262    unsigned int x;
263
264    RANDOMIZE(x);
265
266    return x % max; /* 0 .. max-1 */
267 }
268
269 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
270 static int
271 random_choice(void)
272 {
273    unsigned char x;
274
275    RANDOMIZE(x);
276
277    return x & 1;
278 }
279 #endif
280 #endif /* PNG_READ_SUPPORTED */
281
282 /* A numeric ID based on PNG file characteristics.  The 'do_interlace' field
283  * simply records whether pngvalid did the interlace itself or whether it
284  * was done by libpng.  Width and height must be less than 256.  'palette' is an
285  * index of the palette to use for formats with a palette (0 otherwise.)
286  */
287 #define FILEID(col, depth, palette, interlace, width, height, do_interlace) \
288    ((png_uint_32)((col) + ((depth)<<3) + ((palette)<<8) + ((interlace)<<13) + \
289     (((do_interlace)!=0)<<15) + ((width)<<16) + ((height)<<24)))
290
291 #define COL_FROM_ID(id) ((png_byte)((id)& 0x7U))
292 #define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU))
293 #define PALETTE_FROM_ID(id) (((id) >> 8) & 0x1f)
294 #define INTERLACE_FROM_ID(id) ((int)(((id) >> 13) & 0x3))
295 #define DO_INTERLACE_FROM_ID(id) ((int)(((id)>>15) & 1))
296 #define WIDTH_FROM_ID(id) (((id)>>16) & 0xff)
297 #define HEIGHT_FROM_ID(id) (((id)>>24) & 0xff)
298
299 /* Utility to construct a standard name for a standard image. */
300 static size_t
301 standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type,
302     int bit_depth, unsigned int npalette, int interlace_type,
303     png_uint_32 w, png_uint_32 h, int do_interlace)
304 {
305    pos = safecat(buffer, bufsize, pos, colour_types[colour_type]);
306    if (npalette > 0)
307    {
308       pos = safecat(buffer, bufsize, pos, "[");
309       pos = safecatn(buffer, bufsize, pos, npalette);
310       pos = safecat(buffer, bufsize, pos, "]");
311    }
312    pos = safecat(buffer, bufsize, pos, " ");
313    pos = safecatn(buffer, bufsize, pos, bit_depth);
314    pos = safecat(buffer, bufsize, pos, " bit");
315
316    if (interlace_type != PNG_INTERLACE_NONE)
317    {
318       pos = safecat(buffer, bufsize, pos, " interlaced");
319       if (do_interlace)
320          pos = safecat(buffer, bufsize, pos, "(pngvalid)");
321       else
322          pos = safecat(buffer, bufsize, pos, "(libpng)");
323    }
324
325    if (w > 0 || h > 0)
326    {
327       pos = safecat(buffer, bufsize, pos, " ");
328       pos = safecatn(buffer, bufsize, pos, w);
329       pos = safecat(buffer, bufsize, pos, "x");
330       pos = safecatn(buffer, bufsize, pos, h);
331    }
332
333    return pos;
334 }
335
336 static size_t
337 standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id)
338 {
339    return standard_name(buffer, bufsize, pos, COL_FROM_ID(id),
340       DEPTH_FROM_ID(id), PALETTE_FROM_ID(id), INTERLACE_FROM_ID(id),
341       WIDTH_FROM_ID(id), HEIGHT_FROM_ID(id), DO_INTERLACE_FROM_ID(id));
342 }
343
344 /* Convenience API and defines to list valid formats.  Note that 16 bit read and
345  * write support is required to do 16 bit read tests (we must be able to make a
346  * 16 bit image to test!)
347  */
348 #ifdef PNG_WRITE_16BIT_SUPPORTED
349 #  define WRITE_BDHI 4
350 #  ifdef PNG_READ_16BIT_SUPPORTED
351 #     define READ_BDHI 4
352 #     define DO_16BIT
353 #  endif
354 #else
355 #  define WRITE_BDHI 3
356 #endif
357 #ifndef DO_16BIT
358 #  define READ_BDHI 3
359 #endif
360
361 /* The following defines the number of different palettes to generate for
362  * each log bit depth of a colour type 3 standard image.
363  */
364 #define PALETTE_COUNT(bit_depth) ((bit_depth) > 4 ? 1U : 16U)
365
366 static int
367 next_format(png_bytep colour_type, png_bytep bit_depth,
368    unsigned int* palette_number, int no_low_depth_gray)
369 {
370    if (*bit_depth == 0)
371    {
372       *colour_type = 0;
373       if (no_low_depth_gray)
374          *bit_depth = 8;
375       else
376          *bit_depth = 1;
377       *palette_number = 0;
378       return 1;
379    }
380
381    if (*colour_type == 3)
382    {
383       /* Add multiple palettes for colour type 3. */
384       if (++*palette_number < PALETTE_COUNT(*bit_depth))
385          return 1;
386
387       *palette_number = 0;
388    }
389
390    *bit_depth = (png_byte)(*bit_depth << 1);
391
392    /* Palette images are restricted to 8 bit depth */
393    if (*bit_depth <= 8
394 #     ifdef DO_16BIT
395          || (*colour_type != 3 && *bit_depth <= 16)
396 #     endif
397       )
398       return 1;
399
400    /* Move to the next color type, or return 0 at the end. */
401    switch (*colour_type)
402    {
403       case 0:
404          *colour_type = 2;
405          *bit_depth = 8;
406          return 1;
407
408       case 2:
409          *colour_type = 3;
410          *bit_depth = 1;
411          return 1;
412
413       case 3:
414          *colour_type = 4;
415          *bit_depth = 8;
416          return 1;
417
418       case 4:
419          *colour_type = 6;
420          *bit_depth = 8;
421          return 1;
422
423       default:
424          return 0;
425    }
426 }
427
428 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
429 static unsigned int
430 sample(png_const_bytep row, png_byte colour_type, png_byte bit_depth,
431     png_uint_32 x, unsigned int sample_index)
432 {
433    png_uint_32 bit_index, result;
434
435    /* Find a sample index for the desired sample: */
436    x *= bit_depth;
437    bit_index = x;
438
439    if ((colour_type & 1) == 0) /* !palette */
440    {
441       if (colour_type & 2)
442          bit_index *= 3;
443
444       if (colour_type & 4)
445          bit_index += x; /* Alpha channel */
446
447       /* Multiple channels; select one: */
448       if (colour_type & (2+4))
449          bit_index += sample_index * bit_depth;
450    }
451
452    /* Return the sample from the row as an integer. */
453    row += bit_index >> 3;
454    result = *row;
455
456    if (bit_depth == 8)
457       return result;
458
459    else if (bit_depth > 8)
460       return (result << 8) + *++row;
461
462    /* Less than 8 bits per sample. */
463    bit_index &= 7;
464    return (result >> (8-bit_index-bit_depth)) & ((1U<<bit_depth)-1);
465 }
466 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
467
468 /* Copy a single pixel, of a given size, from one buffer to another -
469  * while this is basically bit addressed there is an implicit assumption
470  * that pixels 8 or more bits in size are byte aligned and that pixels
471  * do not otherwise cross byte boundaries.  (This is, so far as I know,
472  * universally true in bitmap computer graphics.  [JCB 20101212])
473  *
474  * NOTE: The to and from buffers may be the same.
475  */
476 static void
477 pixel_copy(png_bytep toBuffer, png_uint_32 toIndex,
478    png_const_bytep fromBuffer, png_uint_32 fromIndex, unsigned int pixelSize)
479 {
480    /* Assume we can multiply by 'size' without overflow because we are
481     * just working in a single buffer.
482     */
483    toIndex *= pixelSize;
484    fromIndex *= pixelSize;
485    if (pixelSize < 8) /* Sub-byte */
486    {
487       /* Mask to select the location of the copied pixel: */
488       unsigned int destMask = ((1U<<pixelSize)-1) << (8-pixelSize-(toIndex&7));
489       /* The following read the entire pixels and clears the extra: */
490       unsigned int destByte = toBuffer[toIndex >> 3] & ~destMask;
491       unsigned int sourceByte = fromBuffer[fromIndex >> 3];
492
493       /* Don't rely on << or >> supporting '0' here, just in case: */
494       fromIndex &= 7;
495       if (fromIndex > 0) sourceByte <<= fromIndex;
496       if ((toIndex & 7) > 0) sourceByte >>= toIndex & 7;
497
498       toBuffer[toIndex >> 3] = (png_byte)(destByte | (sourceByte & destMask));
499    }
500    else /* One or more bytes */
501       memmove(toBuffer+(toIndex>>3), fromBuffer+(fromIndex>>3), pixelSize>>3);
502 }
503
504 #ifdef PNG_READ_SUPPORTED
505 /* Copy a complete row of pixels, taking into account potential partial
506  * bytes at the end.
507  */
508 static void
509 row_copy(png_bytep toBuffer, png_const_bytep fromBuffer, unsigned int bitWidth)
510 {
511    memcpy(toBuffer, fromBuffer, bitWidth >> 3);
512
513    if ((bitWidth & 7) != 0)
514    {
515       unsigned int mask;
516
517       toBuffer += bitWidth >> 3;
518       fromBuffer += bitWidth >> 3;
519       /* The remaining bits are in the top of the byte, the mask is the bits to
520        * retain.
521        */
522       mask = 0xff >> (bitWidth & 7);
523       *toBuffer = (png_byte)((*toBuffer & mask) | (*fromBuffer & ~mask));
524    }
525 }
526
527 /* Compare pixels - they are assumed to start at the first byte in the
528  * given buffers.
529  */
530 static int
531 pixel_cmp(png_const_bytep pa, png_const_bytep pb, png_uint_32 bit_width)
532 {
533 #if PNG_LIBPNG_VER < 10506
534    if (memcmp(pa, pb, bit_width>>3) == 0)
535    {
536       png_uint_32 p;
537
538       if ((bit_width & 7) == 0) return 0;
539
540       /* Ok, any differences? */
541       p = pa[bit_width >> 3];
542       p ^= pb[bit_width >> 3];
543
544       if (p == 0) return 0;
545
546       /* There are, but they may not be significant, remove the bits
547        * after the end (the low order bits in PNG.)
548        */
549       bit_width &= 7;
550       p >>= 8-bit_width;
551
552       if (p == 0) return 0;
553    }
554 #else
555    /* From libpng-1.5.6 the overwrite should be fixed, so compare the trailing
556     * bits too:
557     */
558    if (memcmp(pa, pb, (bit_width+7)>>3) == 0)
559       return 0;
560 #endif
561
562    /* Return the index of the changed byte. */
563    {
564       png_uint_32 where = 0;
565
566       while (pa[where] == pb[where]) ++where;
567       return 1+where;
568    }
569 }
570 #endif /* PNG_READ_SUPPORTED */
571
572 /*************************** BASIC PNG FILE WRITING ***************************/
573 /* A png_store takes data from the sequential writer or provides data
574  * to the sequential reader.  It can also store the result of a PNG
575  * write for later retrieval.
576  */
577 #define STORE_BUFFER_SIZE 500 /* arbitrary */
578 typedef struct png_store_buffer
579 {
580    struct png_store_buffer*  prev;    /* NOTE: stored in reverse order */
581    png_byte                  buffer[STORE_BUFFER_SIZE];
582 } png_store_buffer;
583
584 #define FILE_NAME_SIZE 64
585
586 typedef struct store_palette_entry /* record of a single palette entry */
587 {
588    png_byte red;
589    png_byte green;
590    png_byte blue;
591    png_byte alpha;
592 } store_palette_entry, store_palette[256];
593
594 typedef struct png_store_file
595 {
596    struct png_store_file*  next;      /* as many as you like... */
597    char                    name[FILE_NAME_SIZE];
598    png_uint_32             id;        /* must be correct (see FILEID) */
599    png_size_t              datacount; /* In this (the last) buffer */
600    png_store_buffer        data;      /* Last buffer in file */
601    int                     npalette;  /* Number of entries in palette */
602    store_palette_entry*    palette;   /* May be NULL */
603 } png_store_file;
604
605 /* The following is a pool of memory allocated by a single libpng read or write
606  * operation.
607  */
608 typedef struct store_pool
609 {
610    struct png_store    *store;   /* Back pointer */
611    struct store_memory *list;    /* List of allocated memory */
612    png_byte             mark[4]; /* Before and after data */
613
614    /* Statistics for this run. */
615    png_alloc_size_t     max;     /* Maximum single allocation */
616    png_alloc_size_t     current; /* Current allocation */
617    png_alloc_size_t     limit;   /* Highest current allocation */
618    png_alloc_size_t     total;   /* Total allocation */
619
620    /* Overall statistics (retained across successive runs). */
621    png_alloc_size_t     max_max;
622    png_alloc_size_t     max_limit;
623    png_alloc_size_t     max_total;
624 } store_pool;
625
626 typedef struct png_store
627 {
628    /* For cexcept.h exception handling - simply store one of these;
629     * the context is a self pointer but it may point to a different
630     * png_store (in fact it never does in this program.)
631     */
632    struct exception_context
633                       exception_context;
634
635    unsigned int       verbose :1;
636    unsigned int       treat_warnings_as_errors :1;
637    unsigned int       expect_error :1;
638    unsigned int       expect_warning :1;
639    unsigned int       saw_warning :1;
640    unsigned int       speed :1;
641    unsigned int       progressive :1; /* use progressive read */
642    unsigned int       validated :1;   /* used as a temporary flag */
643    int                nerrors;
644    int                nwarnings;
645    int                noptions;       /* number of options below: */
646    struct {
647       unsigned char   option;         /* option number, 0..30 */
648       unsigned char   setting;        /* setting (unset,invalid,on,off) */
649    }                  options[16];
650    char               test[128];      /* Name of test */
651    char               error[256];
652
653    /* Read fields */
654    png_structp        pread;    /* Used to read a saved file */
655    png_infop          piread;
656    png_store_file*    current;  /* Set when reading */
657    png_store_buffer*  next;     /* Set when reading */
658    png_size_t         readpos;  /* Position in *next */
659    png_byte*          image;    /* Buffer for reading interlaced images */
660    png_size_t         cb_image; /* Size of this buffer */
661    png_size_t         cb_row;   /* Row size of the image(s) */
662    png_uint_32        image_h;  /* Number of rows in a single image */
663    store_pool         read_memory_pool;
664
665    /* Write fields */
666    png_store_file*    saved;
667    png_structp        pwrite;   /* Used when writing a new file */
668    png_infop          piwrite;
669    png_size_t         writepos; /* Position in .new */
670    char               wname[FILE_NAME_SIZE];
671    png_store_buffer   new;      /* The end of the new PNG file being written. */
672    store_pool         write_memory_pool;
673    store_palette_entry* palette;
674    int                  npalette;
675 } png_store;
676
677 /* Initialization and cleanup */
678 static void
679 store_pool_mark(png_bytep mark)
680 {
681    static png_uint_32 store_seed[2] = { 0x12345678, 1};
682
683    make_four_random_bytes(store_seed, mark);
684 }
685
686 #ifdef PNG_READ_SUPPORTED
687 /* Use this for random 32 bit values; this function makes sure the result is
688  * non-zero.
689  */
690 static png_uint_32
691 random_32(void)
692 {
693
694    for(;;)
695    {
696       png_byte mark[4];
697       png_uint_32 result;
698
699       store_pool_mark(mark);
700       result = png_get_uint_32(mark);
701
702       if (result != 0)
703          return result;
704    }
705 }
706 #endif /* PNG_READ_SUPPORTED */
707
708 static void
709 store_pool_init(png_store *ps, store_pool *pool)
710 {
711    memset(pool, 0, sizeof *pool);
712
713    pool->store = ps;
714    pool->list = NULL;
715    pool->max = pool->current = pool->limit = pool->total = 0;
716    pool->max_max = pool->max_limit = pool->max_total = 0;
717    store_pool_mark(pool->mark);
718 }
719
720 static void
721 store_init(png_store* ps)
722 {
723    memset(ps, 0, sizeof *ps);
724    init_exception_context(&ps->exception_context);
725    store_pool_init(ps, &ps->read_memory_pool);
726    store_pool_init(ps, &ps->write_memory_pool);
727    ps->verbose = 0;
728    ps->treat_warnings_as_errors = 0;
729    ps->expect_error = 0;
730    ps->expect_warning = 0;
731    ps->saw_warning = 0;
732    ps->speed = 0;
733    ps->progressive = 0;
734    ps->validated = 0;
735    ps->nerrors = ps->nwarnings = 0;
736    ps->pread = NULL;
737    ps->piread = NULL;
738    ps->saved = ps->current = NULL;
739    ps->next = NULL;
740    ps->readpos = 0;
741    ps->image = NULL;
742    ps->cb_image = 0;
743    ps->cb_row = 0;
744    ps->image_h = 0;
745    ps->pwrite = NULL;
746    ps->piwrite = NULL;
747    ps->writepos = 0;
748    ps->new.prev = NULL;
749    ps->palette = NULL;
750    ps->npalette = 0;
751    ps->noptions = 0;
752 }
753
754 static void
755 store_freebuffer(png_store_buffer* psb)
756 {
757    if (psb->prev)
758    {
759       store_freebuffer(psb->prev);
760       free(psb->prev);
761       psb->prev = NULL;
762    }
763 }
764
765 static void
766 store_freenew(png_store *ps)
767 {
768    store_freebuffer(&ps->new);
769    ps->writepos = 0;
770    if (ps->palette != NULL)
771    {
772       free(ps->palette);
773       ps->palette = NULL;
774       ps->npalette = 0;
775    }
776 }
777
778 static void
779 store_storenew(png_store *ps)
780 {
781    png_store_buffer *pb;
782
783    if (ps->writepos != STORE_BUFFER_SIZE)
784       png_error(ps->pwrite, "invalid store call");
785
786    pb = voidcast(png_store_buffer*, malloc(sizeof *pb));
787
788    if (pb == NULL)
789       png_error(ps->pwrite, "store new: OOM");
790
791    *pb = ps->new;
792    ps->new.prev = pb;
793    ps->writepos = 0;
794 }
795
796 static void
797 store_freefile(png_store_file **ppf)
798 {
799    if (*ppf != NULL)
800    {
801       store_freefile(&(*ppf)->next);
802
803       store_freebuffer(&(*ppf)->data);
804       (*ppf)->datacount = 0;
805       if ((*ppf)->palette != NULL)
806       {
807          free((*ppf)->palette);
808          (*ppf)->palette = NULL;
809          (*ppf)->npalette = 0;
810       }
811       free(*ppf);
812       *ppf = NULL;
813    }
814 }
815
816 /* Main interface to file storeage, after writing a new PNG file (see the API
817  * below) call store_storefile to store the result with the given name and id.
818  */
819 static void
820 store_storefile(png_store *ps, png_uint_32 id)
821 {
822    png_store_file *pf = voidcast(png_store_file*, malloc(sizeof *pf));
823    if (pf == NULL)
824       png_error(ps->pwrite, "storefile: OOM");
825    safecat(pf->name, sizeof pf->name, 0, ps->wname);
826    pf->id = id;
827    pf->data = ps->new;
828    pf->datacount = ps->writepos;
829    ps->new.prev = NULL;
830    ps->writepos = 0;
831    pf->palette = ps->palette;
832    pf->npalette = ps->npalette;
833    ps->palette = 0;
834    ps->npalette = 0;
835
836    /* And save it. */
837    pf->next = ps->saved;
838    ps->saved = pf;
839 }
840
841 /* Generate an error message (in the given buffer) */
842 static size_t
843 store_message(png_store *ps, png_const_structp pp, char *buffer, size_t bufsize,
844    size_t pos, PNG_CONST char *msg)
845 {
846    if (pp != NULL && pp == ps->pread)
847    {
848       /* Reading a file */
849       pos = safecat(buffer, bufsize, pos, "read: ");
850
851       if (ps->current != NULL)
852       {
853          pos = safecat(buffer, bufsize, pos, ps->current->name);
854          pos = safecat(buffer, bufsize, pos, sep);
855       }
856    }
857
858    else if (pp != NULL && pp == ps->pwrite)
859    {
860       /* Writing a file */
861       pos = safecat(buffer, bufsize, pos, "write: ");
862       pos = safecat(buffer, bufsize, pos, ps->wname);
863       pos = safecat(buffer, bufsize, pos, sep);
864    }
865
866    else
867    {
868       /* Neither reading nor writing (or a memory error in struct delete) */
869       pos = safecat(buffer, bufsize, pos, "pngvalid: ");
870    }
871
872    if (ps->test[0] != 0)
873    {
874       pos = safecat(buffer, bufsize, pos, ps->test);
875       pos = safecat(buffer, bufsize, pos, sep);
876    }
877    pos = safecat(buffer, bufsize, pos, msg);
878    return pos;
879 }
880
881 /* Verbose output to the error stream: */
882 static void
883 store_verbose(png_store *ps, png_const_structp pp, png_const_charp prefix,
884    png_const_charp message)
885 {
886    char buffer[512];
887
888    if (prefix)
889       fputs(prefix, stderr);
890
891    (void)store_message(ps, pp, buffer, sizeof buffer, 0, message);
892    fputs(buffer, stderr);
893    fputc('\n', stderr);
894 }
895
896 /* Log an error or warning - the relevant count is always incremented. */
897 static void
898 store_log(png_store* ps, png_const_structp pp, png_const_charp message,
899    int is_error)
900 {
901    /* The warning is copied to the error buffer if there are no errors and it is
902     * the first warning.  The error is copied to the error buffer if it is the
903     * first error (overwriting any prior warnings).
904     */
905    if (is_error ? (ps->nerrors)++ == 0 :
906        (ps->nwarnings)++ == 0 && ps->nerrors == 0)
907       store_message(ps, pp, ps->error, sizeof ps->error, 0, message);
908
909    if (ps->verbose)
910       store_verbose(ps, pp, is_error ? "error: " : "warning: ", message);
911 }
912
913 #ifdef PNG_READ_SUPPORTED
914 /* Internal error function, called with a png_store but no libpng stuff. */
915 static void
916 internal_error(png_store *ps, png_const_charp message)
917 {
918    store_log(ps, NULL, message, 1 /* error */);
919
920    /* And finally throw an exception. */
921    {
922       struct exception_context *the_exception_context = &ps->exception_context;
923       Throw ps;
924    }
925 }
926 #endif /* PNG_READ_SUPPORTED */
927
928 /* Functions to use as PNG callbacks. */
929 static void PNGCBAPI
930 store_error(png_structp ppIn, png_const_charp message) /* PNG_NORETURN */
931 {
932    png_const_structp pp = ppIn;
933    png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
934
935    if (!ps->expect_error)
936       store_log(ps, pp, message, 1 /* error */);
937
938    /* And finally throw an exception. */
939    {
940       struct exception_context *the_exception_context = &ps->exception_context;
941       Throw ps;
942    }
943 }
944
945 static void PNGCBAPI
946 store_warning(png_structp ppIn, png_const_charp message)
947 {
948    png_const_structp pp = ppIn;
949    png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
950
951    if (!ps->expect_warning)
952       store_log(ps, pp, message, 0 /* warning */);
953    else
954       ps->saw_warning = 1;
955 }
956
957 /* These somewhat odd functions are used when reading an image to ensure that
958  * the buffer is big enough, the png_structp is for errors.
959  */
960 /* Return a single row from the correct image. */
961 static png_bytep
962 store_image_row(PNG_CONST png_store* ps, png_const_structp pp, int nImage,
963    png_uint_32 y)
964 {
965    png_size_t coffset = (nImage * ps->image_h + y) * (ps->cb_row + 5) + 2;
966
967    if (ps->image == NULL)
968       png_error(pp, "no allocated image");
969
970    if (coffset + ps->cb_row + 3 > ps->cb_image)
971       png_error(pp, "image too small");
972
973    return ps->image + coffset;
974 }
975
976 static void
977 store_image_free(png_store *ps, png_const_structp pp)
978 {
979    if (ps->image != NULL)
980    {
981       png_bytep image = ps->image;
982
983       if (image[-1] != 0xed || image[ps->cb_image] != 0xfe)
984       {
985          if (pp != NULL)
986             png_error(pp, "png_store image overwrite (1)");
987          else
988             store_log(ps, NULL, "png_store image overwrite (2)", 1);
989       }
990
991       ps->image = NULL;
992       ps->cb_image = 0;
993       --image;
994       free(image);
995    }
996 }
997
998 static void
999 store_ensure_image(png_store *ps, png_const_structp pp, int nImages,
1000    png_size_t cbRow, png_uint_32 cRows)
1001 {
1002    png_size_t cb = nImages * cRows * (cbRow + 5);
1003
1004    if (ps->cb_image < cb)
1005    {
1006       png_bytep image;
1007
1008       store_image_free(ps, pp);
1009
1010       /* The buffer is deliberately mis-aligned. */
1011       image = voidcast(png_bytep, malloc(cb+2));
1012       if (image == NULL)
1013       {
1014          /* Called from the startup - ignore the error for the moment. */
1015          if (pp == NULL)
1016             return;
1017
1018          png_error(pp, "OOM allocating image buffer");
1019       }
1020
1021       /* These magic tags are used to detect overwrites above. */
1022       ++image;
1023       image[-1] = 0xed;
1024       image[cb] = 0xfe;
1025
1026       ps->image = image;
1027       ps->cb_image = cb;
1028    }
1029
1030    /* We have an adequate sized image; lay out the rows.  There are 2 bytes at
1031     * the start and three at the end of each (this ensures that the row
1032     * alignment starts out odd - 2+1 and changes for larger images on each row.)
1033     */
1034    ps->cb_row = cbRow;
1035    ps->image_h = cRows;
1036
1037    /* For error checking, the whole buffer is set to 10110010 (0xb2 - 178).
1038     * This deliberately doesn't match the bits in the size test image which are
1039     * outside the image; these are set to 0xff (all 1).  To make the row
1040     * comparison work in the 'size' test case the size rows are pre-initialized
1041     * to the same value prior to calling 'standard_row'.
1042     */
1043    memset(ps->image, 178, cb);
1044
1045    /* Then put in the marks. */
1046    while (--nImages >= 0)
1047    {
1048       png_uint_32 y;
1049
1050       for (y=0; y<cRows; ++y)
1051       {
1052          png_bytep row = store_image_row(ps, pp, nImages, y);
1053
1054          /* The markers: */
1055          row[-2] = 190;
1056          row[-1] = 239;
1057          row[cbRow] = 222;
1058          row[cbRow+1] = 173;
1059          row[cbRow+2] = 17;
1060       }
1061    }
1062 }
1063
1064 #ifdef PNG_READ_SUPPORTED
1065 static void
1066 store_image_check(PNG_CONST png_store* ps, png_const_structp pp, int iImage)
1067 {
1068    png_const_bytep image = ps->image;
1069
1070    if (image[-1] != 0xed || image[ps->cb_image] != 0xfe)
1071       png_error(pp, "image overwrite");
1072    else
1073    {
1074       png_size_t cbRow = ps->cb_row;
1075       png_uint_32 rows = ps->image_h;
1076
1077       image += iImage * (cbRow+5) * ps->image_h;
1078
1079       image += 2; /* skip image first row markers */
1080
1081       while (rows-- > 0)
1082       {
1083          if (image[-2] != 190 || image[-1] != 239)
1084             png_error(pp, "row start overwritten");
1085
1086          if (image[cbRow] != 222 || image[cbRow+1] != 173 ||
1087             image[cbRow+2] != 17)
1088             png_error(pp, "row end overwritten");
1089
1090          image += cbRow+5;
1091       }
1092    }
1093 }
1094 #endif /* PNG_READ_SUPPORTED */
1095
1096 static void PNGCBAPI
1097 store_write(png_structp ppIn, png_bytep pb, png_size_t st)
1098 {
1099    png_const_structp pp = ppIn;
1100    png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
1101
1102    if (ps->pwrite != pp)
1103       png_error(pp, "store state damaged");
1104
1105    while (st > 0)
1106    {
1107       size_t cb;
1108
1109       if (ps->writepos >= STORE_BUFFER_SIZE)
1110          store_storenew(ps);
1111
1112       cb = st;
1113
1114       if (cb > STORE_BUFFER_SIZE - ps->writepos)
1115          cb = STORE_BUFFER_SIZE - ps->writepos;
1116
1117       memcpy(ps->new.buffer + ps->writepos, pb, cb);
1118       pb += cb;
1119       st -= cb;
1120       ps->writepos += cb;
1121    }
1122 }
1123
1124 static void PNGCBAPI
1125 store_flush(png_structp ppIn)
1126 {
1127    UNUSED(ppIn) /*DOES NOTHING*/
1128 }
1129
1130 #ifdef PNG_READ_SUPPORTED
1131 static size_t
1132 store_read_buffer_size(png_store *ps)
1133 {
1134    /* Return the bytes available for read in the current buffer. */
1135    if (ps->next != &ps->current->data)
1136       return STORE_BUFFER_SIZE;
1137
1138    return ps->current->datacount;
1139 }
1140
1141 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1142 /* Return total bytes available for read. */
1143 static size_t
1144 store_read_buffer_avail(png_store *ps)
1145 {
1146    if (ps->current != NULL && ps->next != NULL)
1147    {
1148       png_store_buffer *next = &ps->current->data;
1149       size_t cbAvail = ps->current->datacount;
1150
1151       while (next != ps->next && next != NULL)
1152       {
1153          next = next->prev;
1154          cbAvail += STORE_BUFFER_SIZE;
1155       }
1156
1157       if (next != ps->next)
1158          png_error(ps->pread, "buffer read error");
1159
1160       if (cbAvail > ps->readpos)
1161          return cbAvail - ps->readpos;
1162    }
1163
1164    return 0;
1165 }
1166 #endif
1167
1168 static int
1169 store_read_buffer_next(png_store *ps)
1170 {
1171    png_store_buffer *pbOld = ps->next;
1172    png_store_buffer *pbNew = &ps->current->data;
1173    if (pbOld != pbNew)
1174    {
1175       while (pbNew != NULL && pbNew->prev != pbOld)
1176          pbNew = pbNew->prev;
1177
1178       if (pbNew != NULL)
1179       {
1180          ps->next = pbNew;
1181          ps->readpos = 0;
1182          return 1;
1183       }
1184
1185       png_error(ps->pread, "buffer lost");
1186    }
1187
1188    return 0; /* EOF or error */
1189 }
1190
1191 /* Need separate implementation and callback to allow use of the same code
1192  * during progressive read, where the io_ptr is set internally by libpng.
1193  */
1194 static void
1195 store_read_imp(png_store *ps, png_bytep pb, png_size_t st)
1196 {
1197    if (ps->current == NULL || ps->next == NULL)
1198       png_error(ps->pread, "store state damaged");
1199
1200    while (st > 0)
1201    {
1202       size_t cbAvail = store_read_buffer_size(ps) - ps->readpos;
1203
1204       if (cbAvail > 0)
1205       {
1206          if (cbAvail > st) cbAvail = st;
1207          memcpy(pb, ps->next->buffer + ps->readpos, cbAvail);
1208          st -= cbAvail;
1209          pb += cbAvail;
1210          ps->readpos += cbAvail;
1211       }
1212
1213       else if (!store_read_buffer_next(ps))
1214          png_error(ps->pread, "read beyond end of file");
1215    }
1216 }
1217
1218 static void PNGCBAPI
1219 store_read(png_structp ppIn, png_bytep pb, png_size_t st)
1220 {
1221    png_const_structp pp = ppIn;
1222    png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
1223
1224    if (ps == NULL || ps->pread != pp)
1225       png_error(pp, "bad store read call");
1226
1227    store_read_imp(ps, pb, st);
1228 }
1229
1230 static void
1231 store_progressive_read(png_store *ps, png_structp pp, png_infop pi)
1232 {
1233    /* Notice that a call to store_read will cause this function to fail because
1234     * readpos will be set.
1235     */
1236    if (ps->pread != pp || ps->current == NULL || ps->next == NULL)
1237       png_error(pp, "store state damaged (progressive)");
1238
1239    do
1240    {
1241       if (ps->readpos != 0)
1242          png_error(pp, "store_read called during progressive read");
1243
1244       png_process_data(pp, pi, ps->next->buffer, store_read_buffer_size(ps));
1245    }
1246    while (store_read_buffer_next(ps));
1247 }
1248 #endif /* PNG_READ_SUPPORTED */
1249
1250 /* The caller must fill this in: */
1251 static store_palette_entry *
1252 store_write_palette(png_store *ps, int npalette)
1253 {
1254    if (ps->pwrite == NULL)
1255       store_log(ps, NULL, "attempt to write palette without write stream", 1);
1256
1257    if (ps->palette != NULL)
1258       png_error(ps->pwrite, "multiple store_write_palette calls");
1259
1260    /* This function can only return NULL if called with '0'! */
1261    if (npalette > 0)
1262    {
1263       ps->palette = voidcast(store_palette_entry*, malloc(npalette *
1264          sizeof *ps->palette));
1265
1266       if (ps->palette == NULL)
1267          png_error(ps->pwrite, "store new palette: OOM");
1268
1269       ps->npalette = npalette;
1270    }
1271
1272    return ps->palette;
1273 }
1274
1275 #ifdef PNG_READ_SUPPORTED
1276 static store_palette_entry *
1277 store_current_palette(png_store *ps, int *npalette)
1278 {
1279    /* This is an internal error (the call has been made outside a read
1280     * operation.)
1281     */
1282    if (ps->current == NULL)
1283       store_log(ps, ps->pread, "no current stream for palette", 1);
1284
1285    /* The result may be null if there is no palette. */
1286    *npalette = ps->current->npalette;
1287    return ps->current->palette;
1288 }
1289 #endif /* PNG_READ_SUPPORTED */
1290
1291 /***************************** MEMORY MANAGEMENT*** ***************************/
1292 #ifdef PNG_USER_MEM_SUPPORTED
1293 /* A store_memory is simply the header for an allocated block of memory.  The
1294  * pointer returned to libpng is just after the end of the header block, the
1295  * allocated memory is followed by a second copy of the 'mark'.
1296  */
1297 typedef struct store_memory
1298 {
1299    store_pool          *pool;    /* Originating pool */
1300    struct store_memory *next;    /* Singly linked list */
1301    png_alloc_size_t     size;    /* Size of memory allocated */
1302    png_byte             mark[4]; /* ID marker */
1303 } store_memory;
1304
1305 /* Handle a fatal error in memory allocation.  This calls png_error if the
1306  * libpng struct is non-NULL, else it outputs a message and returns.  This means
1307  * that a memory problem while libpng is running will abort (png_error) the
1308  * handling of particular file while one in cleanup (after the destroy of the
1309  * struct has returned) will simply keep going and free (or attempt to free)
1310  * all the memory.
1311  */
1312 static void
1313 store_pool_error(png_store *ps, png_const_structp pp, PNG_CONST char *msg)
1314 {
1315    if (pp != NULL)
1316       png_error(pp, msg);
1317
1318    /* Else we have to do it ourselves.  png_error eventually calls store_log,
1319     * above.  store_log accepts a NULL png_structp - it just changes what gets
1320     * output by store_message.
1321     */
1322    store_log(ps, pp, msg, 1 /* error */);
1323 }
1324
1325 static void
1326 store_memory_free(png_const_structp pp, store_pool *pool, store_memory *memory)
1327 {
1328    /* Note that pp may be NULL (see store_pool_delete below), the caller has
1329     * found 'memory' in pool->list *and* unlinked this entry, so this is a valid
1330     * pointer (for sure), but the contents may have been trashed.
1331     */
1332    if (memory->pool != pool)
1333       store_pool_error(pool->store, pp, "memory corrupted (pool)");
1334
1335    else if (memcmp(memory->mark, pool->mark, sizeof memory->mark) != 0)
1336       store_pool_error(pool->store, pp, "memory corrupted (start)");
1337
1338    /* It should be safe to read the size field now. */
1339    else
1340    {
1341       png_alloc_size_t cb = memory->size;
1342
1343       if (cb > pool->max)
1344          store_pool_error(pool->store, pp, "memory corrupted (size)");
1345
1346       else if (memcmp((png_bytep)(memory+1)+cb, pool->mark, sizeof pool->mark)
1347          != 0)
1348          store_pool_error(pool->store, pp, "memory corrupted (end)");
1349
1350       /* Finally give the library a chance to find problems too: */
1351       else
1352          {
1353          pool->current -= cb;
1354          free(memory);
1355          }
1356    }
1357 }
1358
1359 static void
1360 store_pool_delete(png_store *ps, store_pool *pool)
1361 {
1362    if (pool->list != NULL)
1363    {
1364       fprintf(stderr, "%s: %s %s: memory lost (list follows):\n", ps->test,
1365          pool == &ps->read_memory_pool ? "read" : "write",
1366          pool == &ps->read_memory_pool ? (ps->current != NULL ?
1367             ps->current->name : "unknown file") : ps->wname);
1368       ++ps->nerrors;
1369
1370       do
1371       {
1372          store_memory *next = pool->list;
1373          pool->list = next->next;
1374          next->next = NULL;
1375
1376          fprintf(stderr, "\t%lu bytes @ %p\n",
1377              (unsigned long)next->size, (PNG_CONST void*)(next+1));
1378          /* The NULL means this will always return, even if the memory is
1379           * corrupted.
1380           */
1381          store_memory_free(NULL, pool, next);
1382       }
1383       while (pool->list != NULL);
1384    }
1385
1386    /* And reset the other fields too for the next time. */
1387    if (pool->max > pool->max_max) pool->max_max = pool->max;
1388    pool->max = 0;
1389    if (pool->current != 0) /* unexpected internal error */
1390       fprintf(stderr, "%s: %s %s: memory counter mismatch (internal error)\n",
1391          ps->test, pool == &ps->read_memory_pool ? "read" : "write",
1392          pool == &ps->read_memory_pool ? (ps->current != NULL ?
1393             ps->current->name : "unknown file") : ps->wname);
1394    pool->current = 0;
1395
1396    if (pool->limit > pool->max_limit)
1397       pool->max_limit = pool->limit;
1398
1399    pool->limit = 0;
1400
1401    if (pool->total > pool->max_total)
1402       pool->max_total = pool->total;
1403
1404    pool->total = 0;
1405
1406    /* Get a new mark too. */
1407    store_pool_mark(pool->mark);
1408 }
1409
1410 /* The memory callbacks: */
1411 static png_voidp PNGCBAPI
1412 store_malloc(png_structp ppIn, png_alloc_size_t cb)
1413 {
1414    png_const_structp pp = ppIn;
1415    store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
1416    store_memory *new = voidcast(store_memory*, malloc(cb + (sizeof *new) +
1417       (sizeof pool->mark)));
1418
1419    if (new != NULL)
1420    {
1421       if (cb > pool->max)
1422          pool->max = cb;
1423
1424       pool->current += cb;
1425
1426       if (pool->current > pool->limit)
1427          pool->limit = pool->current;
1428
1429       pool->total += cb;
1430
1431       new->size = cb;
1432       memcpy(new->mark, pool->mark, sizeof new->mark);
1433       memcpy((png_byte*)(new+1) + cb, pool->mark, sizeof pool->mark);
1434       new->pool = pool;
1435       new->next = pool->list;
1436       pool->list = new;
1437       ++new;
1438    }
1439
1440    else
1441    {
1442       /* NOTE: the PNG user malloc function cannot use the png_ptr it is passed
1443        * other than to retrieve the allocation pointer!  libpng calls the
1444        * store_malloc callback in two basic cases:
1445        *
1446        * 1) From png_malloc; png_malloc will do a png_error itself if NULL is
1447        *    returned.
1448        * 2) From png_struct or png_info structure creation; png_malloc is
1449        *    to return so cleanup can be performed.
1450        *
1451        * To handle this store_malloc can log a message, but can't do anything
1452        * else.
1453        */
1454       store_log(pool->store, pp, "out of memory", 1 /* is_error */);
1455    }
1456
1457    return new;
1458 }
1459
1460 static void PNGCBAPI
1461 store_free(png_structp ppIn, png_voidp memory)
1462 {
1463    png_const_structp pp = ppIn;
1464    store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
1465    store_memory *this = voidcast(store_memory*, memory), **test;
1466
1467    /* Because libpng calls store_free with a dummy png_struct when deleting
1468     * png_struct or png_info via png_destroy_struct_2 it is necessary to check
1469     * the passed in png_structp to ensure it is valid, and not pass it to
1470     * png_error if it is not.
1471     */
1472    if (pp != pool->store->pread && pp != pool->store->pwrite)
1473       pp = NULL;
1474
1475    /* First check that this 'memory' really is valid memory - it must be in the
1476     * pool list.  If it is, use the shared memory_free function to free it.
1477     */
1478    --this;
1479    for (test = &pool->list; *test != this; test = &(*test)->next)
1480    {
1481       if (*test == NULL)
1482       {
1483          store_pool_error(pool->store, pp, "bad pointer to free");
1484          return;
1485       }
1486    }
1487
1488    /* Unlink this entry, *test == this. */
1489    *test = this->next;
1490    this->next = NULL;
1491    store_memory_free(pp, pool, this);
1492 }
1493 #endif /* PNG_USER_MEM_SUPPORTED */
1494
1495 /* Setup functions. */
1496 /* Cleanup when aborting a write or after storing the new file. */
1497 static void
1498 store_write_reset(png_store *ps)
1499 {
1500    if (ps->pwrite != NULL)
1501    {
1502       anon_context(ps);
1503
1504       Try
1505          png_destroy_write_struct(&ps->pwrite, &ps->piwrite);
1506
1507       Catch_anonymous
1508       {
1509          /* memory corruption: continue. */
1510       }
1511
1512       ps->pwrite = NULL;
1513       ps->piwrite = NULL;
1514    }
1515
1516    /* And make sure that all the memory has been freed - this will output
1517     * spurious errors in the case of memory corruption above, but this is safe.
1518     */
1519 #  ifdef PNG_USER_MEM_SUPPORTED
1520       store_pool_delete(ps, &ps->write_memory_pool);
1521 #  endif
1522
1523    store_freenew(ps);
1524 }
1525
1526 /* The following is the main write function, it returns a png_struct and,
1527  * optionally, a png_info suitable for writiing a new PNG file.  Use
1528  * store_storefile above to record this file after it has been written.  The
1529  * returned libpng structures as destroyed by store_write_reset above.
1530  */
1531 static png_structp
1532 set_store_for_write(png_store *ps, png_infopp ppi,
1533    PNG_CONST char * volatile name)
1534 {
1535    anon_context(ps);
1536
1537    Try
1538    {
1539       if (ps->pwrite != NULL)
1540          png_error(ps->pwrite, "write store already in use");
1541
1542       store_write_reset(ps);
1543       safecat(ps->wname, sizeof ps->wname, 0, name);
1544
1545       /* Don't do the slow memory checks if doing a speed test, also if user
1546        * memory is not supported we can't do it anyway.
1547        */
1548 #     ifdef PNG_USER_MEM_SUPPORTED
1549          if (!ps->speed)
1550             ps->pwrite = png_create_write_struct_2(PNG_LIBPNG_VER_STRING,
1551                ps, store_error, store_warning, &ps->write_memory_pool,
1552                store_malloc, store_free);
1553
1554          else
1555 #     endif
1556          ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING,
1557             ps, store_error, store_warning);
1558
1559       png_set_write_fn(ps->pwrite, ps, store_write, store_flush);
1560
1561 #     ifdef PNG_SET_OPTION_SUPPORTED
1562          {
1563             int opt;
1564             for (opt=0; opt<ps->noptions; ++opt)
1565                if (png_set_option(ps->pwrite, ps->options[opt].option,
1566                   ps->options[opt].setting) == PNG_OPTION_INVALID)
1567                   png_error(ps->pwrite, "png option invalid");
1568          }
1569 #     endif
1570
1571       if (ppi != NULL)
1572          *ppi = ps->piwrite = png_create_info_struct(ps->pwrite);
1573    }
1574
1575    Catch_anonymous
1576       return NULL;
1577
1578    return ps->pwrite;
1579 }
1580
1581 /* Cleanup when finished reading (either due to error or in the success case).
1582  * This routine exists even when there is no read support to make the code
1583  * tidier (avoid a mass of ifdefs) and so easier to maintain.
1584  */
1585 static void
1586 store_read_reset(png_store *ps)
1587 {
1588 #  ifdef PNG_READ_SUPPORTED
1589       if (ps->pread != NULL)
1590       {
1591          anon_context(ps);
1592
1593          Try
1594             png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
1595
1596          Catch_anonymous
1597          {
1598             /* error already output: continue */
1599          }
1600
1601          ps->pread = NULL;
1602          ps->piread = NULL;
1603       }
1604 #  endif
1605
1606 #  ifdef PNG_USER_MEM_SUPPORTED
1607       /* Always do this to be safe. */
1608       store_pool_delete(ps, &ps->read_memory_pool);
1609 #  endif
1610
1611    ps->current = NULL;
1612    ps->next = NULL;
1613    ps->readpos = 0;
1614    ps->validated = 0;
1615 }
1616
1617 #ifdef PNG_READ_SUPPORTED
1618 static void
1619 store_read_set(png_store *ps, png_uint_32 id)
1620 {
1621    png_store_file *pf = ps->saved;
1622
1623    while (pf != NULL)
1624    {
1625       if (pf->id == id)
1626       {
1627          ps->current = pf;
1628          ps->next = NULL;
1629          store_read_buffer_next(ps);
1630          return;
1631       }
1632
1633       pf = pf->next;
1634    }
1635
1636    {
1637       size_t pos;
1638       char msg[FILE_NAME_SIZE+64];
1639
1640       pos = standard_name_from_id(msg, sizeof msg, 0, id);
1641       pos = safecat(msg, sizeof msg, pos, ": file not found");
1642       png_error(ps->pread, msg);
1643    }
1644 }
1645
1646 /* The main interface for reading a saved file - pass the id number of the file
1647  * to retrieve.  Ids must be unique or the earlier file will be hidden.  The API
1648  * returns a png_struct and, optionally, a png_info.  Both of these will be
1649  * destroyed by store_read_reset above.
1650  */
1651 static png_structp
1652 set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id,
1653    PNG_CONST char *name)
1654 {
1655    /* Set the name for png_error */
1656    safecat(ps->test, sizeof ps->test, 0, name);
1657
1658    if (ps->pread != NULL)
1659       png_error(ps->pread, "read store already in use");
1660
1661    store_read_reset(ps);
1662
1663    /* Both the create APIs can return NULL if used in their default mode
1664     * (because there is no other way of handling an error because the jmp_buf
1665     * by default is stored in png_struct and that has not been allocated!)
1666     * However, given that store_error works correctly in these circumstances
1667     * we don't ever expect NULL in this program.
1668     */
1669 #  ifdef PNG_USER_MEM_SUPPORTED
1670       if (!ps->speed)
1671          ps->pread = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, ps,
1672              store_error, store_warning, &ps->read_memory_pool, store_malloc,
1673              store_free);
1674
1675       else
1676 #  endif
1677    ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps, store_error,
1678       store_warning);
1679
1680    if (ps->pread == NULL)
1681    {
1682       struct exception_context *the_exception_context = &ps->exception_context;
1683
1684       store_log(ps, NULL, "png_create_read_struct returned NULL (unexpected)",
1685          1 /*error*/);
1686
1687       Throw ps;
1688    }
1689
1690 #  ifdef PNG_SET_OPTION_SUPPORTED
1691       {
1692          int opt;
1693          for (opt=0; opt<ps->noptions; ++opt)
1694             if (png_set_option(ps->pread, ps->options[opt].option,
1695                ps->options[opt].setting) == PNG_OPTION_INVALID)
1696                   png_error(ps->pread, "png option invalid");
1697       }
1698 #  endif
1699
1700    store_read_set(ps, id);
1701
1702    if (ppi != NULL)
1703       *ppi = ps->piread = png_create_info_struct(ps->pread);
1704
1705    return ps->pread;
1706 }
1707 #endif /* PNG_READ_SUPPORTED */
1708
1709 /* The overall cleanup of a store simply calls the above then removes all the
1710  * saved files.  This does not delete the store itself.
1711  */
1712 static void
1713 store_delete(png_store *ps)
1714 {
1715    store_write_reset(ps);
1716    store_read_reset(ps);
1717    store_freefile(&ps->saved);
1718    store_image_free(ps, NULL);
1719 }
1720
1721 /*********************** PNG FILE MODIFICATION ON READ ************************/
1722 /* Files may be modified on read.  The following structure contains a complete
1723  * png_store together with extra members to handle modification and a special
1724  * read callback for libpng.  To use this the 'modifications' field must be set
1725  * to a list of png_modification structures that actually perform the
1726  * modification, otherwise a png_modifier is functionally equivalent to a
1727  * png_store.  There is a special read function, set_modifier_for_read, which
1728  * replaces set_store_for_read.
1729  */
1730 typedef enum modifier_state
1731 {
1732    modifier_start,                        /* Initial value */
1733    modifier_signature,                    /* Have a signature */
1734    modifier_IHDR                          /* Have an IHDR */
1735 } modifier_state;
1736
1737 typedef struct CIE_color
1738 {
1739    /* A single CIE tristimulus value, representing the unique response of a
1740     * standard observer to a variety of light spectra.  The observer recognizes
1741     * all spectra that produce this response as the same color, therefore this
1742     * is effectively a description of a color.
1743     */
1744    double X, Y, Z;
1745 } CIE_color;
1746
1747 typedef struct color_encoding
1748 {
1749    /* A description of an (R,G,B) encoding of color (as defined above); this
1750     * includes the actual colors of the (R,G,B) triples (1,0,0), (0,1,0) and
1751     * (0,0,1) plus an encoding value that is used to encode the linear
1752     * components R, G and B to give the actual values R^gamma, G^gamma and
1753     * B^gamma that are stored.
1754     */
1755    double    gamma;            /* Encoding (file) gamma of space */
1756    CIE_color red, green, blue; /* End points */
1757 } color_encoding;
1758
1759 #ifdef PNG_READ_SUPPORTED
1760 static double
1761 chromaticity_x(CIE_color c)
1762 {
1763    return c.X / (c.X + c.Y + c.Z);
1764 }
1765
1766 static double
1767 chromaticity_y(CIE_color c)
1768 {
1769    return c.Y / (c.X + c.Y + c.Z);
1770 }
1771
1772 static CIE_color
1773 white_point(PNG_CONST color_encoding *encoding)
1774 {
1775    CIE_color white;
1776
1777    white.X = encoding->red.X + encoding->green.X + encoding->blue.X;
1778    white.Y = encoding->red.Y + encoding->green.Y + encoding->blue.Y;
1779    white.Z = encoding->red.Z + encoding->green.Z + encoding->blue.Z;
1780
1781    return white;
1782 }
1783
1784 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1785 static void
1786 normalize_color_encoding(color_encoding *encoding)
1787 {
1788    PNG_CONST double whiteY = encoding->red.Y + encoding->green.Y +
1789       encoding->blue.Y;
1790
1791    if (whiteY != 1)
1792    {
1793       encoding->red.X /= whiteY;
1794       encoding->red.Y /= whiteY;
1795       encoding->red.Z /= whiteY;
1796       encoding->green.X /= whiteY;
1797       encoding->green.Y /= whiteY;
1798       encoding->green.Z /= whiteY;
1799       encoding->blue.X /= whiteY;
1800       encoding->blue.Y /= whiteY;
1801       encoding->blue.Z /= whiteY;
1802    }
1803 }
1804 #endif
1805
1806 static size_t
1807 safecat_color_encoding(char *buffer, size_t bufsize, size_t pos,
1808    PNG_CONST color_encoding *e, double encoding_gamma)
1809 {
1810    if (e != 0)
1811    {
1812       if (encoding_gamma != 0)
1813          pos = safecat(buffer, bufsize, pos, "(");
1814       pos = safecat(buffer, bufsize, pos, "R(");
1815       pos = safecatd(buffer, bufsize, pos, e->red.X, 4);
1816       pos = safecat(buffer, bufsize, pos, ",");
1817       pos = safecatd(buffer, bufsize, pos, e->red.Y, 4);
1818       pos = safecat(buffer, bufsize, pos, ",");
1819       pos = safecatd(buffer, bufsize, pos, e->red.Z, 4);
1820       pos = safecat(buffer, bufsize, pos, "),G(");
1821       pos = safecatd(buffer, bufsize, pos, e->green.X, 4);
1822       pos = safecat(buffer, bufsize, pos, ",");
1823       pos = safecatd(buffer, bufsize, pos, e->green.Y, 4);
1824       pos = safecat(buffer, bufsize, pos, ",");
1825       pos = safecatd(buffer, bufsize, pos, e->green.Z, 4);
1826       pos = safecat(buffer, bufsize, pos, "),B(");
1827       pos = safecatd(buffer, bufsize, pos, e->blue.X, 4);
1828       pos = safecat(buffer, bufsize, pos, ",");
1829       pos = safecatd(buffer, bufsize, pos, e->blue.Y, 4);
1830       pos = safecat(buffer, bufsize, pos, ",");
1831       pos = safecatd(buffer, bufsize, pos, e->blue.Z, 4);
1832       pos = safecat(buffer, bufsize, pos, ")");
1833       if (encoding_gamma != 0)
1834          pos = safecat(buffer, bufsize, pos, ")");
1835    }
1836
1837    if (encoding_gamma != 0)
1838    {
1839       pos = safecat(buffer, bufsize, pos, "^");
1840       pos = safecatd(buffer, bufsize, pos, encoding_gamma, 5);
1841    }
1842
1843    return pos;
1844 }
1845 #endif /* PNG_READ_SUPPORTED */
1846
1847 typedef struct png_modifier
1848 {
1849    png_store               this;             /* I am a png_store */
1850    struct png_modification *modifications;   /* Changes to make */
1851
1852    modifier_state           state;           /* My state */
1853
1854    /* Information from IHDR: */
1855    png_byte                 bit_depth;       /* From IHDR */
1856    png_byte                 colour_type;     /* From IHDR */
1857
1858    /* While handling PLTE, IDAT and IEND these chunks may be pended to allow
1859     * other chunks to be inserted.
1860     */
1861    png_uint_32              pending_len;
1862    png_uint_32              pending_chunk;
1863
1864    /* Test values */
1865    double                   *gammas;
1866    unsigned int              ngammas;
1867    unsigned int              ngamma_tests;     /* Number of gamma tests to run*/
1868    double                    current_gamma;    /* 0 if not set */
1869    PNG_CONST color_encoding *encodings;
1870    unsigned int              nencodings;
1871    PNG_CONST color_encoding *current_encoding; /* If an encoding has been set */
1872    unsigned int              encoding_counter; /* For iteration */
1873    int                       encoding_ignored; /* Something overwrote it */
1874
1875    /* Control variables used to iterate through possible encodings, the
1876     * following must be set to 0 and tested by the function that uses the
1877     * png_modifier because the modifier only sets it to 1 (true.)
1878     */
1879    unsigned int              repeat :1;   /* Repeat this transform test. */
1880    unsigned int              test_uses_encoding :1;
1881
1882    /* Lowest sbit to test (libpng fails for sbit < 8) */
1883    png_byte                 sbitlow;
1884
1885    /* Error control - these are the limits on errors accepted by the gamma tests
1886     * below.
1887     */
1888    double                   maxout8;  /* Maximum output value error */
1889    double                   maxabs8;  /* Absolute sample error 0..1 */
1890    double                   maxcalc8; /* Absolute sample error 0..1 */
1891    double                   maxpc8;   /* Percentage sample error 0..100% */
1892    double                   maxout16; /* Maximum output value error */
1893    double                   maxabs16; /* Absolute sample error 0..1 */
1894    double                   maxcalc16;/* Absolute sample error 0..1 */
1895    double                   maxcalcG; /* Absolute sample error 0..1 */
1896    double                   maxpc16;  /* Percentage sample error 0..100% */
1897
1898    /* This is set by transforms that need to allow a higher limit, it is an
1899     * internal check on pngvalid to ensure that the calculated error limits are
1900     * not ridiculous; without this it is too easy to make a mistake in pngvalid
1901     * that allows any value through.
1902     */
1903    double                   limit;    /* limit on error values, normally 4E-3 */
1904
1905    /* Log limits - values above this are logged, but not necessarily
1906     * warned.
1907     */
1908    double                   log8;     /* Absolute error in 8 bits to log */
1909    double                   log16;    /* Absolute error in 16 bits to log */
1910
1911    /* Logged 8 and 16 bit errors ('output' values): */
1912    double                   error_gray_2;
1913    double                   error_gray_4;
1914    double                   error_gray_8;
1915    double                   error_gray_16;
1916    double                   error_color_8;
1917    double                   error_color_16;
1918    double                   error_indexed;
1919
1920    /* Flags: */
1921    /* Whether to call png_read_update_info, not png_read_start_image, and how
1922     * many times to call it.
1923     */
1924    int                      use_update_info;
1925
1926    /* Whether or not to interlace. */
1927    int                      interlace_type :9; /* int, but must store '1' */
1928
1929    /* Run the standard tests? */
1930    unsigned int             test_standard :1;
1931
1932    /* Run the odd-sized image and interlace read/write tests? */
1933    unsigned int             test_size :1;
1934
1935    /* Run tests on reading with a combination of transforms, */
1936    unsigned int             test_transform :1;
1937
1938    /* When to use the use_input_precision option, this controls the gamma
1939     * validation code checks.  If set any value that is within the transformed
1940     * range input-.5 to input+.5 will be accepted, otherwise the value must be
1941     * within the normal limits.  It should not be necessary to set this; the
1942     * result should always be exact within the permitted error limits.
1943     */
1944    unsigned int             use_input_precision :1;
1945    unsigned int             use_input_precision_sbit :1;
1946    unsigned int             use_input_precision_16to8 :1;
1947
1948    /* If set assume that the calculation bit depth is set by the input
1949     * precision, not the output precision.
1950     */
1951    unsigned int             calculations_use_input_precision :1;
1952
1953    /* If set assume that the calculations are done in 16 bits even if the sample
1954     * depth is 8 bits.
1955     */
1956    unsigned int             assume_16_bit_calculations :1;
1957
1958    /* Which gamma tests to run: */
1959    unsigned int             test_gamma_threshold :1;
1960    unsigned int             test_gamma_transform :1; /* main tests */
1961    unsigned int             test_gamma_sbit :1;
1962    unsigned int             test_gamma_scale16 :1;
1963    unsigned int             test_gamma_background :1;
1964    unsigned int             test_gamma_alpha_mode :1;
1965    unsigned int             test_gamma_expand16 :1;
1966    unsigned int             test_exhaustive :1;
1967
1968    unsigned int             log :1;   /* Log max error */
1969
1970    /* Buffer information, the buffer size limits the size of the chunks that can
1971     * be modified - they must fit (including header and CRC) into the buffer!
1972     */
1973    size_t                   flush;           /* Count of bytes to flush */
1974    size_t                   buffer_count;    /* Bytes in buffer */
1975    size_t                   buffer_position; /* Position in buffer */
1976    png_byte                 buffer[1024];
1977 } png_modifier;
1978
1979 /* This returns true if the test should be stopped now because it has already
1980  * failed and it is running silently.
1981   */
1982 static int fail(png_modifier *pm)
1983 {
1984    return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 ||
1985        (pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0));
1986 }
1987
1988 static void
1989 modifier_init(png_modifier *pm)
1990 {
1991    memset(pm, 0, sizeof *pm);
1992    store_init(&pm->this);
1993    pm->modifications = NULL;
1994    pm->state = modifier_start;
1995    pm->sbitlow = 1U;
1996    pm->ngammas = 0;
1997    pm->ngamma_tests = 0;
1998    pm->gammas = 0;
1999    pm->current_gamma = 0;
2000    pm->encodings = 0;
2001    pm->nencodings = 0;
2002    pm->current_encoding = 0;
2003    pm->encoding_counter = 0;
2004    pm->encoding_ignored = 0;
2005    pm->repeat = 0;
2006    pm->test_uses_encoding = 0;
2007    pm->maxout8 = pm->maxpc8 = pm->maxabs8 = pm->maxcalc8 = 0;
2008    pm->maxout16 = pm->maxpc16 = pm->maxabs16 = pm->maxcalc16 = 0;
2009    pm->maxcalcG = 0;
2010    pm->limit = 4E-3;
2011    pm->log8 = pm->log16 = 0; /* Means 'off' */
2012    pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0;
2013    pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0;
2014    pm->error_indexed = 0;
2015    pm->use_update_info = 0;
2016    pm->interlace_type = PNG_INTERLACE_NONE;
2017    pm->test_standard = 0;
2018    pm->test_size = 0;
2019    pm->test_transform = 0;
2020    pm->use_input_precision = 0;
2021    pm->use_input_precision_sbit = 0;
2022    pm->use_input_precision_16to8 = 0;
2023    pm->calculations_use_input_precision = 0;
2024    pm->assume_16_bit_calculations = 0;
2025    pm->test_gamma_threshold = 0;
2026    pm->test_gamma_transform = 0;
2027    pm->test_gamma_sbit = 0;
2028    pm->test_gamma_scale16 = 0;
2029    pm->test_gamma_background = 0;
2030    pm->test_gamma_alpha_mode = 0;
2031    pm->test_gamma_expand16 = 0;
2032    pm->test_exhaustive = 0;
2033    pm->log = 0;
2034
2035    /* Rely on the memset for all the other fields - there are no pointers */
2036 }
2037
2038 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
2039
2040 /* This controls use of checks that explicitly know how libpng digitizes the
2041  * samples in calculations; setting this circumvents simple error limit checking
2042  * in the rgb_to_gray check, replacing it with an exact copy of the libpng 1.5
2043  * algorithm.
2044  */
2045 #define DIGITIZE PNG_LIBPNG_VER < 10700
2046
2047 /* If pm->calculations_use_input_precision is set then operations will happen
2048  * with the precision of the input, not the precision of the output depth.
2049  *
2050  * If pm->assume_16_bit_calculations is set then even 8 bit calculations use 16
2051  * bit precision.  This only affects those of the following limits that pertain
2052  * to a calculation - not a digitization operation - unless the following API is
2053  * called directly.
2054  */
2055 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2056 #if DIGITIZE
2057 static double digitize(double value, int depth, int do_round)
2058 {
2059    /* 'value' is in the range 0 to 1, the result is the same value rounded to a
2060     * multiple of the digitization factor - 8 or 16 bits depending on both the
2061     * sample depth and the 'assume' setting.  Digitization is normally by
2062     * rounding and 'do_round' should be 1, if it is 0 the digitized value will
2063     * be truncated.
2064     */
2065    PNG_CONST unsigned int digitization_factor = (1U << depth) -1;
2066
2067    /* Limiting the range is done as a convenience to the caller - it's easier to
2068     * do it once here than every time at the call site.
2069     */
2070    if (value <= 0)
2071       value = 0;
2072
2073    else if (value >= 1)
2074       value = 1;
2075
2076    value *= digitization_factor;
2077    if (do_round) value += .5;
2078    return floor(value)/digitization_factor;
2079 }
2080 #endif
2081 #endif /* RGB_TO_GRAY */
2082
2083 #ifdef PNG_READ_GAMMA_SUPPORTED
2084 static double abserr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2085 {
2086    /* Absolute error permitted in linear values - affected by the bit depth of
2087     * the calculations.
2088     */
2089    if (pm->assume_16_bit_calculations ||
2090       (pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2091       return pm->maxabs16;
2092    else
2093       return pm->maxabs8;
2094 }
2095
2096 static double calcerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2097 {
2098    /* Error in the linear composition arithmetic - only relevant when
2099     * composition actually happens (0 < alpha < 1).
2100     */
2101    if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2102       return pm->maxcalc16;
2103    else if (pm->assume_16_bit_calculations)
2104       return pm->maxcalcG;
2105    else
2106       return pm->maxcalc8;
2107 }
2108
2109 static double pcerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2110 {
2111    /* Percentage error permitted in the linear values.  Note that the specified
2112     * value is a percentage but this routine returns a simple number.
2113     */
2114    if (pm->assume_16_bit_calculations ||
2115       (pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2116       return pm->maxpc16 * .01;
2117    else
2118       return pm->maxpc8 * .01;
2119 }
2120
2121 /* Output error - the error in the encoded value.  This is determined by the
2122  * digitization of the output so can be +/-0.5 in the actual output value.  In
2123  * the expand_16 case with the current code in libpng the expand happens after
2124  * all the calculations are done in 8 bit arithmetic, so even though the output
2125  * depth is 16 the output error is determined by the 8 bit calculation.
2126  *
2127  * This limit is not determined by the bit depth of internal calculations.
2128  *
2129  * The specified parameter does *not* include the base .5 digitization error but
2130  * it is added here.
2131  */
2132 static double outerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2133 {
2134    /* There is a serious error in the 2 and 4 bit grayscale transform because
2135     * the gamma table value (8 bits) is simply shifted, not rounded, so the
2136     * error in 4 bit grayscale gamma is up to the value below.  This is a hack
2137     * to allow pngvalid to succeed:
2138     *
2139     * TODO: fix this in libpng
2140     */
2141    if (out_depth == 2)
2142       return .73182-.5;
2143
2144    if (out_depth == 4)
2145       return .90644-.5;
2146
2147    if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2148       return pm->maxout16;
2149
2150    /* This is the case where the value was calculated at 8-bit precision then
2151     * scaled to 16 bits.
2152     */
2153    else if (out_depth == 16)
2154       return pm->maxout8 * 257;
2155
2156    else
2157       return pm->maxout8;
2158 }
2159
2160 /* This does the same thing as the above however it returns the value to log,
2161  * rather than raising a warning.  This is useful for debugging to track down
2162  * exactly what set of parameters cause high error values.
2163  */
2164 static double outlog(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2165 {
2166    /* The command line parameters are either 8 bit (0..255) or 16 bit (0..65535)
2167     * and so must be adjusted for low bit depth grayscale:
2168     */
2169    if (out_depth <= 8)
2170    {
2171       if (pm->log8 == 0) /* switched off */
2172          return 256;
2173
2174       if (out_depth < 8)
2175          return pm->log8 / 255 * ((1<<out_depth)-1);
2176
2177       return pm->log8;
2178    }
2179
2180    if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2181    {
2182       if (pm->log16 == 0)
2183          return 65536;
2184
2185       return pm->log16;
2186    }
2187
2188    /* This is the case where the value was calculated at 8-bit precision then
2189     * scaled to 16 bits.
2190     */
2191    if (pm->log8 == 0)
2192       return 65536;
2193
2194    return pm->log8 * 257;
2195 }
2196
2197 /* This complements the above by providing the appropriate quantization for the
2198  * final value.  Normally this would just be quantization to an integral value,
2199  * but in the 8 bit calculation case it's actually quantization to a multiple of
2200  * 257!
2201  */
2202 static int output_quantization_factor(PNG_CONST png_modifier *pm, int in_depth,
2203    int out_depth)
2204 {
2205    if (out_depth == 16 && in_depth != 16 &&
2206       pm->calculations_use_input_precision)
2207       return 257;
2208    else
2209       return 1;
2210 }
2211 #endif /* PNG_READ_GAMMA_SUPPORTED */
2212
2213 /* One modification structure must be provided for each chunk to be modified (in
2214  * fact more than one can be provided if multiple separate changes are desired
2215  * for a single chunk.)  Modifications include adding a new chunk when a
2216  * suitable chunk does not exist.
2217  *
2218  * The caller of modify_fn will reset the CRC of the chunk and record 'modified'
2219  * or 'added' as appropriate if the modify_fn returns 1 (true).  If the
2220  * modify_fn is NULL the chunk is simply removed.
2221  */
2222 typedef struct png_modification
2223 {
2224    struct png_modification *next;
2225    png_uint_32              chunk;
2226
2227    /* If the following is NULL all matching chunks will be removed: */
2228    int                    (*modify_fn)(struct png_modifier *pm,
2229                                struct png_modification *me, int add);
2230
2231    /* If the following is set to PLTE, IDAT or IEND and the chunk has not been
2232     * found and modified (and there is a modify_fn) the modify_fn will be called
2233     * to add the chunk before the relevant chunk.
2234     */
2235    png_uint_32              add;
2236    unsigned int             modified :1;     /* Chunk was modified */
2237    unsigned int             added    :1;     /* Chunk was added */
2238    unsigned int             removed  :1;     /* Chunk was removed */
2239 } png_modification;
2240
2241 static void
2242 modification_reset(png_modification *pmm)
2243 {
2244    if (pmm != NULL)
2245    {
2246       pmm->modified = 0;
2247       pmm->added = 0;
2248       pmm->removed = 0;
2249       modification_reset(pmm->next);
2250    }
2251 }
2252
2253 static void
2254 modification_init(png_modification *pmm)
2255 {
2256    memset(pmm, 0, sizeof *pmm);
2257    pmm->next = NULL;
2258    pmm->chunk = 0;
2259    pmm->modify_fn = NULL;
2260    pmm->add = 0;
2261    modification_reset(pmm);
2262 }
2263
2264 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2265 static void
2266 modifier_current_encoding(PNG_CONST png_modifier *pm, color_encoding *ce)
2267 {
2268    if (pm->current_encoding != 0)
2269       *ce = *pm->current_encoding;
2270
2271    else
2272       memset(ce, 0, sizeof *ce);
2273
2274    ce->gamma = pm->current_gamma;
2275 }
2276 #endif
2277
2278 static size_t
2279 safecat_current_encoding(char *buffer, size_t bufsize, size_t pos,
2280    PNG_CONST png_modifier *pm)
2281 {
2282    pos = safecat_color_encoding(buffer, bufsize, pos, pm->current_encoding,
2283       pm->current_gamma);
2284
2285    if (pm->encoding_ignored)
2286       pos = safecat(buffer, bufsize, pos, "[overridden]");
2287
2288    return pos;
2289 }
2290
2291 /* Iterate through the usefully testable color encodings.  An encoding is one
2292  * of:
2293  *
2294  * 1) Nothing (no color space, no gamma).
2295  * 2) Just a gamma value from the gamma array (including 1.0)
2296  * 3) A color space from the encodings array with the corresponding gamma.
2297  * 4) The same, but with gamma 1.0 (only really useful with 16 bit calculations)
2298  *
2299  * The iterator selects these in turn, the randomizer selects one at random,
2300  * which is used depends on the setting of the 'test_exhaustive' flag.  Notice
2301  * that this function changes the colour space encoding so it must only be
2302  * called on completion of the previous test.  This is what 'modifier_reset'
2303  * does, below.
2304  *
2305  * After the function has been called the 'repeat' flag will still be set; the
2306  * caller of modifier_reset must reset it at the start of each run of the test!
2307  */
2308 static unsigned int
2309 modifier_total_encodings(PNG_CONST png_modifier *pm)
2310 {
2311    return 1 +                 /* (1) nothing */
2312       pm->ngammas +           /* (2) gamma values to test */
2313       pm->nencodings +        /* (3) total number of encodings */
2314       /* The following test only works after the first time through the
2315        * png_modifier code because 'bit_depth' is set when the IHDR is read.
2316        * modifier_reset, below, preserves the setting until after it has called
2317        * the iterate function (also below.)
2318        *
2319        * For this reason do not rely on this function outside a call to
2320        * modifier_reset.
2321        */
2322       ((pm->bit_depth == 16 || pm->assume_16_bit_calculations) ?
2323          pm->nencodings : 0); /* (4) encodings with gamma == 1.0 */
2324 }
2325
2326 static void
2327 modifier_encoding_iterate(png_modifier *pm)
2328 {
2329    if (!pm->repeat && /* Else something needs the current encoding again. */
2330       pm->test_uses_encoding) /* Some transform is encoding dependent */
2331    {
2332       if (pm->test_exhaustive)
2333       {
2334          if (++pm->encoding_counter >= modifier_total_encodings(pm))
2335             pm->encoding_counter = 0; /* This will stop the repeat */
2336       }
2337
2338       else
2339       {
2340          /* Not exhaustive - choose an encoding at random; generate a number in
2341           * the range 1..(max-1), so the result is always non-zero:
2342           */
2343          if (pm->encoding_counter == 0)
2344             pm->encoding_counter = random_mod(modifier_total_encodings(pm)-1)+1;
2345          else
2346             pm->encoding_counter = 0;
2347       }
2348
2349       if (pm->encoding_counter > 0)
2350          pm->repeat = 1;
2351    }
2352
2353    else if (!pm->repeat)
2354       pm->encoding_counter = 0;
2355 }
2356
2357 static void
2358 modifier_reset(png_modifier *pm)
2359 {
2360    store_read_reset(&pm->this);
2361    pm->limit = 4E-3;
2362    pm->pending_len = pm->pending_chunk = 0;
2363    pm->flush = pm->buffer_count = pm->buffer_position = 0;
2364    pm->modifications = NULL;
2365    pm->state = modifier_start;
2366    modifier_encoding_iterate(pm);
2367    /* The following must be set in the next run.  In particular
2368     * test_uses_encodings must be set in the _ini function of each transform
2369     * that looks at the encodings.  (Not the 'add' function!)
2370     */
2371    pm->test_uses_encoding = 0;
2372    pm->current_gamma = 0;
2373    pm->current_encoding = 0;
2374    pm->encoding_ignored = 0;
2375    /* These only become value after IHDR is read: */
2376    pm->bit_depth = pm->colour_type = 0;
2377 }
2378
2379 /* The following must be called before anything else to get the encoding set up
2380  * on the modifier.  In particular it must be called before the transform init
2381  * functions are called.
2382  */
2383 static void
2384 modifier_set_encoding(png_modifier *pm)
2385 {
2386    /* Set the encoding to the one specified by the current encoding counter,
2387     * first clear out all the settings - this corresponds to an encoding_counter
2388     * of 0.
2389     */
2390    pm->current_gamma = 0;
2391    pm->current_encoding = 0;
2392    pm->encoding_ignored = 0; /* not ignored yet - happens in _ini functions. */
2393
2394    /* Now, if required, set the gamma and encoding fields. */
2395    if (pm->encoding_counter > 0)
2396    {
2397       /* The gammas[] array is an array of screen gammas, not encoding gammas,
2398        * so we need the inverse:
2399        */
2400       if (pm->encoding_counter <= pm->ngammas)
2401          pm->current_gamma = 1/pm->gammas[pm->encoding_counter-1];
2402
2403       else
2404       {
2405          unsigned int i = pm->encoding_counter - pm->ngammas;
2406
2407          if (i >= pm->nencodings)
2408          {
2409             i %= pm->nencodings;
2410             pm->current_gamma = 1; /* Linear, only in the 16 bit case */
2411          }
2412
2413          else
2414             pm->current_gamma = pm->encodings[i].gamma;
2415
2416          pm->current_encoding = pm->encodings + i;
2417       }
2418    }
2419 }
2420
2421 /* Enquiry functions to find out what is set.  Notice that there is an implicit
2422  * assumption below that the first encoding in the list is the one for sRGB.
2423  */
2424 static int
2425 modifier_color_encoding_is_sRGB(PNG_CONST png_modifier *pm)
2426 {
2427    return pm->current_encoding != 0 && pm->current_encoding == pm->encodings &&
2428       pm->current_encoding->gamma == pm->current_gamma;
2429 }
2430
2431 static int
2432 modifier_color_encoding_is_set(PNG_CONST png_modifier *pm)
2433 {
2434    return pm->current_gamma != 0;
2435 }
2436
2437 /* Convenience macros. */
2438 #define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
2439 #define CHUNK_IHDR CHUNK(73,72,68,82)
2440 #define CHUNK_PLTE CHUNK(80,76,84,69)
2441 #define CHUNK_IDAT CHUNK(73,68,65,84)
2442 #define CHUNK_IEND CHUNK(73,69,78,68)
2443 #define CHUNK_cHRM CHUNK(99,72,82,77)
2444 #define CHUNK_gAMA CHUNK(103,65,77,65)
2445 #define CHUNK_sBIT CHUNK(115,66,73,84)
2446 #define CHUNK_sRGB CHUNK(115,82,71,66)
2447
2448 /* The guts of modification are performed during a read. */
2449 static void
2450 modifier_crc(png_bytep buffer)
2451 {
2452    /* Recalculate the chunk CRC - a complete chunk must be in
2453     * the buffer, at the start.
2454     */
2455    uInt datalen = png_get_uint_32(buffer);
2456    uLong crc = crc32(0, buffer+4, datalen+4);
2457    /* The cast to png_uint_32 is safe because a crc32 is always a 32 bit value.
2458     */
2459    png_save_uint_32(buffer+datalen+8, (png_uint_32)crc);
2460 }
2461
2462 static void
2463 modifier_setbuffer(png_modifier *pm)
2464 {
2465    modifier_crc(pm->buffer);
2466    pm->buffer_count = png_get_uint_32(pm->buffer)+12;
2467    pm->buffer_position = 0;
2468 }
2469
2470 /* Separate the callback into the actual implementation (which is passed the
2471  * png_modifier explicitly) and the callback, which gets the modifier from the
2472  * png_struct.
2473  */
2474 static void
2475 modifier_read_imp(png_modifier *pm, png_bytep pb, png_size_t st)
2476 {
2477    while (st > 0)
2478    {
2479       size_t cb;
2480       png_uint_32 len, chunk;
2481       png_modification *mod;
2482
2483       if (pm->buffer_position >= pm->buffer_count) switch (pm->state)
2484       {
2485          static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
2486          case modifier_start:
2487             store_read_imp(&pm->this, pm->buffer, 8); /* size of signature. */
2488             pm->buffer_count = 8;
2489             pm->buffer_position = 0;
2490
2491             if (memcmp(pm->buffer, sign, 8) != 0)
2492                png_error(pm->this.pread, "invalid PNG file signature");
2493             pm->state = modifier_signature;
2494             break;
2495
2496          case modifier_signature:
2497             store_read_imp(&pm->this, pm->buffer, 13+12); /* size of IHDR */
2498             pm->buffer_count = 13+12;
2499             pm->buffer_position = 0;
2500
2501             if (png_get_uint_32(pm->buffer) != 13 ||
2502                 png_get_uint_32(pm->buffer+4) != CHUNK_IHDR)
2503                png_error(pm->this.pread, "invalid IHDR");
2504
2505             /* Check the list of modifiers for modifications to the IHDR. */
2506             mod = pm->modifications;
2507             while (mod != NULL)
2508             {
2509                if (mod->chunk == CHUNK_IHDR && mod->modify_fn &&
2510                    (*mod->modify_fn)(pm, mod, 0))
2511                   {
2512                   mod->modified = 1;
2513                   modifier_setbuffer(pm);
2514                   }
2515
2516                /* Ignore removal or add if IHDR! */
2517                mod = mod->next;
2518             }
2519
2520             /* Cache information from the IHDR (the modified one.) */
2521             pm->bit_depth = pm->buffer[8+8];
2522             pm->colour_type = pm->buffer[8+8+1];
2523
2524             pm->state = modifier_IHDR;
2525             pm->flush = 0;
2526             break;
2527
2528          case modifier_IHDR:
2529          default:
2530             /* Read a new chunk and process it until we see PLTE, IDAT or
2531              * IEND.  'flush' indicates that there is still some data to
2532              * output from the preceding chunk.
2533              */
2534             if ((cb = pm->flush) > 0)
2535             {
2536                if (cb > st) cb = st;
2537                pm->flush -= cb;
2538                store_read_imp(&pm->this, pb, cb);
2539                pb += cb;
2540                st -= cb;
2541                if (st == 0) return;
2542             }
2543
2544             /* No more bytes to flush, read a header, or handle a pending
2545              * chunk.
2546              */
2547             if (pm->pending_chunk != 0)
2548             {
2549                png_save_uint_32(pm->buffer, pm->pending_len);
2550                png_save_uint_32(pm->buffer+4, pm->pending_chunk);
2551                pm->pending_len = 0;
2552                pm->pending_chunk = 0;
2553             }
2554             else
2555                store_read_imp(&pm->this, pm->buffer, 8);
2556
2557             pm->buffer_count = 8;
2558             pm->buffer_position = 0;
2559
2560             /* Check for something to modify or a terminator chunk. */
2561             len = png_get_uint_32(pm->buffer);
2562             chunk = png_get_uint_32(pm->buffer+4);
2563
2564             /* Terminators first, they may have to be delayed for added
2565              * chunks
2566              */
2567             if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT ||
2568                 chunk == CHUNK_IEND)
2569             {
2570                mod = pm->modifications;
2571
2572                while (mod != NULL)
2573                {
2574                   if ((mod->add == chunk ||
2575                       (mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT)) &&
2576                       mod->modify_fn != NULL && !mod->modified && !mod->added)
2577                   {
2578                      /* Regardless of what the modify function does do not run
2579                       * this again.
2580                       */
2581                      mod->added = 1;
2582
2583                      if ((*mod->modify_fn)(pm, mod, 1 /*add*/))
2584                      {
2585                         /* Reset the CRC on a new chunk */
2586                         if (pm->buffer_count > 0)
2587                            modifier_setbuffer(pm);
2588
2589                         else
2590                            {
2591                            pm->buffer_position = 0;
2592                            mod->removed = 1;
2593                            }
2594
2595                         /* The buffer has been filled with something (we assume)
2596                          * so output this.  Pend the current chunk.
2597                          */
2598                         pm->pending_len = len;
2599                         pm->pending_chunk = chunk;
2600                         break; /* out of while */
2601                      }
2602                   }
2603
2604                   mod = mod->next;
2605                }
2606
2607                /* Don't do any further processing if the buffer was modified -
2608                 * otherwise the code will end up modifying a chunk that was
2609                 * just added.
2610                 */
2611                if (mod != NULL)
2612                   break; /* out of switch */
2613             }
2614
2615             /* If we get to here then this chunk may need to be modified.  To
2616              * do this it must be less than 1024 bytes in total size, otherwise
2617              * it just gets flushed.
2618              */
2619             if (len+12 <= sizeof pm->buffer)
2620             {
2621                store_read_imp(&pm->this, pm->buffer+pm->buffer_count,
2622                    len+12-pm->buffer_count);
2623                pm->buffer_count = len+12;
2624
2625                /* Check for a modification, else leave it be. */
2626                mod = pm->modifications;
2627                while (mod != NULL)
2628                {
2629                   if (mod->chunk == chunk)
2630                   {
2631                      if (mod->modify_fn == NULL)
2632                      {
2633                         /* Remove this chunk */
2634                         pm->buffer_count = pm->buffer_position = 0;
2635                         mod->removed = 1;
2636                         break; /* Terminate the while loop */
2637                      }
2638
2639                      else if ((*mod->modify_fn)(pm, mod, 0))
2640                      {
2641                         mod->modified = 1;
2642                         /* The chunk may have been removed: */
2643                         if (pm->buffer_count == 0)
2644                         {
2645                            pm->buffer_position = 0;
2646                            break;
2647                         }
2648                         modifier_setbuffer(pm);
2649                      }
2650                   }
2651
2652                   mod = mod->next;
2653                }
2654             }
2655
2656             else
2657                pm->flush = len+12 - pm->buffer_count; /* data + crc */
2658
2659             /* Take the data from the buffer (if there is any). */
2660             break;
2661       }
2662
2663       /* Here to read from the modifier buffer (not directly from
2664        * the store, as in the flush case above.)
2665        */
2666       cb = pm->buffer_count - pm->buffer_position;
2667
2668       if (cb > st)
2669          cb = st;
2670
2671       memcpy(pb, pm->buffer + pm->buffer_position, cb);
2672       st -= cb;
2673       pb += cb;
2674       pm->buffer_position += cb;
2675    }
2676 }
2677
2678 /* The callback: */
2679 static void PNGCBAPI
2680 modifier_read(png_structp ppIn, png_bytep pb, png_size_t st)
2681 {
2682    png_const_structp pp = ppIn;
2683    png_modifier *pm = voidcast(png_modifier*, png_get_io_ptr(pp));
2684
2685    if (pm == NULL || pm->this.pread != pp)
2686       png_error(pp, "bad modifier_read call");
2687
2688    modifier_read_imp(pm, pb, st);
2689 }
2690
2691 /* Like store_progressive_read but the data is getting changed as we go so we
2692  * need a local buffer.
2693  */
2694 static void
2695 modifier_progressive_read(png_modifier *pm, png_structp pp, png_infop pi)
2696 {
2697    if (pm->this.pread != pp || pm->this.current == NULL ||
2698        pm->this.next == NULL)
2699       png_error(pp, "store state damaged (progressive)");
2700
2701    /* This is another Horowitz and Hill random noise generator.  In this case
2702     * the aim is to stress the progressive reader with truly horrible variable
2703     * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers
2704     * is generated.  We could probably just count from 1 to 32767 and get as
2705     * good a result.
2706     */
2707    for (;;)
2708    {
2709       static png_uint_32 noise = 1;
2710       png_size_t cb, cbAvail;
2711       png_byte buffer[512];
2712
2713       /* Generate 15 more bits of stuff: */
2714       noise = (noise << 9) | ((noise ^ (noise >> (9-5))) & 0x1ff);
2715       cb = noise & 0x1ff;
2716
2717       /* Check that this number of bytes are available (in the current buffer.)
2718        * (This doesn't quite work - the modifier might delete a chunk; unlikely
2719        * but possible, it doesn't happen at present because the modifier only
2720        * adds chunks to standard images.)
2721        */
2722       cbAvail = store_read_buffer_avail(&pm->this);
2723       if (pm->buffer_count > pm->buffer_position)
2724          cbAvail += pm->buffer_count - pm->buffer_position;
2725
2726       if (cb > cbAvail)
2727       {
2728          /* Check for EOF: */
2729          if (cbAvail == 0)
2730             break;
2731
2732          cb = cbAvail;
2733       }
2734
2735       modifier_read_imp(pm, buffer, cb);
2736       png_process_data(pp, pi, buffer, cb);
2737    }
2738
2739    /* Check the invariants at the end (if this fails it's a problem in this
2740     * file!)
2741     */
2742    if (pm->buffer_count > pm->buffer_position ||
2743        pm->this.next != &pm->this.current->data ||
2744        pm->this.readpos < pm->this.current->datacount)
2745       png_error(pp, "progressive read implementation error");
2746 }
2747
2748 /* Set up a modifier. */
2749 static png_structp
2750 set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id,
2751     PNG_CONST char *name)
2752 {
2753    /* Do this first so that the modifier fields are cleared even if an error
2754     * happens allocating the png_struct.  No allocation is done here so no
2755     * cleanup is required.
2756     */
2757    pm->state = modifier_start;
2758    pm->bit_depth = 0;
2759    pm->colour_type = 255;
2760
2761    pm->pending_len = 0;
2762    pm->pending_chunk = 0;
2763    pm->flush = 0;
2764    pm->buffer_count = 0;
2765    pm->buffer_position = 0;
2766
2767    return set_store_for_read(&pm->this, ppi, id, name);
2768 }
2769
2770
2771 /******************************** MODIFICATIONS *******************************/
2772 /* Standard modifications to add chunks.  These do not require the _SUPPORTED
2773  * macros because the chunks can be there regardless of whether this specific
2774  * libpng supports them.
2775  */
2776 typedef struct gama_modification
2777 {
2778    png_modification this;
2779    png_fixed_point  gamma;
2780 } gama_modification;
2781
2782 static int
2783 gama_modify(png_modifier *pm, png_modification *me, int add)
2784 {
2785    UNUSED(add)
2786    /* This simply dumps the given gamma value into the buffer. */
2787    png_save_uint_32(pm->buffer, 4);
2788    png_save_uint_32(pm->buffer+4, CHUNK_gAMA);
2789    png_save_uint_32(pm->buffer+8, ((gama_modification*)me)->gamma);
2790    return 1;
2791 }
2792
2793 static void
2794 gama_modification_init(gama_modification *me, png_modifier *pm, double gammad)
2795 {
2796    double g;
2797
2798    modification_init(&me->this);
2799    me->this.chunk = CHUNK_gAMA;
2800    me->this.modify_fn = gama_modify;
2801    me->this.add = CHUNK_PLTE;
2802    g = fix(gammad);
2803    me->gamma = (png_fixed_point)g;
2804    me->this.next = pm->modifications;
2805    pm->modifications = &me->this;
2806 }
2807
2808 typedef struct chrm_modification
2809 {
2810    png_modification          this;
2811    PNG_CONST color_encoding *encoding;
2812    png_fixed_point           wx, wy, rx, ry, gx, gy, bx, by;
2813 } chrm_modification;
2814
2815 static int
2816 chrm_modify(png_modifier *pm, png_modification *me, int add)
2817 {
2818    UNUSED(add)
2819    /* As with gAMA this just adds the required cHRM chunk to the buffer. */
2820    png_save_uint_32(pm->buffer   , 32);
2821    png_save_uint_32(pm->buffer+ 4, CHUNK_cHRM);
2822    png_save_uint_32(pm->buffer+ 8, ((chrm_modification*)me)->wx);
2823    png_save_uint_32(pm->buffer+12, ((chrm_modification*)me)->wy);
2824    png_save_uint_32(pm->buffer+16, ((chrm_modification*)me)->rx);
2825    png_save_uint_32(pm->buffer+20, ((chrm_modification*)me)->ry);
2826    png_save_uint_32(pm->buffer+24, ((chrm_modification*)me)->gx);
2827    png_save_uint_32(pm->buffer+28, ((chrm_modification*)me)->gy);
2828    png_save_uint_32(pm->buffer+32, ((chrm_modification*)me)->bx);
2829    png_save_uint_32(pm->buffer+36, ((chrm_modification*)me)->by);
2830    return 1;
2831 }
2832
2833 static void
2834 chrm_modification_init(chrm_modification *me, png_modifier *pm,
2835    PNG_CONST color_encoding *encoding)
2836 {
2837    CIE_color white = white_point(encoding);
2838
2839    /* Original end points: */
2840    me->encoding = encoding;
2841
2842    /* Chromaticities (in fixed point): */
2843    me->wx = fix(chromaticity_x(white));
2844    me->wy = fix(chromaticity_y(white));
2845
2846    me->rx = fix(chromaticity_x(encoding->red));
2847    me->ry = fix(chromaticity_y(encoding->red));
2848    me->gx = fix(chromaticity_x(encoding->green));
2849    me->gy = fix(chromaticity_y(encoding->green));
2850    me->bx = fix(chromaticity_x(encoding->blue));
2851    me->by = fix(chromaticity_y(encoding->blue));
2852
2853    modification_init(&me->this);
2854    me->this.chunk = CHUNK_cHRM;
2855    me->this.modify_fn = chrm_modify;
2856    me->this.add = CHUNK_PLTE;
2857    me->this.next = pm->modifications;
2858    pm->modifications = &me->this;
2859 }
2860
2861 typedef struct srgb_modification
2862 {
2863    png_modification this;
2864    png_byte         intent;
2865 } srgb_modification;
2866
2867 static int
2868 srgb_modify(png_modifier *pm, png_modification *me, int add)
2869 {
2870    UNUSED(add)
2871    /* As above, ignore add and just make a new chunk */
2872    png_save_uint_32(pm->buffer, 1);
2873    png_save_uint_32(pm->buffer+4, CHUNK_sRGB);
2874    pm->buffer[8] = ((srgb_modification*)me)->intent;
2875    return 1;
2876 }
2877
2878 static void
2879 srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent)
2880 {
2881    modification_init(&me->this);
2882    me->this.chunk = CHUNK_sBIT;
2883
2884    if (intent <= 3) /* if valid, else *delete* sRGB chunks */
2885    {
2886       me->this.modify_fn = srgb_modify;
2887       me->this.add = CHUNK_PLTE;
2888       me->intent = intent;
2889    }
2890
2891    else
2892    {
2893       me->this.modify_fn = 0;
2894       me->this.add = 0;
2895       me->intent = 0;
2896    }
2897
2898    me->this.next = pm->modifications;
2899    pm->modifications = &me->this;
2900 }
2901
2902 #ifdef PNG_READ_GAMMA_SUPPORTED
2903 typedef struct sbit_modification
2904 {
2905    png_modification this;
2906    png_byte         sbit;
2907 } sbit_modification;
2908
2909 static int
2910 sbit_modify(png_modifier *pm, png_modification *me, int add)
2911 {
2912    png_byte sbit = ((sbit_modification*)me)->sbit;
2913    if (pm->bit_depth > sbit)
2914    {
2915       int cb = 0;
2916       switch (pm->colour_type)
2917       {
2918          case 0:
2919             cb = 1;
2920             break;
2921
2922          case 2:
2923          case 3:
2924             cb = 3;
2925             break;
2926
2927          case 4:
2928             cb = 2;
2929             break;
2930
2931          case 6:
2932             cb = 4;
2933             break;
2934
2935          default:
2936             png_error(pm->this.pread,
2937                "unexpected colour type in sBIT modification");
2938       }
2939
2940       png_save_uint_32(pm->buffer, cb);
2941       png_save_uint_32(pm->buffer+4, CHUNK_sBIT);
2942
2943       while (cb > 0)
2944          (pm->buffer+8)[--cb] = sbit;
2945
2946       return 1;
2947    }
2948    else if (!add)
2949    {
2950       /* Remove the sBIT chunk */
2951       pm->buffer_count = pm->buffer_position = 0;
2952       return 1;
2953    }
2954    else
2955       return 0; /* do nothing */
2956 }
2957
2958 static void
2959 sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit)
2960 {
2961    modification_init(&me->this);
2962    me->this.chunk = CHUNK_sBIT;
2963    me->this.modify_fn = sbit_modify;
2964    me->this.add = CHUNK_PLTE;
2965    me->sbit = sbit;
2966    me->this.next = pm->modifications;
2967    pm->modifications = &me->this;
2968 }
2969 #endif /* PNG_READ_GAMMA_SUPPORTED */
2970 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
2971
2972 /***************************** STANDARD PNG FILES *****************************/
2973 /* Standard files - write and save standard files. */
2974 /* There are two basic forms of standard images.  Those which attempt to have
2975  * all the possible pixel values (not possible for 16bpp images, but a range of
2976  * values are produced) and those which have a range of image sizes.  The former
2977  * are used for testing transforms, in particular gamma correction and bit
2978  * reduction and increase.  The latter are reserved for testing the behavior of
2979  * libpng with respect to 'odd' image sizes - particularly small images where
2980  * rows become 1 byte and interlace passes disappear.
2981  *
2982  * The first, most useful, set are the 'transform' images, the second set of
2983  * small images are the 'size' images.
2984  *
2985  * The transform files are constructed with rows which fit into a 1024 byte row
2986  * buffer.  This makes allocation easier below.  Further regardless of the file
2987  * format every row has 128 pixels (giving 1024 bytes for 64bpp formats).
2988  *
2989  * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed
2990  * and with an ID derived from the colour type, bit depth and interlace type
2991  * as above (FILEID).  The width (128) and height (variable) are not stored in
2992  * the FILEID - instead the fields are set to 0, indicating a transform file.
2993  *
2994  * The size files ar constructed with rows a maximum of 128 bytes wide, allowing
2995  * a maximum width of 16 pixels (for the 64bpp case.)  They also have a maximum
2996  * height of 16 rows.  The width and height are stored in the FILEID and, being
2997  * non-zero, indicate a size file.
2998  *
2999  * Because the PNG filter code is typically the largest CPU consumer within
3000  * libpng itself there is a tendency to attempt to optimize it.  This results in
3001  * special case code which needs to be validated.  To cause this to happen the
3002  * 'size' images are made to use each possible filter, in so far as this is
3003  * possible for smaller images.
3004  *
3005  * For palette image (colour type 3) multiple transform images are stored with
3006  * the same bit depth to allow testing of more colour combinations -
3007  * particularly important for testing the gamma code because libpng uses a
3008  * different code path for palette images.  For size images a single palette is
3009  * used.
3010  */
3011
3012 /* Make a 'standard' palette.  Because there are only 256 entries in a palette
3013  * (maximum) this actually makes a random palette in the hope that enough tests
3014  * will catch enough errors.  (Note that the same palette isn't produced every
3015  * time for the same test - it depends on what previous tests have been run -
3016  * but a given set of arguments to pngvalid will always produce the same palette
3017  * at the same test!  This is why pseudo-random number generators are useful for
3018  * testing.)
3019  *
3020  * The store must be open for write when this is called, otherwise an internal
3021  * error will occur.  This routine contains its own magic number seed, so the
3022  * palettes generated don't change if there are intervening errors (changing the
3023  * calls to the store_mark seed.)
3024  */
3025 static store_palette_entry *
3026 make_standard_palette(png_store* ps, int npalette, int do_tRNS)
3027 {
3028    static png_uint_32 palette_seed[2] = { 0x87654321, 9 };
3029
3030    int i = 0;
3031    png_byte values[256][4];
3032
3033    /* Always put in black and white plus the six primary and secondary colors.
3034     */
3035    for (; i<8; ++i)
3036    {
3037       values[i][1] = (png_byte)((i&1) ? 255U : 0U);
3038       values[i][2] = (png_byte)((i&2) ? 255U : 0U);
3039       values[i][3] = (png_byte)((i&4) ? 255U : 0U);
3040    }
3041
3042    /* Then add 62 grays (one quarter of the remaining 256 slots). */
3043    {
3044       int j = 0;
3045       png_byte random_bytes[4];
3046       png_byte need[256];
3047
3048       need[0] = 0; /*got black*/
3049       memset(need+1, 1, (sizeof need)-2); /*need these*/
3050       need[255] = 0; /*but not white*/
3051
3052       while (i<70)
3053       {
3054          png_byte b;
3055
3056          if (j==0)
3057          {
3058             make_four_random_bytes(palette_seed, random_bytes);
3059             j = 4;
3060          }
3061
3062          b = random_bytes[--j];
3063          if (need[b])
3064          {
3065             values[i][1] = b;
3066             values[i][2] = b;
3067             values[i++][3] = b;
3068          }
3069       }
3070    }
3071
3072    /* Finally add 192 colors at random - don't worry about matches to things we
3073     * already have, chance is less than 1/65536.  Don't worry about grays,
3074     * chance is the same, so we get a duplicate or extra gray less than 1 time
3075     * in 170.
3076     */
3077    for (; i<256; ++i)
3078       make_four_random_bytes(palette_seed, values[i]);
3079
3080    /* Fill in the alpha values in the first byte.  Just use all possible values
3081     * (0..255) in an apparently random order:
3082     */
3083    {
3084       store_palette_entry *palette;
3085       png_byte selector[4];
3086
3087       make_four_random_bytes(palette_seed, selector);
3088
3089       if (do_tRNS)
3090          for (i=0; i<256; ++i)
3091             values[i][0] = (png_byte)(i ^ selector[0]);
3092
3093       else
3094          for (i=0; i<256; ++i)
3095             values[i][0] = 255; /* no transparency/tRNS chunk */
3096
3097       /* 'values' contains 256 ARGB values, but we only need 'npalette'.
3098        * 'npalette' will always be a power of 2: 2, 4, 16 or 256.  In the low
3099        * bit depth cases select colors at random, else it is difficult to have
3100        * a set of low bit depth palette test with any chance of a reasonable
3101        * range of colors.  Do this by randomly permuting values into the low
3102        * 'npalette' entries using an XOR mask generated here.  This also
3103        * permutes the npalette == 256 case in a potentially useful way (there is
3104        * no relationship between palette index and the color value therein!)
3105        */
3106       palette = store_write_palette(ps, npalette);
3107
3108       for (i=0; i<npalette; ++i)
3109       {
3110          palette[i].alpha = values[i ^ selector[1]][0];
3111          palette[i].red   = values[i ^ selector[1]][1];
3112          palette[i].green = values[i ^ selector[1]][2];
3113          palette[i].blue  = values[i ^ selector[1]][3];
3114       }
3115
3116       return palette;
3117    }
3118 }
3119
3120 /* Initialize a standard palette on a write stream.  The 'do_tRNS' argument
3121  * indicates whether or not to also set the tRNS chunk.
3122  */
3123 /* TODO: the png_structp here can probably be 'const' in the future */
3124 static void
3125 init_standard_palette(png_store *ps, png_structp pp, png_infop pi, int npalette,
3126    int do_tRNS)
3127 {
3128    store_palette_entry *ppal = make_standard_palette(ps, npalette, do_tRNS);
3129
3130    {
3131       int i;
3132       png_color palette[256];
3133
3134       /* Set all entries to detect overread errors. */
3135       for (i=0; i<npalette; ++i)
3136       {
3137          palette[i].red = ppal[i].red;
3138          palette[i].green = ppal[i].green;
3139          palette[i].blue = ppal[i].blue;
3140       }
3141
3142       /* Just in case fill in the rest with detectable values: */
3143       for (; i<256; ++i)
3144          palette[i].red = palette[i].green = palette[i].blue = 42;
3145
3146       png_set_PLTE(pp, pi, palette, npalette);
3147    }
3148
3149    if (do_tRNS)
3150    {
3151       int i, j;
3152       png_byte tRNS[256];
3153
3154       /* Set all the entries, but skip trailing opaque entries */
3155       for (i=j=0; i<npalette; ++i)
3156          if ((tRNS[i] = ppal[i].alpha) < 255)
3157             j = i+1;
3158
3159       /* Fill in the remainder with a detectable value: */
3160       for (; i<256; ++i)
3161          tRNS[i] = 24;
3162
3163 #     ifdef PNG_WRITE_tRNS_SUPPORTED
3164          if (j > 0)
3165             png_set_tRNS(pp, pi, tRNS, j, 0/*color*/);
3166 #     endif
3167    }
3168 }
3169
3170 /* The number of passes is related to the interlace type. There was no libpng
3171  * API to determine this prior to 1.5, so we need an inquiry function:
3172  */
3173 static int
3174 npasses_from_interlace_type(png_const_structp pp, int interlace_type)
3175 {
3176    switch (interlace_type)
3177    {
3178    default:
3179       png_error(pp, "invalid interlace type");
3180
3181    case PNG_INTERLACE_NONE:
3182       return 1;
3183
3184    case PNG_INTERLACE_ADAM7:
3185       return PNG_INTERLACE_ADAM7_PASSES;
3186    }
3187 }
3188
3189 static unsigned int
3190 bit_size(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
3191 {
3192    switch (colour_type)
3193    {
3194       default: png_error(pp, "invalid color type");
3195
3196       case 0:  return bit_depth;
3197
3198       case 2:  return 3*bit_depth;
3199
3200       case 3:  return bit_depth;
3201
3202       case 4:  return 2*bit_depth;
3203
3204       case 6:  return 4*bit_depth;
3205    }
3206 }
3207
3208 #define TRANSFORM_WIDTH  128U
3209 #define TRANSFORM_ROWMAX (TRANSFORM_WIDTH*8U)
3210 #define SIZE_ROWMAX (16*8U) /* 16 pixels, max 8 bytes each - 128 bytes */
3211 #define STANDARD_ROWMAX TRANSFORM_ROWMAX /* The larger of the two */
3212 #define SIZE_HEIGHTMAX 16 /* Maximum range of size images */
3213
3214 static size_t
3215 transform_rowsize(png_const_structp pp, png_byte colour_type,
3216    png_byte bit_depth)
3217 {
3218    return (TRANSFORM_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8;
3219 }
3220
3221 /* transform_width(pp, colour_type, bit_depth) current returns the same number
3222  * every time, so just use a macro:
3223  */
3224 #define transform_width(pp, colour_type, bit_depth) TRANSFORM_WIDTH
3225
3226 static png_uint_32
3227 transform_height(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
3228 {
3229    switch (bit_size(pp, colour_type, bit_depth))
3230    {
3231       case 1:
3232       case 2:
3233       case 4:
3234          return 1;   /* Total of 128 pixels */
3235
3236       case 8:
3237          return 2;   /* Total of 256 pixels/bytes */
3238
3239       case 16:
3240          return 512; /* Total of 65536 pixels */
3241
3242       case 24:
3243       case 32:
3244          return 512; /* 65536 pixels */
3245
3246       case 48:
3247       case 64:
3248          return 2048;/* 4 x 65536 pixels. */
3249 #        define TRANSFORM_HEIGHTMAX 2048
3250
3251       default:
3252          return 0;   /* Error, will be caught later */
3253    }
3254 }
3255
3256 #ifdef PNG_READ_SUPPORTED
3257 /* The following can only be defined here, now we have the definitions
3258  * of the transform image sizes.
3259  */
3260 static png_uint_32
3261 standard_width(png_const_structp pp, png_uint_32 id)
3262 {
3263    png_uint_32 width = WIDTH_FROM_ID(id);
3264    UNUSED(pp)
3265
3266    if (width == 0)
3267       width = transform_width(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3268
3269    return width;
3270 }
3271
3272 static png_uint_32
3273 standard_height(png_const_structp pp, png_uint_32 id)
3274 {
3275    png_uint_32 height = HEIGHT_FROM_ID(id);
3276
3277    if (height == 0)
3278       height = transform_height(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3279
3280    return height;
3281 }
3282
3283 static png_uint_32
3284 standard_rowsize(png_const_structp pp, png_uint_32 id)
3285 {
3286    png_uint_32 width = standard_width(pp, id);
3287
3288    /* This won't overflow: */
3289    width *= bit_size(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3290    return (width + 7) / 8;
3291 }
3292 #endif /* PNG_READ_SUPPORTED */
3293
3294 static void
3295 transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
3296    png_byte colour_type, png_byte bit_depth, png_uint_32 y)
3297 {
3298    png_uint_32 v = y << 7;
3299    png_uint_32 i = 0;
3300
3301    switch (bit_size(pp, colour_type, bit_depth))
3302    {
3303       case 1:
3304          while (i<128/8) buffer[i] = (png_byte)(v & 0xff), v += 17, ++i;
3305          return;
3306
3307       case 2:
3308          while (i<128/4) buffer[i] = (png_byte)(v & 0xff), v += 33, ++i;
3309          return;
3310
3311       case 4:
3312          while (i<128/2) buffer[i] = (png_byte)(v & 0xff), v += 65, ++i;
3313          return;
3314
3315       case 8:
3316          /* 256 bytes total, 128 bytes in each row set as follows: */
3317          while (i<128) buffer[i] = (png_byte)(v & 0xff), ++v, ++i;
3318          return;
3319
3320       case 16:
3321          /* Generate all 65536 pixel values in order, which includes the 8 bit
3322           * GA case as well as the 16 bit G case.
3323           */
3324          while (i<128)
3325          {
3326             buffer[2*i] = (png_byte)((v>>8) & 0xff);
3327             buffer[2*i+1] = (png_byte)(v & 0xff);
3328             ++v;
3329             ++i;
3330          }
3331
3332          return;
3333
3334       case 24:
3335          /* 65535 pixels, but rotate the values. */
3336          while (i<128)
3337          {
3338             /* Three bytes per pixel, r, g, b, make b by r^g */
3339             buffer[3*i+0] = (png_byte)((v >> 8) & 0xff);
3340             buffer[3*i+1] = (png_byte)(v & 0xff);
3341             buffer[3*i+2] = (png_byte)(((v >> 8) ^ v) & 0xff);
3342             ++v;
3343             ++i;
3344          }
3345
3346          return;
3347
3348       case 32:
3349          /* 65535 pixels, r, g, b, a; just replicate */
3350          while (i<128)
3351          {
3352             buffer[4*i+0] = (png_byte)((v >> 8) & 0xff);
3353             buffer[4*i+1] = (png_byte)(v & 0xff);
3354             buffer[4*i+2] = (png_byte)((v >> 8) & 0xff);
3355             buffer[4*i+3] = (png_byte)(v & 0xff);
3356             ++v;
3357             ++i;
3358          }
3359
3360          return;
3361
3362       case 48:
3363          /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at
3364           * each pixel, g increase by 257 (0x101) and 'b' by 0x1111:
3365           */
3366          while (i<128)
3367          {
3368             png_uint_32 t = v++;
3369             buffer[6*i+0] = (png_byte)((t >> 8) & 0xff);
3370             buffer[6*i+1] = (png_byte)(t & 0xff);
3371             t *= 257;
3372             buffer[6*i+2] = (png_byte)((t >> 8) & 0xff);
3373             buffer[6*i+3] = (png_byte)(t & 0xff);
3374             t *= 17;
3375             buffer[6*i+4] = (png_byte)((t >> 8) & 0xff);
3376             buffer[6*i+5] = (png_byte)(t & 0xff);
3377             ++i;
3378          }
3379
3380          return;
3381
3382       case 64:
3383          /* As above in the 32 bit case. */
3384          while (i<128)
3385          {
3386             png_uint_32 t = v++;
3387             buffer[8*i+0] = (png_byte)((t >> 8) & 0xff);
3388             buffer[8*i+1] = (png_byte)(t & 0xff);
3389             buffer[8*i+4] = (png_byte)((t >> 8) & 0xff);
3390             buffer[8*i+5] = (png_byte)(t & 0xff);
3391             t *= 257;
3392             buffer[8*i+2] = (png_byte)((t >> 8) & 0xff);
3393             buffer[8*i+3] = (png_byte)(t & 0xff);
3394             buffer[8*i+6] = (png_byte)((t >> 8) & 0xff);
3395             buffer[8*i+7] = (png_byte)(t & 0xff);
3396             ++i;
3397          }
3398          return;
3399
3400       default:
3401          break;
3402    }
3403
3404    png_error(pp, "internal error");
3405 }
3406
3407 /* This is just to do the right cast - could be changed to a function to check
3408  * 'bd' but there isn't much point.
3409  */
3410 #define DEPTH(bd) ((png_byte)(1U << (bd)))
3411
3412 /* This is just a helper for compiling on minimal systems with no write
3413  * interlacing support.  If there is no write interlacing we can't generate test
3414  * cases with interlace:
3415  */
3416 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
3417 #  define INTERLACE_LAST PNG_INTERLACE_LAST
3418 #  define check_interlace_type(type) ((void)(type))
3419 #else
3420 #  define INTERLACE_LAST (PNG_INTERLACE_NONE+1)
3421 #  define png_set_interlace_handling(a) (1)
3422
3423 static void
3424 check_interlace_type(int PNG_CONST interlace_type)
3425 {
3426    if (interlace_type != PNG_INTERLACE_NONE)
3427    {
3428       /* This is an internal error - --interlace tests should be skipped, not
3429        * attempted.
3430        */
3431       fprintf(stderr, "pngvalid: no interlace support\n");
3432       exit(99);
3433    }
3434 }
3435 #endif
3436
3437 /* Make a standardized image given a an image colour type, bit depth and
3438  * interlace type.  The standard images have a very restricted range of
3439  * rows and heights and are used for testing transforms rather than image
3440  * layout details.  See make_size_images below for a way to make images
3441  * that test odd sizes along with the libpng interlace handling.
3442  */
3443 static void
3444 make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
3445     png_byte PNG_CONST bit_depth, unsigned int palette_number,
3446     int interlace_type, png_const_charp name)
3447 {
3448    context(ps, fault);
3449
3450    check_interlace_type(interlace_type);
3451
3452    Try
3453    {
3454       png_infop pi;
3455       png_structp pp = set_store_for_write(ps, &pi, name);
3456       png_uint_32 h;
3457
3458       /* In the event of a problem return control to the Catch statement below
3459        * to do the clean up - it is not possible to 'return' directly from a Try
3460        * block.
3461        */
3462       if (pp == NULL)
3463          Throw ps;
3464
3465       h = transform_height(pp, colour_type, bit_depth);
3466
3467       png_set_IHDR(pp, pi, transform_width(pp, colour_type, bit_depth), h,
3468          bit_depth, colour_type, interlace_type,
3469          PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3470
3471 #ifdef PNG_TEXT_SUPPORTED
3472 #  if defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)
3473 #     define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_zTXt
3474 #  else
3475 #     define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_NONE
3476 #  endif
3477       {
3478          static char key[] = "image name"; /* must be writeable */
3479          size_t pos;
3480          png_text text;
3481          char copy[FILE_NAME_SIZE];
3482
3483          /* Use a compressed text string to test the correct interaction of text
3484           * compression and IDAT compression.
3485           */
3486          text.compression = TEXT_COMPRESSION;
3487          text.key = key;
3488          /* Yuck: the text must be writable! */
3489          pos = safecat(copy, sizeof copy, 0, ps->wname);
3490          text.text = copy;
3491          text.text_length = pos;
3492          text.itxt_length = 0;
3493          text.lang = 0;
3494          text.lang_key = 0;
3495
3496          png_set_text(pp, pi, &text, 1);
3497       }
3498 #endif
3499
3500       if (colour_type == 3) /* palette */
3501          init_standard_palette(ps, pp, pi, 1U << bit_depth, 1/*do tRNS*/);
3502
3503       png_write_info(pp, pi);
3504
3505       if (png_get_rowbytes(pp, pi) !=
3506           transform_rowsize(pp, colour_type, bit_depth))
3507          png_error(pp, "row size incorrect");
3508
3509       else
3510       {
3511          /* Somewhat confusingly this must be called *after* png_write_info
3512           * because if it is called before, the information in *pp has not been
3513           * updated to reflect the interlaced image.
3514           */
3515          int npasses = png_set_interlace_handling(pp);
3516          int pass;
3517
3518          if (npasses != npasses_from_interlace_type(pp, interlace_type))
3519             png_error(pp, "write: png_set_interlace_handling failed");
3520
3521          for (pass=0; pass<npasses; ++pass)
3522          {
3523             png_uint_32 y;
3524
3525             for (y=0; y<h; ++y)
3526             {
3527                png_byte buffer[TRANSFORM_ROWMAX];
3528
3529                transform_row(pp, buffer, colour_type, bit_depth, y);
3530                png_write_row(pp, buffer);
3531             }
3532          }
3533       }
3534
3535 #ifdef PNG_TEXT_SUPPORTED
3536       {
3537          static char key[] = "end marker";
3538          static char comment[] = "end";
3539          png_text text;
3540
3541          /* Use a compressed text string to test the correct interaction of text
3542           * compression and IDAT compression.
3543           */
3544          text.compression = TEXT_COMPRESSION;
3545          text.key = key;
3546          text.text = comment;
3547          text.text_length = (sizeof comment)-1;
3548          text.itxt_length = 0;
3549          text.lang = 0;
3550          text.lang_key = 0;
3551
3552          png_set_text(pp, pi, &text, 1);
3553       }
3554 #endif
3555
3556       png_write_end(pp, pi);
3557
3558       /* And store this under the appropriate id, then clean up. */
3559       store_storefile(ps, FILEID(colour_type, bit_depth, palette_number,
3560          interlace_type, 0, 0, 0));
3561
3562       store_write_reset(ps);
3563    }
3564
3565    Catch(fault)
3566    {
3567       /* Use the png_store returned by the exception. This may help the compiler
3568        * because 'ps' is not used in this branch of the setjmp.  Note that fault
3569        * and ps will always be the same value.
3570        */
3571       store_write_reset(fault);
3572    }
3573 }
3574
3575 static void
3576 make_transform_images(png_store *ps)
3577 {
3578    png_byte colour_type = 0;
3579    png_byte bit_depth = 0;
3580    unsigned int palette_number = 0;
3581
3582    /* This is in case of errors. */
3583    safecat(ps->test, sizeof ps->test, 0, "make standard images");
3584
3585    /* Use next_format to enumerate all the combinations we test, including
3586     * generating multiple low bit depth palette images.
3587     */
3588    while (next_format(&colour_type, &bit_depth, &palette_number, 0))
3589    {
3590       int interlace_type;
3591
3592       for (interlace_type = PNG_INTERLACE_NONE;
3593            interlace_type < INTERLACE_LAST; ++interlace_type)
3594       {
3595          char name[FILE_NAME_SIZE];
3596
3597          standard_name(name, sizeof name, 0, colour_type, bit_depth,
3598             palette_number, interlace_type, 0, 0, 0);
3599          make_transform_image(ps, colour_type, bit_depth, palette_number,
3600             interlace_type, name);
3601       }
3602    }
3603 }
3604
3605 /* The following two routines use the PNG interlace support macros from
3606  * png.h to interlace or deinterlace rows.
3607  */
3608 static void
3609 interlace_row(png_bytep buffer, png_const_bytep imageRow,
3610    unsigned int pixel_size, png_uint_32 w, int pass)
3611 {
3612    png_uint_32 xin, xout, xstep;
3613
3614    /* Note that this can, trivially, be optimized to a memcpy on pass 7, the
3615     * code is presented this way to make it easier to understand.  In practice
3616     * consult the code in the libpng source to see other ways of doing this.
3617     */
3618    xin = PNG_PASS_START_COL(pass);
3619    xstep = 1U<<PNG_PASS_COL_SHIFT(pass);
3620
3621    for (xout=0; xin<w; xin+=xstep)
3622    {
3623       pixel_copy(buffer, xout, imageRow, xin, pixel_size);
3624       ++xout;
3625    }
3626 }
3627
3628 #ifdef PNG_READ_SUPPORTED
3629 static void
3630 deinterlace_row(png_bytep buffer, png_const_bytep row,
3631    unsigned int pixel_size, png_uint_32 w, int pass)
3632 {
3633    /* The inverse of the above, 'row' is part of row 'y' of the output image,
3634     * in 'buffer'.  The image is 'w' wide and this is pass 'pass', distribute
3635     * the pixels of row into buffer and return the number written (to allow
3636     * this to be checked).
3637     */
3638    png_uint_32 xin, xout, xstep;
3639
3640    xout = PNG_PASS_START_COL(pass);
3641    xstep = 1U<<PNG_PASS_COL_SHIFT(pass);
3642
3643    for (xin=0; xout<w; xout+=xstep)
3644    {
3645       pixel_copy(buffer, xout, row, xin, pixel_size);
3646       ++xin;
3647    }
3648 }
3649 #endif /* PNG_READ_SUPPORTED */
3650
3651 /* Build a single row for the 'size' test images; this fills in only the
3652  * first bit_width bits of the sample row.
3653  */
3654 static void
3655 size_row(png_byte buffer[SIZE_ROWMAX], png_uint_32 bit_width, png_uint_32 y)
3656 {
3657    /* height is in the range 1 to 16, so: */
3658    y = ((y & 1) << 7) + ((y & 2) << 6) + ((y & 4) << 5) + ((y & 8) << 4);
3659    /* the following ensures bits are set in small images: */
3660    y ^= 0xA5;
3661
3662    while (bit_width >= 8)
3663       *buffer++ = (png_byte)y++, bit_width -= 8;
3664
3665    /* There may be up to 7 remaining bits, these go in the most significant
3666     * bits of the byte.
3667     */
3668    if (bit_width > 0)
3669    {
3670       png_uint_32 mask = (1U<<(8-bit_width))-1;
3671       *buffer = (png_byte)((*buffer & mask) | (y & ~mask));
3672    }
3673 }
3674
3675 static void
3676 make_size_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
3677     png_byte PNG_CONST bit_depth, int PNG_CONST interlace_type,
3678     png_uint_32 PNG_CONST w, png_uint_32 PNG_CONST h,
3679     int PNG_CONST do_interlace)
3680 {
3681    context(ps, fault);
3682
3683    /* At present libpng does not support the write of an interlaced image unless
3684     * PNG_WRITE_INTERLACING_SUPPORTED, even with do_interlace so the code here
3685     * does the pixel interlace itself, so:
3686     */
3687    check_interlace_type(interlace_type);
3688
3689    Try
3690    {
3691       png_infop pi;
3692       png_structp pp;
3693       unsigned int pixel_size;
3694
3695       /* Make a name and get an appropriate id for the store: */
3696       char name[FILE_NAME_SIZE];
3697       PNG_CONST png_uint_32 id = FILEID(colour_type, bit_depth, 0/*palette*/,
3698          interlace_type, w, h, do_interlace);
3699
3700       standard_name_from_id(name, sizeof name, 0, id);
3701       pp = set_store_for_write(ps, &pi, name);
3702
3703       /* In the event of a problem return control to the Catch statement below
3704        * to do the clean up - it is not possible to 'return' directly from a Try
3705        * block.
3706        */
3707       if (pp == NULL)
3708          Throw ps;
3709
3710       png_set_IHDR(pp, pi, w, h, bit_depth, colour_type, interlace_type,
3711          PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3712
3713 #ifdef PNG_TEXT_SUPPORTED
3714       {
3715          static char key[] = "image name"; /* must be writeable */
3716          size_t pos;
3717          png_text text;
3718          char copy[FILE_NAME_SIZE];
3719
3720          /* Use a compressed text string to test the correct interaction of text
3721           * compression and IDAT compression.
3722           */
3723          text.compression = TEXT_COMPRESSION;
3724          text.key = key;
3725          /* Yuck: the text must be writable! */
3726          pos = safecat(copy, sizeof copy, 0, ps->wname);
3727          text.text = copy;
3728          text.text_length = pos;
3729          text.itxt_length = 0;
3730          text.lang = 0;
3731          text.lang_key = 0;
3732
3733          png_set_text(pp, pi, &text, 1);
3734       }
3735 #endif
3736
3737       if (colour_type == 3) /* palette */
3738          init_standard_palette(ps, pp, pi, 1U << bit_depth, 0/*do tRNS*/);
3739
3740       png_write_info(pp, pi);
3741
3742       /* Calculate the bit size, divide by 8 to get the byte size - this won't
3743        * overflow because we know the w values are all small enough even for
3744        * a system where 'unsigned int' is only 16 bits.
3745        */
3746       pixel_size = bit_size(pp, colour_type, bit_depth);
3747       if (png_get_rowbytes(pp, pi) != ((w * pixel_size) + 7) / 8)
3748          png_error(pp, "row size incorrect");
3749
3750       else
3751       {
3752          int npasses = npasses_from_interlace_type(pp, interlace_type);
3753          png_uint_32 y;
3754          int pass;
3755 #        ifdef PNG_WRITE_FILTER_SUPPORTED
3756             int nfilter = PNG_FILTER_VALUE_LAST;
3757 #        endif
3758          png_byte image[16][SIZE_ROWMAX];
3759
3760          /* To help consistent error detection make the parts of this buffer
3761           * that aren't set below all '1':
3762           */
3763          memset(image, 0xff, sizeof image);
3764
3765          if (!do_interlace && npasses != png_set_interlace_handling(pp))
3766             png_error(pp, "write: png_set_interlace_handling failed");
3767
3768          /* Prepare the whole image first to avoid making it 7 times: */
3769          for (y=0; y<h; ++y)
3770             size_row(image[y], w * pixel_size, y);
3771
3772          for (pass=0; pass<npasses; ++pass)
3773          {
3774             /* The following two are for checking the macros: */
3775             PNG_CONST png_uint_32 wPass = PNG_PASS_COLS(w, pass);
3776
3777             /* If do_interlace is set we don't call png_write_row for every
3778              * row because some of them are empty.  In fact, for a 1x1 image,
3779              * most of them are empty!
3780              */
3781             for (y=0; y<h; ++y)
3782             {
3783                png_const_bytep row = image[y];
3784                png_byte tempRow[SIZE_ROWMAX];
3785
3786                /* If do_interlace *and* the image is interlaced we
3787                 * need a reduced interlace row; this may be reduced
3788                 * to empty.
3789                 */
3790                if (do_interlace && interlace_type == PNG_INTERLACE_ADAM7)
3791                {
3792                   /* The row must not be written if it doesn't exist, notice
3793                    * that there are two conditions here, either the row isn't
3794                    * ever in the pass or the row would be but isn't wide
3795                    * enough to contribute any pixels.  In fact the wPass test
3796                    * can be used to skip the whole y loop in this case.
3797                    */
3798                   if (PNG_ROW_IN_INTERLACE_PASS(y, pass) && wPass > 0)
3799                   {
3800                      /* Set to all 1's for error detection (libpng tends to
3801                       * set unset things to 0).
3802                       */
3803                      memset(tempRow, 0xff, sizeof tempRow);
3804                      interlace_row(tempRow, row, pixel_size, w, pass);
3805                      row = tempRow;
3806                   }
3807                   else
3808                      continue;
3809                }
3810
3811 #           ifdef PNG_WRITE_FILTER_SUPPORTED
3812                /* Only get to here if the row has some pixels in it, set the
3813                 * filters to 'all' for the very first row and thereafter to a
3814                 * single filter.  It isn't well documented, but png_set_filter
3815                 * does accept a filter number (per the spec) as well as a bit
3816                 * mask.
3817                 *
3818                 * The apparent wackiness of decrementing nfilter rather than
3819                 * incrementing is so that Paeth gets used in all images bigger
3820                 * than 1 row - it's the tricky one.
3821                 */
3822                png_set_filter(pp, 0/*method*/,
3823                   nfilter >= PNG_FILTER_VALUE_LAST ? PNG_ALL_FILTERS : nfilter);
3824
3825                if (nfilter-- == 0)
3826                   nfilter = PNG_FILTER_VALUE_LAST-1;
3827 #           endif
3828
3829                png_write_row(pp, row);
3830             }
3831          }
3832       }
3833
3834 #ifdef PNG_TEXT_SUPPORTED
3835       {
3836          static char key[] = "end marker";
3837          static char comment[] = "end";
3838          png_text text;
3839
3840          /* Use a compressed text string to test the correct interaction of text
3841           * compression and IDAT compression.
3842           */
3843          text.compression = TEXT_COMPRESSION;
3844          text.key = key;
3845          text.text = comment;
3846          text.text_length = (sizeof comment)-1;
3847          text.itxt_length = 0;
3848          text.lang = 0;
3849          text.lang_key = 0;
3850
3851          png_set_text(pp, pi, &text, 1);
3852       }
3853 #endif
3854
3855       png_write_end(pp, pi);
3856
3857       /* And store this under the appropriate id, then clean up. */
3858       store_storefile(ps, id);
3859
3860       store_write_reset(ps);
3861    }
3862
3863    Catch(fault)
3864    {
3865       /* Use the png_store returned by the exception. This may help the compiler
3866        * because 'ps' is not used in this branch of the setjmp.  Note that fault
3867        * and ps will always be the same value.
3868        */
3869       store_write_reset(fault);
3870    }
3871 }
3872
3873 static void
3874 make_size(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, int bdlo,
3875     int PNG_CONST bdhi)
3876 {
3877    for (; bdlo <= bdhi; ++bdlo)
3878    {
3879       png_uint_32 width;
3880
3881       for (width = 1; width <= 16; ++width)
3882       {
3883          png_uint_32 height;
3884
3885          for (height = 1; height <= 16; ++height)
3886          {
3887             /* The four combinations of DIY interlace and interlace or not -
3888              * no interlace + DIY should be identical to no interlace with
3889              * libpng doing it.
3890              */
3891             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_NONE,
3892                width, height, 0);
3893             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_NONE,
3894                width, height, 1);
3895 #        ifdef PNG_WRITE_INTERLACING_SUPPORTED
3896             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
3897                width, height, 0);
3898             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
3899                width, height, 1);
3900 #        endif
3901          }
3902       }
3903    }
3904 }
3905
3906 static void
3907 make_size_images(png_store *ps)
3908 {
3909    /* This is in case of errors. */
3910    safecat(ps->test, sizeof ps->test, 0, "make size images");
3911
3912    /* Arguments are colour_type, low bit depth, high bit depth
3913     */
3914    make_size(ps, 0, 0, WRITE_BDHI);
3915    make_size(ps, 2, 3, WRITE_BDHI);
3916    make_size(ps, 3, 0, 3 /*palette: max 8 bits*/);
3917    make_size(ps, 4, 3, WRITE_BDHI);
3918    make_size(ps, 6, 3, WRITE_BDHI);
3919 }
3920
3921 #ifdef PNG_READ_SUPPORTED
3922 /* Return a row based on image id and 'y' for checking: */
3923 static void
3924 standard_row(png_const_structp pp, png_byte std[STANDARD_ROWMAX],
3925    png_uint_32 id, png_uint_32 y)
3926 {
3927    if (WIDTH_FROM_ID(id) == 0)
3928       transform_row(pp, std, COL_FROM_ID(id), DEPTH_FROM_ID(id), y);
3929    else
3930       size_row(std, WIDTH_FROM_ID(id) * bit_size(pp, COL_FROM_ID(id),
3931          DEPTH_FROM_ID(id)), y);
3932 }
3933 #endif /* PNG_READ_SUPPORTED */
3934
3935 /* Tests - individual test cases */
3936 /* Like 'make_standard' but errors are deliberately introduced into the calls
3937  * to ensure that they get detected - it should not be possible to write an
3938  * invalid image with libpng!
3939  */
3940 /* TODO: the 'set' functions can probably all be made to take a
3941  * png_const_structp rather than a modifiable one.
3942  */
3943 #ifdef PNG_WARNINGS_SUPPORTED
3944 static void
3945 sBIT0_error_fn(png_structp pp, png_infop pi)
3946 {
3947    /* 0 is invalid... */
3948    png_color_8 bad;
3949    bad.red = bad.green = bad.blue = bad.gray = bad.alpha = 0;
3950    png_set_sBIT(pp, pi, &bad);
3951 }
3952
3953 static void
3954 sBIT_error_fn(png_structp pp, png_infop pi)
3955 {
3956    png_byte bit_depth;
3957    png_color_8 bad;
3958
3959    if (png_get_color_type(pp, pi) == PNG_COLOR_TYPE_PALETTE)
3960       bit_depth = 8;
3961
3962    else
3963       bit_depth = png_get_bit_depth(pp, pi);
3964
3965    /* Now we know the bit depth we can easily generate an invalid sBIT entry */
3966    bad.red = bad.green = bad.blue = bad.gray = bad.alpha =
3967       (png_byte)(bit_depth+1);
3968    png_set_sBIT(pp, pi, &bad);
3969 }
3970
3971 static PNG_CONST struct
3972 {
3973    void          (*fn)(png_structp, png_infop);
3974    PNG_CONST char *msg;
3975    unsigned int    warning :1; /* the error is a warning... */
3976 } error_test[] =
3977     {
3978        /* no warnings makes these errors undetectable. */
3979        { sBIT0_error_fn, "sBIT(0): failed to detect error", 1 },
3980        { sBIT_error_fn, "sBIT(too big): failed to detect error", 1 },
3981     };
3982
3983 static void
3984 make_error(png_store* volatile psIn, png_byte PNG_CONST colour_type,
3985     png_byte bit_depth, int interlace_type, int test, png_const_charp name)
3986 {
3987    png_store * volatile ps = psIn;
3988
3989    context(ps, fault);
3990
3991    check_interlace_type(interlace_type);
3992
3993    Try
3994    {
3995       png_structp pp;
3996       png_infop pi;
3997
3998       pp = set_store_for_write(ps, &pi, name);
3999
4000       if (pp == NULL)
4001          Throw ps;
4002
4003       png_set_IHDR(pp, pi, transform_width(pp, colour_type, bit_depth),
4004          transform_height(pp, colour_type, bit_depth), bit_depth, colour_type,
4005          interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
4006
4007       if (colour_type == 3) /* palette */
4008          init_standard_palette(ps, pp, pi, 1U << bit_depth, 0/*do tRNS*/);
4009
4010       /* Time for a few errors; these are in various optional chunks, the
4011        * standard tests test the standard chunks pretty well.
4012        */
4013 #     define exception__prev exception_prev_1
4014 #     define exception__env exception_env_1
4015       Try
4016       {
4017          /* Expect this to throw: */
4018          ps->expect_error = !error_test[test].warning;
4019          ps->expect_warning = error_test[test].warning;
4020          ps->saw_warning = 0;
4021          error_test[test].fn(pp, pi);
4022
4023          /* Normally the error is only detected here: */
4024          png_write_info(pp, pi);
4025
4026          /* And handle the case where it was only a warning: */
4027          if (ps->expect_warning && ps->saw_warning)
4028             Throw ps;
4029
4030          /* If we get here there is a problem, we have success - no error or
4031           * no warning - when we shouldn't have success.  Log an error.
4032           */
4033          store_log(ps, pp, error_test[test].msg, 1 /*error*/);
4034       }
4035
4036       Catch (fault)
4037          ps = fault; /* expected exit, make sure ps is not clobbered */
4038 #undef exception__prev
4039 #undef exception__env
4040
4041       /* And clear these flags */
4042       ps->expect_error = 0;
4043       ps->expect_warning = 0;
4044
4045       /* Now write the whole image, just to make sure that the detected, or
4046        * undetected, errro has not created problems inside libpng.
4047        */
4048       if (png_get_rowbytes(pp, pi) !=
4049           transform_rowsize(pp, colour_type, bit_depth))
4050          png_error(pp, "row size incorrect");
4051
4052       else
4053       {
4054          png_uint_32 h = transform_height(pp, colour_type, bit_depth);
4055          int npasses = png_set_interlace_handling(pp);
4056          int pass;
4057
4058          if (npasses != npasses_from_interlace_type(pp, interlace_type))
4059             png_error(pp, "write: png_set_interlace_handling failed");
4060
4061          for (pass=0; pass<npasses; ++pass)
4062          {
4063             png_uint_32 y;
4064
4065             for (y=0; y<h; ++y)
4066             {
4067                png_byte buffer[TRANSFORM_ROWMAX];
4068
4069                transform_row(pp, buffer, colour_type, bit_depth, y);
4070                png_write_row(pp, buffer);
4071             }
4072          }
4073       }
4074
4075       png_write_end(pp, pi);
4076
4077       /* The following deletes the file that was just written. */
4078       store_write_reset(ps);
4079    }
4080
4081    Catch(fault)
4082    {
4083       store_write_reset(fault);
4084    }
4085 }
4086
4087 static int
4088 make_errors(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
4089     int bdlo, int PNG_CONST bdhi)
4090 {
4091    for (; bdlo <= bdhi; ++bdlo)
4092    {
4093       int interlace_type;
4094
4095       for (interlace_type = PNG_INTERLACE_NONE;
4096            interlace_type < INTERLACE_LAST; ++interlace_type)
4097       {
4098          unsigned int test;
4099          char name[FILE_NAME_SIZE];
4100
4101          standard_name(name, sizeof name, 0, colour_type, 1<<bdlo, 0,
4102             interlace_type, 0, 0, 0);
4103
4104          for (test=0; test<(sizeof error_test)/(sizeof error_test[0]); ++test)
4105          {
4106             make_error(&pm->this, colour_type, DEPTH(bdlo), interlace_type,
4107                test, name);
4108
4109             if (fail(pm))
4110                return 0;
4111          }
4112       }
4113    }
4114
4115    return 1; /* keep going */
4116 }
4117 #endif /* PNG_WARNINGS_SUPPORTED */
4118
4119 static void
4120 perform_error_test(png_modifier *pm)
4121 {
4122 #ifdef PNG_WARNINGS_SUPPORTED /* else there are no cases that work! */
4123    /* Need to do this here because we just write in this test. */
4124    safecat(pm->this.test, sizeof pm->this.test, 0, "error test");
4125
4126    if (!make_errors(pm, 0, 0, WRITE_BDHI))
4127       return;
4128
4129    if (!make_errors(pm, 2, 3, WRITE_BDHI))
4130       return;
4131
4132    if (!make_errors(pm, 3, 0, 3))
4133       return;
4134
4135    if (!make_errors(pm, 4, 3, WRITE_BDHI))
4136       return;
4137
4138    if (!make_errors(pm, 6, 3, WRITE_BDHI))
4139       return;
4140 #else
4141    UNUSED(pm)
4142 #endif
4143 }
4144
4145 /* This is just to validate the internal PNG formatting code - if this fails
4146  * then the warning messages the library outputs will probably be garbage.
4147  */
4148 static void
4149 perform_formatting_test(png_store *volatile ps)
4150 {
4151 #ifdef PNG_TIME_RFC1123_SUPPORTED
4152    /* The handle into the formatting code is the RFC1123 support; this test does
4153     * nothing if that is compiled out.
4154     */
4155    context(ps, fault);
4156
4157    Try
4158    {
4159       png_const_charp correct = "29 Aug 2079 13:53:60 +0000";
4160       png_const_charp result;
4161 #     if PNG_LIBPNG_VER >= 10600
4162          char timestring[29];
4163 #     endif
4164       png_structp pp;
4165       png_time pt;
4166
4167       pp = set_store_for_write(ps, NULL, "libpng formatting test");
4168
4169       if (pp == NULL)
4170          Throw ps;
4171
4172
4173       /* Arbitrary settings: */
4174       pt.year = 2079;
4175       pt.month = 8;
4176       pt.day = 29;
4177       pt.hour = 13;
4178       pt.minute = 53;
4179       pt.second = 60; /* a leap second */
4180
4181 #     if PNG_LIBPNG_VER < 10600
4182          result = png_convert_to_rfc1123(pp, &pt);
4183 #     else
4184          if (png_convert_to_rfc1123_buffer(timestring, &pt))
4185             result = timestring;
4186
4187          else
4188             result = NULL;
4189 #     endif
4190
4191       if (result == NULL)
4192          png_error(pp, "png_convert_to_rfc1123 failed");
4193
4194       if (strcmp(result, correct) != 0)
4195       {
4196          size_t pos = 0;
4197          char msg[128];
4198
4199          pos = safecat(msg, sizeof msg, pos, "png_convert_to_rfc1123(");
4200          pos = safecat(msg, sizeof msg, pos, correct);
4201          pos = safecat(msg, sizeof msg, pos, ") returned: '");
4202          pos = safecat(msg, sizeof msg, pos, result);
4203          pos = safecat(msg, sizeof msg, pos, "'");
4204
4205          png_error(pp, msg);
4206       }
4207
4208       store_write_reset(ps);
4209    }
4210
4211    Catch(fault)
4212    {
4213       store_write_reset(fault);
4214    }
4215 #else
4216    UNUSED(ps)
4217 #endif
4218 }
4219
4220 #ifdef PNG_READ_SUPPORTED
4221 /* Because we want to use the same code in both the progressive reader and the
4222  * sequential reader it is necessary to deal with the fact that the progressive
4223  * reader callbacks only have one parameter (png_get_progressive_ptr()), so this
4224  * must contain all the test parameters and all the local variables directly
4225  * accessible to the sequential reader implementation.
4226  *
4227  * The technique adopted is to reinvent part of what Dijkstra termed a
4228  * 'display'; an array of pointers to the stack frames of enclosing functions so
4229  * that a nested function definition can access the local (C auto) variables of
4230  * the functions that contain its definition.  In fact C provides the first
4231  * pointer (the local variables - the stack frame pointer) and the last (the
4232  * global variables - the BCPL global vector typically implemented as global
4233  * addresses), this code requires one more pointer to make the display - the
4234  * local variables (and function call parameters) of the function that actually
4235  * invokes either the progressive or sequential reader.
4236  *
4237  * Perhaps confusingly this technique is confounded with classes - the
4238  * 'standard_display' defined here is sub-classed as the 'gamma_display' below.
4239  * A gamma_display is a standard_display, taking advantage of the ANSI-C
4240  * requirement that the pointer to the first member of a structure must be the
4241  * same as the pointer to the structure.  This allows us to reuse standard_
4242  * functions in the gamma test code; something that could not be done with
4243  * nested functions!
4244  */
4245 typedef struct standard_display
4246 {
4247    png_store*  ps;             /* Test parameters (passed to the function) */
4248    png_byte    colour_type;
4249    png_byte    bit_depth;
4250    png_byte    red_sBIT;       /* Input data sBIT values. */
4251    png_byte    green_sBIT;
4252    png_byte    blue_sBIT;
4253    png_byte    alpha_sBIT;
4254    int         interlace_type;
4255    png_uint_32 id;             /* Calculated file ID */
4256    png_uint_32 w;              /* Width of image */
4257    png_uint_32 h;              /* Height of image */
4258    int         npasses;        /* Number of interlaced passes */
4259    png_uint_32 pixel_size;     /* Width of one pixel in bits */
4260    png_uint_32 bit_width;      /* Width of output row in bits */
4261    size_t      cbRow;          /* Bytes in a row of the output image */
4262    int         do_interlace;   /* Do interlacing internally */
4263    int         is_transparent; /* Transparency information was present. */
4264    int         speed;          /* Doing a speed test */
4265    int         use_update_info;/* Call update_info, not start_image */
4266    struct
4267    {
4268       png_uint_16 red;
4269       png_uint_16 green;
4270       png_uint_16 blue;
4271    }           transparent;    /* The transparent color, if set. */
4272    int         npalette;       /* Number of entries in the palette. */
4273    store_palette
4274                palette;
4275 } standard_display;
4276
4277 static void
4278 standard_display_init(standard_display *dp, png_store* ps, png_uint_32 id,
4279    int do_interlace, int use_update_info)
4280 {
4281    memset(dp, 0, sizeof *dp);
4282
4283    dp->ps = ps;
4284    dp->colour_type = COL_FROM_ID(id);
4285    dp->bit_depth = DEPTH_FROM_ID(id);
4286    if (dp->bit_depth < 1 || dp->bit_depth > 16)
4287       internal_error(ps, "internal: bad bit depth");
4288    if (dp->colour_type == 3)
4289       dp->red_sBIT = dp->blue_sBIT = dp->green_sBIT = dp->alpha_sBIT = 8;
4290    else
4291       dp->red_sBIT = dp->blue_sBIT = dp->green_sBIT = dp->alpha_sBIT =
4292          dp->bit_depth;
4293    dp->interlace_type = INTERLACE_FROM_ID(id);
4294    check_interlace_type(dp->interlace_type);
4295    dp->id = id;
4296    /* All the rest are filled in after the read_info: */
4297    dp->w = 0;
4298    dp->h = 0;
4299    dp->npasses = 0;
4300    dp->pixel_size = 0;
4301    dp->bit_width = 0;
4302    dp->cbRow = 0;
4303    dp->do_interlace = do_interlace;
4304    dp->is_transparent = 0;
4305    dp->speed = ps->speed;
4306    dp->use_update_info = use_update_info;
4307    dp->npalette = 0;
4308    /* Preset the transparent color to black: */
4309    memset(&dp->transparent, 0, sizeof dp->transparent);
4310    /* Preset the palette to full intensity/opaque througout: */
4311    memset(dp->palette, 0xff, sizeof dp->palette);
4312 }
4313
4314 /* Initialize the palette fields - this must be done later because the palette
4315  * comes from the particular png_store_file that is selected.
4316  */
4317 static void
4318 standard_palette_init(standard_display *dp)
4319 {
4320    store_palette_entry *palette = store_current_palette(dp->ps, &dp->npalette);
4321
4322    /* The remaining entries remain white/opaque. */
4323    if (dp->npalette > 0)
4324    {
4325       int i = dp->npalette;
4326       memcpy(dp->palette, palette, i * sizeof *palette);
4327
4328       /* Check for a non-opaque palette entry: */
4329       while (--i >= 0)
4330          if (palette[i].alpha < 255)
4331             break;
4332
4333 #     ifdef __GNUC__
4334          /* GCC can't handle the more obviously optimizable version. */
4335          if (i >= 0)
4336             dp->is_transparent = 1;
4337          else
4338             dp->is_transparent = 0;
4339 #     else
4340          dp->is_transparent = (i >= 0);
4341 #     endif
4342    }
4343 }
4344
4345 /* Utility to read the palette from the PNG file and convert it into
4346  * store_palette format.  This returns 1 if there is any transparency in the
4347  * palette (it does not check for a transparent colour in the non-palette case.)
4348  */
4349 static int
4350 read_palette(store_palette palette, int *npalette, png_const_structp pp,
4351    png_infop pi)
4352 {
4353    png_colorp pal;
4354    png_bytep trans_alpha;
4355    int num;
4356
4357    pal = 0;
4358    *npalette = -1;
4359
4360    if (png_get_PLTE(pp, pi, &pal, npalette) & PNG_INFO_PLTE)
4361    {
4362       int i = *npalette;
4363
4364       if (i <= 0 || i > 256)
4365          png_error(pp, "validate: invalid PLTE count");
4366
4367       while (--i >= 0)
4368       {
4369          palette[i].red = pal[i].red;
4370          palette[i].green = pal[i].green;
4371          palette[i].blue = pal[i].blue;
4372       }
4373
4374       /* Mark the remainder of the entries with a flag value (other than
4375        * white/opaque which is the flag value stored above.)
4376        */
4377       memset(palette + *npalette, 126, (256-*npalette) * sizeof *palette);
4378    }
4379
4380    else /* !png_get_PLTE */
4381    {
4382       if (*npalette != (-1))
4383          png_error(pp, "validate: invalid PLTE result");
4384       /* But there is no palette, so record this: */
4385       *npalette = 0;
4386       memset(palette, 113, sizeof (store_palette));
4387    }
4388
4389    trans_alpha = 0;
4390    num = 2; /* force error below */
4391    if ((png_get_tRNS(pp, pi, &trans_alpha, &num, 0) & PNG_INFO_tRNS) != 0 &&
4392       (trans_alpha != NULL || num != 1/*returns 1 for a transparent color*/) &&
4393       /* Oops, if a palette tRNS gets expanded png_read_update_info (at least so
4394        * far as 1.5.4) does not remove the trans_alpha pointer, only num_trans,
4395        * so in the above call we get a success, we get a pointer (who knows what
4396        * to) and we get num_trans == 0:
4397        */
4398       !(trans_alpha != NULL && num == 0)) /* TODO: fix this in libpng. */
4399    {
4400       int i;
4401
4402       /* Any of these are crash-worthy - given the implementation of
4403        * png_get_tRNS up to 1.5 an app won't crash if it just checks the
4404        * result above and fails to check that the variables it passed have
4405        * actually been filled in!  Note that if the app were to pass the
4406        * last, png_color_16p, variable too it couldn't rely on this.
4407        */
4408       if (trans_alpha == NULL || num <= 0 || num > 256 || num > *npalette)
4409          png_error(pp, "validate: unexpected png_get_tRNS (palette) result");
4410
4411       for (i=0; i<num; ++i)
4412          palette[i].alpha = trans_alpha[i];
4413
4414       for (num=*npalette; i<num; ++i)
4415          palette[i].alpha = 255;
4416
4417       for (; i<256; ++i)
4418          palette[i].alpha = 33; /* flag value */
4419
4420       return 1; /* transparency */
4421    }
4422
4423    else
4424    {
4425       /* No palette transparency - just set the alpha channel to opaque. */
4426       int i;
4427
4428       for (i=0, num=*npalette; i<num; ++i)
4429          palette[i].alpha = 255;
4430
4431       for (; i<256; ++i)
4432          palette[i].alpha = 55; /* flag value */
4433
4434       return 0; /* no transparency */
4435    }
4436 }
4437
4438 /* Utility to validate the palette if it should not have changed (the
4439  * non-transform case).
4440  */
4441 static void
4442 standard_palette_validate(standard_display *dp, png_const_structp pp,
4443    png_infop pi)
4444 {
4445    int npalette;
4446    store_palette palette;
4447
4448    if (read_palette(palette, &npalette, pp, pi) != dp->is_transparent)
4449       png_error(pp, "validate: palette transparency changed");
4450
4451    if (npalette != dp->npalette)
4452    {
4453       size_t pos = 0;
4454       char msg[64];
4455
4456       pos = safecat(msg, sizeof msg, pos, "validate: palette size changed: ");
4457       pos = safecatn(msg, sizeof msg, pos, dp->npalette);
4458       pos = safecat(msg, sizeof msg, pos, " -> ");
4459       pos = safecatn(msg, sizeof msg, pos, npalette);
4460       png_error(pp, msg);
4461    }
4462
4463    {
4464       int i = npalette; /* npalette is aliased */
4465
4466       while (--i >= 0)
4467          if (palette[i].red != dp->palette[i].red ||
4468             palette[i].green != dp->palette[i].green ||
4469             palette[i].blue != dp->palette[i].blue ||
4470             palette[i].alpha != dp->palette[i].alpha)
4471             png_error(pp, "validate: PLTE or tRNS chunk changed");
4472    }
4473 }
4474
4475 /* By passing a 'standard_display' the progressive callbacks can be used
4476  * directly by the sequential code, the functions suffixed "_imp" are the
4477  * implementations, the functions without the suffix are the callbacks.
4478  *
4479  * The code for the info callback is split into two because this callback calls
4480  * png_read_update_info or png_start_read_image and what gets called depends on
4481  * whether the info needs updating (we want to test both calls in pngvalid.)
4482  */
4483 static void
4484 standard_info_part1(standard_display *dp, png_structp pp, png_infop pi)
4485 {
4486    if (png_get_bit_depth(pp, pi) != dp->bit_depth)
4487       png_error(pp, "validate: bit depth changed");
4488
4489    if (png_get_color_type(pp, pi) != dp->colour_type)
4490       png_error(pp, "validate: color type changed");
4491
4492    if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE)
4493       png_error(pp, "validate: filter type changed");
4494
4495    if (png_get_interlace_type(pp, pi) != dp->interlace_type)
4496       png_error(pp, "validate: interlacing changed");
4497
4498    if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE)
4499       png_error(pp, "validate: compression type changed");
4500
4501    dp->w = png_get_image_width(pp, pi);
4502
4503    if (dp->w != standard_width(pp, dp->id))
4504       png_error(pp, "validate: image width changed");
4505
4506    dp->h = png_get_image_height(pp, pi);
4507
4508    if (dp->h != standard_height(pp, dp->id))
4509       png_error(pp, "validate: image height changed");
4510
4511    /* Record (but don't check at present) the input sBIT according to the colour
4512     * type information.
4513     */
4514    {
4515       png_color_8p sBIT = 0;
4516
4517       if (png_get_sBIT(pp, pi, &sBIT) & PNG_INFO_sBIT)
4518       {
4519          int sBIT_invalid = 0;
4520
4521          if (sBIT == 0)
4522             png_error(pp, "validate: unexpected png_get_sBIT result");
4523
4524          if (dp->colour_type & PNG_COLOR_MASK_COLOR)
4525          {
4526             if (sBIT->red == 0 || sBIT->red > dp->bit_depth)
4527                sBIT_invalid = 1;
4528             else
4529                dp->red_sBIT = sBIT->red;
4530
4531             if (sBIT->green == 0 || sBIT->green > dp->bit_depth)
4532                sBIT_invalid = 1;
4533             else
4534                dp->green_sBIT = sBIT->green;
4535
4536             if (sBIT->blue == 0 || sBIT->blue > dp->bit_depth)
4537                sBIT_invalid = 1;
4538             else
4539                dp->blue_sBIT = sBIT->blue;
4540          }
4541
4542          else /* !COLOR */
4543          {
4544             if (sBIT->gray == 0 || sBIT->gray > dp->bit_depth)
4545                sBIT_invalid = 1;
4546             else
4547                dp->blue_sBIT = dp->green_sBIT = dp->red_sBIT = sBIT->gray;
4548          }
4549
4550          /* All 8 bits in tRNS for a palette image are significant - see the
4551           * spec.
4552           */
4553          if (dp->colour_type & PNG_COLOR_MASK_ALPHA)
4554          {
4555             if (sBIT->alpha == 0 || sBIT->alpha > dp->bit_depth)
4556                sBIT_invalid = 1;
4557             else
4558                dp->alpha_sBIT = sBIT->alpha;
4559          }
4560
4561          if (sBIT_invalid)
4562             png_error(pp, "validate: sBIT value out of range");
4563       }
4564    }
4565
4566    /* Important: this is validating the value *before* any transforms have been
4567     * put in place.  It doesn't matter for the standard tests, where there are
4568     * no transforms, but it does for other tests where rowbytes may change after
4569     * png_read_update_info.
4570     */
4571    if (png_get_rowbytes(pp, pi) != standard_rowsize(pp, dp->id))
4572       png_error(pp, "validate: row size changed");
4573
4574    /* Validate the colour type 3 palette (this can be present on other color
4575     * types.)
4576     */
4577    standard_palette_validate(dp, pp, pi);
4578
4579    /* In any case always check for a tranparent color (notice that the
4580     * colour type 3 case must not give a successful return on the get_tRNS call
4581     * with these arguments!)
4582     */
4583    {
4584       png_color_16p trans_color = 0;
4585
4586       if (png_get_tRNS(pp, pi, 0, 0, &trans_color) & PNG_INFO_tRNS)
4587       {
4588          if (trans_color == 0)
4589             png_error(pp, "validate: unexpected png_get_tRNS (color) result");
4590
4591          switch (dp->colour_type)
4592          {
4593          case 0:
4594             dp->transparent.red = dp->transparent.green = dp->transparent.blue =
4595                trans_color->gray;
4596             dp->is_transparent = 1;
4597             break;
4598
4599          case 2:
4600             dp->transparent.red = trans_color->red;
4601             dp->transparent.green = trans_color->green;
4602             dp->transparent.blue = trans_color->blue;
4603             dp->is_transparent = 1;
4604             break;
4605
4606          case 3:
4607             /* Not expected because it should result in the array case
4608              * above.
4609              */
4610             png_error(pp, "validate: unexpected png_get_tRNS result");
4611             break;
4612
4613          default:
4614             png_error(pp, "validate: invalid tRNS chunk with alpha image");
4615          }
4616       }
4617    }
4618
4619    /* Read the number of passes - expected to match the value used when
4620     * creating the image (interlaced or not).  This has the side effect of
4621     * turning on interlace handling (if do_interlace is not set.)
4622     */
4623    dp->npasses = npasses_from_interlace_type(pp, dp->interlace_type);
4624    if (!dp->do_interlace && dp->npasses != png_set_interlace_handling(pp))
4625       png_error(pp, "validate: file changed interlace type");
4626
4627    /* Caller calls png_read_update_info or png_start_read_image now, then calls
4628     * part2.
4629     */
4630 }
4631
4632 /* This must be called *after* the png_read_update_info call to get the correct
4633  * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed
4634  * image.
4635  */
4636 static void
4637 standard_info_part2(standard_display *dp, png_const_structp pp,
4638     png_const_infop pi, int nImages)
4639 {
4640    /* Record cbRow now that it can be found. */
4641    dp->pixel_size = bit_size(pp, png_get_color_type(pp, pi),
4642       png_get_bit_depth(pp, pi));
4643    dp->bit_width = png_get_image_width(pp, pi) * dp->pixel_size;
4644    dp->cbRow = png_get_rowbytes(pp, pi);
4645
4646    /* Validate the rowbytes here again. */
4647    if (dp->cbRow != (dp->bit_width+7)/8)
4648       png_error(pp, "bad png_get_rowbytes calculation");
4649
4650    /* Then ensure there is enough space for the output image(s). */
4651    store_ensure_image(dp->ps, pp, nImages, dp->cbRow, dp->h);
4652 }
4653
4654 static void
4655 standard_info_imp(standard_display *dp, png_structp pp, png_infop pi,
4656     int nImages)
4657 {
4658    /* Note that the validation routine has the side effect of turning on
4659     * interlace handling in the subsequent code.
4660     */
4661    standard_info_part1(dp, pp, pi);
4662
4663    /* And the info callback has to call this (or png_read_update_info - see
4664     * below in the png_modifier code for that variant.
4665     */
4666    if (dp->use_update_info)
4667    {
4668       /* For debugging the effect of multiple calls: */
4669       int i = dp->use_update_info;
4670       while (i-- > 0)
4671          png_read_update_info(pp, pi);
4672    }
4673
4674    else
4675       png_start_read_image(pp);
4676
4677    /* Validate the height, width and rowbytes plus ensure that sufficient buffer
4678     * exists for decoding the image.
4679     */
4680    standard_info_part2(dp, pp, pi, nImages);
4681 }
4682
4683 static void PNGCBAPI
4684 standard_info(png_structp pp, png_infop pi)
4685 {
4686    standard_display *dp = voidcast(standard_display*,
4687       png_get_progressive_ptr(pp));
4688
4689    /* Call with nImages==1 because the progressive reader can only produce one
4690     * image.
4691     */
4692    standard_info_imp(dp, pp, pi, 1 /*only one image*/);
4693 }
4694
4695 static void PNGCBAPI
4696 progressive_row(png_structp ppIn, png_bytep new_row, png_uint_32 y, int pass)
4697 {
4698    png_const_structp pp = ppIn;
4699    PNG_CONST standard_display *dp = voidcast(standard_display*,
4700       png_get_progressive_ptr(pp));
4701
4702    /* When handling interlacing some rows will be absent in each pass, the
4703     * callback still gets called, but with a NULL pointer.  This is checked
4704     * in the 'else' clause below.  We need our own 'cbRow', but we can't call
4705     * png_get_rowbytes because we got no info structure.
4706     */
4707    if (new_row != NULL)
4708    {
4709       png_bytep row;
4710
4711       /* In the case where the reader doesn't do the interlace it gives
4712        * us the y in the sub-image:
4713        */
4714       if (dp->do_interlace && dp->interlace_type == PNG_INTERLACE_ADAM7)
4715       {
4716 #ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED
4717          /* Use this opportunity to validate the png 'current' APIs: */
4718          if (y != png_get_current_row_number(pp))
4719             png_error(pp, "png_get_current_row_number is broken");
4720
4721          if (pass != png_get_current_pass_number(pp))
4722             png_error(pp, "png_get_current_pass_number is broken");
4723 #endif
4724
4725          y = PNG_ROW_FROM_PASS_ROW(y, pass);
4726       }
4727
4728       /* Validate this just in case. */
4729       if (y >= dp->h)
4730          png_error(pp, "invalid y to progressive row callback");
4731
4732       row = store_image_row(dp->ps, pp, 0, y);
4733
4734 #ifdef PNG_READ_INTERLACING_SUPPORTED
4735       /* Combine the new row into the old: */
4736       if (dp->do_interlace)
4737       {
4738          if (dp->interlace_type == PNG_INTERLACE_ADAM7)
4739             deinterlace_row(row, new_row, dp->pixel_size, dp->w, pass);
4740          else
4741             row_copy(row, new_row, dp->pixel_size * dp->w);
4742       }
4743       else
4744          png_progressive_combine_row(pp, row, new_row);
4745 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4746    }
4747
4748 #ifdef PNG_READ_INTERLACING_SUPPORTED
4749    else if (dp->interlace_type == PNG_INTERLACE_ADAM7 &&
4750        PNG_ROW_IN_INTERLACE_PASS(y, pass) &&
4751        PNG_PASS_COLS(dp->w, pass) > 0)
4752       png_error(pp, "missing row in progressive de-interlacing");
4753 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4754 }
4755
4756 static void
4757 sequential_row(standard_display *dp, png_structp pp, png_infop pi,
4758     PNG_CONST int iImage, PNG_CONST int iDisplay)
4759 {
4760    PNG_CONST int         npasses = dp->npasses;
4761    PNG_CONST int         do_interlace = dp->do_interlace &&
4762       dp->interlace_type == PNG_INTERLACE_ADAM7;
4763    PNG_CONST png_uint_32 height = standard_height(pp, dp->id);
4764    PNG_CONST png_uint_32 width = standard_width(pp, dp->id);
4765    PNG_CONST png_store*  ps = dp->ps;
4766    int pass;
4767
4768    for (pass=0; pass<npasses; ++pass)
4769    {
4770       png_uint_32 y;
4771       png_uint_32 wPass = PNG_PASS_COLS(width, pass);
4772
4773       for (y=0; y<height; ++y)
4774       {
4775          if (do_interlace)
4776          {
4777             /* wPass may be zero or this row may not be in this pass.
4778              * png_read_row must not be called in either case.
4779              */
4780             if (wPass > 0 && PNG_ROW_IN_INTERLACE_PASS(y, pass))
4781             {
4782                /* Read the row into a pair of temporary buffers, then do the
4783                 * merge here into the output rows.
4784                 */
4785                png_byte row[STANDARD_ROWMAX], display[STANDARD_ROWMAX];
4786
4787                /* The following aids (to some extent) error detection - we can
4788                 * see where png_read_row wrote.  Use opposite values in row and
4789                 * display to make this easier.  Don't use 0xff (which is used in
4790                 * the image write code to fill unused bits) or 0 (which is a
4791                 * likely value to overwrite unused bits with).
4792                 */
4793                memset(row, 0xc5, sizeof row);
4794                memset(display, 0x5c, sizeof display);
4795
4796                png_read_row(pp, row, display);
4797
4798                if (iImage >= 0)
4799                   deinterlace_row(store_image_row(ps, pp, iImage, y), row,
4800                      dp->pixel_size, dp->w, pass);
4801
4802                if (iDisplay >= 0)
4803                   deinterlace_row(store_image_row(ps, pp, iDisplay, y), display,
4804                      dp->pixel_size, dp->w, pass);
4805             }
4806          }
4807          else
4808             png_read_row(pp,
4809                iImage >= 0 ? store_image_row(ps, pp, iImage, y) : NULL,
4810                iDisplay >= 0 ? store_image_row(ps, pp, iDisplay, y) : NULL);
4811       }
4812    }
4813
4814    /* And finish the read operation (only really necessary if the caller wants
4815     * to find additional data in png_info from chunks after the last IDAT.)
4816     */
4817    png_read_end(pp, pi);
4818 }
4819
4820 #ifdef PNG_TEXT_SUPPORTED
4821 static void
4822 standard_check_text(png_const_structp pp, png_const_textp tp,
4823    png_const_charp keyword, png_const_charp text)
4824 {
4825    char msg[1024];
4826    size_t pos = safecat(msg, sizeof msg, 0, "text: ");
4827    size_t ok;
4828
4829    pos = safecat(msg, sizeof msg, pos, keyword);
4830    pos = safecat(msg, sizeof msg, pos, ": ");
4831    ok = pos;
4832
4833    if (tp->compression != TEXT_COMPRESSION)
4834    {
4835       char buf[64];
4836
4837       sprintf(buf, "compression [%d->%d], ", TEXT_COMPRESSION,
4838          tp->compression);
4839       pos = safecat(msg, sizeof msg, pos, buf);
4840    }
4841
4842    if (tp->key == NULL || strcmp(tp->key, keyword) != 0)
4843    {
4844       pos = safecat(msg, sizeof msg, pos, "keyword \"");
4845       if (tp->key != NULL)
4846       {
4847          pos = safecat(msg, sizeof msg, pos, tp->key);
4848          pos = safecat(msg, sizeof msg, pos, "\", ");
4849       }
4850
4851       else
4852          pos = safecat(msg, sizeof msg, pos, "null, ");
4853    }
4854
4855    if (tp->text == NULL)
4856       pos = safecat(msg, sizeof msg, pos, "text lost, ");
4857
4858    else
4859    {
4860       if (tp->text_length != strlen(text))
4861       {
4862          char buf[64];
4863          sprintf(buf, "text length changed[%lu->%lu], ",
4864             (unsigned long)strlen(text), (unsigned long)tp->text_length);
4865          pos = safecat(msg, sizeof msg, pos, buf);
4866       }
4867
4868       if (strcmp(tp->text, text) != 0)
4869       {
4870          pos = safecat(msg, sizeof msg, pos, "text becomes \"");
4871          pos = safecat(msg, sizeof msg, pos, tp->text);
4872          pos = safecat(msg, sizeof msg, pos, "\" (was \"");
4873          pos = safecat(msg, sizeof msg, pos, text);
4874          pos = safecat(msg, sizeof msg, pos, "\"), ");
4875       }
4876    }
4877
4878    if (tp->itxt_length != 0)
4879       pos = safecat(msg, sizeof msg, pos, "iTXt length set, ");
4880
4881    if (tp->lang != NULL)
4882    {
4883       pos = safecat(msg, sizeof msg, pos, "iTXt language \"");
4884       pos = safecat(msg, sizeof msg, pos, tp->lang);
4885       pos = safecat(msg, sizeof msg, pos, "\", ");
4886    }
4887
4888    if (tp->lang_key != NULL)
4889    {
4890       pos = safecat(msg, sizeof msg, pos, "iTXt keyword \"");
4891       pos = safecat(msg, sizeof msg, pos, tp->lang_key);
4892       pos = safecat(msg, sizeof msg, pos, "\", ");
4893    }
4894
4895    if (pos > ok)
4896    {
4897       msg[pos-2] = '\0'; /* Remove the ", " at the end */
4898       png_error(pp, msg);
4899    }
4900 }
4901
4902 static void
4903 standard_text_validate(standard_display *dp, png_const_structp pp,
4904    png_infop pi, int check_end)
4905 {
4906    png_textp tp = NULL;
4907    png_uint_32 num_text = png_get_text(pp, pi, &tp, NULL);
4908
4909    if (num_text == 2 && tp != NULL)
4910    {
4911       standard_check_text(pp, tp, "image name", dp->ps->current->name);
4912
4913       /* This exists because prior to 1.5.18 the progressive reader left the
4914        * png_struct z_stream unreset at the end of the image, so subsequent
4915        * attempts to use it simply returns Z_STREAM_END.
4916        */
4917       if (check_end)
4918          standard_check_text(pp, tp+1, "end marker", "end");
4919    }
4920
4921    else
4922    {
4923       char msg[64];
4924
4925       sprintf(msg, "expected two text items, got %lu",
4926          (unsigned long)num_text);
4927       png_error(pp, msg);
4928    }
4929 }
4930 #else
4931 #  define standard_text_validate(dp,pp,pi,check_end) ((void)0)
4932 #endif
4933
4934 static void
4935 standard_row_validate(standard_display *dp, png_const_structp pp,
4936    int iImage, int iDisplay, png_uint_32 y)
4937 {
4938    int where;
4939    png_byte std[STANDARD_ROWMAX];
4940
4941    /* The row must be pre-initialized to the magic number here for the size
4942     * tests to pass:
4943     */
4944    memset(std, 178, sizeof std);
4945    standard_row(pp, std, dp->id, y);
4946
4947    /* At the end both the 'row' and 'display' arrays should end up identical.
4948     * In earlier passes 'row' will be partially filled in, with only the pixels
4949     * that have been read so far, but 'display' will have those pixels
4950     * replicated to fill the unread pixels while reading an interlaced image.
4951 #if PNG_LIBPNG_VER < 10506
4952     * The side effect inside the libpng sequential reader is that the 'row'
4953     * array retains the correct values for unwritten pixels within the row
4954     * bytes, while the 'display' array gets bits off the end of the image (in
4955     * the last byte) trashed.  Unfortunately in the progressive reader the
4956     * row bytes are always trashed, so we always do a pixel_cmp here even though
4957     * a memcmp of all cbRow bytes will succeed for the sequential reader.
4958 #endif
4959     */
4960    if (iImage >= 0 &&
4961       (where = pixel_cmp(std, store_image_row(dp->ps, pp, iImage, y),
4962             dp->bit_width)) != 0)
4963    {
4964       char msg[64];
4965       sprintf(msg, "PNG image row[%lu][%d] changed from %.2x to %.2x",
4966          (unsigned long)y, where-1, std[where-1],
4967          store_image_row(dp->ps, pp, iImage, y)[where-1]);
4968       png_error(pp, msg);
4969    }
4970
4971 #if PNG_LIBPNG_VER < 10506
4972    /* In this case use pixel_cmp because we need to compare a partial
4973     * byte at the end of the row if the row is not an exact multiple
4974     * of 8 bits wide.  (This is fixed in libpng-1.5.6 and pixel_cmp is
4975     * changed to match!)
4976     */
4977 #endif
4978    if (iDisplay >= 0 &&
4979       (where = pixel_cmp(std, store_image_row(dp->ps, pp, iDisplay, y),
4980          dp->bit_width)) != 0)
4981    {
4982       char msg[64];
4983       sprintf(msg, "display  row[%lu][%d] changed from %.2x to %.2x",
4984          (unsigned long)y, where-1, std[where-1],
4985          store_image_row(dp->ps, pp, iDisplay, y)[where-1]);
4986       png_error(pp, msg);
4987    }
4988 }
4989
4990 static void
4991 standard_image_validate(standard_display *dp, png_const_structp pp, int iImage,
4992     int iDisplay)
4993 {
4994    png_uint_32 y;
4995
4996    if (iImage >= 0)
4997       store_image_check(dp->ps, pp, iImage);
4998
4999    if (iDisplay >= 0)
5000       store_image_check(dp->ps, pp, iDisplay);
5001
5002    for (y=0; y<dp->h; ++y)
5003       standard_row_validate(dp, pp, iImage, iDisplay, y);
5004
5005    /* This avoids false positives if the validation code is never called! */
5006    dp->ps->validated = 1;
5007 }
5008
5009 static void PNGCBAPI
5010 standard_end(png_structp ppIn, png_infop pi)
5011 {
5012    png_const_structp pp = ppIn;
5013    standard_display *dp = voidcast(standard_display*,
5014       png_get_progressive_ptr(pp));
5015
5016    UNUSED(pi)
5017
5018    /* Validate the image - progressive reading only produces one variant for
5019     * interlaced images.
5020     */
5021    standard_text_validate(dp, pp, pi,
5022       PNG_LIBPNG_VER >= 10518/*check_end: see comments above*/);
5023    standard_image_validate(dp, pp, 0, -1);
5024 }
5025
5026 /* A single test run checking the standard image to ensure it is not damaged. */
5027 static void
5028 standard_test(png_store* PNG_CONST psIn, png_uint_32 PNG_CONST id,
5029    int do_interlace, int use_update_info)
5030 {
5031    standard_display d;
5032    context(psIn, fault);
5033
5034    /* Set up the display (stack frame) variables from the arguments to the
5035     * function and initialize the locals that are filled in later.
5036     */
5037    standard_display_init(&d, psIn, id, do_interlace, use_update_info);
5038
5039    /* Everything is protected by a Try/Catch.  The functions called also
5040     * typically have local Try/Catch blocks.
5041     */
5042    Try
5043    {
5044       png_structp pp;
5045       png_infop pi;
5046
5047       /* Get a png_struct for reading the image. This will throw an error if it
5048        * fails, so we don't need to check the result.
5049        */
5050       pp = set_store_for_read(d.ps, &pi, d.id,
5051          d.do_interlace ?  (d.ps->progressive ?
5052             "pngvalid progressive deinterlacer" :
5053             "pngvalid sequential deinterlacer") : (d.ps->progressive ?
5054                "progressive reader" : "sequential reader"));
5055
5056       /* Initialize the palette correctly from the png_store_file. */
5057       standard_palette_init(&d);
5058
5059       /* Introduce the correct read function. */
5060       if (d.ps->progressive)
5061       {
5062          png_set_progressive_read_fn(pp, &d, standard_info, progressive_row,
5063             standard_end);
5064
5065          /* Now feed data into the reader until we reach the end: */
5066          store_progressive_read(d.ps, pp, pi);
5067       }
5068       else
5069       {
5070          /* Note that this takes the store, not the display. */
5071          png_set_read_fn(pp, d.ps, store_read);
5072
5073          /* Check the header values: */
5074          png_read_info(pp, pi);
5075
5076          /* The code tests both versions of the images that the sequential
5077           * reader can produce.
5078           */
5079          standard_info_imp(&d, pp, pi, 2 /*images*/);
5080
5081          /* Need the total bytes in the image below; we can't get to this point
5082           * unless the PNG file values have been checked against the expected
5083           * values.
5084           */
5085          {
5086             sequential_row(&d, pp, pi, 0, 1);
5087
5088             /* After the last pass loop over the rows again to check that the
5089              * image is correct.
5090              */
5091             if (!d.speed)
5092             {
5093                standard_text_validate(&d, pp, pi, 1/*check_end*/);
5094                standard_image_validate(&d, pp, 0, 1);
5095             }
5096             else
5097                d.ps->validated = 1;
5098          }
5099       }
5100
5101       /* Check for validation. */
5102       if (!d.ps->validated)
5103          png_error(pp, "image read failed silently");
5104
5105       /* Successful completion. */
5106    }
5107
5108    Catch(fault)
5109       d.ps = fault; /* make sure this hasn't been clobbered. */
5110
5111    /* In either case clean up the store. */
5112    store_read_reset(d.ps);
5113 }
5114
5115 static int
5116 test_standard(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
5117     int bdlo, int PNG_CONST bdhi)
5118 {
5119    for (; bdlo <= bdhi; ++bdlo)
5120    {
5121       int interlace_type;
5122
5123       for (interlace_type = PNG_INTERLACE_NONE;
5124            interlace_type < INTERLACE_LAST; ++interlace_type)
5125       {
5126          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5127             interlace_type, 0, 0, 0), 0/*do_interlace*/, pm->use_update_info);
5128
5129          if (fail(pm))
5130             return 0;
5131       }
5132    }
5133
5134    return 1; /* keep going */
5135 }
5136
5137 static void
5138 perform_standard_test(png_modifier *pm)
5139 {
5140    /* Test each colour type over the valid range of bit depths (expressed as
5141     * log2(bit_depth) in turn, stop as soon as any error is detected.
5142     */
5143    if (!test_standard(pm, 0, 0, READ_BDHI))
5144       return;
5145
5146    if (!test_standard(pm, 2, 3, READ_BDHI))
5147       return;
5148
5149    if (!test_standard(pm, 3, 0, 3))
5150       return;
5151
5152    if (!test_standard(pm, 4, 3, READ_BDHI))
5153       return;
5154
5155    if (!test_standard(pm, 6, 3, READ_BDHI))
5156       return;
5157 }
5158
5159
5160 /********************************** SIZE TESTS ********************************/
5161 static int
5162 test_size(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
5163     int bdlo, int PNG_CONST bdhi)
5164 {
5165    /* Run the tests on each combination.
5166     *
5167     * NOTE: on my 32 bit x86 each of the following blocks takes
5168     * a total of 3.5 seconds if done across every combo of bit depth
5169     * width and height.  This is a waste of time in practice, hence the
5170     * hinc and winc stuff:
5171     */
5172    static PNG_CONST png_byte hinc[] = {1, 3, 11, 1, 5};
5173    static PNG_CONST png_byte winc[] = {1, 9, 5, 7, 1};
5174    for (; bdlo <= bdhi; ++bdlo)
5175    {
5176       png_uint_32 h, w;
5177
5178       for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
5179       {
5180          /* First test all the 'size' images against the sequential
5181           * reader using libpng to deinterlace (where required.)  This
5182           * validates the write side of libpng.  There are four possibilities
5183           * to validate.
5184           */
5185          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5186             PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
5187             pm->use_update_info);
5188
5189          if (fail(pm))
5190             return 0;
5191
5192          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5193             PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
5194             pm->use_update_info);
5195
5196          if (fail(pm))
5197             return 0;
5198
5199 #     ifdef PNG_WRITE_INTERLACING_SUPPORTED
5200          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5201             PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
5202             pm->use_update_info);
5203
5204          if (fail(pm))
5205             return 0;
5206
5207          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5208             PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
5209             pm->use_update_info);
5210
5211          if (fail(pm))
5212             return 0;
5213 #     endif
5214
5215          /* Now validate the interlaced read side - do_interlace true,
5216           * in the progressive case this does actually make a difference
5217           * to the code used in the non-interlaced case too.
5218           */
5219          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5220             PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
5221             pm->use_update_info);
5222
5223          if (fail(pm))
5224             return 0;
5225
5226 #     ifdef PNG_WRITE_INTERLACING_SUPPORTED
5227          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5228             PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
5229             pm->use_update_info);
5230
5231          if (fail(pm))
5232             return 0;
5233 #     endif
5234       }
5235    }
5236
5237    return 1; /* keep going */
5238 }
5239
5240 static void
5241 perform_size_test(png_modifier *pm)
5242 {
5243    /* Test each colour type over the valid range of bit depths (expressed as
5244     * log2(bit_depth) in turn, stop as soon as any error is detected.
5245     */
5246    if (!test_size(pm, 0, 0, READ_BDHI))
5247       return;
5248
5249    if (!test_size(pm, 2, 3, READ_BDHI))
5250       return;
5251
5252    /* For the moment don't do the palette test - it's a waste of time when
5253     * compared to the grayscale test.
5254     */
5255 #if 0
5256    if (!test_size(pm, 3, 0, 3))
5257       return;
5258 #endif
5259
5260    if (!test_size(pm, 4, 3, READ_BDHI))
5261       return;
5262
5263    if (!test_size(pm, 6, 3, READ_BDHI))
5264       return;
5265 }
5266
5267
5268 /******************************* TRANSFORM TESTS ******************************/
5269 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
5270 /* A set of tests to validate libpng image transforms.  The possibilities here
5271  * are legion because the transforms can be combined in a combinatorial
5272  * fashion.  To deal with this some measure of restraint is required, otherwise
5273  * the tests would take forever.
5274  */
5275 typedef struct image_pixel
5276 {
5277    /* A local (pngvalid) representation of a PNG pixel, in all its
5278     * various forms.
5279     */
5280    unsigned int red, green, blue, alpha; /* For non-palette images. */
5281    unsigned int palette_index;           /* For a palette image. */
5282    png_byte colour_type;                 /* As in the spec. */
5283    png_byte bit_depth;                   /* Defines bit size in row */
5284    png_byte sample_depth;                /* Scale of samples */
5285    int      have_tRNS;                   /* tRNS chunk may need processing */
5286
5287    /* For checking the code calculates double precision floating point values
5288     * along with an error value, accumulated from the transforms.  Because an
5289     * sBIT setting allows larger error bounds (indeed, by the spec, apparently
5290     * up to just less than +/-1 in the scaled value) the *lowest* sBIT for each
5291     * channel is stored.  This sBIT value is folded in to the stored error value
5292     * at the end of the application of the transforms to the pixel.
5293     */
5294    double   redf, greenf, bluef, alphaf;
5295    double   rede, greene, bluee, alphae;
5296    png_byte red_sBIT, green_sBIT, blue_sBIT, alpha_sBIT;
5297 } image_pixel;
5298
5299 /* Shared utility function, see below. */
5300 static void
5301 image_pixel_setf(image_pixel *this, unsigned int max)
5302 {
5303    this->redf = this->red / (double)max;
5304    this->greenf = this->green / (double)max;
5305    this->bluef = this->blue / (double)max;
5306    this->alphaf = this->alpha / (double)max;
5307
5308    if (this->red < max)
5309       this->rede = this->redf * DBL_EPSILON;
5310    else
5311       this->rede = 0;
5312    if (this->green < max)
5313       this->greene = this->greenf * DBL_EPSILON;
5314    else
5315       this->greene = 0;
5316    if (this->blue < max)
5317       this->bluee = this->bluef * DBL_EPSILON;
5318    else
5319       this->bluee = 0;
5320    if (this->alpha < max)
5321       this->alphae = this->alphaf * DBL_EPSILON;
5322    else
5323       this->alphae = 0;
5324 }
5325
5326 /* Initialize the structure for the next pixel - call this before doing any
5327  * transforms and call it for each pixel since all the fields may need to be
5328  * reset.
5329  */
5330 static void
5331 image_pixel_init(image_pixel *this, png_const_bytep row, png_byte colour_type,
5332     png_byte bit_depth, png_uint_32 x, store_palette palette)
5333 {
5334    PNG_CONST png_byte sample_depth = (png_byte)(colour_type ==
5335       PNG_COLOR_TYPE_PALETTE ? 8 : bit_depth);
5336    PNG_CONST unsigned int max = (1U<<sample_depth)-1;
5337
5338    /* Initially just set everything to the same number and the alpha to opaque.
5339     * Note that this currently assumes a simple palette where entry x has colour
5340     * rgb(x,x,x)!
5341     */
5342    this->palette_index = this->red = this->green = this->blue =
5343       sample(row, colour_type, bit_depth, x, 0);
5344    this->alpha = max;
5345    this->red_sBIT = this->green_sBIT = this->blue_sBIT = this->alpha_sBIT =
5346       sample_depth;
5347
5348    /* Then override as appropriate: */
5349    if (colour_type == 3) /* palette */
5350    {
5351       /* This permits the caller to default to the sample value. */
5352       if (palette != 0)
5353       {
5354          PNG_CONST unsigned int i = this->palette_index;
5355
5356          this->red = palette[i].red;
5357          this->green = palette[i].green;
5358          this->blue = palette[i].blue;
5359          this->alpha = palette[i].alpha;
5360       }
5361    }
5362
5363    else /* not palette */
5364    {
5365       unsigned int i = 0;
5366
5367       if (colour_type & 2)
5368       {
5369          this->green = sample(row, colour_type, bit_depth, x, 1);
5370          this->blue = sample(row, colour_type, bit_depth, x, 2);
5371          i = 2;
5372       }
5373       if (colour_type & 4)
5374          this->alpha = sample(row, colour_type, bit_depth, x, ++i);
5375    }
5376
5377    /* Calculate the scaled values, these are simply the values divided by
5378     * 'max' and the error is initialized to the double precision epsilon value
5379     * from the header file.
5380     */
5381    image_pixel_setf(this, max);
5382
5383    /* Store the input information for use in the transforms - these will
5384     * modify the information.
5385     */
5386    this->colour_type = colour_type;
5387    this->bit_depth = bit_depth;
5388    this->sample_depth = sample_depth;
5389    this->have_tRNS = 0;
5390 }
5391
5392 /* Convert a palette image to an rgb image.  This necessarily converts the tRNS
5393  * chunk at the same time, because the tRNS will be in palette form.  The way
5394  * palette validation works means that the original palette is never updated,
5395  * instead the image_pixel value from the row contains the RGB of the
5396  * corresponding palette entry and *this* is updated.  Consequently this routine
5397  * only needs to change the colour type information.
5398  */
5399 static void
5400 image_pixel_convert_PLTE(image_pixel *this)
5401 {
5402    if (this->colour_type == PNG_COLOR_TYPE_PALETTE)
5403    {
5404       if (this->have_tRNS)
5405       {
5406          this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
5407          this->have_tRNS = 0;
5408       }
5409       else
5410          this->colour_type = PNG_COLOR_TYPE_RGB;
5411
5412       /* The bit depth of the row changes at this point too (notice that this is
5413        * the row format, not the sample depth, which is separate.)
5414        */
5415       this->bit_depth = 8;
5416    }
5417 }
5418
5419 /* Add an alpha channel; this will import the tRNS information because tRNS is
5420  * not valid in an alpha image.  The bit depth will invariably be set to at
5421  * least 8.  Palette images will be converted to alpha (using the above API).
5422  */
5423 static void
5424 image_pixel_add_alpha(image_pixel *this, PNG_CONST standard_display *display)
5425 {
5426    if (this->colour_type == PNG_COLOR_TYPE_PALETTE)
5427       image_pixel_convert_PLTE(this);
5428
5429    if ((this->colour_type & PNG_COLOR_MASK_ALPHA) == 0)
5430    {
5431       if (this->colour_type == PNG_COLOR_TYPE_GRAY)
5432       {
5433          if (this->bit_depth < 8)
5434             this->bit_depth = 8;
5435
5436          if (this->have_tRNS)
5437          {
5438             this->have_tRNS = 0;
5439
5440             /* Check the input, original, channel value here against the
5441              * original tRNS gray chunk valie.
5442              */
5443             if (this->red == display->transparent.red)
5444                this->alphaf = 0;
5445             else
5446                this->alphaf = 1;
5447          }
5448          else
5449             this->alphaf = 1;
5450
5451          this->colour_type = PNG_COLOR_TYPE_GRAY_ALPHA;
5452       }
5453
5454       else if (this->colour_type == PNG_COLOR_TYPE_RGB)
5455       {
5456          if (this->have_tRNS)
5457          {
5458             this->have_tRNS = 0;
5459
5460             /* Again, check the exact input values, not the current transformed
5461              * value!
5462              */
5463             if (this->red == display->transparent.red &&
5464                this->green == display->transparent.green &&
5465                this->blue == display->transparent.blue)
5466                this->alphaf = 0;
5467             else
5468                this->alphaf = 1;
5469
5470             this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
5471          }
5472       }
5473
5474       /* The error in the alpha is zero and the sBIT value comes from the
5475        * original sBIT data (actually it will always be the original bit depth).
5476        */
5477       this->alphae = 0;
5478       this->alpha_sBIT = display->alpha_sBIT;
5479    }
5480 }
5481
5482 struct transform_display;
5483 typedef struct image_transform
5484 {
5485    /* The name of this transform: a string. */
5486    PNG_CONST char *name;
5487
5488    /* Each transform can be disabled from the command line: */
5489    int enable;
5490
5491    /* The global list of transforms; read only. */
5492    struct image_transform *PNG_CONST list;
5493
5494    /* The global count of the number of times this transform has been set on an
5495     * image.
5496     */
5497    unsigned int global_use;
5498
5499    /* The local count of the number of times this transform has been set. */
5500    unsigned int local_use;
5501
5502    /* The next transform in the list, each transform must call its own next
5503     * transform after it has processed the pixel successfully.
5504     */
5505    PNG_CONST struct image_transform *next;
5506
5507    /* A single transform for the image, expressed as a series of function
5508     * callbacks and some space for values.
5509     *
5510     * First a callback to add any required modifications to the png_modifier;
5511     * this gets called just before the modifier is set up for read.
5512     */
5513    void (*ini)(PNG_CONST struct image_transform *this,
5514       struct transform_display *that);
5515
5516    /* And a callback to set the transform on the current png_read_struct:
5517     */
5518    void (*set)(PNG_CONST struct image_transform *this,
5519       struct transform_display *that, png_structp pp, png_infop pi);
5520
5521    /* Then a transform that takes an input pixel in one PNG format or another
5522     * and modifies it by a pngvalid implementation of the transform (thus
5523     * duplicating the libpng intent without, we hope, duplicating the bugs
5524     * in the libpng implementation!)  The png_structp is solely to allow error
5525     * reporting via png_error and png_warning.
5526     */
5527    void (*mod)(PNG_CONST struct image_transform *this, image_pixel *that,
5528       png_const_structp pp, PNG_CONST struct transform_display *display);
5529
5530    /* Add this transform to the list and return true if the transform is
5531     * meaningful for this colour type and bit depth - if false then the
5532     * transform should have no effect on the image so there's not a lot of
5533     * point running it.
5534     */
5535    int (*add)(struct image_transform *this,
5536       PNG_CONST struct image_transform **that, png_byte colour_type,
5537       png_byte bit_depth);
5538 } image_transform;
5539
5540 typedef struct transform_display
5541 {
5542    standard_display this;
5543
5544    /* Parameters */
5545    png_modifier*              pm;
5546    PNG_CONST image_transform* transform_list;
5547
5548    /* Local variables */
5549    png_byte output_colour_type;
5550    png_byte output_bit_depth;
5551
5552    /* Modifications (not necessarily used.) */
5553    gama_modification gama_mod;
5554    chrm_modification chrm_mod;
5555    srgb_modification srgb_mod;
5556 } transform_display;
5557
5558 /* Set sRGB, cHRM and gAMA transforms as required by the current encoding. */
5559 static void
5560 transform_set_encoding(transform_display *this)
5561 {
5562    /* Set up the png_modifier '_current' fields then use these to determine how
5563     * to add appropriate chunks.
5564     */
5565    png_modifier *pm = this->pm;
5566
5567    modifier_set_encoding(pm);
5568
5569    if (modifier_color_encoding_is_set(pm))
5570    {
5571       if (modifier_color_encoding_is_sRGB(pm))
5572          srgb_modification_init(&this->srgb_mod, pm, PNG_sRGB_INTENT_ABSOLUTE);
5573
5574       else
5575       {
5576          /* Set gAMA and cHRM separately. */
5577          gama_modification_init(&this->gama_mod, pm, pm->current_gamma);
5578
5579          if (pm->current_encoding != 0)
5580             chrm_modification_init(&this->chrm_mod, pm, pm->current_encoding);
5581       }
5582    }
5583 }
5584
5585 /* Three functions to end the list: */
5586 static void
5587 image_transform_ini_end(PNG_CONST image_transform *this,
5588    transform_display *that)
5589 {
5590    UNUSED(this)
5591    UNUSED(that)
5592 }
5593
5594 static void
5595 image_transform_set_end(PNG_CONST image_transform *this,
5596    transform_display *that, png_structp pp, png_infop pi)
5597 {
5598    UNUSED(this)
5599    UNUSED(that)
5600    UNUSED(pp)
5601    UNUSED(pi)
5602 }
5603
5604 /* At the end of the list recalculate the output image pixel value from the
5605  * double precision values set up by the preceding 'mod' calls:
5606  */
5607 static unsigned int
5608 sample_scale(double sample_value, unsigned int scale)
5609 {
5610    sample_value = floor(sample_value * scale + .5);
5611
5612    /* Return NaN as 0: */
5613    if (!(sample_value > 0))
5614       sample_value = 0;
5615    else if (sample_value > scale)
5616       sample_value = scale;
5617
5618    return (unsigned int)sample_value;
5619 }
5620
5621 static void
5622 image_transform_mod_end(PNG_CONST image_transform *this, image_pixel *that,
5623     png_const_structp pp, PNG_CONST transform_display *display)
5624 {
5625    PNG_CONST unsigned int scale = (1U<<that->sample_depth)-1;
5626
5627    UNUSED(this)
5628    UNUSED(pp)
5629    UNUSED(display)
5630
5631    /* At the end recalculate the digitized red green and blue values according
5632     * to the current sample_depth of the pixel.
5633     *
5634     * The sample value is simply scaled to the maximum, checking for over
5635     * and underflow (which can both happen for some image transforms,
5636     * including simple size scaling, though libpng doesn't do that at present.
5637     */
5638    that->red = sample_scale(that->redf, scale);
5639
5640    /* The error value is increased, at the end, according to the lowest sBIT
5641     * value seen.  Common sense tells us that the intermediate integer
5642     * representations are no more accurate than +/- 0.5 in the integral values,
5643     * the sBIT allows the implementation to be worse than this.  In addition the
5644     * PNG specification actually permits any error within the range (-1..+1),
5645     * but that is ignored here.  Instead the final digitized value is compared,
5646     * below to the digitized value of the error limits - this has the net effect
5647     * of allowing (almost) +/-1 in the output value.  It's difficult to see how
5648     * any algorithm that digitizes intermediate results can be more accurate.
5649     */
5650    that->rede += 1./(2*((1U<<that->red_sBIT)-1));
5651
5652    if (that->colour_type & PNG_COLOR_MASK_COLOR)
5653    {
5654       that->green = sample_scale(that->greenf, scale);
5655       that->blue = sample_scale(that->bluef, scale);
5656       that->greene += 1./(2*((1U<<that->green_sBIT)-1));
5657       that->bluee += 1./(2*((1U<<that->blue_sBIT)-1));
5658    }
5659    else
5660    {
5661       that->blue = that->green = that->red;
5662       that->bluef = that->greenf = that->redf;
5663       that->bluee = that->greene = that->rede;
5664    }
5665
5666    if ((that->colour_type & PNG_COLOR_MASK_ALPHA) ||
5667       that->colour_type == PNG_COLOR_TYPE_PALETTE)
5668    {
5669       that->alpha = sample_scale(that->alphaf, scale);
5670       that->alphae += 1./(2*((1U<<that->alpha_sBIT)-1));
5671    }
5672    else
5673    {
5674       that->alpha = scale; /* opaque */
5675       that->alpha = 1;     /* Override this. */
5676       that->alphae = 0;    /* It's exact ;-) */
5677    }
5678 }
5679
5680 /* Static 'end' structure: */
5681 static image_transform image_transform_end =
5682 {
5683    "(end)", /* name */
5684    1, /* enable */
5685    0, /* list */
5686    0, /* global_use */
5687    0, /* local_use */
5688    0, /* next */
5689    image_transform_ini_end,
5690    image_transform_set_end,
5691    image_transform_mod_end,
5692    0 /* never called, I want it to crash if it is! */
5693 };
5694
5695 /* Reader callbacks and implementations, where they differ from the standard
5696  * ones.
5697  */
5698 static void
5699 transform_display_init(transform_display *dp, png_modifier *pm, png_uint_32 id,
5700     PNG_CONST image_transform *transform_list)
5701 {
5702    memset(dp, 0, sizeof *dp);
5703
5704    /* Standard fields */
5705    standard_display_init(&dp->this, &pm->this, id, 0/*do_interlace*/,
5706       pm->use_update_info);
5707
5708    /* Parameter fields */
5709    dp->pm = pm;
5710    dp->transform_list = transform_list;
5711
5712    /* Local variable fields */
5713    dp->output_colour_type = 255; /* invalid */
5714    dp->output_bit_depth = 255;  /* invalid */
5715 }
5716
5717 static void
5718 transform_info_imp(transform_display *dp, png_structp pp, png_infop pi)
5719 {
5720    /* Reuse the standard stuff as appropriate. */
5721    standard_info_part1(&dp->this, pp, pi);
5722
5723    /* Now set the list of transforms. */
5724    dp->transform_list->set(dp->transform_list, dp, pp, pi);
5725
5726    /* Update the info structure for these transforms: */
5727    {
5728       int i = dp->this.use_update_info;
5729       /* Always do one call, even if use_update_info is 0. */
5730       do
5731          png_read_update_info(pp, pi);
5732       while (--i > 0);
5733    }
5734
5735    /* And get the output information into the standard_display */
5736    standard_info_part2(&dp->this, pp, pi, 1/*images*/);
5737
5738    /* Plus the extra stuff we need for the transform tests: */
5739    dp->output_colour_type = png_get_color_type(pp, pi);
5740    dp->output_bit_depth = png_get_bit_depth(pp, pi);
5741
5742    /* Validate the combination of colour type and bit depth that we are getting
5743     * out of libpng; the semantics of something not in the PNG spec are, at
5744     * best, unclear.
5745     */
5746    switch (dp->output_colour_type)
5747    {
5748    case PNG_COLOR_TYPE_PALETTE:
5749       if (dp->output_bit_depth > 8) goto error;
5750       /*FALL THROUGH*/
5751    case PNG_COLOR_TYPE_GRAY:
5752       if (dp->output_bit_depth == 1 || dp->output_bit_depth == 2 ||
5753          dp->output_bit_depth == 4)
5754          break;
5755       /*FALL THROUGH*/
5756    default:
5757       if (dp->output_bit_depth == 8 || dp->output_bit_depth == 16)
5758          break;
5759       /*FALL THROUGH*/
5760    error:
5761       {
5762          char message[128];
5763          size_t pos;
5764
5765          pos = safecat(message, sizeof message, 0,
5766             "invalid final bit depth: colour type(");
5767          pos = safecatn(message, sizeof message, pos, dp->output_colour_type);
5768          pos = safecat(message, sizeof message, pos, ") with bit depth: ");
5769          pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5770
5771          png_error(pp, message);
5772       }
5773    }
5774
5775    /* Use a test pixel to check that the output agrees with what we expect -
5776     * this avoids running the whole test if the output is unexpected.
5777     */
5778    {
5779       image_pixel test_pixel;
5780
5781       memset(&test_pixel, 0, sizeof test_pixel);
5782       test_pixel.colour_type = dp->this.colour_type; /* input */
5783       test_pixel.bit_depth = dp->this.bit_depth;
5784       if (test_pixel.colour_type == PNG_COLOR_TYPE_PALETTE)
5785          test_pixel.sample_depth = 8;
5786       else
5787          test_pixel.sample_depth = test_pixel.bit_depth;
5788       /* Don't need sBIT here, but it must be set to non-zero to avoid
5789        * arithmetic overflows.
5790        */
5791       test_pixel.have_tRNS = dp->this.is_transparent;
5792       test_pixel.red_sBIT = test_pixel.green_sBIT = test_pixel.blue_sBIT =
5793          test_pixel.alpha_sBIT = test_pixel.sample_depth;
5794
5795       dp->transform_list->mod(dp->transform_list, &test_pixel, pp, dp);
5796
5797       if (test_pixel.colour_type != dp->output_colour_type)
5798       {
5799          char message[128];
5800          size_t pos = safecat(message, sizeof message, 0, "colour type ");
5801
5802          pos = safecatn(message, sizeof message, pos, dp->output_colour_type);
5803          pos = safecat(message, sizeof message, pos, " expected ");
5804          pos = safecatn(message, sizeof message, pos, test_pixel.colour_type);
5805
5806          png_error(pp, message);
5807       }
5808
5809       if (test_pixel.bit_depth != dp->output_bit_depth)
5810       {
5811          char message[128];
5812          size_t pos = safecat(message, sizeof message, 0, "bit depth ");
5813
5814          pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5815          pos = safecat(message, sizeof message, pos, " expected ");
5816          pos = safecatn(message, sizeof message, pos, test_pixel.bit_depth);
5817
5818          png_error(pp, message);
5819       }
5820
5821       /* If both bit depth and colour type are correct check the sample depth.
5822        * I believe these are both internal errors.
5823        */
5824       if (test_pixel.colour_type == PNG_COLOR_TYPE_PALETTE)
5825       {
5826          if (test_pixel.sample_depth != 8) /* oops - internal error! */
5827             png_error(pp, "pngvalid: internal: palette sample depth not 8");
5828       }
5829       else if (test_pixel.sample_depth != dp->output_bit_depth)
5830       {
5831          char message[128];
5832          size_t pos = safecat(message, sizeof message, 0,
5833             "internal: sample depth ");
5834
5835          pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5836          pos = safecat(message, sizeof message, pos, " expected ");
5837          pos = safecatn(message, sizeof message, pos, test_pixel.sample_depth);
5838
5839          png_error(pp, message);
5840       }
5841    }
5842 }
5843
5844 static void PNGCBAPI
5845 transform_info(png_structp pp, png_infop pi)
5846 {
5847    transform_info_imp(voidcast(transform_display*, png_get_progressive_ptr(pp)),
5848       pp, pi);
5849 }
5850
5851 static void
5852 transform_range_check(png_const_structp pp, unsigned int r, unsigned int g,
5853    unsigned int b, unsigned int a, unsigned int in_digitized, double in,
5854    unsigned int out, png_byte sample_depth, double err, double limit,
5855    PNG_CONST char *name, double digitization_error)
5856 {
5857    /* Compare the scaled, digitzed, values of our local calculation (in+-err)
5858     * with the digitized values libpng produced;  'sample_depth' is the actual
5859     * digitization depth of the libpng output colors (the bit depth except for
5860     * palette images where it is always 8.)  The check on 'err' is to detect
5861     * internal errors in pngvalid itself.
5862     */
5863    unsigned int max = (1U<<sample_depth)-1;
5864    double in_min = ceil((in-err)*max - digitization_error);
5865    double in_max = floor((in+err)*max + digitization_error);
5866    if (err > limit || !(out >= in_min && out <= in_max))
5867    {
5868       char message[256];
5869       size_t pos;
5870
5871       pos = safecat(message, sizeof message, 0, name);
5872       pos = safecat(message, sizeof message, pos, " output value error: rgba(");
5873       pos = safecatn(message, sizeof message, pos, r);
5874       pos = safecat(message, sizeof message, pos, ",");
5875       pos = safecatn(message, sizeof message, pos, g);
5876       pos = safecat(message, sizeof message, pos, ",");
5877       pos = safecatn(message, sizeof message, pos, b);
5878       pos = safecat(message, sizeof message, pos, ",");
5879       pos = safecatn(message, sizeof message, pos, a);
5880       pos = safecat(message, sizeof message, pos, "): ");
5881       pos = safecatn(message, sizeof message, pos, out);
5882       pos = safecat(message, sizeof message, pos, " expected: ");
5883       pos = safecatn(message, sizeof message, pos, in_digitized);
5884       pos = safecat(message, sizeof message, pos, " (");
5885       pos = safecatd(message, sizeof message, pos, (in-err)*max, 3);
5886       pos = safecat(message, sizeof message, pos, "..");
5887       pos = safecatd(message, sizeof message, pos, (in+err)*max, 3);
5888       pos = safecat(message, sizeof message, pos, ")");
5889
5890       png_error(pp, message);
5891    }
5892 }
5893
5894 static void
5895 transform_image_validate(transform_display *dp, png_const_structp pp,
5896    png_infop pi)
5897 {
5898    /* Constants for the loop below: */
5899    PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
5900    PNG_CONST png_byte in_ct = dp->this.colour_type;
5901    PNG_CONST png_byte in_bd = dp->this.bit_depth;
5902    PNG_CONST png_uint_32 w = dp->this.w;
5903    PNG_CONST png_uint_32 h = dp->this.h;
5904    PNG_CONST png_byte out_ct = dp->output_colour_type;
5905    PNG_CONST png_byte out_bd = dp->output_bit_depth;
5906    PNG_CONST png_byte sample_depth = (png_byte)(out_ct ==
5907       PNG_COLOR_TYPE_PALETTE ? 8 : out_bd);
5908    PNG_CONST png_byte red_sBIT = dp->this.red_sBIT;
5909    PNG_CONST png_byte green_sBIT = dp->this.green_sBIT;
5910    PNG_CONST png_byte blue_sBIT = dp->this.blue_sBIT;
5911    PNG_CONST png_byte alpha_sBIT = dp->this.alpha_sBIT;
5912    PNG_CONST int have_tRNS = dp->this.is_transparent;
5913    double digitization_error;
5914
5915    store_palette out_palette;
5916    png_uint_32 y;
5917
5918    UNUSED(pi)
5919
5920    /* Check for row overwrite errors */
5921    store_image_check(dp->this.ps, pp, 0);
5922
5923    /* Read the palette corresponding to the output if the output colour type
5924     * indicates a palette, othewise set out_palette to garbage.
5925     */
5926    if (out_ct == PNG_COLOR_TYPE_PALETTE)
5927    {
5928       /* Validate that the palette count itself has not changed - this is not
5929        * expected.
5930        */
5931       int npalette = (-1);
5932
5933       (void)read_palette(out_palette, &npalette, pp, pi);
5934       if (npalette != dp->this.npalette)
5935          png_error(pp, "unexpected change in palette size");
5936
5937       digitization_error = .5;
5938    }
5939    else
5940    {
5941       png_byte in_sample_depth;
5942
5943       memset(out_palette, 0x5e, sizeof out_palette);
5944
5945       /* use-input-precision means assume that if the input has 8 bit (or less)
5946        * samples and the output has 16 bit samples the calculations will be done
5947        * with 8 bit precision, not 16.
5948        */
5949       if (in_ct == PNG_COLOR_TYPE_PALETTE || in_bd < 16)
5950          in_sample_depth = 8;
5951       else
5952          in_sample_depth = in_bd;
5953
5954       if (sample_depth != 16 || in_sample_depth > 8 ||
5955          !dp->pm->calculations_use_input_precision)
5956          digitization_error = .5;
5957
5958       /* Else calculations are at 8 bit precision, and the output actually
5959        * consists of scaled 8-bit values, so scale .5 in 8 bits to the 16 bits:
5960        */
5961       else
5962          digitization_error = .5 * 257;
5963    }
5964
5965    for (y=0; y<h; ++y)
5966    {
5967       png_const_bytep PNG_CONST pRow = store_image_row(ps, pp, 0, y);
5968       png_uint_32 x;
5969
5970       /* The original, standard, row pre-transforms. */
5971       png_byte std[STANDARD_ROWMAX];
5972
5973       transform_row(pp, std, in_ct, in_bd, y);
5974
5975       /* Go through each original pixel transforming it and comparing with what
5976        * libpng did to the same pixel.
5977        */
5978       for (x=0; x<w; ++x)
5979       {
5980          image_pixel in_pixel, out_pixel;
5981          unsigned int r, g, b, a;
5982
5983          /* Find out what we think the pixel should be: */
5984          image_pixel_init(&in_pixel, std, in_ct, in_bd, x, dp->this.palette);
5985
5986          in_pixel.red_sBIT = red_sBIT;
5987          in_pixel.green_sBIT = green_sBIT;
5988          in_pixel.blue_sBIT = blue_sBIT;
5989          in_pixel.alpha_sBIT = alpha_sBIT;
5990          in_pixel.have_tRNS = have_tRNS;
5991
5992          /* For error detection, below. */
5993          r = in_pixel.red;
5994          g = in_pixel.green;
5995          b = in_pixel.blue;
5996          a = in_pixel.alpha;
5997
5998          dp->transform_list->mod(dp->transform_list, &in_pixel, pp, dp);
5999
6000          /* Read the output pixel and compare it to what we got, we don't
6001           * use the error field here, so no need to update sBIT.
6002           */
6003          image_pixel_init(&out_pixel, pRow, out_ct, out_bd, x, out_palette);
6004
6005          /* We don't expect changes to the index here even if the bit depth is
6006           * changed.
6007           */
6008          if (in_ct == PNG_COLOR_TYPE_PALETTE &&
6009             out_ct == PNG_COLOR_TYPE_PALETTE)
6010          {
6011             if (in_pixel.palette_index != out_pixel.palette_index)
6012                png_error(pp, "unexpected transformed palette index");
6013          }
6014
6015          /* Check the colours for palette images too - in fact the palette could
6016           * be separately verified itself in most cases.
6017           */
6018          if (in_pixel.red != out_pixel.red)
6019             transform_range_check(pp, r, g, b, a, in_pixel.red, in_pixel.redf,
6020                out_pixel.red, sample_depth, in_pixel.rede,
6021                dp->pm->limit + 1./(2*((1U<<in_pixel.red_sBIT)-1)), "red/gray",
6022                digitization_error);
6023
6024          if ((out_ct & PNG_COLOR_MASK_COLOR) != 0 &&
6025             in_pixel.green != out_pixel.green)
6026             transform_range_check(pp, r, g, b, a, in_pixel.green,
6027                in_pixel.greenf, out_pixel.green, sample_depth, in_pixel.greene,
6028                dp->pm->limit + 1./(2*((1U<<in_pixel.green_sBIT)-1)), "green",
6029                digitization_error);
6030
6031          if ((out_ct & PNG_COLOR_MASK_COLOR) != 0 &&
6032             in_pixel.blue != out_pixel.blue)
6033             transform_range_check(pp, r, g, b, a, in_pixel.blue, in_pixel.bluef,
6034                out_pixel.blue, sample_depth, in_pixel.bluee,
6035                dp->pm->limit + 1./(2*((1U<<in_pixel.blue_sBIT)-1)), "blue",
6036                digitization_error);
6037
6038          if ((out_ct & PNG_COLOR_MASK_ALPHA) != 0 &&
6039             in_pixel.alpha != out_pixel.alpha)
6040             transform_range_check(pp, r, g, b, a, in_pixel.alpha,
6041                in_pixel.alphaf, out_pixel.alpha, sample_depth, in_pixel.alphae,
6042                dp->pm->limit + 1./(2*((1U<<in_pixel.alpha_sBIT)-1)), "alpha",
6043                digitization_error);
6044       } /* pixel (x) loop */
6045    } /* row (y) loop */
6046
6047    /* Record that something was actually checked to avoid a false positive. */
6048    dp->this.ps->validated = 1;
6049 }
6050
6051 static void PNGCBAPI
6052 transform_end(png_structp ppIn, png_infop pi)
6053 {
6054    png_const_structp pp = ppIn;
6055    transform_display *dp = voidcast(transform_display*,
6056       png_get_progressive_ptr(pp));
6057
6058    if (!dp->this.speed)
6059       transform_image_validate(dp, pp, pi);
6060    else
6061       dp->this.ps->validated = 1;
6062 }
6063
6064 /* A single test run. */
6065 static void
6066 transform_test(png_modifier *pmIn, PNG_CONST png_uint_32 idIn,
6067     PNG_CONST image_transform* transform_listIn, PNG_CONST char * volatile name)
6068 {
6069    transform_display d;
6070    context(&pmIn->this, fault);
6071
6072    transform_display_init(&d, pmIn, idIn, transform_listIn);
6073
6074    Try
6075    {
6076       size_t pos = 0;
6077       png_structp pp;
6078       png_infop pi;
6079       char full_name[256];
6080
6081       /* Make sure the encoding fields are correct and enter the required
6082        * modifications.
6083        */
6084       transform_set_encoding(&d);
6085
6086       /* Add any modifications required by the transform list. */
6087       d.transform_list->ini(d.transform_list, &d);
6088
6089       /* Add the color space information, if any, to the name. */
6090       pos = safecat(full_name, sizeof full_name, pos, name);
6091       pos = safecat_current_encoding(full_name, sizeof full_name, pos, d.pm);
6092
6093       /* Get a png_struct for reading the image. */
6094       pp = set_modifier_for_read(d.pm, &pi, d.this.id, full_name);
6095       standard_palette_init(&d.this);
6096
6097 #     if 0
6098          /* Logging (debugging only) */
6099          {
6100             char buffer[256];
6101
6102             (void)store_message(&d.pm->this, pp, buffer, sizeof buffer, 0,
6103                "running test");
6104
6105             fprintf(stderr, "%s\n", buffer);
6106          }
6107 #     endif
6108
6109       /* Introduce the correct read function. */
6110       if (d.pm->this.progressive)
6111       {
6112          /* Share the row function with the standard implementation. */
6113          png_set_progressive_read_fn(pp, &d, transform_info, progressive_row,
6114             transform_end);
6115
6116          /* Now feed data into the reader until we reach the end: */
6117          modifier_progressive_read(d.pm, pp, pi);
6118       }
6119       else
6120       {
6121          /* modifier_read expects a png_modifier* */
6122          png_set_read_fn(pp, d.pm, modifier_read);
6123
6124          /* Check the header values: */
6125          png_read_info(pp, pi);
6126
6127          /* Process the 'info' requirements. Only one image is generated */
6128          transform_info_imp(&d, pp, pi);
6129
6130          sequential_row(&d.this, pp, pi, -1, 0);
6131
6132          if (!d.this.speed)
6133             transform_image_validate(&d, pp, pi);
6134          else
6135             d.this.ps->validated = 1;
6136       }
6137
6138       modifier_reset(d.pm);
6139    }
6140
6141    Catch(fault)
6142    {
6143       modifier_reset(voidcast(png_modifier*,(void*)fault));
6144    }
6145 }
6146
6147 /* The transforms: */
6148 #define ITSTRUCT(name) image_transform_##name
6149 #define ITDATA(name) image_transform_data_##name
6150 #define image_transform_ini image_transform_default_ini
6151 #define IT(name)\
6152 static image_transform ITSTRUCT(name) =\
6153 {\
6154    #name,\
6155    1, /*enable*/\
6156    &PT, /*list*/\
6157    0, /*global_use*/\
6158    0, /*local_use*/\
6159    0, /*next*/\
6160    image_transform_ini,\
6161    image_transform_png_set_##name##_set,\
6162    image_transform_png_set_##name##_mod,\
6163    image_transform_png_set_##name##_add\
6164 }
6165 #define PT ITSTRUCT(end) /* stores the previous transform */
6166
6167 /* To save code: */
6168 static void
6169 image_transform_default_ini(PNG_CONST image_transform *this,
6170     transform_display *that)
6171 {
6172    this->next->ini(this->next, that);
6173 }
6174
6175 #ifdef PNG_READ_BACKGROUND_SUPPORTED
6176 static int
6177 image_transform_default_add(image_transform *this,
6178     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6179 {
6180    UNUSED(colour_type)
6181    UNUSED(bit_depth)
6182
6183    this->next = *that;
6184    *that = this;
6185
6186    return 1;
6187 }
6188 #endif
6189
6190 #ifdef PNG_READ_EXPAND_SUPPORTED
6191 /* png_set_palette_to_rgb */
6192 static void
6193 image_transform_png_set_palette_to_rgb_set(PNG_CONST image_transform *this,
6194     transform_display *that, png_structp pp, png_infop pi)
6195 {
6196    png_set_palette_to_rgb(pp);
6197    this->next->set(this->next, that, pp, pi);
6198 }
6199
6200 static void
6201 image_transform_png_set_palette_to_rgb_mod(PNG_CONST image_transform *this,
6202     image_pixel *that, png_const_structp pp,
6203     PNG_CONST transform_display *display)
6204 {
6205    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6206       image_pixel_convert_PLTE(that);
6207
6208    this->next->mod(this->next, that, pp, display);
6209 }
6210
6211 static int
6212 image_transform_png_set_palette_to_rgb_add(image_transform *this,
6213     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6214 {
6215    UNUSED(bit_depth)
6216
6217    this->next = *that;
6218    *that = this;
6219
6220    return colour_type == PNG_COLOR_TYPE_PALETTE;
6221 }
6222
6223 IT(palette_to_rgb);
6224 #undef PT
6225 #define PT ITSTRUCT(palette_to_rgb)
6226 #endif /* PNG_READ_EXPAND_SUPPORTED */
6227
6228 #ifdef PNG_READ_EXPAND_SUPPORTED
6229 /* png_set_tRNS_to_alpha */
6230 static void
6231 image_transform_png_set_tRNS_to_alpha_set(PNG_CONST image_transform *this,
6232    transform_display *that, png_structp pp, png_infop pi)
6233 {
6234    png_set_tRNS_to_alpha(pp);
6235    this->next->set(this->next, that, pp, pi);
6236 }
6237
6238 static void
6239 image_transform_png_set_tRNS_to_alpha_mod(PNG_CONST image_transform *this,
6240    image_pixel *that, png_const_structp pp,
6241    PNG_CONST transform_display *display)
6242 {
6243    /* LIBPNG BUG: this always forces palette images to RGB. */
6244    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6245       image_pixel_convert_PLTE(that);
6246
6247    /* This effectively does an 'expand' only if there is some transparency to
6248     * convert to an alpha channel.
6249     */
6250    if (that->have_tRNS)
6251       image_pixel_add_alpha(that, &display->this);
6252
6253    /* LIBPNG BUG: otherwise libpng still expands to 8 bits! */
6254    else
6255    {
6256       if (that->bit_depth < 8)
6257          that->bit_depth =8;
6258       if (that->sample_depth < 8)
6259          that->sample_depth = 8;
6260    }
6261
6262    this->next->mod(this->next, that, pp, display);
6263 }
6264
6265 static int
6266 image_transform_png_set_tRNS_to_alpha_add(image_transform *this,
6267     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6268 {
6269    UNUSED(bit_depth)
6270
6271    this->next = *that;
6272    *that = this;
6273
6274    /* We don't know yet whether there will be a tRNS chunk, but we know that
6275     * this transformation should do nothing if there already is an alpha
6276     * channel.
6277     */
6278    return (colour_type & PNG_COLOR_MASK_ALPHA) == 0;
6279 }
6280
6281 IT(tRNS_to_alpha);
6282 #undef PT
6283 #define PT ITSTRUCT(tRNS_to_alpha)
6284 #endif /* PNG_READ_EXPAND_SUPPORTED */
6285
6286 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
6287 /* png_set_gray_to_rgb */
6288 static void
6289 image_transform_png_set_gray_to_rgb_set(PNG_CONST image_transform *this,
6290     transform_display *that, png_structp pp, png_infop pi)
6291 {
6292    png_set_gray_to_rgb(pp);
6293    this->next->set(this->next, that, pp, pi);
6294 }
6295
6296 static void
6297 image_transform_png_set_gray_to_rgb_mod(PNG_CONST image_transform *this,
6298     image_pixel *that, png_const_structp pp,
6299     PNG_CONST transform_display *display)
6300 {
6301    /* NOTE: we can actually pend the tRNS processing at this point because we
6302     * can correctly recognize the original pixel value even though we have
6303     * mapped the one gray channel to the three RGB ones, but in fact libpng
6304     * doesn't do this, so we don't either.
6305     */
6306    if ((that->colour_type & PNG_COLOR_MASK_COLOR) == 0 && that->have_tRNS)
6307       image_pixel_add_alpha(that, &display->this);
6308
6309    /* Simply expand the bit depth and alter the colour type as required. */
6310    if (that->colour_type == PNG_COLOR_TYPE_GRAY)
6311    {
6312       /* RGB images have a bit depth at least equal to '8' */
6313       if (that->bit_depth < 8)
6314          that->sample_depth = that->bit_depth = 8;
6315
6316       /* And just changing the colour type works here because the green and blue
6317        * channels are being maintained in lock-step with the red/gray:
6318        */
6319       that->colour_type = PNG_COLOR_TYPE_RGB;
6320    }
6321
6322    else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6323       that->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
6324
6325    this->next->mod(this->next, that, pp, display);
6326 }
6327
6328 static int
6329 image_transform_png_set_gray_to_rgb_add(image_transform *this,
6330     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6331 {
6332    UNUSED(bit_depth)
6333
6334    this->next = *that;
6335    *that = this;
6336
6337    return (colour_type & PNG_COLOR_MASK_COLOR) == 0;
6338 }
6339
6340 IT(gray_to_rgb);
6341 #undef PT
6342 #define PT ITSTRUCT(gray_to_rgb)
6343 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
6344
6345 #ifdef PNG_READ_EXPAND_SUPPORTED
6346 /* png_set_expand */
6347 static void
6348 image_transform_png_set_expand_set(PNG_CONST image_transform *this,
6349     transform_display *that, png_structp pp, png_infop pi)
6350 {
6351    png_set_expand(pp);
6352    this->next->set(this->next, that, pp, pi);
6353 }
6354
6355 static void
6356 image_transform_png_set_expand_mod(PNG_CONST image_transform *this,
6357     image_pixel *that, png_const_structp pp,
6358     PNG_CONST transform_display *display)
6359 {
6360    /* The general expand case depends on what the colour type is: */
6361    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6362       image_pixel_convert_PLTE(that);
6363    else if (that->bit_depth < 8) /* grayscale */
6364       that->sample_depth = that->bit_depth = 8;
6365
6366    if (that->have_tRNS)
6367       image_pixel_add_alpha(that, &display->this);
6368
6369    this->next->mod(this->next, that, pp, display);
6370 }
6371
6372 static int
6373 image_transform_png_set_expand_add(image_transform *this,
6374     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6375 {
6376    UNUSED(bit_depth)
6377
6378    this->next = *that;
6379    *that = this;
6380
6381    /* 'expand' should do nothing for RGBA or GA input - no tRNS and the bit
6382     * depth is at least 8 already.
6383     */
6384    return (colour_type & PNG_COLOR_MASK_ALPHA) == 0;
6385 }
6386
6387 IT(expand);
6388 #undef PT
6389 #define PT ITSTRUCT(expand)
6390 #endif /* PNG_READ_EXPAND_SUPPORTED */
6391
6392 #ifdef PNG_READ_EXPAND_SUPPORTED
6393 /* png_set_expand_gray_1_2_4_to_8
6394  * LIBPNG BUG: this just does an 'expand'
6395  */
6396 static void
6397 image_transform_png_set_expand_gray_1_2_4_to_8_set(
6398     PNG_CONST image_transform *this, transform_display *that, png_structp pp,
6399     png_infop pi)
6400 {
6401    png_set_expand_gray_1_2_4_to_8(pp);
6402    this->next->set(this->next, that, pp, pi);
6403 }
6404
6405 static void
6406 image_transform_png_set_expand_gray_1_2_4_to_8_mod(
6407     PNG_CONST image_transform *this, image_pixel *that, png_const_structp pp,
6408     PNG_CONST transform_display *display)
6409 {
6410    image_transform_png_set_expand_mod(this, that, pp, display);
6411 }
6412
6413 static int
6414 image_transform_png_set_expand_gray_1_2_4_to_8_add(image_transform *this,
6415     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6416 {
6417    return image_transform_png_set_expand_add(this, that, colour_type,
6418       bit_depth);
6419 }
6420
6421 IT(expand_gray_1_2_4_to_8);
6422 #undef PT
6423 #define PT ITSTRUCT(expand_gray_1_2_4_to_8)
6424 #endif /* PNG_READ_EXPAND_SUPPORTED */
6425
6426 #ifdef PNG_READ_EXPAND_16_SUPPORTED
6427 /* png_set_expand_16 */
6428 static void
6429 image_transform_png_set_expand_16_set(PNG_CONST image_transform *this,
6430     transform_display *that, png_structp pp, png_infop pi)
6431 {
6432    png_set_expand_16(pp);
6433    this->next->set(this->next, that, pp, pi);
6434 }
6435
6436 static void
6437 image_transform_png_set_expand_16_mod(PNG_CONST image_transform *this,
6438     image_pixel *that, png_const_structp pp,
6439     PNG_CONST transform_display *display)
6440 {
6441    /* Expect expand_16 to expand everything to 16 bits as a result of also
6442     * causing 'expand' to happen.
6443     */
6444    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6445       image_pixel_convert_PLTE(that);
6446
6447    if (that->have_tRNS)
6448       image_pixel_add_alpha(that, &display->this);
6449
6450    if (that->bit_depth < 16)
6451       that->sample_depth = that->bit_depth = 16;
6452
6453    this->next->mod(this->next, that, pp, display);
6454 }
6455
6456 static int
6457 image_transform_png_set_expand_16_add(image_transform *this,
6458     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6459 {
6460    UNUSED(colour_type)
6461
6462    this->next = *that;
6463    *that = this;
6464
6465    /* expand_16 does something unless the bit depth is already 16. */
6466    return bit_depth < 16;
6467 }
6468
6469 IT(expand_16);
6470 #undef PT
6471 #define PT ITSTRUCT(expand_16)
6472 #endif /* PNG_READ_EXPAND_16_SUPPORTED */
6473
6474 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED  /* API added in 1.5.4 */
6475 /* png_set_scale_16 */
6476 static void
6477 image_transform_png_set_scale_16_set(PNG_CONST image_transform *this,
6478     transform_display *that, png_structp pp, png_infop pi)
6479 {
6480    png_set_scale_16(pp);
6481    this->next->set(this->next, that, pp, pi);
6482 }
6483
6484 static void
6485 image_transform_png_set_scale_16_mod(PNG_CONST image_transform *this,
6486     image_pixel *that, png_const_structp pp,
6487     PNG_CONST transform_display *display)
6488 {
6489    if (that->bit_depth == 16)
6490    {
6491       that->sample_depth = that->bit_depth = 8;
6492       if (that->red_sBIT > 8) that->red_sBIT = 8;
6493       if (that->green_sBIT > 8) that->green_sBIT = 8;
6494       if (that->blue_sBIT > 8) that->blue_sBIT = 8;
6495       if (that->alpha_sBIT > 8) that->alpha_sBIT = 8;
6496    }
6497
6498    this->next->mod(this->next, that, pp, display);
6499 }
6500
6501 static int
6502 image_transform_png_set_scale_16_add(image_transform *this,
6503     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6504 {
6505    UNUSED(colour_type)
6506
6507    this->next = *that;
6508    *that = this;
6509
6510    return bit_depth > 8;
6511 }
6512
6513 IT(scale_16);
6514 #undef PT
6515 #define PT ITSTRUCT(scale_16)
6516 #endif /* PNG_READ_SCALE_16_TO_8_SUPPORTED (1.5.4 on) */
6517
6518 #ifdef PNG_READ_16_TO_8_SUPPORTED /* the default before 1.5.4 */
6519 /* png_set_strip_16 */
6520 static void
6521 image_transform_png_set_strip_16_set(PNG_CONST image_transform *this,
6522     transform_display *that, png_structp pp, png_infop pi)
6523 {
6524    png_set_strip_16(pp);
6525    this->next->set(this->next, that, pp, pi);
6526 }
6527
6528 static void
6529 image_transform_png_set_strip_16_mod(PNG_CONST image_transform *this,
6530     image_pixel *that, png_const_structp pp,
6531     PNG_CONST transform_display *display)
6532 {
6533    if (that->bit_depth == 16)
6534    {
6535       that->sample_depth = that->bit_depth = 8;
6536       if (that->red_sBIT > 8) that->red_sBIT = 8;
6537       if (that->green_sBIT > 8) that->green_sBIT = 8;
6538       if (that->blue_sBIT > 8) that->blue_sBIT = 8;
6539       if (that->alpha_sBIT > 8) that->alpha_sBIT = 8;
6540
6541       /* Prior to 1.5.4 png_set_strip_16 would use an 'accurate' method if this
6542        * configuration option is set.  From 1.5.4 the flag is never set and the
6543        * 'scale' API (above) must be used.
6544        */
6545 #     ifdef PNG_READ_ACCURATE_SCALE_SUPPORTED
6546 #        if PNG_LIBPNG_VER >= 10504
6547 #           error PNG_READ_ACCURATE_SCALE should not be set
6548 #        endif
6549
6550          /* The strip 16 algorithm drops the low 8 bits rather than calculating
6551           * 1/257, so we need to adjust the permitted errors appropriately:
6552           * Notice that this is only relevant prior to the addition of the
6553           * png_set_scale_16 API in 1.5.4 (but 1.5.4+ always defines the above!)
6554           */
6555          {
6556             PNG_CONST double d = (255-128.5)/65535;
6557             that->rede += d;
6558             that->greene += d;
6559             that->bluee += d;
6560             that->alphae += d;
6561          }
6562 #     endif
6563    }
6564
6565    this->next->mod(this->next, that, pp, display);
6566 }
6567
6568 static int
6569 image_transform_png_set_strip_16_add(image_transform *this,
6570     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6571 {
6572    UNUSED(colour_type)
6573
6574    this->next = *that;
6575    *that = this;
6576
6577    return bit_depth > 8;
6578 }
6579
6580 IT(strip_16);
6581 #undef PT
6582 #define PT ITSTRUCT(strip_16)
6583 #endif /* PNG_READ_16_TO_8_SUPPORTED */
6584
6585 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
6586 /* png_set_strip_alpha */
6587 static void
6588 image_transform_png_set_strip_alpha_set(PNG_CONST image_transform *this,
6589     transform_display *that, png_structp pp, png_infop pi)
6590 {
6591    png_set_strip_alpha(pp);
6592    this->next->set(this->next, that, pp, pi);
6593 }
6594
6595 static void
6596 image_transform_png_set_strip_alpha_mod(PNG_CONST image_transform *this,
6597     image_pixel *that, png_const_structp pp,
6598     PNG_CONST transform_display *display)
6599 {
6600    if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6601       that->colour_type = PNG_COLOR_TYPE_GRAY;
6602    else if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
6603       that->colour_type = PNG_COLOR_TYPE_RGB;
6604
6605    that->have_tRNS = 0;
6606    that->alphaf = 1;
6607
6608    this->next->mod(this->next, that, pp, display);
6609 }
6610
6611 static int
6612 image_transform_png_set_strip_alpha_add(image_transform *this,
6613     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6614 {
6615    UNUSED(bit_depth)
6616
6617    this->next = *that;
6618    *that = this;
6619
6620    return (colour_type & PNG_COLOR_MASK_ALPHA) != 0;
6621 }
6622
6623 IT(strip_alpha);
6624 #undef PT
6625 #define PT ITSTRUCT(strip_alpha)
6626 #endif /* PNG_READ_STRIP_ALPHA_SUPPORTED */
6627
6628 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
6629 /* png_set_rgb_to_gray(png_structp, int err_action, double red, double green)
6630  * png_set_rgb_to_gray_fixed(png_structp, int err_action, png_fixed_point red,
6631  *    png_fixed_point green)
6632  * png_get_rgb_to_gray_status
6633  *
6634  * The 'default' test here uses values known to be used inside libpng:
6635  *
6636  *   red:    6968
6637  *   green: 23434
6638  *   blue:   2366
6639  *
6640  * These values are being retained for compatibility, along with the somewhat
6641  * broken truncation calculation in the fast-and-inaccurate code path.  Older
6642  * versions of libpng will fail the accuracy tests below because they use the
6643  * truncation algorithm everywhere.
6644  */
6645 #define data ITDATA(rgb_to_gray)
6646 static struct
6647 {
6648    double gamma;      /* File gamma to use in processing */
6649
6650    /* The following are the parameters for png_set_rgb_to_gray: */
6651 #  ifdef PNG_FLOATING_POINT_SUPPORTED
6652       double red_to_set;
6653       double green_to_set;
6654 #  else
6655       png_fixed_point red_to_set;
6656       png_fixed_point green_to_set;
6657 #  endif
6658
6659    /* The actual coefficients: */
6660    double red_coefficient;
6661    double green_coefficient;
6662    double blue_coefficient;
6663
6664    /* Set if the coeefficients have been overridden. */
6665    int coefficients_overridden;
6666 } data;
6667
6668 #undef image_transform_ini
6669 #define image_transform_ini image_transform_png_set_rgb_to_gray_ini
6670 static void
6671 image_transform_png_set_rgb_to_gray_ini(PNG_CONST image_transform *this,
6672     transform_display *that)
6673 {
6674    png_modifier *pm = that->pm;
6675    PNG_CONST color_encoding *e = pm->current_encoding;
6676
6677    UNUSED(this)
6678
6679    /* Since we check the encoding this flag must be set: */
6680    pm->test_uses_encoding = 1;
6681
6682    /* If 'e' is not NULL chromaticity information is present and either a cHRM
6683     * or an sRGB chunk will be inserted.
6684     */
6685    if (e != 0)
6686    {
6687       /* Coefficients come from the encoding, but may need to be normalized to a
6688        * white point Y of 1.0
6689        */
6690       PNG_CONST double whiteY = e->red.Y + e->green.Y + e->blue.Y;
6691
6692       data.red_coefficient = e->red.Y;
6693       data.green_coefficient = e->green.Y;
6694       data.blue_coefficient = e->blue.Y;
6695
6696       if (whiteY != 1)
6697       {
6698          data.red_coefficient /= whiteY;
6699          data.green_coefficient /= whiteY;
6700          data.blue_coefficient /= whiteY;
6701       }
6702    }
6703
6704    else
6705    {
6706       /* The default (built in) coeffcients, as above: */
6707       data.red_coefficient = 6968 / 32768.;
6708       data.green_coefficient = 23434 / 32768.;
6709       data.blue_coefficient = 2366 / 32768.;
6710    }
6711
6712    data.gamma = pm->current_gamma;
6713
6714    /* If not set then the calculations assume linear encoding (implicitly): */
6715    if (data.gamma == 0)
6716       data.gamma = 1;
6717
6718    /* The arguments to png_set_rgb_to_gray can override the coefficients implied
6719     * by the color space encoding.  If doing exhaustive checks do the override
6720     * in each case, otherwise do it randomly.
6721     */
6722    if (pm->test_exhaustive)
6723    {
6724       /* First time in coefficients_overridden is 0, the following sets it to 1,
6725        * so repeat if it is set.  If a test fails this may mean we subsequently
6726        * skip a non-override test, ignore that.
6727        */
6728       data.coefficients_overridden = !data.coefficients_overridden;
6729       pm->repeat = data.coefficients_overridden != 0;
6730    }
6731
6732    else
6733       data.coefficients_overridden = random_choice();
6734
6735    if (data.coefficients_overridden)
6736    {
6737       /* These values override the color encoding defaults, simply use random
6738        * numbers.
6739        */
6740       png_uint_32 ru;
6741       double total;
6742
6743       RANDOMIZE(ru);
6744       data.green_coefficient = total = (ru & 0xffff) / 65535.;
6745       ru >>= 16;
6746       data.red_coefficient = (1 - total) * (ru & 0xffff) / 65535.;
6747       total += data.red_coefficient;
6748       data.blue_coefficient = 1 - total;
6749
6750 #     ifdef PNG_FLOATING_POINT_SUPPORTED
6751          data.red_to_set = data.red_coefficient;
6752          data.green_to_set = data.green_coefficient;
6753 #     else
6754          data.red_to_set = fix(data.red_coefficient);
6755          data.green_to_set = fix(data.green_coefficient);
6756 #     endif
6757
6758       /* The following just changes the error messages: */
6759       pm->encoding_ignored = 1;
6760    }
6761
6762    else
6763    {
6764       data.red_to_set = -1;
6765       data.green_to_set = -1;
6766    }
6767
6768    /* Adjust the error limit in the png_modifier because of the larger errors
6769     * produced in the digitization during the gamma handling.
6770     */
6771    if (data.gamma != 1) /* Use gamma tables */
6772    {
6773       if (that->this.bit_depth == 16 || pm->assume_16_bit_calculations)
6774       {
6775          /* The computations have the form:
6776           *
6777           *    r * rc + g * gc + b * bc
6778           *
6779           *  Each component of which is +/-1/65535 from the gamma_to_1 table
6780           *  lookup, resulting in a base error of +/-6.  The gamma_from_1
6781           *  conversion adds another +/-2 in the 16-bit case and
6782           *  +/-(1<<(15-PNG_MAX_GAMMA_8)) in the 8-bit case.
6783           */
6784          that->pm->limit += pow(
6785 #           if PNG_MAX_GAMMA_8 < 14
6786                (that->this.bit_depth == 16 ? 8. :
6787                   6. + (1<<(15-PNG_MAX_GAMMA_8)))
6788 #           else
6789                8.
6790 #           endif
6791                /65535, data.gamma);
6792       }
6793
6794       else
6795       {
6796          /* Rounding to 8 bits in the linear space causes massive errors which
6797           * will trigger the error check in transform_range_check.  Fix that
6798           * here by taking the gamma encoding into account.
6799           *
6800           * When DIGITIZE is set because a pre-1.7 version of libpng is being
6801           * tested allow a bigger slack.
6802           *
6803           * NOTE: this magic number was determined by experiment to be 1.1 (when
6804           * using fixed point arithmetic).  There's no great merit to the value
6805           * below, however it only affects the limit used for checking for
6806           * internal calculation errors, not the actual limit imposed by
6807           * pngvalid on the output errors.
6808           */
6809          that->pm->limit += pow(
6810 #           if DIGITIZE
6811                1.1
6812 #           else
6813                1.
6814 #           endif
6815                /255, data.gamma);
6816       }
6817    }
6818
6819    else
6820    {
6821       /* With no gamma correction a large error comes from the truncation of the
6822        * calculation in the 8 bit case, allow for that here.
6823        */
6824       if (that->this.bit_depth != 16 && !pm->assume_16_bit_calculations)
6825          that->pm->limit += 4E-3;
6826    }
6827 }
6828
6829 static void
6830 image_transform_png_set_rgb_to_gray_set(PNG_CONST image_transform *this,
6831     transform_display *that, png_structp pp, png_infop pi)
6832 {
6833    PNG_CONST int error_action = 1; /* no error, no defines in png.h */
6834
6835 #  ifdef PNG_FLOATING_POINT_SUPPORTED
6836       png_set_rgb_to_gray(pp, error_action, data.red_to_set, data.green_to_set);
6837 #  else
6838       png_set_rgb_to_gray_fixed(pp, error_action, data.red_to_set,
6839          data.green_to_set);
6840 #  endif
6841
6842 #  ifdef PNG_READ_cHRM_SUPPORTED
6843       if (that->pm->current_encoding != 0)
6844       {
6845          /* We have an encoding so a cHRM chunk may have been set; if so then
6846           * check that the libpng APIs give the correct (X,Y,Z) values within
6847           * some margin of error for the round trip through the chromaticity
6848           * form.
6849           */
6850 #        ifdef PNG_FLOATING_POINT_SUPPORTED
6851 #           define API_function png_get_cHRM_XYZ
6852 #           define API_form "FP"
6853 #           define API_type double
6854 #           define API_cvt(x) (x)
6855 #        else
6856 #           define API_function png_get_cHRM_XYZ_fixed
6857 #           define API_form "fixed"
6858 #           define API_type png_fixed_point
6859 #           define API_cvt(x) ((double)(x)/PNG_FP_1)
6860 #        endif
6861
6862          API_type rX, gX, bX;
6863          API_type rY, gY, bY;
6864          API_type rZ, gZ, bZ;
6865
6866          if ((API_function(pp, pi, &rX, &rY, &rZ, &gX, &gY, &gZ, &bX, &bY, &bZ)
6867                & PNG_INFO_cHRM) != 0)
6868          {
6869             double maxe;
6870             PNG_CONST char *el;
6871             color_encoding e, o;
6872
6873             /* Expect libpng to return a normalized result, but the original
6874              * color space encoding may not be normalized.
6875              */
6876             modifier_current_encoding(that->pm, &o);
6877             normalize_color_encoding(&o);
6878
6879             /* Sanity check the pngvalid code - the coefficients should match
6880              * the normalized Y values of the encoding unless they were
6881              * overridden.
6882              */
6883             if (data.red_to_set == -1 && data.green_to_set == -1 &&
6884                (fabs(o.red.Y - data.red_coefficient) > DBL_EPSILON ||
6885                fabs(o.green.Y - data.green_coefficient) > DBL_EPSILON ||
6886                fabs(o.blue.Y - data.blue_coefficient) > DBL_EPSILON))
6887                png_error(pp, "internal pngvalid cHRM coefficient error");
6888
6889             /* Generate a colour space encoding. */
6890             e.gamma = o.gamma; /* not used */
6891             e.red.X = API_cvt(rX);
6892             e.red.Y = API_cvt(rY);
6893             e.red.Z = API_cvt(rZ);
6894             e.green.X = API_cvt(gX);
6895             e.green.Y = API_cvt(gY);
6896             e.green.Z = API_cvt(gZ);
6897             e.blue.X = API_cvt(bX);
6898             e.blue.Y = API_cvt(bY);
6899             e.blue.Z = API_cvt(bZ);
6900
6901             /* This should match the original one from the png_modifier, within
6902              * the range permitted by the libpng fixed point representation.
6903              */
6904             maxe = 0;
6905             el = "-"; /* Set to element name with error */
6906
6907 #           define CHECK(col,x)\
6908             {\
6909                double err = fabs(o.col.x - e.col.x);\
6910                if (err > maxe)\
6911                {\
6912                   maxe = err;\
6913                   el = #col "(" #x ")";\
6914                }\
6915             }
6916
6917             CHECK(red,X)
6918             CHECK(red,Y)
6919             CHECK(red,Z)
6920             CHECK(green,X)
6921             CHECK(green,Y)
6922             CHECK(green,Z)
6923             CHECK(blue,X)
6924             CHECK(blue,Y)
6925             CHECK(blue,Z)
6926
6927             /* Here in both fixed and floating cases to check the values read
6928              * from the cHRm chunk.  PNG uses fixed point in the cHRM chunk, so
6929              * we can't expect better than +/-.5E-5 on the result, allow 1E-5.
6930              */
6931             if (maxe >= 1E-5)
6932             {
6933                size_t pos = 0;
6934                char buffer[256];
6935
6936                pos = safecat(buffer, sizeof buffer, pos, API_form);
6937                pos = safecat(buffer, sizeof buffer, pos, " cHRM ");
6938                pos = safecat(buffer, sizeof buffer, pos, el);
6939                pos = safecat(buffer, sizeof buffer, pos, " error: ");
6940                pos = safecatd(buffer, sizeof buffer, pos, maxe, 7);
6941                pos = safecat(buffer, sizeof buffer, pos, " ");
6942                /* Print the color space without the gamma value: */
6943                pos = safecat_color_encoding(buffer, sizeof buffer, pos, &o, 0);
6944                pos = safecat(buffer, sizeof buffer, pos, " -> ");
6945                pos = safecat_color_encoding(buffer, sizeof buffer, pos, &e, 0);
6946
6947                png_error(pp, buffer);
6948             }
6949          }
6950       }
6951 #  endif /* READ_cHRM */
6952
6953    this->next->set(this->next, that, pp, pi);
6954 }
6955
6956 static void
6957 image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
6958     image_pixel *that, png_const_structp pp,
6959     PNG_CONST transform_display *display)
6960 {
6961    if ((that->colour_type & PNG_COLOR_MASK_COLOR) != 0)
6962    {
6963       double gray, err;
6964
6965       if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6966          image_pixel_convert_PLTE(that);
6967
6968       /* Image now has RGB channels... */
6969 #  if DIGITIZE
6970       {
6971          PNG_CONST png_modifier *pm = display->pm;
6972          const unsigned int sample_depth = that->sample_depth;
6973          const unsigned int calc_depth = (pm->assume_16_bit_calculations ? 16 :
6974             sample_depth);
6975          const unsigned int gamma_depth = (sample_depth == 16 ? 16 :
6976             (pm->assume_16_bit_calculations ? PNG_MAX_GAMMA_8 : sample_depth));
6977          int isgray;
6978          double r, g, b;
6979          double rlo, rhi, glo, ghi, blo, bhi, graylo, grayhi;
6980
6981          /* Do this using interval arithmetic, otherwise it is too difficult to
6982           * handle the errors correctly.
6983           *
6984           * To handle the gamma correction work out the upper and lower bounds
6985           * of the digitized value.  Assume rounding here - normally the values
6986           * will be identical after this operation if there is only one
6987           * transform, feel free to delete the png_error checks on this below in
6988           * the future (this is just me trying to ensure it works!)
6989           */
6990          r = rlo = rhi = that->redf;
6991          rlo -= that->rede;
6992          rlo = digitize(rlo, calc_depth, 1/*round*/);
6993          rhi += that->rede;
6994          rhi = digitize(rhi, calc_depth, 1/*round*/);
6995
6996          g = glo = ghi = that->greenf;
6997          glo -= that->greene;
6998          glo = digitize(glo, calc_depth, 1/*round*/);
6999          ghi += that->greene;
7000          ghi = digitize(ghi, calc_depth, 1/*round*/);
7001
7002          b = blo = bhi = that->bluef;
7003          blo -= that->bluee;
7004          blo = digitize(blo, calc_depth, 1/*round*/);
7005          bhi += that->greene;
7006          bhi = digitize(bhi, calc_depth, 1/*round*/);
7007
7008          isgray = r==g && g==b;
7009
7010          if (data.gamma != 1)
7011          {
7012             PNG_CONST double power = 1/data.gamma;
7013             PNG_CONST double abse = calc_depth == 16 ? .5/65535 : .5/255;
7014
7015             /* 'abse' is the absolute error permitted in linear calculations. It
7016              * is used here to capture the error permitted in the handling
7017              * (undoing) of the gamma encoding.  Once again digitization occurs
7018              * to handle the upper and lower bounds of the values.  This is
7019              * where the real errors are introduced.
7020              */
7021             r = pow(r, power);
7022             rlo = digitize(pow(rlo, power)-abse, calc_depth, 1);
7023             rhi = digitize(pow(rhi, power)+abse, calc_depth, 1);
7024
7025             g = pow(g, power);
7026             glo = digitize(pow(glo, power)-abse, calc_depth, 1);
7027             ghi = digitize(pow(ghi, power)+abse, calc_depth, 1);
7028
7029             b = pow(b, power);
7030             blo = digitize(pow(blo, power)-abse, calc_depth, 1);
7031             bhi = digitize(pow(bhi, power)+abse, calc_depth, 1);
7032          }
7033
7034          /* Now calculate the actual gray values.  Although the error in the
7035           * coefficients depends on whether they were specified on the command
7036           * line (in which case truncation to 15 bits happened) or not (rounding
7037           * was used) the maxium error in an individual coefficient is always
7038           * 1/32768, because even in the rounding case the requirement that
7039           * coefficients add up to 32768 can cause a larger rounding error.
7040           *
7041           * The only time when rounding doesn't occur in 1.5.5 and later is when
7042           * the non-gamma code path is used for less than 16 bit data.
7043           */
7044          gray = r * data.red_coefficient + g * data.green_coefficient +
7045             b * data.blue_coefficient;
7046
7047          {
7048             PNG_CONST int do_round = data.gamma != 1 || calc_depth == 16;
7049             PNG_CONST double ce = 1. / 32768;
7050
7051             graylo = digitize(rlo * (data.red_coefficient-ce) +
7052                glo * (data.green_coefficient-ce) +
7053                blo * (data.blue_coefficient-ce), gamma_depth, do_round);
7054             if (graylo <= 0)
7055                graylo = 0;
7056
7057             grayhi = digitize(rhi * (data.red_coefficient+ce) +
7058                ghi * (data.green_coefficient+ce) +
7059                bhi * (data.blue_coefficient+ce), gamma_depth, do_round);
7060             if (grayhi >= 1)
7061                grayhi = 1;
7062          }
7063
7064          /* And invert the gamma. */
7065          if (data.gamma != 1)
7066          {
7067             PNG_CONST double power = data.gamma;
7068
7069             gray = pow(gray, power);
7070             graylo = digitize(pow(graylo, power), sample_depth, 1);
7071             grayhi = digitize(pow(grayhi, power), sample_depth, 1);
7072          }
7073
7074          /* Now the error can be calculated.
7075           *
7076           * If r==g==b because there is no overall gamma correction libpng
7077           * currently preserves the original value.
7078           */
7079          if (isgray)
7080             err = (that->rede + that->greene + that->bluee)/3;
7081
7082          else
7083          {
7084             err = fabs(grayhi-gray);
7085             if (fabs(gray - graylo) > err)
7086                err = fabs(graylo-gray);
7087
7088             /* Check that this worked: */
7089             if (err > pm->limit)
7090             {
7091                size_t pos = 0;
7092                char buffer[128];
7093
7094                pos = safecat(buffer, sizeof buffer, pos, "rgb_to_gray error ");
7095                pos = safecatd(buffer, sizeof buffer, pos, err, 6);
7096                pos = safecat(buffer, sizeof buffer, pos, " exceeds limit ");
7097                pos = safecatd(buffer, sizeof buffer, pos, pm->limit, 6);
7098                png_error(pp, buffer);
7099             }
7100          }
7101       }
7102 #  else  /* DIGITIZE */
7103       {
7104          double r = that->redf;
7105          double re = that->rede;
7106          double g = that->greenf;
7107          double ge = that->greene;
7108          double b = that->bluef;
7109          double be = that->bluee;
7110
7111          /* The true gray case involves no math. */
7112          if (r == g && r == b)
7113          {
7114             gray = r;
7115             err = re;
7116             if (err < ge) err = ge;
7117             if (err < be) err = be;
7118          }
7119
7120          else if (data.gamma == 1)
7121          {
7122             /* There is no need to do the conversions to and from linear space,
7123              * so the calculation should be a lot more accurate.  There is a
7124              * built in 1/32768 error in the coefficients because they only have
7125              * 15 bits and are adjusted to make sure they add up to 32768, so
7126              * the result may have an additional error up to 1/32768.  (Note
7127              * that adding the 1/32768 here avoids needing to increase the
7128              * global error limits to take this into account.)
7129              */
7130             gray = r * data.red_coefficient + g * data.green_coefficient +
7131                b * data.blue_coefficient;
7132             err = re * data.red_coefficient + ge * data.green_coefficient +
7133                be * data.blue_coefficient + 1./32768 + gray * 5 * DBL_EPSILON;
7134          }
7135
7136          else
7137          {
7138             /* The calculation happens in linear space, and this produces much
7139              * wider errors in the encoded space.  These are handled here by
7140              * factoring the errors in to the calculation.  There are two table
7141              * lookups in the calculation and each introduces a quantization
7142              * error defined by the table size.
7143              */
7144             PNG_CONST png_modifier *pm = display->pm;
7145             double in_qe = (that->sample_depth > 8 ? .5/65535 : .5/255);
7146             double out_qe = (that->sample_depth > 8 ? .5/65535 :
7147                (pm->assume_16_bit_calculations ? .5/(1<<PNG_MAX_GAMMA_8) :
7148                .5/255));
7149             double rhi, ghi, bhi, grayhi;
7150             double g1 = 1/data.gamma;
7151
7152             rhi = r + re + in_qe; if (rhi > 1) rhi = 1;
7153             r -= re + in_qe; if (r < 0) r = 0;
7154             ghi = g + ge + in_qe; if (ghi > 1) ghi = 1;
7155             g -= ge + in_qe; if (g < 0) g = 0;
7156             bhi = b + be + in_qe; if (bhi > 1) bhi = 1;
7157             b -= be + in_qe; if (b < 0) b = 0;
7158
7159             r = pow(r, g1)*(1-DBL_EPSILON); rhi = pow(rhi, g1)*(1+DBL_EPSILON);
7160             g = pow(g, g1)*(1-DBL_EPSILON); ghi = pow(ghi, g1)*(1+DBL_EPSILON);
7161             b = pow(b, g1)*(1-DBL_EPSILON); bhi = pow(bhi, g1)*(1+DBL_EPSILON);
7162
7163             /* Work out the lower and upper bounds for the gray value in the
7164              * encoded space, then work out an average and error.  Remove the
7165              * previously added input quantization error at this point.
7166              */
7167             gray = r * data.red_coefficient + g * data.green_coefficient +
7168                b * data.blue_coefficient - 1./32768 - out_qe;
7169             if (gray <= 0)
7170                gray = 0;
7171             else
7172             {
7173                gray *= (1 - 6 * DBL_EPSILON);
7174                gray = pow(gray, data.gamma) * (1-DBL_EPSILON);
7175             }
7176
7177             grayhi = rhi * data.red_coefficient + ghi * data.green_coefficient +
7178                bhi * data.blue_coefficient + 1./32768 + out_qe;
7179             grayhi *= (1 + 6 * DBL_EPSILON);
7180             if (grayhi >= 1)
7181                grayhi = 1;
7182             else
7183                grayhi = pow(grayhi, data.gamma) * (1+DBL_EPSILON);
7184
7185             err = (grayhi - gray) / 2;
7186             gray = (grayhi + gray) / 2;
7187
7188             if (err <= in_qe)
7189                err = gray * DBL_EPSILON;
7190
7191             else
7192                err -= in_qe;
7193
7194             /* Validate that the error is within limits (this has caused
7195              * problems before, it's much easier to detect them here.)
7196              */
7197             if (err > pm->limit)
7198             {
7199                size_t pos = 0;
7200                char buffer[128];
7201
7202                pos = safecat(buffer, sizeof buffer, pos, "rgb_to_gray error ");
7203                pos = safecatd(buffer, sizeof buffer, pos, err, 6);
7204                pos = safecat(buffer, sizeof buffer, pos, " exceeds limit ");
7205                pos = safecatd(buffer, sizeof buffer, pos, pm->limit, 6);
7206                png_error(pp, buffer);
7207             }
7208          }
7209       }
7210 #  endif /* !DIGITIZE */
7211
7212       that->bluef = that->greenf = that->redf = gray;
7213       that->bluee = that->greene = that->rede = err;
7214
7215       /* The sBIT is the minium of the three colour channel sBITs. */
7216       if (that->red_sBIT > that->green_sBIT)
7217          that->red_sBIT = that->green_sBIT;
7218       if (that->red_sBIT > that->blue_sBIT)
7219          that->red_sBIT = that->blue_sBIT;
7220       that->blue_sBIT = that->green_sBIT = that->red_sBIT;
7221
7222       /* And remove the colour bit in the type: */
7223       if (that->colour_type == PNG_COLOR_TYPE_RGB)
7224          that->colour_type = PNG_COLOR_TYPE_GRAY;
7225       else if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
7226          that->colour_type = PNG_COLOR_TYPE_GRAY_ALPHA;
7227    }
7228
7229    this->next->mod(this->next, that, pp, display);
7230 }
7231
7232 static int
7233 image_transform_png_set_rgb_to_gray_add(image_transform *this,
7234     PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
7235 {
7236    UNUSED(bit_depth)
7237
7238    this->next = *that;
7239    *that = this;
7240
7241    return (colour_type & PNG_COLOR_MASK_COLOR) != 0;
7242 }
7243
7244 #undef data
7245 IT(rgb_to_gray);
7246 #undef PT
7247 #define PT ITSTRUCT(rgb_to_gray)
7248 #undef image_transform_ini
7249 #define image_transform_ini image_transform_default_ini
7250 #endif /* PNG_READ_RGB_TO_GRAY_SUPPORTED */
7251
7252 #ifdef PNG_READ_BACKGROUND_SUPPORTED
7253 /* png_set_background(png_structp, png_const_color_16p background_color,
7254  *    int background_gamma_code, int need_expand, double background_gamma)
7255  * png_set_background_fixed(png_structp, png_const_color_16p background_color,
7256  *    int background_gamma_code, int need_expand,
7257  *    png_fixed_point background_gamma)
7258  *
7259  * This ignores the gamma (at present.)
7260 */
7261 #define data ITDATA(background)
7262 static image_pixel data;
7263
7264 static void
7265 image_transform_png_set_background_set(PNG_CONST image_transform *this,
7266     transform_display *that, png_structp pp, png_infop pi)
7267 {
7268    png_byte colour_type, bit_depth;
7269    png_byte random_bytes[8]; /* 8 bytes - 64 bits - the biggest pixel */
7270    int expand;
7271    png_color_16 back;
7272
7273    /* We need a background colour, because we don't know exactly what transforms
7274     * have been set we have to supply the colour in the original file format and
7275     * so we need to know what that is!  The background colour is stored in the
7276     * transform_display.
7277     */
7278    RANDOMIZE(random_bytes);
7279
7280    /* Read the random value, for colour type 3 the background colour is actually
7281     * expressed as a 24bit rgb, not an index.
7282     */
7283    colour_type = that->this.colour_type;
7284    if (colour_type == 3)
7285    {
7286       colour_type = PNG_COLOR_TYPE_RGB;
7287       bit_depth = 8;
7288       expand = 0; /* passing in an RGB not a pixel index */
7289    }
7290
7291    else
7292    {
7293       bit_depth = that->this.bit_depth;
7294       expand = 1;
7295    }
7296
7297    image_pixel_init(&data, random_bytes, colour_type,
7298       bit_depth, 0/*x*/, 0/*unused: palette*/);
7299
7300    /* Extract the background colour from this image_pixel, but make sure the
7301     * unused fields of 'back' are garbage.
7302     */
7303    RANDOMIZE(back);
7304
7305    if (colour_type & PNG_COLOR_MASK_COLOR)
7306    {
7307       back.red = (png_uint_16)data.red;
7308       back.green = (png_uint_16)data.green;
7309       back.blue = (png_uint_16)data.blue;
7310    }
7311
7312    else
7313       back.gray = (png_uint_16)data.red;
7314
7315 #  ifdef PNG_FLOATING_POINT_SUPPORTED
7316       png_set_background(pp, &back, PNG_BACKGROUND_GAMMA_FILE, expand, 0);
7317 #  else
7318       png_set_background_fixed(pp, &back, PNG_BACKGROUND_GAMMA_FILE, expand, 0);
7319 #  endif
7320
7321    this->next->set(this->next, that, pp, pi);
7322 }
7323
7324 static void
7325 image_transform_png_set_background_mod(PNG_CONST image_transform *this,
7326     image_pixel *that, png_const_structp pp,
7327     PNG_CONST transform_display *display)
7328 {
7329    /* Check for tRNS first: */
7330    if (that->have_tRNS && that->colour_type != PNG_COLOR_TYPE_PALETTE)
7331       image_pixel_add_alpha(that, &display->this);
7332
7333    /* This is only necessary if the alpha value is less than 1. */
7334    if (that->alphaf < 1)
7335    {
7336       /* Now we do the background calculation without any gamma correction. */
7337       if (that->alphaf <= 0)
7338       {
7339          that->redf = data.redf;
7340          that->greenf = data.greenf;
7341          that->bluef = data.bluef;
7342
7343          that->rede = data.rede;
7344          that->greene = data.greene;
7345          that->bluee = data.bluee;
7346
7347          that->red_sBIT= data.red_sBIT;
7348          that->green_sBIT= data.green_sBIT;
7349          that->blue_sBIT= data.blue_sBIT;
7350       }
7351
7352       else /* 0 < alpha < 1 */
7353       {
7354          double alf = 1 - that->alphaf;
7355
7356          that->redf = that->redf * that->alphaf + data.redf * alf;
7357          that->rede = that->rede * that->alphaf + data.rede * alf +
7358             DBL_EPSILON;
7359          that->greenf = that->greenf * that->alphaf + data.greenf * alf;
7360          that->greene = that->greene * that->alphaf + data.greene * alf +
7361             DBL_EPSILON;
7362          that->bluef = that->bluef * that->alphaf + data.bluef * alf;
7363          that->bluee = that->bluee * that->alphaf + data.bluee * alf +
7364             DBL_EPSILON;
7365       }
7366
7367       /* Remove the alpha type and set the alpha (not in that order.) */
7368       that->alphaf = 1;
7369       that->alphae = 0;
7370
7371       if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
7372          that->colour_type = PNG_COLOR_TYPE_RGB;
7373       else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7374          that->colour_type = PNG_COLOR_TYPE_GRAY;
7375       /* PNG_COLOR_TYPE_PALETTE is not changed */
7376    }
7377
7378    this->next->mod(this->next, that, pp, display);
7379 }
7380
7381 #define image_transform_png_set_background_add image_transform_default_add
7382
7383 #undef data
7384 IT(background);
7385 #undef PT
7386 #define PT ITSTRUCT(background)
7387 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
7388
7389 /* This may just be 'end' if all the transforms are disabled! */
7390 static image_transform *PNG_CONST image_transform_first = &PT;
7391
7392 static void
7393 transform_enable(PNG_CONST char *name)
7394 {
7395    /* Everything starts out enabled, so if we see an 'enable' disabled
7396     * everything else the first time round.
7397     */
7398    static int all_disabled = 0;
7399    int found_it = 0;
7400    image_transform *list = image_transform_first;
7401
7402    while (list != &image_transform_end)
7403    {
7404       if (strcmp(list->name, name) == 0)
7405       {
7406          list->enable = 1;
7407          found_it = 1;
7408       }
7409       else if (!all_disabled)
7410          list->enable = 0;
7411
7412       list = list->list;
7413    }
7414
7415    all_disabled = 1;
7416
7417    if (!found_it)
7418    {
7419       fprintf(stderr, "pngvalid: --transform-enable=%s: unknown transform\n",
7420          name);
7421       exit(99);
7422    }
7423 }
7424
7425 static void
7426 transform_disable(PNG_CONST char *name)
7427 {
7428    image_transform *list = image_transform_first;
7429
7430    while (list != &image_transform_end)
7431    {
7432       if (strcmp(list->name, name) == 0)
7433       {
7434          list->enable = 0;
7435          return;
7436       }
7437
7438       list = list->list;
7439    }
7440
7441    fprintf(stderr, "pngvalid: --transform-disable=%s: unknown transform\n",
7442       name);
7443    exit(99);
7444 }
7445
7446 static void
7447 image_transform_reset_count(void)
7448 {
7449    image_transform *next = image_transform_first;
7450    int count = 0;
7451
7452    while (next != &image_transform_end)
7453    {
7454       next->local_use = 0;
7455       next->next = 0;
7456       next = next->list;
7457       ++count;
7458    }
7459
7460    /* This can only happen if we every have more than 32 transforms (excluding
7461     * the end) in the list.
7462     */
7463    if (count > 32) abort();
7464 }
7465
7466 static int
7467 image_transform_test_counter(png_uint_32 counter, unsigned int max)
7468 {
7469    /* Test the list to see if there is any point contining, given a current
7470     * counter and a 'max' value.
7471     */
7472    image_transform *next = image_transform_first;
7473
7474    while (next != &image_transform_end)
7475    {
7476       /* For max 0 or 1 continue until the counter overflows: */
7477       counter >>= 1;
7478
7479       /* Continue if any entry hasn't reacked the max. */
7480       if (max > 1 && next->local_use < max)
7481          return 1;
7482       next = next->list;
7483    }
7484
7485    return max <= 1 && counter == 0;
7486 }
7487
7488 static png_uint_32
7489 image_transform_add(PNG_CONST image_transform **this, unsigned int max,
7490    png_uint_32 counter, char *name, size_t sizeof_name, size_t *pos,
7491    png_byte colour_type, png_byte bit_depth)
7492 {
7493    for (;;) /* until we manage to add something */
7494    {
7495       png_uint_32 mask;
7496       image_transform *list;
7497
7498       /* Find the next counter value, if the counter is zero this is the start
7499        * of the list.  This routine always returns the current counter (not the
7500        * next) so it returns 0 at the end and expects 0 at the beginning.
7501        */
7502       if (counter == 0) /* first time */
7503       {
7504          image_transform_reset_count();
7505          if (max <= 1)
7506             counter = 1;
7507          else
7508             counter = random_32();
7509       }
7510       else /* advance the counter */
7511       {
7512          switch (max)
7513          {
7514             case 0:  ++counter; break;
7515             case 1:  counter <<= 1; break;
7516             default: counter = random_32(); break;
7517          }
7518       }
7519
7520       /* Now add all these items, if possible */
7521       *this = &image_transform_end;
7522       list = image_transform_first;
7523       mask = 1;
7524
7525       /* Go through the whole list adding anything that the counter selects: */
7526       while (list != &image_transform_end)
7527       {
7528          if ((counter & mask) != 0 && list->enable &&
7529              (max == 0 || list->local_use < max))
7530          {
7531             /* Candidate to add: */
7532             if (list->add(list, this, colour_type, bit_depth) || max == 0)
7533             {
7534                /* Added, so add to the name too. */
7535                *pos = safecat(name, sizeof_name, *pos, " +");
7536                *pos = safecat(name, sizeof_name, *pos, list->name);
7537             }
7538
7539             else
7540             {
7541                /* Not useful and max>0, so remove it from *this: */
7542                *this = list->next;
7543                list->next = 0;
7544
7545                /* And, since we know it isn't useful, stop it being added again
7546                 * in this run:
7547                 */
7548                list->local_use = max;
7549             }
7550          }
7551
7552          mask <<= 1;
7553          list = list->list;
7554       }
7555
7556       /* Now if anything was added we have something to do. */
7557       if (*this != &image_transform_end)
7558          return counter;
7559
7560       /* Nothing added, but was there anything in there to add? */
7561       if (!image_transform_test_counter(counter, max))
7562          return 0;
7563    }
7564 }
7565
7566 #ifdef THIS_IS_THE_PROFORMA
7567 static void
7568 image_transform_png_set_@_set(PNG_CONST image_transform *this,
7569     transform_display *that, png_structp pp, png_infop pi)
7570 {
7571    png_set_@(pp);
7572    this->next->set(this->next, that, pp, pi);
7573 }
7574
7575 static void
7576 image_transform_png_set_@_mod(PNG_CONST image_transform *this,
7577     image_pixel *that, png_const_structp pp,
7578     PNG_CONST transform_display *display)
7579 {
7580    this->next->mod(this->next, that, pp, display);
7581 }
7582
7583 static int
7584 image_transform_png_set_@_add(image_transform *this,
7585     PNG_CONST image_transform **that, char *name, size_t sizeof_name,
7586     size_t *pos, png_byte colour_type, png_byte bit_depth)
7587 {
7588    this->next = *that;
7589    *that = this;
7590
7591    *pos = safecat(name, sizeof_name, *pos, " +@");
7592
7593    return 1;
7594 }
7595
7596 IT(@);
7597 #endif
7598
7599 /* png_set_quantize(png_structp, png_colorp palette, int num_palette,
7600  *    int maximum_colors, png_const_uint_16p histogram, int full_quantize)
7601  *
7602  * Very difficult to validate this!
7603  */
7604 /*NOTE: TBD NYI */
7605
7606 /* The data layout transforms are handled by swapping our own channel data,
7607  * necessarily these need to happen at the end of the transform list because the
7608  * semantic of the channels changes after these are executed.  Some of these,
7609  * like set_shift and set_packing, can't be done at present because they change
7610  * the layout of the data at the sub-sample level so sample() won't get the
7611  * right answer.
7612  */
7613 /* png_set_invert_alpha */
7614 /*NOTE: TBD NYI */
7615
7616 /* png_set_bgr */
7617 /*NOTE: TBD NYI */
7618
7619 /* png_set_swap_alpha */
7620 /*NOTE: TBD NYI */
7621
7622 /* png_set_swap */
7623 /*NOTE: TBD NYI */
7624
7625 /* png_set_filler, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7626 /*NOTE: TBD NYI */
7627
7628 /* png_set_add_alpha, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7629 /*NOTE: TBD NYI */
7630
7631 /* png_set_packing */
7632 /*NOTE: TBD NYI */
7633
7634 /* png_set_packswap */
7635 /*NOTE: TBD NYI */
7636
7637 /* png_set_invert_mono */
7638 /*NOTE: TBD NYI */
7639
7640 /* png_set_shift(png_structp, png_const_color_8p true_bits) */
7641 /*NOTE: TBD NYI */
7642
7643 static void
7644 perform_transform_test(png_modifier *pm)
7645 {
7646    png_byte colour_type = 0;
7647    png_byte bit_depth = 0;
7648    unsigned int palette_number = 0;
7649
7650    while (next_format(&colour_type, &bit_depth, &palette_number, 0))
7651    {
7652       png_uint_32 counter = 0;
7653       size_t base_pos;
7654       char name[64];
7655
7656       base_pos = safecat(name, sizeof name, 0, "transform:");
7657
7658       for (;;)
7659       {
7660          size_t pos = base_pos;
7661          PNG_CONST image_transform *list = 0;
7662
7663          /* 'max' is currently hardwired to '1'; this should be settable on the
7664           * command line.
7665           */
7666          counter = image_transform_add(&list, 1/*max*/, counter,
7667             name, sizeof name, &pos, colour_type, bit_depth);
7668
7669          if (counter == 0)
7670             break;
7671
7672          /* The command line can change this to checking interlaced images. */
7673          do
7674          {
7675             pm->repeat = 0;
7676             transform_test(pm, FILEID(colour_type, bit_depth, palette_number,
7677                pm->interlace_type, 0, 0, 0), list, name);
7678
7679             if (fail(pm))
7680                return;
7681          }
7682          while (pm->repeat);
7683       }
7684    }
7685 }
7686 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
7687
7688 /********************************* GAMMA TESTS ********************************/
7689 #ifdef PNG_READ_GAMMA_SUPPORTED
7690 /* Reader callbacks and implementations, where they differ from the standard
7691  * ones.
7692  */
7693 typedef struct gamma_display
7694 {
7695    standard_display this;
7696
7697    /* Parameters */
7698    png_modifier*    pm;
7699    double           file_gamma;
7700    double           screen_gamma;
7701    double           background_gamma;
7702    png_byte         sbit;
7703    int              threshold_test;
7704    int              use_input_precision;
7705    int              scale16;
7706    int              expand16;
7707    int              do_background;
7708    png_color_16     background_color;
7709
7710    /* Local variables */
7711    double       maxerrout;
7712    double       maxerrpc;
7713    double       maxerrabs;
7714 } gamma_display;
7715
7716 #define ALPHA_MODE_OFFSET 4
7717
7718 static void
7719 gamma_display_init(gamma_display *dp, png_modifier *pm, png_uint_32 id,
7720     double file_gamma, double screen_gamma, png_byte sbit, int threshold_test,
7721     int use_input_precision, int scale16, int expand16,
7722     int do_background, PNG_CONST png_color_16 *pointer_to_the_background_color,
7723     double background_gamma)
7724 {
7725    /* Standard fields */
7726    standard_display_init(&dp->this, &pm->this, id, 0/*do_interlace*/,
7727       pm->use_update_info);
7728
7729    /* Parameter fields */
7730    dp->pm = pm;
7731    dp->file_gamma = file_gamma;
7732    dp->screen_gamma = screen_gamma;
7733    dp->background_gamma = background_gamma;
7734    dp->sbit = sbit;
7735    dp->threshold_test = threshold_test;
7736    dp->use_input_precision = use_input_precision;
7737    dp->scale16 = scale16;
7738    dp->expand16 = expand16;
7739    dp->do_background = do_background;
7740    if (do_background && pointer_to_the_background_color != 0)
7741       dp->background_color = *pointer_to_the_background_color;
7742    else
7743       memset(&dp->background_color, 0, sizeof dp->background_color);
7744
7745    /* Local variable fields */
7746    dp->maxerrout = dp->maxerrpc = dp->maxerrabs = 0;
7747 }
7748
7749 static void
7750 gamma_info_imp(gamma_display *dp, png_structp pp, png_infop pi)
7751 {
7752    /* Reuse the standard stuff as appropriate. */
7753    standard_info_part1(&dp->this, pp, pi);
7754
7755    /* If requested strip 16 to 8 bits - this is handled automagically below
7756     * because the output bit depth is read from the library.  Note that there
7757     * are interactions with sBIT but, internally, libpng makes sbit at most
7758     * PNG_MAX_GAMMA_8 when doing the following.
7759     */
7760    if (dp->scale16)
7761 #     ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
7762          png_set_scale_16(pp);
7763 #     else
7764          /* The following works both in 1.5.4 and earlier versions: */
7765 #        ifdef PNG_READ_16_TO_8_SUPPORTED
7766             png_set_strip_16(pp);
7767 #        else
7768             png_error(pp, "scale16 (16 to 8 bit conversion) not supported");
7769 #        endif
7770 #     endif
7771
7772    if (dp->expand16)
7773 #     ifdef PNG_READ_EXPAND_16_SUPPORTED
7774          png_set_expand_16(pp);
7775 #     else
7776          png_error(pp, "expand16 (8 to 16 bit conversion) not supported");
7777 #     endif
7778
7779    if (dp->do_background >= ALPHA_MODE_OFFSET)
7780    {
7781 #     ifdef PNG_READ_ALPHA_MODE_SUPPORTED
7782       {
7783          /* This tests the alpha mode handling, if supported. */
7784          int mode = dp->do_background - ALPHA_MODE_OFFSET;
7785
7786          /* The gamma value is the output gamma, and is in the standard,
7787           * non-inverted, represenation.  It provides a default for the PNG file
7788           * gamma, but since the file has a gAMA chunk this does not matter.
7789           */
7790          PNG_CONST double sg = dp->screen_gamma;
7791 #        ifndef PNG_FLOATING_POINT_SUPPORTED
7792             PNG_CONST png_fixed_point g = fix(sg);
7793 #        endif
7794
7795 #        ifdef PNG_FLOATING_POINT_SUPPORTED
7796             png_set_alpha_mode(pp, mode, sg);
7797 #        else
7798             png_set_alpha_mode_fixed(pp, mode, g);
7799 #        endif
7800
7801          /* However, for the standard Porter-Duff algorithm the output defaults
7802           * to be linear, so if the test requires non-linear output it must be
7803           * corrected here.
7804           */
7805          if (mode == PNG_ALPHA_STANDARD && sg != 1)
7806          {
7807 #           ifdef PNG_FLOATING_POINT_SUPPORTED
7808                png_set_gamma(pp, sg, dp->file_gamma);
7809 #           else
7810                png_fixed_point f = fix(dp->file_gamma);
7811                png_set_gamma_fixed(pp, g, f);
7812 #           endif
7813          }
7814       }
7815 #     else
7816          png_error(pp, "alpha mode handling not supported");
7817 #     endif
7818    }
7819
7820    else
7821    {
7822       /* Set up gamma processing. */
7823 #     ifdef PNG_FLOATING_POINT_SUPPORTED
7824          png_set_gamma(pp, dp->screen_gamma, dp->file_gamma);
7825 #     else
7826       {
7827          png_fixed_point s = fix(dp->screen_gamma);
7828          png_fixed_point f = fix(dp->file_gamma);
7829          png_set_gamma_fixed(pp, s, f);
7830       }
7831 #     endif
7832
7833       if (dp->do_background)
7834       {
7835 #     ifdef PNG_READ_BACKGROUND_SUPPORTED
7836          /* NOTE: this assumes the caller provided the correct background gamma!
7837           */
7838          PNG_CONST double bg = dp->background_gamma;
7839 #        ifndef PNG_FLOATING_POINT_SUPPORTED
7840             PNG_CONST png_fixed_point g = fix(bg);
7841 #        endif
7842
7843 #        ifdef PNG_FLOATING_POINT_SUPPORTED
7844             png_set_background(pp, &dp->background_color, dp->do_background,
7845                0/*need_expand*/, bg);
7846 #        else
7847             png_set_background_fixed(pp, &dp->background_color,
7848                dp->do_background, 0/*need_expand*/, g);
7849 #        endif
7850 #     else
7851          png_error(pp, "png_set_background not supported");
7852 #     endif
7853       }
7854    }
7855
7856    {
7857       int i = dp->this.use_update_info;
7858       /* Always do one call, even if use_update_info is 0. */
7859       do
7860          png_read_update_info(pp, pi);
7861       while (--i > 0);
7862    }
7863
7864    /* Now we may get a different cbRow: */
7865    standard_info_part2(&dp->this, pp, pi, 1 /*images*/);
7866 }
7867
7868 static void PNGCBAPI
7869 gamma_info(png_structp pp, png_infop pi)
7870 {
7871    gamma_info_imp(voidcast(gamma_display*, png_get_progressive_ptr(pp)), pp,
7872       pi);
7873 }
7874
7875 /* Validate a single component value - the routine gets the input and output
7876  * sample values as unscaled PNG component values along with a cache of all the
7877  * information required to validate the values.
7878  */
7879 typedef struct validate_info
7880 {
7881    png_const_structp  pp;
7882    gamma_display *dp;
7883    png_byte sbit;
7884    int use_input_precision;
7885    int do_background;
7886    int scale16;
7887    unsigned int sbit_max;
7888    unsigned int isbit_shift;
7889    unsigned int outmax;
7890
7891    double gamma_correction; /* Overall correction required. */
7892    double file_inverse;     /* Inverse of file gamma. */
7893    double screen_gamma;
7894    double screen_inverse;   /* Inverse of screen gamma. */
7895
7896    double background_red;   /* Linear background value, red or gray. */
7897    double background_green;
7898    double background_blue;
7899
7900    double maxabs;
7901    double maxpc;
7902    double maxcalc;
7903    double maxout;
7904    double maxout_total;     /* Total including quantization error */
7905    double outlog;
7906    int    outquant;
7907 }
7908 validate_info;
7909
7910 static void
7911 init_validate_info(validate_info *vi, gamma_display *dp, png_const_structp pp,
7912     int in_depth, int out_depth)
7913 {
7914    PNG_CONST unsigned int outmax = (1U<<out_depth)-1;
7915
7916    vi->pp = pp;
7917    vi->dp = dp;
7918
7919    if (dp->sbit > 0 && dp->sbit < in_depth)
7920    {
7921       vi->sbit = dp->sbit;
7922       vi->isbit_shift = in_depth - dp->sbit;
7923    }
7924
7925    else
7926    {
7927       vi->sbit = (png_byte)in_depth;
7928       vi->isbit_shift = 0;
7929    }
7930
7931    vi->sbit_max = (1U << vi->sbit)-1;
7932
7933    /* This mimics the libpng threshold test, '0' is used to prevent gamma
7934     * correction in the validation test.
7935     */
7936    vi->screen_gamma = dp->screen_gamma;
7937    if (fabs(vi->screen_gamma-1) < PNG_GAMMA_THRESHOLD)
7938       vi->screen_gamma = vi->screen_inverse = 0;
7939    else
7940       vi->screen_inverse = 1/vi->screen_gamma;
7941
7942    vi->use_input_precision = dp->use_input_precision;
7943    vi->outmax = outmax;
7944    vi->maxabs = abserr(dp->pm, in_depth, out_depth);
7945    vi->maxpc = pcerr(dp->pm, in_depth, out_depth);
7946    vi->maxcalc = calcerr(dp->pm, in_depth, out_depth);
7947    vi->maxout = outerr(dp->pm, in_depth, out_depth);
7948    vi->outquant = output_quantization_factor(dp->pm, in_depth, out_depth);
7949    vi->maxout_total = vi->maxout + vi->outquant * .5;
7950    vi->outlog = outlog(dp->pm, in_depth, out_depth);
7951
7952    if ((dp->this.colour_type & PNG_COLOR_MASK_ALPHA) != 0 ||
7953       (dp->this.colour_type == 3 && dp->this.is_transparent))
7954    {
7955       vi->do_background = dp->do_background;
7956
7957       if (vi->do_background != 0)
7958       {
7959          PNG_CONST double bg_inverse = 1/dp->background_gamma;
7960          double r, g, b;
7961
7962          /* Caller must at least put the gray value into the red channel */
7963          r = dp->background_color.red; r /= outmax;
7964          g = dp->background_color.green; g /= outmax;
7965          b = dp->background_color.blue; b /= outmax;
7966
7967 #     if 0
7968          /* libpng doesn't do this optimization, if we do pngvalid will fail.
7969           */
7970          if (fabs(bg_inverse-1) >= PNG_GAMMA_THRESHOLD)
7971 #     endif
7972          {
7973             r = pow(r, bg_inverse);
7974             g = pow(g, bg_inverse);
7975             b = pow(b, bg_inverse);
7976          }
7977
7978          vi->background_red = r;
7979          vi->background_green = g;
7980          vi->background_blue = b;
7981       }
7982    }
7983    else
7984       vi->do_background = 0;
7985
7986    if (vi->do_background == 0)
7987       vi->background_red = vi->background_green = vi->background_blue = 0;
7988
7989    vi->gamma_correction = 1/(dp->file_gamma*dp->screen_gamma);
7990    if (fabs(vi->gamma_correction-1) < PNG_GAMMA_THRESHOLD)
7991       vi->gamma_correction = 0;
7992
7993    vi->file_inverse = 1/dp->file_gamma;
7994    if (fabs(vi->file_inverse-1) < PNG_GAMMA_THRESHOLD)
7995       vi->file_inverse = 0;
7996
7997    vi->scale16 = dp->scale16;
7998 }
7999
8000 /* This function handles composition of a single non-alpha component.  The
8001  * argument is the input sample value, in the range 0..1, and the alpha value.
8002  * The result is the composed, linear, input sample.  If alpha is less than zero
8003  * this is the alpha component and the function should not be called!
8004  */
8005 static double
8006 gamma_component_compose(int do_background, double input_sample, double alpha,
8007    double background, int *compose)
8008 {
8009    switch (do_background)
8010    {
8011 #ifdef PNG_READ_BACKGROUND_SUPPORTED
8012       case PNG_BACKGROUND_GAMMA_SCREEN:
8013       case PNG_BACKGROUND_GAMMA_FILE:
8014       case PNG_BACKGROUND_GAMMA_UNIQUE:
8015          /* Standard PNG background processing. */
8016          if (alpha < 1)
8017          {
8018             if (alpha > 0)
8019             {
8020                input_sample = input_sample * alpha + background * (1-alpha);
8021                if (compose != NULL)
8022                   *compose = 1;
8023             }
8024
8025             else
8026                input_sample = background;
8027          }
8028          break;
8029 #endif
8030
8031 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8032       case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
8033       case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
8034          /* The components are premultiplied in either case and the output is
8035           * gamma encoded (to get standard Porter-Duff we expect the output
8036           * gamma to be set to 1.0!)
8037           */
8038       case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
8039          /* The optimization is that the partial-alpha entries are linear
8040           * while the opaque pixels are gamma encoded, but this only affects the
8041           * output encoding.
8042           */
8043          if (alpha < 1)
8044          {
8045             if (alpha > 0)
8046             {
8047                input_sample *= alpha;
8048                if (compose != NULL)
8049                   *compose = 1;
8050             }
8051
8052             else
8053                input_sample = 0;
8054          }
8055          break;
8056 #endif
8057
8058       default:
8059          /* Standard cases where no compositing is done (so the component
8060           * value is already correct.)
8061           */
8062          UNUSED(alpha)
8063          UNUSED(background)
8064          UNUSED(compose)
8065          break;
8066    }
8067
8068    return input_sample;
8069 }
8070
8071 /* This API returns the encoded *input* component, in the range 0..1 */
8072 static double
8073 gamma_component_validate(PNG_CONST char *name, PNG_CONST validate_info *vi,
8074     PNG_CONST unsigned int id, PNG_CONST unsigned int od,
8075     PNG_CONST double alpha /* <0 for the alpha channel itself */,
8076     PNG_CONST double background /* component background value */)
8077 {
8078    PNG_CONST unsigned int isbit = id >> vi->isbit_shift;
8079    PNG_CONST unsigned int sbit_max = vi->sbit_max;
8080    PNG_CONST unsigned int outmax = vi->outmax;
8081    PNG_CONST int do_background = vi->do_background;
8082
8083    double i;
8084
8085    /* First check on the 'perfect' result obtained from the digitized input
8086     * value, id, and compare this against the actual digitized result, 'od'.
8087     * 'i' is the input result in the range 0..1:
8088     */
8089    i = isbit; i /= sbit_max;
8090
8091    /* Check for the fast route: if we don't do any background composition or if
8092     * this is the alpha channel ('alpha' < 0) or if the pixel is opaque then
8093     * just use the gamma_correction field to correct to the final output gamma.
8094     */
8095    if (alpha == 1 /* opaque pixel component */ || !do_background
8096 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8097       || do_background == ALPHA_MODE_OFFSET + PNG_ALPHA_PNG
8098 #endif
8099       || (alpha < 0 /* alpha channel */
8100 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8101       && do_background != ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN
8102 #endif
8103       ))
8104    {
8105       /* Then get the gamma corrected version of 'i' and compare to 'od', any
8106        * error less than .5 is insignificant - just quantization of the output
8107        * value to the nearest digital value (nevertheless the error is still
8108        * recorded - it's interesting ;-)
8109        */
8110       double encoded_sample = i;
8111       double encoded_error;
8112
8113       /* alpha less than 0 indicates the alpha channel, which is always linear
8114        */
8115       if (alpha >= 0 && vi->gamma_correction > 0)
8116          encoded_sample = pow(encoded_sample, vi->gamma_correction);
8117       encoded_sample *= outmax;
8118
8119       encoded_error = fabs(od-encoded_sample);
8120
8121       if (encoded_error > vi->dp->maxerrout)
8122          vi->dp->maxerrout = encoded_error;
8123
8124       if (encoded_error < vi->maxout_total && encoded_error < vi->outlog)
8125          return i;
8126    }
8127
8128    /* The slow route - attempt to do linear calculations. */
8129    /* There may be an error, or background processing is required, so calculate
8130     * the actual sample values - unencoded light intensity values.  Note that in
8131     * practice these are not completely unencoded because they include a
8132     * 'viewing correction' to decrease or (normally) increase the perceptual
8133     * contrast of the image.  There's nothing we can do about this - we don't
8134     * know what it is - so assume the unencoded value is perceptually linear.
8135     */
8136    {
8137       double input_sample = i; /* In range 0..1 */
8138       double output, error, encoded_sample, encoded_error;
8139       double es_lo, es_hi;
8140       int compose = 0;           /* Set to one if composition done */
8141       int output_is_encoded;     /* Set if encoded to screen gamma */
8142       int log_max_error = 1;     /* Check maximum error values */
8143       png_const_charp pass = 0;  /* Reason test passes (or 0 for fail) */
8144
8145       /* Convert to linear light (with the above caveat.)  The alpha channel is
8146        * already linear.
8147        */
8148       if (alpha >= 0)
8149       {
8150          int tcompose;
8151
8152          if (vi->file_inverse > 0)
8153             input_sample = pow(input_sample, vi->file_inverse);
8154
8155          /* Handle the compose processing: */
8156          tcompose = 0;
8157          input_sample = gamma_component_compose(do_background, input_sample,
8158             alpha, background, &tcompose);
8159
8160          if (tcompose)
8161             compose = 1;
8162       }
8163
8164       /* And similarly for the output value, but we need to check the background
8165        * handling to linearize it correctly.
8166        */
8167       output = od;
8168       output /= outmax;
8169
8170       output_is_encoded = vi->screen_gamma > 0;
8171
8172       if (alpha < 0) /* The alpha channel */
8173       {
8174 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8175          if (do_background != ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN)
8176 #endif
8177          {
8178             /* In all other cases the output alpha channel is linear already,
8179              * don't log errors here, they are much larger in linear data.
8180              */
8181             output_is_encoded = 0;
8182             log_max_error = 0;
8183          }
8184       }
8185
8186 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8187       else /* A component */
8188       {
8189          if (do_background == ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED &&
8190             alpha < 1) /* the optimized case - linear output */
8191          {
8192             if (alpha > 0) log_max_error = 0;
8193             output_is_encoded = 0;
8194          }
8195       }
8196 #endif
8197
8198       if (output_is_encoded)
8199          output = pow(output, vi->screen_gamma);
8200
8201       /* Calculate (or recalculate) the encoded_sample value and repeat the
8202        * check above (unnecessary if we took the fast route, but harmless.)
8203        */
8204       encoded_sample = input_sample;
8205       if (output_is_encoded)
8206          encoded_sample = pow(encoded_sample, vi->screen_inverse);
8207       encoded_sample *= outmax;
8208
8209       encoded_error = fabs(od-encoded_sample);
8210
8211       /* Don't log errors in the alpha channel, or the 'optimized' case,
8212        * neither are significant to the overall perception.
8213        */
8214       if (log_max_error && encoded_error > vi->dp->maxerrout)
8215          vi->dp->maxerrout = encoded_error;
8216
8217       if (encoded_error < vi->maxout_total)
8218       {
8219          if (encoded_error < vi->outlog)
8220             return i;
8221
8222          /* Test passed but error is bigger than the log limit, record why the
8223           * test passed:
8224           */
8225          pass = "less than maxout:\n";
8226       }
8227
8228       /* i: the original input value in the range 0..1
8229        *
8230        * pngvalid calculations:
8231        *  input_sample: linear result; i linearized and composed, range 0..1
8232        *  encoded_sample: encoded result; input_sample scaled to ouput bit depth
8233        *
8234        * libpng calculations:
8235        *  output: linear result; od scaled to 0..1 and linearized
8236        *  od: encoded result from libpng
8237        */
8238
8239       /* Now we have the numbers for real errors, both absolute values as as a
8240        * percentage of the correct value (output):
8241        */
8242       error = fabs(input_sample-output);
8243
8244       if (log_max_error && error > vi->dp->maxerrabs)
8245          vi->dp->maxerrabs = error;
8246
8247       /* The following is an attempt to ignore the tendency of quantization to
8248        * dominate the percentage errors for lower result values:
8249        */
8250       if (log_max_error && input_sample > .5)
8251       {
8252          double percentage_error = error/input_sample;
8253          if (percentage_error > vi->dp->maxerrpc)
8254             vi->dp->maxerrpc = percentage_error;
8255       }
8256
8257       /* Now calculate the digitization limits for 'encoded_sample' using the
8258        * 'max' values.  Note that maxout is in the encoded space but maxpc and
8259        * maxabs are in linear light space.
8260        *
8261        * First find the maximum error in linear light space, range 0..1:
8262        */
8263       {
8264          double tmp = input_sample * vi->maxpc;
8265          if (tmp < vi->maxabs) tmp = vi->maxabs;
8266          /* If 'compose' is true the composition was done in linear space using
8267           * integer arithmetic.  This introduces an extra error of +/- 0.5 (at
8268           * least) in the integer space used.  'maxcalc' records this, taking
8269           * into account the possibility that even for 16 bit output 8 bit space
8270           * may have been used.
8271           */
8272          if (compose && tmp < vi->maxcalc) tmp = vi->maxcalc;
8273
8274          /* The 'maxout' value refers to the encoded result, to compare with
8275           * this encode input_sample adjusted by the maximum error (tmp) above.
8276           */
8277          es_lo = encoded_sample - vi->maxout;
8278
8279          if (es_lo > 0 && input_sample-tmp > 0)
8280          {
8281             double low_value = input_sample-tmp;
8282             if (output_is_encoded)
8283                low_value = pow(low_value, vi->screen_inverse);
8284             low_value *= outmax;
8285             if (low_value < es_lo) es_lo = low_value;
8286
8287             /* Quantize this appropriately: */
8288             es_lo = ceil(es_lo / vi->outquant - .5) * vi->outquant;
8289          }
8290
8291          else
8292             es_lo = 0;
8293
8294          es_hi = encoded_sample + vi->maxout;
8295
8296          if (es_hi < outmax && input_sample+tmp < 1)
8297          {
8298             double high_value = input_sample+tmp;
8299             if (output_is_encoded)
8300                high_value = pow(high_value, vi->screen_inverse);
8301             high_value *= outmax;
8302             if (high_value > es_hi) es_hi = high_value;
8303
8304             es_hi = floor(es_hi / vi->outquant + .5) * vi->outquant;
8305          }
8306
8307          else
8308             es_hi = outmax;
8309       }
8310
8311       /* The primary test is that the final encoded value returned by the
8312        * library should be between the two limits (inclusive) that were
8313        * calculated above.
8314        */
8315       if (od >= es_lo && od <= es_hi)
8316       {
8317          /* The value passes, but we may need to log the information anyway. */
8318          if (encoded_error < vi->outlog)
8319             return i;
8320
8321          if (pass == 0)
8322             pass = "within digitization limits:\n";
8323       }
8324
8325       {
8326          /* There has been an error in processing, or we need to log this
8327           * value.
8328           */
8329          double is_lo, is_hi;
8330
8331          /* pass is set at this point if either of the tests above would have
8332           * passed.  Don't do these additional tests here - just log the
8333           * original [es_lo..es_hi] values.
8334           */
8335          if (pass == 0 && vi->use_input_precision && vi->dp->sbit)
8336          {
8337             /* Ok, something is wrong - this actually happens in current libpng
8338              * 16-to-8 processing.  Assume that the input value (id, adjusted
8339              * for sbit) can be anywhere between value-.5 and value+.5 - quite a
8340              * large range if sbit is low.
8341              *
8342              * NOTE: at present because the libpng gamma table stuff has been
8343              * changed to use a rounding algorithm to correct errors in 8-bit
8344              * calculations the precise sbit calculation (a shift) has been
8345              * lost.  This can result in up to a +/-1 error in the presence of
8346              * an sbit less than the bit depth.
8347              */
8348 #           if PNG_LIBPNG_VER < 10700
8349 #              define SBIT_ERROR .5
8350 #           else
8351 #              define SBIT_ERROR 1.
8352 #           endif
8353             double tmp = (isbit - SBIT_ERROR)/sbit_max;
8354
8355             if (tmp <= 0)
8356                tmp = 0;
8357
8358             else if (alpha >= 0 && vi->file_inverse > 0 && tmp < 1)
8359                tmp = pow(tmp, vi->file_inverse);
8360
8361             tmp = gamma_component_compose(do_background, tmp, alpha, background,
8362                NULL);
8363
8364             if (output_is_encoded && tmp > 0 && tmp < 1)
8365                tmp = pow(tmp, vi->screen_inverse);
8366
8367             is_lo = ceil(outmax * tmp - vi->maxout_total);
8368
8369             if (is_lo < 0)
8370                is_lo = 0;
8371
8372             tmp = (isbit + SBIT_ERROR)/sbit_max;
8373
8374             if (tmp >= 1)
8375                tmp = 1;
8376
8377             else if (alpha >= 0 && vi->file_inverse > 0 && tmp < 1)
8378                tmp = pow(tmp, vi->file_inverse);
8379
8380             tmp = gamma_component_compose(do_background, tmp, alpha, background,
8381                NULL);
8382
8383             if (output_is_encoded && tmp > 0 && tmp < 1)
8384                tmp = pow(tmp, vi->screen_inverse);
8385
8386             is_hi = floor(outmax * tmp + vi->maxout_total);
8387
8388             if (is_hi > outmax)
8389                is_hi = outmax;
8390
8391             if (!(od < is_lo || od > is_hi))
8392             {
8393                if (encoded_error < vi->outlog)
8394                   return i;
8395
8396                pass = "within input precision limits:\n";
8397             }
8398
8399             /* One last chance.  If this is an alpha channel and the 16to8
8400              * option has been used and 'inaccurate' scaling is used then the
8401              * bit reduction is obtained by simply using the top 8 bits of the
8402              * value.
8403              *
8404              * This is only done for older libpng versions when the 'inaccurate'
8405              * (chop) method of scaling was used.
8406              */
8407 #           ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
8408 #              if PNG_LIBPNG_VER < 10504
8409                   /* This may be required for other components in the future,
8410                    * but at present the presence of gamma correction effectively
8411                    * prevents the errors in the component scaling (I don't quite
8412                    * understand why, but since it's better this way I care not
8413                    * to ask, JB 20110419.)
8414                    */
8415                   if (pass == 0 && alpha < 0 && vi->scale16 && vi->sbit > 8 &&
8416                      vi->sbit + vi->isbit_shift == 16)
8417                   {
8418                      tmp = ((id >> 8) - .5)/255;
8419
8420                      if (tmp > 0)
8421                      {
8422                         is_lo = ceil(outmax * tmp - vi->maxout_total);
8423                         if (is_lo < 0) is_lo = 0;
8424                      }
8425
8426                      else
8427                         is_lo = 0;
8428
8429                      tmp = ((id >> 8) + .5)/255;
8430
8431                      if (tmp < 1)
8432                      {
8433                         is_hi = floor(outmax * tmp + vi->maxout_total);
8434                         if (is_hi > outmax) is_hi = outmax;
8435                      }
8436
8437                      else
8438                         is_hi = outmax;
8439
8440                      if (!(od < is_lo || od > is_hi))
8441                      {
8442                         if (encoded_error < vi->outlog)
8443                            return i;
8444
8445                         pass = "within 8 bit limits:\n";
8446                      }
8447                   }
8448 #              endif
8449 #           endif
8450          }
8451          else /* !use_input_precision */
8452             is_lo = es_lo, is_hi = es_hi;
8453
8454          /* Attempt to output a meaningful error/warning message: the message
8455           * output depends on the background/composite operation being performed
8456           * because this changes what parameters were actually used above.
8457           */
8458          {
8459             size_t pos = 0;
8460             /* Need either 1/255 or 1/65535 precision here; 3 or 6 decimal
8461              * places.  Just use outmax to work out which.
8462              */
8463             int precision = (outmax >= 1000 ? 6 : 3);
8464             int use_input=1, use_background=0, do_compose=0;
8465             char msg[256];
8466
8467             if (pass != 0)
8468                pos = safecat(msg, sizeof msg, pos, "\n\t");
8469
8470             /* Set up the various flags, the output_is_encoded flag above
8471              * is also used below.  do_compose is just a double check.
8472              */
8473             switch (do_background)
8474             {
8475 #           ifdef PNG_READ_BACKGROUND_SUPPORTED
8476                case PNG_BACKGROUND_GAMMA_SCREEN:
8477                case PNG_BACKGROUND_GAMMA_FILE:
8478                case PNG_BACKGROUND_GAMMA_UNIQUE:
8479                   use_background = (alpha >= 0 && alpha < 1);
8480                   /*FALL THROUGH*/
8481 #           endif
8482 #           ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8483                case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
8484                case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
8485                case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
8486 #           endif /* ALPHA_MODE_SUPPORTED */
8487                do_compose = (alpha > 0 && alpha < 1);
8488                use_input = (alpha != 0);
8489                break;
8490
8491             default:
8492                break;
8493             }
8494
8495             /* Check the 'compose' flag */
8496             if (compose != do_compose)
8497                png_error(vi->pp, "internal error (compose)");
8498
8499             /* 'name' is the component name */
8500             pos = safecat(msg, sizeof msg, pos, name);
8501             pos = safecat(msg, sizeof msg, pos, "(");
8502             pos = safecatn(msg, sizeof msg, pos, id);
8503             if (use_input || pass != 0/*logging*/)
8504             {
8505                if (isbit != id)
8506                {
8507                   /* sBIT has reduced the precision of the input: */
8508                   pos = safecat(msg, sizeof msg, pos, ", sbit(");
8509                   pos = safecatn(msg, sizeof msg, pos, vi->sbit);
8510                   pos = safecat(msg, sizeof msg, pos, "): ");
8511                   pos = safecatn(msg, sizeof msg, pos, isbit);
8512                }
8513                pos = safecat(msg, sizeof msg, pos, "/");
8514                /* The output is either "id/max" or "id sbit(sbit): isbit/max" */
8515                pos = safecatn(msg, sizeof msg, pos, vi->sbit_max);
8516             }
8517             pos = safecat(msg, sizeof msg, pos, ")");
8518
8519             /* A component may have been multiplied (in linear space) by the
8520              * alpha value, 'compose' says whether this is relevant.
8521              */
8522             if (compose || pass != 0)
8523             {
8524                /* If any form of composition is being done report our
8525                 * calculated linear value here (the code above doesn't record
8526                 * the input value before composition is performed, so what
8527                 * gets reported is the value after composition.)
8528                 */
8529                if (use_input || pass != 0)
8530                {
8531                   if (vi->file_inverse > 0)
8532                   {
8533                      pos = safecat(msg, sizeof msg, pos, "^");
8534                      pos = safecatd(msg, sizeof msg, pos, vi->file_inverse, 2);
8535                   }
8536
8537                   else
8538                      pos = safecat(msg, sizeof msg, pos, "[linear]");
8539
8540                   pos = safecat(msg, sizeof msg, pos, "*(alpha)");
8541                   pos = safecatd(msg, sizeof msg, pos, alpha, precision);
8542                }
8543
8544                /* Now record the *linear* background value if it was used
8545                 * (this function is not passed the original, non-linear,
8546                 * value but it is contained in the test name.)
8547                 */
8548                if (use_background)
8549                {
8550                   pos = safecat(msg, sizeof msg, pos, use_input ? "+" : " ");
8551                   pos = safecat(msg, sizeof msg, pos, "(background)");
8552                   pos = safecatd(msg, sizeof msg, pos, background, precision);
8553                   pos = safecat(msg, sizeof msg, pos, "*");
8554                   pos = safecatd(msg, sizeof msg, pos, 1-alpha, precision);
8555                }
8556             }
8557
8558             /* Report the calculated value (input_sample) and the linearized
8559              * libpng value (output) unless this is just a component gamma
8560              * correction.
8561              */
8562             if (compose || alpha < 0 || pass != 0)
8563             {
8564                pos = safecat(msg, sizeof msg, pos,
8565                   pass != 0 ? " =\n\t" : " = ");
8566                pos = safecatd(msg, sizeof msg, pos, input_sample, precision);
8567                pos = safecat(msg, sizeof msg, pos, " (libpng: ");
8568                pos = safecatd(msg, sizeof msg, pos, output, precision);
8569                pos = safecat(msg, sizeof msg, pos, ")");
8570
8571                /* Finally report the output gamma encoding, if any. */
8572                if (output_is_encoded)
8573                {
8574                   pos = safecat(msg, sizeof msg, pos, " ^");
8575                   pos = safecatd(msg, sizeof msg, pos, vi->screen_inverse, 2);
8576                   pos = safecat(msg, sizeof msg, pos, "(to screen) =");
8577                }
8578
8579                else
8580                   pos = safecat(msg, sizeof msg, pos, " [screen is linear] =");
8581             }
8582
8583             if ((!compose && alpha >= 0) || pass != 0)
8584             {
8585                if (pass != 0) /* logging */
8586                   pos = safecat(msg, sizeof msg, pos, "\n\t[overall:");
8587
8588                /* This is the non-composition case, the internal linear
8589                 * values are irrelevant (though the log below will reveal
8590                 * them.)  Output a much shorter warning/error message and report
8591                 * the overall gamma correction.
8592                 */
8593                if (vi->gamma_correction > 0)
8594                {
8595                   pos = safecat(msg, sizeof msg, pos, " ^");
8596                   pos = safecatd(msg, sizeof msg, pos, vi->gamma_correction, 2);
8597                   pos = safecat(msg, sizeof msg, pos, "(gamma correction) =");
8598                }
8599
8600                else
8601                   pos = safecat(msg, sizeof msg, pos,
8602                      " [no gamma correction] =");
8603
8604                if (pass != 0)
8605                   pos = safecat(msg, sizeof msg, pos, "]");
8606             }
8607
8608             /* This is our calculated encoded_sample which should (but does
8609              * not) match od:
8610              */
8611             pos = safecat(msg, sizeof msg, pos, pass != 0 ? "\n\t" : " ");
8612             pos = safecatd(msg, sizeof msg, pos, is_lo, 1);
8613             pos = safecat(msg, sizeof msg, pos, " < ");
8614             pos = safecatd(msg, sizeof msg, pos, encoded_sample, 1);
8615             pos = safecat(msg, sizeof msg, pos, " (libpng: ");
8616             pos = safecatn(msg, sizeof msg, pos, od);
8617             pos = safecat(msg, sizeof msg, pos, ")");
8618             pos = safecat(msg, sizeof msg, pos, "/");
8619             pos = safecatn(msg, sizeof msg, pos, outmax);
8620             pos = safecat(msg, sizeof msg, pos, " < ");
8621             pos = safecatd(msg, sizeof msg, pos, is_hi, 1);
8622
8623             if (pass == 0) /* The error condition */
8624             {
8625 #              ifdef PNG_WARNINGS_SUPPORTED
8626                   png_warning(vi->pp, msg);
8627 #              else
8628                   store_warning(vi->pp, msg);
8629 #              endif
8630             }
8631
8632             else /* logging this value */
8633                store_verbose(&vi->dp->pm->this, vi->pp, pass, msg);
8634          }
8635       }
8636    }
8637
8638    return i;
8639 }
8640
8641 static void
8642 gamma_image_validate(gamma_display *dp, png_const_structp pp,
8643    png_infop pi)
8644 {
8645    /* Get some constants derived from the input and output file formats: */
8646    PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
8647    PNG_CONST png_byte in_ct = dp->this.colour_type;
8648    PNG_CONST png_byte in_bd = dp->this.bit_depth;
8649    PNG_CONST png_uint_32 w = dp->this.w;
8650    PNG_CONST png_uint_32 h = dp->this.h;
8651    PNG_CONST size_t cbRow = dp->this.cbRow;
8652    PNG_CONST png_byte out_ct = png_get_color_type(pp, pi);
8653    PNG_CONST png_byte out_bd = png_get_bit_depth(pp, pi);
8654
8655    /* There are three sources of error, firstly the quantization in the
8656     * file encoding, determined by sbit and/or the file depth, secondly
8657     * the output (screen) gamma and thirdly the output file encoding.
8658     *
8659     * Since this API receives the screen and file gamma in double
8660     * precision it is possible to calculate an exact answer given an input
8661     * pixel value.  Therefore we assume that the *input* value is exact -
8662     * sample/maxsample - calculate the corresponding gamma corrected
8663     * output to the limits of double precision arithmetic and compare with
8664     * what libpng returns.
8665     *
8666     * Since the library must quantize the output to 8 or 16 bits there is
8667     * a fundamental limit on the accuracy of the output of +/-.5 - this
8668     * quantization limit is included in addition to the other limits
8669     * specified by the paramaters to the API.  (Effectively, add .5
8670     * everywhere.)
8671     *
8672     * The behavior of the 'sbit' paramter is defined by section 12.5
8673     * (sample depth scaling) of the PNG spec.  That section forces the
8674     * decoder to assume that the PNG values have been scaled if sBIT is
8675     * present:
8676     *
8677     *     png-sample = floor( input-sample * (max-out/max-in) + .5);
8678     *
8679     * This means that only a subset of the possible PNG values should
8680     * appear in the input. However, the spec allows the encoder to use a
8681     * variety of approximations to the above and doesn't require any
8682     * restriction of the values produced.
8683     *
8684     * Nevertheless the spec requires that the upper 'sBIT' bits of the
8685     * value stored in a PNG file be the original sample bits.
8686     * Consequently the code below simply scales the top sbit bits by
8687     * (1<<sbit)-1 to obtain an original sample value.
8688     *
8689     * Because there is limited precision in the input it is arguable that
8690     * an acceptable result is any valid result from input-.5 to input+.5.
8691     * The basic tests below do not do this, however if 'use_input_precision'
8692     * is set a subsequent test is performed above.
8693     */
8694    PNG_CONST unsigned int samples_per_pixel = (out_ct & 2U) ? 3U : 1U;
8695    int processing;
8696    png_uint_32 y;
8697    PNG_CONST store_palette_entry *in_palette = dp->this.palette;
8698    PNG_CONST int in_is_transparent = dp->this.is_transparent;
8699    int out_npalette = -1;
8700    int out_is_transparent = 0; /* Just refers to the palette case */
8701    store_palette out_palette;
8702    validate_info vi;
8703
8704    /* Check for row overwrite errors */
8705    store_image_check(dp->this.ps, pp, 0);
8706
8707    /* Supply the input and output sample depths here - 8 for an indexed image,
8708     * otherwise the bit depth.
8709     */
8710    init_validate_info(&vi, dp, pp, in_ct==3?8:in_bd, out_ct==3?8:out_bd);
8711
8712    processing = (vi.gamma_correction > 0 && !dp->threshold_test)
8713       || in_bd != out_bd || in_ct != out_ct || vi.do_background;
8714
8715    /* TODO: FIX THIS: MAJOR BUG!  If the transformations all happen inside
8716     * the palette there is no way of finding out, because libpng fails to
8717     * update the palette on png_read_update_info.  Indeed, libpng doesn't
8718     * even do the required work until much later, when it doesn't have any
8719     * info pointer.  Oops.  For the moment 'processing' is turned off if
8720     * out_ct is palette.
8721     */
8722    if (in_ct == 3 && out_ct == 3)
8723       processing = 0;
8724
8725    if (processing && out_ct == 3)
8726       out_is_transparent = read_palette(out_palette, &out_npalette, pp, pi);
8727
8728    for (y=0; y<h; ++y)
8729    {
8730       png_const_bytep pRow = store_image_row(ps, pp, 0, y);
8731       png_byte std[STANDARD_ROWMAX];
8732
8733       transform_row(pp, std, in_ct, in_bd, y);
8734
8735       if (processing)
8736       {
8737          unsigned int x;
8738
8739          for (x=0; x<w; ++x)
8740          {
8741             double alpha = 1; /* serves as a flag value */
8742
8743             /* Record the palette index for index images. */
8744             PNG_CONST unsigned int in_index =
8745                in_ct == 3 ? sample(std, 3, in_bd, x, 0) : 256;
8746             PNG_CONST unsigned int out_index =
8747                out_ct == 3 ? sample(std, 3, out_bd, x, 0) : 256;
8748
8749             /* Handle input alpha - png_set_background will cause the output
8750              * alpha to disappear so there is nothing to check.
8751              */
8752             if ((in_ct & PNG_COLOR_MASK_ALPHA) != 0 || (in_ct == 3 &&
8753                in_is_transparent))
8754             {
8755                PNG_CONST unsigned int input_alpha = in_ct == 3 ?
8756                   dp->this.palette[in_index].alpha :
8757                   sample(std, in_ct, in_bd, x, samples_per_pixel);
8758
8759                unsigned int output_alpha = 65536 /* as a flag value */;
8760
8761                if (out_ct == 3)
8762                {
8763                   if (out_is_transparent)
8764                      output_alpha = out_palette[out_index].alpha;
8765                }
8766
8767                else if ((out_ct & PNG_COLOR_MASK_ALPHA) != 0)
8768                   output_alpha = sample(pRow, out_ct, out_bd, x,
8769                      samples_per_pixel);
8770
8771                if (output_alpha != 65536)
8772                   alpha = gamma_component_validate("alpha", &vi, input_alpha,
8773                      output_alpha, -1/*alpha*/, 0/*background*/);
8774
8775                else /* no alpha in output */
8776                {
8777                   /* This is a copy of the calculation of 'i' above in order to
8778                    * have the alpha value to use in the background calculation.
8779                    */
8780                   alpha = input_alpha >> vi.isbit_shift;
8781                   alpha /= vi.sbit_max;
8782                }
8783             }
8784
8785             /* Handle grayscale or RGB components. */
8786             if ((in_ct & PNG_COLOR_MASK_COLOR) == 0) /* grayscale */
8787                (void)gamma_component_validate("gray", &vi,
8788                   sample(std, in_ct, in_bd, x, 0),
8789                   sample(pRow, out_ct, out_bd, x, 0), alpha/*component*/,
8790                   vi.background_red);
8791             else /* RGB or palette */
8792             {
8793                (void)gamma_component_validate("red", &vi,
8794                   in_ct == 3 ? in_palette[in_index].red :
8795                      sample(std, in_ct, in_bd, x, 0),
8796                   out_ct == 3 ? out_palette[out_index].red :
8797                      sample(pRow, out_ct, out_bd, x, 0),
8798                   alpha/*component*/, vi.background_red);
8799
8800                (void)gamma_component_validate("green", &vi,
8801                   in_ct == 3 ? in_palette[in_index].green :
8802                      sample(std, in_ct, in_bd, x, 1),
8803                   out_ct == 3 ? out_palette[out_index].green :
8804                      sample(pRow, out_ct, out_bd, x, 1),
8805                   alpha/*component*/, vi.background_green);
8806
8807                (void)gamma_component_validate("blue", &vi,
8808                   in_ct == 3 ? in_palette[in_index].blue :
8809                      sample(std, in_ct, in_bd, x, 2),
8810                   out_ct == 3 ? out_palette[out_index].blue :
8811                      sample(pRow, out_ct, out_bd, x, 2),
8812                   alpha/*component*/, vi.background_blue);
8813             }
8814          }
8815       }
8816
8817       else if (memcmp(std, pRow, cbRow) != 0)
8818       {
8819          char msg[64];
8820
8821          /* No transform is expected on the threshold tests. */
8822          sprintf(msg, "gamma: below threshold row %lu changed",
8823             (unsigned long)y);
8824
8825          png_error(pp, msg);
8826       }
8827    } /* row (y) loop */
8828
8829    dp->this.ps->validated = 1;
8830 }
8831
8832 static void PNGCBAPI
8833 gamma_end(png_structp ppIn, png_infop pi)
8834 {
8835    png_const_structp pp = ppIn;
8836    gamma_display *dp = voidcast(gamma_display*, png_get_progressive_ptr(pp));
8837
8838    if (!dp->this.speed)
8839       gamma_image_validate(dp, pp, pi);
8840    else
8841       dp->this.ps->validated = 1;
8842 }
8843
8844 /* A single test run checking a gamma transformation.
8845  *
8846  * maxabs: maximum absolute error as a fraction
8847  * maxout: maximum output error in the output units
8848  * maxpc:  maximum percentage error (as a percentage)
8849  */
8850 static void
8851 gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn,
8852     PNG_CONST png_byte bit_depthIn, PNG_CONST int palette_numberIn,
8853     PNG_CONST int interlace_typeIn,
8854     PNG_CONST double file_gammaIn, PNG_CONST double screen_gammaIn,
8855     PNG_CONST png_byte sbitIn, PNG_CONST int threshold_testIn,
8856     PNG_CONST char *name,
8857     PNG_CONST int use_input_precisionIn, PNG_CONST int scale16In,
8858     PNG_CONST int expand16In, PNG_CONST int do_backgroundIn,
8859     PNG_CONST png_color_16 *bkgd_colorIn, double bkgd_gammaIn)
8860 {
8861    gamma_display d;
8862    context(&pmIn->this, fault);
8863
8864    gamma_display_init(&d, pmIn, FILEID(colour_typeIn, bit_depthIn,
8865       palette_numberIn, interlace_typeIn, 0, 0, 0),
8866       file_gammaIn, screen_gammaIn, sbitIn,
8867       threshold_testIn, use_input_precisionIn, scale16In,
8868       expand16In, do_backgroundIn, bkgd_colorIn, bkgd_gammaIn);
8869
8870    Try
8871    {
8872       png_structp pp;
8873       png_infop pi;
8874       gama_modification gama_mod;
8875       srgb_modification srgb_mod;
8876       sbit_modification sbit_mod;
8877
8878       /* For the moment don't use the png_modifier support here. */
8879       d.pm->encoding_counter = 0;
8880       modifier_set_encoding(d.pm); /* Just resets everything */
8881       d.pm->current_gamma = d.file_gamma;
8882
8883       /* Make an appropriate modifier to set the PNG file gamma to the
8884        * given gamma value and the sBIT chunk to the given precision.
8885        */
8886       d.pm->modifications = NULL;
8887       gama_modification_init(&gama_mod, d.pm, d.file_gamma);
8888       srgb_modification_init(&srgb_mod, d.pm, 127 /*delete*/);
8889       if (d.sbit > 0)
8890          sbit_modification_init(&sbit_mod, d.pm, d.sbit);
8891
8892       modification_reset(d.pm->modifications);
8893
8894       /* Get a png_struct for writing the image. */
8895       pp = set_modifier_for_read(d.pm, &pi, d.this.id, name);
8896       standard_palette_init(&d.this);
8897
8898       /* Introduce the correct read function. */
8899       if (d.pm->this.progressive)
8900       {
8901          /* Share the row function with the standard implementation. */
8902          png_set_progressive_read_fn(pp, &d, gamma_info, progressive_row,
8903             gamma_end);
8904
8905          /* Now feed data into the reader until we reach the end: */
8906          modifier_progressive_read(d.pm, pp, pi);
8907       }
8908       else
8909       {
8910          /* modifier_read expects a png_modifier* */
8911          png_set_read_fn(pp, d.pm, modifier_read);
8912
8913          /* Check the header values: */
8914          png_read_info(pp, pi);
8915
8916          /* Process the 'info' requirements. Only one image is generated */
8917          gamma_info_imp(&d, pp, pi);
8918
8919          sequential_row(&d.this, pp, pi, -1, 0);
8920
8921          if (!d.this.speed)
8922             gamma_image_validate(&d, pp, pi);
8923          else
8924             d.this.ps->validated = 1;
8925       }
8926
8927       modifier_reset(d.pm);
8928
8929       if (d.pm->log && !d.threshold_test && !d.this.speed)
8930          fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n",
8931             d.this.bit_depth, colour_types[d.this.colour_type], name,
8932             d.maxerrout, d.maxerrabs, 100*d.maxerrpc);
8933
8934       /* Log the summary values too. */
8935       if (d.this.colour_type == 0 || d.this.colour_type == 4)
8936       {
8937          switch (d.this.bit_depth)
8938          {
8939          case 1:
8940             break;
8941
8942          case 2:
8943             if (d.maxerrout > d.pm->error_gray_2)
8944                d.pm->error_gray_2 = d.maxerrout;
8945
8946             break;
8947
8948          case 4:
8949             if (d.maxerrout > d.pm->error_gray_4)
8950                d.pm->error_gray_4 = d.maxerrout;
8951
8952             break;
8953
8954          case 8:
8955             if (d.maxerrout > d.pm->error_gray_8)
8956                d.pm->error_gray_8 = d.maxerrout;
8957
8958             break;
8959
8960          case 16:
8961             if (d.maxerrout > d.pm->error_gray_16)
8962                d.pm->error_gray_16 = d.maxerrout;
8963
8964             break;
8965
8966          default:
8967             png_error(pp, "bad bit depth (internal: 1)");
8968          }
8969       }
8970
8971       else if (d.this.colour_type == 2 || d.this.colour_type == 6)
8972       {
8973          switch (d.this.bit_depth)
8974          {
8975          case 8:
8976
8977             if (d.maxerrout > d.pm->error_color_8)
8978                d.pm->error_color_8 = d.maxerrout;
8979
8980             break;
8981
8982          case 16:
8983
8984             if (d.maxerrout > d.pm->error_color_16)
8985                d.pm->error_color_16 = d.maxerrout;
8986
8987             break;
8988
8989          default:
8990             png_error(pp, "bad bit depth (internal: 2)");
8991          }
8992       }
8993
8994       else if (d.this.colour_type == 3)
8995       {
8996          if (d.maxerrout > d.pm->error_indexed)
8997             d.pm->error_indexed = d.maxerrout;
8998       }
8999    }
9000
9001    Catch(fault)
9002       modifier_reset(voidcast(png_modifier*,(void*)fault));
9003 }
9004
9005 static void gamma_threshold_test(png_modifier *pm, png_byte colour_type,
9006     png_byte bit_depth, int interlace_type, double file_gamma,
9007     double screen_gamma)
9008 {
9009    size_t pos = 0;
9010    char name[64];
9011    pos = safecat(name, sizeof name, pos, "threshold ");
9012    pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9013    pos = safecat(name, sizeof name, pos, "/");
9014    pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9015
9016    (void)gamma_test(pm, colour_type, bit_depth, 0/*palette*/, interlace_type,
9017       file_gamma, screen_gamma, 0/*sBIT*/, 1/*threshold test*/, name,
9018       0 /*no input precision*/,
9019       0 /*no scale16*/, 0 /*no expand16*/, 0 /*no background*/, 0 /*hence*/,
9020       0 /*no background gamma*/);
9021 }
9022
9023 static void
9024 perform_gamma_threshold_tests(png_modifier *pm)
9025 {
9026    png_byte colour_type = 0;
9027    png_byte bit_depth = 0;
9028    unsigned int palette_number = 0;
9029
9030    /* Don't test more than one instance of each palette - it's pointless, in
9031     * fact this test is somewhat excessive since libpng doesn't make this
9032     * decision based on colour type or bit depth!
9033     */
9034    while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9035       if (palette_number == 0)
9036    {
9037       double test_gamma = 1.0;
9038       while (test_gamma >= .4)
9039       {
9040          /* There's little point testing the interlacing vs non-interlacing,
9041           * but this can be set from the command line.
9042           */
9043          gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
9044             test_gamma, 1/test_gamma);
9045          test_gamma *= .95;
9046       }
9047
9048       /* And a special test for sRGB */
9049       gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
9050           .45455, 2.2);
9051
9052       if (fail(pm))
9053          return;
9054    }
9055 }
9056
9057 static void gamma_transform_test(png_modifier *pm,
9058    PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
9059    PNG_CONST int palette_number,
9060    PNG_CONST int interlace_type, PNG_CONST double file_gamma,
9061    PNG_CONST double screen_gamma, PNG_CONST png_byte sbit,
9062    PNG_CONST int use_input_precision, PNG_CONST int scale16)
9063 {
9064    size_t pos = 0;
9065    char name[64];
9066
9067    if (sbit != bit_depth && sbit != 0)
9068    {
9069       pos = safecat(name, sizeof name, pos, "sbit(");
9070       pos = safecatn(name, sizeof name, pos, sbit);
9071       pos = safecat(name, sizeof name, pos, ") ");
9072    }
9073
9074    else
9075       pos = safecat(name, sizeof name, pos, "gamma ");
9076
9077    if (scale16)
9078       pos = safecat(name, sizeof name, pos, "16to8 ");
9079
9080    pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9081    pos = safecat(name, sizeof name, pos, "->");
9082    pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9083
9084    gamma_test(pm, colour_type, bit_depth, palette_number, interlace_type,
9085       file_gamma, screen_gamma, sbit, 0, name, use_input_precision,
9086       scale16, pm->test_gamma_expand16, 0 , 0, 0);
9087 }
9088
9089 static void perform_gamma_transform_tests(png_modifier *pm)
9090 {
9091    png_byte colour_type = 0;
9092    png_byte bit_depth = 0;
9093    unsigned int palette_number = 0;
9094
9095    while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9096    {
9097       unsigned int i, j;
9098
9099       for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
9100          if (i != j)
9101          {
9102             gamma_transform_test(pm, colour_type, bit_depth, palette_number,
9103                pm->interlace_type, 1/pm->gammas[i], pm->gammas[j], 0/*sBIT*/,
9104                pm->use_input_precision, 0 /*do not scale16*/);
9105
9106             if (fail(pm))
9107                return;
9108          }
9109    }
9110 }
9111
9112 static void perform_gamma_sbit_tests(png_modifier *pm)
9113 {
9114    png_byte sbit;
9115
9116    /* The only interesting cases are colour and grayscale, alpha is ignored here
9117     * for overall speed.  Only bit depths where sbit is less than the bit depth
9118     * are tested.
9119     */
9120    for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit)
9121    {
9122       png_byte colour_type = 0, bit_depth = 0;
9123       unsigned int npalette = 0;
9124
9125       while (next_format(&colour_type, &bit_depth, &npalette, 1/*gamma*/))
9126          if ((colour_type & PNG_COLOR_MASK_ALPHA) == 0 &&
9127             ((colour_type == 3 && sbit < 8) ||
9128             (colour_type != 3 && sbit < bit_depth)))
9129       {
9130          unsigned int i;
9131
9132          for (i=0; i<pm->ngamma_tests; ++i)
9133          {
9134             unsigned int j;
9135
9136             for (j=0; j<pm->ngamma_tests; ++j) if (i != j)
9137             {
9138                gamma_transform_test(pm, colour_type, bit_depth, npalette,
9139                   pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
9140                   sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
9141
9142                if (fail(pm))
9143                   return;
9144             }
9145          }
9146       }
9147    }
9148 }
9149
9150 /* Note that this requires a 16 bit source image but produces 8 bit output, so
9151  * we only need the 16bit write support, but the 16 bit images are only
9152  * generated if DO_16BIT is defined.
9153  */
9154 #ifdef DO_16BIT
9155 static void perform_gamma_scale16_tests(png_modifier *pm)
9156 {
9157 #  ifndef PNG_MAX_GAMMA_8
9158 #     define PNG_MAX_GAMMA_8 11
9159 #  endif
9160 #  define SBIT_16_TO_8 PNG_MAX_GAMMA_8
9161    /* Include the alpha cases here. Note that sbit matches the internal value
9162     * used by the library - otherwise we will get spurious errors from the
9163     * internal sbit style approximation.
9164     *
9165     * The threshold test is here because otherwise the 16 to 8 conversion will
9166     * proceed *without* gamma correction, and the tests above will fail (but not
9167     * by much) - this could be fixed, it only appears with the -g option.
9168     */
9169    unsigned int i, j;
9170    for (i=0; i<pm->ngamma_tests; ++i)
9171    {
9172       for (j=0; j<pm->ngamma_tests; ++j)
9173       {
9174          if (i != j &&
9175              fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD)
9176          {
9177             gamma_transform_test(pm, 0, 16, 0, pm->interlace_type,
9178                1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9179                pm->use_input_precision_16to8, 1 /*scale16*/);
9180
9181             if (fail(pm))
9182                return;
9183
9184             gamma_transform_test(pm, 2, 16, 0, pm->interlace_type,
9185                1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9186                pm->use_input_precision_16to8, 1 /*scale16*/);
9187
9188             if (fail(pm))
9189                return;
9190
9191             gamma_transform_test(pm, 4, 16, 0, pm->interlace_type,
9192                1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9193                pm->use_input_precision_16to8, 1 /*scale16*/);
9194
9195             if (fail(pm))
9196                return;
9197
9198             gamma_transform_test(pm, 6, 16, 0, pm->interlace_type,
9199                1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9200                pm->use_input_precision_16to8, 1 /*scale16*/);
9201
9202             if (fail(pm))
9203                return;
9204          }
9205       }
9206    }
9207 }
9208 #endif /* 16 to 8 bit conversion */
9209
9210 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
9211    defined(PNG_READ_ALPHA_MODE_SUPPORTED)
9212 static void gamma_composition_test(png_modifier *pm,
9213    PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
9214    PNG_CONST int palette_number,
9215    PNG_CONST int interlace_type, PNG_CONST double file_gamma,
9216    PNG_CONST double screen_gamma,
9217    PNG_CONST int use_input_precision, PNG_CONST int do_background,
9218    PNG_CONST int expand_16)
9219 {
9220    size_t pos = 0;
9221    png_const_charp base;
9222    double bg;
9223    char name[128];
9224    png_color_16 background;
9225
9226    /* Make up a name and get an appropriate background gamma value. */
9227    switch (do_background)
9228    {
9229       default:
9230          base = "";
9231          bg = 4; /* should not be used */
9232          break;
9233       case PNG_BACKGROUND_GAMMA_SCREEN:
9234          base = " bckg(Screen):";
9235          bg = 1/screen_gamma;
9236          break;
9237       case PNG_BACKGROUND_GAMMA_FILE:
9238          base = " bckg(File):";
9239          bg = file_gamma;
9240          break;
9241       case PNG_BACKGROUND_GAMMA_UNIQUE:
9242          base = " bckg(Unique):";
9243          /* This tests the handling of a unique value, the math is such that the
9244           * value tends to be <1, but is neither screen nor file (even if they
9245           * match!)
9246           */
9247          bg = (file_gamma + screen_gamma) / 3;
9248          break;
9249 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9250       case ALPHA_MODE_OFFSET + PNG_ALPHA_PNG:
9251          base = " alpha(PNG)";
9252          bg = 4; /* should not be used */
9253          break;
9254       case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
9255          base = " alpha(Porter-Duff)";
9256          bg = 4; /* should not be used */
9257          break;
9258       case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
9259          base = " alpha(Optimized)";
9260          bg = 4; /* should not be used */
9261          break;
9262       case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
9263          base = " alpha(Broken)";
9264          bg = 4; /* should not be used */
9265          break;
9266 #endif
9267    }
9268
9269    /* Use random background values - the background is always presented in the
9270     * output space (8 or 16 bit components).
9271     */
9272    if (expand_16 || bit_depth == 16)
9273    {
9274       png_uint_32 r = random_32();
9275
9276       background.red = (png_uint_16)r;
9277       background.green = (png_uint_16)(r >> 16);
9278       r = random_32();
9279       background.blue = (png_uint_16)r;
9280       background.gray = (png_uint_16)(r >> 16);
9281    }
9282
9283    else /* 8 bit colors */
9284    {
9285       png_uint_32 r = random_32();
9286
9287       background.red = (png_byte)r;
9288       background.green = (png_byte)(r >> 8);
9289       background.blue = (png_byte)(r >> 16);
9290       background.gray = (png_byte)(r >> 24);
9291    }
9292
9293    background.index = 193; /* rgb(193,193,193) to detect errors */
9294    if (!(colour_type & PNG_COLOR_MASK_COLOR))
9295    {
9296       /* Grayscale input, we do not convert to RGB (TBD), so we must set the
9297        * background to gray - else libpng seems to fail.
9298        */
9299       background.red = background.green = background.blue = background.gray;
9300    }
9301
9302    pos = safecat(name, sizeof name, pos, "gamma ");
9303    pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9304    pos = safecat(name, sizeof name, pos, "->");
9305    pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9306
9307    pos = safecat(name, sizeof name, pos, base);
9308    if (do_background < ALPHA_MODE_OFFSET)
9309    {
9310       /* Include the background color and gamma in the name: */
9311       pos = safecat(name, sizeof name, pos, "(");
9312       /* This assumes no expand gray->rgb - the current code won't handle that!
9313        */
9314       if (colour_type & PNG_COLOR_MASK_COLOR)
9315       {
9316          pos = safecatn(name, sizeof name, pos, background.red);
9317          pos = safecat(name, sizeof name, pos, ",");
9318          pos = safecatn(name, sizeof name, pos, background.green);
9319          pos = safecat(name, sizeof name, pos, ",");
9320          pos = safecatn(name, sizeof name, pos, background.blue);
9321       }
9322       else
9323          pos = safecatn(name, sizeof name, pos, background.gray);
9324       pos = safecat(name, sizeof name, pos, ")^");
9325       pos = safecatd(name, sizeof name, pos, bg, 3);
9326    }
9327
9328    gamma_test(pm, colour_type, bit_depth, palette_number, interlace_type,
9329       file_gamma, screen_gamma, 0/*sBIT*/, 0, name, use_input_precision,
9330       0/*strip 16*/, expand_16, do_background, &background, bg);
9331 }
9332
9333
9334 static void
9335 perform_gamma_composition_tests(png_modifier *pm, int do_background,
9336    int expand_16)
9337 {
9338    png_byte colour_type = 0;
9339    png_byte bit_depth = 0;
9340    unsigned int palette_number = 0;
9341
9342    /* Skip the non-alpha cases - there is no setting of a transparency colour at
9343     * present.
9344     */
9345    while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9346       if ((colour_type & PNG_COLOR_MASK_ALPHA) != 0)
9347    {
9348       unsigned int i, j;
9349
9350       /* Don't skip the i==j case here - it's relevant. */
9351       for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
9352       {
9353          gamma_composition_test(pm, colour_type, bit_depth, palette_number,
9354             pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
9355             pm->use_input_precision, do_background, expand_16);
9356
9357          if (fail(pm))
9358             return;
9359       }
9360    }
9361 }
9362 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
9363
9364 static void
9365 init_gamma_errors(png_modifier *pm)
9366 {
9367    /* Use -1 to catch tests that were not actually run */
9368    pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = -1.;
9369    pm->error_color_8 = -1.;
9370    pm->error_indexed = -1.;
9371    pm->error_gray_16 = pm->error_color_16 = -1.;
9372 }
9373
9374 static void
9375 print_one(const char *leader, double err)
9376 {
9377    if (err != -1.)
9378       printf(" %s %.5f\n", leader, err);
9379 }
9380
9381 static void
9382 summarize_gamma_errors(png_modifier *pm, png_const_charp who, int low_bit_depth,
9383    int indexed)
9384 {
9385    fflush(stderr);
9386
9387    if (who)
9388       printf("\nGamma correction with %s:\n", who);
9389
9390    else
9391       printf("\nBasic gamma correction:\n");
9392
9393    if (low_bit_depth)
9394    {
9395       print_one(" 2 bit gray: ", pm->error_gray_2);
9396       print_one(" 4 bit gray: ", pm->error_gray_4);
9397       print_one(" 8 bit gray: ", pm->error_gray_8);
9398       print_one(" 8 bit color:", pm->error_color_8);
9399       if (indexed)
9400          print_one(" indexed:    ", pm->error_indexed);
9401    }
9402
9403    print_one("16 bit gray: ", pm->error_gray_16);
9404    print_one("16 bit color:", pm->error_color_16);
9405
9406    fflush(stdout);
9407 }
9408
9409 static void
9410 perform_gamma_test(png_modifier *pm, int summary)
9411 {
9412    /*TODO: remove this*/
9413    /* Save certain values for the temporary overrides below. */
9414    unsigned int calculations_use_input_precision =
9415       pm->calculations_use_input_precision;
9416 #  ifdef PNG_READ_BACKGROUND_SUPPORTED
9417       double maxout8 = pm->maxout8;
9418 #  endif
9419
9420    /* First some arbitrary no-transform tests: */
9421    if (!pm->this.speed && pm->test_gamma_threshold)
9422    {
9423       perform_gamma_threshold_tests(pm);
9424
9425       if (fail(pm))
9426          return;
9427    }
9428
9429    /* Now some real transforms. */
9430    if (pm->test_gamma_transform)
9431    {
9432       if (summary)
9433       {
9434          fflush(stderr);
9435          printf("Gamma correction error summary\n\n");
9436          printf("The printed value is the maximum error in the pixel values\n");
9437          printf("calculated by the libpng gamma correction code.  The error\n");
9438          printf("is calculated as the difference between the output pixel\n");
9439          printf("value (always an integer) and the ideal value from the\n");
9440          printf("libpng specification (typically not an integer).\n\n");
9441
9442          printf("Expect this value to be less than .5 for 8 bit formats,\n");
9443          printf("less than 1 for formats with fewer than 8 bits and a small\n");
9444          printf("number (typically less than 5) for the 16 bit formats.\n");
9445          printf("For performance reasons the value for 16 bit formats\n");
9446          printf("increases when the image file includes an sBIT chunk.\n");
9447          fflush(stdout);
9448       }
9449
9450       init_gamma_errors(pm);
9451       /*TODO: remove this.  Necessary because the current libpng
9452        * implementation works in 8 bits:
9453        */
9454       if (pm->test_gamma_expand16)
9455          pm->calculations_use_input_precision = 1;
9456       perform_gamma_transform_tests(pm);
9457       if (!calculations_use_input_precision)
9458          pm->calculations_use_input_precision = 0;
9459
9460       if (summary)
9461          summarize_gamma_errors(pm, 0/*who*/, 1/*low bit depth*/, 1/*indexed*/);
9462
9463       if (fail(pm))
9464          return;
9465    }
9466
9467    /* The sbit tests produce much larger errors: */
9468    if (pm->test_gamma_sbit)
9469    {
9470       init_gamma_errors(pm);
9471       perform_gamma_sbit_tests(pm);
9472
9473       if (summary)
9474          summarize_gamma_errors(pm, "sBIT", pm->sbitlow < 8U, 1/*indexed*/);
9475
9476       if (fail(pm))
9477          return;
9478    }
9479
9480 #ifdef DO_16BIT /* Should be READ_16BIT_SUPPORTED */
9481    if (pm->test_gamma_scale16)
9482    {
9483       /* The 16 to 8 bit strip operations: */
9484       init_gamma_errors(pm);
9485       perform_gamma_scale16_tests(pm);
9486
9487       if (summary)
9488       {
9489          fflush(stderr);
9490          printf("\nGamma correction with 16 to 8 bit reduction:\n");
9491          printf(" 16 bit gray:  %.5f\n", pm->error_gray_16);
9492          printf(" 16 bit color: %.5f\n", pm->error_color_16);
9493          fflush(stdout);
9494       }
9495
9496       if (fail(pm))
9497          return;
9498    }
9499 #endif
9500
9501 #ifdef PNG_READ_BACKGROUND_SUPPORTED
9502    if (pm->test_gamma_background)
9503    {
9504       init_gamma_errors(pm);
9505
9506       /*TODO: remove this.  Necessary because the current libpng
9507        * implementation works in 8 bits:
9508        */
9509       if (pm->test_gamma_expand16)
9510       {
9511          pm->calculations_use_input_precision = 1;
9512          pm->maxout8 = .499; /* because the 16 bit background is smashed */
9513       }
9514       perform_gamma_composition_tests(pm, PNG_BACKGROUND_GAMMA_UNIQUE,
9515          pm->test_gamma_expand16);
9516       if (!calculations_use_input_precision)
9517          pm->calculations_use_input_precision = 0;
9518       pm->maxout8 = maxout8;
9519
9520       if (summary)
9521          summarize_gamma_errors(pm, "background", 1, 0/*indexed*/);
9522
9523       if (fail(pm))
9524          return;
9525    }
9526 #endif
9527
9528 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9529    if (pm->test_gamma_alpha_mode)
9530    {
9531       int do_background;
9532
9533       init_gamma_errors(pm);
9534
9535       /*TODO: remove this.  Necessary because the current libpng
9536        * implementation works in 8 bits:
9537        */
9538       if (pm->test_gamma_expand16)
9539          pm->calculations_use_input_precision = 1;
9540       for (do_background = ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD;
9541          do_background <= ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN && !fail(pm);
9542          ++do_background)
9543          perform_gamma_composition_tests(pm, do_background,
9544             pm->test_gamma_expand16);
9545       if (!calculations_use_input_precision)
9546          pm->calculations_use_input_precision = 0;
9547
9548       if (summary)
9549          summarize_gamma_errors(pm, "alpha mode", 1, 0/*indexed*/);
9550
9551       if (fail(pm))
9552          return;
9553    }
9554 #endif
9555 }
9556 #endif /* PNG_READ_GAMMA_SUPPORTED */
9557 #endif /* PNG_READ_SUPPORTED */
9558
9559 /* INTERLACE MACRO VALIDATION */
9560 /* This is copied verbatim from the specification, it is simply the pass
9561  * number in which each pixel in each 8x8 tile appears.  The array must
9562  * be indexed adam7[y][x] and notice that the pass numbers are based at
9563  * 1, not 0 - the base libpng uses.
9564  */
9565 static PNG_CONST
9566 png_byte adam7[8][8] =
9567 {
9568    { 1,6,4,6,2,6,4,6 },
9569    { 7,7,7,7,7,7,7,7 },
9570    { 5,6,5,6,5,6,5,6 },
9571    { 7,7,7,7,7,7,7,7 },
9572    { 3,6,4,6,3,6,4,6 },
9573    { 7,7,7,7,7,7,7,7 },
9574    { 5,6,5,6,5,6,5,6 },
9575    { 7,7,7,7,7,7,7,7 }
9576 };
9577
9578 /* This routine validates all the interlace support macros in png.h for
9579  * a variety of valid PNG widths and heights.  It uses a number of similarly
9580  * named internal routines that feed off the above array.
9581  */
9582 static png_uint_32
9583 png_pass_start_row(int pass)
9584 {
9585    int x, y;
9586    ++pass;
9587    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9588       return y;
9589    return 0xf;
9590 }
9591
9592 static png_uint_32
9593 png_pass_start_col(int pass)
9594 {
9595    int x, y;
9596    ++pass;
9597    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9598       return x;
9599    return 0xf;
9600 }
9601
9602 static int
9603 png_pass_row_shift(int pass)
9604 {
9605    int x, y, base=(-1), inc=8;
9606    ++pass;
9607    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9608    {
9609       if (base == (-1))
9610          base = y;
9611       else if (base == y)
9612          {}
9613       else if (inc == y-base)
9614          base=y;
9615       else if (inc == 8)
9616          inc = y-base, base=y;
9617       else if (inc != y-base)
9618          return 0xff; /* error - more than one 'inc' value! */
9619    }
9620
9621    if (base == (-1)) return 0xfe; /* error - no row in pass! */
9622
9623    /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9624    switch (inc)
9625    {
9626 case 2: return 1;
9627 case 4: return 2;
9628 case 8: return 3;
9629 default: break;
9630    }
9631
9632    /* error - unrecognized 'inc' */
9633    return (inc << 8) + 0xfd;
9634 }
9635
9636 static int
9637 png_pass_col_shift(int pass)
9638 {
9639    int x, y, base=(-1), inc=8;
9640    ++pass;
9641    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9642    {
9643       if (base == (-1))
9644          base = x;
9645       else if (base == x)
9646          {}
9647       else if (inc == x-base)
9648          base=x;
9649       else if (inc == 8)
9650          inc = x-base, base=x;
9651       else if (inc != x-base)
9652          return 0xff; /* error - more than one 'inc' value! */
9653    }
9654
9655    if (base == (-1)) return 0xfe; /* error - no row in pass! */
9656
9657    /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9658    switch (inc)
9659    {
9660 case 1: return 0; /* pass 7 has all the columns */
9661 case 2: return 1;
9662 case 4: return 2;
9663 case 8: return 3;
9664 default: break;
9665    }
9666
9667    /* error - unrecognized 'inc' */
9668    return (inc << 8) + 0xfd;
9669 }
9670
9671 static png_uint_32
9672 png_row_from_pass_row(png_uint_32 yIn, int pass)
9673 {
9674    /* By examination of the array: */
9675    switch (pass)
9676    {
9677 case 0: return yIn * 8;
9678 case 1: return yIn * 8;
9679 case 2: return yIn * 8 + 4;
9680 case 3: return yIn * 4;
9681 case 4: return yIn * 4 + 2;
9682 case 5: return yIn * 2;
9683 case 6: return yIn * 2 + 1;
9684 default: break;
9685    }
9686
9687    return 0xff; /* bad pass number */
9688 }
9689
9690 static png_uint_32
9691 png_col_from_pass_col(png_uint_32 xIn, int pass)
9692 {
9693    /* By examination of the array: */
9694    switch (pass)
9695    {
9696 case 0: return xIn * 8;
9697 case 1: return xIn * 8 + 4;
9698 case 2: return xIn * 4;
9699 case 3: return xIn * 4 + 2;
9700 case 4: return xIn * 2;
9701 case 5: return xIn * 2 + 1;
9702 case 6: return xIn;
9703 default: break;
9704    }
9705
9706    return 0xff; /* bad pass number */
9707 }
9708
9709 static int
9710 png_row_in_interlace_pass(png_uint_32 y, int pass)
9711 {
9712    /* Is row 'y' in pass 'pass'? */
9713    int x;
9714    y &= 7;
9715    ++pass;
9716    for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9717       return 1;
9718
9719    return 0;
9720 }
9721
9722 static int
9723 png_col_in_interlace_pass(png_uint_32 x, int pass)
9724 {
9725    /* Is column 'x' in pass 'pass'? */
9726    int y;
9727    x &= 7;
9728    ++pass;
9729    for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9730       return 1;
9731
9732    return 0;
9733 }
9734
9735 static png_uint_32
9736 png_pass_rows(png_uint_32 height, int pass)
9737 {
9738    png_uint_32 tiles = height>>3;
9739    png_uint_32 rows = 0;
9740    unsigned int x, y;
9741
9742    height &= 7;
9743    ++pass;
9744    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9745    {
9746       rows += tiles;
9747       if (y < height) ++rows;
9748       break; /* i.e. break the 'x', column, loop. */
9749    }
9750
9751    return rows;
9752 }
9753
9754 static png_uint_32
9755 png_pass_cols(png_uint_32 width, int pass)
9756 {
9757    png_uint_32 tiles = width>>3;
9758    png_uint_32 cols = 0;
9759    unsigned int x, y;
9760
9761    width &= 7;
9762    ++pass;
9763    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9764    {
9765       cols += tiles;
9766       if (x < width) ++cols;
9767       break; /* i.e. break the 'y', row, loop. */
9768    }
9769
9770    return cols;
9771 }
9772
9773 static void
9774 perform_interlace_macro_validation(void)
9775 {
9776    /* The macros to validate, first those that depend only on pass:
9777     *
9778     * PNG_PASS_START_ROW(pass)
9779     * PNG_PASS_START_COL(pass)
9780     * PNG_PASS_ROW_SHIFT(pass)
9781     * PNG_PASS_COL_SHIFT(pass)
9782     */
9783    int pass;
9784
9785    for (pass=0; pass<7; ++pass)
9786    {
9787       png_uint_32 m, f, v;
9788
9789       m = PNG_PASS_START_ROW(pass);
9790       f = png_pass_start_row(pass);
9791       if (m != f)
9792       {
9793          fprintf(stderr, "PNG_PASS_START_ROW(%d) = %u != %x\n", pass, m, f);
9794          exit(99);
9795       }
9796
9797       m = PNG_PASS_START_COL(pass);
9798       f = png_pass_start_col(pass);
9799       if (m != f)
9800       {
9801          fprintf(stderr, "PNG_PASS_START_COL(%d) = %u != %x\n", pass, m, f);
9802          exit(99);
9803       }
9804
9805       m = PNG_PASS_ROW_SHIFT(pass);
9806       f = png_pass_row_shift(pass);
9807       if (m != f)
9808       {
9809          fprintf(stderr, "PNG_PASS_ROW_SHIFT(%d) = %u != %x\n", pass, m, f);
9810          exit(99);
9811       }
9812
9813       m = PNG_PASS_COL_SHIFT(pass);
9814       f = png_pass_col_shift(pass);
9815       if (m != f)
9816       {
9817          fprintf(stderr, "PNG_PASS_COL_SHIFT(%d) = %u != %x\n", pass, m, f);
9818          exit(99);
9819       }
9820
9821       /* Macros that depend on the image or sub-image height too:
9822        *
9823        * PNG_PASS_ROWS(height, pass)
9824        * PNG_PASS_COLS(width, pass)
9825        * PNG_ROW_FROM_PASS_ROW(yIn, pass)
9826        * PNG_COL_FROM_PASS_COL(xIn, pass)
9827        * PNG_ROW_IN_INTERLACE_PASS(y, pass)
9828        * PNG_COL_IN_INTERLACE_PASS(x, pass)
9829        */
9830       for (v=0;;)
9831       {
9832          /* First the base 0 stuff: */
9833          m = PNG_ROW_FROM_PASS_ROW(v, pass);
9834          f = png_row_from_pass_row(v, pass);
9835          if (m != f)
9836          {
9837             fprintf(stderr, "PNG_ROW_FROM_PASS_ROW(%u, %d) = %u != %x\n",
9838                v, pass, m, f);
9839             exit(99);
9840          }
9841
9842          m = PNG_COL_FROM_PASS_COL(v, pass);
9843          f = png_col_from_pass_col(v, pass);
9844          if (m != f)
9845          {
9846             fprintf(stderr, "PNG_COL_FROM_PASS_COL(%u, %d) = %u != %x\n",
9847                v, pass, m, f);
9848             exit(99);
9849          }
9850
9851          m = PNG_ROW_IN_INTERLACE_PASS(v, pass);
9852          f = png_row_in_interlace_pass(v, pass);
9853          if (m != f)
9854          {
9855             fprintf(stderr, "PNG_ROW_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9856                v, pass, m, f);
9857             exit(99);
9858          }
9859
9860          m = PNG_COL_IN_INTERLACE_PASS(v, pass);
9861          f = png_col_in_interlace_pass(v, pass);
9862          if (m != f)
9863          {
9864             fprintf(stderr, "PNG_COL_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9865                v, pass, m, f);
9866             exit(99);
9867          }
9868
9869          /* Then the base 1 stuff: */
9870          ++v;
9871          m = PNG_PASS_ROWS(v, pass);
9872          f = png_pass_rows(v, pass);
9873          if (m != f)
9874          {
9875             fprintf(stderr, "PNG_PASS_ROWS(%u, %d) = %u != %x\n",
9876                v, pass, m, f);
9877             exit(99);
9878          }
9879
9880          m = PNG_PASS_COLS(v, pass);
9881          f = png_pass_cols(v, pass);
9882          if (m != f)
9883          {
9884             fprintf(stderr, "PNG_PASS_COLS(%u, %d) = %u != %x\n",
9885                v, pass, m, f);
9886             exit(99);
9887          }
9888
9889          /* Move to the next v - the stepping algorithm starts skipping
9890           * values above 1024.
9891           */
9892          if (v > 1024)
9893          {
9894             if (v == PNG_UINT_31_MAX)
9895                break;
9896
9897             v = (v << 1) ^ v;
9898             if (v >= PNG_UINT_31_MAX)
9899                v = PNG_UINT_31_MAX-1;
9900          }
9901       }
9902    }
9903 }
9904
9905 /* Test color encodings. These values are back-calculated from the published
9906  * chromaticities.  The values are accurate to about 14 decimal places; 15 are
9907  * given.  These values are much more accurate than the ones given in the spec,
9908  * which typically don't exceed 4 decimal places.  This allows testing of the
9909  * libpng code to its theoretical accuracy of 4 decimal places.  (If pngvalid
9910  * used the published errors the 'slack' permitted would have to be +/-.5E-4 or
9911  * more.)
9912  *
9913  * The png_modifier code assumes that encodings[0] is sRGB and treats it
9914  * specially: do not change the first entry in this list!
9915  */
9916 static PNG_CONST color_encoding test_encodings[] =
9917 {
9918 /* sRGB: must be first in this list! */
9919 /*gamma:*/ { 1/2.2,
9920 /*red:  */ { 0.412390799265959, 0.212639005871510, 0.019330818715592 },
9921 /*green:*/ { 0.357584339383878, 0.715168678767756, 0.119194779794626 },
9922 /*blue: */ { 0.180480788401834, 0.072192315360734, 0.950532152249660} },
9923 /* Kodak ProPhoto (wide gamut) */
9924 /*gamma:*/ { 1/1.6 /*approximate: uses 1.8 power law compared to sRGB 2.4*/,
9925 /*red:  */ { 0.797760489672303, 0.288071128229293, 0.000000000000000 },
9926 /*green:*/ { 0.135185837175740, 0.711843217810102, 0.000000000000000 },
9927 /*blue: */ { 0.031349349581525, 0.000085653960605, 0.825104602510460} },
9928 /* Adobe RGB (1998) */
9929 /*gamma:*/ { 1/(2+51./256),
9930 /*red:  */ { 0.576669042910131, 0.297344975250536, 0.027031361386412 },
9931 /*green:*/ { 0.185558237906546, 0.627363566255466, 0.070688852535827 },
9932 /*blue: */ { 0.188228646234995, 0.075291458493998, 0.991337536837639} },
9933 /* Adobe Wide Gamut RGB */
9934 /*gamma:*/ { 1/(2+51./256),
9935 /*red:  */ { 0.716500716779386, 0.258728243040113, 0.000000000000000 },
9936 /*green:*/ { 0.101020574397477, 0.724682314948566, 0.051211818965388 },
9937 /*blue: */ { 0.146774385252705, 0.016589442011321, 0.773892783545073} },
9938 };
9939
9940 /* signal handler
9941  *
9942  * This attempts to trap signals and escape without crashing.  It needs a
9943  * context pointer so that it can throw an exception (call longjmp) to recover
9944  * from the condition; this is handled by making the png_modifier used by 'main'
9945  * into a global variable.
9946  */
9947 static png_modifier pm;
9948
9949 static void signal_handler(int signum)
9950 {
9951
9952    size_t pos = 0;
9953    char msg[64];
9954
9955    pos = safecat(msg, sizeof msg, pos, "caught signal: ");
9956
9957    switch (signum)
9958    {
9959       case SIGABRT:
9960          pos = safecat(msg, sizeof msg, pos, "abort");
9961          break;
9962
9963       case SIGFPE:
9964          pos = safecat(msg, sizeof msg, pos, "floating point exception");
9965          break;
9966
9967       case SIGILL:
9968          pos = safecat(msg, sizeof msg, pos, "illegal instruction");
9969          break;
9970
9971       case SIGINT:
9972          pos = safecat(msg, sizeof msg, pos, "interrupt");
9973          break;
9974
9975       case SIGSEGV:
9976          pos = safecat(msg, sizeof msg, pos, "invalid memory access");
9977          break;
9978
9979       case SIGTERM:
9980          pos = safecat(msg, sizeof msg, pos, "termination request");
9981          break;
9982
9983       default:
9984          pos = safecat(msg, sizeof msg, pos, "unknown ");
9985          pos = safecatn(msg, sizeof msg, pos, signum);
9986          break;
9987    }
9988
9989    store_log(&pm.this, NULL/*png_structp*/, msg, 1/*error*/);
9990
9991    /* And finally throw an exception so we can keep going, unless this is
9992     * SIGTERM in which case stop now.
9993     */
9994    if (signum != SIGTERM)
9995    {
9996       struct exception_context *the_exception_context =
9997          &pm.this.exception_context;
9998
9999       Throw &pm.this;
10000    }
10001
10002    else
10003       exit(1);
10004 }
10005
10006 /* main program */
10007 int main(int argc, char **argv)
10008 {
10009    volatile int summary = 1;  /* Print the error summary at the end */
10010    volatile int memstats = 0; /* Print memory statistics at the end */
10011
10012    /* Create the given output file on success: */
10013    PNG_CONST char *volatile touch = NULL;
10014
10015    /* This is an array of standard gamma values (believe it or not I've seen
10016     * every one of these mentioned somewhere.)
10017     *
10018     * In the following list the most useful values are first!
10019     */
10020    static double
10021       gammas[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9};
10022
10023    /* This records the command and arguments: */
10024    size_t cp = 0;
10025    char command[1024];
10026
10027    anon_context(&pm.this);
10028
10029    /* Add appropriate signal handlers, just the ANSI specified ones: */
10030    signal(SIGABRT, signal_handler);
10031    signal(SIGFPE, signal_handler);
10032    signal(SIGILL, signal_handler);
10033    signal(SIGINT, signal_handler);
10034    signal(SIGSEGV, signal_handler);
10035    signal(SIGTERM, signal_handler);
10036
10037 #ifdef HAVE_FEENABLEEXCEPT
10038    /* Only required to enable FP exceptions on platforms where they start off
10039     * disabled; this is not necessary but if it is not done pngvalid will likely
10040     * end up ignoring FP conditions that other platforms fault.
10041     */
10042    feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
10043 #endif
10044
10045    modifier_init(&pm);
10046
10047    /* Preallocate the image buffer, because we know how big it needs to be,
10048     * note that, for testing purposes, it is deliberately mis-aligned by tag
10049     * bytes either side.  All rows have an additional five bytes of padding for
10050     * overwrite checking.
10051     */
10052    store_ensure_image(&pm.this, NULL, 2, TRANSFORM_ROWMAX, TRANSFORM_HEIGHTMAX);
10053
10054    /* Don't give argv[0], it's normally some horrible libtool string: */
10055    cp = safecat(command, sizeof command, cp, "pngvalid");
10056
10057    /* Default to error on warning: */
10058    pm.this.treat_warnings_as_errors = 1;
10059
10060    /* Default assume_16_bit_calculations appropriately; this tells the checking
10061     * code that 16-bit arithmetic is used for 8-bit samples when it would make a
10062     * difference.
10063     */
10064    pm.assume_16_bit_calculations = PNG_LIBPNG_VER >= 10700;
10065
10066    /* Currently 16 bit expansion happens at the end of the pipeline, so the
10067     * calculations are done in the input bit depth not the output.
10068     *
10069     * TODO: fix this
10070     */
10071    pm.calculations_use_input_precision = 1U;
10072
10073    /* Store the test gammas */
10074    pm.gammas = gammas;
10075    pm.ngammas = (sizeof gammas) / (sizeof gammas[0]);
10076    pm.ngamma_tests = 0; /* default to off */
10077
10078    /* And the test encodings */
10079    pm.encodings = test_encodings;
10080    pm.nencodings = (sizeof test_encodings) / (sizeof test_encodings[0]);
10081
10082    pm.sbitlow = 8U; /* because libpng doesn't do sBIT below 8! */
10083
10084    /* The following allows results to pass if they correspond to anything in the
10085     * transformed range [input-.5,input+.5]; this is is required because of the
10086     * way libpng treates the 16_TO_8 flag when building the gamma tables in
10087     * releases up to 1.6.0.
10088     *
10089     * TODO: review this
10090     */
10091    pm.use_input_precision_16to8 = 1U;
10092    pm.use_input_precision_sbit = 1U; /* because libpng now rounds sBIT */
10093
10094    /* Some default values (set the behavior for 'make check' here).
10095     * These values simply control the maximum error permitted in the gamma
10096     * transformations.  The practial limits for human perception are described
10097     * below (the setting for maxpc16), however for 8 bit encodings it isn't
10098     * possible to meet the accepted capabilities of human vision - i.e. 8 bit
10099     * images can never be good enough, regardless of encoding.
10100     */
10101    pm.maxout8 = .1;     /* Arithmetic error in *encoded* value */
10102    pm.maxabs8 = .00005; /* 1/20000 */
10103    pm.maxcalc8 = 1./255;  /* +/-1 in 8 bits for compose errors */
10104    pm.maxpc8 = .499;    /* I.e., .499% fractional error */
10105    pm.maxout16 = .499;  /* Error in *encoded* value */
10106    pm.maxabs16 = .00005;/* 1/20000 */
10107    pm.maxcalc16 =1./65535;/* +/-1 in 16 bits for compose errors */
10108    pm.maxcalcG = 1./((1<<PNG_MAX_GAMMA_8)-1);
10109
10110    /* NOTE: this is a reasonable perceptual limit. We assume that humans can
10111     * perceive light level differences of 1% over a 100:1 range, so we need to
10112     * maintain 1 in 10000 accuracy (in linear light space), which is what the
10113     * following guarantees.  It also allows significantly higher errors at
10114     * higher 16 bit values, which is important for performance.  The actual
10115     * maximum 16 bit error is about +/-1.9 in the fixed point implementation but
10116     * this is only allowed for values >38149 by the following:
10117     */
10118    pm.maxpc16 = .005;   /* I.e., 1/200% - 1/20000 */
10119
10120    /* Now parse the command line options. */
10121    while (--argc >= 1)
10122    {
10123       int catmore = 0; /* Set if the argument has an argument. */
10124
10125       /* Record each argument for posterity: */
10126       cp = safecat(command, sizeof command, cp, " ");
10127       cp = safecat(command, sizeof command, cp, *++argv);
10128
10129       if (strcmp(*argv, "-v") == 0)
10130          pm.this.verbose = 1;
10131
10132       else if (strcmp(*argv, "-l") == 0)
10133          pm.log = 1;
10134
10135       else if (strcmp(*argv, "-q") == 0)
10136          summary = pm.this.verbose = pm.log = 0;
10137
10138       else if (strcmp(*argv, "-w") == 0)
10139          pm.this.treat_warnings_as_errors = 0;
10140
10141       else if (strcmp(*argv, "--speed") == 0)
10142          pm.this.speed = 1, pm.ngamma_tests = pm.ngammas, pm.test_standard = 0,
10143             summary = 0;
10144
10145       else if (strcmp(*argv, "--memory") == 0)
10146          memstats = 1;
10147
10148       else if (strcmp(*argv, "--size") == 0)
10149          pm.test_size = 1;
10150
10151       else if (strcmp(*argv, "--nosize") == 0)
10152          pm.test_size = 0;
10153
10154       else if (strcmp(*argv, "--standard") == 0)
10155          pm.test_standard = 1;
10156
10157       else if (strcmp(*argv, "--nostandard") == 0)
10158          pm.test_standard = 0;
10159
10160       else if (strcmp(*argv, "--transform") == 0)
10161          pm.test_transform = 1;
10162
10163       else if (strcmp(*argv, "--notransform") == 0)
10164          pm.test_transform = 0;
10165
10166 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10167       else if (strncmp(*argv, "--transform-disable=",
10168          sizeof "--transform-disable") == 0)
10169          {
10170          pm.test_transform = 1;
10171          transform_disable(*argv + sizeof "--transform-disable");
10172          }
10173
10174       else if (strncmp(*argv, "--transform-enable=",
10175          sizeof "--transform-enable") == 0)
10176          {
10177          pm.test_transform = 1;
10178          transform_enable(*argv + sizeof "--transform-enable");
10179          }
10180 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10181
10182       else if (strcmp(*argv, "--gamma") == 0)
10183          {
10184          /* Just do two gamma tests here (2.2 and linear) for speed: */
10185          pm.ngamma_tests = 2U;
10186          pm.test_gamma_threshold = 1;
10187          pm.test_gamma_transform = 1;
10188          pm.test_gamma_sbit = 1;
10189          pm.test_gamma_scale16 = 1;
10190          pm.test_gamma_background = 1;
10191          pm.test_gamma_alpha_mode = 1;
10192          }
10193
10194       else if (strcmp(*argv, "--nogamma") == 0)
10195          pm.ngamma_tests = 0;
10196
10197       else if (strcmp(*argv, "--gamma-threshold") == 0)
10198          pm.ngamma_tests = 2U, pm.test_gamma_threshold = 1;
10199
10200       else if (strcmp(*argv, "--nogamma-threshold") == 0)
10201          pm.test_gamma_threshold = 0;
10202
10203       else if (strcmp(*argv, "--gamma-transform") == 0)
10204          pm.ngamma_tests = 2U, pm.test_gamma_transform = 1;
10205
10206       else if (strcmp(*argv, "--nogamma-transform") == 0)
10207          pm.test_gamma_transform = 0;
10208
10209       else if (strcmp(*argv, "--gamma-sbit") == 0)
10210          pm.ngamma_tests = 2U, pm.test_gamma_sbit = 1;
10211
10212       else if (strcmp(*argv, "--nogamma-sbit") == 0)
10213          pm.test_gamma_sbit = 0;
10214
10215       else if (strcmp(*argv, "--gamma-16-to-8") == 0)
10216          pm.ngamma_tests = 2U, pm.test_gamma_scale16 = 1;
10217
10218       else if (strcmp(*argv, "--nogamma-16-to-8") == 0)
10219          pm.test_gamma_scale16 = 0;
10220
10221       else if (strcmp(*argv, "--gamma-background") == 0)
10222          pm.ngamma_tests = 2U, pm.test_gamma_background = 1;
10223
10224       else if (strcmp(*argv, "--nogamma-background") == 0)
10225          pm.test_gamma_background = 0;
10226
10227       else if (strcmp(*argv, "--gamma-alpha-mode") == 0)
10228          pm.ngamma_tests = 2U, pm.test_gamma_alpha_mode = 1;
10229
10230       else if (strcmp(*argv, "--nogamma-alpha-mode") == 0)
10231          pm.test_gamma_alpha_mode = 0;
10232
10233       else if (strcmp(*argv, "--expand16") == 0)
10234          pm.test_gamma_expand16 = 1;
10235
10236       else if (strcmp(*argv, "--noexpand16") == 0)
10237          pm.test_gamma_expand16 = 0;
10238
10239       else if (strcmp(*argv, "--more-gammas") == 0)
10240          pm.ngamma_tests = 3U;
10241
10242       else if (strcmp(*argv, "--all-gammas") == 0)
10243          pm.ngamma_tests = pm.ngammas;
10244
10245       else if (strcmp(*argv, "--progressive-read") == 0)
10246          pm.this.progressive = 1;
10247
10248       else if (strcmp(*argv, "--use-update-info") == 0)
10249          ++pm.use_update_info; /* Can call multiple times */
10250
10251       else if (strcmp(*argv, "--interlace") == 0)
10252       {
10253 #        ifdef PNG_WRITE_INTERLACING_SUPPORTED
10254             pm.interlace_type = PNG_INTERLACE_ADAM7;
10255 #        else
10256             fprintf(stderr, "pngvalid: no write interlace support\n");
10257             return SKIP;
10258 #        endif
10259       }
10260
10261       else if (strcmp(*argv, "--use-input-precision") == 0)
10262          pm.use_input_precision = 1U;
10263
10264       else if (strcmp(*argv, "--use-calculation-precision") == 0)
10265          pm.use_input_precision = 0;
10266
10267       else if (strcmp(*argv, "--calculations-use-input-precision") == 0)
10268          pm.calculations_use_input_precision = 1U;
10269
10270       else if (strcmp(*argv, "--assume-16-bit-calculations") == 0)
10271          pm.assume_16_bit_calculations = 1U;
10272
10273       else if (strcmp(*argv, "--calculations-follow-bit-depth") == 0)
10274          pm.calculations_use_input_precision =
10275             pm.assume_16_bit_calculations = 0;
10276
10277       else if (strcmp(*argv, "--exhaustive") == 0)
10278          pm.test_exhaustive = 1;
10279
10280       else if (argc > 1 && strcmp(*argv, "--sbitlow") == 0)
10281          --argc, pm.sbitlow = (png_byte)atoi(*++argv), catmore = 1;
10282
10283       else if (argc > 1 && strcmp(*argv, "--touch") == 0)
10284          --argc, touch = *++argv, catmore = 1;
10285
10286       else if (argc > 1 && strncmp(*argv, "--max", 5) == 0)
10287       {
10288          --argc;
10289
10290          if (strcmp(5+*argv, "abs8") == 0)
10291             pm.maxabs8 = atof(*++argv);
10292
10293          else if (strcmp(5+*argv, "abs16") == 0)
10294             pm.maxabs16 = atof(*++argv);
10295
10296          else if (strcmp(5+*argv, "calc8") == 0)
10297             pm.maxcalc8 = atof(*++argv);
10298
10299          else if (strcmp(5+*argv, "calc16") == 0)
10300             pm.maxcalc16 = atof(*++argv);
10301
10302          else if (strcmp(5+*argv, "out8") == 0)
10303             pm.maxout8 = atof(*++argv);
10304
10305          else if (strcmp(5+*argv, "out16") == 0)
10306             pm.maxout16 = atof(*++argv);
10307
10308          else if (strcmp(5+*argv, "pc8") == 0)
10309             pm.maxpc8 = atof(*++argv);
10310
10311          else if (strcmp(5+*argv, "pc16") == 0)
10312             pm.maxpc16 = atof(*++argv);
10313
10314          else
10315          {
10316             fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv);
10317             exit(99);
10318          }
10319
10320          catmore = 1;
10321       }
10322
10323       else if (strcmp(*argv, "--log8") == 0)
10324          --argc, pm.log8 = atof(*++argv), catmore = 1;
10325
10326       else if (strcmp(*argv, "--log16") == 0)
10327          --argc, pm.log16 = atof(*++argv), catmore = 1;
10328
10329 #ifdef PNG_SET_OPTION_SUPPORTED
10330       else if (strncmp(*argv, "--option=", 9) == 0)
10331       {
10332          /* Syntax of the argument is <option>:{on|off} */
10333          const char *arg = 9+*argv;
10334          unsigned char option=0, setting=0;
10335
10336 #ifdef PNG_ARM_NEON_API_SUPPORTED
10337          if (strncmp(arg, "arm-neon:", 9) == 0)
10338             option = PNG_ARM_NEON, arg += 9;
10339
10340          else
10341 #endif
10342 #ifdef PNG_MAXIMUM_INFLATE_WINDOW
10343          if (strncmp(arg, "max-inflate-window:", 19) == 0)
10344             option = PNG_MAXIMUM_INFLATE_WINDOW, arg += 19;
10345
10346          else
10347 #endif
10348          {
10349             fprintf(stderr, "pngvalid: %s: %s: unknown option\n", *argv, arg);
10350             exit(99);
10351          }
10352
10353          if (strcmp(arg, "off") == 0)
10354             setting = PNG_OPTION_OFF;
10355
10356          else if (strcmp(arg, "on") == 0)
10357             setting = PNG_OPTION_ON;
10358
10359          else
10360          {
10361             fprintf(stderr,
10362                "pngvalid: %s: %s: unknown setting (use 'on' or 'off')\n",
10363                *argv, arg);
10364             exit(99);
10365          }
10366
10367          pm.this.options[pm.this.noptions].option = option;
10368          pm.this.options[pm.this.noptions++].setting = setting;
10369       }
10370 #endif /* PNG_SET_OPTION_SUPPORTED */
10371
10372       else
10373       {
10374          fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv);
10375          exit(99);
10376       }
10377
10378       if (catmore) /* consumed an extra *argv */
10379       {
10380          cp = safecat(command, sizeof command, cp, " ");
10381          cp = safecat(command, sizeof command, cp, *argv);
10382       }
10383    }
10384
10385    /* If pngvalid is run with no arguments default to a reasonable set of the
10386     * tests.
10387     */
10388    if (pm.test_standard == 0 && pm.test_size == 0 && pm.test_transform == 0 &&
10389       pm.ngamma_tests == 0)
10390    {
10391       /* Make this do all the tests done in the test shell scripts with the same
10392        * parameters, where possible.  The limitation is that all the progressive
10393        * read and interlace stuff has to be done in separate runs, so only the
10394        * basic 'standard' and 'size' tests are done.
10395        */
10396       pm.test_standard = 1;
10397       pm.test_size = 1;
10398       pm.test_transform = 1;
10399       pm.ngamma_tests = 2U;
10400    }
10401
10402    if (pm.ngamma_tests > 0 &&
10403       pm.test_gamma_threshold == 0 && pm.test_gamma_transform == 0 &&
10404       pm.test_gamma_sbit == 0 && pm.test_gamma_scale16 == 0 &&
10405       pm.test_gamma_background == 0 && pm.test_gamma_alpha_mode == 0)
10406    {
10407       pm.test_gamma_threshold = 1;
10408       pm.test_gamma_transform = 1;
10409       pm.test_gamma_sbit = 1;
10410       pm.test_gamma_scale16 = 1;
10411       pm.test_gamma_background = 1;
10412       pm.test_gamma_alpha_mode = 1;
10413    }
10414
10415    else if (pm.ngamma_tests == 0)
10416    {
10417       /* Nothing to test so turn everything off: */
10418       pm.test_gamma_threshold = 0;
10419       pm.test_gamma_transform = 0;
10420       pm.test_gamma_sbit = 0;
10421       pm.test_gamma_scale16 = 0;
10422       pm.test_gamma_background = 0;
10423       pm.test_gamma_alpha_mode = 0;
10424    }
10425
10426    Try
10427    {
10428       /* Make useful base images */
10429       make_transform_images(&pm.this);
10430
10431       /* Perform the standard and gamma tests. */
10432       if (pm.test_standard)
10433       {
10434          perform_interlace_macro_validation();
10435          perform_formatting_test(&pm.this);
10436 #        ifdef PNG_READ_SUPPORTED
10437             perform_standard_test(&pm);
10438 #        endif
10439          perform_error_test(&pm);
10440       }
10441
10442       /* Various oddly sized images: */
10443       if (pm.test_size)
10444       {
10445          make_size_images(&pm.this);
10446 #        ifdef PNG_READ_SUPPORTED
10447             perform_size_test(&pm);
10448 #        endif
10449       }
10450
10451 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10452       /* Combinatorial transforms: */
10453       if (pm.test_transform)
10454          perform_transform_test(&pm);
10455 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10456
10457 #ifdef PNG_READ_GAMMA_SUPPORTED
10458       if (pm.ngamma_tests > 0)
10459          perform_gamma_test(&pm, summary);
10460 #endif
10461    }
10462
10463    Catch_anonymous
10464    {
10465       fprintf(stderr, "pngvalid: test aborted (probably failed in cleanup)\n");
10466       if (!pm.this.verbose)
10467       {
10468          if (pm.this.error[0] != 0)
10469             fprintf(stderr, "pngvalid: first error: %s\n", pm.this.error);
10470
10471          fprintf(stderr, "pngvalid: run with -v to see what happened\n");
10472       }
10473       exit(1);
10474    }
10475
10476    if (summary)
10477    {
10478       printf("%s: %s (%s point arithmetic)\n",
10479          (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
10480             pm.this.nwarnings)) ? "FAIL" : "PASS",
10481          command,
10482 #if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500
10483          "floating"
10484 #else
10485          "fixed"
10486 #endif
10487          );
10488    }
10489
10490    if (memstats)
10491    {
10492       printf("Allocated memory statistics (in bytes):\n"
10493          "\tread  %lu maximum single, %lu peak, %lu total\n"
10494          "\twrite %lu maximum single, %lu peak, %lu total\n",
10495          (unsigned long)pm.this.read_memory_pool.max_max,
10496          (unsigned long)pm.this.read_memory_pool.max_limit,
10497          (unsigned long)pm.this.read_memory_pool.max_total,
10498          (unsigned long)pm.this.write_memory_pool.max_max,
10499          (unsigned long)pm.this.write_memory_pool.max_limit,
10500          (unsigned long)pm.this.write_memory_pool.max_total);
10501    }
10502
10503    /* Do this here to provoke memory corruption errors in memory not directly
10504     * allocated by libpng - not a complete test, but better than nothing.
10505     */
10506    store_delete(&pm.this);
10507
10508    /* Error exit if there are any errors, and maybe if there are any
10509     * warnings.
10510     */
10511    if (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
10512        pm.this.nwarnings))
10513    {
10514       if (!pm.this.verbose)
10515          fprintf(stderr, "pngvalid: %s\n", pm.this.error);
10516
10517       fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors,
10518           pm.this.nwarnings);
10519
10520       exit(1);
10521    }
10522
10523    /* Success case. */
10524    if (touch != NULL)
10525    {
10526       FILE *fsuccess = fopen(touch, "wt");
10527
10528       if (fsuccess != NULL)
10529       {
10530          int error = 0;
10531          fprintf(fsuccess, "PNG validation succeeded\n");
10532          fflush(fsuccess);
10533          error = ferror(fsuccess);
10534
10535          if (fclose(fsuccess) || error)
10536          {
10537             fprintf(stderr, "%s: write failed\n", touch);
10538             exit(1);
10539          }
10540       }
10541
10542       else
10543       {
10544          fprintf(stderr, "%s: open failed\n", touch);
10545          exit(1);
10546       }
10547    }
10548
10549    /* This is required because some very minimal configurations do not use it:
10550     */
10551    UNUSED(fail)
10552    return 0;
10553 }
10554 #else /* write or low level APIs not supported */
10555 int main(void)
10556 {
10557    fprintf(stderr,
10558       "pngvalid: no low level write support in libpng, all tests skipped\n");
10559    /* So the test is skipped: */
10560    return SKIP;
10561 }
10562 #endif