2 /* pngvalid.c - validate libpng by constructing then reading png files.
4 * Last changed in libpng 1.6.10 [March 6, 2014]
5 * Copyright (c) 2014 Glenn Randers-Pehrson
6 * Written by John Cunningham Bowler
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
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.
18 * The program can be modified and extended to test the correctness of
19 * transformations performed by libpng.
22 #define _POSIX_SOURCE 1
23 #define _ISOC99_SOURCE 1 /* For floating point */
24 #define _GNU_SOURCE 1 /* For the floating point exception extension */
29 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
33 #ifdef HAVE_FEENABLEEXCEPT /* from config.h, if included */
37 /* Define the following to use this test against your installed libpng, rather
38 * than the one being built here:
40 #ifdef PNG_FREESTANDING_TESTS
43 # include "../../png.h"
46 #ifdef PNG_ZLIB_HEADER
47 # include PNG_ZLIB_HEADER
49 # include <zlib.h> /* For crc32 */
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:
55 #if PNG_LIBPNG_VER < 10601
61 /* pngvalid requires write support and one of the fixed or floating point APIs.
63 #if defined(PNG_WRITE_SUPPORTED) &&\
64 (defined(PNG_FIXED_POINT_SUPPORTED) || defined(PNG_FLOATING_POINT_SUPPORTED))
66 #if PNG_LIBPNG_VER < 10500
67 /* This deliberately lacks the PNG_CONST. */
68 typedef png_byte *png_const_bytep;
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)
92 /* These are needed too for the default build: */
93 #define PNG_WRITE_16BIT_SUPPORTED
94 #define PNG_READ_16BIT_SUPPORTED
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))
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.
106 # define png_const_structp png_structp
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 */
114 /* Unused formal parameter errors are removed using the following macro which is
115 * expected to have no bad effects on performance.
118 # if defined(__GNUC__) || defined(_MSC_VER)
119 # define UNUSED(param) (void)param;
121 # define UNUSED(param)
125 /***************************** EXCEPTION HANDLING *****************************/
126 #ifdef PNG_FREESTANDING_TESTS
127 # include <cexcept.h>
129 # include "../visupng/cexcept.h"
133 # define this not_the_cpp_this
134 # define new not_the_cpp_new
135 # define voidcast(type, value) static_cast<type>(value)
137 # define voidcast(type, value) (value)
138 #endif /* __cplusplus */
141 define_exception_type(struct png_store*);
143 /* The following are macros to reduce typing everywhere where the well known
144 * name 'the_exception_context' must be defined.
146 #define anon_context(ps) struct exception_context *the_exception_context = \
147 &(ps)->exception_context
148 #define context(ps,fault) anon_context(ps); png_store *fault
150 /******************************* UTILITIES ************************************/
151 /* Error handling is particularly problematic in production code - error
152 * handlers often themselves have bugs which lead to programs that detect
153 * minor errors crashing. The following functions deal with one very
154 * common class of errors in error handlers - attempting to format error or
155 * warning messages into buffers that are too small.
157 static size_t safecat(char *buffer, size_t bufsize, size_t pos,
160 while (pos < bufsize && cat != NULL && *cat != 0)
161 buffer[pos++] = *cat++;
170 static size_t safecatn(char *buffer, size_t bufsize, size_t pos, int n)
173 sprintf(number, "%d", n);
174 return safecat(buffer, bufsize, pos, number);
177 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
178 static size_t safecatd(char *buffer, size_t bufsize, size_t pos, double d,
182 sprintf(number, "%.*f", precision, d);
183 return safecat(buffer, bufsize, pos, number);
187 static PNG_CONST char invalid[] = "invalid";
188 static PNG_CONST char sep[] = ": ";
190 static PNG_CONST char *colour_types[8] =
192 "grayscale", invalid, "truecolour", "indexed-colour",
193 "grayscale with alpha", invalid, "truecolour with alpha", invalid
196 #ifdef PNG_READ_SUPPORTED
197 /* Convert a double precision value to fixed point. */
198 static png_fixed_point
201 d = floor(d * PNG_FP_1 + .5);
202 return (png_fixed_point)d;
204 #endif /* PNG_READ_SUPPORTED */
206 /* Generate random bytes. This uses a boring repeatable algorithm and it
207 * is implemented here so that it gives the same set of numbers on every
208 * architecture. It's a linear congruential generator (Knuth or Sedgewick
209 * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
210 * Hill, "The Art of Electronics" (Pseudo-Random Bit Sequences and Noise
214 make_random_bytes(png_uint_32* seed, void* pv, size_t size)
216 png_uint_32 u0 = seed[0], u1 = seed[1];
217 png_bytep bytes = voidcast(png_bytep, pv);
219 /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
220 * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
223 for (i=0; i<size; ++i)
225 /* First generate 8 new bits then shift them in at the end. */
226 png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff;
231 *bytes++ = (png_byte)u;
239 make_four_random_bytes(png_uint_32* seed, png_bytep bytes)
241 make_random_bytes(seed, bytes, 4);
244 #ifdef PNG_READ_SUPPORTED
246 randomize(void *pv, size_t size)
248 static png_uint_32 random_seed[2] = {0x56789abc, 0xd};
249 make_random_bytes(random_seed, pv, size);
252 #define RANDOMIZE(this) randomize(&(this), sizeof (this))
255 random_mod(unsigned int max)
261 return x % max; /* 0 .. max-1 */
264 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
275 #endif /* PNG_READ_SUPPORTED */
277 /* A numeric ID based on PNG file characteristics. The 'do_interlace' field
278 * simply records whether pngvalid did the interlace itself or whether it
279 * was done by libpng. Width and height must be less than 256. 'palette' is an
280 * index of the palette to use for formats with a palette (0 otherwise.)
282 #define FILEID(col, depth, palette, interlace, width, height, do_interlace) \
283 ((png_uint_32)((col) + ((depth)<<3) + ((palette)<<8) + ((interlace)<<13) + \
284 (((do_interlace)!=0)<<15) + ((width)<<16) + ((height)<<24)))
286 #define COL_FROM_ID(id) ((png_byte)((id)& 0x7U))
287 #define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU))
288 #define PALETTE_FROM_ID(id) (((id) >> 8) & 0x1f)
289 #define INTERLACE_FROM_ID(id) ((int)(((id) >> 13) & 0x3))
290 #define DO_INTERLACE_FROM_ID(id) ((int)(((id)>>15) & 1))
291 #define WIDTH_FROM_ID(id) (((id)>>16) & 0xff)
292 #define HEIGHT_FROM_ID(id) (((id)>>24) & 0xff)
294 /* Utility to construct a standard name for a standard image. */
296 standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type,
297 int bit_depth, unsigned int npalette, int interlace_type,
298 png_uint_32 w, png_uint_32 h, int do_interlace)
300 pos = safecat(buffer, bufsize, pos, colour_types[colour_type]);
303 pos = safecat(buffer, bufsize, pos, "[");
304 pos = safecatn(buffer, bufsize, pos, npalette);
305 pos = safecat(buffer, bufsize, pos, "]");
307 pos = safecat(buffer, bufsize, pos, " ");
308 pos = safecatn(buffer, bufsize, pos, bit_depth);
309 pos = safecat(buffer, bufsize, pos, " bit");
311 if (interlace_type != PNG_INTERLACE_NONE)
313 pos = safecat(buffer, bufsize, pos, " interlaced");
315 pos = safecat(buffer, bufsize, pos, "(pngvalid)");
317 pos = safecat(buffer, bufsize, pos, "(libpng)");
322 pos = safecat(buffer, bufsize, pos, " ");
323 pos = safecatn(buffer, bufsize, pos, w);
324 pos = safecat(buffer, bufsize, pos, "x");
325 pos = safecatn(buffer, bufsize, pos, h);
332 standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id)
334 return standard_name(buffer, bufsize, pos, COL_FROM_ID(id),
335 DEPTH_FROM_ID(id), PALETTE_FROM_ID(id), INTERLACE_FROM_ID(id),
336 WIDTH_FROM_ID(id), HEIGHT_FROM_ID(id), DO_INTERLACE_FROM_ID(id));
339 /* Convenience API and defines to list valid formats. Note that 16 bit read and
340 * write support is required to do 16 bit read tests (we must be able to make a
341 * 16 bit image to test!)
343 #ifdef PNG_WRITE_16BIT_SUPPORTED
344 # define WRITE_BDHI 4
345 # ifdef PNG_READ_16BIT_SUPPORTED
350 # define WRITE_BDHI 3
356 /* The following defines the number of different palettes to generate for
357 * each log bit depth of a colour type 3 standard image.
359 #define PALETTE_COUNT(bit_depth) ((bit_depth) > 4 ? 1U : 16U)
362 next_format(png_bytep colour_type, png_bytep bit_depth,
363 unsigned int* palette_number, int no_low_depth_gray)
368 if (no_low_depth_gray)
376 if (*colour_type == 3)
378 /* Add multiple palettes for colour type 3. */
379 if (++*palette_number < PALETTE_COUNT(*bit_depth))
385 *bit_depth = (png_byte)(*bit_depth << 1);
387 /* Palette images are restricted to 8 bit depth */
390 || (*colour_type != 3 && *bit_depth <= 16)
395 /* Move to the next color type, or return 0 at the end. */
396 switch (*colour_type)
423 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
425 sample(png_const_bytep row, png_byte colour_type, png_byte bit_depth,
426 png_uint_32 x, unsigned int sample_index)
428 png_uint_32 bit_index, result;
430 /* Find a sample index for the desired sample: */
434 if ((colour_type & 1) == 0) /* !palette */
440 bit_index += x; /* Alpha channel */
442 /* Multiple channels; select one: */
443 if (colour_type & (2+4))
444 bit_index += sample_index * bit_depth;
447 /* Return the sample from the row as an integer. */
448 row += bit_index >> 3;
454 else if (bit_depth > 8)
455 return (result << 8) + *++row;
457 /* Less than 8 bits per sample. */
459 return (result >> (8-bit_index-bit_depth)) & ((1U<<bit_depth)-1);
461 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
463 /* Copy a single pixel, of a given size, from one buffer to another -
464 * while this is basically bit addressed there is an implicit assumption
465 * that pixels 8 or more bits in size are byte aligned and that pixels
466 * do not otherwise cross byte boundaries. (This is, so far as I know,
467 * universally true in bitmap computer graphics. [JCB 20101212])
469 * NOTE: The to and from buffers may be the same.
472 pixel_copy(png_bytep toBuffer, png_uint_32 toIndex,
473 png_const_bytep fromBuffer, png_uint_32 fromIndex, unsigned int pixelSize)
475 /* Assume we can multiply by 'size' without overflow because we are
476 * just working in a single buffer.
478 toIndex *= pixelSize;
479 fromIndex *= pixelSize;
480 if (pixelSize < 8) /* Sub-byte */
482 /* Mask to select the location of the copied pixel: */
483 unsigned int destMask = ((1U<<pixelSize)-1) << (8-pixelSize-(toIndex&7));
484 /* The following read the entire pixels and clears the extra: */
485 unsigned int destByte = toBuffer[toIndex >> 3] & ~destMask;
486 unsigned int sourceByte = fromBuffer[fromIndex >> 3];
488 /* Don't rely on << or >> supporting '0' here, just in case: */
490 if (fromIndex > 0) sourceByte <<= fromIndex;
491 if ((toIndex & 7) > 0) sourceByte >>= toIndex & 7;
493 toBuffer[toIndex >> 3] = (png_byte)(destByte | (sourceByte & destMask));
495 else /* One or more bytes */
496 memmove(toBuffer+(toIndex>>3), fromBuffer+(fromIndex>>3), pixelSize>>3);
499 #ifdef PNG_READ_SUPPORTED
500 /* Copy a complete row of pixels, taking into account potential partial
504 row_copy(png_bytep toBuffer, png_const_bytep fromBuffer, unsigned int bitWidth)
506 memcpy(toBuffer, fromBuffer, bitWidth >> 3);
508 if ((bitWidth & 7) != 0)
512 toBuffer += bitWidth >> 3;
513 fromBuffer += bitWidth >> 3;
514 /* The remaining bits are in the top of the byte, the mask is the bits to
517 mask = 0xff >> (bitWidth & 7);
518 *toBuffer = (png_byte)((*toBuffer & mask) | (*fromBuffer & ~mask));
522 /* Compare pixels - they are assumed to start at the first byte in the
526 pixel_cmp(png_const_bytep pa, png_const_bytep pb, png_uint_32 bit_width)
528 #if PNG_LIBPNG_VER < 10506
529 if (memcmp(pa, pb, bit_width>>3) == 0)
533 if ((bit_width & 7) == 0) return 0;
535 /* Ok, any differences? */
536 p = pa[bit_width >> 3];
537 p ^= pb[bit_width >> 3];
539 if (p == 0) return 0;
541 /* There are, but they may not be significant, remove the bits
542 * after the end (the low order bits in PNG.)
547 if (p == 0) return 0;
550 /* From libpng-1.5.6 the overwrite should be fixed, so compare the trailing
553 if (memcmp(pa, pb, (bit_width+7)>>3) == 0)
557 /* Return the index of the changed byte. */
559 png_uint_32 where = 0;
561 while (pa[where] == pb[where]) ++where;
565 #endif /* PNG_READ_SUPPORTED */
567 /*************************** BASIC PNG FILE WRITING ***************************/
568 /* A png_store takes data from the sequential writer or provides data
569 * to the sequential reader. It can also store the result of a PNG
570 * write for later retrieval.
572 #define STORE_BUFFER_SIZE 500 /* arbitrary */
573 typedef struct png_store_buffer
575 struct png_store_buffer* prev; /* NOTE: stored in reverse order */
576 png_byte buffer[STORE_BUFFER_SIZE];
579 #define FILE_NAME_SIZE 64
581 typedef struct store_palette_entry /* record of a single palette entry */
587 } store_palette_entry, store_palette[256];
589 typedef struct png_store_file
591 struct png_store_file* next; /* as many as you like... */
592 char name[FILE_NAME_SIZE];
593 png_uint_32 id; /* must be correct (see FILEID) */
594 png_size_t datacount; /* In this (the last) buffer */
595 png_store_buffer data; /* Last buffer in file */
596 int npalette; /* Number of entries in palette */
597 store_palette_entry* palette; /* May be NULL */
600 /* The following is a pool of memory allocated by a single libpng read or write
603 typedef struct store_pool
605 struct png_store *store; /* Back pointer */
606 struct store_memory *list; /* List of allocated memory */
607 png_byte mark[4]; /* Before and after data */
609 /* Statistics for this run. */
610 png_alloc_size_t max; /* Maximum single allocation */
611 png_alloc_size_t current; /* Current allocation */
612 png_alloc_size_t limit; /* Highest current allocation */
613 png_alloc_size_t total; /* Total allocation */
615 /* Overall statistics (retained across successive runs). */
616 png_alloc_size_t max_max;
617 png_alloc_size_t max_limit;
618 png_alloc_size_t max_total;
621 typedef struct png_store
623 /* For cexcept.h exception handling - simply store one of these;
624 * the context is a self pointer but it may point to a different
625 * png_store (in fact it never does in this program.)
627 struct exception_context
630 unsigned int verbose :1;
631 unsigned int treat_warnings_as_errors :1;
632 unsigned int expect_error :1;
633 unsigned int expect_warning :1;
634 unsigned int saw_warning :1;
635 unsigned int speed :1;
636 unsigned int progressive :1; /* use progressive read */
637 unsigned int validated :1; /* used as a temporary flag */
640 int noptions; /* number of options below: */
642 unsigned char option; /* option number, 0..30 */
643 unsigned char setting; /* setting (unset,invalid,on,off) */
645 char test[128]; /* Name of test */
649 png_structp pread; /* Used to read a saved file */
651 png_store_file* current; /* Set when reading */
652 png_store_buffer* next; /* Set when reading */
653 png_size_t readpos; /* Position in *next */
654 png_byte* image; /* Buffer for reading interlaced images */
655 png_size_t cb_image; /* Size of this buffer */
656 png_size_t cb_row; /* Row size of the image(s) */
657 png_uint_32 image_h; /* Number of rows in a single image */
658 store_pool read_memory_pool;
661 png_store_file* saved;
662 png_structp pwrite; /* Used when writing a new file */
664 png_size_t writepos; /* Position in .new */
665 char wname[FILE_NAME_SIZE];
666 png_store_buffer new; /* The end of the new PNG file being written. */
667 store_pool write_memory_pool;
668 store_palette_entry* palette;
672 /* Initialization and cleanup */
674 store_pool_mark(png_bytep mark)
676 static png_uint_32 store_seed[2] = { 0x12345678, 1};
678 make_four_random_bytes(store_seed, mark);
681 #ifdef PNG_READ_SUPPORTED
682 /* Use this for random 32 bit values; this function makes sure the result is
694 store_pool_mark(mark);
695 result = png_get_uint_32(mark);
701 #endif /* PNG_READ_SUPPORTED */
704 store_pool_init(png_store *ps, store_pool *pool)
706 memset(pool, 0, sizeof *pool);
710 pool->max = pool->current = pool->limit = pool->total = 0;
711 pool->max_max = pool->max_limit = pool->max_total = 0;
712 store_pool_mark(pool->mark);
716 store_init(png_store* ps)
718 memset(ps, 0, sizeof *ps);
719 init_exception_context(&ps->exception_context);
720 store_pool_init(ps, &ps->read_memory_pool);
721 store_pool_init(ps, &ps->write_memory_pool);
723 ps->treat_warnings_as_errors = 0;
724 ps->expect_error = 0;
725 ps->expect_warning = 0;
730 ps->nerrors = ps->nwarnings = 0;
733 ps->saved = ps->current = NULL;
750 store_freebuffer(png_store_buffer* psb)
754 store_freebuffer(psb->prev);
761 store_freenew(png_store *ps)
763 store_freebuffer(&ps->new);
765 if (ps->palette != NULL)
774 store_storenew(png_store *ps)
776 png_store_buffer *pb;
778 if (ps->writepos != STORE_BUFFER_SIZE)
779 png_error(ps->pwrite, "invalid store call");
781 pb = voidcast(png_store_buffer*, malloc(sizeof *pb));
784 png_error(ps->pwrite, "store new: OOM");
792 store_freefile(png_store_file **ppf)
796 store_freefile(&(*ppf)->next);
798 store_freebuffer(&(*ppf)->data);
799 (*ppf)->datacount = 0;
800 if ((*ppf)->palette != NULL)
802 free((*ppf)->palette);
803 (*ppf)->palette = NULL;
804 (*ppf)->npalette = 0;
811 /* Main interface to file storeage, after writing a new PNG file (see the API
812 * below) call store_storefile to store the result with the given name and id.
815 store_storefile(png_store *ps, png_uint_32 id)
817 png_store_file *pf = voidcast(png_store_file*, malloc(sizeof *pf));
819 png_error(ps->pwrite, "storefile: OOM");
820 safecat(pf->name, sizeof pf->name, 0, ps->wname);
823 pf->datacount = ps->writepos;
826 pf->palette = ps->palette;
827 pf->npalette = ps->npalette;
832 pf->next = ps->saved;
836 /* Generate an error message (in the given buffer) */
838 store_message(png_store *ps, png_const_structp pp, char *buffer, size_t bufsize,
839 size_t pos, PNG_CONST char *msg)
841 if (pp != NULL && pp == ps->pread)
844 pos = safecat(buffer, bufsize, pos, "read: ");
846 if (ps->current != NULL)
848 pos = safecat(buffer, bufsize, pos, ps->current->name);
849 pos = safecat(buffer, bufsize, pos, sep);
853 else if (pp != NULL && pp == ps->pwrite)
856 pos = safecat(buffer, bufsize, pos, "write: ");
857 pos = safecat(buffer, bufsize, pos, ps->wname);
858 pos = safecat(buffer, bufsize, pos, sep);
863 /* Neither reading nor writing (or a memory error in struct delete) */
864 pos = safecat(buffer, bufsize, pos, "pngvalid: ");
867 if (ps->test[0] != 0)
869 pos = safecat(buffer, bufsize, pos, ps->test);
870 pos = safecat(buffer, bufsize, pos, sep);
872 pos = safecat(buffer, bufsize, pos, msg);
876 /* Verbose output to the error stream: */
878 store_verbose(png_store *ps, png_const_structp pp, png_const_charp prefix,
879 png_const_charp message)
884 fputs(prefix, stderr);
886 (void)store_message(ps, pp, buffer, sizeof buffer, 0, message);
887 fputs(buffer, stderr);
891 /* Log an error or warning - the relevant count is always incremented. */
893 store_log(png_store* ps, png_const_structp pp, png_const_charp message,
896 /* The warning is copied to the error buffer if there are no errors and it is
897 * the first warning. The error is copied to the error buffer if it is the
898 * first error (overwriting any prior warnings).
900 if (is_error ? (ps->nerrors)++ == 0 :
901 (ps->nwarnings)++ == 0 && ps->nerrors == 0)
902 store_message(ps, pp, ps->error, sizeof ps->error, 0, message);
905 store_verbose(ps, pp, is_error ? "error: " : "warning: ", message);
908 #ifdef PNG_READ_SUPPORTED
909 /* Internal error function, called with a png_store but no libpng stuff. */
911 internal_error(png_store *ps, png_const_charp message)
913 store_log(ps, NULL, message, 1 /* error */);
915 /* And finally throw an exception. */
917 struct exception_context *the_exception_context = &ps->exception_context;
921 #endif /* PNG_READ_SUPPORTED */
923 /* Functions to use as PNG callbacks. */
925 store_error(png_structp ppIn, png_const_charp message) /* PNG_NORETURN */
927 png_const_structp pp = ppIn;
928 png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
930 if (!ps->expect_error)
931 store_log(ps, pp, message, 1 /* error */);
933 /* And finally throw an exception. */
935 struct exception_context *the_exception_context = &ps->exception_context;
941 store_warning(png_structp ppIn, png_const_charp message)
943 png_const_structp pp = ppIn;
944 png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
946 if (!ps->expect_warning)
947 store_log(ps, pp, message, 0 /* warning */);
952 /* These somewhat odd functions are used when reading an image to ensure that
953 * the buffer is big enough, the png_structp is for errors.
955 /* Return a single row from the correct image. */
957 store_image_row(PNG_CONST png_store* ps, png_const_structp pp, int nImage,
960 png_size_t coffset = (nImage * ps->image_h + y) * (ps->cb_row + 5) + 2;
962 if (ps->image == NULL)
963 png_error(pp, "no allocated image");
965 if (coffset + ps->cb_row + 3 > ps->cb_image)
966 png_error(pp, "image too small");
968 return ps->image + coffset;
972 store_image_free(png_store *ps, png_const_structp pp)
974 if (ps->image != NULL)
976 png_bytep image = ps->image;
978 if (image[-1] != 0xed || image[ps->cb_image] != 0xfe)
981 png_error(pp, "png_store image overwrite (1)");
983 store_log(ps, NULL, "png_store image overwrite (2)", 1);
994 store_ensure_image(png_store *ps, png_const_structp pp, int nImages,
995 png_size_t cbRow, png_uint_32 cRows)
997 png_size_t cb = nImages * cRows * (cbRow + 5);
999 if (ps->cb_image < cb)
1003 store_image_free(ps, pp);
1005 /* The buffer is deliberately mis-aligned. */
1006 image = voidcast(png_bytep, malloc(cb+2));
1009 /* Called from the startup - ignore the error for the moment. */
1013 png_error(pp, "OOM allocating image buffer");
1016 /* These magic tags are used to detect overwrites above. */
1025 /* We have an adequate sized image; lay out the rows. There are 2 bytes at
1026 * the start and three at the end of each (this ensures that the row
1027 * alignment starts out odd - 2+1 and changes for larger images on each row.)
1030 ps->image_h = cRows;
1032 /* For error checking, the whole buffer is set to 10110010 (0xb2 - 178).
1033 * This deliberately doesn't match the bits in the size test image which are
1034 * outside the image; these are set to 0xff (all 1). To make the row
1035 * comparison work in the 'size' test case the size rows are pre-initialized
1036 * to the same value prior to calling 'standard_row'.
1038 memset(ps->image, 178, cb);
1040 /* Then put in the marks. */
1041 while (--nImages >= 0)
1045 for (y=0; y<cRows; ++y)
1047 png_bytep row = store_image_row(ps, pp, nImages, y);
1059 #ifdef PNG_READ_SUPPORTED
1061 store_image_check(PNG_CONST png_store* ps, png_const_structp pp, int iImage)
1063 png_const_bytep image = ps->image;
1065 if (image[-1] != 0xed || image[ps->cb_image] != 0xfe)
1066 png_error(pp, "image overwrite");
1069 png_size_t cbRow = ps->cb_row;
1070 png_uint_32 rows = ps->image_h;
1072 image += iImage * (cbRow+5) * ps->image_h;
1074 image += 2; /* skip image first row markers */
1078 if (image[-2] != 190 || image[-1] != 239)
1079 png_error(pp, "row start overwritten");
1081 if (image[cbRow] != 222 || image[cbRow+1] != 173 ||
1082 image[cbRow+2] != 17)
1083 png_error(pp, "row end overwritten");
1089 #endif /* PNG_READ_SUPPORTED */
1091 static void PNGCBAPI
1092 store_write(png_structp ppIn, png_bytep pb, png_size_t st)
1094 png_const_structp pp = ppIn;
1095 png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
1097 if (ps->pwrite != pp)
1098 png_error(pp, "store state damaged");
1104 if (ps->writepos >= STORE_BUFFER_SIZE)
1109 if (cb > STORE_BUFFER_SIZE - ps->writepos)
1110 cb = STORE_BUFFER_SIZE - ps->writepos;
1112 memcpy(ps->new.buffer + ps->writepos, pb, cb);
1119 static void PNGCBAPI
1120 store_flush(png_structp ppIn)
1122 UNUSED(ppIn) /*DOES NOTHING*/
1125 #ifdef PNG_READ_SUPPORTED
1127 store_read_buffer_size(png_store *ps)
1129 /* Return the bytes available for read in the current buffer. */
1130 if (ps->next != &ps->current->data)
1131 return STORE_BUFFER_SIZE;
1133 return ps->current->datacount;
1136 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1137 /* Return total bytes available for read. */
1139 store_read_buffer_avail(png_store *ps)
1141 if (ps->current != NULL && ps->next != NULL)
1143 png_store_buffer *next = &ps->current->data;
1144 size_t cbAvail = ps->current->datacount;
1146 while (next != ps->next && next != NULL)
1149 cbAvail += STORE_BUFFER_SIZE;
1152 if (next != ps->next)
1153 png_error(ps->pread, "buffer read error");
1155 if (cbAvail > ps->readpos)
1156 return cbAvail - ps->readpos;
1164 store_read_buffer_next(png_store *ps)
1166 png_store_buffer *pbOld = ps->next;
1167 png_store_buffer *pbNew = &ps->current->data;
1170 while (pbNew != NULL && pbNew->prev != pbOld)
1171 pbNew = pbNew->prev;
1180 png_error(ps->pread, "buffer lost");
1183 return 0; /* EOF or error */
1186 /* Need separate implementation and callback to allow use of the same code
1187 * during progressive read, where the io_ptr is set internally by libpng.
1190 store_read_imp(png_store *ps, png_bytep pb, png_size_t st)
1192 if (ps->current == NULL || ps->next == NULL)
1193 png_error(ps->pread, "store state damaged");
1197 size_t cbAvail = store_read_buffer_size(ps) - ps->readpos;
1201 if (cbAvail > st) cbAvail = st;
1202 memcpy(pb, ps->next->buffer + ps->readpos, cbAvail);
1205 ps->readpos += cbAvail;
1208 else if (!store_read_buffer_next(ps))
1209 png_error(ps->pread, "read beyond end of file");
1213 static void PNGCBAPI
1214 store_read(png_structp ppIn, png_bytep pb, png_size_t st)
1216 png_const_structp pp = ppIn;
1217 png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
1219 if (ps == NULL || ps->pread != pp)
1220 png_error(pp, "bad store read call");
1222 store_read_imp(ps, pb, st);
1226 store_progressive_read(png_store *ps, png_structp pp, png_infop pi)
1228 /* Notice that a call to store_read will cause this function to fail because
1229 * readpos will be set.
1231 if (ps->pread != pp || ps->current == NULL || ps->next == NULL)
1232 png_error(pp, "store state damaged (progressive)");
1236 if (ps->readpos != 0)
1237 png_error(pp, "store_read called during progressive read");
1239 png_process_data(pp, pi, ps->next->buffer, store_read_buffer_size(ps));
1241 while (store_read_buffer_next(ps));
1243 #endif /* PNG_READ_SUPPORTED */
1245 /* The caller must fill this in: */
1246 static store_palette_entry *
1247 store_write_palette(png_store *ps, int npalette)
1249 if (ps->pwrite == NULL)
1250 store_log(ps, NULL, "attempt to write palette without write stream", 1);
1252 if (ps->palette != NULL)
1253 png_error(ps->pwrite, "multiple store_write_palette calls");
1255 /* This function can only return NULL if called with '0'! */
1258 ps->palette = voidcast(store_palette_entry*, malloc(npalette *
1259 sizeof *ps->palette));
1261 if (ps->palette == NULL)
1262 png_error(ps->pwrite, "store new palette: OOM");
1264 ps->npalette = npalette;
1270 #ifdef PNG_READ_SUPPORTED
1271 static store_palette_entry *
1272 store_current_palette(png_store *ps, int *npalette)
1274 /* This is an internal error (the call has been made outside a read
1277 if (ps->current == NULL)
1278 store_log(ps, ps->pread, "no current stream for palette", 1);
1280 /* The result may be null if there is no palette. */
1281 *npalette = ps->current->npalette;
1282 return ps->current->palette;
1284 #endif /* PNG_READ_SUPPORTED */
1286 /***************************** MEMORY MANAGEMENT*** ***************************/
1287 #ifdef PNG_USER_MEM_SUPPORTED
1288 /* A store_memory is simply the header for an allocated block of memory. The
1289 * pointer returned to libpng is just after the end of the header block, the
1290 * allocated memory is followed by a second copy of the 'mark'.
1292 typedef struct store_memory
1294 store_pool *pool; /* Originating pool */
1295 struct store_memory *next; /* Singly linked list */
1296 png_alloc_size_t size; /* Size of memory allocated */
1297 png_byte mark[4]; /* ID marker */
1300 /* Handle a fatal error in memory allocation. This calls png_error if the
1301 * libpng struct is non-NULL, else it outputs a message and returns. This means
1302 * that a memory problem while libpng is running will abort (png_error) the
1303 * handling of particular file while one in cleanup (after the destroy of the
1304 * struct has returned) will simply keep going and free (or attempt to free)
1308 store_pool_error(png_store *ps, png_const_structp pp, PNG_CONST char *msg)
1313 /* Else we have to do it ourselves. png_error eventually calls store_log,
1314 * above. store_log accepts a NULL png_structp - it just changes what gets
1315 * output by store_message.
1317 store_log(ps, pp, msg, 1 /* error */);
1321 store_memory_free(png_const_structp pp, store_pool *pool, store_memory *memory)
1323 /* Note that pp may be NULL (see store_pool_delete below), the caller has
1324 * found 'memory' in pool->list *and* unlinked this entry, so this is a valid
1325 * pointer (for sure), but the contents may have been trashed.
1327 if (memory->pool != pool)
1328 store_pool_error(pool->store, pp, "memory corrupted (pool)");
1330 else if (memcmp(memory->mark, pool->mark, sizeof memory->mark) != 0)
1331 store_pool_error(pool->store, pp, "memory corrupted (start)");
1333 /* It should be safe to read the size field now. */
1336 png_alloc_size_t cb = memory->size;
1339 store_pool_error(pool->store, pp, "memory corrupted (size)");
1341 else if (memcmp((png_bytep)(memory+1)+cb, pool->mark, sizeof pool->mark)
1343 store_pool_error(pool->store, pp, "memory corrupted (end)");
1345 /* Finally give the library a chance to find problems too: */
1348 pool->current -= cb;
1355 store_pool_delete(png_store *ps, store_pool *pool)
1357 if (pool->list != NULL)
1359 fprintf(stderr, "%s: %s %s: memory lost (list follows):\n", ps->test,
1360 pool == &ps->read_memory_pool ? "read" : "write",
1361 pool == &ps->read_memory_pool ? (ps->current != NULL ?
1362 ps->current->name : "unknown file") : ps->wname);
1367 store_memory *next = pool->list;
1368 pool->list = next->next;
1371 fprintf(stderr, "\t%lu bytes @ %p\n",
1372 (unsigned long)next->size, (PNG_CONST void*)(next+1));
1373 /* The NULL means this will always return, even if the memory is
1376 store_memory_free(NULL, pool, next);
1378 while (pool->list != NULL);
1381 /* And reset the other fields too for the next time. */
1382 if (pool->max > pool->max_max) pool->max_max = pool->max;
1384 if (pool->current != 0) /* unexpected internal error */
1385 fprintf(stderr, "%s: %s %s: memory counter mismatch (internal error)\n",
1386 ps->test, pool == &ps->read_memory_pool ? "read" : "write",
1387 pool == &ps->read_memory_pool ? (ps->current != NULL ?
1388 ps->current->name : "unknown file") : ps->wname);
1391 if (pool->limit > pool->max_limit)
1392 pool->max_limit = pool->limit;
1396 if (pool->total > pool->max_total)
1397 pool->max_total = pool->total;
1401 /* Get a new mark too. */
1402 store_pool_mark(pool->mark);
1405 /* The memory callbacks: */
1406 static png_voidp PNGCBAPI
1407 store_malloc(png_structp ppIn, png_alloc_size_t cb)
1409 png_const_structp pp = ppIn;
1410 store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
1411 store_memory *new = voidcast(store_memory*, malloc(cb + (sizeof *new) +
1412 (sizeof pool->mark)));
1419 pool->current += cb;
1421 if (pool->current > pool->limit)
1422 pool->limit = pool->current;
1427 memcpy(new->mark, pool->mark, sizeof new->mark);
1428 memcpy((png_byte*)(new+1) + cb, pool->mark, sizeof pool->mark);
1430 new->next = pool->list;
1437 /* NOTE: the PNG user malloc function cannot use the png_ptr it is passed
1438 * other than to retrieve the allocation pointer! libpng calls the
1439 * store_malloc callback in two basic cases:
1441 * 1) From png_malloc; png_malloc will do a png_error itself if NULL is
1443 * 2) From png_struct or png_info structure creation; png_malloc is
1444 * to return so cleanup can be performed.
1446 * To handle this store_malloc can log a message, but can't do anything
1449 store_log(pool->store, pp, "out of memory", 1 /* is_error */);
1455 static void PNGCBAPI
1456 store_free(png_structp ppIn, png_voidp memory)
1458 png_const_structp pp = ppIn;
1459 store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
1460 store_memory *this = voidcast(store_memory*, memory), **test;
1462 /* Because libpng calls store_free with a dummy png_struct when deleting
1463 * png_struct or png_info via png_destroy_struct_2 it is necessary to check
1464 * the passed in png_structp to ensure it is valid, and not pass it to
1465 * png_error if it is not.
1467 if (pp != pool->store->pread && pp != pool->store->pwrite)
1470 /* First check that this 'memory' really is valid memory - it must be in the
1471 * pool list. If it is, use the shared memory_free function to free it.
1474 for (test = &pool->list; *test != this; test = &(*test)->next)
1478 store_pool_error(pool->store, pp, "bad pointer to free");
1483 /* Unlink this entry, *test == this. */
1486 store_memory_free(pp, pool, this);
1488 #endif /* PNG_USER_MEM_SUPPORTED */
1490 /* Setup functions. */
1491 /* Cleanup when aborting a write or after storing the new file. */
1493 store_write_reset(png_store *ps)
1495 if (ps->pwrite != NULL)
1500 png_destroy_write_struct(&ps->pwrite, &ps->piwrite);
1504 /* memory corruption: continue. */
1511 /* And make sure that all the memory has been freed - this will output
1512 * spurious errors in the case of memory corruption above, but this is safe.
1514 # ifdef PNG_USER_MEM_SUPPORTED
1515 store_pool_delete(ps, &ps->write_memory_pool);
1521 /* The following is the main write function, it returns a png_struct and,
1522 * optionally, a png_info suitable for writiing a new PNG file. Use
1523 * store_storefile above to record this file after it has been written. The
1524 * returned libpng structures as destroyed by store_write_reset above.
1527 set_store_for_write(png_store *ps, png_infopp ppi,
1528 PNG_CONST char * volatile name)
1534 if (ps->pwrite != NULL)
1535 png_error(ps->pwrite, "write store already in use");
1537 store_write_reset(ps);
1538 safecat(ps->wname, sizeof ps->wname, 0, name);
1540 /* Don't do the slow memory checks if doing a speed test, also if user
1541 * memory is not supported we can't do it anyway.
1543 # ifdef PNG_USER_MEM_SUPPORTED
1545 ps->pwrite = png_create_write_struct_2(PNG_LIBPNG_VER_STRING,
1546 ps, store_error, store_warning, &ps->write_memory_pool,
1547 store_malloc, store_free);
1551 ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING,
1552 ps, store_error, store_warning);
1554 png_set_write_fn(ps->pwrite, ps, store_write, store_flush);
1556 # ifdef PNG_SET_OPTION_SUPPORTED
1559 for (opt=0; opt<ps->noptions; ++opt)
1560 if (png_set_option(ps->pwrite, ps->options[opt].option,
1561 ps->options[opt].setting) == PNG_OPTION_INVALID)
1562 png_error(ps->pwrite, "png option invalid");
1567 *ppi = ps->piwrite = png_create_info_struct(ps->pwrite);
1576 /* Cleanup when finished reading (either due to error or in the success case).
1577 * This routine exists even when there is no read support to make the code
1578 * tidier (avoid a mass of ifdefs) and so easier to maintain.
1581 store_read_reset(png_store *ps)
1583 # ifdef PNG_READ_SUPPORTED
1584 if (ps->pread != NULL)
1589 png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
1593 /* error already output: continue */
1601 # ifdef PNG_USER_MEM_SUPPORTED
1602 /* Always do this to be safe. */
1603 store_pool_delete(ps, &ps->read_memory_pool);
1612 #ifdef PNG_READ_SUPPORTED
1614 store_read_set(png_store *ps, png_uint_32 id)
1616 png_store_file *pf = ps->saved;
1624 store_read_buffer_next(ps);
1633 char msg[FILE_NAME_SIZE+64];
1635 pos = standard_name_from_id(msg, sizeof msg, 0, id);
1636 pos = safecat(msg, sizeof msg, pos, ": file not found");
1637 png_error(ps->pread, msg);
1641 /* The main interface for reading a saved file - pass the id number of the file
1642 * to retrieve. Ids must be unique or the earlier file will be hidden. The API
1643 * returns a png_struct and, optionally, a png_info. Both of these will be
1644 * destroyed by store_read_reset above.
1647 set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id,
1648 PNG_CONST char *name)
1650 /* Set the name for png_error */
1651 safecat(ps->test, sizeof ps->test, 0, name);
1653 if (ps->pread != NULL)
1654 png_error(ps->pread, "read store already in use");
1656 store_read_reset(ps);
1658 /* Both the create APIs can return NULL if used in their default mode
1659 * (because there is no other way of handling an error because the jmp_buf
1660 * by default is stored in png_struct and that has not been allocated!)
1661 * However, given that store_error works correctly in these circumstances
1662 * we don't ever expect NULL in this program.
1664 # ifdef PNG_USER_MEM_SUPPORTED
1666 ps->pread = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, ps,
1667 store_error, store_warning, &ps->read_memory_pool, store_malloc,
1672 ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps, store_error,
1675 if (ps->pread == NULL)
1677 struct exception_context *the_exception_context = &ps->exception_context;
1679 store_log(ps, NULL, "png_create_read_struct returned NULL (unexpected)",
1685 # ifdef PNG_SET_OPTION_SUPPORTED
1688 for (opt=0; opt<ps->noptions; ++opt)
1689 if (png_set_option(ps->pread, ps->options[opt].option,
1690 ps->options[opt].setting) == PNG_OPTION_INVALID)
1691 png_error(ps->pread, "png option invalid");
1695 store_read_set(ps, id);
1698 *ppi = ps->piread = png_create_info_struct(ps->pread);
1702 #endif /* PNG_READ_SUPPORTED */
1704 /* The overall cleanup of a store simply calls the above then removes all the
1705 * saved files. This does not delete the store itself.
1708 store_delete(png_store *ps)
1710 store_write_reset(ps);
1711 store_read_reset(ps);
1712 store_freefile(&ps->saved);
1713 store_image_free(ps, NULL);
1716 /*********************** PNG FILE MODIFICATION ON READ ************************/
1717 /* Files may be modified on read. The following structure contains a complete
1718 * png_store together with extra members to handle modification and a special
1719 * read callback for libpng. To use this the 'modifications' field must be set
1720 * to a list of png_modification structures that actually perform the
1721 * modification, otherwise a png_modifier is functionally equivalent to a
1722 * png_store. There is a special read function, set_modifier_for_read, which
1723 * replaces set_store_for_read.
1725 typedef enum modifier_state
1727 modifier_start, /* Initial value */
1728 modifier_signature, /* Have a signature */
1729 modifier_IHDR /* Have an IHDR */
1732 typedef struct CIE_color
1734 /* A single CIE tristimulus value, representing the unique response of a
1735 * standard observer to a variety of light spectra. The observer recognizes
1736 * all spectra that produce this response as the same color, therefore this
1737 * is effectively a description of a color.
1742 typedef struct color_encoding
1744 /* A description of an (R,G,B) encoding of color (as defined above); this
1745 * includes the actual colors of the (R,G,B) triples (1,0,0), (0,1,0) and
1746 * (0,0,1) plus an encoding value that is used to encode the linear
1747 * components R, G and B to give the actual values R^gamma, G^gamma and
1748 * B^gamma that are stored.
1750 double gamma; /* Encoding (file) gamma of space */
1751 CIE_color red, green, blue; /* End points */
1754 #ifdef PNG_READ_SUPPORTED
1756 chromaticity_x(CIE_color c)
1758 return c.X / (c.X + c.Y + c.Z);
1762 chromaticity_y(CIE_color c)
1764 return c.Y / (c.X + c.Y + c.Z);
1768 white_point(PNG_CONST color_encoding *encoding)
1772 white.X = encoding->red.X + encoding->green.X + encoding->blue.X;
1773 white.Y = encoding->red.Y + encoding->green.Y + encoding->blue.Y;
1774 white.Z = encoding->red.Z + encoding->green.Z + encoding->blue.Z;
1779 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1781 normalize_color_encoding(color_encoding *encoding)
1783 PNG_CONST double whiteY = encoding->red.Y + encoding->green.Y +
1788 encoding->red.X /= whiteY;
1789 encoding->red.Y /= whiteY;
1790 encoding->red.Z /= whiteY;
1791 encoding->green.X /= whiteY;
1792 encoding->green.Y /= whiteY;
1793 encoding->green.Z /= whiteY;
1794 encoding->blue.X /= whiteY;
1795 encoding->blue.Y /= whiteY;
1796 encoding->blue.Z /= whiteY;
1802 safecat_color_encoding(char *buffer, size_t bufsize, size_t pos,
1803 PNG_CONST color_encoding *e, double encoding_gamma)
1807 if (encoding_gamma != 0)
1808 pos = safecat(buffer, bufsize, pos, "(");
1809 pos = safecat(buffer, bufsize, pos, "R(");
1810 pos = safecatd(buffer, bufsize, pos, e->red.X, 4);
1811 pos = safecat(buffer, bufsize, pos, ",");
1812 pos = safecatd(buffer, bufsize, pos, e->red.Y, 4);
1813 pos = safecat(buffer, bufsize, pos, ",");
1814 pos = safecatd(buffer, bufsize, pos, e->red.Z, 4);
1815 pos = safecat(buffer, bufsize, pos, "),G(");
1816 pos = safecatd(buffer, bufsize, pos, e->green.X, 4);
1817 pos = safecat(buffer, bufsize, pos, ",");
1818 pos = safecatd(buffer, bufsize, pos, e->green.Y, 4);
1819 pos = safecat(buffer, bufsize, pos, ",");
1820 pos = safecatd(buffer, bufsize, pos, e->green.Z, 4);
1821 pos = safecat(buffer, bufsize, pos, "),B(");
1822 pos = safecatd(buffer, bufsize, pos, e->blue.X, 4);
1823 pos = safecat(buffer, bufsize, pos, ",");
1824 pos = safecatd(buffer, bufsize, pos, e->blue.Y, 4);
1825 pos = safecat(buffer, bufsize, pos, ",");
1826 pos = safecatd(buffer, bufsize, pos, e->blue.Z, 4);
1827 pos = safecat(buffer, bufsize, pos, ")");
1828 if (encoding_gamma != 0)
1829 pos = safecat(buffer, bufsize, pos, ")");
1832 if (encoding_gamma != 0)
1834 pos = safecat(buffer, bufsize, pos, "^");
1835 pos = safecatd(buffer, bufsize, pos, encoding_gamma, 5);
1840 #endif /* PNG_READ_SUPPORTED */
1842 typedef struct png_modifier
1844 png_store this; /* I am a png_store */
1845 struct png_modification *modifications; /* Changes to make */
1847 modifier_state state; /* My state */
1849 /* Information from IHDR: */
1850 png_byte bit_depth; /* From IHDR */
1851 png_byte colour_type; /* From IHDR */
1853 /* While handling PLTE, IDAT and IEND these chunks may be pended to allow
1854 * other chunks to be inserted.
1856 png_uint_32 pending_len;
1857 png_uint_32 pending_chunk;
1861 unsigned int ngammas;
1862 unsigned int ngamma_tests; /* Number of gamma tests to run*/
1863 double current_gamma; /* 0 if not set */
1864 PNG_CONST color_encoding *encodings;
1865 unsigned int nencodings;
1866 PNG_CONST color_encoding *current_encoding; /* If an encoding has been set */
1867 unsigned int encoding_counter; /* For iteration */
1868 int encoding_ignored; /* Something overwrote it */
1870 /* Control variables used to iterate through possible encodings, the
1871 * following must be set to 0 and tested by the function that uses the
1872 * png_modifier because the modifier only sets it to 1 (true.)
1874 unsigned int repeat :1; /* Repeat this transform test. */
1875 unsigned int test_uses_encoding :1;
1877 /* Lowest sbit to test (libpng fails for sbit < 8) */
1880 /* Error control - these are the limits on errors accepted by the gamma tests
1883 double maxout8; /* Maximum output value error */
1884 double maxabs8; /* Absolute sample error 0..1 */
1885 double maxcalc8; /* Absolute sample error 0..1 */
1886 double maxpc8; /* Percentage sample error 0..100% */
1887 double maxout16; /* Maximum output value error */
1888 double maxabs16; /* Absolute sample error 0..1 */
1889 double maxcalc16;/* Absolute sample error 0..1 */
1890 double maxcalcG; /* Absolute sample error 0..1 */
1891 double maxpc16; /* Percentage sample error 0..100% */
1893 /* This is set by transforms that need to allow a higher limit, it is an
1894 * internal check on pngvalid to ensure that the calculated error limits are
1895 * not ridiculous; without this it is too easy to make a mistake in pngvalid
1896 * that allows any value through.
1898 double limit; /* limit on error values, normally 4E-3 */
1900 /* Log limits - values above this are logged, but not necessarily
1903 double log8; /* Absolute error in 8 bits to log */
1904 double log16; /* Absolute error in 16 bits to log */
1906 /* Logged 8 and 16 bit errors ('output' values): */
1907 double error_gray_2;
1908 double error_gray_4;
1909 double error_gray_8;
1910 double error_gray_16;
1911 double error_color_8;
1912 double error_color_16;
1913 double error_indexed;
1916 /* Whether to call png_read_update_info, not png_read_start_image, and how
1917 * many times to call it.
1919 int use_update_info;
1921 /* Whether or not to interlace. */
1922 int interlace_type :9; /* int, but must store '1' */
1924 /* Run the standard tests? */
1925 unsigned int test_standard :1;
1927 /* Run the odd-sized image and interlace read/write tests? */
1928 unsigned int test_size :1;
1930 /* Run tests on reading with a combination of transforms, */
1931 unsigned int test_transform :1;
1933 /* When to use the use_input_precision option, this controls the gamma
1934 * validation code checks. If set any value that is within the transformed
1935 * range input-.5 to input+.5 will be accepted, otherwise the value must be
1936 * within the normal limits. It should not be necessary to set this; the
1937 * result should always be exact within the permitted error limits.
1939 unsigned int use_input_precision :1;
1940 unsigned int use_input_precision_sbit :1;
1941 unsigned int use_input_precision_16to8 :1;
1943 /* If set assume that the calculation bit depth is set by the input
1944 * precision, not the output precision.
1946 unsigned int calculations_use_input_precision :1;
1948 /* If set assume that the calculations are done in 16 bits even if the sample
1951 unsigned int assume_16_bit_calculations :1;
1953 /* Which gamma tests to run: */
1954 unsigned int test_gamma_threshold :1;
1955 unsigned int test_gamma_transform :1; /* main tests */
1956 unsigned int test_gamma_sbit :1;
1957 unsigned int test_gamma_scale16 :1;
1958 unsigned int test_gamma_background :1;
1959 unsigned int test_gamma_alpha_mode :1;
1960 unsigned int test_gamma_expand16 :1;
1961 unsigned int test_exhaustive :1;
1963 unsigned int log :1; /* Log max error */
1965 /* Buffer information, the buffer size limits the size of the chunks that can
1966 * be modified - they must fit (including header and CRC) into the buffer!
1968 size_t flush; /* Count of bytes to flush */
1969 size_t buffer_count; /* Bytes in buffer */
1970 size_t buffer_position; /* Position in buffer */
1971 png_byte buffer[1024];
1974 /* This returns true if the test should be stopped now because it has already
1975 * failed and it is running silently.
1977 static int fail(png_modifier *pm)
1979 return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 ||
1980 (pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0));
1984 modifier_init(png_modifier *pm)
1986 memset(pm, 0, sizeof *pm);
1987 store_init(&pm->this);
1988 pm->modifications = NULL;
1989 pm->state = modifier_start;
1992 pm->ngamma_tests = 0;
1994 pm->current_gamma = 0;
1997 pm->current_encoding = 0;
1998 pm->encoding_counter = 0;
1999 pm->encoding_ignored = 0;
2001 pm->test_uses_encoding = 0;
2002 pm->maxout8 = pm->maxpc8 = pm->maxabs8 = pm->maxcalc8 = 0;
2003 pm->maxout16 = pm->maxpc16 = pm->maxabs16 = pm->maxcalc16 = 0;
2006 pm->log8 = pm->log16 = 0; /* Means 'off' */
2007 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0;
2008 pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0;
2009 pm->error_indexed = 0;
2010 pm->use_update_info = 0;
2011 pm->interlace_type = PNG_INTERLACE_NONE;
2012 pm->test_standard = 0;
2014 pm->test_transform = 0;
2015 pm->use_input_precision = 0;
2016 pm->use_input_precision_sbit = 0;
2017 pm->use_input_precision_16to8 = 0;
2018 pm->calculations_use_input_precision = 0;
2019 pm->assume_16_bit_calculations = 0;
2020 pm->test_gamma_threshold = 0;
2021 pm->test_gamma_transform = 0;
2022 pm->test_gamma_sbit = 0;
2023 pm->test_gamma_scale16 = 0;
2024 pm->test_gamma_background = 0;
2025 pm->test_gamma_alpha_mode = 0;
2026 pm->test_gamma_expand16 = 0;
2027 pm->test_exhaustive = 0;
2030 /* Rely on the memset for all the other fields - there are no pointers */
2033 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
2035 /* This controls use of checks that explicitly know how libpng digitizes the
2036 * samples in calculations; setting this circumvents simple error limit checking
2037 * in the rgb_to_gray check, replacing it with an exact copy of the libpng 1.5
2040 #define DIGITIZE PNG_LIBPNG_VER < 10700
2042 /* If pm->calculations_use_input_precision is set then operations will happen
2043 * with the precision of the input, not the precision of the output depth.
2045 * If pm->assume_16_bit_calculations is set then even 8 bit calculations use 16
2046 * bit precision. This only affects those of the following limits that pertain
2047 * to a calculation - not a digitization operation - unless the following API is
2050 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2052 static double digitize(double value, int depth, int do_round)
2054 /* 'value' is in the range 0 to 1, the result is the same value rounded to a
2055 * multiple of the digitization factor - 8 or 16 bits depending on both the
2056 * sample depth and the 'assume' setting. Digitization is normally by
2057 * rounding and 'do_round' should be 1, if it is 0 the digitized value will
2060 PNG_CONST unsigned int digitization_factor = (1U << depth) -1;
2062 /* Limiting the range is done as a convenience to the caller - it's easier to
2063 * do it once here than every time at the call site.
2068 else if (value >= 1)
2071 value *= digitization_factor;
2072 if (do_round) value += .5;
2073 return floor(value)/digitization_factor;
2076 #endif /* RGB_TO_GRAY */
2078 #ifdef PNG_READ_GAMMA_SUPPORTED
2079 static double abserr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2081 /* Absolute error permitted in linear values - affected by the bit depth of
2084 if (pm->assume_16_bit_calculations ||
2085 (pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2086 return pm->maxabs16;
2091 static double calcerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2093 /* Error in the linear composition arithmetic - only relevant when
2094 * composition actually happens (0 < alpha < 1).
2096 if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2097 return pm->maxcalc16;
2098 else if (pm->assume_16_bit_calculations)
2099 return pm->maxcalcG;
2101 return pm->maxcalc8;
2104 static double pcerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2106 /* Percentage error permitted in the linear values. Note that the specified
2107 * value is a percentage but this routine returns a simple number.
2109 if (pm->assume_16_bit_calculations ||
2110 (pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2111 return pm->maxpc16 * .01;
2113 return pm->maxpc8 * .01;
2116 /* Output error - the error in the encoded value. This is determined by the
2117 * digitization of the output so can be +/-0.5 in the actual output value. In
2118 * the expand_16 case with the current code in libpng the expand happens after
2119 * all the calculations are done in 8 bit arithmetic, so even though the output
2120 * depth is 16 the output error is determined by the 8 bit calculation.
2122 * This limit is not determined by the bit depth of internal calculations.
2124 * The specified parameter does *not* include the base .5 digitization error but
2127 static double outerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2129 /* There is a serious error in the 2 and 4 bit grayscale transform because
2130 * the gamma table value (8 bits) is simply shifted, not rounded, so the
2131 * error in 4 bit grayscale gamma is up to the value below. This is a hack
2132 * to allow pngvalid to succeed:
2134 * TODO: fix this in libpng
2142 if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2143 return pm->maxout16;
2145 /* This is the case where the value was calculated at 8-bit precision then
2146 * scaled to 16 bits.
2148 else if (out_depth == 16)
2149 return pm->maxout8 * 257;
2155 /* This does the same thing as the above however it returns the value to log,
2156 * rather than raising a warning. This is useful for debugging to track down
2157 * exactly what set of parameters cause high error values.
2159 static double outlog(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
2161 /* The command line parameters are either 8 bit (0..255) or 16 bit (0..65535)
2162 * and so must be adjusted for low bit depth grayscale:
2166 if (pm->log8 == 0) /* switched off */
2170 return pm->log8 / 255 * ((1<<out_depth)-1);
2175 if ((pm->calculations_use_input_precision ? in_depth : out_depth) == 16)
2183 /* This is the case where the value was calculated at 8-bit precision then
2184 * scaled to 16 bits.
2189 return pm->log8 * 257;
2192 /* This complements the above by providing the appropriate quantization for the
2193 * final value. Normally this would just be quantization to an integral value,
2194 * but in the 8 bit calculation case it's actually quantization to a multiple of
2197 static int output_quantization_factor(PNG_CONST png_modifier *pm, int in_depth,
2200 if (out_depth == 16 && in_depth != 16 &&
2201 pm->calculations_use_input_precision)
2206 #endif /* PNG_READ_GAMMA_SUPPORTED */
2208 /* One modification structure must be provided for each chunk to be modified (in
2209 * fact more than one can be provided if multiple separate changes are desired
2210 * for a single chunk.) Modifications include adding a new chunk when a
2211 * suitable chunk does not exist.
2213 * The caller of modify_fn will reset the CRC of the chunk and record 'modified'
2214 * or 'added' as appropriate if the modify_fn returns 1 (true). If the
2215 * modify_fn is NULL the chunk is simply removed.
2217 typedef struct png_modification
2219 struct png_modification *next;
2222 /* If the following is NULL all matching chunks will be removed: */
2223 int (*modify_fn)(struct png_modifier *pm,
2224 struct png_modification *me, int add);
2226 /* If the following is set to PLTE, IDAT or IEND and the chunk has not been
2227 * found and modified (and there is a modify_fn) the modify_fn will be called
2228 * to add the chunk before the relevant chunk.
2231 unsigned int modified :1; /* Chunk was modified */
2232 unsigned int added :1; /* Chunk was added */
2233 unsigned int removed :1; /* Chunk was removed */
2237 modification_reset(png_modification *pmm)
2244 modification_reset(pmm->next);
2249 modification_init(png_modification *pmm)
2251 memset(pmm, 0, sizeof *pmm);
2254 pmm->modify_fn = NULL;
2256 modification_reset(pmm);
2259 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2261 modifier_current_encoding(PNG_CONST png_modifier *pm, color_encoding *ce)
2263 if (pm->current_encoding != 0)
2264 *ce = *pm->current_encoding;
2267 memset(ce, 0, sizeof *ce);
2269 ce->gamma = pm->current_gamma;
2274 safecat_current_encoding(char *buffer, size_t bufsize, size_t pos,
2275 PNG_CONST png_modifier *pm)
2277 pos = safecat_color_encoding(buffer, bufsize, pos, pm->current_encoding,
2280 if (pm->encoding_ignored)
2281 pos = safecat(buffer, bufsize, pos, "[overridden]");
2286 /* Iterate through the usefully testable color encodings. An encoding is one
2289 * 1) Nothing (no color space, no gamma).
2290 * 2) Just a gamma value from the gamma array (including 1.0)
2291 * 3) A color space from the encodings array with the corresponding gamma.
2292 * 4) The same, but with gamma 1.0 (only really useful with 16 bit calculations)
2294 * The iterator selects these in turn, the randomizer selects one at random,
2295 * which is used depends on the setting of the 'test_exhaustive' flag. Notice
2296 * that this function changes the colour space encoding so it must only be
2297 * called on completion of the previous test. This is what 'modifier_reset'
2300 * After the function has been called the 'repeat' flag will still be set; the
2301 * caller of modifier_reset must reset it at the start of each run of the test!
2304 modifier_total_encodings(PNG_CONST png_modifier *pm)
2306 return 1 + /* (1) nothing */
2307 pm->ngammas + /* (2) gamma values to test */
2308 pm->nencodings + /* (3) total number of encodings */
2309 /* The following test only works after the first time through the
2310 * png_modifier code because 'bit_depth' is set when the IHDR is read.
2311 * modifier_reset, below, preserves the setting until after it has called
2312 * the iterate function (also below.)
2314 * For this reason do not rely on this function outside a call to
2317 ((pm->bit_depth == 16 || pm->assume_16_bit_calculations) ?
2318 pm->nencodings : 0); /* (4) encodings with gamma == 1.0 */
2322 modifier_encoding_iterate(png_modifier *pm)
2324 if (!pm->repeat && /* Else something needs the current encoding again. */
2325 pm->test_uses_encoding) /* Some transform is encoding dependent */
2327 if (pm->test_exhaustive)
2329 if (++pm->encoding_counter >= modifier_total_encodings(pm))
2330 pm->encoding_counter = 0; /* This will stop the repeat */
2335 /* Not exhaustive - choose an encoding at random; generate a number in
2336 * the range 1..(max-1), so the result is always non-zero:
2338 if (pm->encoding_counter == 0)
2339 pm->encoding_counter = random_mod(modifier_total_encodings(pm)-1)+1;
2341 pm->encoding_counter = 0;
2344 if (pm->encoding_counter > 0)
2348 else if (!pm->repeat)
2349 pm->encoding_counter = 0;
2353 modifier_reset(png_modifier *pm)
2355 store_read_reset(&pm->this);
2357 pm->pending_len = pm->pending_chunk = 0;
2358 pm->flush = pm->buffer_count = pm->buffer_position = 0;
2359 pm->modifications = NULL;
2360 pm->state = modifier_start;
2361 modifier_encoding_iterate(pm);
2362 /* The following must be set in the next run. In particular
2363 * test_uses_encodings must be set in the _ini function of each transform
2364 * that looks at the encodings. (Not the 'add' function!)
2366 pm->test_uses_encoding = 0;
2367 pm->current_gamma = 0;
2368 pm->current_encoding = 0;
2369 pm->encoding_ignored = 0;
2370 /* These only become value after IHDR is read: */
2371 pm->bit_depth = pm->colour_type = 0;
2374 /* The following must be called before anything else to get the encoding set up
2375 * on the modifier. In particular it must be called before the transform init
2376 * functions are called.
2379 modifier_set_encoding(png_modifier *pm)
2381 /* Set the encoding to the one specified by the current encoding counter,
2382 * first clear out all the settings - this corresponds to an encoding_counter
2385 pm->current_gamma = 0;
2386 pm->current_encoding = 0;
2387 pm->encoding_ignored = 0; /* not ignored yet - happens in _ini functions. */
2389 /* Now, if required, set the gamma and encoding fields. */
2390 if (pm->encoding_counter > 0)
2392 /* The gammas[] array is an array of screen gammas, not encoding gammas,
2393 * so we need the inverse:
2395 if (pm->encoding_counter <= pm->ngammas)
2396 pm->current_gamma = 1/pm->gammas[pm->encoding_counter-1];
2400 unsigned int i = pm->encoding_counter - pm->ngammas;
2402 if (i >= pm->nencodings)
2404 i %= pm->nencodings;
2405 pm->current_gamma = 1; /* Linear, only in the 16 bit case */
2409 pm->current_gamma = pm->encodings[i].gamma;
2411 pm->current_encoding = pm->encodings + i;
2416 /* Enquiry functions to find out what is set. Notice that there is an implicit
2417 * assumption below that the first encoding in the list is the one for sRGB.
2420 modifier_color_encoding_is_sRGB(PNG_CONST png_modifier *pm)
2422 return pm->current_encoding != 0 && pm->current_encoding == pm->encodings &&
2423 pm->current_encoding->gamma == pm->current_gamma;
2427 modifier_color_encoding_is_set(PNG_CONST png_modifier *pm)
2429 return pm->current_gamma != 0;
2432 /* Convenience macros. */
2433 #define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
2434 #define CHUNK_IHDR CHUNK(73,72,68,82)
2435 #define CHUNK_PLTE CHUNK(80,76,84,69)
2436 #define CHUNK_IDAT CHUNK(73,68,65,84)
2437 #define CHUNK_IEND CHUNK(73,69,78,68)
2438 #define CHUNK_cHRM CHUNK(99,72,82,77)
2439 #define CHUNK_gAMA CHUNK(103,65,77,65)
2440 #define CHUNK_sBIT CHUNK(115,66,73,84)
2441 #define CHUNK_sRGB CHUNK(115,82,71,66)
2443 /* The guts of modification are performed during a read. */
2445 modifier_crc(png_bytep buffer)
2447 /* Recalculate the chunk CRC - a complete chunk must be in
2448 * the buffer, at the start.
2450 uInt datalen = png_get_uint_32(buffer);
2451 uLong crc = crc32(0, buffer+4, datalen+4);
2452 /* The cast to png_uint_32 is safe because a crc32 is always a 32 bit value.
2454 png_save_uint_32(buffer+datalen+8, (png_uint_32)crc);
2458 modifier_setbuffer(png_modifier *pm)
2460 modifier_crc(pm->buffer);
2461 pm->buffer_count = png_get_uint_32(pm->buffer)+12;
2462 pm->buffer_position = 0;
2465 /* Separate the callback into the actual implementation (which is passed the
2466 * png_modifier explicitly) and the callback, which gets the modifier from the
2470 modifier_read_imp(png_modifier *pm, png_bytep pb, png_size_t st)
2475 png_uint_32 len, chunk;
2476 png_modification *mod;
2478 if (pm->buffer_position >= pm->buffer_count) switch (pm->state)
2480 static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
2481 case modifier_start:
2482 store_read_imp(&pm->this, pm->buffer, 8); /* size of signature. */
2483 pm->buffer_count = 8;
2484 pm->buffer_position = 0;
2486 if (memcmp(pm->buffer, sign, 8) != 0)
2487 png_error(pm->this.pread, "invalid PNG file signature");
2488 pm->state = modifier_signature;
2491 case modifier_signature:
2492 store_read_imp(&pm->this, pm->buffer, 13+12); /* size of IHDR */
2493 pm->buffer_count = 13+12;
2494 pm->buffer_position = 0;
2496 if (png_get_uint_32(pm->buffer) != 13 ||
2497 png_get_uint_32(pm->buffer+4) != CHUNK_IHDR)
2498 png_error(pm->this.pread, "invalid IHDR");
2500 /* Check the list of modifiers for modifications to the IHDR. */
2501 mod = pm->modifications;
2504 if (mod->chunk == CHUNK_IHDR && mod->modify_fn &&
2505 (*mod->modify_fn)(pm, mod, 0))
2508 modifier_setbuffer(pm);
2511 /* Ignore removal or add if IHDR! */
2515 /* Cache information from the IHDR (the modified one.) */
2516 pm->bit_depth = pm->buffer[8+8];
2517 pm->colour_type = pm->buffer[8+8+1];
2519 pm->state = modifier_IHDR;
2525 /* Read a new chunk and process it until we see PLTE, IDAT or
2526 * IEND. 'flush' indicates that there is still some data to
2527 * output from the preceding chunk.
2529 if ((cb = pm->flush) > 0)
2531 if (cb > st) cb = st;
2533 store_read_imp(&pm->this, pb, cb);
2536 if (st == 0) return;
2539 /* No more bytes to flush, read a header, or handle a pending
2542 if (pm->pending_chunk != 0)
2544 png_save_uint_32(pm->buffer, pm->pending_len);
2545 png_save_uint_32(pm->buffer+4, pm->pending_chunk);
2546 pm->pending_len = 0;
2547 pm->pending_chunk = 0;
2550 store_read_imp(&pm->this, pm->buffer, 8);
2552 pm->buffer_count = 8;
2553 pm->buffer_position = 0;
2555 /* Check for something to modify or a terminator chunk. */
2556 len = png_get_uint_32(pm->buffer);
2557 chunk = png_get_uint_32(pm->buffer+4);
2559 /* Terminators first, they may have to be delayed for added
2562 if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT ||
2563 chunk == CHUNK_IEND)
2565 mod = pm->modifications;
2569 if ((mod->add == chunk ||
2570 (mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT)) &&
2571 mod->modify_fn != NULL && !mod->modified && !mod->added)
2573 /* Regardless of what the modify function does do not run
2578 if ((*mod->modify_fn)(pm, mod, 1 /*add*/))
2580 /* Reset the CRC on a new chunk */
2581 if (pm->buffer_count > 0)
2582 modifier_setbuffer(pm);
2586 pm->buffer_position = 0;
2590 /* The buffer has been filled with something (we assume)
2591 * so output this. Pend the current chunk.
2593 pm->pending_len = len;
2594 pm->pending_chunk = chunk;
2595 break; /* out of while */
2602 /* Don't do any further processing if the buffer was modified -
2603 * otherwise the code will end up modifying a chunk that was
2607 break; /* out of switch */
2610 /* If we get to here then this chunk may need to be modified. To
2611 * do this it must be less than 1024 bytes in total size, otherwise
2612 * it just gets flushed.
2614 if (len+12 <= sizeof pm->buffer)
2616 store_read_imp(&pm->this, pm->buffer+pm->buffer_count,
2617 len+12-pm->buffer_count);
2618 pm->buffer_count = len+12;
2620 /* Check for a modification, else leave it be. */
2621 mod = pm->modifications;
2624 if (mod->chunk == chunk)
2626 if (mod->modify_fn == NULL)
2628 /* Remove this chunk */
2629 pm->buffer_count = pm->buffer_position = 0;
2631 break; /* Terminate the while loop */
2634 else if ((*mod->modify_fn)(pm, mod, 0))
2637 /* The chunk may have been removed: */
2638 if (pm->buffer_count == 0)
2640 pm->buffer_position = 0;
2643 modifier_setbuffer(pm);
2652 pm->flush = len+12 - pm->buffer_count; /* data + crc */
2654 /* Take the data from the buffer (if there is any). */
2658 /* Here to read from the modifier buffer (not directly from
2659 * the store, as in the flush case above.)
2661 cb = pm->buffer_count - pm->buffer_position;
2666 memcpy(pb, pm->buffer + pm->buffer_position, cb);
2669 pm->buffer_position += cb;
2674 static void PNGCBAPI
2675 modifier_read(png_structp ppIn, png_bytep pb, png_size_t st)
2677 png_const_structp pp = ppIn;
2678 png_modifier *pm = voidcast(png_modifier*, png_get_io_ptr(pp));
2680 if (pm == NULL || pm->this.pread != pp)
2681 png_error(pp, "bad modifier_read call");
2683 modifier_read_imp(pm, pb, st);
2686 /* Like store_progressive_read but the data is getting changed as we go so we
2687 * need a local buffer.
2690 modifier_progressive_read(png_modifier *pm, png_structp pp, png_infop pi)
2692 if (pm->this.pread != pp || pm->this.current == NULL ||
2693 pm->this.next == NULL)
2694 png_error(pp, "store state damaged (progressive)");
2696 /* This is another Horowitz and Hill random noise generator. In this case
2697 * the aim is to stress the progressive reader with truly horrible variable
2698 * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers
2699 * is generated. We could probably just count from 1 to 32767 and get as
2704 static png_uint_32 noise = 1;
2705 png_size_t cb, cbAvail;
2706 png_byte buffer[512];
2708 /* Generate 15 more bits of stuff: */
2709 noise = (noise << 9) | ((noise ^ (noise >> (9-5))) & 0x1ff);
2712 /* Check that this number of bytes are available (in the current buffer.)
2713 * (This doesn't quite work - the modifier might delete a chunk; unlikely
2714 * but possible, it doesn't happen at present because the modifier only
2715 * adds chunks to standard images.)
2717 cbAvail = store_read_buffer_avail(&pm->this);
2718 if (pm->buffer_count > pm->buffer_position)
2719 cbAvail += pm->buffer_count - pm->buffer_position;
2723 /* Check for EOF: */
2730 modifier_read_imp(pm, buffer, cb);
2731 png_process_data(pp, pi, buffer, cb);
2734 /* Check the invariants at the end (if this fails it's a problem in this
2737 if (pm->buffer_count > pm->buffer_position ||
2738 pm->this.next != &pm->this.current->data ||
2739 pm->this.readpos < pm->this.current->datacount)
2740 png_error(pp, "progressive read implementation error");
2743 /* Set up a modifier. */
2745 set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id,
2746 PNG_CONST char *name)
2748 /* Do this first so that the modifier fields are cleared even if an error
2749 * happens allocating the png_struct. No allocation is done here so no
2750 * cleanup is required.
2752 pm->state = modifier_start;
2754 pm->colour_type = 255;
2756 pm->pending_len = 0;
2757 pm->pending_chunk = 0;
2759 pm->buffer_count = 0;
2760 pm->buffer_position = 0;
2762 return set_store_for_read(&pm->this, ppi, id, name);
2766 /******************************** MODIFICATIONS *******************************/
2767 /* Standard modifications to add chunks. These do not require the _SUPPORTED
2768 * macros because the chunks can be there regardless of whether this specific
2769 * libpng supports them.
2771 typedef struct gama_modification
2773 png_modification this;
2774 png_fixed_point gamma;
2775 } gama_modification;
2778 gama_modify(png_modifier *pm, png_modification *me, int add)
2781 /* This simply dumps the given gamma value into the buffer. */
2782 png_save_uint_32(pm->buffer, 4);
2783 png_save_uint_32(pm->buffer+4, CHUNK_gAMA);
2784 png_save_uint_32(pm->buffer+8, ((gama_modification*)me)->gamma);
2789 gama_modification_init(gama_modification *me, png_modifier *pm, double gammad)
2793 modification_init(&me->this);
2794 me->this.chunk = CHUNK_gAMA;
2795 me->this.modify_fn = gama_modify;
2796 me->this.add = CHUNK_PLTE;
2798 me->gamma = (png_fixed_point)g;
2799 me->this.next = pm->modifications;
2800 pm->modifications = &me->this;
2803 typedef struct chrm_modification
2805 png_modification this;
2806 PNG_CONST color_encoding *encoding;
2807 png_fixed_point wx, wy, rx, ry, gx, gy, bx, by;
2808 } chrm_modification;
2811 chrm_modify(png_modifier *pm, png_modification *me, int add)
2814 /* As with gAMA this just adds the required cHRM chunk to the buffer. */
2815 png_save_uint_32(pm->buffer , 32);
2816 png_save_uint_32(pm->buffer+ 4, CHUNK_cHRM);
2817 png_save_uint_32(pm->buffer+ 8, ((chrm_modification*)me)->wx);
2818 png_save_uint_32(pm->buffer+12, ((chrm_modification*)me)->wy);
2819 png_save_uint_32(pm->buffer+16, ((chrm_modification*)me)->rx);
2820 png_save_uint_32(pm->buffer+20, ((chrm_modification*)me)->ry);
2821 png_save_uint_32(pm->buffer+24, ((chrm_modification*)me)->gx);
2822 png_save_uint_32(pm->buffer+28, ((chrm_modification*)me)->gy);
2823 png_save_uint_32(pm->buffer+32, ((chrm_modification*)me)->bx);
2824 png_save_uint_32(pm->buffer+36, ((chrm_modification*)me)->by);
2829 chrm_modification_init(chrm_modification *me, png_modifier *pm,
2830 PNG_CONST color_encoding *encoding)
2832 CIE_color white = white_point(encoding);
2834 /* Original end points: */
2835 me->encoding = encoding;
2837 /* Chromaticities (in fixed point): */
2838 me->wx = fix(chromaticity_x(white));
2839 me->wy = fix(chromaticity_y(white));
2841 me->rx = fix(chromaticity_x(encoding->red));
2842 me->ry = fix(chromaticity_y(encoding->red));
2843 me->gx = fix(chromaticity_x(encoding->green));
2844 me->gy = fix(chromaticity_y(encoding->green));
2845 me->bx = fix(chromaticity_x(encoding->blue));
2846 me->by = fix(chromaticity_y(encoding->blue));
2848 modification_init(&me->this);
2849 me->this.chunk = CHUNK_cHRM;
2850 me->this.modify_fn = chrm_modify;
2851 me->this.add = CHUNK_PLTE;
2852 me->this.next = pm->modifications;
2853 pm->modifications = &me->this;
2856 typedef struct srgb_modification
2858 png_modification this;
2860 } srgb_modification;
2863 srgb_modify(png_modifier *pm, png_modification *me, int add)
2866 /* As above, ignore add and just make a new chunk */
2867 png_save_uint_32(pm->buffer, 1);
2868 png_save_uint_32(pm->buffer+4, CHUNK_sRGB);
2869 pm->buffer[8] = ((srgb_modification*)me)->intent;
2874 srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent)
2876 modification_init(&me->this);
2877 me->this.chunk = CHUNK_sBIT;
2879 if (intent <= 3) /* if valid, else *delete* sRGB chunks */
2881 me->this.modify_fn = srgb_modify;
2882 me->this.add = CHUNK_PLTE;
2883 me->intent = intent;
2888 me->this.modify_fn = 0;
2893 me->this.next = pm->modifications;
2894 pm->modifications = &me->this;
2897 #ifdef PNG_READ_GAMMA_SUPPORTED
2898 typedef struct sbit_modification
2900 png_modification this;
2902 } sbit_modification;
2905 sbit_modify(png_modifier *pm, png_modification *me, int add)
2907 png_byte sbit = ((sbit_modification*)me)->sbit;
2908 if (pm->bit_depth > sbit)
2911 switch (pm->colour_type)
2931 png_error(pm->this.pread,
2932 "unexpected colour type in sBIT modification");
2935 png_save_uint_32(pm->buffer, cb);
2936 png_save_uint_32(pm->buffer+4, CHUNK_sBIT);
2939 (pm->buffer+8)[--cb] = sbit;
2945 /* Remove the sBIT chunk */
2946 pm->buffer_count = pm->buffer_position = 0;
2950 return 0; /* do nothing */
2954 sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit)
2956 modification_init(&me->this);
2957 me->this.chunk = CHUNK_sBIT;
2958 me->this.modify_fn = sbit_modify;
2959 me->this.add = CHUNK_PLTE;
2961 me->this.next = pm->modifications;
2962 pm->modifications = &me->this;
2964 #endif /* PNG_READ_GAMMA_SUPPORTED */
2965 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
2967 /***************************** STANDARD PNG FILES *****************************/
2968 /* Standard files - write and save standard files. */
2969 /* There are two basic forms of standard images. Those which attempt to have
2970 * all the possible pixel values (not possible for 16bpp images, but a range of
2971 * values are produced) and those which have a range of image sizes. The former
2972 * are used for testing transforms, in particular gamma correction and bit
2973 * reduction and increase. The latter are reserved for testing the behavior of
2974 * libpng with respect to 'odd' image sizes - particularly small images where
2975 * rows become 1 byte and interlace passes disappear.
2977 * The first, most useful, set are the 'transform' images, the second set of
2978 * small images are the 'size' images.
2980 * The transform files are constructed with rows which fit into a 1024 byte row
2981 * buffer. This makes allocation easier below. Further regardless of the file
2982 * format every row has 128 pixels (giving 1024 bytes for 64bpp formats).
2984 * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed
2985 * and with an ID derived from the colour type, bit depth and interlace type
2986 * as above (FILEID). The width (128) and height (variable) are not stored in
2987 * the FILEID - instead the fields are set to 0, indicating a transform file.
2989 * The size files ar constructed with rows a maximum of 128 bytes wide, allowing
2990 * a maximum width of 16 pixels (for the 64bpp case.) They also have a maximum
2991 * height of 16 rows. The width and height are stored in the FILEID and, being
2992 * non-zero, indicate a size file.
2994 * Because the PNG filter code is typically the largest CPU consumer within
2995 * libpng itself there is a tendency to attempt to optimize it. This results in
2996 * special case code which needs to be validated. To cause this to happen the
2997 * 'size' images are made to use each possible filter, in so far as this is
2998 * possible for smaller images.
3000 * For palette image (colour type 3) multiple transform images are stored with
3001 * the same bit depth to allow testing of more colour combinations -
3002 * particularly important for testing the gamma code because libpng uses a
3003 * different code path for palette images. For size images a single palette is
3007 /* Make a 'standard' palette. Because there are only 256 entries in a palette
3008 * (maximum) this actually makes a random palette in the hope that enough tests
3009 * will catch enough errors. (Note that the same palette isn't produced every
3010 * time for the same test - it depends on what previous tests have been run -
3011 * but a given set of arguments to pngvalid will always produce the same palette
3012 * at the same test! This is why pseudo-random number generators are useful for
3015 * The store must be open for write when this is called, otherwise an internal
3016 * error will occur. This routine contains its own magic number seed, so the
3017 * palettes generated don't change if there are intervening errors (changing the
3018 * calls to the store_mark seed.)
3020 static store_palette_entry *
3021 make_standard_palette(png_store* ps, int npalette, int do_tRNS)
3023 static png_uint_32 palette_seed[2] = { 0x87654321, 9 };
3026 png_byte values[256][4];
3028 /* Always put in black and white plus the six primary and secondary colors.
3032 values[i][1] = (png_byte)((i&1) ? 255U : 0U);
3033 values[i][2] = (png_byte)((i&2) ? 255U : 0U);
3034 values[i][3] = (png_byte)((i&4) ? 255U : 0U);
3037 /* Then add 62 grays (one quarter of the remaining 256 slots). */
3040 png_byte random_bytes[4];
3043 need[0] = 0; /*got black*/
3044 memset(need+1, 1, (sizeof need)-2); /*need these*/
3045 need[255] = 0; /*but not white*/
3053 make_four_random_bytes(palette_seed, random_bytes);
3057 b = random_bytes[--j];
3067 /* Finally add 192 colors at random - don't worry about matches to things we
3068 * already have, chance is less than 1/65536. Don't worry about grays,
3069 * chance is the same, so we get a duplicate or extra gray less than 1 time
3073 make_four_random_bytes(palette_seed, values[i]);
3075 /* Fill in the alpha values in the first byte. Just use all possible values
3076 * (0..255) in an apparently random order:
3079 store_palette_entry *palette;
3080 png_byte selector[4];
3082 make_four_random_bytes(palette_seed, selector);
3085 for (i=0; i<256; ++i)
3086 values[i][0] = (png_byte)(i ^ selector[0]);
3089 for (i=0; i<256; ++i)
3090 values[i][0] = 255; /* no transparency/tRNS chunk */
3092 /* 'values' contains 256 ARGB values, but we only need 'npalette'.
3093 * 'npalette' will always be a power of 2: 2, 4, 16 or 256. In the low
3094 * bit depth cases select colors at random, else it is difficult to have
3095 * a set of low bit depth palette test with any chance of a reasonable
3096 * range of colors. Do this by randomly permuting values into the low
3097 * 'npalette' entries using an XOR mask generated here. This also
3098 * permutes the npalette == 256 case in a potentially useful way (there is
3099 * no relationship between palette index and the color value therein!)
3101 palette = store_write_palette(ps, npalette);
3103 for (i=0; i<npalette; ++i)
3105 palette[i].alpha = values[i ^ selector[1]][0];
3106 palette[i].red = values[i ^ selector[1]][1];
3107 palette[i].green = values[i ^ selector[1]][2];
3108 palette[i].blue = values[i ^ selector[1]][3];
3115 /* Initialize a standard palette on a write stream. The 'do_tRNS' argument
3116 * indicates whether or not to also set the tRNS chunk.
3118 /* TODO: the png_structp here can probably be 'const' in the future */
3120 init_standard_palette(png_store *ps, png_structp pp, png_infop pi, int npalette,
3123 store_palette_entry *ppal = make_standard_palette(ps, npalette, do_tRNS);
3127 png_color palette[256];
3129 /* Set all entries to detect overread errors. */
3130 for (i=0; i<npalette; ++i)
3132 palette[i].red = ppal[i].red;
3133 palette[i].green = ppal[i].green;
3134 palette[i].blue = ppal[i].blue;
3137 /* Just in case fill in the rest with detectable values: */
3139 palette[i].red = palette[i].green = palette[i].blue = 42;
3141 png_set_PLTE(pp, pi, palette, npalette);
3149 /* Set all the entries, but skip trailing opaque entries */
3150 for (i=j=0; i<npalette; ++i)
3151 if ((tRNS[i] = ppal[i].alpha) < 255)
3154 /* Fill in the remainder with a detectable value: */
3158 # ifdef PNG_WRITE_tRNS_SUPPORTED
3160 png_set_tRNS(pp, pi, tRNS, j, 0/*color*/);
3165 /* The number of passes is related to the interlace type. There was no libpng
3166 * API to determine this prior to 1.5, so we need an inquiry function:
3169 npasses_from_interlace_type(png_const_structp pp, int interlace_type)
3171 switch (interlace_type)
3174 png_error(pp, "invalid interlace type");
3176 case PNG_INTERLACE_NONE:
3179 case PNG_INTERLACE_ADAM7:
3180 return PNG_INTERLACE_ADAM7_PASSES;
3185 bit_size(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
3187 switch (colour_type)
3189 default: png_error(pp, "invalid color type");
3191 case 0: return bit_depth;
3193 case 2: return 3*bit_depth;
3195 case 3: return bit_depth;
3197 case 4: return 2*bit_depth;
3199 case 6: return 4*bit_depth;
3203 #define TRANSFORM_WIDTH 128U
3204 #define TRANSFORM_ROWMAX (TRANSFORM_WIDTH*8U)
3205 #define SIZE_ROWMAX (16*8U) /* 16 pixels, max 8 bytes each - 128 bytes */
3206 #define STANDARD_ROWMAX TRANSFORM_ROWMAX /* The larger of the two */
3207 #define SIZE_HEIGHTMAX 16 /* Maximum range of size images */
3210 transform_rowsize(png_const_structp pp, png_byte colour_type,
3213 return (TRANSFORM_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8;
3216 /* transform_width(pp, colour_type, bit_depth) current returns the same number
3217 * every time, so just use a macro:
3219 #define transform_width(pp, colour_type, bit_depth) TRANSFORM_WIDTH
3222 transform_height(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
3224 switch (bit_size(pp, colour_type, bit_depth))
3229 return 1; /* Total of 128 pixels */
3232 return 2; /* Total of 256 pixels/bytes */
3235 return 512; /* Total of 65536 pixels */
3239 return 512; /* 65536 pixels */
3243 return 2048;/* 4 x 65536 pixels. */
3244 # define TRANSFORM_HEIGHTMAX 2048
3247 return 0; /* Error, will be caught later */
3251 #ifdef PNG_READ_SUPPORTED
3252 /* The following can only be defined here, now we have the definitions
3253 * of the transform image sizes.
3256 standard_width(png_const_structp pp, png_uint_32 id)
3258 png_uint_32 width = WIDTH_FROM_ID(id);
3262 width = transform_width(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3268 standard_height(png_const_structp pp, png_uint_32 id)
3270 png_uint_32 height = HEIGHT_FROM_ID(id);
3273 height = transform_height(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3279 standard_rowsize(png_const_structp pp, png_uint_32 id)
3281 png_uint_32 width = standard_width(pp, id);
3283 /* This won't overflow: */
3284 width *= bit_size(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
3285 return (width + 7) / 8;
3287 #endif /* PNG_READ_SUPPORTED */
3290 transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
3291 png_byte colour_type, png_byte bit_depth, png_uint_32 y)
3293 png_uint_32 v = y << 7;
3296 switch (bit_size(pp, colour_type, bit_depth))
3299 while (i<128/8) buffer[i] = (png_byte)(v & 0xff), v += 17, ++i;
3303 while (i<128/4) buffer[i] = (png_byte)(v & 0xff), v += 33, ++i;
3307 while (i<128/2) buffer[i] = (png_byte)(v & 0xff), v += 65, ++i;
3311 /* 256 bytes total, 128 bytes in each row set as follows: */
3312 while (i<128) buffer[i] = (png_byte)(v & 0xff), ++v, ++i;
3316 /* Generate all 65536 pixel values in order, which includes the 8 bit
3317 * GA case as well as the 16 bit G case.
3321 buffer[2*i] = (png_byte)((v>>8) & 0xff);
3322 buffer[2*i+1] = (png_byte)(v & 0xff);
3330 /* 65535 pixels, but rotate the values. */
3333 /* Three bytes per pixel, r, g, b, make b by r^g */
3334 buffer[3*i+0] = (png_byte)((v >> 8) & 0xff);
3335 buffer[3*i+1] = (png_byte)(v & 0xff);
3336 buffer[3*i+2] = (png_byte)(((v >> 8) ^ v) & 0xff);
3344 /* 65535 pixels, r, g, b, a; just replicate */
3347 buffer[4*i+0] = (png_byte)((v >> 8) & 0xff);
3348 buffer[4*i+1] = (png_byte)(v & 0xff);
3349 buffer[4*i+2] = (png_byte)((v >> 8) & 0xff);
3350 buffer[4*i+3] = (png_byte)(v & 0xff);
3358 /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at
3359 * each pixel, g increase by 257 (0x101) and 'b' by 0x1111:
3363 png_uint_32 t = v++;
3364 buffer[6*i+0] = (png_byte)((t >> 8) & 0xff);
3365 buffer[6*i+1] = (png_byte)(t & 0xff);
3367 buffer[6*i+2] = (png_byte)((t >> 8) & 0xff);
3368 buffer[6*i+3] = (png_byte)(t & 0xff);
3370 buffer[6*i+4] = (png_byte)((t >> 8) & 0xff);
3371 buffer[6*i+5] = (png_byte)(t & 0xff);
3378 /* As above in the 32 bit case. */
3381 png_uint_32 t = v++;
3382 buffer[8*i+0] = (png_byte)((t >> 8) & 0xff);
3383 buffer[8*i+1] = (png_byte)(t & 0xff);
3384 buffer[8*i+4] = (png_byte)((t >> 8) & 0xff);
3385 buffer[8*i+5] = (png_byte)(t & 0xff);
3387 buffer[8*i+2] = (png_byte)((t >> 8) & 0xff);
3388 buffer[8*i+3] = (png_byte)(t & 0xff);
3389 buffer[8*i+6] = (png_byte)((t >> 8) & 0xff);
3390 buffer[8*i+7] = (png_byte)(t & 0xff);
3399 png_error(pp, "internal error");
3402 /* This is just to do the right cast - could be changed to a function to check
3403 * 'bd' but there isn't much point.
3405 #define DEPTH(bd) ((png_byte)(1U << (bd)))
3407 /* This is just a helper for compiling on minimal systems with no write
3408 * interlacing support. If there is no write interlacing we can't generate test
3409 * cases with interlace:
3411 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
3412 # define INTERLACE_LAST PNG_INTERLACE_LAST
3413 # define check_interlace_type(type) ((void)(type))
3415 # define INTERLACE_LAST (PNG_INTERLACE_NONE+1)
3416 # define png_set_interlace_handling(a) (1)
3419 check_interlace_type(int PNG_CONST interlace_type)
3421 if (interlace_type != PNG_INTERLACE_NONE)
3423 /* This is an internal error - --interlace tests should be skipped, not
3426 fprintf(stderr, "pngvalid: no interlace support\n");
3432 /* Make a standardized image given a an image colour type, bit depth and
3433 * interlace type. The standard images have a very restricted range of
3434 * rows and heights and are used for testing transforms rather than image
3435 * layout details. See make_size_images below for a way to make images
3436 * that test odd sizes along with the libpng interlace handling.
3439 make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
3440 png_byte PNG_CONST bit_depth, unsigned int palette_number,
3441 int interlace_type, png_const_charp name)
3445 check_interlace_type(interlace_type);
3450 png_structp pp = set_store_for_write(ps, &pi, name);
3453 /* In the event of a problem return control to the Catch statement below
3454 * to do the clean up - it is not possible to 'return' directly from a Try
3460 h = transform_height(pp, colour_type, bit_depth);
3462 png_set_IHDR(pp, pi, transform_width(pp, colour_type, bit_depth), h,
3463 bit_depth, colour_type, interlace_type,
3464 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3466 #ifdef PNG_TEXT_SUPPORTED
3467 # if defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)
3468 # define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_zTXt
3470 # define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_NONE
3473 static char key[] = "image name"; /* must be writeable */
3476 char copy[FILE_NAME_SIZE];
3478 /* Use a compressed text string to test the correct interaction of text
3479 * compression and IDAT compression.
3481 text.compression = TEXT_COMPRESSION;
3483 /* Yuck: the text must be writable! */
3484 pos = safecat(copy, sizeof copy, 0, ps->wname);
3486 text.text_length = pos;
3487 text.itxt_length = 0;
3491 png_set_text(pp, pi, &text, 1);
3495 if (colour_type == 3) /* palette */
3496 init_standard_palette(ps, pp, pi, 1U << bit_depth, 1/*do tRNS*/);
3498 png_write_info(pp, pi);
3500 if (png_get_rowbytes(pp, pi) !=
3501 transform_rowsize(pp, colour_type, bit_depth))
3502 png_error(pp, "row size incorrect");
3506 /* Somewhat confusingly this must be called *after* png_write_info
3507 * because if it is called before, the information in *pp has not been
3508 * updated to reflect the interlaced image.
3510 int npasses = png_set_interlace_handling(pp);
3513 if (npasses != npasses_from_interlace_type(pp, interlace_type))
3514 png_error(pp, "write: png_set_interlace_handling failed");
3516 for (pass=0; pass<npasses; ++pass)
3522 png_byte buffer[TRANSFORM_ROWMAX];
3524 transform_row(pp, buffer, colour_type, bit_depth, y);
3525 png_write_row(pp, buffer);
3530 #ifdef PNG_TEXT_SUPPORTED
3532 static char key[] = "end marker";
3533 static char comment[] = "end";
3536 /* Use a compressed text string to test the correct interaction of text
3537 * compression and IDAT compression.
3539 text.compression = TEXT_COMPRESSION;
3541 text.text = comment;
3542 text.text_length = (sizeof comment)-1;
3543 text.itxt_length = 0;
3547 png_set_text(pp, pi, &text, 1);
3551 png_write_end(pp, pi);
3553 /* And store this under the appropriate id, then clean up. */
3554 store_storefile(ps, FILEID(colour_type, bit_depth, palette_number,
3555 interlace_type, 0, 0, 0));
3557 store_write_reset(ps);
3562 /* Use the png_store returned by the exception. This may help the compiler
3563 * because 'ps' is not used in this branch of the setjmp. Note that fault
3564 * and ps will always be the same value.
3566 store_write_reset(fault);
3571 make_transform_images(png_store *ps)
3573 png_byte colour_type = 0;
3574 png_byte bit_depth = 0;
3575 unsigned int palette_number = 0;
3577 /* This is in case of errors. */
3578 safecat(ps->test, sizeof ps->test, 0, "make standard images");
3580 /* Use next_format to enumerate all the combinations we test, including
3581 * generating multiple low bit depth palette images.
3583 while (next_format(&colour_type, &bit_depth, &palette_number, 0))
3587 for (interlace_type = PNG_INTERLACE_NONE;
3588 interlace_type < INTERLACE_LAST; ++interlace_type)
3590 char name[FILE_NAME_SIZE];
3592 standard_name(name, sizeof name, 0, colour_type, bit_depth,
3593 palette_number, interlace_type, 0, 0, 0);
3594 make_transform_image(ps, colour_type, bit_depth, palette_number,
3595 interlace_type, name);
3600 /* The following two routines use the PNG interlace support macros from
3601 * png.h to interlace or deinterlace rows.
3604 interlace_row(png_bytep buffer, png_const_bytep imageRow,
3605 unsigned int pixel_size, png_uint_32 w, int pass)
3607 png_uint_32 xin, xout, xstep;
3609 /* Note that this can, trivially, be optimized to a memcpy on pass 7, the
3610 * code is presented this way to make it easier to understand. In practice
3611 * consult the code in the libpng source to see other ways of doing this.
3613 xin = PNG_PASS_START_COL(pass);
3614 xstep = 1U<<PNG_PASS_COL_SHIFT(pass);
3616 for (xout=0; xin<w; xin+=xstep)
3618 pixel_copy(buffer, xout, imageRow, xin, pixel_size);
3623 #ifdef PNG_READ_SUPPORTED
3625 deinterlace_row(png_bytep buffer, png_const_bytep row,
3626 unsigned int pixel_size, png_uint_32 w, int pass)
3628 /* The inverse of the above, 'row' is part of row 'y' of the output image,
3629 * in 'buffer'. The image is 'w' wide and this is pass 'pass', distribute
3630 * the pixels of row into buffer and return the number written (to allow
3631 * this to be checked).
3633 png_uint_32 xin, xout, xstep;
3635 xout = PNG_PASS_START_COL(pass);
3636 xstep = 1U<<PNG_PASS_COL_SHIFT(pass);
3638 for (xin=0; xout<w; xout+=xstep)
3640 pixel_copy(buffer, xout, row, xin, pixel_size);
3644 #endif /* PNG_READ_SUPPORTED */
3646 /* Build a single row for the 'size' test images; this fills in only the
3647 * first bit_width bits of the sample row.
3650 size_row(png_byte buffer[SIZE_ROWMAX], png_uint_32 bit_width, png_uint_32 y)
3652 /* height is in the range 1 to 16, so: */
3653 y = ((y & 1) << 7) + ((y & 2) << 6) + ((y & 4) << 5) + ((y & 8) << 4);
3654 /* the following ensures bits are set in small images: */
3657 while (bit_width >= 8)
3658 *buffer++ = (png_byte)y++, bit_width -= 8;
3660 /* There may be up to 7 remaining bits, these go in the most significant
3665 png_uint_32 mask = (1U<<(8-bit_width))-1;
3666 *buffer = (png_byte)((*buffer & mask) | (y & ~mask));
3671 make_size_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
3672 png_byte PNG_CONST bit_depth, int PNG_CONST interlace_type,
3673 png_uint_32 PNG_CONST w, png_uint_32 PNG_CONST h,
3674 int PNG_CONST do_interlace)
3678 /* At present libpng does not support the write of an interlaced image unless
3679 * PNG_WRITE_INTERLACING_SUPPORTED, even with do_interlace so the code here
3680 * does the pixel interlace itself, so:
3682 check_interlace_type(interlace_type);
3688 unsigned int pixel_size;
3690 /* Make a name and get an appropriate id for the store: */
3691 char name[FILE_NAME_SIZE];
3692 PNG_CONST png_uint_32 id = FILEID(colour_type, bit_depth, 0/*palette*/,
3693 interlace_type, w, h, do_interlace);
3695 standard_name_from_id(name, sizeof name, 0, id);
3696 pp = set_store_for_write(ps, &pi, name);
3698 /* In the event of a problem return control to the Catch statement below
3699 * to do the clean up - it is not possible to 'return' directly from a Try
3705 png_set_IHDR(pp, pi, w, h, bit_depth, colour_type, interlace_type,
3706 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
3708 #ifdef PNG_TEXT_SUPPORTED
3710 static char key[] = "image name"; /* must be writeable */
3713 char copy[FILE_NAME_SIZE];
3715 /* Use a compressed text string to test the correct interaction of text
3716 * compression and IDAT compression.
3718 text.compression = TEXT_COMPRESSION;
3720 /* Yuck: the text must be writable! */
3721 pos = safecat(copy, sizeof copy, 0, ps->wname);
3723 text.text_length = pos;
3724 text.itxt_length = 0;
3728 png_set_text(pp, pi, &text, 1);
3732 if (colour_type == 3) /* palette */
3733 init_standard_palette(ps, pp, pi, 1U << bit_depth, 0/*do tRNS*/);
3735 png_write_info(pp, pi);
3737 /* Calculate the bit size, divide by 8 to get the byte size - this won't
3738 * overflow because we know the w values are all small enough even for
3739 * a system where 'unsigned int' is only 16 bits.
3741 pixel_size = bit_size(pp, colour_type, bit_depth);
3742 if (png_get_rowbytes(pp, pi) != ((w * pixel_size) + 7) / 8)
3743 png_error(pp, "row size incorrect");
3747 int npasses = npasses_from_interlace_type(pp, interlace_type);
3750 # ifdef PNG_WRITE_FILTER_SUPPORTED
3751 int nfilter = PNG_FILTER_VALUE_LAST;
3753 png_byte image[16][SIZE_ROWMAX];
3755 /* To help consistent error detection make the parts of this buffer
3756 * that aren't set below all '1':
3758 memset(image, 0xff, sizeof image);
3760 if (!do_interlace && npasses != png_set_interlace_handling(pp))
3761 png_error(pp, "write: png_set_interlace_handling failed");
3763 /* Prepare the whole image first to avoid making it 7 times: */
3765 size_row(image[y], w * pixel_size, y);
3767 for (pass=0; pass<npasses; ++pass)
3769 /* The following two are for checking the macros: */
3770 PNG_CONST png_uint_32 wPass = PNG_PASS_COLS(w, pass);
3772 /* If do_interlace is set we don't call png_write_row for every
3773 * row because some of them are empty. In fact, for a 1x1 image,
3774 * most of them are empty!
3778 png_const_bytep row = image[y];
3779 png_byte tempRow[SIZE_ROWMAX];
3781 /* If do_interlace *and* the image is interlaced we
3782 * need a reduced interlace row; this may be reduced
3785 if (do_interlace && interlace_type == PNG_INTERLACE_ADAM7)
3787 /* The row must not be written if it doesn't exist, notice
3788 * that there are two conditions here, either the row isn't
3789 * ever in the pass or the row would be but isn't wide
3790 * enough to contribute any pixels. In fact the wPass test
3791 * can be used to skip the whole y loop in this case.
3793 if (PNG_ROW_IN_INTERLACE_PASS(y, pass) && wPass > 0)
3795 /* Set to all 1's for error detection (libpng tends to
3796 * set unset things to 0).
3798 memset(tempRow, 0xff, sizeof tempRow);
3799 interlace_row(tempRow, row, pixel_size, w, pass);
3806 # ifdef PNG_WRITE_FILTER_SUPPORTED
3807 /* Only get to here if the row has some pixels in it, set the
3808 * filters to 'all' for the very first row and thereafter to a
3809 * single filter. It isn't well documented, but png_set_filter
3810 * does accept a filter number (per the spec) as well as a bit
3813 * The apparent wackiness of decrementing nfilter rather than
3814 * incrementing is so that Paeth gets used in all images bigger
3815 * than 1 row - it's the tricky one.
3817 png_set_filter(pp, 0/*method*/,
3818 nfilter >= PNG_FILTER_VALUE_LAST ? PNG_ALL_FILTERS : nfilter);
3821 nfilter = PNG_FILTER_VALUE_LAST-1;
3824 png_write_row(pp, row);
3829 #ifdef PNG_TEXT_SUPPORTED
3831 static char key[] = "end marker";
3832 static char comment[] = "end";
3835 /* Use a compressed text string to test the correct interaction of text
3836 * compression and IDAT compression.
3838 text.compression = TEXT_COMPRESSION;
3840 text.text = comment;
3841 text.text_length = (sizeof comment)-1;
3842 text.itxt_length = 0;
3846 png_set_text(pp, pi, &text, 1);
3850 png_write_end(pp, pi);
3852 /* And store this under the appropriate id, then clean up. */
3853 store_storefile(ps, id);
3855 store_write_reset(ps);
3860 /* Use the png_store returned by the exception. This may help the compiler
3861 * because 'ps' is not used in this branch of the setjmp. Note that fault
3862 * and ps will always be the same value.
3864 store_write_reset(fault);
3869 make_size(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, int bdlo,
3872 for (; bdlo <= bdhi; ++bdlo)
3876 for (width = 1; width <= 16; ++width)
3880 for (height = 1; height <= 16; ++height)
3882 /* The four combinations of DIY interlace and interlace or not -
3883 * no interlace + DIY should be identical to no interlace with
3886 make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_NONE,
3888 make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_NONE,
3890 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
3891 make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
3893 make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
3902 make_size_images(png_store *ps)
3904 /* This is in case of errors. */
3905 safecat(ps->test, sizeof ps->test, 0, "make size images");
3907 /* Arguments are colour_type, low bit depth, high bit depth
3909 make_size(ps, 0, 0, WRITE_BDHI);
3910 make_size(ps, 2, 3, WRITE_BDHI);
3911 make_size(ps, 3, 0, 3 /*palette: max 8 bits*/);
3912 make_size(ps, 4, 3, WRITE_BDHI);
3913 make_size(ps, 6, 3, WRITE_BDHI);
3916 #ifdef PNG_READ_SUPPORTED
3917 /* Return a row based on image id and 'y' for checking: */
3919 standard_row(png_const_structp pp, png_byte std[STANDARD_ROWMAX],
3920 png_uint_32 id, png_uint_32 y)
3922 if (WIDTH_FROM_ID(id) == 0)
3923 transform_row(pp, std, COL_FROM_ID(id), DEPTH_FROM_ID(id), y);
3925 size_row(std, WIDTH_FROM_ID(id) * bit_size(pp, COL_FROM_ID(id),
3926 DEPTH_FROM_ID(id)), y);
3928 #endif /* PNG_READ_SUPPORTED */
3930 /* Tests - individual test cases */
3931 /* Like 'make_standard' but errors are deliberately introduced into the calls
3932 * to ensure that they get detected - it should not be possible to write an
3933 * invalid image with libpng!
3935 /* TODO: the 'set' functions can probably all be made to take a
3936 * png_const_structp rather than a modifiable one.
3938 #ifdef PNG_WARNINGS_SUPPORTED
3940 sBIT0_error_fn(png_structp pp, png_infop pi)
3942 /* 0 is invalid... */
3944 bad.red = bad.green = bad.blue = bad.gray = bad.alpha = 0;
3945 png_set_sBIT(pp, pi, &bad);
3949 sBIT_error_fn(png_structp pp, png_infop pi)
3954 if (png_get_color_type(pp, pi) == PNG_COLOR_TYPE_PALETTE)
3958 bit_depth = png_get_bit_depth(pp, pi);
3960 /* Now we know the bit depth we can easily generate an invalid sBIT entry */
3961 bad.red = bad.green = bad.blue = bad.gray = bad.alpha =
3962 (png_byte)(bit_depth+1);
3963 png_set_sBIT(pp, pi, &bad);
3966 static PNG_CONST struct
3968 void (*fn)(png_structp, png_infop);
3969 PNG_CONST char *msg;
3970 unsigned int warning :1; /* the error is a warning... */
3973 /* no warnings makes these errors undetectable. */
3974 { sBIT0_error_fn, "sBIT(0): failed to detect error", 1 },
3975 { sBIT_error_fn, "sBIT(too big): failed to detect error", 1 },
3979 make_error(png_store* volatile psIn, png_byte PNG_CONST colour_type,
3980 png_byte bit_depth, int interlace_type, int test, png_const_charp name)
3982 png_store * volatile ps = psIn;
3986 check_interlace_type(interlace_type);
3993 pp = set_store_for_write(ps, &pi, name);
3998 png_set_IHDR(pp, pi, transform_width(pp, colour_type, bit_depth),
3999 transform_height(pp, colour_type, bit_depth), bit_depth, colour_type,
4000 interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
4002 if (colour_type == 3) /* palette */
4003 init_standard_palette(ps, pp, pi, 1U << bit_depth, 0/*do tRNS*/);
4005 /* Time for a few errors; these are in various optional chunks, the
4006 * standard tests test the standard chunks pretty well.
4008 # define exception__prev exception_prev_1
4009 # define exception__env exception_env_1
4012 /* Expect this to throw: */
4013 ps->expect_error = !error_test[test].warning;
4014 ps->expect_warning = error_test[test].warning;
4015 ps->saw_warning = 0;
4016 error_test[test].fn(pp, pi);
4018 /* Normally the error is only detected here: */
4019 png_write_info(pp, pi);
4021 /* And handle the case where it was only a warning: */
4022 if (ps->expect_warning && ps->saw_warning)
4025 /* If we get here there is a problem, we have success - no error or
4026 * no warning - when we shouldn't have success. Log an error.
4028 store_log(ps, pp, error_test[test].msg, 1 /*error*/);
4032 ps = fault; /* expected exit, make sure ps is not clobbered */
4033 #undef exception__prev
4034 #undef exception__env
4036 /* And clear these flags */
4037 ps->expect_error = 0;
4038 ps->expect_warning = 0;
4040 /* Now write the whole image, just to make sure that the detected, or
4041 * undetected, errro has not created problems inside libpng.
4043 if (png_get_rowbytes(pp, pi) !=
4044 transform_rowsize(pp, colour_type, bit_depth))
4045 png_error(pp, "row size incorrect");
4049 png_uint_32 h = transform_height(pp, colour_type, bit_depth);
4050 int npasses = png_set_interlace_handling(pp);
4053 if (npasses != npasses_from_interlace_type(pp, interlace_type))
4054 png_error(pp, "write: png_set_interlace_handling failed");
4056 for (pass=0; pass<npasses; ++pass)
4062 png_byte buffer[TRANSFORM_ROWMAX];
4064 transform_row(pp, buffer, colour_type, bit_depth, y);
4065 png_write_row(pp, buffer);
4070 png_write_end(pp, pi);
4072 /* The following deletes the file that was just written. */
4073 store_write_reset(ps);
4078 store_write_reset(fault);
4083 make_errors(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
4084 int bdlo, int PNG_CONST bdhi)
4086 for (; bdlo <= bdhi; ++bdlo)
4090 for (interlace_type = PNG_INTERLACE_NONE;
4091 interlace_type < INTERLACE_LAST; ++interlace_type)
4094 char name[FILE_NAME_SIZE];
4096 standard_name(name, sizeof name, 0, colour_type, 1<<bdlo, 0,
4097 interlace_type, 0, 0, 0);
4099 for (test=0; test<(sizeof error_test)/(sizeof error_test[0]); ++test)
4101 make_error(&pm->this, colour_type, DEPTH(bdlo), interlace_type,
4110 return 1; /* keep going */
4112 #endif /* PNG_WARNINGS_SUPPORTED */
4115 perform_error_test(png_modifier *pm)
4117 #ifdef PNG_WARNINGS_SUPPORTED /* else there are no cases that work! */
4118 /* Need to do this here because we just write in this test. */
4119 safecat(pm->this.test, sizeof pm->this.test, 0, "error test");
4121 if (!make_errors(pm, 0, 0, WRITE_BDHI))
4124 if (!make_errors(pm, 2, 3, WRITE_BDHI))
4127 if (!make_errors(pm, 3, 0, 3))
4130 if (!make_errors(pm, 4, 3, WRITE_BDHI))
4133 if (!make_errors(pm, 6, 3, WRITE_BDHI))
4140 /* This is just to validate the internal PNG formatting code - if this fails
4141 * then the warning messages the library outputs will probably be garbage.
4144 perform_formatting_test(png_store *volatile ps)
4146 #ifdef PNG_TIME_RFC1123_SUPPORTED
4147 /* The handle into the formatting code is the RFC1123 support; this test does
4148 * nothing if that is compiled out.
4154 png_const_charp correct = "29 Aug 2079 13:53:60 +0000";
4155 png_const_charp result;
4156 # if PNG_LIBPNG_VER >= 10600
4157 char timestring[29];
4162 pp = set_store_for_write(ps, NULL, "libpng formatting test");
4168 /* Arbitrary settings: */
4174 pt.second = 60; /* a leap second */
4176 # if PNG_LIBPNG_VER < 10600
4177 result = png_convert_to_rfc1123(pp, &pt);
4179 if (png_convert_to_rfc1123_buffer(timestring, &pt))
4180 result = timestring;
4187 png_error(pp, "png_convert_to_rfc1123 failed");
4189 if (strcmp(result, correct) != 0)
4194 pos = safecat(msg, sizeof msg, pos, "png_convert_to_rfc1123(");
4195 pos = safecat(msg, sizeof msg, pos, correct);
4196 pos = safecat(msg, sizeof msg, pos, ") returned: '");
4197 pos = safecat(msg, sizeof msg, pos, result);
4198 pos = safecat(msg, sizeof msg, pos, "'");
4203 store_write_reset(ps);
4208 store_write_reset(fault);
4215 #ifdef PNG_READ_SUPPORTED
4216 /* Because we want to use the same code in both the progressive reader and the
4217 * sequential reader it is necessary to deal with the fact that the progressive
4218 * reader callbacks only have one parameter (png_get_progressive_ptr()), so this
4219 * must contain all the test parameters and all the local variables directly
4220 * accessible to the sequential reader implementation.
4222 * The technique adopted is to reinvent part of what Dijkstra termed a
4223 * 'display'; an array of pointers to the stack frames of enclosing functions so
4224 * that a nested function definition can access the local (C auto) variables of
4225 * the functions that contain its definition. In fact C provides the first
4226 * pointer (the local variables - the stack frame pointer) and the last (the
4227 * global variables - the BCPL global vector typically implemented as global
4228 * addresses), this code requires one more pointer to make the display - the
4229 * local variables (and function call parameters) of the function that actually
4230 * invokes either the progressive or sequential reader.
4232 * Perhaps confusingly this technique is confounded with classes - the
4233 * 'standard_display' defined here is sub-classed as the 'gamma_display' below.
4234 * A gamma_display is a standard_display, taking advantage of the ANSI-C
4235 * requirement that the pointer to the first member of a structure must be the
4236 * same as the pointer to the structure. This allows us to reuse standard_
4237 * functions in the gamma test code; something that could not be done with
4240 typedef struct standard_display
4242 png_store* ps; /* Test parameters (passed to the function) */
4243 png_byte colour_type;
4245 png_byte red_sBIT; /* Input data sBIT values. */
4246 png_byte green_sBIT;
4248 png_byte alpha_sBIT;
4250 png_uint_32 id; /* Calculated file ID */
4251 png_uint_32 w; /* Width of image */
4252 png_uint_32 h; /* Height of image */
4253 int npasses; /* Number of interlaced passes */
4254 png_uint_32 pixel_size; /* Width of one pixel in bits */
4255 png_uint_32 bit_width; /* Width of output row in bits */
4256 size_t cbRow; /* Bytes in a row of the output image */
4257 int do_interlace; /* Do interlacing internally */
4258 int is_transparent; /* Transparency information was present. */
4259 int speed; /* Doing a speed test */
4260 int use_update_info;/* Call update_info, not start_image */
4266 } transparent; /* The transparent color, if set. */
4267 int npalette; /* Number of entries in the palette. */
4273 standard_display_init(standard_display *dp, png_store* ps, png_uint_32 id,
4274 int do_interlace, int use_update_info)
4276 memset(dp, 0, sizeof *dp);
4279 dp->colour_type = COL_FROM_ID(id);
4280 dp->bit_depth = DEPTH_FROM_ID(id);
4281 if (dp->bit_depth < 1 || dp->bit_depth > 16)
4282 internal_error(ps, "internal: bad bit depth");
4283 if (dp->colour_type == 3)
4284 dp->red_sBIT = dp->blue_sBIT = dp->green_sBIT = dp->alpha_sBIT = 8;
4286 dp->red_sBIT = dp->blue_sBIT = dp->green_sBIT = dp->alpha_sBIT =
4288 dp->interlace_type = INTERLACE_FROM_ID(id);
4289 check_interlace_type(dp->interlace_type);
4291 /* All the rest are filled in after the read_info: */
4298 dp->do_interlace = do_interlace;
4299 dp->is_transparent = 0;
4300 dp->speed = ps->speed;
4301 dp->use_update_info = use_update_info;
4303 /* Preset the transparent color to black: */
4304 memset(&dp->transparent, 0, sizeof dp->transparent);
4305 /* Preset the palette to full intensity/opaque througout: */
4306 memset(dp->palette, 0xff, sizeof dp->palette);
4309 /* Initialize the palette fields - this must be done later because the palette
4310 * comes from the particular png_store_file that is selected.
4313 standard_palette_init(standard_display *dp)
4315 store_palette_entry *palette = store_current_palette(dp->ps, &dp->npalette);
4317 /* The remaining entries remain white/opaque. */
4318 if (dp->npalette > 0)
4320 int i = dp->npalette;
4321 memcpy(dp->palette, palette, i * sizeof *palette);
4323 /* Check for a non-opaque palette entry: */
4325 if (palette[i].alpha < 255)
4329 /* GCC can't handle the more obviously optimizable version. */
4331 dp->is_transparent = 1;
4333 dp->is_transparent = 0;
4335 dp->is_transparent = (i >= 0);
4340 /* Utility to read the palette from the PNG file and convert it into
4341 * store_palette format. This returns 1 if there is any transparency in the
4342 * palette (it does not check for a transparent colour in the non-palette case.)
4345 read_palette(store_palette palette, int *npalette, png_const_structp pp,
4349 png_bytep trans_alpha;
4355 if (png_get_PLTE(pp, pi, &pal, npalette) & PNG_INFO_PLTE)
4359 if (i <= 0 || i > 256)
4360 png_error(pp, "validate: invalid PLTE count");
4364 palette[i].red = pal[i].red;
4365 palette[i].green = pal[i].green;
4366 palette[i].blue = pal[i].blue;
4369 /* Mark the remainder of the entries with a flag value (other than
4370 * white/opaque which is the flag value stored above.)
4372 memset(palette + *npalette, 126, (256-*npalette) * sizeof *palette);
4375 else /* !png_get_PLTE */
4377 if (*npalette != (-1))
4378 png_error(pp, "validate: invalid PLTE result");
4379 /* But there is no palette, so record this: */
4381 memset(palette, 113, sizeof (store_palette));
4385 num = 2; /* force error below */
4386 if ((png_get_tRNS(pp, pi, &trans_alpha, &num, 0) & PNG_INFO_tRNS) != 0 &&
4387 (trans_alpha != NULL || num != 1/*returns 1 for a transparent color*/) &&
4388 /* Oops, if a palette tRNS gets expanded png_read_update_info (at least so
4389 * far as 1.5.4) does not remove the trans_alpha pointer, only num_trans,
4390 * so in the above call we get a success, we get a pointer (who knows what
4391 * to) and we get num_trans == 0:
4393 !(trans_alpha != NULL && num == 0)) /* TODO: fix this in libpng. */
4397 /* Any of these are crash-worthy - given the implementation of
4398 * png_get_tRNS up to 1.5 an app won't crash if it just checks the
4399 * result above and fails to check that the variables it passed have
4400 * actually been filled in! Note that if the app were to pass the
4401 * last, png_color_16p, variable too it couldn't rely on this.
4403 if (trans_alpha == NULL || num <= 0 || num > 256 || num > *npalette)
4404 png_error(pp, "validate: unexpected png_get_tRNS (palette) result");
4406 for (i=0; i<num; ++i)
4407 palette[i].alpha = trans_alpha[i];
4409 for (num=*npalette; i<num; ++i)
4410 palette[i].alpha = 255;
4413 palette[i].alpha = 33; /* flag value */
4415 return 1; /* transparency */
4420 /* No palette transparency - just set the alpha channel to opaque. */
4423 for (i=0, num=*npalette; i<num; ++i)
4424 palette[i].alpha = 255;
4427 palette[i].alpha = 55; /* flag value */
4429 return 0; /* no transparency */
4433 /* Utility to validate the palette if it should not have changed (the
4434 * non-transform case).
4437 standard_palette_validate(standard_display *dp, png_const_structp pp,
4441 store_palette palette;
4443 if (read_palette(palette, &npalette, pp, pi) != dp->is_transparent)
4444 png_error(pp, "validate: palette transparency changed");
4446 if (npalette != dp->npalette)
4451 pos = safecat(msg, sizeof msg, pos, "validate: palette size changed: ");
4452 pos = safecatn(msg, sizeof msg, pos, dp->npalette);
4453 pos = safecat(msg, sizeof msg, pos, " -> ");
4454 pos = safecatn(msg, sizeof msg, pos, npalette);
4459 int i = npalette; /* npalette is aliased */
4462 if (palette[i].red != dp->palette[i].red ||
4463 palette[i].green != dp->palette[i].green ||
4464 palette[i].blue != dp->palette[i].blue ||
4465 palette[i].alpha != dp->palette[i].alpha)
4466 png_error(pp, "validate: PLTE or tRNS chunk changed");
4470 /* By passing a 'standard_display' the progressive callbacks can be used
4471 * directly by the sequential code, the functions suffixed "_imp" are the
4472 * implementations, the functions without the suffix are the callbacks.
4474 * The code for the info callback is split into two because this callback calls
4475 * png_read_update_info or png_start_read_image and what gets called depends on
4476 * whether the info needs updating (we want to test both calls in pngvalid.)
4479 standard_info_part1(standard_display *dp, png_structp pp, png_infop pi)
4481 if (png_get_bit_depth(pp, pi) != dp->bit_depth)
4482 png_error(pp, "validate: bit depth changed");
4484 if (png_get_color_type(pp, pi) != dp->colour_type)
4485 png_error(pp, "validate: color type changed");
4487 if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE)
4488 png_error(pp, "validate: filter type changed");
4490 if (png_get_interlace_type(pp, pi) != dp->interlace_type)
4491 png_error(pp, "validate: interlacing changed");
4493 if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE)
4494 png_error(pp, "validate: compression type changed");
4496 dp->w = png_get_image_width(pp, pi);
4498 if (dp->w != standard_width(pp, dp->id))
4499 png_error(pp, "validate: image width changed");
4501 dp->h = png_get_image_height(pp, pi);
4503 if (dp->h != standard_height(pp, dp->id))
4504 png_error(pp, "validate: image height changed");
4506 /* Record (but don't check at present) the input sBIT according to the colour
4510 png_color_8p sBIT = 0;
4512 if (png_get_sBIT(pp, pi, &sBIT) & PNG_INFO_sBIT)
4514 int sBIT_invalid = 0;
4517 png_error(pp, "validate: unexpected png_get_sBIT result");
4519 if (dp->colour_type & PNG_COLOR_MASK_COLOR)
4521 if (sBIT->red == 0 || sBIT->red > dp->bit_depth)
4524 dp->red_sBIT = sBIT->red;
4526 if (sBIT->green == 0 || sBIT->green > dp->bit_depth)
4529 dp->green_sBIT = sBIT->green;
4531 if (sBIT->blue == 0 || sBIT->blue > dp->bit_depth)
4534 dp->blue_sBIT = sBIT->blue;
4539 if (sBIT->gray == 0 || sBIT->gray > dp->bit_depth)
4542 dp->blue_sBIT = dp->green_sBIT = dp->red_sBIT = sBIT->gray;
4545 /* All 8 bits in tRNS for a palette image are significant - see the
4548 if (dp->colour_type & PNG_COLOR_MASK_ALPHA)
4550 if (sBIT->alpha == 0 || sBIT->alpha > dp->bit_depth)
4553 dp->alpha_sBIT = sBIT->alpha;
4557 png_error(pp, "validate: sBIT value out of range");
4561 /* Important: this is validating the value *before* any transforms have been
4562 * put in place. It doesn't matter for the standard tests, where there are
4563 * no transforms, but it does for other tests where rowbytes may change after
4564 * png_read_update_info.
4566 if (png_get_rowbytes(pp, pi) != standard_rowsize(pp, dp->id))
4567 png_error(pp, "validate: row size changed");
4569 /* Validate the colour type 3 palette (this can be present on other color
4572 standard_palette_validate(dp, pp, pi);
4574 /* In any case always check for a tranparent color (notice that the
4575 * colour type 3 case must not give a successful return on the get_tRNS call
4576 * with these arguments!)
4579 png_color_16p trans_color = 0;
4581 if (png_get_tRNS(pp, pi, 0, 0, &trans_color) & PNG_INFO_tRNS)
4583 if (trans_color == 0)
4584 png_error(pp, "validate: unexpected png_get_tRNS (color) result");
4586 switch (dp->colour_type)
4589 dp->transparent.red = dp->transparent.green = dp->transparent.blue =
4591 dp->is_transparent = 1;
4595 dp->transparent.red = trans_color->red;
4596 dp->transparent.green = trans_color->green;
4597 dp->transparent.blue = trans_color->blue;
4598 dp->is_transparent = 1;
4602 /* Not expected because it should result in the array case
4605 png_error(pp, "validate: unexpected png_get_tRNS result");
4609 png_error(pp, "validate: invalid tRNS chunk with alpha image");
4614 /* Read the number of passes - expected to match the value used when
4615 * creating the image (interlaced or not). This has the side effect of
4616 * turning on interlace handling (if do_interlace is not set.)
4618 dp->npasses = npasses_from_interlace_type(pp, dp->interlace_type);
4619 if (!dp->do_interlace && dp->npasses != png_set_interlace_handling(pp))
4620 png_error(pp, "validate: file changed interlace type");
4622 /* Caller calls png_read_update_info or png_start_read_image now, then calls
4627 /* This must be called *after* the png_read_update_info call to get the correct
4628 * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed
4632 standard_info_part2(standard_display *dp, png_const_structp pp,
4633 png_const_infop pi, int nImages)
4635 /* Record cbRow now that it can be found. */
4636 dp->pixel_size = bit_size(pp, png_get_color_type(pp, pi),
4637 png_get_bit_depth(pp, pi));
4638 dp->bit_width = png_get_image_width(pp, pi) * dp->pixel_size;
4639 dp->cbRow = png_get_rowbytes(pp, pi);
4641 /* Validate the rowbytes here again. */
4642 if (dp->cbRow != (dp->bit_width+7)/8)
4643 png_error(pp, "bad png_get_rowbytes calculation");
4645 /* Then ensure there is enough space for the output image(s). */
4646 store_ensure_image(dp->ps, pp, nImages, dp->cbRow, dp->h);
4650 standard_info_imp(standard_display *dp, png_structp pp, png_infop pi,
4653 /* Note that the validation routine has the side effect of turning on
4654 * interlace handling in the subsequent code.
4656 standard_info_part1(dp, pp, pi);
4658 /* And the info callback has to call this (or png_read_update_info - see
4659 * below in the png_modifier code for that variant.
4661 if (dp->use_update_info)
4663 /* For debugging the effect of multiple calls: */
4664 int i = dp->use_update_info;
4666 png_read_update_info(pp, pi);
4670 png_start_read_image(pp);
4672 /* Validate the height, width and rowbytes plus ensure that sufficient buffer
4673 * exists for decoding the image.
4675 standard_info_part2(dp, pp, pi, nImages);
4678 static void PNGCBAPI
4679 standard_info(png_structp pp, png_infop pi)
4681 standard_display *dp = voidcast(standard_display*,
4682 png_get_progressive_ptr(pp));
4684 /* Call with nImages==1 because the progressive reader can only produce one
4687 standard_info_imp(dp, pp, pi, 1 /*only one image*/);
4690 static void PNGCBAPI
4691 progressive_row(png_structp ppIn, png_bytep new_row, png_uint_32 y, int pass)
4693 png_const_structp pp = ppIn;
4694 PNG_CONST standard_display *dp = voidcast(standard_display*,
4695 png_get_progressive_ptr(pp));
4697 /* When handling interlacing some rows will be absent in each pass, the
4698 * callback still gets called, but with a NULL pointer. This is checked
4699 * in the 'else' clause below. We need our own 'cbRow', but we can't call
4700 * png_get_rowbytes because we got no info structure.
4702 if (new_row != NULL)
4706 /* In the case where the reader doesn't do the interlace it gives
4707 * us the y in the sub-image:
4709 if (dp->do_interlace && dp->interlace_type == PNG_INTERLACE_ADAM7)
4711 #ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED
4712 /* Use this opportunity to validate the png 'current' APIs: */
4713 if (y != png_get_current_row_number(pp))
4714 png_error(pp, "png_get_current_row_number is broken");
4716 if (pass != png_get_current_pass_number(pp))
4717 png_error(pp, "png_get_current_pass_number is broken");
4720 y = PNG_ROW_FROM_PASS_ROW(y, pass);
4723 /* Validate this just in case. */
4725 png_error(pp, "invalid y to progressive row callback");
4727 row = store_image_row(dp->ps, pp, 0, y);
4729 #ifdef PNG_READ_INTERLACING_SUPPORTED
4730 /* Combine the new row into the old: */
4731 if (dp->do_interlace)
4733 if (dp->interlace_type == PNG_INTERLACE_ADAM7)
4734 deinterlace_row(row, new_row, dp->pixel_size, dp->w, pass);
4736 row_copy(row, new_row, dp->pixel_size * dp->w);
4739 png_progressive_combine_row(pp, row, new_row);
4740 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4743 #ifdef PNG_READ_INTERLACING_SUPPORTED
4744 else if (dp->interlace_type == PNG_INTERLACE_ADAM7 &&
4745 PNG_ROW_IN_INTERLACE_PASS(y, pass) &&
4746 PNG_PASS_COLS(dp->w, pass) > 0)
4747 png_error(pp, "missing row in progressive de-interlacing");
4748 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4752 sequential_row(standard_display *dp, png_structp pp, png_infop pi,
4753 PNG_CONST int iImage, PNG_CONST int iDisplay)
4755 PNG_CONST int npasses = dp->npasses;
4756 PNG_CONST int do_interlace = dp->do_interlace &&
4757 dp->interlace_type == PNG_INTERLACE_ADAM7;
4758 PNG_CONST png_uint_32 height = standard_height(pp, dp->id);
4759 PNG_CONST png_uint_32 width = standard_width(pp, dp->id);
4760 PNG_CONST png_store* ps = dp->ps;
4763 for (pass=0; pass<npasses; ++pass)
4766 png_uint_32 wPass = PNG_PASS_COLS(width, pass);
4768 for (y=0; y<height; ++y)
4772 /* wPass may be zero or this row may not be in this pass.
4773 * png_read_row must not be called in either case.
4775 if (wPass > 0 && PNG_ROW_IN_INTERLACE_PASS(y, pass))
4777 /* Read the row into a pair of temporary buffers, then do the
4778 * merge here into the output rows.
4780 png_byte row[STANDARD_ROWMAX], display[STANDARD_ROWMAX];
4782 /* The following aids (to some extent) error detection - we can
4783 * see where png_read_row wrote. Use opposite values in row and
4784 * display to make this easier. Don't use 0xff (which is used in
4785 * the image write code to fill unused bits) or 0 (which is a
4786 * likely value to overwrite unused bits with).
4788 memset(row, 0xc5, sizeof row);
4789 memset(display, 0x5c, sizeof display);
4791 png_read_row(pp, row, display);
4794 deinterlace_row(store_image_row(ps, pp, iImage, y), row,
4795 dp->pixel_size, dp->w, pass);
4798 deinterlace_row(store_image_row(ps, pp, iDisplay, y), display,
4799 dp->pixel_size, dp->w, pass);
4804 iImage >= 0 ? store_image_row(ps, pp, iImage, y) : NULL,
4805 iDisplay >= 0 ? store_image_row(ps, pp, iDisplay, y) : NULL);
4809 /* And finish the read operation (only really necessary if the caller wants
4810 * to find additional data in png_info from chunks after the last IDAT.)
4812 png_read_end(pp, pi);
4815 #ifdef PNG_TEXT_SUPPORTED
4817 standard_check_text(png_const_structp pp, png_const_textp tp,
4818 png_const_charp keyword, png_const_charp text)
4821 size_t pos = safecat(msg, sizeof msg, 0, "text: ");
4824 pos = safecat(msg, sizeof msg, pos, keyword);
4825 pos = safecat(msg, sizeof msg, pos, ": ");
4828 if (tp->compression != TEXT_COMPRESSION)
4832 sprintf(buf, "compression [%d->%d], ", TEXT_COMPRESSION,
4834 pos = safecat(msg, sizeof msg, pos, buf);
4837 if (tp->key == NULL || strcmp(tp->key, keyword) != 0)
4839 pos = safecat(msg, sizeof msg, pos, "keyword \"");
4840 if (tp->key != NULL)
4842 pos = safecat(msg, sizeof msg, pos, tp->key);
4843 pos = safecat(msg, sizeof msg, pos, "\", ");
4847 pos = safecat(msg, sizeof msg, pos, "null, ");
4850 if (tp->text == NULL)
4851 pos = safecat(msg, sizeof msg, pos, "text lost, ");
4855 if (tp->text_length != strlen(text))
4858 sprintf(buf, "text length changed[%lu->%lu], ",
4859 (unsigned long)strlen(text), (unsigned long)tp->text_length);
4860 pos = safecat(msg, sizeof msg, pos, buf);
4863 if (strcmp(tp->text, text) != 0)
4865 pos = safecat(msg, sizeof msg, pos, "text becomes \"");
4866 pos = safecat(msg, sizeof msg, pos, tp->text);
4867 pos = safecat(msg, sizeof msg, pos, "\" (was \"");
4868 pos = safecat(msg, sizeof msg, pos, text);
4869 pos = safecat(msg, sizeof msg, pos, "\"), ");
4873 if (tp->itxt_length != 0)
4874 pos = safecat(msg, sizeof msg, pos, "iTXt length set, ");
4876 if (tp->lang != NULL)
4878 pos = safecat(msg, sizeof msg, pos, "iTXt language \"");
4879 pos = safecat(msg, sizeof msg, pos, tp->lang);
4880 pos = safecat(msg, sizeof msg, pos, "\", ");
4883 if (tp->lang_key != NULL)
4885 pos = safecat(msg, sizeof msg, pos, "iTXt keyword \"");
4886 pos = safecat(msg, sizeof msg, pos, tp->lang_key);
4887 pos = safecat(msg, sizeof msg, pos, "\", ");
4892 msg[pos-2] = '\0'; /* Remove the ", " at the end */
4898 standard_text_validate(standard_display *dp, png_const_structp pp,
4899 png_infop pi, int check_end)
4901 png_textp tp = NULL;
4902 png_uint_32 num_text = png_get_text(pp, pi, &tp, NULL);
4904 if (num_text == 2 && tp != NULL)
4906 standard_check_text(pp, tp, "image name", dp->ps->current->name);
4908 /* This exists because prior to 1.5.18 the progressive reader left the
4909 * png_struct z_stream unreset at the end of the image, so subsequent
4910 * attempts to use it simply returns Z_STREAM_END.
4913 standard_check_text(pp, tp+1, "end marker", "end");
4920 sprintf(msg, "expected two text items, got %lu",
4921 (unsigned long)num_text);
4926 # define standard_text_validate(dp,pp,pi,check_end) ((void)0)
4930 standard_row_validate(standard_display *dp, png_const_structp pp,
4931 int iImage, int iDisplay, png_uint_32 y)
4934 png_byte std[STANDARD_ROWMAX];
4936 /* The row must be pre-initialized to the magic number here for the size
4939 memset(std, 178, sizeof std);
4940 standard_row(pp, std, dp->id, y);
4942 /* At the end both the 'row' and 'display' arrays should end up identical.
4943 * In earlier passes 'row' will be partially filled in, with only the pixels
4944 * that have been read so far, but 'display' will have those pixels
4945 * replicated to fill the unread pixels while reading an interlaced image.
4946 #if PNG_LIBPNG_VER < 10506
4947 * The side effect inside the libpng sequential reader is that the 'row'
4948 * array retains the correct values for unwritten pixels within the row
4949 * bytes, while the 'display' array gets bits off the end of the image (in
4950 * the last byte) trashed. Unfortunately in the progressive reader the
4951 * row bytes are always trashed, so we always do a pixel_cmp here even though
4952 * a memcmp of all cbRow bytes will succeed for the sequential reader.
4956 (where = pixel_cmp(std, store_image_row(dp->ps, pp, iImage, y),
4957 dp->bit_width)) != 0)
4960 sprintf(msg, "PNG image row[%lu][%d] changed from %.2x to %.2x",
4961 (unsigned long)y, where-1, std[where-1],
4962 store_image_row(dp->ps, pp, iImage, y)[where-1]);
4966 #if PNG_LIBPNG_VER < 10506
4967 /* In this case use pixel_cmp because we need to compare a partial
4968 * byte at the end of the row if the row is not an exact multiple
4969 * of 8 bits wide. (This is fixed in libpng-1.5.6 and pixel_cmp is
4970 * changed to match!)
4973 if (iDisplay >= 0 &&
4974 (where = pixel_cmp(std, store_image_row(dp->ps, pp, iDisplay, y),
4975 dp->bit_width)) != 0)
4978 sprintf(msg, "display row[%lu][%d] changed from %.2x to %.2x",
4979 (unsigned long)y, where-1, std[where-1],
4980 store_image_row(dp->ps, pp, iDisplay, y)[where-1]);
4986 standard_image_validate(standard_display *dp, png_const_structp pp, int iImage,
4992 store_image_check(dp->ps, pp, iImage);
4995 store_image_check(dp->ps, pp, iDisplay);
4997 for (y=0; y<dp->h; ++y)
4998 standard_row_validate(dp, pp, iImage, iDisplay, y);
5000 /* This avoids false positives if the validation code is never called! */
5001 dp->ps->validated = 1;
5004 static void PNGCBAPI
5005 standard_end(png_structp ppIn, png_infop pi)
5007 png_const_structp pp = ppIn;
5008 standard_display *dp = voidcast(standard_display*,
5009 png_get_progressive_ptr(pp));
5013 /* Validate the image - progressive reading only produces one variant for
5014 * interlaced images.
5016 standard_text_validate(dp, pp, pi,
5017 PNG_LIBPNG_VER >= 10518/*check_end: see comments above*/);
5018 standard_image_validate(dp, pp, 0, -1);
5021 /* A single test run checking the standard image to ensure it is not damaged. */
5023 standard_test(png_store* PNG_CONST psIn, png_uint_32 PNG_CONST id,
5024 int do_interlace, int use_update_info)
5027 context(psIn, fault);
5029 /* Set up the display (stack frame) variables from the arguments to the
5030 * function and initialize the locals that are filled in later.
5032 standard_display_init(&d, psIn, id, do_interlace, use_update_info);
5034 /* Everything is protected by a Try/Catch. The functions called also
5035 * typically have local Try/Catch blocks.
5042 /* Get a png_struct for reading the image. This will throw an error if it
5043 * fails, so we don't need to check the result.
5045 pp = set_store_for_read(d.ps, &pi, d.id,
5046 d.do_interlace ? (d.ps->progressive ?
5047 "pngvalid progressive deinterlacer" :
5048 "pngvalid sequential deinterlacer") : (d.ps->progressive ?
5049 "progressive reader" : "sequential reader"));
5051 /* Initialize the palette correctly from the png_store_file. */
5052 standard_palette_init(&d);
5054 /* Introduce the correct read function. */
5055 if (d.ps->progressive)
5057 png_set_progressive_read_fn(pp, &d, standard_info, progressive_row,
5060 /* Now feed data into the reader until we reach the end: */
5061 store_progressive_read(d.ps, pp, pi);
5065 /* Note that this takes the store, not the display. */
5066 png_set_read_fn(pp, d.ps, store_read);
5068 /* Check the header values: */
5069 png_read_info(pp, pi);
5071 /* The code tests both versions of the images that the sequential
5072 * reader can produce.
5074 standard_info_imp(&d, pp, pi, 2 /*images*/);
5076 /* Need the total bytes in the image below; we can't get to this point
5077 * unless the PNG file values have been checked against the expected
5081 sequential_row(&d, pp, pi, 0, 1);
5083 /* After the last pass loop over the rows again to check that the
5088 standard_text_validate(&d, pp, pi, 1/*check_end*/);
5089 standard_image_validate(&d, pp, 0, 1);
5092 d.ps->validated = 1;
5096 /* Check for validation. */
5097 if (!d.ps->validated)
5098 png_error(pp, "image read failed silently");
5100 /* Successful completion. */
5104 d.ps = fault; /* make sure this hasn't been clobbered. */
5106 /* In either case clean up the store. */
5107 store_read_reset(d.ps);
5111 test_standard(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
5112 int bdlo, int PNG_CONST bdhi)
5114 for (; bdlo <= bdhi; ++bdlo)
5118 for (interlace_type = PNG_INTERLACE_NONE;
5119 interlace_type < INTERLACE_LAST; ++interlace_type)
5121 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5122 interlace_type, 0, 0, 0), 0/*do_interlace*/, pm->use_update_info);
5129 return 1; /* keep going */
5133 perform_standard_test(png_modifier *pm)
5135 /* Test each colour type over the valid range of bit depths (expressed as
5136 * log2(bit_depth) in turn, stop as soon as any error is detected.
5138 if (!test_standard(pm, 0, 0, READ_BDHI))
5141 if (!test_standard(pm, 2, 3, READ_BDHI))
5144 if (!test_standard(pm, 3, 0, 3))
5147 if (!test_standard(pm, 4, 3, READ_BDHI))
5150 if (!test_standard(pm, 6, 3, READ_BDHI))
5155 /********************************** SIZE TESTS ********************************/
5157 test_size(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
5158 int bdlo, int PNG_CONST bdhi)
5160 /* Run the tests on each combination.
5162 * NOTE: on my 32 bit x86 each of the following blocks takes
5163 * a total of 3.5 seconds if done across every combo of bit depth
5164 * width and height. This is a waste of time in practice, hence the
5165 * hinc and winc stuff:
5167 static PNG_CONST png_byte hinc[] = {1, 3, 11, 1, 5};
5168 static PNG_CONST png_byte winc[] = {1, 9, 5, 7, 1};
5169 for (; bdlo <= bdhi; ++bdlo)
5173 for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
5175 /* First test all the 'size' images against the sequential
5176 * reader using libpng to deinterlace (where required.) This
5177 * validates the write side of libpng. There are four possibilities
5180 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5181 PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
5182 pm->use_update_info);
5187 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5188 PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
5189 pm->use_update_info);
5194 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
5195 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5196 PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
5197 pm->use_update_info);
5202 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5203 PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
5204 pm->use_update_info);
5210 /* Now validate the interlaced read side - do_interlace true,
5211 * in the progressive case this does actually make a difference
5212 * to the code used in the non-interlaced case too.
5214 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5215 PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
5216 pm->use_update_info);
5221 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
5222 standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
5223 PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
5224 pm->use_update_info);
5232 return 1; /* keep going */
5236 perform_size_test(png_modifier *pm)
5238 /* Test each colour type over the valid range of bit depths (expressed as
5239 * log2(bit_depth) in turn, stop as soon as any error is detected.
5241 if (!test_size(pm, 0, 0, READ_BDHI))
5244 if (!test_size(pm, 2, 3, READ_BDHI))
5247 /* For the moment don't do the palette test - it's a waste of time when
5248 * compared to the grayscale test.
5251 if (!test_size(pm, 3, 0, 3))
5255 if (!test_size(pm, 4, 3, READ_BDHI))
5258 if (!test_size(pm, 6, 3, READ_BDHI))
5263 /******************************* TRANSFORM TESTS ******************************/
5264 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
5265 /* A set of tests to validate libpng image transforms. The possibilities here
5266 * are legion because the transforms can be combined in a combinatorial
5267 * fashion. To deal with this some measure of restraint is required, otherwise
5268 * the tests would take forever.
5270 typedef struct image_pixel
5272 /* A local (pngvalid) representation of a PNG pixel, in all its
5275 unsigned int red, green, blue, alpha; /* For non-palette images. */
5276 unsigned int palette_index; /* For a palette image. */
5277 png_byte colour_type; /* As in the spec. */
5278 png_byte bit_depth; /* Defines bit size in row */
5279 png_byte sample_depth; /* Scale of samples */
5280 int have_tRNS; /* tRNS chunk may need processing */
5282 /* For checking the code calculates double precision floating point values
5283 * along with an error value, accumulated from the transforms. Because an
5284 * sBIT setting allows larger error bounds (indeed, by the spec, apparently
5285 * up to just less than +/-1 in the scaled value) the *lowest* sBIT for each
5286 * channel is stored. This sBIT value is folded in to the stored error value
5287 * at the end of the application of the transforms to the pixel.
5289 double redf, greenf, bluef, alphaf;
5290 double rede, greene, bluee, alphae;
5291 png_byte red_sBIT, green_sBIT, blue_sBIT, alpha_sBIT;
5294 /* Shared utility function, see below. */
5296 image_pixel_setf(image_pixel *this, unsigned int max)
5298 this->redf = this->red / (double)max;
5299 this->greenf = this->green / (double)max;
5300 this->bluef = this->blue / (double)max;
5301 this->alphaf = this->alpha / (double)max;
5303 if (this->red < max)
5304 this->rede = this->redf * DBL_EPSILON;
5307 if (this->green < max)
5308 this->greene = this->greenf * DBL_EPSILON;
5311 if (this->blue < max)
5312 this->bluee = this->bluef * DBL_EPSILON;
5315 if (this->alpha < max)
5316 this->alphae = this->alphaf * DBL_EPSILON;
5321 /* Initialize the structure for the next pixel - call this before doing any
5322 * transforms and call it for each pixel since all the fields may need to be
5326 image_pixel_init(image_pixel *this, png_const_bytep row, png_byte colour_type,
5327 png_byte bit_depth, png_uint_32 x, store_palette palette)
5329 PNG_CONST png_byte sample_depth = (png_byte)(colour_type ==
5330 PNG_COLOR_TYPE_PALETTE ? 8 : bit_depth);
5331 PNG_CONST unsigned int max = (1U<<sample_depth)-1;
5333 /* Initially just set everything to the same number and the alpha to opaque.
5334 * Note that this currently assumes a simple palette where entry x has colour
5337 this->palette_index = this->red = this->green = this->blue =
5338 sample(row, colour_type, bit_depth, x, 0);
5340 this->red_sBIT = this->green_sBIT = this->blue_sBIT = this->alpha_sBIT =
5343 /* Then override as appropriate: */
5344 if (colour_type == 3) /* palette */
5346 /* This permits the caller to default to the sample value. */
5349 PNG_CONST unsigned int i = this->palette_index;
5351 this->red = palette[i].red;
5352 this->green = palette[i].green;
5353 this->blue = palette[i].blue;
5354 this->alpha = palette[i].alpha;
5358 else /* not palette */
5362 if (colour_type & 2)
5364 this->green = sample(row, colour_type, bit_depth, x, 1);
5365 this->blue = sample(row, colour_type, bit_depth, x, 2);
5368 if (colour_type & 4)
5369 this->alpha = sample(row, colour_type, bit_depth, x, ++i);
5372 /* Calculate the scaled values, these are simply the values divided by
5373 * 'max' and the error is initialized to the double precision epsilon value
5374 * from the header file.
5376 image_pixel_setf(this, max);
5378 /* Store the input information for use in the transforms - these will
5379 * modify the information.
5381 this->colour_type = colour_type;
5382 this->bit_depth = bit_depth;
5383 this->sample_depth = sample_depth;
5384 this->have_tRNS = 0;
5387 /* Convert a palette image to an rgb image. This necessarily converts the tRNS
5388 * chunk at the same time, because the tRNS will be in palette form. The way
5389 * palette validation works means that the original palette is never updated,
5390 * instead the image_pixel value from the row contains the RGB of the
5391 * corresponding palette entry and *this* is updated. Consequently this routine
5392 * only needs to change the colour type information.
5395 image_pixel_convert_PLTE(image_pixel *this)
5397 if (this->colour_type == PNG_COLOR_TYPE_PALETTE)
5399 if (this->have_tRNS)
5401 this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
5402 this->have_tRNS = 0;
5405 this->colour_type = PNG_COLOR_TYPE_RGB;
5407 /* The bit depth of the row changes at this point too (notice that this is
5408 * the row format, not the sample depth, which is separate.)
5410 this->bit_depth = 8;
5414 /* Add an alpha channel; this will import the tRNS information because tRNS is
5415 * not valid in an alpha image. The bit depth will invariably be set to at
5416 * least 8. Palette images will be converted to alpha (using the above API).
5419 image_pixel_add_alpha(image_pixel *this, PNG_CONST standard_display *display)
5421 if (this->colour_type == PNG_COLOR_TYPE_PALETTE)
5422 image_pixel_convert_PLTE(this);
5424 if ((this->colour_type & PNG_COLOR_MASK_ALPHA) == 0)
5426 if (this->colour_type == PNG_COLOR_TYPE_GRAY)
5428 if (this->bit_depth < 8)
5429 this->bit_depth = 8;
5431 if (this->have_tRNS)
5433 this->have_tRNS = 0;
5435 /* Check the input, original, channel value here against the
5436 * original tRNS gray chunk valie.
5438 if (this->red == display->transparent.red)
5446 this->colour_type = PNG_COLOR_TYPE_GRAY_ALPHA;
5449 else if (this->colour_type == PNG_COLOR_TYPE_RGB)
5451 if (this->have_tRNS)
5453 this->have_tRNS = 0;
5455 /* Again, check the exact input values, not the current transformed
5458 if (this->red == display->transparent.red &&
5459 this->green == display->transparent.green &&
5460 this->blue == display->transparent.blue)
5465 this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
5469 /* The error in the alpha is zero and the sBIT value comes from the
5470 * original sBIT data (actually it will always be the original bit depth).
5473 this->alpha_sBIT = display->alpha_sBIT;
5477 struct transform_display;
5478 typedef struct image_transform
5480 /* The name of this transform: a string. */
5481 PNG_CONST char *name;
5483 /* Each transform can be disabled from the command line: */
5486 /* The global list of transforms; read only. */
5487 struct image_transform *PNG_CONST list;
5489 /* The global count of the number of times this transform has been set on an
5492 unsigned int global_use;
5494 /* The local count of the number of times this transform has been set. */
5495 unsigned int local_use;
5497 /* The next transform in the list, each transform must call its own next
5498 * transform after it has processed the pixel successfully.
5500 PNG_CONST struct image_transform *next;
5502 /* A single transform for the image, expressed as a series of function
5503 * callbacks and some space for values.
5505 * First a callback to add any required modifications to the png_modifier;
5506 * this gets called just before the modifier is set up for read.
5508 void (*ini)(PNG_CONST struct image_transform *this,
5509 struct transform_display *that);
5511 /* And a callback to set the transform on the current png_read_struct:
5513 void (*set)(PNG_CONST struct image_transform *this,
5514 struct transform_display *that, png_structp pp, png_infop pi);
5516 /* Then a transform that takes an input pixel in one PNG format or another
5517 * and modifies it by a pngvalid implementation of the transform (thus
5518 * duplicating the libpng intent without, we hope, duplicating the bugs
5519 * in the libpng implementation!) The png_structp is solely to allow error
5520 * reporting via png_error and png_warning.
5522 void (*mod)(PNG_CONST struct image_transform *this, image_pixel *that,
5523 png_const_structp pp, PNG_CONST struct transform_display *display);
5525 /* Add this transform to the list and return true if the transform is
5526 * meaningful for this colour type and bit depth - if false then the
5527 * transform should have no effect on the image so there's not a lot of
5530 int (*add)(struct image_transform *this,
5531 PNG_CONST struct image_transform **that, png_byte colour_type,
5532 png_byte bit_depth);
5535 typedef struct transform_display
5537 standard_display this;
5541 PNG_CONST image_transform* transform_list;
5543 /* Local variables */
5544 png_byte output_colour_type;
5545 png_byte output_bit_depth;
5547 /* Modifications (not necessarily used.) */
5548 gama_modification gama_mod;
5549 chrm_modification chrm_mod;
5550 srgb_modification srgb_mod;
5551 } transform_display;
5553 /* Set sRGB, cHRM and gAMA transforms as required by the current encoding. */
5555 transform_set_encoding(transform_display *this)
5557 /* Set up the png_modifier '_current' fields then use these to determine how
5558 * to add appropriate chunks.
5560 png_modifier *pm = this->pm;
5562 modifier_set_encoding(pm);
5564 if (modifier_color_encoding_is_set(pm))
5566 if (modifier_color_encoding_is_sRGB(pm))
5567 srgb_modification_init(&this->srgb_mod, pm, PNG_sRGB_INTENT_ABSOLUTE);
5571 /* Set gAMA and cHRM separately. */
5572 gama_modification_init(&this->gama_mod, pm, pm->current_gamma);
5574 if (pm->current_encoding != 0)
5575 chrm_modification_init(&this->chrm_mod, pm, pm->current_encoding);
5580 /* Three functions to end the list: */
5582 image_transform_ini_end(PNG_CONST image_transform *this,
5583 transform_display *that)
5590 image_transform_set_end(PNG_CONST image_transform *this,
5591 transform_display *that, png_structp pp, png_infop pi)
5599 /* At the end of the list recalculate the output image pixel value from the
5600 * double precision values set up by the preceding 'mod' calls:
5603 sample_scale(double sample_value, unsigned int scale)
5605 sample_value = floor(sample_value * scale + .5);
5607 /* Return NaN as 0: */
5608 if (!(sample_value > 0))
5610 else if (sample_value > scale)
5611 sample_value = scale;
5613 return (unsigned int)sample_value;
5617 image_transform_mod_end(PNG_CONST image_transform *this, image_pixel *that,
5618 png_const_structp pp, PNG_CONST transform_display *display)
5620 PNG_CONST unsigned int scale = (1U<<that->sample_depth)-1;
5626 /* At the end recalculate the digitized red green and blue values according
5627 * to the current sample_depth of the pixel.
5629 * The sample value is simply scaled to the maximum, checking for over
5630 * and underflow (which can both happen for some image transforms,
5631 * including simple size scaling, though libpng doesn't do that at present.
5633 that->red = sample_scale(that->redf, scale);
5635 /* The error value is increased, at the end, according to the lowest sBIT
5636 * value seen. Common sense tells us that the intermediate integer
5637 * representations are no more accurate than +/- 0.5 in the integral values,
5638 * the sBIT allows the implementation to be worse than this. In addition the
5639 * PNG specification actually permits any error within the range (-1..+1),
5640 * but that is ignored here. Instead the final digitized value is compared,
5641 * below to the digitized value of the error limits - this has the net effect
5642 * of allowing (almost) +/-1 in the output value. It's difficult to see how
5643 * any algorithm that digitizes intermediate results can be more accurate.
5645 that->rede += 1./(2*((1U<<that->red_sBIT)-1));
5647 if (that->colour_type & PNG_COLOR_MASK_COLOR)
5649 that->green = sample_scale(that->greenf, scale);
5650 that->blue = sample_scale(that->bluef, scale);
5651 that->greene += 1./(2*((1U<<that->green_sBIT)-1));
5652 that->bluee += 1./(2*((1U<<that->blue_sBIT)-1));
5656 that->blue = that->green = that->red;
5657 that->bluef = that->greenf = that->redf;
5658 that->bluee = that->greene = that->rede;
5661 if ((that->colour_type & PNG_COLOR_MASK_ALPHA) ||
5662 that->colour_type == PNG_COLOR_TYPE_PALETTE)
5664 that->alpha = sample_scale(that->alphaf, scale);
5665 that->alphae += 1./(2*((1U<<that->alpha_sBIT)-1));
5669 that->alpha = scale; /* opaque */
5670 that->alpha = 1; /* Override this. */
5671 that->alphae = 0; /* It's exact ;-) */
5675 /* Static 'end' structure: */
5676 static image_transform image_transform_end =
5684 image_transform_ini_end,
5685 image_transform_set_end,
5686 image_transform_mod_end,
5687 0 /* never called, I want it to crash if it is! */
5690 /* Reader callbacks and implementations, where they differ from the standard
5694 transform_display_init(transform_display *dp, png_modifier *pm, png_uint_32 id,
5695 PNG_CONST image_transform *transform_list)
5697 memset(dp, 0, sizeof *dp);
5699 /* Standard fields */
5700 standard_display_init(&dp->this, &pm->this, id, 0/*do_interlace*/,
5701 pm->use_update_info);
5703 /* Parameter fields */
5705 dp->transform_list = transform_list;
5707 /* Local variable fields */
5708 dp->output_colour_type = 255; /* invalid */
5709 dp->output_bit_depth = 255; /* invalid */
5713 transform_info_imp(transform_display *dp, png_structp pp, png_infop pi)
5715 /* Reuse the standard stuff as appropriate. */
5716 standard_info_part1(&dp->this, pp, pi);
5718 /* Now set the list of transforms. */
5719 dp->transform_list->set(dp->transform_list, dp, pp, pi);
5721 /* Update the info structure for these transforms: */
5723 int i = dp->this.use_update_info;
5724 /* Always do one call, even if use_update_info is 0. */
5726 png_read_update_info(pp, pi);
5730 /* And get the output information into the standard_display */
5731 standard_info_part2(&dp->this, pp, pi, 1/*images*/);
5733 /* Plus the extra stuff we need for the transform tests: */
5734 dp->output_colour_type = png_get_color_type(pp, pi);
5735 dp->output_bit_depth = png_get_bit_depth(pp, pi);
5737 /* Validate the combination of colour type and bit depth that we are getting
5738 * out of libpng; the semantics of something not in the PNG spec are, at
5741 switch (dp->output_colour_type)
5743 case PNG_COLOR_TYPE_PALETTE:
5744 if (dp->output_bit_depth > 8) goto error;
5746 case PNG_COLOR_TYPE_GRAY:
5747 if (dp->output_bit_depth == 1 || dp->output_bit_depth == 2 ||
5748 dp->output_bit_depth == 4)
5752 if (dp->output_bit_depth == 8 || dp->output_bit_depth == 16)
5760 pos = safecat(message, sizeof message, 0,
5761 "invalid final bit depth: colour type(");
5762 pos = safecatn(message, sizeof message, pos, dp->output_colour_type);
5763 pos = safecat(message, sizeof message, pos, ") with bit depth: ");
5764 pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5766 png_error(pp, message);
5770 /* Use a test pixel to check that the output agrees with what we expect -
5771 * this avoids running the whole test if the output is unexpected.
5774 image_pixel test_pixel;
5776 memset(&test_pixel, 0, sizeof test_pixel);
5777 test_pixel.colour_type = dp->this.colour_type; /* input */
5778 test_pixel.bit_depth = dp->this.bit_depth;
5779 if (test_pixel.colour_type == PNG_COLOR_TYPE_PALETTE)
5780 test_pixel.sample_depth = 8;
5782 test_pixel.sample_depth = test_pixel.bit_depth;
5783 /* Don't need sBIT here, but it must be set to non-zero to avoid
5784 * arithmetic overflows.
5786 test_pixel.have_tRNS = dp->this.is_transparent;
5787 test_pixel.red_sBIT = test_pixel.green_sBIT = test_pixel.blue_sBIT =
5788 test_pixel.alpha_sBIT = test_pixel.sample_depth;
5790 dp->transform_list->mod(dp->transform_list, &test_pixel, pp, dp);
5792 if (test_pixel.colour_type != dp->output_colour_type)
5795 size_t pos = safecat(message, sizeof message, 0, "colour type ");
5797 pos = safecatn(message, sizeof message, pos, dp->output_colour_type);
5798 pos = safecat(message, sizeof message, pos, " expected ");
5799 pos = safecatn(message, sizeof message, pos, test_pixel.colour_type);
5801 png_error(pp, message);
5804 if (test_pixel.bit_depth != dp->output_bit_depth)
5807 size_t pos = safecat(message, sizeof message, 0, "bit depth ");
5809 pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5810 pos = safecat(message, sizeof message, pos, " expected ");
5811 pos = safecatn(message, sizeof message, pos, test_pixel.bit_depth);
5813 png_error(pp, message);
5816 /* If both bit depth and colour type are correct check the sample depth.
5817 * I believe these are both internal errors.
5819 if (test_pixel.colour_type == PNG_COLOR_TYPE_PALETTE)
5821 if (test_pixel.sample_depth != 8) /* oops - internal error! */
5822 png_error(pp, "pngvalid: internal: palette sample depth not 8");
5824 else if (test_pixel.sample_depth != dp->output_bit_depth)
5827 size_t pos = safecat(message, sizeof message, 0,
5828 "internal: sample depth ");
5830 pos = safecatn(message, sizeof message, pos, dp->output_bit_depth);
5831 pos = safecat(message, sizeof message, pos, " expected ");
5832 pos = safecatn(message, sizeof message, pos, test_pixel.sample_depth);
5834 png_error(pp, message);
5839 static void PNGCBAPI
5840 transform_info(png_structp pp, png_infop pi)
5842 transform_info_imp(voidcast(transform_display*, png_get_progressive_ptr(pp)),
5847 transform_range_check(png_const_structp pp, unsigned int r, unsigned int g,
5848 unsigned int b, unsigned int a, unsigned int in_digitized, double in,
5849 unsigned int out, png_byte sample_depth, double err, double limit,
5850 PNG_CONST char *name, double digitization_error)
5852 /* Compare the scaled, digitzed, values of our local calculation (in+-err)
5853 * with the digitized values libpng produced; 'sample_depth' is the actual
5854 * digitization depth of the libpng output colors (the bit depth except for
5855 * palette images where it is always 8.) The check on 'err' is to detect
5856 * internal errors in pngvalid itself.
5858 unsigned int max = (1U<<sample_depth)-1;
5859 double in_min = ceil((in-err)*max - digitization_error);
5860 double in_max = floor((in+err)*max + digitization_error);
5861 if (err > limit || !(out >= in_min && out <= in_max))
5866 pos = safecat(message, sizeof message, 0, name);
5867 pos = safecat(message, sizeof message, pos, " output value error: rgba(");
5868 pos = safecatn(message, sizeof message, pos, r);
5869 pos = safecat(message, sizeof message, pos, ",");
5870 pos = safecatn(message, sizeof message, pos, g);
5871 pos = safecat(message, sizeof message, pos, ",");
5872 pos = safecatn(message, sizeof message, pos, b);
5873 pos = safecat(message, sizeof message, pos, ",");
5874 pos = safecatn(message, sizeof message, pos, a);
5875 pos = safecat(message, sizeof message, pos, "): ");
5876 pos = safecatn(message, sizeof message, pos, out);
5877 pos = safecat(message, sizeof message, pos, " expected: ");
5878 pos = safecatn(message, sizeof message, pos, in_digitized);
5879 pos = safecat(message, sizeof message, pos, " (");
5880 pos = safecatd(message, sizeof message, pos, (in-err)*max, 3);
5881 pos = safecat(message, sizeof message, pos, "..");
5882 pos = safecatd(message, sizeof message, pos, (in+err)*max, 3);
5883 pos = safecat(message, sizeof message, pos, ")");
5885 png_error(pp, message);
5890 transform_image_validate(transform_display *dp, png_const_structp pp,
5893 /* Constants for the loop below: */
5894 PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
5895 PNG_CONST png_byte in_ct = dp->this.colour_type;
5896 PNG_CONST png_byte in_bd = dp->this.bit_depth;
5897 PNG_CONST png_uint_32 w = dp->this.w;
5898 PNG_CONST png_uint_32 h = dp->this.h;
5899 PNG_CONST png_byte out_ct = dp->output_colour_type;
5900 PNG_CONST png_byte out_bd = dp->output_bit_depth;
5901 PNG_CONST png_byte sample_depth = (png_byte)(out_ct ==
5902 PNG_COLOR_TYPE_PALETTE ? 8 : out_bd);
5903 PNG_CONST png_byte red_sBIT = dp->this.red_sBIT;
5904 PNG_CONST png_byte green_sBIT = dp->this.green_sBIT;
5905 PNG_CONST png_byte blue_sBIT = dp->this.blue_sBIT;
5906 PNG_CONST png_byte alpha_sBIT = dp->this.alpha_sBIT;
5907 PNG_CONST int have_tRNS = dp->this.is_transparent;
5908 double digitization_error;
5910 store_palette out_palette;
5915 /* Check for row overwrite errors */
5916 store_image_check(dp->this.ps, pp, 0);
5918 /* Read the palette corresponding to the output if the output colour type
5919 * indicates a palette, othewise set out_palette to garbage.
5921 if (out_ct == PNG_COLOR_TYPE_PALETTE)
5923 /* Validate that the palette count itself has not changed - this is not
5926 int npalette = (-1);
5928 (void)read_palette(out_palette, &npalette, pp, pi);
5929 if (npalette != dp->this.npalette)
5930 png_error(pp, "unexpected change in palette size");
5932 digitization_error = .5;
5936 png_byte in_sample_depth;
5938 memset(out_palette, 0x5e, sizeof out_palette);
5940 /* use-input-precision means assume that if the input has 8 bit (or less)
5941 * samples and the output has 16 bit samples the calculations will be done
5942 * with 8 bit precision, not 16.
5944 if (in_ct == PNG_COLOR_TYPE_PALETTE || in_bd < 16)
5945 in_sample_depth = 8;
5947 in_sample_depth = in_bd;
5949 if (sample_depth != 16 || in_sample_depth > 8 ||
5950 !dp->pm->calculations_use_input_precision)
5951 digitization_error = .5;
5953 /* Else calculations are at 8 bit precision, and the output actually
5954 * consists of scaled 8-bit values, so scale .5 in 8 bits to the 16 bits:
5957 digitization_error = .5 * 257;
5962 png_const_bytep PNG_CONST pRow = store_image_row(ps, pp, 0, y);
5965 /* The original, standard, row pre-transforms. */
5966 png_byte std[STANDARD_ROWMAX];
5968 transform_row(pp, std, in_ct, in_bd, y);
5970 /* Go through each original pixel transforming it and comparing with what
5971 * libpng did to the same pixel.
5975 image_pixel in_pixel, out_pixel;
5976 unsigned int r, g, b, a;
5978 /* Find out what we think the pixel should be: */
5979 image_pixel_init(&in_pixel, std, in_ct, in_bd, x, dp->this.palette);
5981 in_pixel.red_sBIT = red_sBIT;
5982 in_pixel.green_sBIT = green_sBIT;
5983 in_pixel.blue_sBIT = blue_sBIT;
5984 in_pixel.alpha_sBIT = alpha_sBIT;
5985 in_pixel.have_tRNS = have_tRNS;
5987 /* For error detection, below. */
5993 dp->transform_list->mod(dp->transform_list, &in_pixel, pp, dp);
5995 /* Read the output pixel and compare it to what we got, we don't
5996 * use the error field here, so no need to update sBIT.
5998 image_pixel_init(&out_pixel, pRow, out_ct, out_bd, x, out_palette);
6000 /* We don't expect changes to the index here even if the bit depth is
6003 if (in_ct == PNG_COLOR_TYPE_PALETTE &&
6004 out_ct == PNG_COLOR_TYPE_PALETTE)
6006 if (in_pixel.palette_index != out_pixel.palette_index)
6007 png_error(pp, "unexpected transformed palette index");
6010 /* Check the colours for palette images too - in fact the palette could
6011 * be separately verified itself in most cases.
6013 if (in_pixel.red != out_pixel.red)
6014 transform_range_check(pp, r, g, b, a, in_pixel.red, in_pixel.redf,
6015 out_pixel.red, sample_depth, in_pixel.rede,
6016 dp->pm->limit + 1./(2*((1U<<in_pixel.red_sBIT)-1)), "red/gray",
6017 digitization_error);
6019 if ((out_ct & PNG_COLOR_MASK_COLOR) != 0 &&
6020 in_pixel.green != out_pixel.green)
6021 transform_range_check(pp, r, g, b, a, in_pixel.green,
6022 in_pixel.greenf, out_pixel.green, sample_depth, in_pixel.greene,
6023 dp->pm->limit + 1./(2*((1U<<in_pixel.green_sBIT)-1)), "green",
6024 digitization_error);
6026 if ((out_ct & PNG_COLOR_MASK_COLOR) != 0 &&
6027 in_pixel.blue != out_pixel.blue)
6028 transform_range_check(pp, r, g, b, a, in_pixel.blue, in_pixel.bluef,
6029 out_pixel.blue, sample_depth, in_pixel.bluee,
6030 dp->pm->limit + 1./(2*((1U<<in_pixel.blue_sBIT)-1)), "blue",
6031 digitization_error);
6033 if ((out_ct & PNG_COLOR_MASK_ALPHA) != 0 &&
6034 in_pixel.alpha != out_pixel.alpha)
6035 transform_range_check(pp, r, g, b, a, in_pixel.alpha,
6036 in_pixel.alphaf, out_pixel.alpha, sample_depth, in_pixel.alphae,
6037 dp->pm->limit + 1./(2*((1U<<in_pixel.alpha_sBIT)-1)), "alpha",
6038 digitization_error);
6039 } /* pixel (x) loop */
6040 } /* row (y) loop */
6042 /* Record that something was actually checked to avoid a false positive. */
6043 dp->this.ps->validated = 1;
6046 static void PNGCBAPI
6047 transform_end(png_structp ppIn, png_infop pi)
6049 png_const_structp pp = ppIn;
6050 transform_display *dp = voidcast(transform_display*,
6051 png_get_progressive_ptr(pp));
6053 if (!dp->this.speed)
6054 transform_image_validate(dp, pp, pi);
6056 dp->this.ps->validated = 1;
6059 /* A single test run. */
6061 transform_test(png_modifier *pmIn, PNG_CONST png_uint_32 idIn,
6062 PNG_CONST image_transform* transform_listIn, PNG_CONST char * volatile name)
6064 transform_display d;
6065 context(&pmIn->this, fault);
6067 transform_display_init(&d, pmIn, idIn, transform_listIn);
6074 char full_name[256];
6076 /* Make sure the encoding fields are correct and enter the required
6079 transform_set_encoding(&d);
6081 /* Add any modifications required by the transform list. */
6082 d.transform_list->ini(d.transform_list, &d);
6084 /* Add the color space information, if any, to the name. */
6085 pos = safecat(full_name, sizeof full_name, pos, name);
6086 pos = safecat_current_encoding(full_name, sizeof full_name, pos, d.pm);
6088 /* Get a png_struct for reading the image. */
6089 pp = set_modifier_for_read(d.pm, &pi, d.this.id, full_name);
6090 standard_palette_init(&d.this);
6093 /* Logging (debugging only) */
6097 (void)store_message(&d.pm->this, pp, buffer, sizeof buffer, 0,
6100 fprintf(stderr, "%s\n", buffer);
6104 /* Introduce the correct read function. */
6105 if (d.pm->this.progressive)
6107 /* Share the row function with the standard implementation. */
6108 png_set_progressive_read_fn(pp, &d, transform_info, progressive_row,
6111 /* Now feed data into the reader until we reach the end: */
6112 modifier_progressive_read(d.pm, pp, pi);
6116 /* modifier_read expects a png_modifier* */
6117 png_set_read_fn(pp, d.pm, modifier_read);
6119 /* Check the header values: */
6120 png_read_info(pp, pi);
6122 /* Process the 'info' requirements. Only one image is generated */
6123 transform_info_imp(&d, pp, pi);
6125 sequential_row(&d.this, pp, pi, -1, 0);
6128 transform_image_validate(&d, pp, pi);
6130 d.this.ps->validated = 1;
6133 modifier_reset(d.pm);
6138 modifier_reset(voidcast(png_modifier*,(void*)fault));
6142 /* The transforms: */
6143 #define ITSTRUCT(name) image_transform_##name
6144 #define ITDATA(name) image_transform_data_##name
6145 #define image_transform_ini image_transform_default_ini
6147 static image_transform ITSTRUCT(name) =\
6155 image_transform_ini,\
6156 image_transform_png_set_##name##_set,\
6157 image_transform_png_set_##name##_mod,\
6158 image_transform_png_set_##name##_add\
6160 #define PT ITSTRUCT(end) /* stores the previous transform */
6164 image_transform_default_ini(PNG_CONST image_transform *this,
6165 transform_display *that)
6167 this->next->ini(this->next, that);
6170 #ifdef PNG_READ_BACKGROUND_SUPPORTED
6172 image_transform_default_add(image_transform *this,
6173 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6185 #ifdef PNG_READ_EXPAND_SUPPORTED
6186 /* png_set_palette_to_rgb */
6188 image_transform_png_set_palette_to_rgb_set(PNG_CONST image_transform *this,
6189 transform_display *that, png_structp pp, png_infop pi)
6191 png_set_palette_to_rgb(pp);
6192 this->next->set(this->next, that, pp, pi);
6196 image_transform_png_set_palette_to_rgb_mod(PNG_CONST image_transform *this,
6197 image_pixel *that, png_const_structp pp,
6198 PNG_CONST transform_display *display)
6200 if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6201 image_pixel_convert_PLTE(that);
6203 this->next->mod(this->next, that, pp, display);
6207 image_transform_png_set_palette_to_rgb_add(image_transform *this,
6208 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6215 return colour_type == PNG_COLOR_TYPE_PALETTE;
6220 #define PT ITSTRUCT(palette_to_rgb)
6221 #endif /* PNG_READ_EXPAND_SUPPORTED */
6223 #ifdef PNG_READ_EXPAND_SUPPORTED
6224 /* png_set_tRNS_to_alpha */
6226 image_transform_png_set_tRNS_to_alpha_set(PNG_CONST image_transform *this,
6227 transform_display *that, png_structp pp, png_infop pi)
6229 png_set_tRNS_to_alpha(pp);
6230 this->next->set(this->next, that, pp, pi);
6234 image_transform_png_set_tRNS_to_alpha_mod(PNG_CONST image_transform *this,
6235 image_pixel *that, png_const_structp pp,
6236 PNG_CONST transform_display *display)
6238 /* LIBPNG BUG: this always forces palette images to RGB. */
6239 if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6240 image_pixel_convert_PLTE(that);
6242 /* This effectively does an 'expand' only if there is some transparency to
6243 * convert to an alpha channel.
6245 if (that->have_tRNS)
6246 image_pixel_add_alpha(that, &display->this);
6248 /* LIBPNG BUG: otherwise libpng still expands to 8 bits! */
6251 if (that->bit_depth < 8)
6253 if (that->sample_depth < 8)
6254 that->sample_depth = 8;
6257 this->next->mod(this->next, that, pp, display);
6261 image_transform_png_set_tRNS_to_alpha_add(image_transform *this,
6262 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6269 /* We don't know yet whether there will be a tRNS chunk, but we know that
6270 * this transformation should do nothing if there already is an alpha
6273 return (colour_type & PNG_COLOR_MASK_ALPHA) == 0;
6278 #define PT ITSTRUCT(tRNS_to_alpha)
6279 #endif /* PNG_READ_EXPAND_SUPPORTED */
6281 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
6282 /* png_set_gray_to_rgb */
6284 image_transform_png_set_gray_to_rgb_set(PNG_CONST image_transform *this,
6285 transform_display *that, png_structp pp, png_infop pi)
6287 png_set_gray_to_rgb(pp);
6288 this->next->set(this->next, that, pp, pi);
6292 image_transform_png_set_gray_to_rgb_mod(PNG_CONST image_transform *this,
6293 image_pixel *that, png_const_structp pp,
6294 PNG_CONST transform_display *display)
6296 /* NOTE: we can actually pend the tRNS processing at this point because we
6297 * can correctly recognize the original pixel value even though we have
6298 * mapped the one gray channel to the three RGB ones, but in fact libpng
6299 * doesn't do this, so we don't either.
6301 if ((that->colour_type & PNG_COLOR_MASK_COLOR) == 0 && that->have_tRNS)
6302 image_pixel_add_alpha(that, &display->this);
6304 /* Simply expand the bit depth and alter the colour type as required. */
6305 if (that->colour_type == PNG_COLOR_TYPE_GRAY)
6307 /* RGB images have a bit depth at least equal to '8' */
6308 if (that->bit_depth < 8)
6309 that->sample_depth = that->bit_depth = 8;
6311 /* And just changing the colour type works here because the green and blue
6312 * channels are being maintained in lock-step with the red/gray:
6314 that->colour_type = PNG_COLOR_TYPE_RGB;
6317 else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6318 that->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
6320 this->next->mod(this->next, that, pp, display);
6324 image_transform_png_set_gray_to_rgb_add(image_transform *this,
6325 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6332 return (colour_type & PNG_COLOR_MASK_COLOR) == 0;
6337 #define PT ITSTRUCT(gray_to_rgb)
6338 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
6340 #ifdef PNG_READ_EXPAND_SUPPORTED
6341 /* png_set_expand */
6343 image_transform_png_set_expand_set(PNG_CONST image_transform *this,
6344 transform_display *that, png_structp pp, png_infop pi)
6347 this->next->set(this->next, that, pp, pi);
6351 image_transform_png_set_expand_mod(PNG_CONST image_transform *this,
6352 image_pixel *that, png_const_structp pp,
6353 PNG_CONST transform_display *display)
6355 /* The general expand case depends on what the colour type is: */
6356 if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6357 image_pixel_convert_PLTE(that);
6358 else if (that->bit_depth < 8) /* grayscale */
6359 that->sample_depth = that->bit_depth = 8;
6361 if (that->have_tRNS)
6362 image_pixel_add_alpha(that, &display->this);
6364 this->next->mod(this->next, that, pp, display);
6368 image_transform_png_set_expand_add(image_transform *this,
6369 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6376 /* 'expand' should do nothing for RGBA or GA input - no tRNS and the bit
6377 * depth is at least 8 already.
6379 return (colour_type & PNG_COLOR_MASK_ALPHA) == 0;
6384 #define PT ITSTRUCT(expand)
6385 #endif /* PNG_READ_EXPAND_SUPPORTED */
6387 #ifdef PNG_READ_EXPAND_SUPPORTED
6388 /* png_set_expand_gray_1_2_4_to_8
6389 * LIBPNG BUG: this just does an 'expand'
6392 image_transform_png_set_expand_gray_1_2_4_to_8_set(
6393 PNG_CONST image_transform *this, transform_display *that, png_structp pp,
6396 png_set_expand_gray_1_2_4_to_8(pp);
6397 this->next->set(this->next, that, pp, pi);
6401 image_transform_png_set_expand_gray_1_2_4_to_8_mod(
6402 PNG_CONST image_transform *this, image_pixel *that, png_const_structp pp,
6403 PNG_CONST transform_display *display)
6405 image_transform_png_set_expand_mod(this, that, pp, display);
6409 image_transform_png_set_expand_gray_1_2_4_to_8_add(image_transform *this,
6410 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6412 return image_transform_png_set_expand_add(this, that, colour_type,
6416 IT(expand_gray_1_2_4_to_8);
6418 #define PT ITSTRUCT(expand_gray_1_2_4_to_8)
6419 #endif /* PNG_READ_EXPAND_SUPPORTED */
6421 #ifdef PNG_READ_EXPAND_16_SUPPORTED
6422 /* png_set_expand_16 */
6424 image_transform_png_set_expand_16_set(PNG_CONST image_transform *this,
6425 transform_display *that, png_structp pp, png_infop pi)
6427 png_set_expand_16(pp);
6428 this->next->set(this->next, that, pp, pi);
6432 image_transform_png_set_expand_16_mod(PNG_CONST image_transform *this,
6433 image_pixel *that, png_const_structp pp,
6434 PNG_CONST transform_display *display)
6436 /* Expect expand_16 to expand everything to 16 bits as a result of also
6437 * causing 'expand' to happen.
6439 if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6440 image_pixel_convert_PLTE(that);
6442 if (that->have_tRNS)
6443 image_pixel_add_alpha(that, &display->this);
6445 if (that->bit_depth < 16)
6446 that->sample_depth = that->bit_depth = 16;
6448 this->next->mod(this->next, that, pp, display);
6452 image_transform_png_set_expand_16_add(image_transform *this,
6453 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6460 /* expand_16 does something unless the bit depth is already 16. */
6461 return bit_depth < 16;
6466 #define PT ITSTRUCT(expand_16)
6467 #endif /* PNG_READ_EXPAND_16_SUPPORTED */
6469 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED /* API added in 1.5.4 */
6470 /* png_set_scale_16 */
6472 image_transform_png_set_scale_16_set(PNG_CONST image_transform *this,
6473 transform_display *that, png_structp pp, png_infop pi)
6475 png_set_scale_16(pp);
6476 this->next->set(this->next, that, pp, pi);
6480 image_transform_png_set_scale_16_mod(PNG_CONST image_transform *this,
6481 image_pixel *that, png_const_structp pp,
6482 PNG_CONST transform_display *display)
6484 if (that->bit_depth == 16)
6486 that->sample_depth = that->bit_depth = 8;
6487 if (that->red_sBIT > 8) that->red_sBIT = 8;
6488 if (that->green_sBIT > 8) that->green_sBIT = 8;
6489 if (that->blue_sBIT > 8) that->blue_sBIT = 8;
6490 if (that->alpha_sBIT > 8) that->alpha_sBIT = 8;
6493 this->next->mod(this->next, that, pp, display);
6497 image_transform_png_set_scale_16_add(image_transform *this,
6498 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6505 return bit_depth > 8;
6510 #define PT ITSTRUCT(scale_16)
6511 #endif /* PNG_READ_SCALE_16_TO_8_SUPPORTED (1.5.4 on) */
6513 #ifdef PNG_READ_16_TO_8_SUPPORTED /* the default before 1.5.4 */
6514 /* png_set_strip_16 */
6516 image_transform_png_set_strip_16_set(PNG_CONST image_transform *this,
6517 transform_display *that, png_structp pp, png_infop pi)
6519 png_set_strip_16(pp);
6520 this->next->set(this->next, that, pp, pi);
6524 image_transform_png_set_strip_16_mod(PNG_CONST image_transform *this,
6525 image_pixel *that, png_const_structp pp,
6526 PNG_CONST transform_display *display)
6528 if (that->bit_depth == 16)
6530 that->sample_depth = that->bit_depth = 8;
6531 if (that->red_sBIT > 8) that->red_sBIT = 8;
6532 if (that->green_sBIT > 8) that->green_sBIT = 8;
6533 if (that->blue_sBIT > 8) that->blue_sBIT = 8;
6534 if (that->alpha_sBIT > 8) that->alpha_sBIT = 8;
6536 /* Prior to 1.5.4 png_set_strip_16 would use an 'accurate' method if this
6537 * configuration option is set. From 1.5.4 the flag is never set and the
6538 * 'scale' API (above) must be used.
6540 # ifdef PNG_READ_ACCURATE_SCALE_SUPPORTED
6541 # if PNG_LIBPNG_VER >= 10504
6542 # error PNG_READ_ACCURATE_SCALE should not be set
6545 /* The strip 16 algorithm drops the low 8 bits rather than calculating
6546 * 1/257, so we need to adjust the permitted errors appropriately:
6547 * Notice that this is only relevant prior to the addition of the
6548 * png_set_scale_16 API in 1.5.4 (but 1.5.4+ always defines the above!)
6551 PNG_CONST double d = (255-128.5)/65535;
6560 this->next->mod(this->next, that, pp, display);
6564 image_transform_png_set_strip_16_add(image_transform *this,
6565 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6572 return bit_depth > 8;
6577 #define PT ITSTRUCT(strip_16)
6578 #endif /* PNG_READ_16_TO_8_SUPPORTED */
6580 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
6581 /* png_set_strip_alpha */
6583 image_transform_png_set_strip_alpha_set(PNG_CONST image_transform *this,
6584 transform_display *that, png_structp pp, png_infop pi)
6586 png_set_strip_alpha(pp);
6587 this->next->set(this->next, that, pp, pi);
6591 image_transform_png_set_strip_alpha_mod(PNG_CONST image_transform *this,
6592 image_pixel *that, png_const_structp pp,
6593 PNG_CONST transform_display *display)
6595 if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
6596 that->colour_type = PNG_COLOR_TYPE_GRAY;
6597 else if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
6598 that->colour_type = PNG_COLOR_TYPE_RGB;
6600 that->have_tRNS = 0;
6603 this->next->mod(this->next, that, pp, display);
6607 image_transform_png_set_strip_alpha_add(image_transform *this,
6608 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
6615 return (colour_type & PNG_COLOR_MASK_ALPHA) != 0;
6620 #define PT ITSTRUCT(strip_alpha)
6621 #endif /* PNG_READ_STRIP_ALPHA_SUPPORTED */
6623 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
6624 /* png_set_rgb_to_gray(png_structp, int err_action, double red, double green)
6625 * png_set_rgb_to_gray_fixed(png_structp, int err_action, png_fixed_point red,
6626 * png_fixed_point green)
6627 * png_get_rgb_to_gray_status
6629 * The 'default' test here uses values known to be used inside libpng:
6635 * These values are being retained for compatibility, along with the somewhat
6636 * broken truncation calculation in the fast-and-inaccurate code path. Older
6637 * versions of libpng will fail the accuracy tests below because they use the
6638 * truncation algorithm everywhere.
6640 #define data ITDATA(rgb_to_gray)
6643 double gamma; /* File gamma to use in processing */
6645 /* The following are the parameters for png_set_rgb_to_gray: */
6646 # ifdef PNG_FLOATING_POINT_SUPPORTED
6648 double green_to_set;
6650 png_fixed_point red_to_set;
6651 png_fixed_point green_to_set;
6654 /* The actual coefficients: */
6655 double red_coefficient;
6656 double green_coefficient;
6657 double blue_coefficient;
6659 /* Set if the coeefficients have been overridden. */
6660 int coefficients_overridden;
6663 #undef image_transform_ini
6664 #define image_transform_ini image_transform_png_set_rgb_to_gray_ini
6666 image_transform_png_set_rgb_to_gray_ini(PNG_CONST image_transform *this,
6667 transform_display *that)
6669 png_modifier *pm = that->pm;
6670 PNG_CONST color_encoding *e = pm->current_encoding;
6674 /* Since we check the encoding this flag must be set: */
6675 pm->test_uses_encoding = 1;
6677 /* If 'e' is not NULL chromaticity information is present and either a cHRM
6678 * or an sRGB chunk will be inserted.
6682 /* Coefficients come from the encoding, but may need to be normalized to a
6683 * white point Y of 1.0
6685 PNG_CONST double whiteY = e->red.Y + e->green.Y + e->blue.Y;
6687 data.red_coefficient = e->red.Y;
6688 data.green_coefficient = e->green.Y;
6689 data.blue_coefficient = e->blue.Y;
6693 data.red_coefficient /= whiteY;
6694 data.green_coefficient /= whiteY;
6695 data.blue_coefficient /= whiteY;
6701 /* The default (built in) coeffcients, as above: */
6702 data.red_coefficient = 6968 / 32768.;
6703 data.green_coefficient = 23434 / 32768.;
6704 data.blue_coefficient = 2366 / 32768.;
6707 data.gamma = pm->current_gamma;
6709 /* If not set then the calculations assume linear encoding (implicitly): */
6710 if (data.gamma == 0)
6713 /* The arguments to png_set_rgb_to_gray can override the coefficients implied
6714 * by the color space encoding. If doing exhaustive checks do the override
6715 * in each case, otherwise do it randomly.
6717 if (pm->test_exhaustive)
6719 /* First time in coefficients_overridden is 0, the following sets it to 1,
6720 * so repeat if it is set. If a test fails this may mean we subsequently
6721 * skip a non-override test, ignore that.
6723 data.coefficients_overridden = !data.coefficients_overridden;
6724 pm->repeat = data.coefficients_overridden != 0;
6728 data.coefficients_overridden = random_choice();
6730 if (data.coefficients_overridden)
6732 /* These values override the color encoding defaults, simply use random
6739 data.green_coefficient = total = (ru & 0xffff) / 65535.;
6741 data.red_coefficient = (1 - total) * (ru & 0xffff) / 65535.;
6742 total += data.red_coefficient;
6743 data.blue_coefficient = 1 - total;
6745 # ifdef PNG_FLOATING_POINT_SUPPORTED
6746 data.red_to_set = data.red_coefficient;
6747 data.green_to_set = data.green_coefficient;
6749 data.red_to_set = fix(data.red_coefficient);
6750 data.green_to_set = fix(data.green_coefficient);
6753 /* The following just changes the error messages: */
6754 pm->encoding_ignored = 1;
6759 data.red_to_set = -1;
6760 data.green_to_set = -1;
6763 /* Adjust the error limit in the png_modifier because of the larger errors
6764 * produced in the digitization during the gamma handling.
6766 if (data.gamma != 1) /* Use gamma tables */
6768 if (that->this.bit_depth == 16 || pm->assume_16_bit_calculations)
6770 /* The computations have the form:
6772 * r * rc + g * gc + b * bc
6774 * Each component of which is +/-1/65535 from the gamma_to_1 table
6775 * lookup, resulting in a base error of +/-6. The gamma_from_1
6776 * conversion adds another +/-2 in the 16-bit case and
6777 * +/-(1<<(15-PNG_MAX_GAMMA_8)) in the 8-bit case.
6780 # if PNG_MAX_GAMMA_8 < 14
6781 pow((that->this.bit_depth == 16 ?
6782 8. : 6. + (1<<(15-PNG_MAX_GAMMA_8)))/65535, data.gamma);
6784 pow((that->this.bit_depth == 16 ?
6785 8. : 8. + (1<<(15-PNG_MAX_GAMMA_8)))/65535, data.gamma);
6791 /* Rounding to 8 bits in the linear space causes massive errors which
6792 * will trigger the error check in transform_range_check. Fix that
6793 * here by taking the gamma encoding into account.
6795 * When DIGITIZE is set because a pre-1.7 version of libpng is being
6796 * tested allow a bigger slack.
6798 * NOTE: this magic number was determined by experiment to be 1.1 (when
6799 * using fixed point arithmetic). There's no great merit to the value
6800 * below, however it only affects the limit used for checking for
6801 * internal calculation errors, not the actual limit imposed by
6802 * pngvalid on the output errors.
6806 pow(1.1 /255, data.gamma);
6808 pow(1.0 /255, data.gamma);
6815 /* With no gamma correction a large error comes from the truncation of the
6816 * calculation in the 8 bit case, allow for that here.
6818 if (that->this.bit_depth != 16 && !pm->assume_16_bit_calculations)
6819 that->pm->limit += 4E-3;
6824 image_transform_png_set_rgb_to_gray_set(PNG_CONST image_transform *this,
6825 transform_display *that, png_structp pp, png_infop pi)
6827 PNG_CONST int error_action = 1; /* no error, no defines in png.h */
6829 # ifdef PNG_FLOATING_POINT_SUPPORTED
6830 png_set_rgb_to_gray(pp, error_action, data.red_to_set, data.green_to_set);
6832 png_set_rgb_to_gray_fixed(pp, error_action, data.red_to_set,
6836 # ifdef PNG_READ_cHRM_SUPPORTED
6837 if (that->pm->current_encoding != 0)
6839 /* We have an encoding so a cHRM chunk may have been set; if so then
6840 * check that the libpng APIs give the correct (X,Y,Z) values within
6841 * some margin of error for the round trip through the chromaticity
6844 # ifdef PNG_FLOATING_POINT_SUPPORTED
6845 # define API_function png_get_cHRM_XYZ
6846 # define API_form "FP"
6847 # define API_type double
6848 # define API_cvt(x) (x)
6850 # define API_function png_get_cHRM_XYZ_fixed
6851 # define API_form "fixed"
6852 # define API_type png_fixed_point
6853 # define API_cvt(x) ((double)(x)/PNG_FP_1)
6856 API_type rX, gX, bX;
6857 API_type rY, gY, bY;
6858 API_type rZ, gZ, bZ;
6860 if ((API_function(pp, pi, &rX, &rY, &rZ, &gX, &gY, &gZ, &bX, &bY, &bZ)
6861 & PNG_INFO_cHRM) != 0)
6865 color_encoding e, o;
6867 /* Expect libpng to return a normalized result, but the original
6868 * color space encoding may not be normalized.
6870 modifier_current_encoding(that->pm, &o);
6871 normalize_color_encoding(&o);
6873 /* Sanity check the pngvalid code - the coefficients should match
6874 * the normalized Y values of the encoding unless they were
6877 if (data.red_to_set == -1 && data.green_to_set == -1 &&
6878 (fabs(o.red.Y - data.red_coefficient) > DBL_EPSILON ||
6879 fabs(o.green.Y - data.green_coefficient) > DBL_EPSILON ||
6880 fabs(o.blue.Y - data.blue_coefficient) > DBL_EPSILON))
6881 png_error(pp, "internal pngvalid cHRM coefficient error");
6883 /* Generate a colour space encoding. */
6884 e.gamma = o.gamma; /* not used */
6885 e.red.X = API_cvt(rX);
6886 e.red.Y = API_cvt(rY);
6887 e.red.Z = API_cvt(rZ);
6888 e.green.X = API_cvt(gX);
6889 e.green.Y = API_cvt(gY);
6890 e.green.Z = API_cvt(gZ);
6891 e.blue.X = API_cvt(bX);
6892 e.blue.Y = API_cvt(bY);
6893 e.blue.Z = API_cvt(bZ);
6895 /* This should match the original one from the png_modifier, within
6896 * the range permitted by the libpng fixed point representation.
6899 el = "-"; /* Set to element name with error */
6901 # define CHECK(col,x)\
6903 double err = fabs(o.col.x - e.col.x);\
6907 el = #col "(" #x ")";\
6921 /* Here in both fixed and floating cases to check the values read
6922 * from the cHRm chunk. PNG uses fixed point in the cHRM chunk, so
6923 * we can't expect better than +/-.5E-5 on the result, allow 1E-5.
6930 pos = safecat(buffer, sizeof buffer, pos, API_form);
6931 pos = safecat(buffer, sizeof buffer, pos, " cHRM ");
6932 pos = safecat(buffer, sizeof buffer, pos, el);
6933 pos = safecat(buffer, sizeof buffer, pos, " error: ");
6934 pos = safecatd(buffer, sizeof buffer, pos, maxe, 7);
6935 pos = safecat(buffer, sizeof buffer, pos, " ");
6936 /* Print the color space without the gamma value: */
6937 pos = safecat_color_encoding(buffer, sizeof buffer, pos, &o, 0);
6938 pos = safecat(buffer, sizeof buffer, pos, " -> ");
6939 pos = safecat_color_encoding(buffer, sizeof buffer, pos, &e, 0);
6941 png_error(pp, buffer);
6945 # endif /* READ_cHRM */
6947 this->next->set(this->next, that, pp, pi);
6951 image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
6952 image_pixel *that, png_const_structp pp,
6953 PNG_CONST transform_display *display)
6955 if ((that->colour_type & PNG_COLOR_MASK_COLOR) != 0)
6959 if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
6960 image_pixel_convert_PLTE(that);
6962 /* Image now has RGB channels... */
6965 PNG_CONST png_modifier *pm = display->pm;
6966 const unsigned int sample_depth = that->sample_depth;
6967 const unsigned int calc_depth = (pm->assume_16_bit_calculations ? 16 :
6969 const unsigned int gamma_depth = (sample_depth == 16 ? 16 :
6970 (pm->assume_16_bit_calculations ? PNG_MAX_GAMMA_8 : sample_depth));
6973 double rlo, rhi, glo, ghi, blo, bhi, graylo, grayhi;
6975 /* Do this using interval arithmetic, otherwise it is too difficult to
6976 * handle the errors correctly.
6978 * To handle the gamma correction work out the upper and lower bounds
6979 * of the digitized value. Assume rounding here - normally the values
6980 * will be identical after this operation if there is only one
6981 * transform, feel free to delete the png_error checks on this below in
6982 * the future (this is just me trying to ensure it works!)
6984 r = rlo = rhi = that->redf;
6986 rlo = digitize(rlo, calc_depth, 1/*round*/);
6988 rhi = digitize(rhi, calc_depth, 1/*round*/);
6990 g = glo = ghi = that->greenf;
6991 glo -= that->greene;
6992 glo = digitize(glo, calc_depth, 1/*round*/);
6993 ghi += that->greene;
6994 ghi = digitize(ghi, calc_depth, 1/*round*/);
6996 b = blo = bhi = that->bluef;
6998 blo = digitize(blo, calc_depth, 1/*round*/);
6999 bhi += that->greene;
7000 bhi = digitize(bhi, calc_depth, 1/*round*/);
7002 isgray = r==g && g==b;
7004 if (data.gamma != 1)
7006 PNG_CONST double power = 1/data.gamma;
7007 PNG_CONST double abse = calc_depth == 16 ? .5/65535 : .5/255;
7009 /* 'abse' is the absolute error permitted in linear calculations. It
7010 * is used here to capture the error permitted in the handling
7011 * (undoing) of the gamma encoding. Once again digitization occurs
7012 * to handle the upper and lower bounds of the values. This is
7013 * where the real errors are introduced.
7016 rlo = digitize(pow(rlo, power)-abse, calc_depth, 1);
7017 rhi = digitize(pow(rhi, power)+abse, calc_depth, 1);
7020 glo = digitize(pow(glo, power)-abse, calc_depth, 1);
7021 ghi = digitize(pow(ghi, power)+abse, calc_depth, 1);
7024 blo = digitize(pow(blo, power)-abse, calc_depth, 1);
7025 bhi = digitize(pow(bhi, power)+abse, calc_depth, 1);
7028 /* Now calculate the actual gray values. Although the error in the
7029 * coefficients depends on whether they were specified on the command
7030 * line (in which case truncation to 15 bits happened) or not (rounding
7031 * was used) the maxium error in an individual coefficient is always
7032 * 1/32768, because even in the rounding case the requirement that
7033 * coefficients add up to 32768 can cause a larger rounding error.
7035 * The only time when rounding doesn't occur in 1.5.5 and later is when
7036 * the non-gamma code path is used for less than 16 bit data.
7038 gray = r * data.red_coefficient + g * data.green_coefficient +
7039 b * data.blue_coefficient;
7042 PNG_CONST int do_round = data.gamma != 1 || calc_depth == 16;
7043 PNG_CONST double ce = 1. / 32768;
7045 graylo = digitize(rlo * (data.red_coefficient-ce) +
7046 glo * (data.green_coefficient-ce) +
7047 blo * (data.blue_coefficient-ce), gamma_depth, do_round);
7051 grayhi = digitize(rhi * (data.red_coefficient+ce) +
7052 ghi * (data.green_coefficient+ce) +
7053 bhi * (data.blue_coefficient+ce), gamma_depth, do_round);
7058 /* And invert the gamma. */
7059 if (data.gamma != 1)
7061 PNG_CONST double power = data.gamma;
7063 gray = pow(gray, power);
7064 graylo = digitize(pow(graylo, power), sample_depth, 1);
7065 grayhi = digitize(pow(grayhi, power), sample_depth, 1);
7068 /* Now the error can be calculated.
7070 * If r==g==b because there is no overall gamma correction libpng
7071 * currently preserves the original value.
7074 err = (that->rede + that->greene + that->bluee)/3;
7078 err = fabs(grayhi-gray);
7079 if (fabs(gray - graylo) > err)
7080 err = fabs(graylo-gray);
7082 /* Check that this worked: */
7083 if (err > pm->limit)
7088 pos = safecat(buffer, sizeof buffer, pos, "rgb_to_gray error ");
7089 pos = safecatd(buffer, sizeof buffer, pos, err, 6);
7090 pos = safecat(buffer, sizeof buffer, pos, " exceeds limit ");
7091 pos = safecatd(buffer, sizeof buffer, pos, pm->limit, 6);
7092 png_error(pp, buffer);
7096 # else /* DIGITIZE */
7098 double r = that->redf;
7099 double re = that->rede;
7100 double g = that->greenf;
7101 double ge = that->greene;
7102 double b = that->bluef;
7103 double be = that->bluee;
7105 /* The true gray case involves no math. */
7106 if (r == g && r == b)
7110 if (err < ge) err = ge;
7111 if (err < be) err = be;
7114 else if (data.gamma == 1)
7116 /* There is no need to do the conversions to and from linear space,
7117 * so the calculation should be a lot more accurate. There is a
7118 * built in 1/32768 error in the coefficients because they only have
7119 * 15 bits and are adjusted to make sure they add up to 32768, so
7120 * the result may have an additional error up to 1/32768. (Note
7121 * that adding the 1/32768 here avoids needing to increase the
7122 * global error limits to take this into account.)
7124 gray = r * data.red_coefficient + g * data.green_coefficient +
7125 b * data.blue_coefficient;
7126 err = re * data.red_coefficient + ge * data.green_coefficient +
7127 be * data.blue_coefficient + 1./32768 + gray * 5 * DBL_EPSILON;
7132 /* The calculation happens in linear space, and this produces much
7133 * wider errors in the encoded space. These are handled here by
7134 * factoring the errors in to the calculation. There are two table
7135 * lookups in the calculation and each introduces a quantization
7136 * error defined by the table size.
7138 PNG_CONST png_modifier *pm = display->pm;
7139 double in_qe = (that->sample_depth > 8 ? .5/65535 : .5/255);
7140 double out_qe = (that->sample_depth > 8 ? .5/65535 :
7141 (pm->assume_16_bit_calculations ? .5/(1<<PNG_MAX_GAMMA_8) :
7143 double rhi, ghi, bhi, grayhi;
7144 double g1 = 1/data.gamma;
7146 rhi = r + re + in_qe; if (rhi > 1) rhi = 1;
7147 r -= re + in_qe; if (r < 0) r = 0;
7148 ghi = g + ge + in_qe; if (ghi > 1) ghi = 1;
7149 g -= ge + in_qe; if (g < 0) g = 0;
7150 bhi = b + be + in_qe; if (bhi > 1) bhi = 1;
7151 b -= be + in_qe; if (b < 0) b = 0;
7153 r = pow(r, g1)*(1-DBL_EPSILON); rhi = pow(rhi, g1)*(1+DBL_EPSILON);
7154 g = pow(g, g1)*(1-DBL_EPSILON); ghi = pow(ghi, g1)*(1+DBL_EPSILON);
7155 b = pow(b, g1)*(1-DBL_EPSILON); bhi = pow(bhi, g1)*(1+DBL_EPSILON);
7157 /* Work out the lower and upper bounds for the gray value in the
7158 * encoded space, then work out an average and error. Remove the
7159 * previously added input quantization error at this point.
7161 gray = r * data.red_coefficient + g * data.green_coefficient +
7162 b * data.blue_coefficient - 1./32768 - out_qe;
7167 gray *= (1 - 6 * DBL_EPSILON);
7168 gray = pow(gray, data.gamma) * (1-DBL_EPSILON);
7171 grayhi = rhi * data.red_coefficient + ghi * data.green_coefficient +
7172 bhi * data.blue_coefficient + 1./32768 + out_qe;
7173 grayhi *= (1 + 6 * DBL_EPSILON);
7177 grayhi = pow(grayhi, data.gamma) * (1+DBL_EPSILON);
7179 err = (grayhi - gray) / 2;
7180 gray = (grayhi + gray) / 2;
7183 err = gray * DBL_EPSILON;
7188 /* Validate that the error is within limits (this has caused
7189 * problems before, it's much easier to detect them here.)
7191 if (err > pm->limit)
7196 pos = safecat(buffer, sizeof buffer, pos, "rgb_to_gray error ");
7197 pos = safecatd(buffer, sizeof buffer, pos, err, 6);
7198 pos = safecat(buffer, sizeof buffer, pos, " exceeds limit ");
7199 pos = safecatd(buffer, sizeof buffer, pos, pm->limit, 6);
7200 png_error(pp, buffer);
7204 # endif /* !DIGITIZE */
7206 that->bluef = that->greenf = that->redf = gray;
7207 that->bluee = that->greene = that->rede = err;
7209 /* The sBIT is the minium of the three colour channel sBITs. */
7210 if (that->red_sBIT > that->green_sBIT)
7211 that->red_sBIT = that->green_sBIT;
7212 if (that->red_sBIT > that->blue_sBIT)
7213 that->red_sBIT = that->blue_sBIT;
7214 that->blue_sBIT = that->green_sBIT = that->red_sBIT;
7216 /* And remove the colour bit in the type: */
7217 if (that->colour_type == PNG_COLOR_TYPE_RGB)
7218 that->colour_type = PNG_COLOR_TYPE_GRAY;
7219 else if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
7220 that->colour_type = PNG_COLOR_TYPE_GRAY_ALPHA;
7223 this->next->mod(this->next, that, pp, display);
7227 image_transform_png_set_rgb_to_gray_add(image_transform *this,
7228 PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
7235 return (colour_type & PNG_COLOR_MASK_COLOR) != 0;
7241 #define PT ITSTRUCT(rgb_to_gray)
7242 #undef image_transform_ini
7243 #define image_transform_ini image_transform_default_ini
7244 #endif /* PNG_READ_RGB_TO_GRAY_SUPPORTED */
7246 #ifdef PNG_READ_BACKGROUND_SUPPORTED
7247 /* png_set_background(png_structp, png_const_color_16p background_color,
7248 * int background_gamma_code, int need_expand, double background_gamma)
7249 * png_set_background_fixed(png_structp, png_const_color_16p background_color,
7250 * int background_gamma_code, int need_expand,
7251 * png_fixed_point background_gamma)
7253 * This ignores the gamma (at present.)
7255 #define data ITDATA(background)
7256 static image_pixel data;
7259 image_transform_png_set_background_set(PNG_CONST image_transform *this,
7260 transform_display *that, png_structp pp, png_infop pi)
7262 png_byte colour_type, bit_depth;
7263 png_byte random_bytes[8]; /* 8 bytes - 64 bits - the biggest pixel */
7267 /* We need a background colour, because we don't know exactly what transforms
7268 * have been set we have to supply the colour in the original file format and
7269 * so we need to know what that is! The background colour is stored in the
7270 * transform_display.
7272 RANDOMIZE(random_bytes);
7274 /* Read the random value, for colour type 3 the background colour is actually
7275 * expressed as a 24bit rgb, not an index.
7277 colour_type = that->this.colour_type;
7278 if (colour_type == 3)
7280 colour_type = PNG_COLOR_TYPE_RGB;
7282 expand = 0; /* passing in an RGB not a pixel index */
7287 bit_depth = that->this.bit_depth;
7291 image_pixel_init(&data, random_bytes, colour_type,
7292 bit_depth, 0/*x*/, 0/*unused: palette*/);
7294 /* Extract the background colour from this image_pixel, but make sure the
7295 * unused fields of 'back' are garbage.
7299 if (colour_type & PNG_COLOR_MASK_COLOR)
7301 back.red = (png_uint_16)data.red;
7302 back.green = (png_uint_16)data.green;
7303 back.blue = (png_uint_16)data.blue;
7307 back.gray = (png_uint_16)data.red;
7309 # ifdef PNG_FLOATING_POINT_SUPPORTED
7310 png_set_background(pp, &back, PNG_BACKGROUND_GAMMA_FILE, expand, 0);
7312 png_set_background_fixed(pp, &back, PNG_BACKGROUND_GAMMA_FILE, expand, 0);
7315 this->next->set(this->next, that, pp, pi);
7319 image_transform_png_set_background_mod(PNG_CONST image_transform *this,
7320 image_pixel *that, png_const_structp pp,
7321 PNG_CONST transform_display *display)
7323 /* Check for tRNS first: */
7324 if (that->have_tRNS && that->colour_type != PNG_COLOR_TYPE_PALETTE)
7325 image_pixel_add_alpha(that, &display->this);
7327 /* This is only necessary if the alpha value is less than 1. */
7328 if (that->alphaf < 1)
7330 /* Now we do the background calculation without any gamma correction. */
7331 if (that->alphaf <= 0)
7333 that->redf = data.redf;
7334 that->greenf = data.greenf;
7335 that->bluef = data.bluef;
7337 that->rede = data.rede;
7338 that->greene = data.greene;
7339 that->bluee = data.bluee;
7341 that->red_sBIT= data.red_sBIT;
7342 that->green_sBIT= data.green_sBIT;
7343 that->blue_sBIT= data.blue_sBIT;
7346 else /* 0 < alpha < 1 */
7348 double alf = 1 - that->alphaf;
7350 that->redf = that->redf * that->alphaf + data.redf * alf;
7351 that->rede = that->rede * that->alphaf + data.rede * alf +
7353 that->greenf = that->greenf * that->alphaf + data.greenf * alf;
7354 that->greene = that->greene * that->alphaf + data.greene * alf +
7356 that->bluef = that->bluef * that->alphaf + data.bluef * alf;
7357 that->bluee = that->bluee * that->alphaf + data.bluee * alf +
7361 /* Remove the alpha type and set the alpha (not in that order.) */
7365 if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
7366 that->colour_type = PNG_COLOR_TYPE_RGB;
7367 else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7368 that->colour_type = PNG_COLOR_TYPE_GRAY;
7369 /* PNG_COLOR_TYPE_PALETTE is not changed */
7372 this->next->mod(this->next, that, pp, display);
7375 #define image_transform_png_set_background_add image_transform_default_add
7380 #define PT ITSTRUCT(background)
7381 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
7383 /* This may just be 'end' if all the transforms are disabled! */
7384 static image_transform *PNG_CONST image_transform_first = &PT;
7387 transform_enable(PNG_CONST char *name)
7389 /* Everything starts out enabled, so if we see an 'enable' disabled
7390 * everything else the first time round.
7392 static int all_disabled = 0;
7394 image_transform *list = image_transform_first;
7396 while (list != &image_transform_end)
7398 if (strcmp(list->name, name) == 0)
7403 else if (!all_disabled)
7413 fprintf(stderr, "pngvalid: --transform-enable=%s: unknown transform\n",
7420 transform_disable(PNG_CONST char *name)
7422 image_transform *list = image_transform_first;
7424 while (list != &image_transform_end)
7426 if (strcmp(list->name, name) == 0)
7435 fprintf(stderr, "pngvalid: --transform-disable=%s: unknown transform\n",
7441 image_transform_reset_count(void)
7443 image_transform *next = image_transform_first;
7446 while (next != &image_transform_end)
7448 next->local_use = 0;
7454 /* This can only happen if we every have more than 32 transforms (excluding
7455 * the end) in the list.
7457 if (count > 32) abort();
7461 image_transform_test_counter(png_uint_32 counter, unsigned int max)
7463 /* Test the list to see if there is any point contining, given a current
7464 * counter and a 'max' value.
7466 image_transform *next = image_transform_first;
7468 while (next != &image_transform_end)
7470 /* For max 0 or 1 continue until the counter overflows: */
7473 /* Continue if any entry hasn't reacked the max. */
7474 if (max > 1 && next->local_use < max)
7479 return max <= 1 && counter == 0;
7483 image_transform_add(PNG_CONST image_transform **this, unsigned int max,
7484 png_uint_32 counter, char *name, size_t sizeof_name, size_t *pos,
7485 png_byte colour_type, png_byte bit_depth)
7487 for (;;) /* until we manage to add something */
7490 image_transform *list;
7492 /* Find the next counter value, if the counter is zero this is the start
7493 * of the list. This routine always returns the current counter (not the
7494 * next) so it returns 0 at the end and expects 0 at the beginning.
7496 if (counter == 0) /* first time */
7498 image_transform_reset_count();
7502 counter = random_32();
7504 else /* advance the counter */
7508 case 0: ++counter; break;
7509 case 1: counter <<= 1; break;
7510 default: counter = random_32(); break;
7514 /* Now add all these items, if possible */
7515 *this = &image_transform_end;
7516 list = image_transform_first;
7519 /* Go through the whole list adding anything that the counter selects: */
7520 while (list != &image_transform_end)
7522 if ((counter & mask) != 0 && list->enable &&
7523 (max == 0 || list->local_use < max))
7525 /* Candidate to add: */
7526 if (list->add(list, this, colour_type, bit_depth) || max == 0)
7528 /* Added, so add to the name too. */
7529 *pos = safecat(name, sizeof_name, *pos, " +");
7530 *pos = safecat(name, sizeof_name, *pos, list->name);
7535 /* Not useful and max>0, so remove it from *this: */
7539 /* And, since we know it isn't useful, stop it being added again
7542 list->local_use = max;
7550 /* Now if anything was added we have something to do. */
7551 if (*this != &image_transform_end)
7554 /* Nothing added, but was there anything in there to add? */
7555 if (!image_transform_test_counter(counter, max))
7560 #ifdef THIS_IS_THE_PROFORMA
7562 image_transform_png_set_@_set(PNG_CONST image_transform *this,
7563 transform_display *that, png_structp pp, png_infop pi)
7566 this->next->set(this->next, that, pp, pi);
7570 image_transform_png_set_@_mod(PNG_CONST image_transform *this,
7571 image_pixel *that, png_const_structp pp,
7572 PNG_CONST transform_display *display)
7574 this->next->mod(this->next, that, pp, display);
7578 image_transform_png_set_@_add(image_transform *this,
7579 PNG_CONST image_transform **that, char *name, size_t sizeof_name,
7580 size_t *pos, png_byte colour_type, png_byte bit_depth)
7585 *pos = safecat(name, sizeof_name, *pos, " +@");
7593 /* png_set_quantize(png_structp, png_colorp palette, int num_palette,
7594 * int maximum_colors, png_const_uint_16p histogram, int full_quantize)
7596 * Very difficult to validate this!
7600 /* The data layout transforms are handled by swapping our own channel data,
7601 * necessarily these need to happen at the end of the transform list because the
7602 * semantic of the channels changes after these are executed. Some of these,
7603 * like set_shift and set_packing, can't be done at present because they change
7604 * the layout of the data at the sub-sample level so sample() won't get the
7607 /* png_set_invert_alpha */
7613 /* png_set_swap_alpha */
7619 /* png_set_filler, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7622 /* png_set_add_alpha, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7625 /* png_set_packing */
7628 /* png_set_packswap */
7631 /* png_set_invert_mono */
7634 /* png_set_shift(png_structp, png_const_color_8p true_bits) */
7638 perform_transform_test(png_modifier *pm)
7640 png_byte colour_type = 0;
7641 png_byte bit_depth = 0;
7642 unsigned int palette_number = 0;
7644 while (next_format(&colour_type, &bit_depth, &palette_number, 0))
7646 png_uint_32 counter = 0;
7650 base_pos = safecat(name, sizeof name, 0, "transform:");
7654 size_t pos = base_pos;
7655 PNG_CONST image_transform *list = 0;
7657 /* 'max' is currently hardwired to '1'; this should be settable on the
7660 counter = image_transform_add(&list, 1/*max*/, counter,
7661 name, sizeof name, &pos, colour_type, bit_depth);
7666 /* The command line can change this to checking interlaced images. */
7670 transform_test(pm, FILEID(colour_type, bit_depth, palette_number,
7671 pm->interlace_type, 0, 0, 0), list, name);
7680 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
7682 /********************************* GAMMA TESTS ********************************/
7683 #ifdef PNG_READ_GAMMA_SUPPORTED
7684 /* Reader callbacks and implementations, where they differ from the standard
7687 typedef struct gamma_display
7689 standard_display this;
7694 double screen_gamma;
7695 double background_gamma;
7698 int use_input_precision;
7702 png_color_16 background_color;
7704 /* Local variables */
7710 #define ALPHA_MODE_OFFSET 4
7713 gamma_display_init(gamma_display *dp, png_modifier *pm, png_uint_32 id,
7714 double file_gamma, double screen_gamma, png_byte sbit, int threshold_test,
7715 int use_input_precision, int scale16, int expand16,
7716 int do_background, PNG_CONST png_color_16 *pointer_to_the_background_color,
7717 double background_gamma)
7719 /* Standard fields */
7720 standard_display_init(&dp->this, &pm->this, id, 0/*do_interlace*/,
7721 pm->use_update_info);
7723 /* Parameter fields */
7725 dp->file_gamma = file_gamma;
7726 dp->screen_gamma = screen_gamma;
7727 dp->background_gamma = background_gamma;
7729 dp->threshold_test = threshold_test;
7730 dp->use_input_precision = use_input_precision;
7731 dp->scale16 = scale16;
7732 dp->expand16 = expand16;
7733 dp->do_background = do_background;
7734 if (do_background && pointer_to_the_background_color != 0)
7735 dp->background_color = *pointer_to_the_background_color;
7737 memset(&dp->background_color, 0, sizeof dp->background_color);
7739 /* Local variable fields */
7740 dp->maxerrout = dp->maxerrpc = dp->maxerrabs = 0;
7744 gamma_info_imp(gamma_display *dp, png_structp pp, png_infop pi)
7746 /* Reuse the standard stuff as appropriate. */
7747 standard_info_part1(&dp->this, pp, pi);
7749 /* If requested strip 16 to 8 bits - this is handled automagically below
7750 * because the output bit depth is read from the library. Note that there
7751 * are interactions with sBIT but, internally, libpng makes sbit at most
7752 * PNG_MAX_GAMMA_8 when doing the following.
7755 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
7756 png_set_scale_16(pp);
7758 /* The following works both in 1.5.4 and earlier versions: */
7759 # ifdef PNG_READ_16_TO_8_SUPPORTED
7760 png_set_strip_16(pp);
7762 png_error(pp, "scale16 (16 to 8 bit conversion) not supported");
7767 # ifdef PNG_READ_EXPAND_16_SUPPORTED
7768 png_set_expand_16(pp);
7770 png_error(pp, "expand16 (8 to 16 bit conversion) not supported");
7773 if (dp->do_background >= ALPHA_MODE_OFFSET)
7775 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
7777 /* This tests the alpha mode handling, if supported. */
7778 int mode = dp->do_background - ALPHA_MODE_OFFSET;
7780 /* The gamma value is the output gamma, and is in the standard,
7781 * non-inverted, represenation. It provides a default for the PNG file
7782 * gamma, but since the file has a gAMA chunk this does not matter.
7784 PNG_CONST double sg = dp->screen_gamma;
7785 # ifndef PNG_FLOATING_POINT_SUPPORTED
7786 PNG_CONST png_fixed_point g = fix(sg);
7789 # ifdef PNG_FLOATING_POINT_SUPPORTED
7790 png_set_alpha_mode(pp, mode, sg);
7792 png_set_alpha_mode_fixed(pp, mode, g);
7795 /* However, for the standard Porter-Duff algorithm the output defaults
7796 * to be linear, so if the test requires non-linear output it must be
7799 if (mode == PNG_ALPHA_STANDARD && sg != 1)
7801 # ifdef PNG_FLOATING_POINT_SUPPORTED
7802 png_set_gamma(pp, sg, dp->file_gamma);
7804 png_fixed_point f = fix(dp->file_gamma);
7805 png_set_gamma_fixed(pp, g, f);
7810 png_error(pp, "alpha mode handling not supported");
7816 /* Set up gamma processing. */
7817 # ifdef PNG_FLOATING_POINT_SUPPORTED
7818 png_set_gamma(pp, dp->screen_gamma, dp->file_gamma);
7821 png_fixed_point s = fix(dp->screen_gamma);
7822 png_fixed_point f = fix(dp->file_gamma);
7823 png_set_gamma_fixed(pp, s, f);
7827 if (dp->do_background)
7829 # ifdef PNG_READ_BACKGROUND_SUPPORTED
7830 /* NOTE: this assumes the caller provided the correct background gamma!
7832 PNG_CONST double bg = dp->background_gamma;
7833 # ifndef PNG_FLOATING_POINT_SUPPORTED
7834 PNG_CONST png_fixed_point g = fix(bg);
7837 # ifdef PNG_FLOATING_POINT_SUPPORTED
7838 png_set_background(pp, &dp->background_color, dp->do_background,
7839 0/*need_expand*/, bg);
7841 png_set_background_fixed(pp, &dp->background_color,
7842 dp->do_background, 0/*need_expand*/, g);
7845 png_error(pp, "png_set_background not supported");
7851 int i = dp->this.use_update_info;
7852 /* Always do one call, even if use_update_info is 0. */
7854 png_read_update_info(pp, pi);
7858 /* Now we may get a different cbRow: */
7859 standard_info_part2(&dp->this, pp, pi, 1 /*images*/);
7862 static void PNGCBAPI
7863 gamma_info(png_structp pp, png_infop pi)
7865 gamma_info_imp(voidcast(gamma_display*, png_get_progressive_ptr(pp)), pp,
7869 /* Validate a single component value - the routine gets the input and output
7870 * sample values as unscaled PNG component values along with a cache of all the
7871 * information required to validate the values.
7873 typedef struct validate_info
7875 png_const_structp pp;
7878 int use_input_precision;
7881 unsigned int sbit_max;
7882 unsigned int isbit_shift;
7883 unsigned int outmax;
7885 double gamma_correction; /* Overall correction required. */
7886 double file_inverse; /* Inverse of file gamma. */
7887 double screen_gamma;
7888 double screen_inverse; /* Inverse of screen gamma. */
7890 double background_red; /* Linear background value, red or gray. */
7891 double background_green;
7892 double background_blue;
7898 double maxout_total; /* Total including quantization error */
7905 init_validate_info(validate_info *vi, gamma_display *dp, png_const_structp pp,
7906 int in_depth, int out_depth)
7908 PNG_CONST unsigned int outmax = (1U<<out_depth)-1;
7913 if (dp->sbit > 0 && dp->sbit < in_depth)
7915 vi->sbit = dp->sbit;
7916 vi->isbit_shift = in_depth - dp->sbit;
7921 vi->sbit = (png_byte)in_depth;
7922 vi->isbit_shift = 0;
7925 vi->sbit_max = (1U << vi->sbit)-1;
7927 /* This mimics the libpng threshold test, '0' is used to prevent gamma
7928 * correction in the validation test.
7930 vi->screen_gamma = dp->screen_gamma;
7931 if (fabs(vi->screen_gamma-1) < PNG_GAMMA_THRESHOLD)
7932 vi->screen_gamma = vi->screen_inverse = 0;
7934 vi->screen_inverse = 1/vi->screen_gamma;
7936 vi->use_input_precision = dp->use_input_precision;
7937 vi->outmax = outmax;
7938 vi->maxabs = abserr(dp->pm, in_depth, out_depth);
7939 vi->maxpc = pcerr(dp->pm, in_depth, out_depth);
7940 vi->maxcalc = calcerr(dp->pm, in_depth, out_depth);
7941 vi->maxout = outerr(dp->pm, in_depth, out_depth);
7942 vi->outquant = output_quantization_factor(dp->pm, in_depth, out_depth);
7943 vi->maxout_total = vi->maxout + vi->outquant * .5;
7944 vi->outlog = outlog(dp->pm, in_depth, out_depth);
7946 if ((dp->this.colour_type & PNG_COLOR_MASK_ALPHA) != 0 ||
7947 (dp->this.colour_type == 3 && dp->this.is_transparent))
7949 vi->do_background = dp->do_background;
7951 if (vi->do_background != 0)
7953 PNG_CONST double bg_inverse = 1/dp->background_gamma;
7956 /* Caller must at least put the gray value into the red channel */
7957 r = dp->background_color.red; r /= outmax;
7958 g = dp->background_color.green; g /= outmax;
7959 b = dp->background_color.blue; b /= outmax;
7962 /* libpng doesn't do this optimization, if we do pngvalid will fail.
7964 if (fabs(bg_inverse-1) >= PNG_GAMMA_THRESHOLD)
7967 r = pow(r, bg_inverse);
7968 g = pow(g, bg_inverse);
7969 b = pow(b, bg_inverse);
7972 vi->background_red = r;
7973 vi->background_green = g;
7974 vi->background_blue = b;
7978 vi->do_background = 0;
7980 if (vi->do_background == 0)
7981 vi->background_red = vi->background_green = vi->background_blue = 0;
7983 vi->gamma_correction = 1/(dp->file_gamma*dp->screen_gamma);
7984 if (fabs(vi->gamma_correction-1) < PNG_GAMMA_THRESHOLD)
7985 vi->gamma_correction = 0;
7987 vi->file_inverse = 1/dp->file_gamma;
7988 if (fabs(vi->file_inverse-1) < PNG_GAMMA_THRESHOLD)
7989 vi->file_inverse = 0;
7991 vi->scale16 = dp->scale16;
7994 /* This function handles composition of a single non-alpha component. The
7995 * argument is the input sample value, in the range 0..1, and the alpha value.
7996 * The result is the composed, linear, input sample. If alpha is less than zero
7997 * this is the alpha component and the function should not be called!
8000 gamma_component_compose(int do_background, double input_sample, double alpha,
8001 double background, int *compose)
8003 switch (do_background)
8005 #ifdef PNG_READ_BACKGROUND_SUPPORTED
8006 case PNG_BACKGROUND_GAMMA_SCREEN:
8007 case PNG_BACKGROUND_GAMMA_FILE:
8008 case PNG_BACKGROUND_GAMMA_UNIQUE:
8009 /* Standard PNG background processing. */
8014 input_sample = input_sample * alpha + background * (1-alpha);
8015 if (compose != NULL)
8020 input_sample = background;
8025 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8026 case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
8027 case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
8028 /* The components are premultiplied in either case and the output is
8029 * gamma encoded (to get standard Porter-Duff we expect the output
8030 * gamma to be set to 1.0!)
8032 case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
8033 /* The optimization is that the partial-alpha entries are linear
8034 * while the opaque pixels are gamma encoded, but this only affects the
8041 input_sample *= alpha;
8042 if (compose != NULL)
8053 /* Standard cases where no compositing is done (so the component
8054 * value is already correct.)
8062 return input_sample;
8065 /* This API returns the encoded *input* component, in the range 0..1 */
8067 gamma_component_validate(PNG_CONST char *name, PNG_CONST validate_info *vi,
8068 PNG_CONST unsigned int id, PNG_CONST unsigned int od,
8069 PNG_CONST double alpha /* <0 for the alpha channel itself */,
8070 PNG_CONST double background /* component background value */)
8072 PNG_CONST unsigned int isbit = id >> vi->isbit_shift;
8073 PNG_CONST unsigned int sbit_max = vi->sbit_max;
8074 PNG_CONST unsigned int outmax = vi->outmax;
8075 PNG_CONST int do_background = vi->do_background;
8079 /* First check on the 'perfect' result obtained from the digitized input
8080 * value, id, and compare this against the actual digitized result, 'od'.
8081 * 'i' is the input result in the range 0..1:
8083 i = isbit; i /= sbit_max;
8085 /* Check for the fast route: if we don't do any background composition or if
8086 * this is the alpha channel ('alpha' < 0) or if the pixel is opaque then
8087 * just use the gamma_correction field to correct to the final output gamma.
8089 if (alpha == 1 /* opaque pixel component */ || !do_background
8090 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8091 || do_background == ALPHA_MODE_OFFSET + PNG_ALPHA_PNG
8093 || (alpha < 0 /* alpha channel */
8094 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8095 && do_background != ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN
8099 /* Then get the gamma corrected version of 'i' and compare to 'od', any
8100 * error less than .5 is insignificant - just quantization of the output
8101 * value to the nearest digital value (nevertheless the error is still
8102 * recorded - it's interesting ;-)
8104 double encoded_sample = i;
8105 double encoded_error;
8107 /* alpha less than 0 indicates the alpha channel, which is always linear
8109 if (alpha >= 0 && vi->gamma_correction > 0)
8110 encoded_sample = pow(encoded_sample, vi->gamma_correction);
8111 encoded_sample *= outmax;
8113 encoded_error = fabs(od-encoded_sample);
8115 if (encoded_error > vi->dp->maxerrout)
8116 vi->dp->maxerrout = encoded_error;
8118 if (encoded_error < vi->maxout_total && encoded_error < vi->outlog)
8122 /* The slow route - attempt to do linear calculations. */
8123 /* There may be an error, or background processing is required, so calculate
8124 * the actual sample values - unencoded light intensity values. Note that in
8125 * practice these are not completely unencoded because they include a
8126 * 'viewing correction' to decrease or (normally) increase the perceptual
8127 * contrast of the image. There's nothing we can do about this - we don't
8128 * know what it is - so assume the unencoded value is perceptually linear.
8131 double input_sample = i; /* In range 0..1 */
8132 double output, error, encoded_sample, encoded_error;
8133 double es_lo, es_hi;
8134 int compose = 0; /* Set to one if composition done */
8135 int output_is_encoded; /* Set if encoded to screen gamma */
8136 int log_max_error = 1; /* Check maximum error values */
8137 png_const_charp pass = 0; /* Reason test passes (or 0 for fail) */
8139 /* Convert to linear light (with the above caveat.) The alpha channel is
8146 if (vi->file_inverse > 0)
8147 input_sample = pow(input_sample, vi->file_inverse);
8149 /* Handle the compose processing: */
8151 input_sample = gamma_component_compose(do_background, input_sample,
8152 alpha, background, &tcompose);
8158 /* And similarly for the output value, but we need to check the background
8159 * handling to linearize it correctly.
8164 output_is_encoded = vi->screen_gamma > 0;
8166 if (alpha < 0) /* The alpha channel */
8168 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8169 if (do_background != ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN)
8172 /* In all other cases the output alpha channel is linear already,
8173 * don't log errors here, they are much larger in linear data.
8175 output_is_encoded = 0;
8180 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8181 else /* A component */
8183 if (do_background == ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED &&
8184 alpha < 1) /* the optimized case - linear output */
8186 if (alpha > 0) log_max_error = 0;
8187 output_is_encoded = 0;
8192 if (output_is_encoded)
8193 output = pow(output, vi->screen_gamma);
8195 /* Calculate (or recalculate) the encoded_sample value and repeat the
8196 * check above (unnecessary if we took the fast route, but harmless.)
8198 encoded_sample = input_sample;
8199 if (output_is_encoded)
8200 encoded_sample = pow(encoded_sample, vi->screen_inverse);
8201 encoded_sample *= outmax;
8203 encoded_error = fabs(od-encoded_sample);
8205 /* Don't log errors in the alpha channel, or the 'optimized' case,
8206 * neither are significant to the overall perception.
8208 if (log_max_error && encoded_error > vi->dp->maxerrout)
8209 vi->dp->maxerrout = encoded_error;
8211 if (encoded_error < vi->maxout_total)
8213 if (encoded_error < vi->outlog)
8216 /* Test passed but error is bigger than the log limit, record why the
8219 pass = "less than maxout:\n";
8222 /* i: the original input value in the range 0..1
8224 * pngvalid calculations:
8225 * input_sample: linear result; i linearized and composed, range 0..1
8226 * encoded_sample: encoded result; input_sample scaled to ouput bit depth
8228 * libpng calculations:
8229 * output: linear result; od scaled to 0..1 and linearized
8230 * od: encoded result from libpng
8233 /* Now we have the numbers for real errors, both absolute values as as a
8234 * percentage of the correct value (output):
8236 error = fabs(input_sample-output);
8238 if (log_max_error && error > vi->dp->maxerrabs)
8239 vi->dp->maxerrabs = error;
8241 /* The following is an attempt to ignore the tendency of quantization to
8242 * dominate the percentage errors for lower result values:
8244 if (log_max_error && input_sample > .5)
8246 double percentage_error = error/input_sample;
8247 if (percentage_error > vi->dp->maxerrpc)
8248 vi->dp->maxerrpc = percentage_error;
8251 /* Now calculate the digitization limits for 'encoded_sample' using the
8252 * 'max' values. Note that maxout is in the encoded space but maxpc and
8253 * maxabs are in linear light space.
8255 * First find the maximum error in linear light space, range 0..1:
8258 double tmp = input_sample * vi->maxpc;
8259 if (tmp < vi->maxabs) tmp = vi->maxabs;
8260 /* If 'compose' is true the composition was done in linear space using
8261 * integer arithmetic. This introduces an extra error of +/- 0.5 (at
8262 * least) in the integer space used. 'maxcalc' records this, taking
8263 * into account the possibility that even for 16 bit output 8 bit space
8264 * may have been used.
8266 if (compose && tmp < vi->maxcalc) tmp = vi->maxcalc;
8268 /* The 'maxout' value refers to the encoded result, to compare with
8269 * this encode input_sample adjusted by the maximum error (tmp) above.
8271 es_lo = encoded_sample - vi->maxout;
8273 if (es_lo > 0 && input_sample-tmp > 0)
8275 double low_value = input_sample-tmp;
8276 if (output_is_encoded)
8277 low_value = pow(low_value, vi->screen_inverse);
8278 low_value *= outmax;
8279 if (low_value < es_lo) es_lo = low_value;
8281 /* Quantize this appropriately: */
8282 es_lo = ceil(es_lo / vi->outquant - .5) * vi->outquant;
8288 es_hi = encoded_sample + vi->maxout;
8290 if (es_hi < outmax && input_sample+tmp < 1)
8292 double high_value = input_sample+tmp;
8293 if (output_is_encoded)
8294 high_value = pow(high_value, vi->screen_inverse);
8295 high_value *= outmax;
8296 if (high_value > es_hi) es_hi = high_value;
8298 es_hi = floor(es_hi / vi->outquant + .5) * vi->outquant;
8305 /* The primary test is that the final encoded value returned by the
8306 * library should be between the two limits (inclusive) that were
8309 if (od >= es_lo && od <= es_hi)
8311 /* The value passes, but we may need to log the information anyway. */
8312 if (encoded_error < vi->outlog)
8316 pass = "within digitization limits:\n";
8320 /* There has been an error in processing, or we need to log this
8323 double is_lo, is_hi;
8325 /* pass is set at this point if either of the tests above would have
8326 * passed. Don't do these additional tests here - just log the
8327 * original [es_lo..es_hi] values.
8329 if (pass == 0 && vi->use_input_precision && vi->dp->sbit)
8331 /* Ok, something is wrong - this actually happens in current libpng
8332 * 16-to-8 processing. Assume that the input value (id, adjusted
8333 * for sbit) can be anywhere between value-.5 and value+.5 - quite a
8334 * large range if sbit is low.
8336 * NOTE: at present because the libpng gamma table stuff has been
8337 * changed to use a rounding algorithm to correct errors in 8-bit
8338 * calculations the precise sbit calculation (a shift) has been
8339 * lost. This can result in up to a +/-1 error in the presence of
8340 * an sbit less than the bit depth.
8342 # if PNG_LIBPNG_VER < 10700
8343 # define SBIT_ERROR .5
8345 # define SBIT_ERROR 1.
8347 double tmp = (isbit - SBIT_ERROR)/sbit_max;
8352 else if (alpha >= 0 && vi->file_inverse > 0 && tmp < 1)
8353 tmp = pow(tmp, vi->file_inverse);
8355 tmp = gamma_component_compose(do_background, tmp, alpha, background,
8358 if (output_is_encoded && tmp > 0 && tmp < 1)
8359 tmp = pow(tmp, vi->screen_inverse);
8361 is_lo = ceil(outmax * tmp - vi->maxout_total);
8366 tmp = (isbit + SBIT_ERROR)/sbit_max;
8371 else if (alpha >= 0 && vi->file_inverse > 0 && tmp < 1)
8372 tmp = pow(tmp, vi->file_inverse);
8374 tmp = gamma_component_compose(do_background, tmp, alpha, background,
8377 if (output_is_encoded && tmp > 0 && tmp < 1)
8378 tmp = pow(tmp, vi->screen_inverse);
8380 is_hi = floor(outmax * tmp + vi->maxout_total);
8385 if (!(od < is_lo || od > is_hi))
8387 if (encoded_error < vi->outlog)
8390 pass = "within input precision limits:\n";
8393 /* One last chance. If this is an alpha channel and the 16to8
8394 * option has been used and 'inaccurate' scaling is used then the
8395 * bit reduction is obtained by simply using the top 8 bits of the
8398 * This is only done for older libpng versions when the 'inaccurate'
8399 * (chop) method of scaling was used.
8401 # ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
8402 # if PNG_LIBPNG_VER < 10504
8403 /* This may be required for other components in the future,
8404 * but at present the presence of gamma correction effectively
8405 * prevents the errors in the component scaling (I don't quite
8406 * understand why, but since it's better this way I care not
8407 * to ask, JB 20110419.)
8409 if (pass == 0 && alpha < 0 && vi->scale16 && vi->sbit > 8 &&
8410 vi->sbit + vi->isbit_shift == 16)
8412 tmp = ((id >> 8) - .5)/255;
8416 is_lo = ceil(outmax * tmp - vi->maxout_total);
8417 if (is_lo < 0) is_lo = 0;
8423 tmp = ((id >> 8) + .5)/255;
8427 is_hi = floor(outmax * tmp + vi->maxout_total);
8428 if (is_hi > outmax) is_hi = outmax;
8434 if (!(od < is_lo || od > is_hi))
8436 if (encoded_error < vi->outlog)
8439 pass = "within 8 bit limits:\n";
8445 else /* !use_input_precision */
8446 is_lo = es_lo, is_hi = es_hi;
8448 /* Attempt to output a meaningful error/warning message: the message
8449 * output depends on the background/composite operation being performed
8450 * because this changes what parameters were actually used above.
8454 /* Need either 1/255 or 1/65535 precision here; 3 or 6 decimal
8455 * places. Just use outmax to work out which.
8457 int precision = (outmax >= 1000 ? 6 : 3);
8458 int use_input=1, use_background=0, do_compose=0;
8462 pos = safecat(msg, sizeof msg, pos, "\n\t");
8464 /* Set up the various flags, the output_is_encoded flag above
8465 * is also used below. do_compose is just a double check.
8467 switch (do_background)
8469 # ifdef PNG_READ_BACKGROUND_SUPPORTED
8470 case PNG_BACKGROUND_GAMMA_SCREEN:
8471 case PNG_BACKGROUND_GAMMA_FILE:
8472 case PNG_BACKGROUND_GAMMA_UNIQUE:
8473 use_background = (alpha >= 0 && alpha < 1);
8476 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8477 case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
8478 case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
8479 case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
8480 # endif /* ALPHA_MODE_SUPPORTED */
8481 do_compose = (alpha > 0 && alpha < 1);
8482 use_input = (alpha != 0);
8489 /* Check the 'compose' flag */
8490 if (compose != do_compose)
8491 png_error(vi->pp, "internal error (compose)");
8493 /* 'name' is the component name */
8494 pos = safecat(msg, sizeof msg, pos, name);
8495 pos = safecat(msg, sizeof msg, pos, "(");
8496 pos = safecatn(msg, sizeof msg, pos, id);
8497 if (use_input || pass != 0/*logging*/)
8501 /* sBIT has reduced the precision of the input: */
8502 pos = safecat(msg, sizeof msg, pos, ", sbit(");
8503 pos = safecatn(msg, sizeof msg, pos, vi->sbit);
8504 pos = safecat(msg, sizeof msg, pos, "): ");
8505 pos = safecatn(msg, sizeof msg, pos, isbit);
8507 pos = safecat(msg, sizeof msg, pos, "/");
8508 /* The output is either "id/max" or "id sbit(sbit): isbit/max" */
8509 pos = safecatn(msg, sizeof msg, pos, vi->sbit_max);
8511 pos = safecat(msg, sizeof msg, pos, ")");
8513 /* A component may have been multiplied (in linear space) by the
8514 * alpha value, 'compose' says whether this is relevant.
8516 if (compose || pass != 0)
8518 /* If any form of composition is being done report our
8519 * calculated linear value here (the code above doesn't record
8520 * the input value before composition is performed, so what
8521 * gets reported is the value after composition.)
8523 if (use_input || pass != 0)
8525 if (vi->file_inverse > 0)
8527 pos = safecat(msg, sizeof msg, pos, "^");
8528 pos = safecatd(msg, sizeof msg, pos, vi->file_inverse, 2);
8532 pos = safecat(msg, sizeof msg, pos, "[linear]");
8534 pos = safecat(msg, sizeof msg, pos, "*(alpha)");
8535 pos = safecatd(msg, sizeof msg, pos, alpha, precision);
8538 /* Now record the *linear* background value if it was used
8539 * (this function is not passed the original, non-linear,
8540 * value but it is contained in the test name.)
8544 pos = safecat(msg, sizeof msg, pos, use_input ? "+" : " ");
8545 pos = safecat(msg, sizeof msg, pos, "(background)");
8546 pos = safecatd(msg, sizeof msg, pos, background, precision);
8547 pos = safecat(msg, sizeof msg, pos, "*");
8548 pos = safecatd(msg, sizeof msg, pos, 1-alpha, precision);
8552 /* Report the calculated value (input_sample) and the linearized
8553 * libpng value (output) unless this is just a component gamma
8556 if (compose || alpha < 0 || pass != 0)
8558 pos = safecat(msg, sizeof msg, pos,
8559 pass != 0 ? " =\n\t" : " = ");
8560 pos = safecatd(msg, sizeof msg, pos, input_sample, precision);
8561 pos = safecat(msg, sizeof msg, pos, " (libpng: ");
8562 pos = safecatd(msg, sizeof msg, pos, output, precision);
8563 pos = safecat(msg, sizeof msg, pos, ")");
8565 /* Finally report the output gamma encoding, if any. */
8566 if (output_is_encoded)
8568 pos = safecat(msg, sizeof msg, pos, " ^");
8569 pos = safecatd(msg, sizeof msg, pos, vi->screen_inverse, 2);
8570 pos = safecat(msg, sizeof msg, pos, "(to screen) =");
8574 pos = safecat(msg, sizeof msg, pos, " [screen is linear] =");
8577 if ((!compose && alpha >= 0) || pass != 0)
8579 if (pass != 0) /* logging */
8580 pos = safecat(msg, sizeof msg, pos, "\n\t[overall:");
8582 /* This is the non-composition case, the internal linear
8583 * values are irrelevant (though the log below will reveal
8584 * them.) Output a much shorter warning/error message and report
8585 * the overall gamma correction.
8587 if (vi->gamma_correction > 0)
8589 pos = safecat(msg, sizeof msg, pos, " ^");
8590 pos = safecatd(msg, sizeof msg, pos, vi->gamma_correction, 2);
8591 pos = safecat(msg, sizeof msg, pos, "(gamma correction) =");
8595 pos = safecat(msg, sizeof msg, pos,
8596 " [no gamma correction] =");
8599 pos = safecat(msg, sizeof msg, pos, "]");
8602 /* This is our calculated encoded_sample which should (but does
8605 pos = safecat(msg, sizeof msg, pos, pass != 0 ? "\n\t" : " ");
8606 pos = safecatd(msg, sizeof msg, pos, is_lo, 1);
8607 pos = safecat(msg, sizeof msg, pos, " < ");
8608 pos = safecatd(msg, sizeof msg, pos, encoded_sample, 1);
8609 pos = safecat(msg, sizeof msg, pos, " (libpng: ");
8610 pos = safecatn(msg, sizeof msg, pos, od);
8611 pos = safecat(msg, sizeof msg, pos, ")");
8612 pos = safecat(msg, sizeof msg, pos, "/");
8613 pos = safecatn(msg, sizeof msg, pos, outmax);
8614 pos = safecat(msg, sizeof msg, pos, " < ");
8615 pos = safecatd(msg, sizeof msg, pos, is_hi, 1);
8617 if (pass == 0) /* The error condition */
8619 # ifdef PNG_WARNINGS_SUPPORTED
8620 png_warning(vi->pp, msg);
8622 store_warning(vi->pp, msg);
8626 else /* logging this value */
8627 store_verbose(&vi->dp->pm->this, vi->pp, pass, msg);
8636 gamma_image_validate(gamma_display *dp, png_const_structp pp,
8639 /* Get some constants derived from the input and output file formats: */
8640 PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
8641 PNG_CONST png_byte in_ct = dp->this.colour_type;
8642 PNG_CONST png_byte in_bd = dp->this.bit_depth;
8643 PNG_CONST png_uint_32 w = dp->this.w;
8644 PNG_CONST png_uint_32 h = dp->this.h;
8645 PNG_CONST size_t cbRow = dp->this.cbRow;
8646 PNG_CONST png_byte out_ct = png_get_color_type(pp, pi);
8647 PNG_CONST png_byte out_bd = png_get_bit_depth(pp, pi);
8649 /* There are three sources of error, firstly the quantization in the
8650 * file encoding, determined by sbit and/or the file depth, secondly
8651 * the output (screen) gamma and thirdly the output file encoding.
8653 * Since this API receives the screen and file gamma in double
8654 * precision it is possible to calculate an exact answer given an input
8655 * pixel value. Therefore we assume that the *input* value is exact -
8656 * sample/maxsample - calculate the corresponding gamma corrected
8657 * output to the limits of double precision arithmetic and compare with
8658 * what libpng returns.
8660 * Since the library must quantize the output to 8 or 16 bits there is
8661 * a fundamental limit on the accuracy of the output of +/-.5 - this
8662 * quantization limit is included in addition to the other limits
8663 * specified by the paramaters to the API. (Effectively, add .5
8666 * The behavior of the 'sbit' paramter is defined by section 12.5
8667 * (sample depth scaling) of the PNG spec. That section forces the
8668 * decoder to assume that the PNG values have been scaled if sBIT is
8671 * png-sample = floor( input-sample * (max-out/max-in) + .5);
8673 * This means that only a subset of the possible PNG values should
8674 * appear in the input. However, the spec allows the encoder to use a
8675 * variety of approximations to the above and doesn't require any
8676 * restriction of the values produced.
8678 * Nevertheless the spec requires that the upper 'sBIT' bits of the
8679 * value stored in a PNG file be the original sample bits.
8680 * Consequently the code below simply scales the top sbit bits by
8681 * (1<<sbit)-1 to obtain an original sample value.
8683 * Because there is limited precision in the input it is arguable that
8684 * an acceptable result is any valid result from input-.5 to input+.5.
8685 * The basic tests below do not do this, however if 'use_input_precision'
8686 * is set a subsequent test is performed above.
8688 PNG_CONST unsigned int samples_per_pixel = (out_ct & 2U) ? 3U : 1U;
8691 PNG_CONST store_palette_entry *in_palette = dp->this.palette;
8692 PNG_CONST int in_is_transparent = dp->this.is_transparent;
8693 int out_npalette = -1;
8694 int out_is_transparent = 0; /* Just refers to the palette case */
8695 store_palette out_palette;
8698 /* Check for row overwrite errors */
8699 store_image_check(dp->this.ps, pp, 0);
8701 /* Supply the input and output sample depths here - 8 for an indexed image,
8702 * otherwise the bit depth.
8704 init_validate_info(&vi, dp, pp, in_ct==3?8:in_bd, out_ct==3?8:out_bd);
8706 processing = (vi.gamma_correction > 0 && !dp->threshold_test)
8707 || in_bd != out_bd || in_ct != out_ct || vi.do_background;
8709 /* TODO: FIX THIS: MAJOR BUG! If the transformations all happen inside
8710 * the palette there is no way of finding out, because libpng fails to
8711 * update the palette on png_read_update_info. Indeed, libpng doesn't
8712 * even do the required work until much later, when it doesn't have any
8713 * info pointer. Oops. For the moment 'processing' is turned off if
8714 * out_ct is palette.
8716 if (in_ct == 3 && out_ct == 3)
8719 if (processing && out_ct == 3)
8720 out_is_transparent = read_palette(out_palette, &out_npalette, pp, pi);
8724 png_const_bytep pRow = store_image_row(ps, pp, 0, y);
8725 png_byte std[STANDARD_ROWMAX];
8727 transform_row(pp, std, in_ct, in_bd, y);
8735 double alpha = 1; /* serves as a flag value */
8737 /* Record the palette index for index images. */
8738 PNG_CONST unsigned int in_index =
8739 in_ct == 3 ? sample(std, 3, in_bd, x, 0) : 256;
8740 PNG_CONST unsigned int out_index =
8741 out_ct == 3 ? sample(std, 3, out_bd, x, 0) : 256;
8743 /* Handle input alpha - png_set_background will cause the output
8744 * alpha to disappear so there is nothing to check.
8746 if ((in_ct & PNG_COLOR_MASK_ALPHA) != 0 || (in_ct == 3 &&
8749 PNG_CONST unsigned int input_alpha = in_ct == 3 ?
8750 dp->this.palette[in_index].alpha :
8751 sample(std, in_ct, in_bd, x, samples_per_pixel);
8753 unsigned int output_alpha = 65536 /* as a flag value */;
8757 if (out_is_transparent)
8758 output_alpha = out_palette[out_index].alpha;
8761 else if ((out_ct & PNG_COLOR_MASK_ALPHA) != 0)
8762 output_alpha = sample(pRow, out_ct, out_bd, x,
8765 if (output_alpha != 65536)
8766 alpha = gamma_component_validate("alpha", &vi, input_alpha,
8767 output_alpha, -1/*alpha*/, 0/*background*/);
8769 else /* no alpha in output */
8771 /* This is a copy of the calculation of 'i' above in order to
8772 * have the alpha value to use in the background calculation.
8774 alpha = input_alpha >> vi.isbit_shift;
8775 alpha /= vi.sbit_max;
8779 /* Handle grayscale or RGB components. */
8780 if ((in_ct & PNG_COLOR_MASK_COLOR) == 0) /* grayscale */
8781 (void)gamma_component_validate("gray", &vi,
8782 sample(std, in_ct, in_bd, x, 0),
8783 sample(pRow, out_ct, out_bd, x, 0), alpha/*component*/,
8785 else /* RGB or palette */
8787 (void)gamma_component_validate("red", &vi,
8788 in_ct == 3 ? in_palette[in_index].red :
8789 sample(std, in_ct, in_bd, x, 0),
8790 out_ct == 3 ? out_palette[out_index].red :
8791 sample(pRow, out_ct, out_bd, x, 0),
8792 alpha/*component*/, vi.background_red);
8794 (void)gamma_component_validate("green", &vi,
8795 in_ct == 3 ? in_palette[in_index].green :
8796 sample(std, in_ct, in_bd, x, 1),
8797 out_ct == 3 ? out_palette[out_index].green :
8798 sample(pRow, out_ct, out_bd, x, 1),
8799 alpha/*component*/, vi.background_green);
8801 (void)gamma_component_validate("blue", &vi,
8802 in_ct == 3 ? in_palette[in_index].blue :
8803 sample(std, in_ct, in_bd, x, 2),
8804 out_ct == 3 ? out_palette[out_index].blue :
8805 sample(pRow, out_ct, out_bd, x, 2),
8806 alpha/*component*/, vi.background_blue);
8811 else if (memcmp(std, pRow, cbRow) != 0)
8815 /* No transform is expected on the threshold tests. */
8816 sprintf(msg, "gamma: below threshold row %lu changed",
8821 } /* row (y) loop */
8823 dp->this.ps->validated = 1;
8826 static void PNGCBAPI
8827 gamma_end(png_structp ppIn, png_infop pi)
8829 png_const_structp pp = ppIn;
8830 gamma_display *dp = voidcast(gamma_display*, png_get_progressive_ptr(pp));
8832 if (!dp->this.speed)
8833 gamma_image_validate(dp, pp, pi);
8835 dp->this.ps->validated = 1;
8838 /* A single test run checking a gamma transformation.
8840 * maxabs: maximum absolute error as a fraction
8841 * maxout: maximum output error in the output units
8842 * maxpc: maximum percentage error (as a percentage)
8845 gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn,
8846 PNG_CONST png_byte bit_depthIn, PNG_CONST int palette_numberIn,
8847 PNG_CONST int interlace_typeIn,
8848 PNG_CONST double file_gammaIn, PNG_CONST double screen_gammaIn,
8849 PNG_CONST png_byte sbitIn, PNG_CONST int threshold_testIn,
8850 PNG_CONST char *name,
8851 PNG_CONST int use_input_precisionIn, PNG_CONST int scale16In,
8852 PNG_CONST int expand16In, PNG_CONST int do_backgroundIn,
8853 PNG_CONST png_color_16 *bkgd_colorIn, double bkgd_gammaIn)
8856 context(&pmIn->this, fault);
8858 gamma_display_init(&d, pmIn, FILEID(colour_typeIn, bit_depthIn,
8859 palette_numberIn, interlace_typeIn, 0, 0, 0),
8860 file_gammaIn, screen_gammaIn, sbitIn,
8861 threshold_testIn, use_input_precisionIn, scale16In,
8862 expand16In, do_backgroundIn, bkgd_colorIn, bkgd_gammaIn);
8868 gama_modification gama_mod;
8869 srgb_modification srgb_mod;
8870 sbit_modification sbit_mod;
8872 /* For the moment don't use the png_modifier support here. */
8873 d.pm->encoding_counter = 0;
8874 modifier_set_encoding(d.pm); /* Just resets everything */
8875 d.pm->current_gamma = d.file_gamma;
8877 /* Make an appropriate modifier to set the PNG file gamma to the
8878 * given gamma value and the sBIT chunk to the given precision.
8880 d.pm->modifications = NULL;
8881 gama_modification_init(&gama_mod, d.pm, d.file_gamma);
8882 srgb_modification_init(&srgb_mod, d.pm, 127 /*delete*/);
8884 sbit_modification_init(&sbit_mod, d.pm, d.sbit);
8886 modification_reset(d.pm->modifications);
8888 /* Get a png_struct for writing the image. */
8889 pp = set_modifier_for_read(d.pm, &pi, d.this.id, name);
8890 standard_palette_init(&d.this);
8892 /* Introduce the correct read function. */
8893 if (d.pm->this.progressive)
8895 /* Share the row function with the standard implementation. */
8896 png_set_progressive_read_fn(pp, &d, gamma_info, progressive_row,
8899 /* Now feed data into the reader until we reach the end: */
8900 modifier_progressive_read(d.pm, pp, pi);
8904 /* modifier_read expects a png_modifier* */
8905 png_set_read_fn(pp, d.pm, modifier_read);
8907 /* Check the header values: */
8908 png_read_info(pp, pi);
8910 /* Process the 'info' requirements. Only one image is generated */
8911 gamma_info_imp(&d, pp, pi);
8913 sequential_row(&d.this, pp, pi, -1, 0);
8916 gamma_image_validate(&d, pp, pi);
8918 d.this.ps->validated = 1;
8921 modifier_reset(d.pm);
8923 if (d.pm->log && !d.threshold_test && !d.this.speed)
8924 fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n",
8925 d.this.bit_depth, colour_types[d.this.colour_type], name,
8926 d.maxerrout, d.maxerrabs, 100*d.maxerrpc);
8928 /* Log the summary values too. */
8929 if (d.this.colour_type == 0 || d.this.colour_type == 4)
8931 switch (d.this.bit_depth)
8937 if (d.maxerrout > d.pm->error_gray_2)
8938 d.pm->error_gray_2 = d.maxerrout;
8943 if (d.maxerrout > d.pm->error_gray_4)
8944 d.pm->error_gray_4 = d.maxerrout;
8949 if (d.maxerrout > d.pm->error_gray_8)
8950 d.pm->error_gray_8 = d.maxerrout;
8955 if (d.maxerrout > d.pm->error_gray_16)
8956 d.pm->error_gray_16 = d.maxerrout;
8961 png_error(pp, "bad bit depth (internal: 1)");
8965 else if (d.this.colour_type == 2 || d.this.colour_type == 6)
8967 switch (d.this.bit_depth)
8971 if (d.maxerrout > d.pm->error_color_8)
8972 d.pm->error_color_8 = d.maxerrout;
8978 if (d.maxerrout > d.pm->error_color_16)
8979 d.pm->error_color_16 = d.maxerrout;
8984 png_error(pp, "bad bit depth (internal: 2)");
8988 else if (d.this.colour_type == 3)
8990 if (d.maxerrout > d.pm->error_indexed)
8991 d.pm->error_indexed = d.maxerrout;
8996 modifier_reset(voidcast(png_modifier*,(void*)fault));
8999 static void gamma_threshold_test(png_modifier *pm, png_byte colour_type,
9000 png_byte bit_depth, int interlace_type, double file_gamma,
9001 double screen_gamma)
9005 pos = safecat(name, sizeof name, pos, "threshold ");
9006 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9007 pos = safecat(name, sizeof name, pos, "/");
9008 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9010 (void)gamma_test(pm, colour_type, bit_depth, 0/*palette*/, interlace_type,
9011 file_gamma, screen_gamma, 0/*sBIT*/, 1/*threshold test*/, name,
9012 0 /*no input precision*/,
9013 0 /*no scale16*/, 0 /*no expand16*/, 0 /*no background*/, 0 /*hence*/,
9014 0 /*no background gamma*/);
9018 perform_gamma_threshold_tests(png_modifier *pm)
9020 png_byte colour_type = 0;
9021 png_byte bit_depth = 0;
9022 unsigned int palette_number = 0;
9024 /* Don't test more than one instance of each palette - it's pointless, in
9025 * fact this test is somewhat excessive since libpng doesn't make this
9026 * decision based on colour type or bit depth!
9028 while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9029 if (palette_number == 0)
9031 double test_gamma = 1.0;
9032 while (test_gamma >= .4)
9034 /* There's little point testing the interlacing vs non-interlacing,
9035 * but this can be set from the command line.
9037 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
9038 test_gamma, 1/test_gamma);
9042 /* And a special test for sRGB */
9043 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
9051 static void gamma_transform_test(png_modifier *pm,
9052 PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
9053 PNG_CONST int palette_number,
9054 PNG_CONST int interlace_type, PNG_CONST double file_gamma,
9055 PNG_CONST double screen_gamma, PNG_CONST png_byte sbit,
9056 PNG_CONST int use_input_precision, PNG_CONST int scale16)
9061 if (sbit != bit_depth && sbit != 0)
9063 pos = safecat(name, sizeof name, pos, "sbit(");
9064 pos = safecatn(name, sizeof name, pos, sbit);
9065 pos = safecat(name, sizeof name, pos, ") ");
9069 pos = safecat(name, sizeof name, pos, "gamma ");
9072 pos = safecat(name, sizeof name, pos, "16to8 ");
9074 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9075 pos = safecat(name, sizeof name, pos, "->");
9076 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9078 gamma_test(pm, colour_type, bit_depth, palette_number, interlace_type,
9079 file_gamma, screen_gamma, sbit, 0, name, use_input_precision,
9080 scale16, pm->test_gamma_expand16, 0 , 0, 0);
9083 static void perform_gamma_transform_tests(png_modifier *pm)
9085 png_byte colour_type = 0;
9086 png_byte bit_depth = 0;
9087 unsigned int palette_number = 0;
9089 while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9093 for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
9096 gamma_transform_test(pm, colour_type, bit_depth, palette_number,
9097 pm->interlace_type, 1/pm->gammas[i], pm->gammas[j], 0/*sBIT*/,
9098 pm->use_input_precision, 0 /*do not scale16*/);
9106 static void perform_gamma_sbit_tests(png_modifier *pm)
9110 /* The only interesting cases are colour and grayscale, alpha is ignored here
9111 * for overall speed. Only bit depths where sbit is less than the bit depth
9114 for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit)
9116 png_byte colour_type = 0, bit_depth = 0;
9117 unsigned int npalette = 0;
9119 while (next_format(&colour_type, &bit_depth, &npalette, 1/*gamma*/))
9120 if ((colour_type & PNG_COLOR_MASK_ALPHA) == 0 &&
9121 ((colour_type == 3 && sbit < 8) ||
9122 (colour_type != 3 && sbit < bit_depth)))
9126 for (i=0; i<pm->ngamma_tests; ++i)
9130 for (j=0; j<pm->ngamma_tests; ++j) if (i != j)
9132 gamma_transform_test(pm, colour_type, bit_depth, npalette,
9133 pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
9134 sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
9144 /* Note that this requires a 16 bit source image but produces 8 bit output, so
9145 * we only need the 16bit write support, but the 16 bit images are only
9146 * generated if DO_16BIT is defined.
9149 static void perform_gamma_scale16_tests(png_modifier *pm)
9151 # ifndef PNG_MAX_GAMMA_8
9152 # define PNG_MAX_GAMMA_8 11
9154 # define SBIT_16_TO_8 PNG_MAX_GAMMA_8
9155 /* Include the alpha cases here. Note that sbit matches the internal value
9156 * used by the library - otherwise we will get spurious errors from the
9157 * internal sbit style approximation.
9159 * The threshold test is here because otherwise the 16 to 8 conversion will
9160 * proceed *without* gamma correction, and the tests above will fail (but not
9161 * by much) - this could be fixed, it only appears with the -g option.
9164 for (i=0; i<pm->ngamma_tests; ++i)
9166 for (j=0; j<pm->ngamma_tests; ++j)
9169 fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD)
9171 gamma_transform_test(pm, 0, 16, 0, pm->interlace_type,
9172 1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9173 pm->use_input_precision_16to8, 1 /*scale16*/);
9178 gamma_transform_test(pm, 2, 16, 0, pm->interlace_type,
9179 1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9180 pm->use_input_precision_16to8, 1 /*scale16*/);
9185 gamma_transform_test(pm, 4, 16, 0, pm->interlace_type,
9186 1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9187 pm->use_input_precision_16to8, 1 /*scale16*/);
9192 gamma_transform_test(pm, 6, 16, 0, pm->interlace_type,
9193 1/pm->gammas[i], pm->gammas[j], SBIT_16_TO_8,
9194 pm->use_input_precision_16to8, 1 /*scale16*/);
9202 #endif /* 16 to 8 bit conversion */
9204 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
9205 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
9206 static void gamma_composition_test(png_modifier *pm,
9207 PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
9208 PNG_CONST int palette_number,
9209 PNG_CONST int interlace_type, PNG_CONST double file_gamma,
9210 PNG_CONST double screen_gamma,
9211 PNG_CONST int use_input_precision, PNG_CONST int do_background,
9212 PNG_CONST int expand_16)
9215 png_const_charp base;
9218 png_color_16 background;
9220 /* Make up a name and get an appropriate background gamma value. */
9221 switch (do_background)
9225 bg = 4; /* should not be used */
9227 case PNG_BACKGROUND_GAMMA_SCREEN:
9228 base = " bckg(Screen):";
9229 bg = 1/screen_gamma;
9231 case PNG_BACKGROUND_GAMMA_FILE:
9232 base = " bckg(File):";
9235 case PNG_BACKGROUND_GAMMA_UNIQUE:
9236 base = " bckg(Unique):";
9237 /* This tests the handling of a unique value, the math is such that the
9238 * value tends to be <1, but is neither screen nor file (even if they
9241 bg = (file_gamma + screen_gamma) / 3;
9243 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9244 case ALPHA_MODE_OFFSET + PNG_ALPHA_PNG:
9245 base = " alpha(PNG)";
9246 bg = 4; /* should not be used */
9248 case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
9249 base = " alpha(Porter-Duff)";
9250 bg = 4; /* should not be used */
9252 case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
9253 base = " alpha(Optimized)";
9254 bg = 4; /* should not be used */
9256 case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
9257 base = " alpha(Broken)";
9258 bg = 4; /* should not be used */
9263 /* Use random background values - the background is always presented in the
9264 * output space (8 or 16 bit components).
9266 if (expand_16 || bit_depth == 16)
9268 png_uint_32 r = random_32();
9270 background.red = (png_uint_16)r;
9271 background.green = (png_uint_16)(r >> 16);
9273 background.blue = (png_uint_16)r;
9274 background.gray = (png_uint_16)(r >> 16);
9276 /* In earlier libpng versions, those where DIGITIZE is set, any background
9277 * gamma correction in the expand16 case was done using 8-bit gamma
9278 * correction tables, resulting in larger errors. To cope with those
9279 * cases use a 16-bit background value which will handle this gamma
9283 if (expand_16 && (do_background == PNG_BACKGROUND_GAMMA_UNIQUE ||
9284 do_background == PNG_BACKGROUND_GAMMA_FILE) &&
9285 fabs(bg*screen_gamma-1) > PNG_GAMMA_THRESHOLD)
9287 /* The background values will be looked up in an 8-bit table to do
9288 * the gamma correction, so only select values which are an exact
9289 * match for the 8-bit table entries:
9291 background.red = (png_uint_16)((background.red >> 8) * 257);
9292 background.green = (png_uint_16)((background.green >> 8) * 257);
9293 background.blue = (png_uint_16)((background.blue >> 8) * 257);
9294 background.gray = (png_uint_16)((background.gray >> 8) * 257);
9299 else /* 8 bit colors */
9301 png_uint_32 r = random_32();
9303 background.red = (png_byte)r;
9304 background.green = (png_byte)(r >> 8);
9305 background.blue = (png_byte)(r >> 16);
9306 background.gray = (png_byte)(r >> 24);
9309 background.index = 193; /* rgb(193,193,193) to detect errors */
9310 if (!(colour_type & PNG_COLOR_MASK_COLOR))
9312 /* Grayscale input, we do not convert to RGB (TBD), so we must set the
9313 * background to gray - else libpng seems to fail.
9315 background.red = background.green = background.blue = background.gray;
9318 pos = safecat(name, sizeof name, pos, "gamma ");
9319 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
9320 pos = safecat(name, sizeof name, pos, "->");
9321 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
9323 pos = safecat(name, sizeof name, pos, base);
9324 if (do_background < ALPHA_MODE_OFFSET)
9326 /* Include the background color and gamma in the name: */
9327 pos = safecat(name, sizeof name, pos, "(");
9328 /* This assumes no expand gray->rgb - the current code won't handle that!
9330 if (colour_type & PNG_COLOR_MASK_COLOR)
9332 pos = safecatn(name, sizeof name, pos, background.red);
9333 pos = safecat(name, sizeof name, pos, ",");
9334 pos = safecatn(name, sizeof name, pos, background.green);
9335 pos = safecat(name, sizeof name, pos, ",");
9336 pos = safecatn(name, sizeof name, pos, background.blue);
9339 pos = safecatn(name, sizeof name, pos, background.gray);
9340 pos = safecat(name, sizeof name, pos, ")^");
9341 pos = safecatd(name, sizeof name, pos, bg, 3);
9344 gamma_test(pm, colour_type, bit_depth, palette_number, interlace_type,
9345 file_gamma, screen_gamma, 0/*sBIT*/, 0, name, use_input_precision,
9346 0/*strip 16*/, expand_16, do_background, &background, bg);
9351 perform_gamma_composition_tests(png_modifier *pm, int do_background,
9354 png_byte colour_type = 0;
9355 png_byte bit_depth = 0;
9356 unsigned int palette_number = 0;
9358 /* Skip the non-alpha cases - there is no setting of a transparency colour at
9361 while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
9362 if ((colour_type & PNG_COLOR_MASK_ALPHA) != 0)
9366 /* Don't skip the i==j case here - it's relevant. */
9367 for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
9369 gamma_composition_test(pm, colour_type, bit_depth, palette_number,
9370 pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
9371 pm->use_input_precision, do_background, expand_16);
9378 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
9381 init_gamma_errors(png_modifier *pm)
9383 /* Use -1 to catch tests that were not actually run */
9384 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = -1.;
9385 pm->error_color_8 = -1.;
9386 pm->error_indexed = -1.;
9387 pm->error_gray_16 = pm->error_color_16 = -1.;
9391 print_one(const char *leader, double err)
9394 printf(" %s %.5f\n", leader, err);
9398 summarize_gamma_errors(png_modifier *pm, png_const_charp who, int low_bit_depth,
9404 printf("\nGamma correction with %s:\n", who);
9407 printf("\nBasic gamma correction:\n");
9411 print_one(" 2 bit gray: ", pm->error_gray_2);
9412 print_one(" 4 bit gray: ", pm->error_gray_4);
9413 print_one(" 8 bit gray: ", pm->error_gray_8);
9414 print_one(" 8 bit color:", pm->error_color_8);
9416 print_one(" indexed: ", pm->error_indexed);
9419 print_one("16 bit gray: ", pm->error_gray_16);
9420 print_one("16 bit color:", pm->error_color_16);
9426 perform_gamma_test(png_modifier *pm, int summary)
9428 /*TODO: remove this*/
9429 /* Save certain values for the temporary overrides below. */
9430 unsigned int calculations_use_input_precision =
9431 pm->calculations_use_input_precision;
9432 # ifdef PNG_READ_BACKGROUND_SUPPORTED
9433 double maxout8 = pm->maxout8;
9436 /* First some arbitrary no-transform tests: */
9437 if (!pm->this.speed && pm->test_gamma_threshold)
9439 perform_gamma_threshold_tests(pm);
9445 /* Now some real transforms. */
9446 if (pm->test_gamma_transform)
9451 printf("Gamma correction error summary\n\n");
9452 printf("The printed value is the maximum error in the pixel values\n");
9453 printf("calculated by the libpng gamma correction code. The error\n");
9454 printf("is calculated as the difference between the output pixel\n");
9455 printf("value (always an integer) and the ideal value from the\n");
9456 printf("libpng specification (typically not an integer).\n\n");
9458 printf("Expect this value to be less than .5 for 8 bit formats,\n");
9459 printf("less than 1 for formats with fewer than 8 bits and a small\n");
9460 printf("number (typically less than 5) for the 16 bit formats.\n");
9461 printf("For performance reasons the value for 16 bit formats\n");
9462 printf("increases when the image file includes an sBIT chunk.\n");
9466 init_gamma_errors(pm);
9467 /*TODO: remove this. Necessary because the current libpng
9468 * implementation works in 8 bits:
9470 if (pm->test_gamma_expand16)
9471 pm->calculations_use_input_precision = 1;
9472 perform_gamma_transform_tests(pm);
9473 if (!calculations_use_input_precision)
9474 pm->calculations_use_input_precision = 0;
9477 summarize_gamma_errors(pm, 0/*who*/, 1/*low bit depth*/, 1/*indexed*/);
9483 /* The sbit tests produce much larger errors: */
9484 if (pm->test_gamma_sbit)
9486 init_gamma_errors(pm);
9487 perform_gamma_sbit_tests(pm);
9490 summarize_gamma_errors(pm, "sBIT", pm->sbitlow < 8U, 1/*indexed*/);
9496 #ifdef DO_16BIT /* Should be READ_16BIT_SUPPORTED */
9497 if (pm->test_gamma_scale16)
9499 /* The 16 to 8 bit strip operations: */
9500 init_gamma_errors(pm);
9501 perform_gamma_scale16_tests(pm);
9506 printf("\nGamma correction with 16 to 8 bit reduction:\n");
9507 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
9508 printf(" 16 bit color: %.5f\n", pm->error_color_16);
9517 #ifdef PNG_READ_BACKGROUND_SUPPORTED
9518 if (pm->test_gamma_background)
9520 init_gamma_errors(pm);
9522 /*TODO: remove this. Necessary because the current libpng
9523 * implementation works in 8 bits:
9525 if (pm->test_gamma_expand16)
9527 pm->calculations_use_input_precision = 1;
9528 pm->maxout8 = .499; /* because the 16 bit background is smashed */
9530 perform_gamma_composition_tests(pm, PNG_BACKGROUND_GAMMA_UNIQUE,
9531 pm->test_gamma_expand16);
9532 if (!calculations_use_input_precision)
9533 pm->calculations_use_input_precision = 0;
9534 pm->maxout8 = maxout8;
9537 summarize_gamma_errors(pm, "background", 1, 0/*indexed*/);
9544 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9545 if (pm->test_gamma_alpha_mode)
9549 init_gamma_errors(pm);
9551 /*TODO: remove this. Necessary because the current libpng
9552 * implementation works in 8 bits:
9554 if (pm->test_gamma_expand16)
9555 pm->calculations_use_input_precision = 1;
9556 for (do_background = ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD;
9557 do_background <= ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN && !fail(pm);
9559 perform_gamma_composition_tests(pm, do_background,
9560 pm->test_gamma_expand16);
9561 if (!calculations_use_input_precision)
9562 pm->calculations_use_input_precision = 0;
9565 summarize_gamma_errors(pm, "alpha mode", 1, 0/*indexed*/);
9572 #endif /* PNG_READ_GAMMA_SUPPORTED */
9573 #endif /* PNG_READ_SUPPORTED */
9575 /* INTERLACE MACRO VALIDATION */
9576 /* This is copied verbatim from the specification, it is simply the pass
9577 * number in which each pixel in each 8x8 tile appears. The array must
9578 * be indexed adam7[y][x] and notice that the pass numbers are based at
9579 * 1, not 0 - the base libpng uses.
9582 png_byte adam7[8][8] =
9584 { 1,6,4,6,2,6,4,6 },
9585 { 7,7,7,7,7,7,7,7 },
9586 { 5,6,5,6,5,6,5,6 },
9587 { 7,7,7,7,7,7,7,7 },
9588 { 3,6,4,6,3,6,4,6 },
9589 { 7,7,7,7,7,7,7,7 },
9590 { 5,6,5,6,5,6,5,6 },
9594 /* This routine validates all the interlace support macros in png.h for
9595 * a variety of valid PNG widths and heights. It uses a number of similarly
9596 * named internal routines that feed off the above array.
9599 png_pass_start_row(int pass)
9603 for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9609 png_pass_start_col(int pass)
9613 for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9619 png_pass_row_shift(int pass)
9621 int x, y, base=(-1), inc=8;
9623 for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9629 else if (inc == y-base)
9632 inc = y-base, base=y;
9633 else if (inc != y-base)
9634 return 0xff; /* error - more than one 'inc' value! */
9637 if (base == (-1)) return 0xfe; /* error - no row in pass! */
9639 /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9648 /* error - unrecognized 'inc' */
9649 return (inc << 8) + 0xfd;
9653 png_pass_col_shift(int pass)
9655 int x, y, base=(-1), inc=8;
9657 for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9663 else if (inc == x-base)
9666 inc = x-base, base=x;
9667 else if (inc != x-base)
9668 return 0xff; /* error - more than one 'inc' value! */
9671 if (base == (-1)) return 0xfe; /* error - no row in pass! */
9673 /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9676 case 1: return 0; /* pass 7 has all the columns */
9683 /* error - unrecognized 'inc' */
9684 return (inc << 8) + 0xfd;
9688 png_row_from_pass_row(png_uint_32 yIn, int pass)
9690 /* By examination of the array: */
9693 case 0: return yIn * 8;
9694 case 1: return yIn * 8;
9695 case 2: return yIn * 8 + 4;
9696 case 3: return yIn * 4;
9697 case 4: return yIn * 4 + 2;
9698 case 5: return yIn * 2;
9699 case 6: return yIn * 2 + 1;
9703 return 0xff; /* bad pass number */
9707 png_col_from_pass_col(png_uint_32 xIn, int pass)
9709 /* By examination of the array: */
9712 case 0: return xIn * 8;
9713 case 1: return xIn * 8 + 4;
9714 case 2: return xIn * 4;
9715 case 3: return xIn * 4 + 2;
9716 case 4: return xIn * 2;
9717 case 5: return xIn * 2 + 1;
9722 return 0xff; /* bad pass number */
9726 png_row_in_interlace_pass(png_uint_32 y, int pass)
9728 /* Is row 'y' in pass 'pass'? */
9732 for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9739 png_col_in_interlace_pass(png_uint_32 x, int pass)
9741 /* Is column 'x' in pass 'pass'? */
9745 for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9752 png_pass_rows(png_uint_32 height, int pass)
9754 png_uint_32 tiles = height>>3;
9755 png_uint_32 rows = 0;
9760 for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
9763 if (y < height) ++rows;
9764 break; /* i.e. break the 'x', column, loop. */
9771 png_pass_cols(png_uint_32 width, int pass)
9773 png_uint_32 tiles = width>>3;
9774 png_uint_32 cols = 0;
9779 for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
9782 if (x < width) ++cols;
9783 break; /* i.e. break the 'y', row, loop. */
9790 perform_interlace_macro_validation(void)
9792 /* The macros to validate, first those that depend only on pass:
9794 * PNG_PASS_START_ROW(pass)
9795 * PNG_PASS_START_COL(pass)
9796 * PNG_PASS_ROW_SHIFT(pass)
9797 * PNG_PASS_COL_SHIFT(pass)
9801 for (pass=0; pass<7; ++pass)
9803 png_uint_32 m, f, v;
9805 m = PNG_PASS_START_ROW(pass);
9806 f = png_pass_start_row(pass);
9809 fprintf(stderr, "PNG_PASS_START_ROW(%d) = %u != %x\n", pass, m, f);
9813 m = PNG_PASS_START_COL(pass);
9814 f = png_pass_start_col(pass);
9817 fprintf(stderr, "PNG_PASS_START_COL(%d) = %u != %x\n", pass, m, f);
9821 m = PNG_PASS_ROW_SHIFT(pass);
9822 f = png_pass_row_shift(pass);
9825 fprintf(stderr, "PNG_PASS_ROW_SHIFT(%d) = %u != %x\n", pass, m, f);
9829 m = PNG_PASS_COL_SHIFT(pass);
9830 f = png_pass_col_shift(pass);
9833 fprintf(stderr, "PNG_PASS_COL_SHIFT(%d) = %u != %x\n", pass, m, f);
9837 /* Macros that depend on the image or sub-image height too:
9839 * PNG_PASS_ROWS(height, pass)
9840 * PNG_PASS_COLS(width, pass)
9841 * PNG_ROW_FROM_PASS_ROW(yIn, pass)
9842 * PNG_COL_FROM_PASS_COL(xIn, pass)
9843 * PNG_ROW_IN_INTERLACE_PASS(y, pass)
9844 * PNG_COL_IN_INTERLACE_PASS(x, pass)
9848 /* First the base 0 stuff: */
9849 m = PNG_ROW_FROM_PASS_ROW(v, pass);
9850 f = png_row_from_pass_row(v, pass);
9853 fprintf(stderr, "PNG_ROW_FROM_PASS_ROW(%u, %d) = %u != %x\n",
9858 m = PNG_COL_FROM_PASS_COL(v, pass);
9859 f = png_col_from_pass_col(v, pass);
9862 fprintf(stderr, "PNG_COL_FROM_PASS_COL(%u, %d) = %u != %x\n",
9867 m = PNG_ROW_IN_INTERLACE_PASS(v, pass);
9868 f = png_row_in_interlace_pass(v, pass);
9871 fprintf(stderr, "PNG_ROW_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9876 m = PNG_COL_IN_INTERLACE_PASS(v, pass);
9877 f = png_col_in_interlace_pass(v, pass);
9880 fprintf(stderr, "PNG_COL_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9885 /* Then the base 1 stuff: */
9887 m = PNG_PASS_ROWS(v, pass);
9888 f = png_pass_rows(v, pass);
9891 fprintf(stderr, "PNG_PASS_ROWS(%u, %d) = %u != %x\n",
9896 m = PNG_PASS_COLS(v, pass);
9897 f = png_pass_cols(v, pass);
9900 fprintf(stderr, "PNG_PASS_COLS(%u, %d) = %u != %x\n",
9905 /* Move to the next v - the stepping algorithm starts skipping
9906 * values above 1024.
9910 if (v == PNG_UINT_31_MAX)
9914 if (v >= PNG_UINT_31_MAX)
9915 v = PNG_UINT_31_MAX-1;
9921 /* Test color encodings. These values are back-calculated from the published
9922 * chromaticities. The values are accurate to about 14 decimal places; 15 are
9923 * given. These values are much more accurate than the ones given in the spec,
9924 * which typically don't exceed 4 decimal places. This allows testing of the
9925 * libpng code to its theoretical accuracy of 4 decimal places. (If pngvalid
9926 * used the published errors the 'slack' permitted would have to be +/-.5E-4 or
9929 * The png_modifier code assumes that encodings[0] is sRGB and treats it
9930 * specially: do not change the first entry in this list!
9932 static PNG_CONST color_encoding test_encodings[] =
9934 /* sRGB: must be first in this list! */
9936 /*red: */ { 0.412390799265959, 0.212639005871510, 0.019330818715592 },
9937 /*green:*/ { 0.357584339383878, 0.715168678767756, 0.119194779794626 },
9938 /*blue: */ { 0.180480788401834, 0.072192315360734, 0.950532152249660} },
9939 /* Kodak ProPhoto (wide gamut) */
9940 /*gamma:*/ { 1/1.6 /*approximate: uses 1.8 power law compared to sRGB 2.4*/,
9941 /*red: */ { 0.797760489672303, 0.288071128229293, 0.000000000000000 },
9942 /*green:*/ { 0.135185837175740, 0.711843217810102, 0.000000000000000 },
9943 /*blue: */ { 0.031349349581525, 0.000085653960605, 0.825104602510460} },
9944 /* Adobe RGB (1998) */
9945 /*gamma:*/ { 1/(2+51./256),
9946 /*red: */ { 0.576669042910131, 0.297344975250536, 0.027031361386412 },
9947 /*green:*/ { 0.185558237906546, 0.627363566255466, 0.070688852535827 },
9948 /*blue: */ { 0.188228646234995, 0.075291458493998, 0.991337536837639} },
9949 /* Adobe Wide Gamut RGB */
9950 /*gamma:*/ { 1/(2+51./256),
9951 /*red: */ { 0.716500716779386, 0.258728243040113, 0.000000000000000 },
9952 /*green:*/ { 0.101020574397477, 0.724682314948566, 0.051211818965388 },
9953 /*blue: */ { 0.146774385252705, 0.016589442011321, 0.773892783545073} },
9958 * This attempts to trap signals and escape without crashing. It needs a
9959 * context pointer so that it can throw an exception (call longjmp) to recover
9960 * from the condition; this is handled by making the png_modifier used by 'main'
9961 * into a global variable.
9963 static png_modifier pm;
9965 static void signal_handler(int signum)
9971 pos = safecat(msg, sizeof msg, pos, "caught signal: ");
9976 pos = safecat(msg, sizeof msg, pos, "abort");
9980 pos = safecat(msg, sizeof msg, pos, "floating point exception");
9984 pos = safecat(msg, sizeof msg, pos, "illegal instruction");
9988 pos = safecat(msg, sizeof msg, pos, "interrupt");
9992 pos = safecat(msg, sizeof msg, pos, "invalid memory access");
9996 pos = safecat(msg, sizeof msg, pos, "termination request");
10000 pos = safecat(msg, sizeof msg, pos, "unknown ");
10001 pos = safecatn(msg, sizeof msg, pos, signum);
10005 store_log(&pm.this, NULL/*png_structp*/, msg, 1/*error*/);
10007 /* And finally throw an exception so we can keep going, unless this is
10008 * SIGTERM in which case stop now.
10010 if (signum != SIGTERM)
10012 struct exception_context *the_exception_context =
10013 &pm.this.exception_context;
10023 int main(int argc, char **argv)
10025 volatile int summary = 1; /* Print the error summary at the end */
10026 volatile int memstats = 0; /* Print memory statistics at the end */
10028 /* Create the given output file on success: */
10029 PNG_CONST char *volatile touch = NULL;
10031 /* This is an array of standard gamma values (believe it or not I've seen
10032 * every one of these mentioned somewhere.)
10034 * In the following list the most useful values are first!
10037 gammas[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9};
10039 /* This records the command and arguments: */
10041 char command[1024];
10043 anon_context(&pm.this);
10045 /* Add appropriate signal handlers, just the ANSI specified ones: */
10046 signal(SIGABRT, signal_handler);
10047 signal(SIGFPE, signal_handler);
10048 signal(SIGILL, signal_handler);
10049 signal(SIGINT, signal_handler);
10050 signal(SIGSEGV, signal_handler);
10051 signal(SIGTERM, signal_handler);
10053 #ifdef HAVE_FEENABLEEXCEPT
10054 /* Only required to enable FP exceptions on platforms where they start off
10055 * disabled; this is not necessary but if it is not done pngvalid will likely
10056 * end up ignoring FP conditions that other platforms fault.
10058 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
10061 modifier_init(&pm);
10063 /* Preallocate the image buffer, because we know how big it needs to be,
10064 * note that, for testing purposes, it is deliberately mis-aligned by tag
10065 * bytes either side. All rows have an additional five bytes of padding for
10066 * overwrite checking.
10068 store_ensure_image(&pm.this, NULL, 2, TRANSFORM_ROWMAX, TRANSFORM_HEIGHTMAX);
10070 /* Don't give argv[0], it's normally some horrible libtool string: */
10071 cp = safecat(command, sizeof command, cp, "pngvalid");
10073 /* Default to error on warning: */
10074 pm.this.treat_warnings_as_errors = 1;
10076 /* Default assume_16_bit_calculations appropriately; this tells the checking
10077 * code that 16-bit arithmetic is used for 8-bit samples when it would make a
10080 pm.assume_16_bit_calculations = PNG_LIBPNG_VER >= 10700;
10082 /* Currently 16 bit expansion happens at the end of the pipeline, so the
10083 * calculations are done in the input bit depth not the output.
10087 pm.calculations_use_input_precision = 1U;
10089 /* Store the test gammas */
10090 pm.gammas = gammas;
10091 pm.ngammas = (sizeof gammas) / (sizeof gammas[0]);
10092 pm.ngamma_tests = 0; /* default to off */
10094 /* And the test encodings */
10095 pm.encodings = test_encodings;
10096 pm.nencodings = (sizeof test_encodings) / (sizeof test_encodings[0]);
10098 pm.sbitlow = 8U; /* because libpng doesn't do sBIT below 8! */
10100 /* The following allows results to pass if they correspond to anything in the
10101 * transformed range [input-.5,input+.5]; this is is required because of the
10102 * way libpng treates the 16_TO_8 flag when building the gamma tables in
10103 * releases up to 1.6.0.
10105 * TODO: review this
10107 pm.use_input_precision_16to8 = 1U;
10108 pm.use_input_precision_sbit = 1U; /* because libpng now rounds sBIT */
10110 /* Some default values (set the behavior for 'make check' here).
10111 * These values simply control the maximum error permitted in the gamma
10112 * transformations. The practial limits for human perception are described
10113 * below (the setting for maxpc16), however for 8 bit encodings it isn't
10114 * possible to meet the accepted capabilities of human vision - i.e. 8 bit
10115 * images can never be good enough, regardless of encoding.
10117 pm.maxout8 = .1; /* Arithmetic error in *encoded* value */
10118 pm.maxabs8 = .00005; /* 1/20000 */
10119 pm.maxcalc8 = 1./255; /* +/-1 in 8 bits for compose errors */
10120 pm.maxpc8 = .499; /* I.e., .499% fractional error */
10121 pm.maxout16 = .499; /* Error in *encoded* value */
10122 pm.maxabs16 = .00005;/* 1/20000 */
10123 pm.maxcalc16 =1./65535;/* +/-1 in 16 bits for compose errors */
10124 pm.maxcalcG = 1./((1<<PNG_MAX_GAMMA_8)-1);
10126 /* NOTE: this is a reasonable perceptual limit. We assume that humans can
10127 * perceive light level differences of 1% over a 100:1 range, so we need to
10128 * maintain 1 in 10000 accuracy (in linear light space), which is what the
10129 * following guarantees. It also allows significantly higher errors at
10130 * higher 16 bit values, which is important for performance. The actual
10131 * maximum 16 bit error is about +/-1.9 in the fixed point implementation but
10132 * this is only allowed for values >38149 by the following:
10134 pm.maxpc16 = .005; /* I.e., 1/200% - 1/20000 */
10136 /* Now parse the command line options. */
10137 while (--argc >= 1)
10139 int catmore = 0; /* Set if the argument has an argument. */
10141 /* Record each argument for posterity: */
10142 cp = safecat(command, sizeof command, cp, " ");
10143 cp = safecat(command, sizeof command, cp, *++argv);
10145 if (strcmp(*argv, "-v") == 0)
10146 pm.this.verbose = 1;
10148 else if (strcmp(*argv, "-l") == 0)
10151 else if (strcmp(*argv, "-q") == 0)
10152 summary = pm.this.verbose = pm.log = 0;
10154 else if (strcmp(*argv, "-w") == 0)
10155 pm.this.treat_warnings_as_errors = 0;
10157 else if (strcmp(*argv, "--speed") == 0)
10158 pm.this.speed = 1, pm.ngamma_tests = pm.ngammas, pm.test_standard = 0,
10161 else if (strcmp(*argv, "--memory") == 0)
10164 else if (strcmp(*argv, "--size") == 0)
10167 else if (strcmp(*argv, "--nosize") == 0)
10170 else if (strcmp(*argv, "--standard") == 0)
10171 pm.test_standard = 1;
10173 else if (strcmp(*argv, "--nostandard") == 0)
10174 pm.test_standard = 0;
10176 else if (strcmp(*argv, "--transform") == 0)
10177 pm.test_transform = 1;
10179 else if (strcmp(*argv, "--notransform") == 0)
10180 pm.test_transform = 0;
10182 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10183 else if (strncmp(*argv, "--transform-disable=",
10184 sizeof "--transform-disable") == 0)
10186 pm.test_transform = 1;
10187 transform_disable(*argv + sizeof "--transform-disable");
10190 else if (strncmp(*argv, "--transform-enable=",
10191 sizeof "--transform-enable") == 0)
10193 pm.test_transform = 1;
10194 transform_enable(*argv + sizeof "--transform-enable");
10196 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10198 else if (strcmp(*argv, "--gamma") == 0)
10200 /* Just do two gamma tests here (2.2 and linear) for speed: */
10201 pm.ngamma_tests = 2U;
10202 pm.test_gamma_threshold = 1;
10203 pm.test_gamma_transform = 1;
10204 pm.test_gamma_sbit = 1;
10205 pm.test_gamma_scale16 = 1;
10206 pm.test_gamma_background = 1;
10207 pm.test_gamma_alpha_mode = 1;
10210 else if (strcmp(*argv, "--nogamma") == 0)
10211 pm.ngamma_tests = 0;
10213 else if (strcmp(*argv, "--gamma-threshold") == 0)
10214 pm.ngamma_tests = 2U, pm.test_gamma_threshold = 1;
10216 else if (strcmp(*argv, "--nogamma-threshold") == 0)
10217 pm.test_gamma_threshold = 0;
10219 else if (strcmp(*argv, "--gamma-transform") == 0)
10220 pm.ngamma_tests = 2U, pm.test_gamma_transform = 1;
10222 else if (strcmp(*argv, "--nogamma-transform") == 0)
10223 pm.test_gamma_transform = 0;
10225 else if (strcmp(*argv, "--gamma-sbit") == 0)
10226 pm.ngamma_tests = 2U, pm.test_gamma_sbit = 1;
10228 else if (strcmp(*argv, "--nogamma-sbit") == 0)
10229 pm.test_gamma_sbit = 0;
10231 else if (strcmp(*argv, "--gamma-16-to-8") == 0)
10232 pm.ngamma_tests = 2U, pm.test_gamma_scale16 = 1;
10234 else if (strcmp(*argv, "--nogamma-16-to-8") == 0)
10235 pm.test_gamma_scale16 = 0;
10237 else if (strcmp(*argv, "--gamma-background") == 0)
10238 pm.ngamma_tests = 2U, pm.test_gamma_background = 1;
10240 else if (strcmp(*argv, "--nogamma-background") == 0)
10241 pm.test_gamma_background = 0;
10243 else if (strcmp(*argv, "--gamma-alpha-mode") == 0)
10244 pm.ngamma_tests = 2U, pm.test_gamma_alpha_mode = 1;
10246 else if (strcmp(*argv, "--nogamma-alpha-mode") == 0)
10247 pm.test_gamma_alpha_mode = 0;
10249 else if (strcmp(*argv, "--expand16") == 0)
10250 pm.test_gamma_expand16 = 1;
10252 else if (strcmp(*argv, "--noexpand16") == 0)
10253 pm.test_gamma_expand16 = 0;
10255 else if (strcmp(*argv, "--more-gammas") == 0)
10256 pm.ngamma_tests = 3U;
10258 else if (strcmp(*argv, "--all-gammas") == 0)
10259 pm.ngamma_tests = pm.ngammas;
10261 else if (strcmp(*argv, "--progressive-read") == 0)
10262 pm.this.progressive = 1;
10264 else if (strcmp(*argv, "--use-update-info") == 0)
10265 ++pm.use_update_info; /* Can call multiple times */
10267 else if (strcmp(*argv, "--interlace") == 0)
10269 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
10270 pm.interlace_type = PNG_INTERLACE_ADAM7;
10272 fprintf(stderr, "pngvalid: no write interlace support\n");
10277 else if (strcmp(*argv, "--use-input-precision") == 0)
10278 pm.use_input_precision = 1U;
10280 else if (strcmp(*argv, "--use-calculation-precision") == 0)
10281 pm.use_input_precision = 0;
10283 else if (strcmp(*argv, "--calculations-use-input-precision") == 0)
10284 pm.calculations_use_input_precision = 1U;
10286 else if (strcmp(*argv, "--assume-16-bit-calculations") == 0)
10287 pm.assume_16_bit_calculations = 1U;
10289 else if (strcmp(*argv, "--calculations-follow-bit-depth") == 0)
10290 pm.calculations_use_input_precision =
10291 pm.assume_16_bit_calculations = 0;
10293 else if (strcmp(*argv, "--exhaustive") == 0)
10294 pm.test_exhaustive = 1;
10296 else if (argc > 1 && strcmp(*argv, "--sbitlow") == 0)
10297 --argc, pm.sbitlow = (png_byte)atoi(*++argv), catmore = 1;
10299 else if (argc > 1 && strcmp(*argv, "--touch") == 0)
10300 --argc, touch = *++argv, catmore = 1;
10302 else if (argc > 1 && strncmp(*argv, "--max", 5) == 0)
10306 if (strcmp(5+*argv, "abs8") == 0)
10307 pm.maxabs8 = atof(*++argv);
10309 else if (strcmp(5+*argv, "abs16") == 0)
10310 pm.maxabs16 = atof(*++argv);
10312 else if (strcmp(5+*argv, "calc8") == 0)
10313 pm.maxcalc8 = atof(*++argv);
10315 else if (strcmp(5+*argv, "calc16") == 0)
10316 pm.maxcalc16 = atof(*++argv);
10318 else if (strcmp(5+*argv, "out8") == 0)
10319 pm.maxout8 = atof(*++argv);
10321 else if (strcmp(5+*argv, "out16") == 0)
10322 pm.maxout16 = atof(*++argv);
10324 else if (strcmp(5+*argv, "pc8") == 0)
10325 pm.maxpc8 = atof(*++argv);
10327 else if (strcmp(5+*argv, "pc16") == 0)
10328 pm.maxpc16 = atof(*++argv);
10332 fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv);
10339 else if (strcmp(*argv, "--log8") == 0)
10340 --argc, pm.log8 = atof(*++argv), catmore = 1;
10342 else if (strcmp(*argv, "--log16") == 0)
10343 --argc, pm.log16 = atof(*++argv), catmore = 1;
10345 #ifdef PNG_SET_OPTION_SUPPORTED
10346 else if (strncmp(*argv, "--option=", 9) == 0)
10348 /* Syntax of the argument is <option>:{on|off} */
10349 const char *arg = 9+*argv;
10350 unsigned char option=0, setting=0;
10352 #ifdef PNG_ARM_NEON_API_SUPPORTED
10353 if (strncmp(arg, "arm-neon:", 9) == 0)
10354 option = PNG_ARM_NEON, arg += 9;
10358 #ifdef PNG_MAXIMUM_INFLATE_WINDOW
10359 if (strncmp(arg, "max-inflate-window:", 19) == 0)
10360 option = PNG_MAXIMUM_INFLATE_WINDOW, arg += 19;
10365 fprintf(stderr, "pngvalid: %s: %s: unknown option\n", *argv, arg);
10369 if (strcmp(arg, "off") == 0)
10370 setting = PNG_OPTION_OFF;
10372 else if (strcmp(arg, "on") == 0)
10373 setting = PNG_OPTION_ON;
10378 "pngvalid: %s: %s: unknown setting (use 'on' or 'off')\n",
10383 pm.this.options[pm.this.noptions].option = option;
10384 pm.this.options[pm.this.noptions++].setting = setting;
10386 #endif /* PNG_SET_OPTION_SUPPORTED */
10390 fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv);
10394 if (catmore) /* consumed an extra *argv */
10396 cp = safecat(command, sizeof command, cp, " ");
10397 cp = safecat(command, sizeof command, cp, *argv);
10401 /* If pngvalid is run with no arguments default to a reasonable set of the
10404 if (pm.test_standard == 0 && pm.test_size == 0 && pm.test_transform == 0 &&
10405 pm.ngamma_tests == 0)
10407 /* Make this do all the tests done in the test shell scripts with the same
10408 * parameters, where possible. The limitation is that all the progressive
10409 * read and interlace stuff has to be done in separate runs, so only the
10410 * basic 'standard' and 'size' tests are done.
10412 pm.test_standard = 1;
10414 pm.test_transform = 1;
10415 pm.ngamma_tests = 2U;
10418 if (pm.ngamma_tests > 0 &&
10419 pm.test_gamma_threshold == 0 && pm.test_gamma_transform == 0 &&
10420 pm.test_gamma_sbit == 0 && pm.test_gamma_scale16 == 0 &&
10421 pm.test_gamma_background == 0 && pm.test_gamma_alpha_mode == 0)
10423 pm.test_gamma_threshold = 1;
10424 pm.test_gamma_transform = 1;
10425 pm.test_gamma_sbit = 1;
10426 pm.test_gamma_scale16 = 1;
10427 pm.test_gamma_background = 1;
10428 pm.test_gamma_alpha_mode = 1;
10431 else if (pm.ngamma_tests == 0)
10433 /* Nothing to test so turn everything off: */
10434 pm.test_gamma_threshold = 0;
10435 pm.test_gamma_transform = 0;
10436 pm.test_gamma_sbit = 0;
10437 pm.test_gamma_scale16 = 0;
10438 pm.test_gamma_background = 0;
10439 pm.test_gamma_alpha_mode = 0;
10444 /* Make useful base images */
10445 make_transform_images(&pm.this);
10447 /* Perform the standard and gamma tests. */
10448 if (pm.test_standard)
10450 perform_interlace_macro_validation();
10451 perform_formatting_test(&pm.this);
10452 # ifdef PNG_READ_SUPPORTED
10453 perform_standard_test(&pm);
10455 perform_error_test(&pm);
10458 /* Various oddly sized images: */
10461 make_size_images(&pm.this);
10462 # ifdef PNG_READ_SUPPORTED
10463 perform_size_test(&pm);
10467 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10468 /* Combinatorial transforms: */
10469 if (pm.test_transform)
10470 perform_transform_test(&pm);
10471 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10473 #ifdef PNG_READ_GAMMA_SUPPORTED
10474 if (pm.ngamma_tests > 0)
10475 perform_gamma_test(&pm, summary);
10481 fprintf(stderr, "pngvalid: test aborted (probably failed in cleanup)\n");
10482 if (!pm.this.verbose)
10484 if (pm.this.error[0] != 0)
10485 fprintf(stderr, "pngvalid: first error: %s\n", pm.this.error);
10487 fprintf(stderr, "pngvalid: run with -v to see what happened\n");
10494 printf("%s: %s (%s point arithmetic)\n",
10495 (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
10496 pm.this.nwarnings)) ? "FAIL" : "PASS",
10498 #if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500
10508 printf("Allocated memory statistics (in bytes):\n"
10509 "\tread %lu maximum single, %lu peak, %lu total\n"
10510 "\twrite %lu maximum single, %lu peak, %lu total\n",
10511 (unsigned long)pm.this.read_memory_pool.max_max,
10512 (unsigned long)pm.this.read_memory_pool.max_limit,
10513 (unsigned long)pm.this.read_memory_pool.max_total,
10514 (unsigned long)pm.this.write_memory_pool.max_max,
10515 (unsigned long)pm.this.write_memory_pool.max_limit,
10516 (unsigned long)pm.this.write_memory_pool.max_total);
10519 /* Do this here to provoke memory corruption errors in memory not directly
10520 * allocated by libpng - not a complete test, but better than nothing.
10522 store_delete(&pm.this);
10524 /* Error exit if there are any errors, and maybe if there are any
10527 if (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
10528 pm.this.nwarnings))
10530 if (!pm.this.verbose)
10531 fprintf(stderr, "pngvalid: %s\n", pm.this.error);
10533 fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors,
10534 pm.this.nwarnings);
10539 /* Success case. */
10542 FILE *fsuccess = fopen(touch, "wt");
10544 if (fsuccess != NULL)
10547 fprintf(fsuccess, "PNG validation succeeded\n");
10549 error = ferror(fsuccess);
10551 if (fclose(fsuccess) || error)
10553 fprintf(stderr, "%s: write failed\n", touch);
10560 fprintf(stderr, "%s: open failed\n", touch);
10565 /* This is required because some very minimal configurations do not use it:
10570 #else /* write or low level APIs not supported */
10574 "pngvalid: no low level write support in libpng, all tests skipped\n");
10575 /* So the test is skipped: */