2 /* pngvalid.c - validate libpng by constructing then reading png files.
4 * Last changed in libpng 1.5.18 [February 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 /* Fixups for various minimal builds */
126 #ifndef PNG_ERROR_TEXT_SUPPORTED
127 # define png_error(a,b) png_err(a)
130 /***************************** EXCEPTION HANDLING *****************************/
131 #ifdef PNG_FREESTANDING_TESTS
132 # include <cexcept.h>
134 # include "../visupng/cexcept.h"
138 # define this not_the_cpp_this
139 # define new not_the_cpp_new
140 # define voidcast(type, value) static_cast<type>(value)
142 # define voidcast(type, value) (value)
143 #endif /* __cplusplus */
146 define_exception_type(struct png_store
*);
148 /* The following are macros to reduce typing everywhere where the well known
149 * name 'the_exception_context' must be defined.
151 #define anon_context(ps) struct exception_context *the_exception_context = \
152 &(ps)->exception_context
153 #define context(ps,fault) anon_context(ps); png_store *fault
155 /******************************* UTILITIES ************************************/
156 /* Error handling is particularly problematic in production code - error
157 * handlers often themselves have bugs which lead to programs that detect
158 * minor errors crashing. The following functions deal with one very
159 * common class of errors in error handlers - attempting to format error or
160 * warning messages into buffers that are too small.
162 static size_t safecat(char *buffer
, size_t bufsize
, size_t pos
,
165 while (pos
< bufsize
&& cat
!= NULL
&& *cat
!= 0)
166 buffer
[pos
++] = *cat
++;
175 static size_t safecatn(char *buffer
, size_t bufsize
, size_t pos
, int n
)
178 sprintf(number
, "%d", n
);
179 return safecat(buffer
, bufsize
, pos
, number
);
182 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
183 static size_t safecatd(char *buffer
, size_t bufsize
, size_t pos
, double d
,
187 sprintf(number
, "%.*f", precision
, d
);
188 return safecat(buffer
, bufsize
, pos
, number
);
192 static PNG_CONST
char invalid
[] = "invalid";
193 static PNG_CONST
char sep
[] = ": ";
195 static PNG_CONST
char *colour_types
[8] =
197 "grayscale", invalid
, "truecolour", "indexed-colour",
198 "grayscale with alpha", invalid
, "truecolour with alpha", invalid
201 #ifdef PNG_READ_SUPPORTED
202 /* Convert a double precision value to fixed point. */
203 static png_fixed_point
206 d
= floor(d
* PNG_FP_1
+ .5);
207 return (png_fixed_point
)d
;
209 #endif /* PNG_READ_SUPPORTED */
211 /* Generate random bytes. This uses a boring repeatable algorithm and it
212 * is implemented here so that it gives the same set of numbers on every
213 * architecture. It's a linear congruential generator (Knuth or Sedgewick
214 * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
215 * Hill, "The Art of Electronics" (Pseudo-Random Bit Sequences and Noise
219 make_random_bytes(png_uint_32
* seed
, void* pv
, size_t size
)
221 png_uint_32 u0
= seed
[0], u1
= seed
[1];
222 png_bytep bytes
= voidcast(png_bytep
, pv
);
224 /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
225 * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
228 for (i
=0; i
<size
; ++i
)
230 /* First generate 8 new bits then shift them in at the end. */
231 png_uint_32 u
= ((u0
>> (20-8)) ^ ((u1
<< 7) | (u0
>> (32-7)))) & 0xff;
236 *bytes
++ = (png_byte
)u
;
244 make_four_random_bytes(png_uint_32
* seed
, png_bytep bytes
)
246 make_random_bytes(seed
, bytes
, 4);
249 #ifdef PNG_READ_SUPPORTED
251 randomize(void *pv
, size_t size
)
253 static png_uint_32 random_seed
[2] = {0x56789abc, 0xd};
254 make_random_bytes(random_seed
, pv
, size
);
257 #define RANDOMIZE(this) randomize(&(this), sizeof (this))
260 random_mod(unsigned int max
)
266 return x
% max
; /* 0 .. max-1 */
269 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
280 #endif /* PNG_READ_SUPPORTED */
282 /* A numeric ID based on PNG file characteristics. The 'do_interlace' field
283 * simply records whether pngvalid did the interlace itself or whether it
284 * was done by libpng. Width and height must be less than 256. 'palette' is an
285 * index of the palette to use for formats with a palette (0 otherwise.)
287 #define FILEID(col, depth, palette, interlace, width, height, do_interlace) \
288 ((png_uint_32)((col) + ((depth)<<3) + ((palette)<<8) + ((interlace)<<13) + \
289 (((do_interlace)!=0)<<15) + ((width)<<16) + ((height)<<24)))
291 #define COL_FROM_ID(id) ((png_byte)((id)& 0x7U))
292 #define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU))
293 #define PALETTE_FROM_ID(id) (((id) >> 8) & 0x1f)
294 #define INTERLACE_FROM_ID(id) ((int)(((id) >> 13) & 0x3))
295 #define DO_INTERLACE_FROM_ID(id) ((int)(((id)>>15) & 1))
296 #define WIDTH_FROM_ID(id) (((id)>>16) & 0xff)
297 #define HEIGHT_FROM_ID(id) (((id)>>24) & 0xff)
299 /* Utility to construct a standard name for a standard image. */
301 standard_name(char *buffer
, size_t bufsize
, size_t pos
, png_byte colour_type
,
302 int bit_depth
, unsigned int npalette
, int interlace_type
,
303 png_uint_32 w
, png_uint_32 h
, int do_interlace
)
305 pos
= safecat(buffer
, bufsize
, pos
, colour_types
[colour_type
]);
308 pos
= safecat(buffer
, bufsize
, pos
, "[");
309 pos
= safecatn(buffer
, bufsize
, pos
, npalette
);
310 pos
= safecat(buffer
, bufsize
, pos
, "]");
312 pos
= safecat(buffer
, bufsize
, pos
, " ");
313 pos
= safecatn(buffer
, bufsize
, pos
, bit_depth
);
314 pos
= safecat(buffer
, bufsize
, pos
, " bit");
316 if (interlace_type
!= PNG_INTERLACE_NONE
)
318 pos
= safecat(buffer
, bufsize
, pos
, " interlaced");
320 pos
= safecat(buffer
, bufsize
, pos
, "(pngvalid)");
322 pos
= safecat(buffer
, bufsize
, pos
, "(libpng)");
327 pos
= safecat(buffer
, bufsize
, pos
, " ");
328 pos
= safecatn(buffer
, bufsize
, pos
, w
);
329 pos
= safecat(buffer
, bufsize
, pos
, "x");
330 pos
= safecatn(buffer
, bufsize
, pos
, h
);
337 standard_name_from_id(char *buffer
, size_t bufsize
, size_t pos
, png_uint_32 id
)
339 return standard_name(buffer
, bufsize
, pos
, COL_FROM_ID(id
),
340 DEPTH_FROM_ID(id
), PALETTE_FROM_ID(id
), INTERLACE_FROM_ID(id
),
341 WIDTH_FROM_ID(id
), HEIGHT_FROM_ID(id
), DO_INTERLACE_FROM_ID(id
));
344 /* Convenience API and defines to list valid formats. Note that 16 bit read and
345 * write support is required to do 16 bit read tests (we must be able to make a
346 * 16 bit image to test!)
348 #ifdef PNG_WRITE_16BIT_SUPPORTED
349 # define WRITE_BDHI 4
350 # ifdef PNG_READ_16BIT_SUPPORTED
355 # define WRITE_BDHI 3
361 /* The following defines the number of different palettes to generate for
362 * each log bit depth of a colour type 3 standard image.
364 #define PALETTE_COUNT(bit_depth) ((bit_depth) > 4 ? 1U : 16U)
367 next_format(png_bytep colour_type
, png_bytep bit_depth
,
368 unsigned int* palette_number
, int no_low_depth_gray
)
373 if (no_low_depth_gray
)
381 if (*colour_type
== 3)
383 /* Add multiple palettes for colour type 3. */
384 if (++*palette_number
< PALETTE_COUNT(*bit_depth
))
390 *bit_depth
= (png_byte
)(*bit_depth
<< 1);
392 /* Palette images are restricted to 8 bit depth */
395 || (*colour_type
!= 3 && *bit_depth
<= 16)
400 /* Move to the next color type, or return 0 at the end. */
401 switch (*colour_type
)
428 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
430 sample(png_const_bytep row
, png_byte colour_type
, png_byte bit_depth
,
431 png_uint_32 x
, unsigned int sample_index
)
433 png_uint_32 bit_index
, result
;
435 /* Find a sample index for the desired sample: */
439 if ((colour_type
& 1) == 0) /* !palette */
445 bit_index
+= x
; /* Alpha channel */
447 /* Multiple channels; select one: */
448 if (colour_type
& (2+4))
449 bit_index
+= sample_index
* bit_depth
;
452 /* Return the sample from the row as an integer. */
453 row
+= bit_index
>> 3;
459 else if (bit_depth
> 8)
460 return (result
<< 8) + *++row
;
462 /* Less than 8 bits per sample. */
464 return (result
>> (8-bit_index
-bit_depth
)) & ((1U<<bit_depth
)-1);
466 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
468 /* Copy a single pixel, of a given size, from one buffer to another -
469 * while this is basically bit addressed there is an implicit assumption
470 * that pixels 8 or more bits in size are byte aligned and that pixels
471 * do not otherwise cross byte boundaries. (This is, so far as I know,
472 * universally true in bitmap computer graphics. [JCB 20101212])
474 * NOTE: The to and from buffers may be the same.
477 pixel_copy(png_bytep toBuffer
, png_uint_32 toIndex
,
478 png_const_bytep fromBuffer
, png_uint_32 fromIndex
, unsigned int pixelSize
)
480 /* Assume we can multiply by 'size' without overflow because we are
481 * just working in a single buffer.
483 toIndex
*= pixelSize
;
484 fromIndex
*= pixelSize
;
485 if (pixelSize
< 8) /* Sub-byte */
487 /* Mask to select the location of the copied pixel: */
488 unsigned int destMask
= ((1U<<pixelSize
)-1) << (8-pixelSize
-(toIndex
&7));
489 /* The following read the entire pixels and clears the extra: */
490 unsigned int destByte
= toBuffer
[toIndex
>> 3] & ~destMask
;
491 unsigned int sourceByte
= fromBuffer
[fromIndex
>> 3];
493 /* Don't rely on << or >> supporting '0' here, just in case: */
495 if (fromIndex
> 0) sourceByte
<<= fromIndex
;
496 if ((toIndex
& 7) > 0) sourceByte
>>= toIndex
& 7;
498 toBuffer
[toIndex
>> 3] = (png_byte
)(destByte
| (sourceByte
& destMask
));
500 else /* One or more bytes */
501 memmove(toBuffer
+(toIndex
>>3), fromBuffer
+(fromIndex
>>3), pixelSize
>>3);
504 #ifdef PNG_READ_SUPPORTED
505 /* Copy a complete row of pixels, taking into account potential partial
509 row_copy(png_bytep toBuffer
, png_const_bytep fromBuffer
, unsigned int bitWidth
)
511 memcpy(toBuffer
, fromBuffer
, bitWidth
>> 3);
513 if ((bitWidth
& 7) != 0)
517 toBuffer
+= bitWidth
>> 3;
518 fromBuffer
+= bitWidth
>> 3;
519 /* The remaining bits are in the top of the byte, the mask is the bits to
522 mask
= 0xff >> (bitWidth
& 7);
523 *toBuffer
= (png_byte
)((*toBuffer
& mask
) | (*fromBuffer
& ~mask
));
527 /* Compare pixels - they are assumed to start at the first byte in the
531 pixel_cmp(png_const_bytep pa
, png_const_bytep pb
, png_uint_32 bit_width
)
533 #if PNG_LIBPNG_VER < 10506
534 if (memcmp(pa
, pb
, bit_width
>>3) == 0)
538 if ((bit_width
& 7) == 0) return 0;
540 /* Ok, any differences? */
541 p
= pa
[bit_width
>> 3];
542 p
^= pb
[bit_width
>> 3];
544 if (p
== 0) return 0;
546 /* There are, but they may not be significant, remove the bits
547 * after the end (the low order bits in PNG.)
552 if (p
== 0) return 0;
555 /* From libpng-1.5.6 the overwrite should be fixed, so compare the trailing
558 if (memcmp(pa
, pb
, (bit_width
+7)>>3) == 0)
562 /* Return the index of the changed byte. */
564 png_uint_32 where
= 0;
566 while (pa
[where
] == pb
[where
]) ++where
;
570 #endif /* PNG_READ_SUPPORTED */
572 /*************************** BASIC PNG FILE WRITING ***************************/
573 /* A png_store takes data from the sequential writer or provides data
574 * to the sequential reader. It can also store the result of a PNG
575 * write for later retrieval.
577 #define STORE_BUFFER_SIZE 500 /* arbitrary */
578 typedef struct png_store_buffer
580 struct png_store_buffer
* prev
; /* NOTE: stored in reverse order */
581 png_byte buffer
[STORE_BUFFER_SIZE
];
584 #define FILE_NAME_SIZE 64
586 typedef struct store_palette_entry
/* record of a single palette entry */
592 } store_palette_entry
, store_palette
[256];
594 typedef struct png_store_file
596 struct png_store_file
* next
; /* as many as you like... */
597 char name
[FILE_NAME_SIZE
];
598 png_uint_32 id
; /* must be correct (see FILEID) */
599 png_size_t datacount
; /* In this (the last) buffer */
600 png_store_buffer data
; /* Last buffer in file */
601 int npalette
; /* Number of entries in palette */
602 store_palette_entry
* palette
; /* May be NULL */
605 /* The following is a pool of memory allocated by a single libpng read or write
608 typedef struct store_pool
610 struct png_store
*store
; /* Back pointer */
611 struct store_memory
*list
; /* List of allocated memory */
612 png_byte mark
[4]; /* Before and after data */
614 /* Statistics for this run. */
615 png_alloc_size_t max
; /* Maximum single allocation */
616 png_alloc_size_t current
; /* Current allocation */
617 png_alloc_size_t limit
; /* Highest current allocation */
618 png_alloc_size_t total
; /* Total allocation */
620 /* Overall statistics (retained across successive runs). */
621 png_alloc_size_t max_max
;
622 png_alloc_size_t max_limit
;
623 png_alloc_size_t max_total
;
626 typedef struct png_store
628 /* For cexcept.h exception handling - simply store one of these;
629 * the context is a self pointer but it may point to a different
630 * png_store (in fact it never does in this program.)
632 struct exception_context
635 unsigned int verbose
:1;
636 unsigned int treat_warnings_as_errors
:1;
637 unsigned int expect_error
:1;
638 unsigned int expect_warning
:1;
639 unsigned int saw_warning
:1;
640 unsigned int speed
:1;
641 unsigned int progressive
:1; /* use progressive read */
642 unsigned int validated
:1; /* used as a temporary flag */
645 int noptions
; /* number of options below: */
647 unsigned char option
; /* option number, 0..30 */
648 unsigned char setting
; /* setting (unset,invalid,on,off) */
650 char test
[128]; /* Name of test */
654 png_structp pread
; /* Used to read a saved file */
656 png_store_file
* current
; /* Set when reading */
657 png_store_buffer
* next
; /* Set when reading */
658 png_size_t readpos
; /* Position in *next */
659 png_byte
* image
; /* Buffer for reading interlaced images */
660 png_size_t cb_image
; /* Size of this buffer */
661 png_size_t cb_row
; /* Row size of the image(s) */
662 png_uint_32 image_h
; /* Number of rows in a single image */
663 store_pool read_memory_pool
;
666 png_store_file
* saved
;
667 png_structp pwrite
; /* Used when writing a new file */
669 png_size_t writepos
; /* Position in .new */
670 char wname
[FILE_NAME_SIZE
];
671 png_store_buffer
new; /* The end of the new PNG file being written. */
672 store_pool write_memory_pool
;
673 store_palette_entry
* palette
;
677 /* Initialization and cleanup */
679 store_pool_mark(png_bytep mark
)
681 static png_uint_32 store_seed
[2] = { 0x12345678, 1};
683 make_four_random_bytes(store_seed
, mark
);
686 #ifdef PNG_READ_SUPPORTED
687 /* Use this for random 32 bit values; this function makes sure the result is
699 store_pool_mark(mark
);
700 result
= png_get_uint_32(mark
);
706 #endif /* PNG_READ_SUPPORTED */
709 store_pool_init(png_store
*ps
, store_pool
*pool
)
711 memset(pool
, 0, sizeof *pool
);
715 pool
->max
= pool
->current
= pool
->limit
= pool
->total
= 0;
716 pool
->max_max
= pool
->max_limit
= pool
->max_total
= 0;
717 store_pool_mark(pool
->mark
);
721 store_init(png_store
* ps
)
723 memset(ps
, 0, sizeof *ps
);
724 init_exception_context(&ps
->exception_context
);
725 store_pool_init(ps
, &ps
->read_memory_pool
);
726 store_pool_init(ps
, &ps
->write_memory_pool
);
728 ps
->treat_warnings_as_errors
= 0;
729 ps
->expect_error
= 0;
730 ps
->expect_warning
= 0;
735 ps
->nerrors
= ps
->nwarnings
= 0;
738 ps
->saved
= ps
->current
= NULL
;
755 store_freebuffer(png_store_buffer
* psb
)
759 store_freebuffer(psb
->prev
);
766 store_freenew(png_store
*ps
)
768 store_freebuffer(&ps
->new);
770 if (ps
->palette
!= NULL
)
779 store_storenew(png_store
*ps
)
781 png_store_buffer
*pb
;
783 if (ps
->writepos
!= STORE_BUFFER_SIZE
)
784 png_error(ps
->pwrite
, "invalid store call");
786 pb
= voidcast(png_store_buffer
*, malloc(sizeof *pb
));
789 png_error(ps
->pwrite
, "store new: OOM");
797 store_freefile(png_store_file
**ppf
)
801 store_freefile(&(*ppf
)->next
);
803 store_freebuffer(&(*ppf
)->data
);
804 (*ppf
)->datacount
= 0;
805 if ((*ppf
)->palette
!= NULL
)
807 free((*ppf
)->palette
);
808 (*ppf
)->palette
= NULL
;
809 (*ppf
)->npalette
= 0;
816 /* Main interface to file storeage, after writing a new PNG file (see the API
817 * below) call store_storefile to store the result with the given name and id.
820 store_storefile(png_store
*ps
, png_uint_32 id
)
822 png_store_file
*pf
= voidcast(png_store_file
*, malloc(sizeof *pf
));
824 png_error(ps
->pwrite
, "storefile: OOM");
825 safecat(pf
->name
, sizeof pf
->name
, 0, ps
->wname
);
828 pf
->datacount
= ps
->writepos
;
831 pf
->palette
= ps
->palette
;
832 pf
->npalette
= ps
->npalette
;
837 pf
->next
= ps
->saved
;
841 /* Generate an error message (in the given buffer) */
843 store_message(png_store
*ps
, png_const_structp pp
, char *buffer
, size_t bufsize
,
844 size_t pos
, PNG_CONST
char *msg
)
846 if (pp
!= NULL
&& pp
== ps
->pread
)
849 pos
= safecat(buffer
, bufsize
, pos
, "read: ");
851 if (ps
->current
!= NULL
)
853 pos
= safecat(buffer
, bufsize
, pos
, ps
->current
->name
);
854 pos
= safecat(buffer
, bufsize
, pos
, sep
);
858 else if (pp
!= NULL
&& pp
== ps
->pwrite
)
861 pos
= safecat(buffer
, bufsize
, pos
, "write: ");
862 pos
= safecat(buffer
, bufsize
, pos
, ps
->wname
);
863 pos
= safecat(buffer
, bufsize
, pos
, sep
);
868 /* Neither reading nor writing (or a memory error in struct delete) */
869 pos
= safecat(buffer
, bufsize
, pos
, "pngvalid: ");
872 if (ps
->test
[0] != 0)
874 pos
= safecat(buffer
, bufsize
, pos
, ps
->test
);
875 pos
= safecat(buffer
, bufsize
, pos
, sep
);
877 pos
= safecat(buffer
, bufsize
, pos
, msg
);
881 /* Verbose output to the error stream: */
883 store_verbose(png_store
*ps
, png_const_structp pp
, png_const_charp prefix
,
884 png_const_charp message
)
889 fputs(prefix
, stderr
);
891 (void)store_message(ps
, pp
, buffer
, sizeof buffer
, 0, message
);
892 fputs(buffer
, stderr
);
896 /* Log an error or warning - the relevant count is always incremented. */
898 store_log(png_store
* ps
, png_const_structp pp
, png_const_charp message
,
901 /* The warning is copied to the error buffer if there are no errors and it is
902 * the first warning. The error is copied to the error buffer if it is the
903 * first error (overwriting any prior warnings).
905 if (is_error ?
(ps
->nerrors
)++ == 0 :
906 (ps
->nwarnings
)++ == 0 && ps
->nerrors
== 0)
907 store_message(ps
, pp
, ps
->error
, sizeof ps
->error
, 0, message
);
910 store_verbose(ps
, pp
, is_error ?
"error: " : "warning: ", message
);
913 #ifdef PNG_READ_SUPPORTED
914 /* Internal error function, called with a png_store but no libpng stuff. */
916 internal_error(png_store
*ps
, png_const_charp message
)
918 store_log(ps
, NULL
, message
, 1 /* error */);
920 /* And finally throw an exception. */
922 struct exception_context
*the_exception_context
= &ps
->exception_context
;
926 #endif /* PNG_READ_SUPPORTED */
928 /* Functions to use as PNG callbacks. */
930 store_error(png_structp ppIn
, png_const_charp message
) /* PNG_NORETURN */
932 png_const_structp pp
= ppIn
;
933 png_store
*ps
= voidcast(png_store
*, png_get_error_ptr(pp
));
935 if (!ps
->expect_error
)
936 store_log(ps
, pp
, message
, 1 /* error */);
938 /* And finally throw an exception. */
940 struct exception_context
*the_exception_context
= &ps
->exception_context
;
946 store_warning(png_structp ppIn
, png_const_charp message
)
948 png_const_structp pp
= ppIn
;
949 png_store
*ps
= voidcast(png_store
*, png_get_error_ptr(pp
));
951 if (!ps
->expect_warning
)
952 store_log(ps
, pp
, message
, 0 /* warning */);
957 /* These somewhat odd functions are used when reading an image to ensure that
958 * the buffer is big enough, the png_structp is for errors.
960 /* Return a single row from the correct image. */
962 store_image_row(PNG_CONST png_store
* ps
, png_const_structp pp
, int nImage
,
965 png_size_t coffset
= (nImage
* ps
->image_h
+ y
) * (ps
->cb_row
+ 5) + 2;
967 if (ps
->image
== NULL
)
968 png_error(pp
, "no allocated image");
970 if (coffset
+ ps
->cb_row
+ 3 > ps
->cb_image
)
971 png_error(pp
, "image too small");
973 return ps
->image
+ coffset
;
977 store_image_free(png_store
*ps
, png_const_structp pp
)
979 if (ps
->image
!= NULL
)
981 png_bytep image
= ps
->image
;
983 if (image
[-1] != 0xed || image
[ps
->cb_image
] != 0xfe)
986 png_error(pp
, "png_store image overwrite (1)");
988 store_log(ps
, NULL
, "png_store image overwrite (2)", 1);
999 store_ensure_image(png_store
*ps
, png_const_structp pp
, int nImages
,
1000 png_size_t cbRow
, png_uint_32 cRows
)
1002 png_size_t cb
= nImages
* cRows
* (cbRow
+ 5);
1004 if (ps
->cb_image
< cb
)
1008 store_image_free(ps
, pp
);
1010 /* The buffer is deliberately mis-aligned. */
1011 image
= voidcast(png_bytep
, malloc(cb
+2));
1014 /* Called from the startup - ignore the error for the moment. */
1018 png_error(pp
, "OOM allocating image buffer");
1021 /* These magic tags are used to detect overwrites above. */
1030 /* We have an adequate sized image; lay out the rows. There are 2 bytes at
1031 * the start and three at the end of each (this ensures that the row
1032 * alignment starts out odd - 2+1 and changes for larger images on each row.)
1035 ps
->image_h
= cRows
;
1037 /* For error checking, the whole buffer is set to 10110010 (0xb2 - 178).
1038 * This deliberately doesn't match the bits in the size test image which are
1039 * outside the image; these are set to 0xff (all 1). To make the row
1040 * comparison work in the 'size' test case the size rows are pre-initialized
1041 * to the same value prior to calling 'standard_row'.
1043 memset(ps
->image
, 178, cb
);
1045 /* Then put in the marks. */
1046 while (--nImages
>= 0)
1050 for (y
=0; y
<cRows
; ++y
)
1052 png_bytep row
= store_image_row(ps
, pp
, nImages
, y
);
1064 #ifdef PNG_READ_SUPPORTED
1066 store_image_check(PNG_CONST png_store
* ps
, png_const_structp pp
, int iImage
)
1068 png_const_bytep image
= ps
->image
;
1070 if (image
[-1] != 0xed || image
[ps
->cb_image
] != 0xfe)
1071 png_error(pp
, "image overwrite");
1074 png_size_t cbRow
= ps
->cb_row
;
1075 png_uint_32 rows
= ps
->image_h
;
1077 image
+= iImage
* (cbRow
+5) * ps
->image_h
;
1079 image
+= 2; /* skip image first row markers */
1083 if (image
[-2] != 190 || image
[-1] != 239)
1084 png_error(pp
, "row start overwritten");
1086 if (image
[cbRow
] != 222 || image
[cbRow
+1] != 173 ||
1087 image
[cbRow
+2] != 17)
1088 png_error(pp
, "row end overwritten");
1094 #endif /* PNG_READ_SUPPORTED */
1097 store_write(png_structp ppIn
, png_bytep pb
, png_size_t st
)
1099 png_const_structp pp
= ppIn
;
1100 png_store
*ps
= voidcast(png_store
*, png_get_io_ptr(pp
));
1102 if (ps
->pwrite
!= pp
)
1103 png_error(pp
, "store state damaged");
1109 if (ps
->writepos
>= STORE_BUFFER_SIZE
)
1114 if (cb
> STORE_BUFFER_SIZE
- ps
->writepos
)
1115 cb
= STORE_BUFFER_SIZE
- ps
->writepos
;
1117 memcpy(ps
->new.buffer
+ ps
->writepos
, pb
, cb
);
1125 store_flush(png_structp ppIn
)
1127 UNUSED(ppIn
) /*DOES NOTHING*/
1130 #ifdef PNG_READ_SUPPORTED
1132 store_read_buffer_size(png_store
*ps
)
1134 /* Return the bytes available for read in the current buffer. */
1135 if (ps
->next
!= &ps
->current
->data
)
1136 return STORE_BUFFER_SIZE
;
1138 return ps
->current
->datacount
;
1141 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1142 /* Return total bytes available for read. */
1144 store_read_buffer_avail(png_store
*ps
)
1146 if (ps
->current
!= NULL
&& ps
->next
!= NULL
)
1148 png_store_buffer
*next
= &ps
->current
->data
;
1149 size_t cbAvail
= ps
->current
->datacount
;
1151 while (next
!= ps
->next
&& next
!= NULL
)
1154 cbAvail
+= STORE_BUFFER_SIZE
;
1157 if (next
!= ps
->next
)
1158 png_error(ps
->pread
, "buffer read error");
1160 if (cbAvail
> ps
->readpos
)
1161 return cbAvail
- ps
->readpos
;
1169 store_read_buffer_next(png_store
*ps
)
1171 png_store_buffer
*pbOld
= ps
->next
;
1172 png_store_buffer
*pbNew
= &ps
->current
->data
;
1175 while (pbNew
!= NULL
&& pbNew
->prev
!= pbOld
)
1176 pbNew
= pbNew
->prev
;
1185 png_error(ps
->pread
, "buffer lost");
1188 return 0; /* EOF or error */
1191 /* Need separate implementation and callback to allow use of the same code
1192 * during progressive read, where the io_ptr is set internally by libpng.
1195 store_read_imp(png_store
*ps
, png_bytep pb
, png_size_t st
)
1197 if (ps
->current
== NULL
|| ps
->next
== NULL
)
1198 png_error(ps
->pread
, "store state damaged");
1202 size_t cbAvail
= store_read_buffer_size(ps
) - ps
->readpos
;
1206 if (cbAvail
> st
) cbAvail
= st
;
1207 memcpy(pb
, ps
->next
->buffer
+ ps
->readpos
, cbAvail
);
1210 ps
->readpos
+= cbAvail
;
1213 else if (!store_read_buffer_next(ps
))
1214 png_error(ps
->pread
, "read beyond end of file");
1219 store_read(png_structp ppIn
, png_bytep pb
, png_size_t st
)
1221 png_const_structp pp
= ppIn
;
1222 png_store
*ps
= voidcast(png_store
*, png_get_io_ptr(pp
));
1224 if (ps
== NULL
|| ps
->pread
!= pp
)
1225 png_error(pp
, "bad store read call");
1227 store_read_imp(ps
, pb
, st
);
1231 store_progressive_read(png_store
*ps
, png_structp pp
, png_infop pi
)
1233 /* Notice that a call to store_read will cause this function to fail because
1234 * readpos will be set.
1236 if (ps
->pread
!= pp
|| ps
->current
== NULL
|| ps
->next
== NULL
)
1237 png_error(pp
, "store state damaged (progressive)");
1241 if (ps
->readpos
!= 0)
1242 png_error(pp
, "store_read called during progressive read");
1244 png_process_data(pp
, pi
, ps
->next
->buffer
, store_read_buffer_size(ps
));
1246 while (store_read_buffer_next(ps
));
1248 #endif /* PNG_READ_SUPPORTED */
1250 /* The caller must fill this in: */
1251 static store_palette_entry
*
1252 store_write_palette(png_store
*ps
, int npalette
)
1254 if (ps
->pwrite
== NULL
)
1255 store_log(ps
, NULL
, "attempt to write palette without write stream", 1);
1257 if (ps
->palette
!= NULL
)
1258 png_error(ps
->pwrite
, "multiple store_write_palette calls");
1260 /* This function can only return NULL if called with '0'! */
1263 ps
->palette
= voidcast(store_palette_entry
*, malloc(npalette
*
1264 sizeof *ps
->palette
));
1266 if (ps
->palette
== NULL
)
1267 png_error(ps
->pwrite
, "store new palette: OOM");
1269 ps
->npalette
= npalette
;
1275 #ifdef PNG_READ_SUPPORTED
1276 static store_palette_entry
*
1277 store_current_palette(png_store
*ps
, int *npalette
)
1279 /* This is an internal error (the call has been made outside a read
1282 if (ps
->current
== NULL
)
1283 store_log(ps
, ps
->pread
, "no current stream for palette", 1);
1285 /* The result may be null if there is no palette. */
1286 *npalette
= ps
->current
->npalette
;
1287 return ps
->current
->palette
;
1289 #endif /* PNG_READ_SUPPORTED */
1291 /***************************** MEMORY MANAGEMENT*** ***************************/
1292 #ifdef PNG_USER_MEM_SUPPORTED
1293 /* A store_memory is simply the header for an allocated block of memory. The
1294 * pointer returned to libpng is just after the end of the header block, the
1295 * allocated memory is followed by a second copy of the 'mark'.
1297 typedef struct store_memory
1299 store_pool
*pool
; /* Originating pool */
1300 struct store_memory
*next
; /* Singly linked list */
1301 png_alloc_size_t size
; /* Size of memory allocated */
1302 png_byte mark
[4]; /* ID marker */
1305 /* Handle a fatal error in memory allocation. This calls png_error if the
1306 * libpng struct is non-NULL, else it outputs a message and returns. This means
1307 * that a memory problem while libpng is running will abort (png_error) the
1308 * handling of particular file while one in cleanup (after the destroy of the
1309 * struct has returned) will simply keep going and free (or attempt to free)
1313 store_pool_error(png_store
*ps
, png_const_structp pp
, PNG_CONST
char *msg
)
1318 /* Else we have to do it ourselves. png_error eventually calls store_log,
1319 * above. store_log accepts a NULL png_structp - it just changes what gets
1320 * output by store_message.
1322 store_log(ps
, pp
, msg
, 1 /* error */);
1326 store_memory_free(png_const_structp pp
, store_pool
*pool
, store_memory
*memory
)
1328 /* Note that pp may be NULL (see store_pool_delete below), the caller has
1329 * found 'memory' in pool->list *and* unlinked this entry, so this is a valid
1330 * pointer (for sure), but the contents may have been trashed.
1332 if (memory
->pool
!= pool
)
1333 store_pool_error(pool
->store
, pp
, "memory corrupted (pool)");
1335 else if (memcmp(memory
->mark
, pool
->mark
, sizeof memory
->mark
) != 0)
1336 store_pool_error(pool
->store
, pp
, "memory corrupted (start)");
1338 /* It should be safe to read the size field now. */
1341 png_alloc_size_t cb
= memory
->size
;
1344 store_pool_error(pool
->store
, pp
, "memory corrupted (size)");
1346 else if (memcmp((png_bytep
)(memory
+1)+cb
, pool
->mark
, sizeof pool
->mark
)
1348 store_pool_error(pool
->store
, pp
, "memory corrupted (end)");
1350 /* Finally give the library a chance to find problems too: */
1353 pool
->current
-= cb
;
1360 store_pool_delete(png_store
*ps
, store_pool
*pool
)
1362 if (pool
->list
!= NULL
)
1364 fprintf(stderr
, "%s: %s %s: memory lost (list follows):\n", ps
->test
,
1365 pool
== &ps
->read_memory_pool ?
"read" : "write",
1366 pool
== &ps
->read_memory_pool ?
(ps
->current
!= NULL ?
1367 ps
->current
->name
: "unknown file") : ps
->wname
);
1372 store_memory
*next
= pool
->list
;
1373 pool
->list
= next
->next
;
1376 fprintf(stderr
, "\t%lu bytes @ %p\n",
1377 (unsigned long)next
->size
, (PNG_CONST
void*)(next
+1));
1378 /* The NULL means this will always return, even if the memory is
1381 store_memory_free(NULL
, pool
, next
);
1383 while (pool
->list
!= NULL
);
1386 /* And reset the other fields too for the next time. */
1387 if (pool
->max
> pool
->max_max
) pool
->max_max
= pool
->max
;
1389 if (pool
->current
!= 0) /* unexpected internal error */
1390 fprintf(stderr
, "%s: %s %s: memory counter mismatch (internal error)\n",
1391 ps
->test
, pool
== &ps
->read_memory_pool ?
"read" : "write",
1392 pool
== &ps
->read_memory_pool ?
(ps
->current
!= NULL ?
1393 ps
->current
->name
: "unknown file") : ps
->wname
);
1396 if (pool
->limit
> pool
->max_limit
)
1397 pool
->max_limit
= pool
->limit
;
1401 if (pool
->total
> pool
->max_total
)
1402 pool
->max_total
= pool
->total
;
1406 /* Get a new mark too. */
1407 store_pool_mark(pool
->mark
);
1410 /* The memory callbacks: */
1412 store_malloc(png_structp ppIn
, png_alloc_size_t cb
)
1414 png_const_structp pp
= ppIn
;
1415 store_pool
*pool
= voidcast(store_pool
*, png_get_mem_ptr(pp
));
1416 store_memory
*new = voidcast(store_memory
*, malloc(cb
+ (sizeof *new) +
1417 (sizeof pool
->mark
)));
1424 pool
->current
+= cb
;
1426 if (pool
->current
> pool
->limit
)
1427 pool
->limit
= pool
->current
;
1432 memcpy(new->mark
, pool
->mark
, sizeof new->mark
);
1433 memcpy((png_byte
*)(new+1) + cb
, pool
->mark
, sizeof pool
->mark
);
1435 new->next
= pool
->list
;
1442 /* NOTE: the PNG user malloc function cannot use the png_ptr it is passed
1443 * other than to retrieve the allocation pointer! libpng calls the
1444 * store_malloc callback in two basic cases:
1446 * 1) From png_malloc; png_malloc will do a png_error itself if NULL is
1448 * 2) From png_struct or png_info structure creation; png_malloc is
1449 * to return so cleanup can be performed.
1451 * To handle this store_malloc can log a message, but can't do anything
1454 store_log(pool
->store
, pp
, "out of memory", 1 /* is_error */);
1461 store_free(png_structp ppIn
, png_voidp memory
)
1463 png_const_structp pp
= ppIn
;
1464 store_pool
*pool
= voidcast(store_pool
*, png_get_mem_ptr(pp
));
1465 store_memory
*this = voidcast(store_memory
*, memory
), **test
;
1467 /* Because libpng calls store_free with a dummy png_struct when deleting
1468 * png_struct or png_info via png_destroy_struct_2 it is necessary to check
1469 * the passed in png_structp to ensure it is valid, and not pass it to
1470 * png_error if it is not.
1472 if (pp
!= pool
->store
->pread
&& pp
!= pool
->store
->pwrite
)
1475 /* First check that this 'memory' really is valid memory - it must be in the
1476 * pool list. If it is, use the shared memory_free function to free it.
1479 for (test
= &pool
->list
; *test
!= this; test
= &(*test
)->next
)
1483 store_pool_error(pool
->store
, pp
, "bad pointer to free");
1488 /* Unlink this entry, *test == this. */
1491 store_memory_free(pp
, pool
, this);
1493 #endif /* PNG_USER_MEM_SUPPORTED */
1495 /* Setup functions. */
1496 /* Cleanup when aborting a write or after storing the new file. */
1498 store_write_reset(png_store
*ps
)
1500 if (ps
->pwrite
!= NULL
)
1505 png_destroy_write_struct(&ps
->pwrite
, &ps
->piwrite
);
1509 /* memory corruption: continue. */
1516 /* And make sure that all the memory has been freed - this will output
1517 * spurious errors in the case of memory corruption above, but this is safe.
1519 # ifdef PNG_USER_MEM_SUPPORTED
1520 store_pool_delete(ps
, &ps
->write_memory_pool
);
1526 /* The following is the main write function, it returns a png_struct and,
1527 * optionally, a png_info suitable for writiing a new PNG file. Use
1528 * store_storefile above to record this file after it has been written. The
1529 * returned libpng structures as destroyed by store_write_reset above.
1532 set_store_for_write(png_store
*ps
, png_infopp ppi
,
1533 PNG_CONST
char * volatile name
)
1539 if (ps
->pwrite
!= NULL
)
1540 png_error(ps
->pwrite
, "write store already in use");
1542 store_write_reset(ps
);
1543 safecat(ps
->wname
, sizeof ps
->wname
, 0, name
);
1545 /* Don't do the slow memory checks if doing a speed test, also if user
1546 * memory is not supported we can't do it anyway.
1548 # ifdef PNG_USER_MEM_SUPPORTED
1550 ps
->pwrite
= png_create_write_struct_2(PNG_LIBPNG_VER_STRING
,
1551 ps
, store_error
, store_warning
, &ps
->write_memory_pool
,
1552 store_malloc
, store_free
);
1556 ps
->pwrite
= png_create_write_struct(PNG_LIBPNG_VER_STRING
,
1557 ps
, store_error
, store_warning
);
1559 png_set_write_fn(ps
->pwrite
, ps
, store_write
, store_flush
);
1561 # ifdef PNG_SET_OPTION_SUPPORTED
1564 for (opt
=0; opt
<ps
->noptions
; ++opt
)
1565 if (png_set_option(ps
->pwrite
, ps
->options
[opt
].option
,
1566 ps
->options
[opt
].setting
) == PNG_OPTION_INVALID
)
1567 png_error(ps
->pwrite
, "png option invalid");
1572 *ppi
= ps
->piwrite
= png_create_info_struct(ps
->pwrite
);
1581 /* Cleanup when finished reading (either due to error or in the success case).
1582 * This routine exists even when there is no read support to make the code
1583 * tidier (avoid a mass of ifdefs) and so easier to maintain.
1586 store_read_reset(png_store
*ps
)
1588 # ifdef PNG_READ_SUPPORTED
1589 if (ps
->pread
!= NULL
)
1594 png_destroy_read_struct(&ps
->pread
, &ps
->piread
, NULL
);
1598 /* error already output: continue */
1606 # ifdef PNG_USER_MEM_SUPPORTED
1607 /* Always do this to be safe. */
1608 store_pool_delete(ps
, &ps
->read_memory_pool
);
1617 #ifdef PNG_READ_SUPPORTED
1619 store_read_set(png_store
*ps
, png_uint_32 id
)
1621 png_store_file
*pf
= ps
->saved
;
1629 store_read_buffer_next(ps
);
1638 char msg
[FILE_NAME_SIZE
+64];
1640 pos
= standard_name_from_id(msg
, sizeof msg
, 0, id
);
1641 pos
= safecat(msg
, sizeof msg
, pos
, ": file not found");
1642 png_error(ps
->pread
, msg
);
1646 /* The main interface for reading a saved file - pass the id number of the file
1647 * to retrieve. Ids must be unique or the earlier file will be hidden. The API
1648 * returns a png_struct and, optionally, a png_info. Both of these will be
1649 * destroyed by store_read_reset above.
1652 set_store_for_read(png_store
*ps
, png_infopp ppi
, png_uint_32 id
,
1653 PNG_CONST
char *name
)
1655 /* Set the name for png_error */
1656 safecat(ps
->test
, sizeof ps
->test
, 0, name
);
1658 if (ps
->pread
!= NULL
)
1659 png_error(ps
->pread
, "read store already in use");
1661 store_read_reset(ps
);
1663 /* Both the create APIs can return NULL if used in their default mode
1664 * (because there is no other way of handling an error because the jmp_buf
1665 * by default is stored in png_struct and that has not been allocated!)
1666 * However, given that store_error works correctly in these circumstances
1667 * we don't ever expect NULL in this program.
1669 # ifdef PNG_USER_MEM_SUPPORTED
1671 ps
->pread
= png_create_read_struct_2(PNG_LIBPNG_VER_STRING
, ps
,
1672 store_error
, store_warning
, &ps
->read_memory_pool
, store_malloc
,
1677 ps
->pread
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, ps
, store_error
,
1680 if (ps
->pread
== NULL
)
1682 struct exception_context
*the_exception_context
= &ps
->exception_context
;
1684 store_log(ps
, NULL
, "png_create_read_struct returned NULL (unexpected)",
1690 # ifdef PNG_SET_OPTION_SUPPORTED
1693 for (opt
=0; opt
<ps
->noptions
; ++opt
)
1694 if (png_set_option(ps
->pread
, ps
->options
[opt
].option
,
1695 ps
->options
[opt
].setting
) == PNG_OPTION_INVALID
)
1696 png_error(ps
->pread
, "png option invalid");
1700 store_read_set(ps
, id
);
1703 *ppi
= ps
->piread
= png_create_info_struct(ps
->pread
);
1707 #endif /* PNG_READ_SUPPORTED */
1709 /* The overall cleanup of a store simply calls the above then removes all the
1710 * saved files. This does not delete the store itself.
1713 store_delete(png_store
*ps
)
1715 store_write_reset(ps
);
1716 store_read_reset(ps
);
1717 store_freefile(&ps
->saved
);
1718 store_image_free(ps
, NULL
);
1721 /*********************** PNG FILE MODIFICATION ON READ ************************/
1722 /* Files may be modified on read. The following structure contains a complete
1723 * png_store together with extra members to handle modification and a special
1724 * read callback for libpng. To use this the 'modifications' field must be set
1725 * to a list of png_modification structures that actually perform the
1726 * modification, otherwise a png_modifier is functionally equivalent to a
1727 * png_store. There is a special read function, set_modifier_for_read, which
1728 * replaces set_store_for_read.
1730 typedef enum modifier_state
1732 modifier_start
, /* Initial value */
1733 modifier_signature
, /* Have a signature */
1734 modifier_IHDR
/* Have an IHDR */
1737 typedef struct CIE_color
1739 /* A single CIE tristimulus value, representing the unique response of a
1740 * standard observer to a variety of light spectra. The observer recognizes
1741 * all spectra that produce this response as the same color, therefore this
1742 * is effectively a description of a color.
1747 typedef struct color_encoding
1749 /* A description of an (R,G,B) encoding of color (as defined above); this
1750 * includes the actual colors of the (R,G,B) triples (1,0,0), (0,1,0) and
1751 * (0,0,1) plus an encoding value that is used to encode the linear
1752 * components R, G and B to give the actual values R^gamma, G^gamma and
1753 * B^gamma that are stored.
1755 double gamma
; /* Encoding (file) gamma of space */
1756 CIE_color red
, green
, blue
; /* End points */
1759 #ifdef PNG_READ_SUPPORTED
1761 chromaticity_x(CIE_color c
)
1763 return c
.X
/ (c
.X
+ c
.Y
+ c
.Z
);
1767 chromaticity_y(CIE_color c
)
1769 return c
.Y
/ (c
.X
+ c
.Y
+ c
.Z
);
1773 white_point(PNG_CONST color_encoding
*encoding
)
1777 white
.X
= encoding
->red
.X
+ encoding
->green
.X
+ encoding
->blue
.X
;
1778 white
.Y
= encoding
->red
.Y
+ encoding
->green
.Y
+ encoding
->blue
.Y
;
1779 white
.Z
= encoding
->red
.Z
+ encoding
->green
.Z
+ encoding
->blue
.Z
;
1784 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1786 normalize_color_encoding(color_encoding
*encoding
)
1788 PNG_CONST
double whiteY
= encoding
->red
.Y
+ encoding
->green
.Y
+
1793 encoding
->red
.X
/= whiteY
;
1794 encoding
->red
.Y
/= whiteY
;
1795 encoding
->red
.Z
/= whiteY
;
1796 encoding
->green
.X
/= whiteY
;
1797 encoding
->green
.Y
/= whiteY
;
1798 encoding
->green
.Z
/= whiteY
;
1799 encoding
->blue
.X
/= whiteY
;
1800 encoding
->blue
.Y
/= whiteY
;
1801 encoding
->blue
.Z
/= whiteY
;
1807 safecat_color_encoding(char *buffer
, size_t bufsize
, size_t pos
,
1808 PNG_CONST color_encoding
*e
, double encoding_gamma
)
1812 if (encoding_gamma
!= 0)
1813 pos
= safecat(buffer
, bufsize
, pos
, "(");
1814 pos
= safecat(buffer
, bufsize
, pos
, "R(");
1815 pos
= safecatd(buffer
, bufsize
, pos
, e
->red
.X
, 4);
1816 pos
= safecat(buffer
, bufsize
, pos
, ",");
1817 pos
= safecatd(buffer
, bufsize
, pos
, e
->red
.Y
, 4);
1818 pos
= safecat(buffer
, bufsize
, pos
, ",");
1819 pos
= safecatd(buffer
, bufsize
, pos
, e
->red
.Z
, 4);
1820 pos
= safecat(buffer
, bufsize
, pos
, "),G(");
1821 pos
= safecatd(buffer
, bufsize
, pos
, e
->green
.X
, 4);
1822 pos
= safecat(buffer
, bufsize
, pos
, ",");
1823 pos
= safecatd(buffer
, bufsize
, pos
, e
->green
.Y
, 4);
1824 pos
= safecat(buffer
, bufsize
, pos
, ",");
1825 pos
= safecatd(buffer
, bufsize
, pos
, e
->green
.Z
, 4);
1826 pos
= safecat(buffer
, bufsize
, pos
, "),B(");
1827 pos
= safecatd(buffer
, bufsize
, pos
, e
->blue
.X
, 4);
1828 pos
= safecat(buffer
, bufsize
, pos
, ",");
1829 pos
= safecatd(buffer
, bufsize
, pos
, e
->blue
.Y
, 4);
1830 pos
= safecat(buffer
, bufsize
, pos
, ",");
1831 pos
= safecatd(buffer
, bufsize
, pos
, e
->blue
.Z
, 4);
1832 pos
= safecat(buffer
, bufsize
, pos
, ")");
1833 if (encoding_gamma
!= 0)
1834 pos
= safecat(buffer
, bufsize
, pos
, ")");
1837 if (encoding_gamma
!= 0)
1839 pos
= safecat(buffer
, bufsize
, pos
, "^");
1840 pos
= safecatd(buffer
, bufsize
, pos
, encoding_gamma
, 5);
1845 #endif /* PNG_READ_SUPPORTED */
1847 typedef struct png_modifier
1849 png_store
this; /* I am a png_store */
1850 struct png_modification
*modifications
; /* Changes to make */
1852 modifier_state state
; /* My state */
1854 /* Information from IHDR: */
1855 png_byte bit_depth
; /* From IHDR */
1856 png_byte colour_type
; /* From IHDR */
1858 /* While handling PLTE, IDAT and IEND these chunks may be pended to allow
1859 * other chunks to be inserted.
1861 png_uint_32 pending_len
;
1862 png_uint_32 pending_chunk
;
1866 unsigned int ngammas
;
1867 unsigned int ngamma_tests
; /* Number of gamma tests to run*/
1868 double current_gamma
; /* 0 if not set */
1869 PNG_CONST color_encoding
*encodings
;
1870 unsigned int nencodings
;
1871 PNG_CONST color_encoding
*current_encoding
; /* If an encoding has been set */
1872 unsigned int encoding_counter
; /* For iteration */
1873 int encoding_ignored
; /* Something overwrote it */
1875 /* Control variables used to iterate through possible encodings, the
1876 * following must be set to 0 and tested by the function that uses the
1877 * png_modifier because the modifier only sets it to 1 (true.)
1879 unsigned int repeat
:1; /* Repeat this transform test. */
1880 unsigned int test_uses_encoding
:1;
1882 /* Lowest sbit to test (libpng fails for sbit < 8) */
1885 /* Error control - these are the limits on errors accepted by the gamma tests
1888 double maxout8
; /* Maximum output value error */
1889 double maxabs8
; /* Absolute sample error 0..1 */
1890 double maxcalc8
; /* Absolute sample error 0..1 */
1891 double maxpc8
; /* Percentage sample error 0..100% */
1892 double maxout16
; /* Maximum output value error */
1893 double maxabs16
; /* Absolute sample error 0..1 */
1894 double maxcalc16
;/* Absolute sample error 0..1 */
1895 double maxcalcG
; /* Absolute sample error 0..1 */
1896 double maxpc16
; /* Percentage sample error 0..100% */
1898 /* This is set by transforms that need to allow a higher limit, it is an
1899 * internal check on pngvalid to ensure that the calculated error limits are
1900 * not ridiculous; without this it is too easy to make a mistake in pngvalid
1901 * that allows any value through.
1903 double limit
; /* limit on error values, normally 4E-3 */
1905 /* Log limits - values above this are logged, but not necessarily
1908 double log8
; /* Absolute error in 8 bits to log */
1909 double log16
; /* Absolute error in 16 bits to log */
1911 /* Logged 8 and 16 bit errors ('output' values): */
1912 double error_gray_2
;
1913 double error_gray_4
;
1914 double error_gray_8
;
1915 double error_gray_16
;
1916 double error_color_8
;
1917 double error_color_16
;
1918 double error_indexed
;
1921 /* Whether to call png_read_update_info, not png_read_start_image, and how
1922 * many times to call it.
1924 int use_update_info
;
1926 /* Whether or not to interlace. */
1927 int interlace_type
:9; /* int, but must store '1' */
1929 /* Run the standard tests? */
1930 unsigned int test_standard
:1;
1932 /* Run the odd-sized image and interlace read/write tests? */
1933 unsigned int test_size
:1;
1935 /* Run tests on reading with a combination of transforms, */
1936 unsigned int test_transform
:1;
1938 /* When to use the use_input_precision option, this controls the gamma
1939 * validation code checks. If set any value that is within the transformed
1940 * range input-.5 to input+.5 will be accepted, otherwise the value must be
1941 * within the normal limits. It should not be necessary to set this; the
1942 * result should always be exact within the permitted error limits.
1944 unsigned int use_input_precision
:1;
1945 unsigned int use_input_precision_sbit
:1;
1946 unsigned int use_input_precision_16to8
:1;
1948 /* If set assume that the calculation bit depth is set by the input
1949 * precision, not the output precision.
1951 unsigned int calculations_use_input_precision
:1;
1953 /* If set assume that the calculations are done in 16 bits even if the sample
1956 unsigned int assume_16_bit_calculations
:1;
1958 /* Which gamma tests to run: */
1959 unsigned int test_gamma_threshold
:1;
1960 unsigned int test_gamma_transform
:1; /* main tests */
1961 unsigned int test_gamma_sbit
:1;
1962 unsigned int test_gamma_scale16
:1;
1963 unsigned int test_gamma_background
:1;
1964 unsigned int test_gamma_alpha_mode
:1;
1965 unsigned int test_gamma_expand16
:1;
1966 unsigned int test_exhaustive
:1;
1968 unsigned int log
:1; /* Log max error */
1970 /* Buffer information, the buffer size limits the size of the chunks that can
1971 * be modified - they must fit (including header and CRC) into the buffer!
1973 size_t flush
; /* Count of bytes to flush */
1974 size_t buffer_count
; /* Bytes in buffer */
1975 size_t buffer_position
; /* Position in buffer */
1976 png_byte buffer
[1024];
1979 /* This returns true if the test should be stopped now because it has already
1980 * failed and it is running silently.
1982 static int fail(png_modifier
*pm
)
1984 return !pm
->log
&& !pm
->this.verbose
&& (pm
->this.nerrors
> 0 ||
1985 (pm
->this.treat_warnings_as_errors
&& pm
->this.nwarnings
> 0));
1989 modifier_init(png_modifier
*pm
)
1991 memset(pm
, 0, sizeof *pm
);
1992 store_init(&pm
->this);
1993 pm
->modifications
= NULL
;
1994 pm
->state
= modifier_start
;
1997 pm
->ngamma_tests
= 0;
1999 pm
->current_gamma
= 0;
2002 pm
->current_encoding
= 0;
2003 pm
->encoding_counter
= 0;
2004 pm
->encoding_ignored
= 0;
2006 pm
->test_uses_encoding
= 0;
2007 pm
->maxout8
= pm
->maxpc8
= pm
->maxabs8
= pm
->maxcalc8
= 0;
2008 pm
->maxout16
= pm
->maxpc16
= pm
->maxabs16
= pm
->maxcalc16
= 0;
2011 pm
->log8
= pm
->log16
= 0; /* Means 'off' */
2012 pm
->error_gray_2
= pm
->error_gray_4
= pm
->error_gray_8
= 0;
2013 pm
->error_gray_16
= pm
->error_color_8
= pm
->error_color_16
= 0;
2014 pm
->error_indexed
= 0;
2015 pm
->use_update_info
= 0;
2016 pm
->interlace_type
= PNG_INTERLACE_NONE
;
2017 pm
->test_standard
= 0;
2019 pm
->test_transform
= 0;
2020 pm
->use_input_precision
= 0;
2021 pm
->use_input_precision_sbit
= 0;
2022 pm
->use_input_precision_16to8
= 0;
2023 pm
->calculations_use_input_precision
= 0;
2024 pm
->assume_16_bit_calculations
= 0;
2025 pm
->test_gamma_threshold
= 0;
2026 pm
->test_gamma_transform
= 0;
2027 pm
->test_gamma_sbit
= 0;
2028 pm
->test_gamma_scale16
= 0;
2029 pm
->test_gamma_background
= 0;
2030 pm
->test_gamma_alpha_mode
= 0;
2031 pm
->test_gamma_expand16
= 0;
2032 pm
->test_exhaustive
= 0;
2035 /* Rely on the memset for all the other fields - there are no pointers */
2038 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
2040 /* This controls use of checks that explicitly know how libpng digitizes the
2041 * samples in calculations; setting this circumvents simple error limit checking
2042 * in the rgb_to_gray check, replacing it with an exact copy of the libpng 1.5
2045 #define DIGITIZE PNG_LIBPNG_VER < 10700
2047 /* If pm->calculations_use_input_precision is set then operations will happen
2048 * with the precision of the input, not the precision of the output depth.
2050 * If pm->assume_16_bit_calculations is set then even 8 bit calculations use 16
2051 * bit precision. This only affects those of the following limits that pertain
2052 * to a calculation - not a digitization operation - unless the following API is
2055 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2057 static double digitize(double value
, int depth
, int do_round
)
2059 /* 'value' is in the range 0 to 1, the result is the same value rounded to a
2060 * multiple of the digitization factor - 8 or 16 bits depending on both the
2061 * sample depth and the 'assume' setting. Digitization is normally by
2062 * rounding and 'do_round' should be 1, if it is 0 the digitized value will
2065 PNG_CONST
unsigned int digitization_factor
= (1U << depth
) -1;
2067 /* Limiting the range is done as a convenience to the caller - it's easier to
2068 * do it once here than every time at the call site.
2073 else if (value
>= 1)
2076 value
*= digitization_factor
;
2077 if (do_round
) value
+= .5;
2078 return floor(value
)/digitization_factor
;
2081 #endif /* RGB_TO_GRAY */
2083 #ifdef PNG_READ_GAMMA_SUPPORTED
2084 static double abserr(PNG_CONST png_modifier
*pm
, int in_depth
, int out_depth
)
2086 /* Absolute error permitted in linear values - affected by the bit depth of
2089 if (pm
->assume_16_bit_calculations
||
2090 (pm
->calculations_use_input_precision ? in_depth
: out_depth
) == 16)
2091 return pm
->maxabs16
;
2096 static double calcerr(PNG_CONST png_modifier
*pm
, int in_depth
, int out_depth
)
2098 /* Error in the linear composition arithmetic - only relevant when
2099 * composition actually happens (0 < alpha < 1).
2101 if ((pm
->calculations_use_input_precision ? in_depth
: out_depth
) == 16)
2102 return pm
->maxcalc16
;
2103 else if (pm
->assume_16_bit_calculations
)
2104 return pm
->maxcalcG
;
2106 return pm
->maxcalc8
;
2109 static double pcerr(PNG_CONST png_modifier
*pm
, int in_depth
, int out_depth
)
2111 /* Percentage error permitted in the linear values. Note that the specified
2112 * value is a percentage but this routine returns a simple number.
2114 if (pm
->assume_16_bit_calculations
||
2115 (pm
->calculations_use_input_precision ? in_depth
: out_depth
) == 16)
2116 return pm
->maxpc16
* .01;
2118 return pm
->maxpc8
* .01;
2121 /* Output error - the error in the encoded value. This is determined by the
2122 * digitization of the output so can be +/-0.5 in the actual output value. In
2123 * the expand_16 case with the current code in libpng the expand happens after
2124 * all the calculations are done in 8 bit arithmetic, so even though the output
2125 * depth is 16 the output error is determined by the 8 bit calculation.
2127 * This limit is not determined by the bit depth of internal calculations.
2129 * The specified parameter does *not* include the base .5 digitization error but
2132 static double outerr(PNG_CONST png_modifier
*pm
, int in_depth
, int out_depth
)
2134 /* There is a serious error in the 2 and 4 bit grayscale transform because
2135 * the gamma table value (8 bits) is simply shifted, not rounded, so the
2136 * error in 4 bit grayscale gamma is up to the value below. This is a hack
2137 * to allow pngvalid to succeed:
2139 * TODO: fix this in libpng
2147 if ((pm
->calculations_use_input_precision ? in_depth
: out_depth
) == 16)
2148 return pm
->maxout16
;
2150 /* This is the case where the value was calculated at 8-bit precision then
2151 * scaled to 16 bits.
2153 else if (out_depth
== 16)
2154 return pm
->maxout8
* 257;
2160 /* This does the same thing as the above however it returns the value to log,
2161 * rather than raising a warning. This is useful for debugging to track down
2162 * exactly what set of parameters cause high error values.
2164 static double outlog(PNG_CONST png_modifier
*pm
, int in_depth
, int out_depth
)
2166 /* The command line parameters are either 8 bit (0..255) or 16 bit (0..65535)
2167 * and so must be adjusted for low bit depth grayscale:
2171 if (pm
->log8
== 0) /* switched off */
2175 return pm
->log8
/ 255 * ((1<<out_depth
)-1);
2180 if ((pm
->calculations_use_input_precision ? in_depth
: out_depth
) == 16)
2188 /* This is the case where the value was calculated at 8-bit precision then
2189 * scaled to 16 bits.
2194 return pm
->log8
* 257;
2197 /* This complements the above by providing the appropriate quantization for the
2198 * final value. Normally this would just be quantization to an integral value,
2199 * but in the 8 bit calculation case it's actually quantization to a multiple of
2202 static int output_quantization_factor(PNG_CONST png_modifier
*pm
, int in_depth
,
2205 if (out_depth
== 16 && in_depth
!= 16 &&
2206 pm
->calculations_use_input_precision
)
2211 #endif /* PNG_READ_GAMMA_SUPPORTED */
2213 /* One modification structure must be provided for each chunk to be modified (in
2214 * fact more than one can be provided if multiple separate changes are desired
2215 * for a single chunk.) Modifications include adding a new chunk when a
2216 * suitable chunk does not exist.
2218 * The caller of modify_fn will reset the CRC of the chunk and record 'modified'
2219 * or 'added' as appropriate if the modify_fn returns 1 (true). If the
2220 * modify_fn is NULL the chunk is simply removed.
2222 typedef struct png_modification
2224 struct png_modification
*next
;
2227 /* If the following is NULL all matching chunks will be removed: */
2228 int (*modify_fn
)(struct png_modifier
*pm
,
2229 struct png_modification
*me
, int add
);
2231 /* If the following is set to PLTE, IDAT or IEND and the chunk has not been
2232 * found and modified (and there is a modify_fn) the modify_fn will be called
2233 * to add the chunk before the relevant chunk.
2236 unsigned int modified
:1; /* Chunk was modified */
2237 unsigned int added
:1; /* Chunk was added */
2238 unsigned int removed
:1; /* Chunk was removed */
2242 modification_reset(png_modification
*pmm
)
2249 modification_reset(pmm
->next
);
2254 modification_init(png_modification
*pmm
)
2256 memset(pmm
, 0, sizeof *pmm
);
2259 pmm
->modify_fn
= NULL
;
2261 modification_reset(pmm
);
2264 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2266 modifier_current_encoding(PNG_CONST png_modifier
*pm
, color_encoding
*ce
)
2268 if (pm
->current_encoding
!= 0)
2269 *ce
= *pm
->current_encoding
;
2272 memset(ce
, 0, sizeof *ce
);
2274 ce
->gamma
= pm
->current_gamma
;
2279 safecat_current_encoding(char *buffer
, size_t bufsize
, size_t pos
,
2280 PNG_CONST png_modifier
*pm
)
2282 pos
= safecat_color_encoding(buffer
, bufsize
, pos
, pm
->current_encoding
,
2285 if (pm
->encoding_ignored
)
2286 pos
= safecat(buffer
, bufsize
, pos
, "[overridden]");
2291 /* Iterate through the usefully testable color encodings. An encoding is one
2294 * 1) Nothing (no color space, no gamma).
2295 * 2) Just a gamma value from the gamma array (including 1.0)
2296 * 3) A color space from the encodings array with the corresponding gamma.
2297 * 4) The same, but with gamma 1.0 (only really useful with 16 bit calculations)
2299 * The iterator selects these in turn, the randomizer selects one at random,
2300 * which is used depends on the setting of the 'test_exhaustive' flag. Notice
2301 * that this function changes the colour space encoding so it must only be
2302 * called on completion of the previous test. This is what 'modifier_reset'
2305 * After the function has been called the 'repeat' flag will still be set; the
2306 * caller of modifier_reset must reset it at the start of each run of the test!
2309 modifier_total_encodings(PNG_CONST png_modifier
*pm
)
2311 return 1 + /* (1) nothing */
2312 pm
->ngammas
+ /* (2) gamma values to test */
2313 pm
->nencodings
+ /* (3) total number of encodings */
2314 /* The following test only works after the first time through the
2315 * png_modifier code because 'bit_depth' is set when the IHDR is read.
2316 * modifier_reset, below, preserves the setting until after it has called
2317 * the iterate function (also below.)
2319 * For this reason do not rely on this function outside a call to
2322 ((pm
->bit_depth
== 16 || pm
->assume_16_bit_calculations
) ?
2323 pm
->nencodings
: 0); /* (4) encodings with gamma == 1.0 */
2327 modifier_encoding_iterate(png_modifier
*pm
)
2329 if (!pm
->repeat
&& /* Else something needs the current encoding again. */
2330 pm
->test_uses_encoding
) /* Some transform is encoding dependent */
2332 if (pm
->test_exhaustive
)
2334 if (++pm
->encoding_counter
>= modifier_total_encodings(pm
))
2335 pm
->encoding_counter
= 0; /* This will stop the repeat */
2340 /* Not exhaustive - choose an encoding at random; generate a number in
2341 * the range 1..(max-1), so the result is always non-zero:
2343 if (pm
->encoding_counter
== 0)
2344 pm
->encoding_counter
= random_mod(modifier_total_encodings(pm
)-1)+1;
2346 pm
->encoding_counter
= 0;
2349 if (pm
->encoding_counter
> 0)
2353 else if (!pm
->repeat
)
2354 pm
->encoding_counter
= 0;
2358 modifier_reset(png_modifier
*pm
)
2360 store_read_reset(&pm
->this);
2362 pm
->pending_len
= pm
->pending_chunk
= 0;
2363 pm
->flush
= pm
->buffer_count
= pm
->buffer_position
= 0;
2364 pm
->modifications
= NULL
;
2365 pm
->state
= modifier_start
;
2366 modifier_encoding_iterate(pm
);
2367 /* The following must be set in the next run. In particular
2368 * test_uses_encodings must be set in the _ini function of each transform
2369 * that looks at the encodings. (Not the 'add' function!)
2371 pm
->test_uses_encoding
= 0;
2372 pm
->current_gamma
= 0;
2373 pm
->current_encoding
= 0;
2374 pm
->encoding_ignored
= 0;
2375 /* These only become value after IHDR is read: */
2376 pm
->bit_depth
= pm
->colour_type
= 0;
2379 /* The following must be called before anything else to get the encoding set up
2380 * on the modifier. In particular it must be called before the transform init
2381 * functions are called.
2384 modifier_set_encoding(png_modifier
*pm
)
2386 /* Set the encoding to the one specified by the current encoding counter,
2387 * first clear out all the settings - this corresponds to an encoding_counter
2390 pm
->current_gamma
= 0;
2391 pm
->current_encoding
= 0;
2392 pm
->encoding_ignored
= 0; /* not ignored yet - happens in _ini functions. */
2394 /* Now, if required, set the gamma and encoding fields. */
2395 if (pm
->encoding_counter
> 0)
2397 /* The gammas[] array is an array of screen gammas, not encoding gammas,
2398 * so we need the inverse:
2400 if (pm
->encoding_counter
<= pm
->ngammas
)
2401 pm
->current_gamma
= 1/pm
->gammas
[pm
->encoding_counter
-1];
2405 unsigned int i
= pm
->encoding_counter
- pm
->ngammas
;
2407 if (i
>= pm
->nencodings
)
2409 i
%= pm
->nencodings
;
2410 pm
->current_gamma
= 1; /* Linear, only in the 16 bit case */
2414 pm
->current_gamma
= pm
->encodings
[i
].gamma
;
2416 pm
->current_encoding
= pm
->encodings
+ i
;
2421 /* Enquiry functions to find out what is set. Notice that there is an implicit
2422 * assumption below that the first encoding in the list is the one for sRGB.
2425 modifier_color_encoding_is_sRGB(PNG_CONST png_modifier
*pm
)
2427 return pm
->current_encoding
!= 0 && pm
->current_encoding
== pm
->encodings
&&
2428 pm
->current_encoding
->gamma
== pm
->current_gamma
;
2432 modifier_color_encoding_is_set(PNG_CONST png_modifier
*pm
)
2434 return pm
->current_gamma
!= 0;
2437 /* Convenience macros. */
2438 #define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
2439 #define CHUNK_IHDR CHUNK(73,72,68,82)
2440 #define CHUNK_PLTE CHUNK(80,76,84,69)
2441 #define CHUNK_IDAT CHUNK(73,68,65,84)
2442 #define CHUNK_IEND CHUNK(73,69,78,68)
2443 #define CHUNK_cHRM CHUNK(99,72,82,77)
2444 #define CHUNK_gAMA CHUNK(103,65,77,65)
2445 #define CHUNK_sBIT CHUNK(115,66,73,84)
2446 #define CHUNK_sRGB CHUNK(115,82,71,66)
2448 /* The guts of modification are performed during a read. */
2450 modifier_crc(png_bytep buffer
)
2452 /* Recalculate the chunk CRC - a complete chunk must be in
2453 * the buffer, at the start.
2455 uInt datalen
= png_get_uint_32(buffer
);
2456 uLong crc
= crc32(0, buffer
+4, datalen
+4);
2457 /* The cast to png_uint_32 is safe because a crc32 is always a 32 bit value.
2459 png_save_uint_32(buffer
+datalen
+8, (png_uint_32
)crc
);
2463 modifier_setbuffer(png_modifier
*pm
)
2465 modifier_crc(pm
->buffer
);
2466 pm
->buffer_count
= png_get_uint_32(pm
->buffer
)+12;
2467 pm
->buffer_position
= 0;
2470 /* Separate the callback into the actual implementation (which is passed the
2471 * png_modifier explicitly) and the callback, which gets the modifier from the
2475 modifier_read_imp(png_modifier
*pm
, png_bytep pb
, png_size_t st
)
2480 png_uint_32 len
, chunk
;
2481 png_modification
*mod
;
2483 if (pm
->buffer_position
>= pm
->buffer_count
) switch (pm
->state
)
2485 static png_byte sign
[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
2486 case modifier_start
:
2487 store_read_imp(&pm
->this, pm
->buffer
, 8); /* size of signature. */
2488 pm
->buffer_count
= 8;
2489 pm
->buffer_position
= 0;
2491 if (memcmp(pm
->buffer
, sign
, 8) != 0)
2492 png_error(pm
->this.pread
, "invalid PNG file signature");
2493 pm
->state
= modifier_signature
;
2496 case modifier_signature
:
2497 store_read_imp(&pm
->this, pm
->buffer
, 13+12); /* size of IHDR */
2498 pm
->buffer_count
= 13+12;
2499 pm
->buffer_position
= 0;
2501 if (png_get_uint_32(pm
->buffer
) != 13 ||
2502 png_get_uint_32(pm
->buffer
+4) != CHUNK_IHDR
)
2503 png_error(pm
->this.pread
, "invalid IHDR");
2505 /* Check the list of modifiers for modifications to the IHDR. */
2506 mod
= pm
->modifications
;
2509 if (mod
->chunk
== CHUNK_IHDR
&& mod
->modify_fn
&&
2510 (*mod
->modify_fn
)(pm
, mod
, 0))
2513 modifier_setbuffer(pm
);
2516 /* Ignore removal or add if IHDR! */
2520 /* Cache information from the IHDR (the modified one.) */
2521 pm
->bit_depth
= pm
->buffer
[8+8];
2522 pm
->colour_type
= pm
->buffer
[8+8+1];
2524 pm
->state
= modifier_IHDR
;
2530 /* Read a new chunk and process it until we see PLTE, IDAT or
2531 * IEND. 'flush' indicates that there is still some data to
2532 * output from the preceding chunk.
2534 if ((cb
= pm
->flush
) > 0)
2536 if (cb
> st
) cb
= st
;
2538 store_read_imp(&pm
->this, pb
, cb
);
2541 if (st
== 0) return;
2544 /* No more bytes to flush, read a header, or handle a pending
2547 if (pm
->pending_chunk
!= 0)
2549 png_save_uint_32(pm
->buffer
, pm
->pending_len
);
2550 png_save_uint_32(pm
->buffer
+4, pm
->pending_chunk
);
2551 pm
->pending_len
= 0;
2552 pm
->pending_chunk
= 0;
2555 store_read_imp(&pm
->this, pm
->buffer
, 8);
2557 pm
->buffer_count
= 8;
2558 pm
->buffer_position
= 0;
2560 /* Check for something to modify or a terminator chunk. */
2561 len
= png_get_uint_32(pm
->buffer
);
2562 chunk
= png_get_uint_32(pm
->buffer
+4);
2564 /* Terminators first, they may have to be delayed for added
2567 if (chunk
== CHUNK_PLTE
|| chunk
== CHUNK_IDAT
||
2568 chunk
== CHUNK_IEND
)
2570 mod
= pm
->modifications
;
2574 if ((mod
->add
== chunk
||
2575 (mod
->add
== CHUNK_PLTE
&& chunk
== CHUNK_IDAT
)) &&
2576 mod
->modify_fn
!= NULL
&& !mod
->modified
&& !mod
->added
)
2578 /* Regardless of what the modify function does do not run
2583 if ((*mod
->modify_fn
)(pm
, mod
, 1 /*add*/))
2585 /* Reset the CRC on a new chunk */
2586 if (pm
->buffer_count
> 0)
2587 modifier_setbuffer(pm
);
2591 pm
->buffer_position
= 0;
2595 /* The buffer has been filled with something (we assume)
2596 * so output this. Pend the current chunk.
2598 pm
->pending_len
= len
;
2599 pm
->pending_chunk
= chunk
;
2600 break; /* out of while */
2607 /* Don't do any further processing if the buffer was modified -
2608 * otherwise the code will end up modifying a chunk that was
2612 break; /* out of switch */
2615 /* If we get to here then this chunk may need to be modified. To
2616 * do this it must be less than 1024 bytes in total size, otherwise
2617 * it just gets flushed.
2619 if (len
+12 <= sizeof pm
->buffer
)
2621 store_read_imp(&pm
->this, pm
->buffer
+pm
->buffer_count
,
2622 len
+12-pm
->buffer_count
);
2623 pm
->buffer_count
= len
+12;
2625 /* Check for a modification, else leave it be. */
2626 mod
= pm
->modifications
;
2629 if (mod
->chunk
== chunk
)
2631 if (mod
->modify_fn
== NULL
)
2633 /* Remove this chunk */
2634 pm
->buffer_count
= pm
->buffer_position
= 0;
2636 break; /* Terminate the while loop */
2639 else if ((*mod
->modify_fn
)(pm
, mod
, 0))
2642 /* The chunk may have been removed: */
2643 if (pm
->buffer_count
== 0)
2645 pm
->buffer_position
= 0;
2648 modifier_setbuffer(pm
);
2657 pm
->flush
= len
+12 - pm
->buffer_count
; /* data + crc */
2659 /* Take the data from the buffer (if there is any). */
2663 /* Here to read from the modifier buffer (not directly from
2664 * the store, as in the flush case above.)
2666 cb
= pm
->buffer_count
- pm
->buffer_position
;
2671 memcpy(pb
, pm
->buffer
+ pm
->buffer_position
, cb
);
2674 pm
->buffer_position
+= cb
;
2680 modifier_read(png_structp ppIn
, png_bytep pb
, png_size_t st
)
2682 png_const_structp pp
= ppIn
;
2683 png_modifier
*pm
= voidcast(png_modifier
*, png_get_io_ptr(pp
));
2685 if (pm
== NULL
|| pm
->this.pread
!= pp
)
2686 png_error(pp
, "bad modifier_read call");
2688 modifier_read_imp(pm
, pb
, st
);
2691 /* Like store_progressive_read but the data is getting changed as we go so we
2692 * need a local buffer.
2695 modifier_progressive_read(png_modifier
*pm
, png_structp pp
, png_infop pi
)
2697 if (pm
->this.pread
!= pp
|| pm
->this.current
== NULL
||
2698 pm
->this.next
== NULL
)
2699 png_error(pp
, "store state damaged (progressive)");
2701 /* This is another Horowitz and Hill random noise generator. In this case
2702 * the aim is to stress the progressive reader with truly horrible variable
2703 * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers
2704 * is generated. We could probably just count from 1 to 32767 and get as
2709 static png_uint_32 noise
= 1;
2710 png_size_t cb
, cbAvail
;
2711 png_byte buffer
[512];
2713 /* Generate 15 more bits of stuff: */
2714 noise
= (noise
<< 9) | ((noise
^ (noise
>> (9-5))) & 0x1ff);
2717 /* Check that this number of bytes are available (in the current buffer.)
2718 * (This doesn't quite work - the modifier might delete a chunk; unlikely
2719 * but possible, it doesn't happen at present because the modifier only
2720 * adds chunks to standard images.)
2722 cbAvail
= store_read_buffer_avail(&pm
->this);
2723 if (pm
->buffer_count
> pm
->buffer_position
)
2724 cbAvail
+= pm
->buffer_count
- pm
->buffer_position
;
2728 /* Check for EOF: */
2735 modifier_read_imp(pm
, buffer
, cb
);
2736 png_process_data(pp
, pi
, buffer
, cb
);
2739 /* Check the invariants at the end (if this fails it's a problem in this
2742 if (pm
->buffer_count
> pm
->buffer_position
||
2743 pm
->this.next
!= &pm
->this.current
->data
||
2744 pm
->this.readpos
< pm
->this.current
->datacount
)
2745 png_error(pp
, "progressive read implementation error");
2748 /* Set up a modifier. */
2750 set_modifier_for_read(png_modifier
*pm
, png_infopp ppi
, png_uint_32 id
,
2751 PNG_CONST
char *name
)
2753 /* Do this first so that the modifier fields are cleared even if an error
2754 * happens allocating the png_struct. No allocation is done here so no
2755 * cleanup is required.
2757 pm
->state
= modifier_start
;
2759 pm
->colour_type
= 255;
2761 pm
->pending_len
= 0;
2762 pm
->pending_chunk
= 0;
2764 pm
->buffer_count
= 0;
2765 pm
->buffer_position
= 0;
2767 return set_store_for_read(&pm
->this, ppi
, id
, name
);
2771 /******************************** MODIFICATIONS *******************************/
2772 /* Standard modifications to add chunks. These do not require the _SUPPORTED
2773 * macros because the chunks can be there regardless of whether this specific
2774 * libpng supports them.
2776 typedef struct gama_modification
2778 png_modification
this;
2779 png_fixed_point gamma
;
2780 } gama_modification
;
2783 gama_modify(png_modifier
*pm
, png_modification
*me
, int add
)
2786 /* This simply dumps the given gamma value into the buffer. */
2787 png_save_uint_32(pm
->buffer
, 4);
2788 png_save_uint_32(pm
->buffer
+4, CHUNK_gAMA
);
2789 png_save_uint_32(pm
->buffer
+8, ((gama_modification
*)me
)->gamma
);
2794 gama_modification_init(gama_modification
*me
, png_modifier
*pm
, double gammad
)
2798 modification_init(&me
->this);
2799 me
->this.chunk
= CHUNK_gAMA
;
2800 me
->this.modify_fn
= gama_modify
;
2801 me
->this.add
= CHUNK_PLTE
;
2803 me
->gamma
= (png_fixed_point
)g
;
2804 me
->this.next
= pm
->modifications
;
2805 pm
->modifications
= &me
->this;
2808 typedef struct chrm_modification
2810 png_modification
this;
2811 PNG_CONST color_encoding
*encoding
;
2812 png_fixed_point wx
, wy
, rx
, ry
, gx
, gy
, bx
, by
;
2813 } chrm_modification
;
2816 chrm_modify(png_modifier
*pm
, png_modification
*me
, int add
)
2819 /* As with gAMA this just adds the required cHRM chunk to the buffer. */
2820 png_save_uint_32(pm
->buffer
, 32);
2821 png_save_uint_32(pm
->buffer
+ 4, CHUNK_cHRM
);
2822 png_save_uint_32(pm
->buffer
+ 8, ((chrm_modification
*)me
)->wx
);
2823 png_save_uint_32(pm
->buffer
+12, ((chrm_modification
*)me
)->wy
);
2824 png_save_uint_32(pm
->buffer
+16, ((chrm_modification
*)me
)->rx
);
2825 png_save_uint_32(pm
->buffer
+20, ((chrm_modification
*)me
)->ry
);
2826 png_save_uint_32(pm
->buffer
+24, ((chrm_modification
*)me
)->gx
);
2827 png_save_uint_32(pm
->buffer
+28, ((chrm_modification
*)me
)->gy
);
2828 png_save_uint_32(pm
->buffer
+32, ((chrm_modification
*)me
)->bx
);
2829 png_save_uint_32(pm
->buffer
+36, ((chrm_modification
*)me
)->by
);
2834 chrm_modification_init(chrm_modification
*me
, png_modifier
*pm
,
2835 PNG_CONST color_encoding
*encoding
)
2837 CIE_color white
= white_point(encoding
);
2839 /* Original end points: */
2840 me
->encoding
= encoding
;
2842 /* Chromaticities (in fixed point): */
2843 me
->wx
= fix(chromaticity_x(white
));
2844 me
->wy
= fix(chromaticity_y(white
));
2846 me
->rx
= fix(chromaticity_x(encoding
->red
));
2847 me
->ry
= fix(chromaticity_y(encoding
->red
));
2848 me
->gx
= fix(chromaticity_x(encoding
->green
));
2849 me
->gy
= fix(chromaticity_y(encoding
->green
));
2850 me
->bx
= fix(chromaticity_x(encoding
->blue
));
2851 me
->by
= fix(chromaticity_y(encoding
->blue
));
2853 modification_init(&me
->this);
2854 me
->this.chunk
= CHUNK_cHRM
;
2855 me
->this.modify_fn
= chrm_modify
;
2856 me
->this.add
= CHUNK_PLTE
;
2857 me
->this.next
= pm
->modifications
;
2858 pm
->modifications
= &me
->this;
2861 typedef struct srgb_modification
2863 png_modification
this;
2865 } srgb_modification
;
2868 srgb_modify(png_modifier
*pm
, png_modification
*me
, int add
)
2871 /* As above, ignore add and just make a new chunk */
2872 png_save_uint_32(pm
->buffer
, 1);
2873 png_save_uint_32(pm
->buffer
+4, CHUNK_sRGB
);
2874 pm
->buffer
[8] = ((srgb_modification
*)me
)->intent
;
2879 srgb_modification_init(srgb_modification
*me
, png_modifier
*pm
, png_byte intent
)
2881 modification_init(&me
->this);
2882 me
->this.chunk
= CHUNK_sBIT
;
2884 if (intent
<= 3) /* if valid, else *delete* sRGB chunks */
2886 me
->this.modify_fn
= srgb_modify
;
2887 me
->this.add
= CHUNK_PLTE
;
2888 me
->intent
= intent
;
2893 me
->this.modify_fn
= 0;
2898 me
->this.next
= pm
->modifications
;
2899 pm
->modifications
= &me
->this;
2902 #ifdef PNG_READ_GAMMA_SUPPORTED
2903 typedef struct sbit_modification
2905 png_modification
this;
2907 } sbit_modification
;
2910 sbit_modify(png_modifier
*pm
, png_modification
*me
, int add
)
2912 png_byte sbit
= ((sbit_modification
*)me
)->sbit
;
2913 if (pm
->bit_depth
> sbit
)
2916 switch (pm
->colour_type
)
2936 png_error(pm
->this.pread
,
2937 "unexpected colour type in sBIT modification");
2940 png_save_uint_32(pm
->buffer
, cb
);
2941 png_save_uint_32(pm
->buffer
+4, CHUNK_sBIT
);
2944 (pm
->buffer
+8)[--cb
] = sbit
;
2950 /* Remove the sBIT chunk */
2951 pm
->buffer_count
= pm
->buffer_position
= 0;
2955 return 0; /* do nothing */
2959 sbit_modification_init(sbit_modification
*me
, png_modifier
*pm
, png_byte sbit
)
2961 modification_init(&me
->this);
2962 me
->this.chunk
= CHUNK_sBIT
;
2963 me
->this.modify_fn
= sbit_modify
;
2964 me
->this.add
= CHUNK_PLTE
;
2966 me
->this.next
= pm
->modifications
;
2967 pm
->modifications
= &me
->this;
2969 #endif /* PNG_READ_GAMMA_SUPPORTED */
2970 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
2972 /***************************** STANDARD PNG FILES *****************************/
2973 /* Standard files - write and save standard files. */
2974 /* There are two basic forms of standard images. Those which attempt to have
2975 * all the possible pixel values (not possible for 16bpp images, but a range of
2976 * values are produced) and those which have a range of image sizes. The former
2977 * are used for testing transforms, in particular gamma correction and bit
2978 * reduction and increase. The latter are reserved for testing the behavior of
2979 * libpng with respect to 'odd' image sizes - particularly small images where
2980 * rows become 1 byte and interlace passes disappear.
2982 * The first, most useful, set are the 'transform' images, the second set of
2983 * small images are the 'size' images.
2985 * The transform files are constructed with rows which fit into a 1024 byte row
2986 * buffer. This makes allocation easier below. Further regardless of the file
2987 * format every row has 128 pixels (giving 1024 bytes for 64bpp formats).
2989 * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed
2990 * and with an ID derived from the colour type, bit depth and interlace type
2991 * as above (FILEID). The width (128) and height (variable) are not stored in
2992 * the FILEID - instead the fields are set to 0, indicating a transform file.
2994 * The size files ar constructed with rows a maximum of 128 bytes wide, allowing
2995 * a maximum width of 16 pixels (for the 64bpp case.) They also have a maximum
2996 * height of 16 rows. The width and height are stored in the FILEID and, being
2997 * non-zero, indicate a size file.
2999 * Because the PNG filter code is typically the largest CPU consumer within
3000 * libpng itself there is a tendency to attempt to optimize it. This results in
3001 * special case code which needs to be validated. To cause this to happen the
3002 * 'size' images are made to use each possible filter, in so far as this is
3003 * possible for smaller images.
3005 * For palette image (colour type 3) multiple transform images are stored with
3006 * the same bit depth to allow testing of more colour combinations -
3007 * particularly important for testing the gamma code because libpng uses a
3008 * different code path for palette images. For size images a single palette is
3012 /* Make a 'standard' palette. Because there are only 256 entries in a palette
3013 * (maximum) this actually makes a random palette in the hope that enough tests
3014 * will catch enough errors. (Note that the same palette isn't produced every
3015 * time for the same test - it depends on what previous tests have been run -
3016 * but a given set of arguments to pngvalid will always produce the same palette
3017 * at the same test! This is why pseudo-random number generators are useful for
3020 * The store must be open for write when this is called, otherwise an internal
3021 * error will occur. This routine contains its own magic number seed, so the
3022 * palettes generated don't change if there are intervening errors (changing the
3023 * calls to the store_mark seed.)
3025 static store_palette_entry
*
3026 make_standard_palette(png_store
* ps
, int npalette
, int do_tRNS
)
3028 static png_uint_32 palette_seed
[2] = { 0x87654321, 9 };
3031 png_byte values
[256][4];
3033 /* Always put in black and white plus the six primary and secondary colors.
3037 values
[i
][1] = (png_byte
)((i
&1) ?
255U : 0U);
3038 values
[i
][2] = (png_byte
)((i
&2) ?
255U : 0U);
3039 values
[i
][3] = (png_byte
)((i
&4) ?
255U : 0U);
3042 /* Then add 62 grays (one quarter of the remaining 256 slots). */
3045 png_byte random_bytes
[4];
3048 need
[0] = 0; /*got black*/
3049 memset(need
+1, 1, (sizeof need
)-2); /*need these*/
3050 need
[255] = 0; /*but not white*/
3058 make_four_random_bytes(palette_seed
, random_bytes
);
3062 b
= random_bytes
[--j
];
3072 /* Finally add 192 colors at random - don't worry about matches to things we
3073 * already have, chance is less than 1/65536. Don't worry about grays,
3074 * chance is the same, so we get a duplicate or extra gray less than 1 time
3078 make_four_random_bytes(palette_seed
, values
[i
]);
3080 /* Fill in the alpha values in the first byte. Just use all possible values
3081 * (0..255) in an apparently random order:
3084 store_palette_entry
*palette
;
3085 png_byte selector
[4];
3087 make_four_random_bytes(palette_seed
, selector
);
3090 for (i
=0; i
<256; ++i
)
3091 values
[i
][0] = (png_byte
)(i
^ selector
[0]);
3094 for (i
=0; i
<256; ++i
)
3095 values
[i
][0] = 255; /* no transparency/tRNS chunk */
3097 /* 'values' contains 256 ARGB values, but we only need 'npalette'.
3098 * 'npalette' will always be a power of 2: 2, 4, 16 or 256. In the low
3099 * bit depth cases select colors at random, else it is difficult to have
3100 * a set of low bit depth palette test with any chance of a reasonable
3101 * range of colors. Do this by randomly permuting values into the low
3102 * 'npalette' entries using an XOR mask generated here. This also
3103 * permutes the npalette == 256 case in a potentially useful way (there is
3104 * no relationship between palette index and the color value therein!)
3106 palette
= store_write_palette(ps
, npalette
);
3108 for (i
=0; i
<npalette
; ++i
)
3110 palette
[i
].alpha
= values
[i
^ selector
[1]][0];
3111 palette
[i
].red
= values
[i
^ selector
[1]][1];
3112 palette
[i
].green
= values
[i
^ selector
[1]][2];
3113 palette
[i
].blue
= values
[i
^ selector
[1]][3];
3120 /* Initialize a standard palette on a write stream. The 'do_tRNS' argument
3121 * indicates whether or not to also set the tRNS chunk.
3123 /* TODO: the png_structp here can probably be 'const' in the future */
3125 init_standard_palette(png_store
*ps
, png_structp pp
, png_infop pi
, int npalette
,
3128 store_palette_entry
*ppal
= make_standard_palette(ps
, npalette
, do_tRNS
);
3132 png_color palette
[256];
3134 /* Set all entries to detect overread errors. */
3135 for (i
=0; i
<npalette
; ++i
)
3137 palette
[i
].red
= ppal
[i
].red
;
3138 palette
[i
].green
= ppal
[i
].green
;
3139 palette
[i
].blue
= ppal
[i
].blue
;
3142 /* Just in case fill in the rest with detectable values: */
3144 palette
[i
].red
= palette
[i
].green
= palette
[i
].blue
= 42;
3146 png_set_PLTE(pp
, pi
, palette
, npalette
);
3154 /* Set all the entries, but skip trailing opaque entries */
3155 for (i
=j
=0; i
<npalette
; ++i
)
3156 if ((tRNS
[i
] = ppal
[i
].alpha
) < 255)
3159 /* Fill in the remainder with a detectable value: */
3163 # ifdef PNG_WRITE_tRNS_SUPPORTED
3165 png_set_tRNS(pp
, pi
, tRNS
, j
, 0/*color*/);
3170 /* The number of passes is related to the interlace type. There was no libpng
3171 * API to determine this prior to 1.5, so we need an inquiry function:
3174 npasses_from_interlace_type(png_const_structp pp
, int interlace_type
)
3176 switch (interlace_type
)
3179 png_error(pp
, "invalid interlace type");
3181 case PNG_INTERLACE_NONE
:
3184 case PNG_INTERLACE_ADAM7
:
3185 return PNG_INTERLACE_ADAM7_PASSES
;
3190 bit_size(png_const_structp pp
, png_byte colour_type
, png_byte bit_depth
)
3192 switch (colour_type
)
3194 default: png_error(pp
, "invalid color type");
3196 case 0: return bit_depth
;
3198 case 2: return 3*bit_depth
;
3200 case 3: return bit_depth
;
3202 case 4: return 2*bit_depth
;
3204 case 6: return 4*bit_depth
;
3208 #define TRANSFORM_WIDTH 128U
3209 #define TRANSFORM_ROWMAX (TRANSFORM_WIDTH*8U)
3210 #define SIZE_ROWMAX (16*8U) /* 16 pixels, max 8 bytes each - 128 bytes */
3211 #define STANDARD_ROWMAX TRANSFORM_ROWMAX /* The larger of the two */
3212 #define SIZE_HEIGHTMAX 16 /* Maximum range of size images */
3215 transform_rowsize(png_const_structp pp
, png_byte colour_type
,
3218 return (TRANSFORM_WIDTH
* bit_size(pp
, colour_type
, bit_depth
)) / 8;
3221 /* transform_width(pp, colour_type, bit_depth) current returns the same number
3222 * every time, so just use a macro:
3224 #define transform_width(pp, colour_type, bit_depth) TRANSFORM_WIDTH
3227 transform_height(png_const_structp pp
, png_byte colour_type
, png_byte bit_depth
)
3229 switch (bit_size(pp
, colour_type
, bit_depth
))
3234 return 1; /* Total of 128 pixels */
3237 return 2; /* Total of 256 pixels/bytes */
3240 return 512; /* Total of 65536 pixels */
3244 return 512; /* 65536 pixels */
3248 return 2048;/* 4 x 65536 pixels. */
3249 # define TRANSFORM_HEIGHTMAX 2048
3252 return 0; /* Error, will be caught later */
3256 #ifdef PNG_READ_SUPPORTED
3257 /* The following can only be defined here, now we have the definitions
3258 * of the transform image sizes.
3261 standard_width(png_const_structp pp
, png_uint_32 id
)
3263 png_uint_32 width
= WIDTH_FROM_ID(id
);
3267 width
= transform_width(pp
, COL_FROM_ID(id
), DEPTH_FROM_ID(id
));
3273 standard_height(png_const_structp pp
, png_uint_32 id
)
3275 png_uint_32 height
= HEIGHT_FROM_ID(id
);
3278 height
= transform_height(pp
, COL_FROM_ID(id
), DEPTH_FROM_ID(id
));
3284 standard_rowsize(png_const_structp pp
, png_uint_32 id
)
3286 png_uint_32 width
= standard_width(pp
, id
);
3288 /* This won't overflow: */
3289 width
*= bit_size(pp
, COL_FROM_ID(id
), DEPTH_FROM_ID(id
));
3290 return (width
+ 7) / 8;
3292 #endif /* PNG_READ_SUPPORTED */
3295 transform_row(png_const_structp pp
, png_byte buffer
[TRANSFORM_ROWMAX
],
3296 png_byte colour_type
, png_byte bit_depth
, png_uint_32 y
)
3298 png_uint_32 v
= y
<< 7;
3301 switch (bit_size(pp
, colour_type
, bit_depth
))
3304 while (i
<128/8) buffer
[i
] = (png_byte
)(v
& 0xff), v
+= 17, ++i
;
3308 while (i
<128/4) buffer
[i
] = (png_byte
)(v
& 0xff), v
+= 33, ++i
;
3312 while (i
<128/2) buffer
[i
] = (png_byte
)(v
& 0xff), v
+= 65, ++i
;
3316 /* 256 bytes total, 128 bytes in each row set as follows: */
3317 while (i
<128) buffer
[i
] = (png_byte
)(v
& 0xff), ++v
, ++i
;
3321 /* Generate all 65536 pixel values in order, which includes the 8 bit
3322 * GA case as well as the 16 bit G case.
3326 buffer
[2*i
] = (png_byte
)((v
>>8) & 0xff);
3327 buffer
[2*i
+1] = (png_byte
)(v
& 0xff);
3335 /* 65535 pixels, but rotate the values. */
3338 /* Three bytes per pixel, r, g, b, make b by r^g */
3339 buffer
[3*i
+0] = (png_byte
)((v
>> 8) & 0xff);
3340 buffer
[3*i
+1] = (png_byte
)(v
& 0xff);
3341 buffer
[3*i
+2] = (png_byte
)(((v
>> 8) ^ v
) & 0xff);
3349 /* 65535 pixels, r, g, b, a; just replicate */
3352 buffer
[4*i
+0] = (png_byte
)((v
>> 8) & 0xff);
3353 buffer
[4*i
+1] = (png_byte
)(v
& 0xff);
3354 buffer
[4*i
+2] = (png_byte
)((v
>> 8) & 0xff);
3355 buffer
[4*i
+3] = (png_byte
)(v
& 0xff);
3363 /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at
3364 * each pixel, g increase by 257 (0x101) and 'b' by 0x1111:
3368 png_uint_32 t
= v
++;
3369 buffer
[6*i
+0] = (png_byte
)((t
>> 8) & 0xff);
3370 buffer
[6*i
+1] = (png_byte
)(t
& 0xff);
3372 buffer
[6*i
+2] = (png_byte
)((t
>> 8) & 0xff);
3373 buffer
[6*i
+3] = (png_byte
)(t
& 0xff);
3375 buffer
[6*i
+4] = (png_byte
)((t
>> 8) & 0xff);
3376 buffer
[6*i
+5] = (png_byte
)(t
& 0xff);
3383 /* As above in the 32 bit case. */
3386 png_uint_32 t
= v
++;
3387 buffer
[8*i
+0] = (png_byte
)((t
>> 8) & 0xff);
3388 buffer
[8*i
+1] = (png_byte
)(t
& 0xff);
3389 buffer
[8*i
+4] = (png_byte
)((t
>> 8) & 0xff);
3390 buffer
[8*i
+5] = (png_byte
)(t
& 0xff);
3392 buffer
[8*i
+2] = (png_byte
)((t
>> 8) & 0xff);
3393 buffer
[8*i
+3] = (png_byte
)(t
& 0xff);
3394 buffer
[8*i
+6] = (png_byte
)((t
>> 8) & 0xff);
3395 buffer
[8*i
+7] = (png_byte
)(t
& 0xff);
3404 png_error(pp
, "internal error");
3407 /* This is just to do the right cast - could be changed to a function to check
3408 * 'bd' but there isn't much point.
3410 #define DEPTH(bd) ((png_byte)(1U << (bd)))
3412 /* This is just a helper for compiling on minimal systems with no write
3413 * interlacing support. If there is no write interlacing we can't generate test
3414 * cases with interlace:
3416 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
3417 # define INTERLACE_LAST PNG_INTERLACE_LAST
3418 # define check_interlace_type(type) ((void)(type))
3420 # define INTERLACE_LAST (PNG_INTERLACE_NONE+1)
3421 # define png_set_interlace_handling(a) (1)
3424 check_interlace_type(int PNG_CONST interlace_type
)
3426 if (interlace_type
!= PNG_INTERLACE_NONE
)
3428 /* This is an internal error - --interlace tests should be skipped, not
3431 fprintf(stderr
, "pngvalid: no interlace support\n");
3437 /* Make a standardized image given a an image colour type, bit depth and
3438 * interlace type. The standard images have a very restricted range of
3439 * rows and heights and are used for testing transforms rather than image
3440 * layout details. See make_size_images below for a way to make images
3441 * that test odd sizes along with the libpng interlace handling.
3444 make_transform_image(png_store
* PNG_CONST ps
, png_byte PNG_CONST colour_type
,
3445 png_byte PNG_CONST bit_depth
, unsigned int palette_number
,
3446 int interlace_type
, png_const_charp name
)
3450 check_interlace_type(interlace_type
);
3455 png_structp pp
= set_store_for_write(ps
, &pi
, name
);
3458 /* In the event of a problem return control to the Catch statement below
3459 * to do the clean up - it is not possible to 'return' directly from a Try
3465 h
= transform_height(pp
, colour_type
, bit_depth
);
3467 png_set_IHDR(pp
, pi
, transform_width(pp
, colour_type
, bit_depth
), h
,
3468 bit_depth
, colour_type
, interlace_type
,
3469 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
3471 #ifdef PNG_TEXT_SUPPORTED
3472 # if defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)
3473 # define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_zTXt
3475 # define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_NONE
3478 static char key
[] = "image name"; /* must be writeable */
3481 char copy
[FILE_NAME_SIZE
];
3483 /* Use a compressed text string to test the correct interaction of text
3484 * compression and IDAT compression.
3486 text
.compression
= TEXT_COMPRESSION
;
3488 /* Yuck: the text must be writable! */
3489 pos
= safecat(copy
, sizeof copy
, 0, ps
->wname
);
3491 text
.text_length
= pos
;
3492 text
.itxt_length
= 0;
3496 png_set_text(pp
, pi
, &text
, 1);
3500 if (colour_type
== 3) /* palette */
3501 init_standard_palette(ps
, pp
, pi
, 1U << bit_depth
, 1/*do tRNS*/);
3503 png_write_info(pp
, pi
);
3505 if (png_get_rowbytes(pp
, pi
) !=
3506 transform_rowsize(pp
, colour_type
, bit_depth
))
3507 png_error(pp
, "row size incorrect");
3511 /* Somewhat confusingly this must be called *after* png_write_info
3512 * because if it is called before, the information in *pp has not been
3513 * updated to reflect the interlaced image.
3515 int npasses
= png_set_interlace_handling(pp
);
3518 if (npasses
!= npasses_from_interlace_type(pp
, interlace_type
))
3519 png_error(pp
, "write: png_set_interlace_handling failed");
3521 for (pass
=0; pass
<npasses
; ++pass
)
3527 png_byte buffer
[TRANSFORM_ROWMAX
];
3529 transform_row(pp
, buffer
, colour_type
, bit_depth
, y
);
3530 png_write_row(pp
, buffer
);
3535 #ifdef PNG_TEXT_SUPPORTED
3537 static char key
[] = "end marker";
3538 static char comment
[] = "end";
3541 /* Use a compressed text string to test the correct interaction of text
3542 * compression and IDAT compression.
3544 text
.compression
= TEXT_COMPRESSION
;
3546 text
.text
= comment
;
3547 text
.text_length
= (sizeof comment
)-1;
3548 text
.itxt_length
= 0;
3552 png_set_text(pp
, pi
, &text
, 1);
3556 png_write_end(pp
, pi
);
3558 /* And store this under the appropriate id, then clean up. */
3559 store_storefile(ps
, FILEID(colour_type
, bit_depth
, palette_number
,
3560 interlace_type
, 0, 0, 0));
3562 store_write_reset(ps
);
3567 /* Use the png_store returned by the exception. This may help the compiler
3568 * because 'ps' is not used in this branch of the setjmp. Note that fault
3569 * and ps will always be the same value.
3571 store_write_reset(fault
);
3576 make_transform_images(png_store
*ps
)
3578 png_byte colour_type
= 0;
3579 png_byte bit_depth
= 0;
3580 unsigned int palette_number
= 0;
3582 /* This is in case of errors. */
3583 safecat(ps
->test
, sizeof ps
->test
, 0, "make standard images");
3585 /* Use next_format to enumerate all the combinations we test, including
3586 * generating multiple low bit depth palette images.
3588 while (next_format(&colour_type
, &bit_depth
, &palette_number
, 0))
3592 for (interlace_type
= PNG_INTERLACE_NONE
;
3593 interlace_type
< INTERLACE_LAST
; ++interlace_type
)
3595 char name
[FILE_NAME_SIZE
];
3597 standard_name(name
, sizeof name
, 0, colour_type
, bit_depth
,
3598 palette_number
, interlace_type
, 0, 0, 0);
3599 make_transform_image(ps
, colour_type
, bit_depth
, palette_number
,
3600 interlace_type
, name
);
3605 /* The following two routines use the PNG interlace support macros from
3606 * png.h to interlace or deinterlace rows.
3609 interlace_row(png_bytep buffer
, png_const_bytep imageRow
,
3610 unsigned int pixel_size
, png_uint_32 w
, int pass
)
3612 png_uint_32 xin
, xout
, xstep
;
3614 /* Note that this can, trivially, be optimized to a memcpy on pass 7, the
3615 * code is presented this way to make it easier to understand. In practice
3616 * consult the code in the libpng source to see other ways of doing this.
3618 xin
= PNG_PASS_START_COL(pass
);
3619 xstep
= 1U<<PNG_PASS_COL_SHIFT(pass
);
3621 for (xout
=0; xin
<w
; xin
+=xstep
)
3623 pixel_copy(buffer
, xout
, imageRow
, xin
, pixel_size
);
3628 #ifdef PNG_READ_SUPPORTED
3630 deinterlace_row(png_bytep buffer
, png_const_bytep row
,
3631 unsigned int pixel_size
, png_uint_32 w
, int pass
)
3633 /* The inverse of the above, 'row' is part of row 'y' of the output image,
3634 * in 'buffer'. The image is 'w' wide and this is pass 'pass', distribute
3635 * the pixels of row into buffer and return the number written (to allow
3636 * this to be checked).
3638 png_uint_32 xin
, xout
, xstep
;
3640 xout
= PNG_PASS_START_COL(pass
);
3641 xstep
= 1U<<PNG_PASS_COL_SHIFT(pass
);
3643 for (xin
=0; xout
<w
; xout
+=xstep
)
3645 pixel_copy(buffer
, xout
, row
, xin
, pixel_size
);
3649 #endif /* PNG_READ_SUPPORTED */
3651 /* Build a single row for the 'size' test images; this fills in only the
3652 * first bit_width bits of the sample row.
3655 size_row(png_byte buffer
[SIZE_ROWMAX
], png_uint_32 bit_width
, png_uint_32 y
)
3657 /* height is in the range 1 to 16, so: */
3658 y
= ((y
& 1) << 7) + ((y
& 2) << 6) + ((y
& 4) << 5) + ((y
& 8) << 4);
3659 /* the following ensures bits are set in small images: */
3662 while (bit_width
>= 8)
3663 *buffer
++ = (png_byte
)y
++, bit_width
-= 8;
3665 /* There may be up to 7 remaining bits, these go in the most significant
3670 png_uint_32 mask
= (1U<<(8-bit_width
))-1;
3671 *buffer
= (png_byte
)((*buffer
& mask
) | (y
& ~mask
));
3676 make_size_image(png_store
* PNG_CONST ps
, png_byte PNG_CONST colour_type
,
3677 png_byte PNG_CONST bit_depth
, int PNG_CONST interlace_type
,
3678 png_uint_32 PNG_CONST w
, png_uint_32 PNG_CONST h
,
3679 int PNG_CONST do_interlace
)
3683 /* At present libpng does not support the write of an interlaced image unless
3684 * PNG_WRITE_INTERLACING_SUPPORTED, even with do_interlace so the code here
3685 * does the pixel interlace itself, so:
3687 check_interlace_type(interlace_type
);
3693 unsigned int pixel_size
;
3695 /* Make a name and get an appropriate id for the store: */
3696 char name
[FILE_NAME_SIZE
];
3697 PNG_CONST png_uint_32 id
= FILEID(colour_type
, bit_depth
, 0/*palette*/,
3698 interlace_type
, w
, h
, do_interlace
);
3700 standard_name_from_id(name
, sizeof name
, 0, id
);
3701 pp
= set_store_for_write(ps
, &pi
, name
);
3703 /* In the event of a problem return control to the Catch statement below
3704 * to do the clean up - it is not possible to 'return' directly from a Try
3710 png_set_IHDR(pp
, pi
, w
, h
, bit_depth
, colour_type
, interlace_type
,
3711 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
3713 #ifdef PNG_TEXT_SUPPORTED
3715 static char key
[] = "image name"; /* must be writeable */
3718 char copy
[FILE_NAME_SIZE
];
3720 /* Use a compressed text string to test the correct interaction of text
3721 * compression and IDAT compression.
3723 text
.compression
= TEXT_COMPRESSION
;
3725 /* Yuck: the text must be writable! */
3726 pos
= safecat(copy
, sizeof copy
, 0, ps
->wname
);
3728 text
.text_length
= pos
;
3729 text
.itxt_length
= 0;
3733 png_set_text(pp
, pi
, &text
, 1);
3737 if (colour_type
== 3) /* palette */
3738 init_standard_palette(ps
, pp
, pi
, 1U << bit_depth
, 0/*do tRNS*/);
3740 png_write_info(pp
, pi
);
3742 /* Calculate the bit size, divide by 8 to get the byte size - this won't
3743 * overflow because we know the w values are all small enough even for
3744 * a system where 'unsigned int' is only 16 bits.
3746 pixel_size
= bit_size(pp
, colour_type
, bit_depth
);
3747 if (png_get_rowbytes(pp
, pi
) != ((w
* pixel_size
) + 7) / 8)
3748 png_error(pp
, "row size incorrect");
3752 int npasses
= npasses_from_interlace_type(pp
, interlace_type
);
3755 # ifdef PNG_WRITE_FILTER_SUPPORTED
3756 int nfilter
= PNG_FILTER_VALUE_LAST
;
3758 png_byte image
[16][SIZE_ROWMAX
];
3760 /* To help consistent error detection make the parts of this buffer
3761 * that aren't set below all '1':
3763 memset(image
, 0xff, sizeof image
);
3765 if (!do_interlace
&& npasses
!= png_set_interlace_handling(pp
))
3766 png_error(pp
, "write: png_set_interlace_handling failed");
3768 /* Prepare the whole image first to avoid making it 7 times: */
3770 size_row(image
[y
], w
* pixel_size
, y
);
3772 for (pass
=0; pass
<npasses
; ++pass
)
3774 /* The following two are for checking the macros: */
3775 PNG_CONST png_uint_32 wPass
= PNG_PASS_COLS(w
, pass
);
3777 /* If do_interlace is set we don't call png_write_row for every
3778 * row because some of them are empty. In fact, for a 1x1 image,
3779 * most of them are empty!
3783 png_const_bytep row
= image
[y
];
3784 png_byte tempRow
[SIZE_ROWMAX
];
3786 /* If do_interlace *and* the image is interlaced we
3787 * need a reduced interlace row; this may be reduced
3790 if (do_interlace
&& interlace_type
== PNG_INTERLACE_ADAM7
)
3792 /* The row must not be written if it doesn't exist, notice
3793 * that there are two conditions here, either the row isn't
3794 * ever in the pass or the row would be but isn't wide
3795 * enough to contribute any pixels. In fact the wPass test
3796 * can be used to skip the whole y loop in this case.
3798 if (PNG_ROW_IN_INTERLACE_PASS(y
, pass
) && wPass
> 0)
3800 /* Set to all 1's for error detection (libpng tends to
3801 * set unset things to 0).
3803 memset(tempRow
, 0xff, sizeof tempRow
);
3804 interlace_row(tempRow
, row
, pixel_size
, w
, pass
);
3811 # ifdef PNG_WRITE_FILTER_SUPPORTED
3812 /* Only get to here if the row has some pixels in it, set the
3813 * filters to 'all' for the very first row and thereafter to a
3814 * single filter. It isn't well documented, but png_set_filter
3815 * does accept a filter number (per the spec) as well as a bit
3818 * The apparent wackiness of decrementing nfilter rather than
3819 * incrementing is so that Paeth gets used in all images bigger
3820 * than 1 row - it's the tricky one.
3822 png_set_filter(pp
, 0/*method*/,
3823 nfilter
>= PNG_FILTER_VALUE_LAST ? PNG_ALL_FILTERS
: nfilter
);
3826 nfilter
= PNG_FILTER_VALUE_LAST
-1;
3829 png_write_row(pp
, row
);
3834 #ifdef PNG_TEXT_SUPPORTED
3836 static char key
[] = "end marker";
3837 static char comment
[] = "end";
3840 /* Use a compressed text string to test the correct interaction of text
3841 * compression and IDAT compression.
3843 text
.compression
= TEXT_COMPRESSION
;
3845 text
.text
= comment
;
3846 text
.text_length
= (sizeof comment
)-1;
3847 text
.itxt_length
= 0;
3851 png_set_text(pp
, pi
, &text
, 1);
3855 png_write_end(pp
, pi
);
3857 /* And store this under the appropriate id, then clean up. */
3858 store_storefile(ps
, id
);
3860 store_write_reset(ps
);
3865 /* Use the png_store returned by the exception. This may help the compiler
3866 * because 'ps' is not used in this branch of the setjmp. Note that fault
3867 * and ps will always be the same value.
3869 store_write_reset(fault
);
3874 make_size(png_store
* PNG_CONST ps
, png_byte PNG_CONST colour_type
, int bdlo
,
3877 for (; bdlo
<= bdhi
; ++bdlo
)
3881 for (width
= 1; width
<= 16; ++width
)
3885 for (height
= 1; height
<= 16; ++height
)
3887 /* The four combinations of DIY interlace and interlace or not -
3888 * no interlace + DIY should be identical to no interlace with
3891 make_size_image(ps
, colour_type
, DEPTH(bdlo
), PNG_INTERLACE_NONE
,
3893 make_size_image(ps
, colour_type
, DEPTH(bdlo
), PNG_INTERLACE_NONE
,
3895 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
3896 make_size_image(ps
, colour_type
, DEPTH(bdlo
), PNG_INTERLACE_ADAM7
,
3898 make_size_image(ps
, colour_type
, DEPTH(bdlo
), PNG_INTERLACE_ADAM7
,
3907 make_size_images(png_store
*ps
)
3909 /* This is in case of errors. */
3910 safecat(ps
->test
, sizeof ps
->test
, 0, "make size images");
3912 /* Arguments are colour_type, low bit depth, high bit depth
3914 make_size(ps
, 0, 0, WRITE_BDHI
);
3915 make_size(ps
, 2, 3, WRITE_BDHI
);
3916 make_size(ps
, 3, 0, 3 /*palette: max 8 bits*/);
3917 make_size(ps
, 4, 3, WRITE_BDHI
);
3918 make_size(ps
, 6, 3, WRITE_BDHI
);
3921 #ifdef PNG_READ_SUPPORTED
3922 /* Return a row based on image id and 'y' for checking: */
3924 standard_row(png_const_structp pp
, png_byte std
[STANDARD_ROWMAX
],
3925 png_uint_32 id
, png_uint_32 y
)
3927 if (WIDTH_FROM_ID(id
) == 0)
3928 transform_row(pp
, std
, COL_FROM_ID(id
), DEPTH_FROM_ID(id
), y
);
3930 size_row(std
, WIDTH_FROM_ID(id
) * bit_size(pp
, COL_FROM_ID(id
),
3931 DEPTH_FROM_ID(id
)), y
);
3933 #endif /* PNG_READ_SUPPORTED */
3935 /* Tests - individual test cases */
3936 /* Like 'make_standard' but errors are deliberately introduced into the calls
3937 * to ensure that they get detected - it should not be possible to write an
3938 * invalid image with libpng!
3940 /* TODO: the 'set' functions can probably all be made to take a
3941 * png_const_structp rather than a modifiable one.
3943 #ifdef PNG_WARNINGS_SUPPORTED
3945 sBIT0_error_fn(png_structp pp
, png_infop pi
)
3947 /* 0 is invalid... */
3949 bad
.red
= bad
.green
= bad
.blue
= bad
.gray
= bad
.alpha
= 0;
3950 png_set_sBIT(pp
, pi
, &bad
);
3954 sBIT_error_fn(png_structp pp
, png_infop pi
)
3959 if (png_get_color_type(pp
, pi
) == PNG_COLOR_TYPE_PALETTE
)
3963 bit_depth
= png_get_bit_depth(pp
, pi
);
3965 /* Now we know the bit depth we can easily generate an invalid sBIT entry */
3966 bad
.red
= bad
.green
= bad
.blue
= bad
.gray
= bad
.alpha
=
3967 (png_byte
)(bit_depth
+1);
3968 png_set_sBIT(pp
, pi
, &bad
);
3971 static PNG_CONST
struct
3973 void (*fn
)(png_structp
, png_infop
);
3974 PNG_CONST
char *msg
;
3975 unsigned int warning
:1; /* the error is a warning... */
3978 /* no warnings makes these errors undetectable. */
3979 { sBIT0_error_fn
, "sBIT(0): failed to detect error", 1 },
3980 { sBIT_error_fn
, "sBIT(too big): failed to detect error", 1 },
3984 make_error(png_store
* volatile psIn
, png_byte PNG_CONST colour_type
,
3985 png_byte bit_depth
, int interlace_type
, int test
, png_const_charp name
)
3987 png_store
* volatile ps
= psIn
;
3991 check_interlace_type(interlace_type
);
3998 pp
= set_store_for_write(ps
, &pi
, name
);
4003 png_set_IHDR(pp
, pi
, transform_width(pp
, colour_type
, bit_depth
),
4004 transform_height(pp
, colour_type
, bit_depth
), bit_depth
, colour_type
,
4005 interlace_type
, PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
4007 if (colour_type
== 3) /* palette */
4008 init_standard_palette(ps
, pp
, pi
, 1U << bit_depth
, 0/*do tRNS*/);
4010 /* Time for a few errors; these are in various optional chunks, the
4011 * standard tests test the standard chunks pretty well.
4013 # define exception__prev exception_prev_1
4014 # define exception__env exception_env_1
4017 /* Expect this to throw: */
4018 ps
->expect_error
= !error_test
[test
].warning
;
4019 ps
->expect_warning
= error_test
[test
].warning
;
4020 ps
->saw_warning
= 0;
4021 error_test
[test
].fn(pp
, pi
);
4023 /* Normally the error is only detected here: */
4024 png_write_info(pp
, pi
);
4026 /* And handle the case where it was only a warning: */
4027 if (ps
->expect_warning
&& ps
->saw_warning
)
4030 /* If we get here there is a problem, we have success - no error or
4031 * no warning - when we shouldn't have success. Log an error.
4033 store_log(ps
, pp
, error_test
[test
].msg
, 1 /*error*/);
4037 ps
= fault
; /* expected exit, make sure ps is not clobbered */
4038 #undef exception__prev
4039 #undef exception__env
4041 /* And clear these flags */
4042 ps
->expect_error
= 0;
4043 ps
->expect_warning
= 0;
4045 /* Now write the whole image, just to make sure that the detected, or
4046 * undetected, errro has not created problems inside libpng.
4048 if (png_get_rowbytes(pp
, pi
) !=
4049 transform_rowsize(pp
, colour_type
, bit_depth
))
4050 png_error(pp
, "row size incorrect");
4054 png_uint_32 h
= transform_height(pp
, colour_type
, bit_depth
);
4055 int npasses
= png_set_interlace_handling(pp
);
4058 if (npasses
!= npasses_from_interlace_type(pp
, interlace_type
))
4059 png_error(pp
, "write: png_set_interlace_handling failed");
4061 for (pass
=0; pass
<npasses
; ++pass
)
4067 png_byte buffer
[TRANSFORM_ROWMAX
];
4069 transform_row(pp
, buffer
, colour_type
, bit_depth
, y
);
4070 png_write_row(pp
, buffer
);
4075 png_write_end(pp
, pi
);
4077 /* The following deletes the file that was just written. */
4078 store_write_reset(ps
);
4083 store_write_reset(fault
);
4088 make_errors(png_modifier
* PNG_CONST pm
, png_byte PNG_CONST colour_type
,
4089 int bdlo
, int PNG_CONST bdhi
)
4091 for (; bdlo
<= bdhi
; ++bdlo
)
4095 for (interlace_type
= PNG_INTERLACE_NONE
;
4096 interlace_type
< INTERLACE_LAST
; ++interlace_type
)
4099 char name
[FILE_NAME_SIZE
];
4101 standard_name(name
, sizeof name
, 0, colour_type
, 1<<bdlo
, 0,
4102 interlace_type
, 0, 0, 0);
4104 for (test
=0; test
<(sizeof error_test
)/(sizeof error_test
[0]); ++test
)
4106 make_error(&pm
->this, colour_type
, DEPTH(bdlo
), interlace_type
,
4115 return 1; /* keep going */
4117 #endif /* PNG_WARNINGS_SUPPORTED */
4120 perform_error_test(png_modifier
*pm
)
4122 #ifdef PNG_WARNINGS_SUPPORTED /* else there are no cases that work! */
4123 /* Need to do this here because we just write in this test. */
4124 safecat(pm
->this.test
, sizeof pm
->this.test
, 0, "error test");
4126 if (!make_errors(pm
, 0, 0, WRITE_BDHI
))
4129 if (!make_errors(pm
, 2, 3, WRITE_BDHI
))
4132 if (!make_errors(pm
, 3, 0, 3))
4135 if (!make_errors(pm
, 4, 3, WRITE_BDHI
))
4138 if (!make_errors(pm
, 6, 3, WRITE_BDHI
))
4145 /* This is just to validate the internal PNG formatting code - if this fails
4146 * then the warning messages the library outputs will probably be garbage.
4149 perform_formatting_test(png_store
*volatile ps
)
4151 #ifdef PNG_TIME_RFC1123_SUPPORTED
4152 /* The handle into the formatting code is the RFC1123 support; this test does
4153 * nothing if that is compiled out.
4159 png_const_charp correct
= "29 Aug 2079 13:53:60 +0000";
4160 png_const_charp result
;
4161 # if PNG_LIBPNG_VER >= 10600
4162 char timestring
[29];
4167 pp
= set_store_for_write(ps
, NULL
, "libpng formatting test");
4173 /* Arbitrary settings: */
4179 pt
.second
= 60; /* a leap second */
4181 # if PNG_LIBPNG_VER < 10600
4182 result
= png_convert_to_rfc1123(pp
, &pt
);
4184 if (png_convert_to_rfc1123_buffer(timestring
, &pt
))
4185 result
= timestring
;
4192 png_error(pp
, "png_convert_to_rfc1123 failed");
4194 if (strcmp(result
, correct
) != 0)
4199 pos
= safecat(msg
, sizeof msg
, pos
, "png_convert_to_rfc1123(");
4200 pos
= safecat(msg
, sizeof msg
, pos
, correct
);
4201 pos
= safecat(msg
, sizeof msg
, pos
, ") returned: '");
4202 pos
= safecat(msg
, sizeof msg
, pos
, result
);
4203 pos
= safecat(msg
, sizeof msg
, pos
, "'");
4208 store_write_reset(ps
);
4213 store_write_reset(fault
);
4220 #ifdef PNG_READ_SUPPORTED
4221 /* Because we want to use the same code in both the progressive reader and the
4222 * sequential reader it is necessary to deal with the fact that the progressive
4223 * reader callbacks only have one parameter (png_get_progressive_ptr()), so this
4224 * must contain all the test parameters and all the local variables directly
4225 * accessible to the sequential reader implementation.
4227 * The technique adopted is to reinvent part of what Dijkstra termed a
4228 * 'display'; an array of pointers to the stack frames of enclosing functions so
4229 * that a nested function definition can access the local (C auto) variables of
4230 * the functions that contain its definition. In fact C provides the first
4231 * pointer (the local variables - the stack frame pointer) and the last (the
4232 * global variables - the BCPL global vector typically implemented as global
4233 * addresses), this code requires one more pointer to make the display - the
4234 * local variables (and function call parameters) of the function that actually
4235 * invokes either the progressive or sequential reader.
4237 * Perhaps confusingly this technique is confounded with classes - the
4238 * 'standard_display' defined here is sub-classed as the 'gamma_display' below.
4239 * A gamma_display is a standard_display, taking advantage of the ANSI-C
4240 * requirement that the pointer to the first member of a structure must be the
4241 * same as the pointer to the structure. This allows us to reuse standard_
4242 * functions in the gamma test code; something that could not be done with
4245 typedef struct standard_display
4247 png_store
* ps
; /* Test parameters (passed to the function) */
4248 png_byte colour_type
;
4250 png_byte red_sBIT
; /* Input data sBIT values. */
4251 png_byte green_sBIT
;
4253 png_byte alpha_sBIT
;
4255 png_uint_32 id
; /* Calculated file ID */
4256 png_uint_32 w
; /* Width of image */
4257 png_uint_32 h
; /* Height of image */
4258 int npasses
; /* Number of interlaced passes */
4259 png_uint_32 pixel_size
; /* Width of one pixel in bits */
4260 png_uint_32 bit_width
; /* Width of output row in bits */
4261 size_t cbRow
; /* Bytes in a row of the output image */
4262 int do_interlace
; /* Do interlacing internally */
4263 int is_transparent
; /* Transparency information was present. */
4264 int speed
; /* Doing a speed test */
4265 int use_update_info
;/* Call update_info, not start_image */
4271 } transparent
; /* The transparent color, if set. */
4272 int npalette
; /* Number of entries in the palette. */
4278 standard_display_init(standard_display
*dp
, png_store
* ps
, png_uint_32 id
,
4279 int do_interlace
, int use_update_info
)
4281 memset(dp
, 0, sizeof *dp
);
4284 dp
->colour_type
= COL_FROM_ID(id
);
4285 dp
->bit_depth
= DEPTH_FROM_ID(id
);
4286 if (dp
->bit_depth
< 1 || dp
->bit_depth
> 16)
4287 internal_error(ps
, "internal: bad bit depth");
4288 if (dp
->colour_type
== 3)
4289 dp
->red_sBIT
= dp
->blue_sBIT
= dp
->green_sBIT
= dp
->alpha_sBIT
= 8;
4291 dp
->red_sBIT
= dp
->blue_sBIT
= dp
->green_sBIT
= dp
->alpha_sBIT
=
4293 dp
->interlace_type
= INTERLACE_FROM_ID(id
);
4294 check_interlace_type(dp
->interlace_type
);
4296 /* All the rest are filled in after the read_info: */
4303 dp
->do_interlace
= do_interlace
;
4304 dp
->is_transparent
= 0;
4305 dp
->speed
= ps
->speed
;
4306 dp
->use_update_info
= use_update_info
;
4308 /* Preset the transparent color to black: */
4309 memset(&dp
->transparent
, 0, sizeof dp
->transparent
);
4310 /* Preset the palette to full intensity/opaque througout: */
4311 memset(dp
->palette
, 0xff, sizeof dp
->palette
);
4314 /* Initialize the palette fields - this must be done later because the palette
4315 * comes from the particular png_store_file that is selected.
4318 standard_palette_init(standard_display
*dp
)
4320 store_palette_entry
*palette
= store_current_palette(dp
->ps
, &dp
->npalette
);
4322 /* The remaining entries remain white/opaque. */
4323 if (dp
->npalette
> 0)
4325 int i
= dp
->npalette
;
4326 memcpy(dp
->palette
, palette
, i
* sizeof *palette
);
4328 /* Check for a non-opaque palette entry: */
4330 if (palette
[i
].alpha
< 255)
4334 /* GCC can't handle the more obviously optimizable version. */
4336 dp
->is_transparent
= 1;
4338 dp
->is_transparent
= 0;
4340 dp
->is_transparent
= (i
>= 0);
4345 /* Utility to read the palette from the PNG file and convert it into
4346 * store_palette format. This returns 1 if there is any transparency in the
4347 * palette (it does not check for a transparent colour in the non-palette case.)
4350 read_palette(store_palette palette
, int *npalette
, png_const_structp pp
,
4354 png_bytep trans_alpha
;
4360 if (png_get_PLTE(pp
, pi
, &pal
, npalette
) & PNG_INFO_PLTE
)
4364 if (i
<= 0 || i
> 256)
4365 png_error(pp
, "validate: invalid PLTE count");
4369 palette
[i
].red
= pal
[i
].red
;
4370 palette
[i
].green
= pal
[i
].green
;
4371 palette
[i
].blue
= pal
[i
].blue
;
4374 /* Mark the remainder of the entries with a flag value (other than
4375 * white/opaque which is the flag value stored above.)
4377 memset(palette
+ *npalette
, 126, (256-*npalette
) * sizeof *palette
);
4380 else /* !png_get_PLTE */
4382 if (*npalette
!= (-1))
4383 png_error(pp
, "validate: invalid PLTE result");
4384 /* But there is no palette, so record this: */
4386 memset(palette
, 113, sizeof (store_palette
));
4390 num
= 2; /* force error below */
4391 if ((png_get_tRNS(pp
, pi
, &trans_alpha
, &num
, 0) & PNG_INFO_tRNS
) != 0 &&
4392 (trans_alpha
!= NULL
|| num
!= 1/*returns 1 for a transparent color*/) &&
4393 /* Oops, if a palette tRNS gets expanded png_read_update_info (at least so
4394 * far as 1.5.4) does not remove the trans_alpha pointer, only num_trans,
4395 * so in the above call we get a success, we get a pointer (who knows what
4396 * to) and we get num_trans == 0:
4398 !(trans_alpha
!= NULL
&& num
== 0)) /* TODO: fix this in libpng. */
4402 /* Any of these are crash-worthy - given the implementation of
4403 * png_get_tRNS up to 1.5 an app won't crash if it just checks the
4404 * result above and fails to check that the variables it passed have
4405 * actually been filled in! Note that if the app were to pass the
4406 * last, png_color_16p, variable too it couldn't rely on this.
4408 if (trans_alpha
== NULL
|| num
<= 0 || num
> 256 || num
> *npalette
)
4409 png_error(pp
, "validate: unexpected png_get_tRNS (palette) result");
4411 for (i
=0; i
<num
; ++i
)
4412 palette
[i
].alpha
= trans_alpha
[i
];
4414 for (num
=*npalette
; i
<num
; ++i
)
4415 palette
[i
].alpha
= 255;
4418 palette
[i
].alpha
= 33; /* flag value */
4420 return 1; /* transparency */
4425 /* No palette transparency - just set the alpha channel to opaque. */
4428 for (i
=0, num
=*npalette
; i
<num
; ++i
)
4429 palette
[i
].alpha
= 255;
4432 palette
[i
].alpha
= 55; /* flag value */
4434 return 0; /* no transparency */
4438 /* Utility to validate the palette if it should not have changed (the
4439 * non-transform case).
4442 standard_palette_validate(standard_display
*dp
, png_const_structp pp
,
4446 store_palette palette
;
4448 if (read_palette(palette
, &npalette
, pp
, pi
) != dp
->is_transparent
)
4449 png_error(pp
, "validate: palette transparency changed");
4451 if (npalette
!= dp
->npalette
)
4456 pos
= safecat(msg
, sizeof msg
, pos
, "validate: palette size changed: ");
4457 pos
= safecatn(msg
, sizeof msg
, pos
, dp
->npalette
);
4458 pos
= safecat(msg
, sizeof msg
, pos
, " -> ");
4459 pos
= safecatn(msg
, sizeof msg
, pos
, npalette
);
4464 int i
= npalette
; /* npalette is aliased */
4467 if (palette
[i
].red
!= dp
->palette
[i
].red
||
4468 palette
[i
].green
!= dp
->palette
[i
].green
||
4469 palette
[i
].blue
!= dp
->palette
[i
].blue
||
4470 palette
[i
].alpha
!= dp
->palette
[i
].alpha
)
4471 png_error(pp
, "validate: PLTE or tRNS chunk changed");
4475 /* By passing a 'standard_display' the progressive callbacks can be used
4476 * directly by the sequential code, the functions suffixed "_imp" are the
4477 * implementations, the functions without the suffix are the callbacks.
4479 * The code for the info callback is split into two because this callback calls
4480 * png_read_update_info or png_start_read_image and what gets called depends on
4481 * whether the info needs updating (we want to test both calls in pngvalid.)
4484 standard_info_part1(standard_display
*dp
, png_structp pp
, png_infop pi
)
4486 if (png_get_bit_depth(pp
, pi
) != dp
->bit_depth
)
4487 png_error(pp
, "validate: bit depth changed");
4489 if (png_get_color_type(pp
, pi
) != dp
->colour_type
)
4490 png_error(pp
, "validate: color type changed");
4492 if (png_get_filter_type(pp
, pi
) != PNG_FILTER_TYPE_BASE
)
4493 png_error(pp
, "validate: filter type changed");
4495 if (png_get_interlace_type(pp
, pi
) != dp
->interlace_type
)
4496 png_error(pp
, "validate: interlacing changed");
4498 if (png_get_compression_type(pp
, pi
) != PNG_COMPRESSION_TYPE_BASE
)
4499 png_error(pp
, "validate: compression type changed");
4501 dp
->w
= png_get_image_width(pp
, pi
);
4503 if (dp
->w
!= standard_width(pp
, dp
->id
))
4504 png_error(pp
, "validate: image width changed");
4506 dp
->h
= png_get_image_height(pp
, pi
);
4508 if (dp
->h
!= standard_height(pp
, dp
->id
))
4509 png_error(pp
, "validate: image height changed");
4511 /* Record (but don't check at present) the input sBIT according to the colour
4515 png_color_8p sBIT
= 0;
4517 if (png_get_sBIT(pp
, pi
, &sBIT
) & PNG_INFO_sBIT
)
4519 int sBIT_invalid
= 0;
4522 png_error(pp
, "validate: unexpected png_get_sBIT result");
4524 if (dp
->colour_type
& PNG_COLOR_MASK_COLOR
)
4526 if (sBIT
->red
== 0 || sBIT
->red
> dp
->bit_depth
)
4529 dp
->red_sBIT
= sBIT
->red
;
4531 if (sBIT
->green
== 0 || sBIT
->green
> dp
->bit_depth
)
4534 dp
->green_sBIT
= sBIT
->green
;
4536 if (sBIT
->blue
== 0 || sBIT
->blue
> dp
->bit_depth
)
4539 dp
->blue_sBIT
= sBIT
->blue
;
4544 if (sBIT
->gray
== 0 || sBIT
->gray
> dp
->bit_depth
)
4547 dp
->blue_sBIT
= dp
->green_sBIT
= dp
->red_sBIT
= sBIT
->gray
;
4550 /* All 8 bits in tRNS for a palette image are significant - see the
4553 if (dp
->colour_type
& PNG_COLOR_MASK_ALPHA
)
4555 if (sBIT
->alpha
== 0 || sBIT
->alpha
> dp
->bit_depth
)
4558 dp
->alpha_sBIT
= sBIT
->alpha
;
4562 png_error(pp
, "validate: sBIT value out of range");
4566 /* Important: this is validating the value *before* any transforms have been
4567 * put in place. It doesn't matter for the standard tests, where there are
4568 * no transforms, but it does for other tests where rowbytes may change after
4569 * png_read_update_info.
4571 if (png_get_rowbytes(pp
, pi
) != standard_rowsize(pp
, dp
->id
))
4572 png_error(pp
, "validate: row size changed");
4574 /* Validate the colour type 3 palette (this can be present on other color
4577 standard_palette_validate(dp
, pp
, pi
);
4579 /* In any case always check for a tranparent color (notice that the
4580 * colour type 3 case must not give a successful return on the get_tRNS call
4581 * with these arguments!)
4584 png_color_16p trans_color
= 0;
4586 if (png_get_tRNS(pp
, pi
, 0, 0, &trans_color
) & PNG_INFO_tRNS
)
4588 if (trans_color
== 0)
4589 png_error(pp
, "validate: unexpected png_get_tRNS (color) result");
4591 switch (dp
->colour_type
)
4594 dp
->transparent
.red
= dp
->transparent
.green
= dp
->transparent
.blue
=
4596 dp
->is_transparent
= 1;
4600 dp
->transparent
.red
= trans_color
->red
;
4601 dp
->transparent
.green
= trans_color
->green
;
4602 dp
->transparent
.blue
= trans_color
->blue
;
4603 dp
->is_transparent
= 1;
4607 /* Not expected because it should result in the array case
4610 png_error(pp
, "validate: unexpected png_get_tRNS result");
4614 png_error(pp
, "validate: invalid tRNS chunk with alpha image");
4619 /* Read the number of passes - expected to match the value used when
4620 * creating the image (interlaced or not). This has the side effect of
4621 * turning on interlace handling (if do_interlace is not set.)
4623 dp
->npasses
= npasses_from_interlace_type(pp
, dp
->interlace_type
);
4624 if (!dp
->do_interlace
&& dp
->npasses
!= png_set_interlace_handling(pp
))
4625 png_error(pp
, "validate: file changed interlace type");
4627 /* Caller calls png_read_update_info or png_start_read_image now, then calls
4632 /* This must be called *after* the png_read_update_info call to get the correct
4633 * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed
4637 standard_info_part2(standard_display
*dp
, png_const_structp pp
,
4638 png_const_infop pi
, int nImages
)
4640 /* Record cbRow now that it can be found. */
4641 dp
->pixel_size
= bit_size(pp
, png_get_color_type(pp
, pi
),
4642 png_get_bit_depth(pp
, pi
));
4643 dp
->bit_width
= png_get_image_width(pp
, pi
) * dp
->pixel_size
;
4644 dp
->cbRow
= png_get_rowbytes(pp
, pi
);
4646 /* Validate the rowbytes here again. */
4647 if (dp
->cbRow
!= (dp
->bit_width
+7)/8)
4648 png_error(pp
, "bad png_get_rowbytes calculation");
4650 /* Then ensure there is enough space for the output image(s). */
4651 store_ensure_image(dp
->ps
, pp
, nImages
, dp
->cbRow
, dp
->h
);
4655 standard_info_imp(standard_display
*dp
, png_structp pp
, png_infop pi
,
4658 /* Note that the validation routine has the side effect of turning on
4659 * interlace handling in the subsequent code.
4661 standard_info_part1(dp
, pp
, pi
);
4663 /* And the info callback has to call this (or png_read_update_info - see
4664 * below in the png_modifier code for that variant.
4666 if (dp
->use_update_info
)
4668 /* For debugging the effect of multiple calls: */
4669 int i
= dp
->use_update_info
;
4671 png_read_update_info(pp
, pi
);
4675 png_start_read_image(pp
);
4677 /* Validate the height, width and rowbytes plus ensure that sufficient buffer
4678 * exists for decoding the image.
4680 standard_info_part2(dp
, pp
, pi
, nImages
);
4684 standard_info(png_structp pp
, png_infop pi
)
4686 standard_display
*dp
= voidcast(standard_display
*,
4687 png_get_progressive_ptr(pp
));
4689 /* Call with nImages==1 because the progressive reader can only produce one
4692 standard_info_imp(dp
, pp
, pi
, 1 /*only one image*/);
4696 progressive_row(png_structp ppIn
, png_bytep new_row
, png_uint_32 y
, int pass
)
4698 png_const_structp pp
= ppIn
;
4699 PNG_CONST standard_display
*dp
= voidcast(standard_display
*,
4700 png_get_progressive_ptr(pp
));
4702 /* When handling interlacing some rows will be absent in each pass, the
4703 * callback still gets called, but with a NULL pointer. This is checked
4704 * in the 'else' clause below. We need our own 'cbRow', but we can't call
4705 * png_get_rowbytes because we got no info structure.
4707 if (new_row
!= NULL
)
4711 /* In the case where the reader doesn't do the interlace it gives
4712 * us the y in the sub-image:
4714 if (dp
->do_interlace
&& dp
->interlace_type
== PNG_INTERLACE_ADAM7
)
4716 #ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED
4717 /* Use this opportunity to validate the png 'current' APIs: */
4718 if (y
!= png_get_current_row_number(pp
))
4719 png_error(pp
, "png_get_current_row_number is broken");
4721 if (pass
!= png_get_current_pass_number(pp
))
4722 png_error(pp
, "png_get_current_pass_number is broken");
4725 y
= PNG_ROW_FROM_PASS_ROW(y
, pass
);
4728 /* Validate this just in case. */
4730 png_error(pp
, "invalid y to progressive row callback");
4732 row
= store_image_row(dp
->ps
, pp
, 0, y
);
4734 #ifdef PNG_READ_INTERLACING_SUPPORTED
4735 /* Combine the new row into the old: */
4736 if (dp
->do_interlace
)
4738 if (dp
->interlace_type
== PNG_INTERLACE_ADAM7
)
4739 deinterlace_row(row
, new_row
, dp
->pixel_size
, dp
->w
, pass
);
4741 row_copy(row
, new_row
, dp
->pixel_size
* dp
->w
);
4744 png_progressive_combine_row(pp
, row
, new_row
);
4745 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4748 #ifdef PNG_READ_INTERLACING_SUPPORTED
4749 else if (dp
->interlace_type
== PNG_INTERLACE_ADAM7
&&
4750 PNG_ROW_IN_INTERLACE_PASS(y
, pass
) &&
4751 PNG_PASS_COLS(dp
->w
, pass
) > 0)
4752 png_error(pp
, "missing row in progressive de-interlacing");
4753 #endif /* PNG_READ_INTERLACING_SUPPORTED */
4757 sequential_row(standard_display
*dp
, png_structp pp
, png_infop pi
,
4758 PNG_CONST
int iImage
, PNG_CONST
int iDisplay
)
4760 PNG_CONST
int npasses
= dp
->npasses
;
4761 PNG_CONST
int do_interlace
= dp
->do_interlace
&&
4762 dp
->interlace_type
== PNG_INTERLACE_ADAM7
;
4763 PNG_CONST png_uint_32 height
= standard_height(pp
, dp
->id
);
4764 PNG_CONST png_uint_32 width
= standard_width(pp
, dp
->id
);
4765 PNG_CONST png_store
* ps
= dp
->ps
;
4768 for (pass
=0; pass
<npasses
; ++pass
)
4771 png_uint_32 wPass
= PNG_PASS_COLS(width
, pass
);
4773 for (y
=0; y
<height
; ++y
)
4777 /* wPass may be zero or this row may not be in this pass.
4778 * png_read_row must not be called in either case.
4780 if (wPass
> 0 && PNG_ROW_IN_INTERLACE_PASS(y
, pass
))
4782 /* Read the row into a pair of temporary buffers, then do the
4783 * merge here into the output rows.
4785 png_byte row
[STANDARD_ROWMAX
], display
[STANDARD_ROWMAX
];
4787 /* The following aids (to some extent) error detection - we can
4788 * see where png_read_row wrote. Use opposite values in row and
4789 * display to make this easier. Don't use 0xff (which is used in
4790 * the image write code to fill unused bits) or 0 (which is a
4791 * likely value to overwrite unused bits with).
4793 memset(row
, 0xc5, sizeof row
);
4794 memset(display
, 0x5c, sizeof display
);
4796 png_read_row(pp
, row
, display
);
4799 deinterlace_row(store_image_row(ps
, pp
, iImage
, y
), row
,
4800 dp
->pixel_size
, dp
->w
, pass
);
4803 deinterlace_row(store_image_row(ps
, pp
, iDisplay
, y
), display
,
4804 dp
->pixel_size
, dp
->w
, pass
);
4809 iImage
>= 0 ?
store_image_row(ps
, pp
, iImage
, y
) : NULL
,
4810 iDisplay
>= 0 ?
store_image_row(ps
, pp
, iDisplay
, y
) : NULL
);
4814 /* And finish the read operation (only really necessary if the caller wants
4815 * to find additional data in png_info from chunks after the last IDAT.)
4817 png_read_end(pp
, pi
);
4820 #ifdef PNG_TEXT_SUPPORTED
4822 standard_check_text(png_const_structp pp
, png_const_textp tp
,
4823 png_const_charp keyword
, png_const_charp text
)
4826 size_t pos
= safecat(msg
, sizeof msg
, 0, "text: ");
4829 pos
= safecat(msg
, sizeof msg
, pos
, keyword
);
4830 pos
= safecat(msg
, sizeof msg
, pos
, ": ");
4833 if (tp
->compression
!= TEXT_COMPRESSION
)
4837 sprintf(buf
, "compression [%d->%d], ", TEXT_COMPRESSION
,
4839 pos
= safecat(msg
, sizeof msg
, pos
, buf
);
4842 if (tp
->key
== NULL
|| strcmp(tp
->key
, keyword
) != 0)
4844 pos
= safecat(msg
, sizeof msg
, pos
, "keyword \"");
4845 if (tp
->key
!= NULL
)
4847 pos
= safecat(msg
, sizeof msg
, pos
, tp
->key
);
4848 pos
= safecat(msg
, sizeof msg
, pos
, "\", ");
4852 pos
= safecat(msg
, sizeof msg
, pos
, "null, ");
4855 if (tp
->text
== NULL
)
4856 pos
= safecat(msg
, sizeof msg
, pos
, "text lost, ");
4860 if (tp
->text_length
!= strlen(text
))
4863 sprintf(buf
, "text length changed[%lu->%lu], ",
4864 (unsigned long)strlen(text
), (unsigned long)tp
->text_length
);
4865 pos
= safecat(msg
, sizeof msg
, pos
, buf
);
4868 if (strcmp(tp
->text
, text
) != 0)
4870 pos
= safecat(msg
, sizeof msg
, pos
, "text becomes \"");
4871 pos
= safecat(msg
, sizeof msg
, pos
, tp
->text
);
4872 pos
= safecat(msg
, sizeof msg
, pos
, "\" (was \"");
4873 pos
= safecat(msg
, sizeof msg
, pos
, text
);
4874 pos
= safecat(msg
, sizeof msg
, pos
, "\"), ");
4878 if (tp
->itxt_length
!= 0)
4879 pos
= safecat(msg
, sizeof msg
, pos
, "iTXt length set, ");
4881 if (tp
->lang
!= NULL
)
4883 pos
= safecat(msg
, sizeof msg
, pos
, "iTXt language \"");
4884 pos
= safecat(msg
, sizeof msg
, pos
, tp
->lang
);
4885 pos
= safecat(msg
, sizeof msg
, pos
, "\", ");
4888 if (tp
->lang_key
!= NULL
)
4890 pos
= safecat(msg
, sizeof msg
, pos
, "iTXt keyword \"");
4891 pos
= safecat(msg
, sizeof msg
, pos
, tp
->lang_key
);
4892 pos
= safecat(msg
, sizeof msg
, pos
, "\", ");
4897 msg
[pos
-2] = '\0'; /* Remove the ", " at the end */
4903 standard_text_validate(standard_display
*dp
, png_const_structp pp
,
4904 png_infop pi
, int check_end
)
4906 png_textp tp
= NULL
;
4907 png_uint_32 num_text
= png_get_text(pp
, pi
, &tp
, NULL
);
4909 if (num_text
== 2 && tp
!= NULL
)
4911 standard_check_text(pp
, tp
, "image name", dp
->ps
->current
->name
);
4913 /* This exists because prior to 1.5.18 the progressive reader left the
4914 * png_struct z_stream unreset at the end of the image, so subsequent
4915 * attempts to use it simply returns Z_STREAM_END.
4918 standard_check_text(pp
, tp
+1, "end marker", "end");
4925 sprintf(msg
, "expected two text items, got %lu",
4926 (unsigned long)num_text
);
4931 # define standard_text_validate(dp,pp,pi,check_end) ((void)0)
4935 standard_row_validate(standard_display
*dp
, png_const_structp pp
,
4936 int iImage
, int iDisplay
, png_uint_32 y
)
4939 png_byte std
[STANDARD_ROWMAX
];
4941 /* The row must be pre-initialized to the magic number here for the size
4944 memset(std
, 178, sizeof std
);
4945 standard_row(pp
, std
, dp
->id
, y
);
4947 /* At the end both the 'row' and 'display' arrays should end up identical.
4948 * In earlier passes 'row' will be partially filled in, with only the pixels
4949 * that have been read so far, but 'display' will have those pixels
4950 * replicated to fill the unread pixels while reading an interlaced image.
4951 #if PNG_LIBPNG_VER < 10506
4952 * The side effect inside the libpng sequential reader is that the 'row'
4953 * array retains the correct values for unwritten pixels within the row
4954 * bytes, while the 'display' array gets bits off the end of the image (in
4955 * the last byte) trashed. Unfortunately in the progressive reader the
4956 * row bytes are always trashed, so we always do a pixel_cmp here even though
4957 * a memcmp of all cbRow bytes will succeed for the sequential reader.
4961 (where
= pixel_cmp(std
, store_image_row(dp
->ps
, pp
, iImage
, y
),
4962 dp
->bit_width
)) != 0)
4965 sprintf(msg
, "PNG image row[%lu][%d] changed from %.2x to %.2x",
4966 (unsigned long)y
, where
-1, std
[where
-1],
4967 store_image_row(dp
->ps
, pp
, iImage
, y
)[where
-1]);
4971 #if PNG_LIBPNG_VER < 10506
4972 /* In this case use pixel_cmp because we need to compare a partial
4973 * byte at the end of the row if the row is not an exact multiple
4974 * of 8 bits wide. (This is fixed in libpng-1.5.6 and pixel_cmp is
4975 * changed to match!)
4978 if (iDisplay
>= 0 &&
4979 (where
= pixel_cmp(std
, store_image_row(dp
->ps
, pp
, iDisplay
, y
),
4980 dp
->bit_width
)) != 0)
4983 sprintf(msg
, "display row[%lu][%d] changed from %.2x to %.2x",
4984 (unsigned long)y
, where
-1, std
[where
-1],
4985 store_image_row(dp
->ps
, pp
, iDisplay
, y
)[where
-1]);
4991 standard_image_validate(standard_display
*dp
, png_const_structp pp
, int iImage
,
4997 store_image_check(dp
->ps
, pp
, iImage
);
5000 store_image_check(dp
->ps
, pp
, iDisplay
);
5002 for (y
=0; y
<dp
->h
; ++y
)
5003 standard_row_validate(dp
, pp
, iImage
, iDisplay
, y
);
5005 /* This avoids false positives if the validation code is never called! */
5006 dp
->ps
->validated
= 1;
5010 standard_end(png_structp ppIn
, png_infop pi
)
5012 png_const_structp pp
= ppIn
;
5013 standard_display
*dp
= voidcast(standard_display
*,
5014 png_get_progressive_ptr(pp
));
5018 /* Validate the image - progressive reading only produces one variant for
5019 * interlaced images.
5021 standard_text_validate(dp
, pp
, pi
,
5022 PNG_LIBPNG_VER
>= 10518/*check_end: see comments above*/);
5023 standard_image_validate(dp
, pp
, 0, -1);
5026 /* A single test run checking the standard image to ensure it is not damaged. */
5028 standard_test(png_store
* PNG_CONST psIn
, png_uint_32 PNG_CONST id
,
5029 int do_interlace
, int use_update_info
)
5032 context(psIn
, fault
);
5034 /* Set up the display (stack frame) variables from the arguments to the
5035 * function and initialize the locals that are filled in later.
5037 standard_display_init(&d
, psIn
, id
, do_interlace
, use_update_info
);
5039 /* Everything is protected by a Try/Catch. The functions called also
5040 * typically have local Try/Catch blocks.
5047 /* Get a png_struct for reading the image. This will throw an error if it
5048 * fails, so we don't need to check the result.
5050 pp
= set_store_for_read(d
.ps
, &pi
, d
.id
,
5051 d
.do_interlace ?
(d
.ps
->progressive ?
5052 "pngvalid progressive deinterlacer" :
5053 "pngvalid sequential deinterlacer") : (d
.ps
->progressive ?
5054 "progressive reader" : "sequential reader"));
5056 /* Initialize the palette correctly from the png_store_file. */
5057 standard_palette_init(&d
);
5059 /* Introduce the correct read function. */
5060 if (d
.ps
->progressive
)
5062 png_set_progressive_read_fn(pp
, &d
, standard_info
, progressive_row
,
5065 /* Now feed data into the reader until we reach the end: */
5066 store_progressive_read(d
.ps
, pp
, pi
);
5070 /* Note that this takes the store, not the display. */
5071 png_set_read_fn(pp
, d
.ps
, store_read
);
5073 /* Check the header values: */
5074 png_read_info(pp
, pi
);
5076 /* The code tests both versions of the images that the sequential
5077 * reader can produce.
5079 standard_info_imp(&d
, pp
, pi
, 2 /*images*/);
5081 /* Need the total bytes in the image below; we can't get to this point
5082 * unless the PNG file values have been checked against the expected
5086 sequential_row(&d
, pp
, pi
, 0, 1);
5088 /* After the last pass loop over the rows again to check that the
5093 standard_text_validate(&d
, pp
, pi
, 1/*check_end*/);
5094 standard_image_validate(&d
, pp
, 0, 1);
5097 d
.ps
->validated
= 1;
5101 /* Check for validation. */
5102 if (!d
.ps
->validated
)
5103 png_error(pp
, "image read failed silently");
5105 /* Successful completion. */
5109 d
.ps
= fault
; /* make sure this hasn't been clobbered. */
5111 /* In either case clean up the store. */
5112 store_read_reset(d
.ps
);
5116 test_standard(png_modifier
* PNG_CONST pm
, png_byte PNG_CONST colour_type
,
5117 int bdlo
, int PNG_CONST bdhi
)
5119 for (; bdlo
<= bdhi
; ++bdlo
)
5123 for (interlace_type
= PNG_INTERLACE_NONE
;
5124 interlace_type
< INTERLACE_LAST
; ++interlace_type
)
5126 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5127 interlace_type
, 0, 0, 0), 0/*do_interlace*/, pm
->use_update_info
);
5134 return 1; /* keep going */
5138 perform_standard_test(png_modifier
*pm
)
5140 /* Test each colour type over the valid range of bit depths (expressed as
5141 * log2(bit_depth) in turn, stop as soon as any error is detected.
5143 if (!test_standard(pm
, 0, 0, READ_BDHI
))
5146 if (!test_standard(pm
, 2, 3, READ_BDHI
))
5149 if (!test_standard(pm
, 3, 0, 3))
5152 if (!test_standard(pm
, 4, 3, READ_BDHI
))
5155 if (!test_standard(pm
, 6, 3, READ_BDHI
))
5160 /********************************** SIZE TESTS ********************************/
5162 test_size(png_modifier
* PNG_CONST pm
, png_byte PNG_CONST colour_type
,
5163 int bdlo
, int PNG_CONST bdhi
)
5165 /* Run the tests on each combination.
5167 * NOTE: on my 32 bit x86 each of the following blocks takes
5168 * a total of 3.5 seconds if done across every combo of bit depth
5169 * width and height. This is a waste of time in practice, hence the
5170 * hinc and winc stuff:
5172 static PNG_CONST png_byte hinc
[] = {1, 3, 11, 1, 5};
5173 static PNG_CONST png_byte winc
[] = {1, 9, 5, 7, 1};
5174 for (; bdlo
<= bdhi
; ++bdlo
)
5178 for (h
=1; h
<=16; h
+=hinc
[bdlo
]) for (w
=1; w
<=16; w
+=winc
[bdlo
])
5180 /* First test all the 'size' images against the sequential
5181 * reader using libpng to deinterlace (where required.) This
5182 * validates the write side of libpng. There are four possibilities
5185 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5186 PNG_INTERLACE_NONE
, w
, h
, 0), 0/*do_interlace*/,
5187 pm
->use_update_info
);
5192 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5193 PNG_INTERLACE_NONE
, w
, h
, 1), 0/*do_interlace*/,
5194 pm
->use_update_info
);
5199 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
5200 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5201 PNG_INTERLACE_ADAM7
, w
, h
, 0), 0/*do_interlace*/,
5202 pm
->use_update_info
);
5207 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5208 PNG_INTERLACE_ADAM7
, w
, h
, 1), 0/*do_interlace*/,
5209 pm
->use_update_info
);
5215 /* Now validate the interlaced read side - do_interlace true,
5216 * in the progressive case this does actually make a difference
5217 * to the code used in the non-interlaced case too.
5219 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5220 PNG_INTERLACE_NONE
, w
, h
, 0), 1/*do_interlace*/,
5221 pm
->use_update_info
);
5226 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
5227 standard_test(&pm
->this, FILEID(colour_type
, DEPTH(bdlo
), 0/*palette*/,
5228 PNG_INTERLACE_ADAM7
, w
, h
, 0), 1/*do_interlace*/,
5229 pm
->use_update_info
);
5237 return 1; /* keep going */
5241 perform_size_test(png_modifier
*pm
)
5243 /* Test each colour type over the valid range of bit depths (expressed as
5244 * log2(bit_depth) in turn, stop as soon as any error is detected.
5246 if (!test_size(pm
, 0, 0, READ_BDHI
))
5249 if (!test_size(pm
, 2, 3, READ_BDHI
))
5252 /* For the moment don't do the palette test - it's a waste of time when
5253 * compared to the grayscale test.
5256 if (!test_size(pm
, 3, 0, 3))
5260 if (!test_size(pm
, 4, 3, READ_BDHI
))
5263 if (!test_size(pm
, 6, 3, READ_BDHI
))
5268 /******************************* TRANSFORM TESTS ******************************/
5269 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
5270 /* A set of tests to validate libpng image transforms. The possibilities here
5271 * are legion because the transforms can be combined in a combinatorial
5272 * fashion. To deal with this some measure of restraint is required, otherwise
5273 * the tests would take forever.
5275 typedef struct image_pixel
5277 /* A local (pngvalid) representation of a PNG pixel, in all its
5280 unsigned int red
, green
, blue
, alpha
; /* For non-palette images. */
5281 unsigned int palette_index
; /* For a palette image. */
5282 png_byte colour_type
; /* As in the spec. */
5283 png_byte bit_depth
; /* Defines bit size in row */
5284 png_byte sample_depth
; /* Scale of samples */
5285 int have_tRNS
; /* tRNS chunk may need processing */
5287 /* For checking the code calculates double precision floating point values
5288 * along with an error value, accumulated from the transforms. Because an
5289 * sBIT setting allows larger error bounds (indeed, by the spec, apparently
5290 * up to just less than +/-1 in the scaled value) the *lowest* sBIT for each
5291 * channel is stored. This sBIT value is folded in to the stored error value
5292 * at the end of the application of the transforms to the pixel.
5294 double redf
, greenf
, bluef
, alphaf
;
5295 double rede
, greene
, bluee
, alphae
;
5296 png_byte red_sBIT
, green_sBIT
, blue_sBIT
, alpha_sBIT
;
5299 /* Shared utility function, see below. */
5301 image_pixel_setf(image_pixel
*this, unsigned int max
)
5303 this->redf
= this->red
/ (double)max
;
5304 this->greenf
= this->green
/ (double)max
;
5305 this->bluef
= this->blue
/ (double)max
;
5306 this->alphaf
= this->alpha
/ (double)max
;
5308 if (this->red
< max
)
5309 this->rede
= this->redf
* DBL_EPSILON
;
5312 if (this->green
< max
)
5313 this->greene
= this->greenf
* DBL_EPSILON
;
5316 if (this->blue
< max
)
5317 this->bluee
= this->bluef
* DBL_EPSILON
;
5320 if (this->alpha
< max
)
5321 this->alphae
= this->alphaf
* DBL_EPSILON
;
5326 /* Initialize the structure for the next pixel - call this before doing any
5327 * transforms and call it for each pixel since all the fields may need to be
5331 image_pixel_init(image_pixel
*this, png_const_bytep row
, png_byte colour_type
,
5332 png_byte bit_depth
, png_uint_32 x
, store_palette palette
)
5334 PNG_CONST png_byte sample_depth
= (png_byte
)(colour_type
==
5335 PNG_COLOR_TYPE_PALETTE ?
8 : bit_depth
);
5336 PNG_CONST
unsigned int max
= (1U<<sample_depth
)-1;
5338 /* Initially just set everything to the same number and the alpha to opaque.
5339 * Note that this currently assumes a simple palette where entry x has colour
5342 this->palette_index
= this->red
= this->green
= this->blue
=
5343 sample(row
, colour_type
, bit_depth
, x
, 0);
5345 this->red_sBIT
= this->green_sBIT
= this->blue_sBIT
= this->alpha_sBIT
=
5348 /* Then override as appropriate: */
5349 if (colour_type
== 3) /* palette */
5351 /* This permits the caller to default to the sample value. */
5354 PNG_CONST
unsigned int i
= this->palette_index
;
5356 this->red
= palette
[i
].red
;
5357 this->green
= palette
[i
].green
;
5358 this->blue
= palette
[i
].blue
;
5359 this->alpha
= palette
[i
].alpha
;
5363 else /* not palette */
5367 if (colour_type
& 2)
5369 this->green
= sample(row
, colour_type
, bit_depth
, x
, 1);
5370 this->blue
= sample(row
, colour_type
, bit_depth
, x
, 2);
5373 if (colour_type
& 4)
5374 this->alpha
= sample(row
, colour_type
, bit_depth
, x
, ++i
);
5377 /* Calculate the scaled values, these are simply the values divided by
5378 * 'max' and the error is initialized to the double precision epsilon value
5379 * from the header file.
5381 image_pixel_setf(this, max
);
5383 /* Store the input information for use in the transforms - these will
5384 * modify the information.
5386 this->colour_type
= colour_type
;
5387 this->bit_depth
= bit_depth
;
5388 this->sample_depth
= sample_depth
;
5389 this->have_tRNS
= 0;
5392 /* Convert a palette image to an rgb image. This necessarily converts the tRNS
5393 * chunk at the same time, because the tRNS will be in palette form. The way
5394 * palette validation works means that the original palette is never updated,
5395 * instead the image_pixel value from the row contains the RGB of the
5396 * corresponding palette entry and *this* is updated. Consequently this routine
5397 * only needs to change the colour type information.
5400 image_pixel_convert_PLTE(image_pixel
*this)
5402 if (this->colour_type
== PNG_COLOR_TYPE_PALETTE
)
5404 if (this->have_tRNS
)
5406 this->colour_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
5407 this->have_tRNS
= 0;
5410 this->colour_type
= PNG_COLOR_TYPE_RGB
;
5412 /* The bit depth of the row changes at this point too (notice that this is
5413 * the row format, not the sample depth, which is separate.)
5415 this->bit_depth
= 8;
5419 /* Add an alpha channel; this will import the tRNS information because tRNS is
5420 * not valid in an alpha image. The bit depth will invariably be set to at
5421 * least 8. Palette images will be converted to alpha (using the above API).
5424 image_pixel_add_alpha(image_pixel
*this, PNG_CONST standard_display
*display
)
5426 if (this->colour_type
== PNG_COLOR_TYPE_PALETTE
)
5427 image_pixel_convert_PLTE(this);
5429 if ((this->colour_type
& PNG_COLOR_MASK_ALPHA
) == 0)
5431 if (this->colour_type
== PNG_COLOR_TYPE_GRAY
)
5433 if (this->bit_depth
< 8)
5434 this->bit_depth
= 8;
5436 if (this->have_tRNS
)
5438 this->have_tRNS
= 0;
5440 /* Check the input, original, channel value here against the
5441 * original tRNS gray chunk valie.
5443 if (this->red
== display
->transparent
.red
)
5451 this->colour_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
5454 else if (this->colour_type
== PNG_COLOR_TYPE_RGB
)
5456 if (this->have_tRNS
)
5458 this->have_tRNS
= 0;
5460 /* Again, check the exact input values, not the current transformed
5463 if (this->red
== display
->transparent
.red
&&
5464 this->green
== display
->transparent
.green
&&
5465 this->blue
== display
->transparent
.blue
)
5470 this->colour_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
5474 /* The error in the alpha is zero and the sBIT value comes from the
5475 * original sBIT data (actually it will always be the original bit depth).
5478 this->alpha_sBIT
= display
->alpha_sBIT
;
5482 struct transform_display
;
5483 typedef struct image_transform
5485 /* The name of this transform: a string. */
5486 PNG_CONST
char *name
;
5488 /* Each transform can be disabled from the command line: */
5491 /* The global list of transforms; read only. */
5492 struct image_transform
*PNG_CONST list
;
5494 /* The global count of the number of times this transform has been set on an
5497 unsigned int global_use
;
5499 /* The local count of the number of times this transform has been set. */
5500 unsigned int local_use
;
5502 /* The next transform in the list, each transform must call its own next
5503 * transform after it has processed the pixel successfully.
5505 PNG_CONST
struct image_transform
*next
;
5507 /* A single transform for the image, expressed as a series of function
5508 * callbacks and some space for values.
5510 * First a callback to add any required modifications to the png_modifier;
5511 * this gets called just before the modifier is set up for read.
5513 void (*ini
)(PNG_CONST
struct image_transform
*this,
5514 struct transform_display
*that
);
5516 /* And a callback to set the transform on the current png_read_struct:
5518 void (*set
)(PNG_CONST
struct image_transform
*this,
5519 struct transform_display
*that
, png_structp pp
, png_infop pi
);
5521 /* Then a transform that takes an input pixel in one PNG format or another
5522 * and modifies it by a pngvalid implementation of the transform (thus
5523 * duplicating the libpng intent without, we hope, duplicating the bugs
5524 * in the libpng implementation!) The png_structp is solely to allow error
5525 * reporting via png_error and png_warning.
5527 void (*mod
)(PNG_CONST
struct image_transform
*this, image_pixel
*that
,
5528 png_const_structp pp
, PNG_CONST
struct transform_display
*display
);
5530 /* Add this transform to the list and return true if the transform is
5531 * meaningful for this colour type and bit depth - if false then the
5532 * transform should have no effect on the image so there's not a lot of
5535 int (*add
)(struct image_transform
*this,
5536 PNG_CONST
struct image_transform
**that
, png_byte colour_type
,
5537 png_byte bit_depth
);
5540 typedef struct transform_display
5542 standard_display
this;
5546 PNG_CONST image_transform
* transform_list
;
5548 /* Local variables */
5549 png_byte output_colour_type
;
5550 png_byte output_bit_depth
;
5552 /* Modifications (not necessarily used.) */
5553 gama_modification gama_mod
;
5554 chrm_modification chrm_mod
;
5555 srgb_modification srgb_mod
;
5556 } transform_display
;
5558 /* Set sRGB, cHRM and gAMA transforms as required by the current encoding. */
5560 transform_set_encoding(transform_display
*this)
5562 /* Set up the png_modifier '_current' fields then use these to determine how
5563 * to add appropriate chunks.
5565 png_modifier
*pm
= this->pm
;
5567 modifier_set_encoding(pm
);
5569 if (modifier_color_encoding_is_set(pm
))
5571 if (modifier_color_encoding_is_sRGB(pm
))
5572 srgb_modification_init(&this->srgb_mod
, pm
, PNG_sRGB_INTENT_ABSOLUTE
);
5576 /* Set gAMA and cHRM separately. */
5577 gama_modification_init(&this->gama_mod
, pm
, pm
->current_gamma
);
5579 if (pm
->current_encoding
!= 0)
5580 chrm_modification_init(&this->chrm_mod
, pm
, pm
->current_encoding
);
5585 /* Three functions to end the list: */
5587 image_transform_ini_end(PNG_CONST image_transform
*this,
5588 transform_display
*that
)
5595 image_transform_set_end(PNG_CONST image_transform
*this,
5596 transform_display
*that
, png_structp pp
, png_infop pi
)
5604 /* At the end of the list recalculate the output image pixel value from the
5605 * double precision values set up by the preceding 'mod' calls:
5608 sample_scale(double sample_value
, unsigned int scale
)
5610 sample_value
= floor(sample_value
* scale
+ .5);
5612 /* Return NaN as 0: */
5613 if (!(sample_value
> 0))
5615 else if (sample_value
> scale
)
5616 sample_value
= scale
;
5618 return (unsigned int)sample_value
;
5622 image_transform_mod_end(PNG_CONST image_transform
*this, image_pixel
*that
,
5623 png_const_structp pp
, PNG_CONST transform_display
*display
)
5625 PNG_CONST
unsigned int scale
= (1U<<that
->sample_depth
)-1;
5631 /* At the end recalculate the digitized red green and blue values according
5632 * to the current sample_depth of the pixel.
5634 * The sample value is simply scaled to the maximum, checking for over
5635 * and underflow (which can both happen for some image transforms,
5636 * including simple size scaling, though libpng doesn't do that at present.
5638 that
->red
= sample_scale(that
->redf
, scale
);
5640 /* The error value is increased, at the end, according to the lowest sBIT
5641 * value seen. Common sense tells us that the intermediate integer
5642 * representations are no more accurate than +/- 0.5 in the integral values,
5643 * the sBIT allows the implementation to be worse than this. In addition the
5644 * PNG specification actually permits any error within the range (-1..+1),
5645 * but that is ignored here. Instead the final digitized value is compared,
5646 * below to the digitized value of the error limits - this has the net effect
5647 * of allowing (almost) +/-1 in the output value. It's difficult to see how
5648 * any algorithm that digitizes intermediate results can be more accurate.
5650 that
->rede
+= 1./(2*((1U<<that
->red_sBIT
)-1));
5652 if (that
->colour_type
& PNG_COLOR_MASK_COLOR
)
5654 that
->green
= sample_scale(that
->greenf
, scale
);
5655 that
->blue
= sample_scale(that
->bluef
, scale
);
5656 that
->greene
+= 1./(2*((1U<<that
->green_sBIT
)-1));
5657 that
->bluee
+= 1./(2*((1U<<that
->blue_sBIT
)-1));
5661 that
->blue
= that
->green
= that
->red
;
5662 that
->bluef
= that
->greenf
= that
->redf
;
5663 that
->bluee
= that
->greene
= that
->rede
;
5666 if ((that
->colour_type
& PNG_COLOR_MASK_ALPHA
) ||
5667 that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
5669 that
->alpha
= sample_scale(that
->alphaf
, scale
);
5670 that
->alphae
+= 1./(2*((1U<<that
->alpha_sBIT
)-1));
5674 that
->alpha
= scale
; /* opaque */
5675 that
->alpha
= 1; /* Override this. */
5676 that
->alphae
= 0; /* It's exact ;-) */
5680 /* Static 'end' structure: */
5681 static image_transform image_transform_end
=
5689 image_transform_ini_end
,
5690 image_transform_set_end
,
5691 image_transform_mod_end
,
5692 0 /* never called, I want it to crash if it is! */
5695 /* Reader callbacks and implementations, where they differ from the standard
5699 transform_display_init(transform_display
*dp
, png_modifier
*pm
, png_uint_32 id
,
5700 PNG_CONST image_transform
*transform_list
)
5702 memset(dp
, 0, sizeof *dp
);
5704 /* Standard fields */
5705 standard_display_init(&dp
->this, &pm
->this, id
, 0/*do_interlace*/,
5706 pm
->use_update_info
);
5708 /* Parameter fields */
5710 dp
->transform_list
= transform_list
;
5712 /* Local variable fields */
5713 dp
->output_colour_type
= 255; /* invalid */
5714 dp
->output_bit_depth
= 255; /* invalid */
5718 transform_info_imp(transform_display
*dp
, png_structp pp
, png_infop pi
)
5720 /* Reuse the standard stuff as appropriate. */
5721 standard_info_part1(&dp
->this, pp
, pi
);
5723 /* Now set the list of transforms. */
5724 dp
->transform_list
->set(dp
->transform_list
, dp
, pp
, pi
);
5726 /* Update the info structure for these transforms: */
5728 int i
= dp
->this.use_update_info
;
5729 /* Always do one call, even if use_update_info is 0. */
5731 png_read_update_info(pp
, pi
);
5735 /* And get the output information into the standard_display */
5736 standard_info_part2(&dp
->this, pp
, pi
, 1/*images*/);
5738 /* Plus the extra stuff we need for the transform tests: */
5739 dp
->output_colour_type
= png_get_color_type(pp
, pi
);
5740 dp
->output_bit_depth
= png_get_bit_depth(pp
, pi
);
5742 /* Validate the combination of colour type and bit depth that we are getting
5743 * out of libpng; the semantics of something not in the PNG spec are, at
5746 switch (dp
->output_colour_type
)
5748 case PNG_COLOR_TYPE_PALETTE
:
5749 if (dp
->output_bit_depth
> 8) goto error
;
5751 case PNG_COLOR_TYPE_GRAY
:
5752 if (dp
->output_bit_depth
== 1 || dp
->output_bit_depth
== 2 ||
5753 dp
->output_bit_depth
== 4)
5757 if (dp
->output_bit_depth
== 8 || dp
->output_bit_depth
== 16)
5765 pos
= safecat(message
, sizeof message
, 0,
5766 "invalid final bit depth: colour type(");
5767 pos
= safecatn(message
, sizeof message
, pos
, dp
->output_colour_type
);
5768 pos
= safecat(message
, sizeof message
, pos
, ") with bit depth: ");
5769 pos
= safecatn(message
, sizeof message
, pos
, dp
->output_bit_depth
);
5771 png_error(pp
, message
);
5775 /* Use a test pixel to check that the output agrees with what we expect -
5776 * this avoids running the whole test if the output is unexpected.
5779 image_pixel test_pixel
;
5781 memset(&test_pixel
, 0, sizeof test_pixel
);
5782 test_pixel
.colour_type
= dp
->this.colour_type
; /* input */
5783 test_pixel
.bit_depth
= dp
->this.bit_depth
;
5784 if (test_pixel
.colour_type
== PNG_COLOR_TYPE_PALETTE
)
5785 test_pixel
.sample_depth
= 8;
5787 test_pixel
.sample_depth
= test_pixel
.bit_depth
;
5788 /* Don't need sBIT here, but it must be set to non-zero to avoid
5789 * arithmetic overflows.
5791 test_pixel
.have_tRNS
= dp
->this.is_transparent
;
5792 test_pixel
.red_sBIT
= test_pixel
.green_sBIT
= test_pixel
.blue_sBIT
=
5793 test_pixel
.alpha_sBIT
= test_pixel
.sample_depth
;
5795 dp
->transform_list
->mod(dp
->transform_list
, &test_pixel
, pp
, dp
);
5797 if (test_pixel
.colour_type
!= dp
->output_colour_type
)
5800 size_t pos
= safecat(message
, sizeof message
, 0, "colour type ");
5802 pos
= safecatn(message
, sizeof message
, pos
, dp
->output_colour_type
);
5803 pos
= safecat(message
, sizeof message
, pos
, " expected ");
5804 pos
= safecatn(message
, sizeof message
, pos
, test_pixel
.colour_type
);
5806 png_error(pp
, message
);
5809 if (test_pixel
.bit_depth
!= dp
->output_bit_depth
)
5812 size_t pos
= safecat(message
, sizeof message
, 0, "bit depth ");
5814 pos
= safecatn(message
, sizeof message
, pos
, dp
->output_bit_depth
);
5815 pos
= safecat(message
, sizeof message
, pos
, " expected ");
5816 pos
= safecatn(message
, sizeof message
, pos
, test_pixel
.bit_depth
);
5818 png_error(pp
, message
);
5821 /* If both bit depth and colour type are correct check the sample depth.
5822 * I believe these are both internal errors.
5824 if (test_pixel
.colour_type
== PNG_COLOR_TYPE_PALETTE
)
5826 if (test_pixel
.sample_depth
!= 8) /* oops - internal error! */
5827 png_error(pp
, "pngvalid: internal: palette sample depth not 8");
5829 else if (test_pixel
.sample_depth
!= dp
->output_bit_depth
)
5832 size_t pos
= safecat(message
, sizeof message
, 0,
5833 "internal: sample depth ");
5835 pos
= safecatn(message
, sizeof message
, pos
, dp
->output_bit_depth
);
5836 pos
= safecat(message
, sizeof message
, pos
, " expected ");
5837 pos
= safecatn(message
, sizeof message
, pos
, test_pixel
.sample_depth
);
5839 png_error(pp
, message
);
5845 transform_info(png_structp pp
, png_infop pi
)
5847 transform_info_imp(voidcast(transform_display
*, png_get_progressive_ptr(pp
)),
5852 transform_range_check(png_const_structp pp
, unsigned int r
, unsigned int g
,
5853 unsigned int b
, unsigned int a
, unsigned int in_digitized
, double in
,
5854 unsigned int out
, png_byte sample_depth
, double err
, double limit
,
5855 PNG_CONST
char *name
, double digitization_error
)
5857 /* Compare the scaled, digitzed, values of our local calculation (in+-err)
5858 * with the digitized values libpng produced; 'sample_depth' is the actual
5859 * digitization depth of the libpng output colors (the bit depth except for
5860 * palette images where it is always 8.) The check on 'err' is to detect
5861 * internal errors in pngvalid itself.
5863 unsigned int max
= (1U<<sample_depth
)-1;
5864 double in_min
= ceil((in
-err
)*max
- digitization_error
);
5865 double in_max
= floor((in
+err
)*max
+ digitization_error
);
5866 if (err
> limit
|| !(out
>= in_min
&& out
<= in_max
))
5871 pos
= safecat(message
, sizeof message
, 0, name
);
5872 pos
= safecat(message
, sizeof message
, pos
, " output value error: rgba(");
5873 pos
= safecatn(message
, sizeof message
, pos
, r
);
5874 pos
= safecat(message
, sizeof message
, pos
, ",");
5875 pos
= safecatn(message
, sizeof message
, pos
, g
);
5876 pos
= safecat(message
, sizeof message
, pos
, ",");
5877 pos
= safecatn(message
, sizeof message
, pos
, b
);
5878 pos
= safecat(message
, sizeof message
, pos
, ",");
5879 pos
= safecatn(message
, sizeof message
, pos
, a
);
5880 pos
= safecat(message
, sizeof message
, pos
, "): ");
5881 pos
= safecatn(message
, sizeof message
, pos
, out
);
5882 pos
= safecat(message
, sizeof message
, pos
, " expected: ");
5883 pos
= safecatn(message
, sizeof message
, pos
, in_digitized
);
5884 pos
= safecat(message
, sizeof message
, pos
, " (");
5885 pos
= safecatd(message
, sizeof message
, pos
, (in
-err
)*max
, 3);
5886 pos
= safecat(message
, sizeof message
, pos
, "..");
5887 pos
= safecatd(message
, sizeof message
, pos
, (in
+err
)*max
, 3);
5888 pos
= safecat(message
, sizeof message
, pos
, ")");
5890 png_error(pp
, message
);
5895 transform_image_validate(transform_display
*dp
, png_const_structp pp
,
5898 /* Constants for the loop below: */
5899 PNG_CONST png_store
* PNG_CONST ps
= dp
->this.ps
;
5900 PNG_CONST png_byte in_ct
= dp
->this.colour_type
;
5901 PNG_CONST png_byte in_bd
= dp
->this.bit_depth
;
5902 PNG_CONST png_uint_32 w
= dp
->this.w
;
5903 PNG_CONST png_uint_32 h
= dp
->this.h
;
5904 PNG_CONST png_byte out_ct
= dp
->output_colour_type
;
5905 PNG_CONST png_byte out_bd
= dp
->output_bit_depth
;
5906 PNG_CONST png_byte sample_depth
= (png_byte
)(out_ct
==
5907 PNG_COLOR_TYPE_PALETTE ?
8 : out_bd
);
5908 PNG_CONST png_byte red_sBIT
= dp
->this.red_sBIT
;
5909 PNG_CONST png_byte green_sBIT
= dp
->this.green_sBIT
;
5910 PNG_CONST png_byte blue_sBIT
= dp
->this.blue_sBIT
;
5911 PNG_CONST png_byte alpha_sBIT
= dp
->this.alpha_sBIT
;
5912 PNG_CONST
int have_tRNS
= dp
->this.is_transparent
;
5913 double digitization_error
;
5915 store_palette out_palette
;
5920 /* Check for row overwrite errors */
5921 store_image_check(dp
->this.ps
, pp
, 0);
5923 /* Read the palette corresponding to the output if the output colour type
5924 * indicates a palette, othewise set out_palette to garbage.
5926 if (out_ct
== PNG_COLOR_TYPE_PALETTE
)
5928 /* Validate that the palette count itself has not changed - this is not
5931 int npalette
= (-1);
5933 (void)read_palette(out_palette
, &npalette
, pp
, pi
);
5934 if (npalette
!= dp
->this.npalette
)
5935 png_error(pp
, "unexpected change in palette size");
5937 digitization_error
= .5;
5941 png_byte in_sample_depth
;
5943 memset(out_palette
, 0x5e, sizeof out_palette
);
5945 /* use-input-precision means assume that if the input has 8 bit (or less)
5946 * samples and the output has 16 bit samples the calculations will be done
5947 * with 8 bit precision, not 16.
5949 if (in_ct
== PNG_COLOR_TYPE_PALETTE
|| in_bd
< 16)
5950 in_sample_depth
= 8;
5952 in_sample_depth
= in_bd
;
5954 if (sample_depth
!= 16 || in_sample_depth
> 8 ||
5955 !dp
->pm
->calculations_use_input_precision
)
5956 digitization_error
= .5;
5958 /* Else calculations are at 8 bit precision, and the output actually
5959 * consists of scaled 8-bit values, so scale .5 in 8 bits to the 16 bits:
5962 digitization_error
= .5 * 257;
5967 png_const_bytep PNG_CONST pRow
= store_image_row(ps
, pp
, 0, y
);
5970 /* The original, standard, row pre-transforms. */
5971 png_byte std
[STANDARD_ROWMAX
];
5973 transform_row(pp
, std
, in_ct
, in_bd
, y
);
5975 /* Go through each original pixel transforming it and comparing with what
5976 * libpng did to the same pixel.
5980 image_pixel in_pixel
, out_pixel
;
5981 unsigned int r
, g
, b
, a
;
5983 /* Find out what we think the pixel should be: */
5984 image_pixel_init(&in_pixel
, std
, in_ct
, in_bd
, x
, dp
->this.palette
);
5986 in_pixel
.red_sBIT
= red_sBIT
;
5987 in_pixel
.green_sBIT
= green_sBIT
;
5988 in_pixel
.blue_sBIT
= blue_sBIT
;
5989 in_pixel
.alpha_sBIT
= alpha_sBIT
;
5990 in_pixel
.have_tRNS
= have_tRNS
;
5992 /* For error detection, below. */
5998 dp
->transform_list
->mod(dp
->transform_list
, &in_pixel
, pp
, dp
);
6000 /* Read the output pixel and compare it to what we got, we don't
6001 * use the error field here, so no need to update sBIT.
6003 image_pixel_init(&out_pixel
, pRow
, out_ct
, out_bd
, x
, out_palette
);
6005 /* We don't expect changes to the index here even if the bit depth is
6008 if (in_ct
== PNG_COLOR_TYPE_PALETTE
&&
6009 out_ct
== PNG_COLOR_TYPE_PALETTE
)
6011 if (in_pixel
.palette_index
!= out_pixel
.palette_index
)
6012 png_error(pp
, "unexpected transformed palette index");
6015 /* Check the colours for palette images too - in fact the palette could
6016 * be separately verified itself in most cases.
6018 if (in_pixel
.red
!= out_pixel
.red
)
6019 transform_range_check(pp
, r
, g
, b
, a
, in_pixel
.red
, in_pixel
.redf
,
6020 out_pixel
.red
, sample_depth
, in_pixel
.rede
,
6021 dp
->pm
->limit
+ 1./(2*((1U<<in_pixel
.red_sBIT
)-1)), "red/gray",
6022 digitization_error
);
6024 if ((out_ct
& PNG_COLOR_MASK_COLOR
) != 0 &&
6025 in_pixel
.green
!= out_pixel
.green
)
6026 transform_range_check(pp
, r
, g
, b
, a
, in_pixel
.green
,
6027 in_pixel
.greenf
, out_pixel
.green
, sample_depth
, in_pixel
.greene
,
6028 dp
->pm
->limit
+ 1./(2*((1U<<in_pixel
.green_sBIT
)-1)), "green",
6029 digitization_error
);
6031 if ((out_ct
& PNG_COLOR_MASK_COLOR
) != 0 &&
6032 in_pixel
.blue
!= out_pixel
.blue
)
6033 transform_range_check(pp
, r
, g
, b
, a
, in_pixel
.blue
, in_pixel
.bluef
,
6034 out_pixel
.blue
, sample_depth
, in_pixel
.bluee
,
6035 dp
->pm
->limit
+ 1./(2*((1U<<in_pixel
.blue_sBIT
)-1)), "blue",
6036 digitization_error
);
6038 if ((out_ct
& PNG_COLOR_MASK_ALPHA
) != 0 &&
6039 in_pixel
.alpha
!= out_pixel
.alpha
)
6040 transform_range_check(pp
, r
, g
, b
, a
, in_pixel
.alpha
,
6041 in_pixel
.alphaf
, out_pixel
.alpha
, sample_depth
, in_pixel
.alphae
,
6042 dp
->pm
->limit
+ 1./(2*((1U<<in_pixel
.alpha_sBIT
)-1)), "alpha",
6043 digitization_error
);
6044 } /* pixel (x) loop */
6045 } /* row (y) loop */
6047 /* Record that something was actually checked to avoid a false positive. */
6048 dp
->this.ps
->validated
= 1;
6052 transform_end(png_structp ppIn
, png_infop pi
)
6054 png_const_structp pp
= ppIn
;
6055 transform_display
*dp
= voidcast(transform_display
*,
6056 png_get_progressive_ptr(pp
));
6058 if (!dp
->this.speed
)
6059 transform_image_validate(dp
, pp
, pi
);
6061 dp
->this.ps
->validated
= 1;
6064 /* A single test run. */
6066 transform_test(png_modifier
*pmIn
, PNG_CONST png_uint_32 idIn
,
6067 PNG_CONST image_transform
* transform_listIn
, PNG_CONST
char * volatile name
)
6069 transform_display d
;
6070 context(&pmIn
->this, fault
);
6072 transform_display_init(&d
, pmIn
, idIn
, transform_listIn
);
6079 char full_name
[256];
6081 /* Make sure the encoding fields are correct and enter the required
6084 transform_set_encoding(&d
);
6086 /* Add any modifications required by the transform list. */
6087 d
.transform_list
->ini(d
.transform_list
, &d
);
6089 /* Add the color space information, if any, to the name. */
6090 pos
= safecat(full_name
, sizeof full_name
, pos
, name
);
6091 pos
= safecat_current_encoding(full_name
, sizeof full_name
, pos
, d
.pm
);
6093 /* Get a png_struct for reading the image. */
6094 pp
= set_modifier_for_read(d
.pm
, &pi
, d
.this.id
, full_name
);
6095 standard_palette_init(&d
.this);
6098 /* Logging (debugging only) */
6102 (void)store_message(&d
.pm
->this, pp
, buffer
, sizeof buffer
, 0,
6105 fprintf(stderr
, "%s\n", buffer
);
6109 /* Introduce the correct read function. */
6110 if (d
.pm
->this.progressive
)
6112 /* Share the row function with the standard implementation. */
6113 png_set_progressive_read_fn(pp
, &d
, transform_info
, progressive_row
,
6116 /* Now feed data into the reader until we reach the end: */
6117 modifier_progressive_read(d
.pm
, pp
, pi
);
6121 /* modifier_read expects a png_modifier* */
6122 png_set_read_fn(pp
, d
.pm
, modifier_read
);
6124 /* Check the header values: */
6125 png_read_info(pp
, pi
);
6127 /* Process the 'info' requirements. Only one image is generated */
6128 transform_info_imp(&d
, pp
, pi
);
6130 sequential_row(&d
.this, pp
, pi
, -1, 0);
6133 transform_image_validate(&d
, pp
, pi
);
6135 d
.this.ps
->validated
= 1;
6138 modifier_reset(d
.pm
);
6143 modifier_reset(voidcast(png_modifier
*,(void*)fault
));
6147 /* The transforms: */
6148 #define ITSTRUCT(name) image_transform_##name
6149 #define ITDATA(name) image_transform_data_##name
6150 #define image_transform_ini image_transform_default_ini
6152 static image_transform ITSTRUCT(name) =\
6160 image_transform_ini,\
6161 image_transform_png_set_##name##_set,\
6162 image_transform_png_set_##name##_mod,\
6163 image_transform_png_set_##name##_add\
6165 #define PT ITSTRUCT(end) /* stores the previous transform */
6169 image_transform_default_ini(PNG_CONST image_transform
*this,
6170 transform_display
*that
)
6172 this->next
->ini(this->next
, that
);
6175 #ifdef PNG_READ_BACKGROUND_SUPPORTED
6177 image_transform_default_add(image_transform
*this,
6178 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6190 #ifdef PNG_READ_EXPAND_SUPPORTED
6191 /* png_set_palette_to_rgb */
6193 image_transform_png_set_palette_to_rgb_set(PNG_CONST image_transform
*this,
6194 transform_display
*that
, png_structp pp
, png_infop pi
)
6196 png_set_palette_to_rgb(pp
);
6197 this->next
->set(this->next
, that
, pp
, pi
);
6201 image_transform_png_set_palette_to_rgb_mod(PNG_CONST image_transform
*this,
6202 image_pixel
*that
, png_const_structp pp
,
6203 PNG_CONST transform_display
*display
)
6205 if (that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
6206 image_pixel_convert_PLTE(that
);
6208 this->next
->mod(this->next
, that
, pp
, display
);
6212 image_transform_png_set_palette_to_rgb_add(image_transform
*this,
6213 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6220 return colour_type
== PNG_COLOR_TYPE_PALETTE
;
6225 #define PT ITSTRUCT(palette_to_rgb)
6226 #endif /* PNG_READ_EXPAND_SUPPORTED */
6228 #ifdef PNG_READ_EXPAND_SUPPORTED
6229 /* png_set_tRNS_to_alpha */
6231 image_transform_png_set_tRNS_to_alpha_set(PNG_CONST image_transform
*this,
6232 transform_display
*that
, png_structp pp
, png_infop pi
)
6234 png_set_tRNS_to_alpha(pp
);
6235 this->next
->set(this->next
, that
, pp
, pi
);
6239 image_transform_png_set_tRNS_to_alpha_mod(PNG_CONST image_transform
*this,
6240 image_pixel
*that
, png_const_structp pp
,
6241 PNG_CONST transform_display
*display
)
6243 /* LIBPNG BUG: this always forces palette images to RGB. */
6244 if (that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
6245 image_pixel_convert_PLTE(that
);
6247 /* This effectively does an 'expand' only if there is some transparency to
6248 * convert to an alpha channel.
6250 if (that
->have_tRNS
)
6251 image_pixel_add_alpha(that
, &display
->this);
6253 /* LIBPNG BUG: otherwise libpng still expands to 8 bits! */
6256 if (that
->bit_depth
< 8)
6258 if (that
->sample_depth
< 8)
6259 that
->sample_depth
= 8;
6262 this->next
->mod(this->next
, that
, pp
, display
);
6266 image_transform_png_set_tRNS_to_alpha_add(image_transform
*this,
6267 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6274 /* We don't know yet whether there will be a tRNS chunk, but we know that
6275 * this transformation should do nothing if there already is an alpha
6278 return (colour_type
& PNG_COLOR_MASK_ALPHA
) == 0;
6283 #define PT ITSTRUCT(tRNS_to_alpha)
6284 #endif /* PNG_READ_EXPAND_SUPPORTED */
6286 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
6287 /* png_set_gray_to_rgb */
6289 image_transform_png_set_gray_to_rgb_set(PNG_CONST image_transform
*this,
6290 transform_display
*that
, png_structp pp
, png_infop pi
)
6292 png_set_gray_to_rgb(pp
);
6293 this->next
->set(this->next
, that
, pp
, pi
);
6297 image_transform_png_set_gray_to_rgb_mod(PNG_CONST image_transform
*this,
6298 image_pixel
*that
, png_const_structp pp
,
6299 PNG_CONST transform_display
*display
)
6301 /* NOTE: we can actually pend the tRNS processing at this point because we
6302 * can correctly recognize the original pixel value even though we have
6303 * mapped the one gray channel to the three RGB ones, but in fact libpng
6304 * doesn't do this, so we don't either.
6306 if ((that
->colour_type
& PNG_COLOR_MASK_COLOR
) == 0 && that
->have_tRNS
)
6307 image_pixel_add_alpha(that
, &display
->this);
6309 /* Simply expand the bit depth and alter the colour type as required. */
6310 if (that
->colour_type
== PNG_COLOR_TYPE_GRAY
)
6312 /* RGB images have a bit depth at least equal to '8' */
6313 if (that
->bit_depth
< 8)
6314 that
->sample_depth
= that
->bit_depth
= 8;
6316 /* And just changing the colour type works here because the green and blue
6317 * channels are being maintained in lock-step with the red/gray:
6319 that
->colour_type
= PNG_COLOR_TYPE_RGB
;
6322 else if (that
->colour_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
6323 that
->colour_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
6325 this->next
->mod(this->next
, that
, pp
, display
);
6329 image_transform_png_set_gray_to_rgb_add(image_transform
*this,
6330 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6337 return (colour_type
& PNG_COLOR_MASK_COLOR
) == 0;
6342 #define PT ITSTRUCT(gray_to_rgb)
6343 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
6345 #ifdef PNG_READ_EXPAND_SUPPORTED
6346 /* png_set_expand */
6348 image_transform_png_set_expand_set(PNG_CONST image_transform
*this,
6349 transform_display
*that
, png_structp pp
, png_infop pi
)
6352 this->next
->set(this->next
, that
, pp
, pi
);
6356 image_transform_png_set_expand_mod(PNG_CONST image_transform
*this,
6357 image_pixel
*that
, png_const_structp pp
,
6358 PNG_CONST transform_display
*display
)
6360 /* The general expand case depends on what the colour type is: */
6361 if (that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
6362 image_pixel_convert_PLTE(that
);
6363 else if (that
->bit_depth
< 8) /* grayscale */
6364 that
->sample_depth
= that
->bit_depth
= 8;
6366 if (that
->have_tRNS
)
6367 image_pixel_add_alpha(that
, &display
->this);
6369 this->next
->mod(this->next
, that
, pp
, display
);
6373 image_transform_png_set_expand_add(image_transform
*this,
6374 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6381 /* 'expand' should do nothing for RGBA or GA input - no tRNS and the bit
6382 * depth is at least 8 already.
6384 return (colour_type
& PNG_COLOR_MASK_ALPHA
) == 0;
6389 #define PT ITSTRUCT(expand)
6390 #endif /* PNG_READ_EXPAND_SUPPORTED */
6392 #ifdef PNG_READ_EXPAND_SUPPORTED
6393 /* png_set_expand_gray_1_2_4_to_8
6394 * LIBPNG BUG: this just does an 'expand'
6397 image_transform_png_set_expand_gray_1_2_4_to_8_set(
6398 PNG_CONST image_transform
*this, transform_display
*that
, png_structp pp
,
6401 png_set_expand_gray_1_2_4_to_8(pp
);
6402 this->next
->set(this->next
, that
, pp
, pi
);
6406 image_transform_png_set_expand_gray_1_2_4_to_8_mod(
6407 PNG_CONST image_transform
*this, image_pixel
*that
, png_const_structp pp
,
6408 PNG_CONST transform_display
*display
)
6410 image_transform_png_set_expand_mod(this, that
, pp
, display
);
6414 image_transform_png_set_expand_gray_1_2_4_to_8_add(image_transform
*this,
6415 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6417 return image_transform_png_set_expand_add(this, that
, colour_type
,
6421 IT(expand_gray_1_2_4_to_8
);
6423 #define PT ITSTRUCT(expand_gray_1_2_4_to_8)
6424 #endif /* PNG_READ_EXPAND_SUPPORTED */
6426 #ifdef PNG_READ_EXPAND_16_SUPPORTED
6427 /* png_set_expand_16 */
6429 image_transform_png_set_expand_16_set(PNG_CONST image_transform
*this,
6430 transform_display
*that
, png_structp pp
, png_infop pi
)
6432 png_set_expand_16(pp
);
6433 this->next
->set(this->next
, that
, pp
, pi
);
6437 image_transform_png_set_expand_16_mod(PNG_CONST image_transform
*this,
6438 image_pixel
*that
, png_const_structp pp
,
6439 PNG_CONST transform_display
*display
)
6441 /* Expect expand_16 to expand everything to 16 bits as a result of also
6442 * causing 'expand' to happen.
6444 if (that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
6445 image_pixel_convert_PLTE(that
);
6447 if (that
->have_tRNS
)
6448 image_pixel_add_alpha(that
, &display
->this);
6450 if (that
->bit_depth
< 16)
6451 that
->sample_depth
= that
->bit_depth
= 16;
6453 this->next
->mod(this->next
, that
, pp
, display
);
6457 image_transform_png_set_expand_16_add(image_transform
*this,
6458 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6465 /* expand_16 does something unless the bit depth is already 16. */
6466 return bit_depth
< 16;
6471 #define PT ITSTRUCT(expand_16)
6472 #endif /* PNG_READ_EXPAND_16_SUPPORTED */
6474 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED /* API added in 1.5.4 */
6475 /* png_set_scale_16 */
6477 image_transform_png_set_scale_16_set(PNG_CONST image_transform
*this,
6478 transform_display
*that
, png_structp pp
, png_infop pi
)
6480 png_set_scale_16(pp
);
6481 this->next
->set(this->next
, that
, pp
, pi
);
6485 image_transform_png_set_scale_16_mod(PNG_CONST image_transform
*this,
6486 image_pixel
*that
, png_const_structp pp
,
6487 PNG_CONST transform_display
*display
)
6489 if (that
->bit_depth
== 16)
6491 that
->sample_depth
= that
->bit_depth
= 8;
6492 if (that
->red_sBIT
> 8) that
->red_sBIT
= 8;
6493 if (that
->green_sBIT
> 8) that
->green_sBIT
= 8;
6494 if (that
->blue_sBIT
> 8) that
->blue_sBIT
= 8;
6495 if (that
->alpha_sBIT
> 8) that
->alpha_sBIT
= 8;
6498 this->next
->mod(this->next
, that
, pp
, display
);
6502 image_transform_png_set_scale_16_add(image_transform
*this,
6503 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6510 return bit_depth
> 8;
6515 #define PT ITSTRUCT(scale_16)
6516 #endif /* PNG_READ_SCALE_16_TO_8_SUPPORTED (1.5.4 on) */
6518 #ifdef PNG_READ_16_TO_8_SUPPORTED /* the default before 1.5.4 */
6519 /* png_set_strip_16 */
6521 image_transform_png_set_strip_16_set(PNG_CONST image_transform
*this,
6522 transform_display
*that
, png_structp pp
, png_infop pi
)
6524 png_set_strip_16(pp
);
6525 this->next
->set(this->next
, that
, pp
, pi
);
6529 image_transform_png_set_strip_16_mod(PNG_CONST image_transform
*this,
6530 image_pixel
*that
, png_const_structp pp
,
6531 PNG_CONST transform_display
*display
)
6533 if (that
->bit_depth
== 16)
6535 that
->sample_depth
= that
->bit_depth
= 8;
6536 if (that
->red_sBIT
> 8) that
->red_sBIT
= 8;
6537 if (that
->green_sBIT
> 8) that
->green_sBIT
= 8;
6538 if (that
->blue_sBIT
> 8) that
->blue_sBIT
= 8;
6539 if (that
->alpha_sBIT
> 8) that
->alpha_sBIT
= 8;
6541 /* Prior to 1.5.4 png_set_strip_16 would use an 'accurate' method if this
6542 * configuration option is set. From 1.5.4 the flag is never set and the
6543 * 'scale' API (above) must be used.
6545 # ifdef PNG_READ_ACCURATE_SCALE_SUPPORTED
6546 # if PNG_LIBPNG_VER >= 10504
6547 # error PNG_READ_ACCURATE_SCALE should not be set
6550 /* The strip 16 algorithm drops the low 8 bits rather than calculating
6551 * 1/257, so we need to adjust the permitted errors appropriately:
6552 * Notice that this is only relevant prior to the addition of the
6553 * png_set_scale_16 API in 1.5.4 (but 1.5.4+ always defines the above!)
6556 PNG_CONST
double d
= (255-128.5)/65535;
6565 this->next
->mod(this->next
, that
, pp
, display
);
6569 image_transform_png_set_strip_16_add(image_transform
*this,
6570 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6577 return bit_depth
> 8;
6582 #define PT ITSTRUCT(strip_16)
6583 #endif /* PNG_READ_16_TO_8_SUPPORTED */
6585 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
6586 /* png_set_strip_alpha */
6588 image_transform_png_set_strip_alpha_set(PNG_CONST image_transform
*this,
6589 transform_display
*that
, png_structp pp
, png_infop pi
)
6591 png_set_strip_alpha(pp
);
6592 this->next
->set(this->next
, that
, pp
, pi
);
6596 image_transform_png_set_strip_alpha_mod(PNG_CONST image_transform
*this,
6597 image_pixel
*that
, png_const_structp pp
,
6598 PNG_CONST transform_display
*display
)
6600 if (that
->colour_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
6601 that
->colour_type
= PNG_COLOR_TYPE_GRAY
;
6602 else if (that
->colour_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
6603 that
->colour_type
= PNG_COLOR_TYPE_RGB
;
6605 that
->have_tRNS
= 0;
6608 this->next
->mod(this->next
, that
, pp
, display
);
6612 image_transform_png_set_strip_alpha_add(image_transform
*this,
6613 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
6620 return (colour_type
& PNG_COLOR_MASK_ALPHA
) != 0;
6625 #define PT ITSTRUCT(strip_alpha)
6626 #endif /* PNG_READ_STRIP_ALPHA_SUPPORTED */
6628 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
6629 /* png_set_rgb_to_gray(png_structp, int err_action, double red, double green)
6630 * png_set_rgb_to_gray_fixed(png_structp, int err_action, png_fixed_point red,
6631 * png_fixed_point green)
6632 * png_get_rgb_to_gray_status
6634 * The 'default' test here uses values known to be used inside libpng:
6640 * These values are being retained for compatibility, along with the somewhat
6641 * broken truncation calculation in the fast-and-inaccurate code path. Older
6642 * versions of libpng will fail the accuracy tests below because they use the
6643 * truncation algorithm everywhere.
6645 #define data ITDATA(rgb_to_gray)
6648 double gamma
; /* File gamma to use in processing */
6650 /* The following are the parameters for png_set_rgb_to_gray: */
6651 # ifdef PNG_FLOATING_POINT_SUPPORTED
6653 double green_to_set
;
6655 png_fixed_point red_to_set
;
6656 png_fixed_point green_to_set
;
6659 /* The actual coefficients: */
6660 double red_coefficient
;
6661 double green_coefficient
;
6662 double blue_coefficient
;
6664 /* Set if the coeefficients have been overridden. */
6665 int coefficients_overridden
;
6668 #undef image_transform_ini
6669 #define image_transform_ini image_transform_png_set_rgb_to_gray_ini
6671 image_transform_png_set_rgb_to_gray_ini(PNG_CONST image_transform
*this,
6672 transform_display
*that
)
6674 png_modifier
*pm
= that
->pm
;
6675 PNG_CONST color_encoding
*e
= pm
->current_encoding
;
6679 /* Since we check the encoding this flag must be set: */
6680 pm
->test_uses_encoding
= 1;
6682 /* If 'e' is not NULL chromaticity information is present and either a cHRM
6683 * or an sRGB chunk will be inserted.
6687 /* Coefficients come from the encoding, but may need to be normalized to a
6688 * white point Y of 1.0
6690 PNG_CONST
double whiteY
= e
->red
.Y
+ e
->green
.Y
+ e
->blue
.Y
;
6692 data
.red_coefficient
= e
->red
.Y
;
6693 data
.green_coefficient
= e
->green
.Y
;
6694 data
.blue_coefficient
= e
->blue
.Y
;
6698 data
.red_coefficient
/= whiteY
;
6699 data
.green_coefficient
/= whiteY
;
6700 data
.blue_coefficient
/= whiteY
;
6706 /* The default (built in) coeffcients, as above: */
6707 data
.red_coefficient
= 6968 / 32768.;
6708 data
.green_coefficient
= 23434 / 32768.;
6709 data
.blue_coefficient
= 2366 / 32768.;
6712 data
.gamma
= pm
->current_gamma
;
6714 /* If not set then the calculations assume linear encoding (implicitly): */
6715 if (data
.gamma
== 0)
6718 /* The arguments to png_set_rgb_to_gray can override the coefficients implied
6719 * by the color space encoding. If doing exhaustive checks do the override
6720 * in each case, otherwise do it randomly.
6722 if (pm
->test_exhaustive
)
6724 /* First time in coefficients_overridden is 0, the following sets it to 1,
6725 * so repeat if it is set. If a test fails this may mean we subsequently
6726 * skip a non-override test, ignore that.
6728 data
.coefficients_overridden
= !data
.coefficients_overridden
;
6729 pm
->repeat
= data
.coefficients_overridden
!= 0;
6733 data
.coefficients_overridden
= random_choice();
6735 if (data
.coefficients_overridden
)
6737 /* These values override the color encoding defaults, simply use random
6744 data
.green_coefficient
= total
= (ru
& 0xffff) / 65535.;
6746 data
.red_coefficient
= (1 - total
) * (ru
& 0xffff) / 65535.;
6747 total
+= data
.red_coefficient
;
6748 data
.blue_coefficient
= 1 - total
;
6750 # ifdef PNG_FLOATING_POINT_SUPPORTED
6751 data
.red_to_set
= data
.red_coefficient
;
6752 data
.green_to_set
= data
.green_coefficient
;
6754 data
.red_to_set
= fix(data
.red_coefficient
);
6755 data
.green_to_set
= fix(data
.green_coefficient
);
6758 /* The following just changes the error messages: */
6759 pm
->encoding_ignored
= 1;
6764 data
.red_to_set
= -1;
6765 data
.green_to_set
= -1;
6768 /* Adjust the error limit in the png_modifier because of the larger errors
6769 * produced in the digitization during the gamma handling.
6771 if (data
.gamma
!= 1) /* Use gamma tables */
6773 if (that
->this.bit_depth
== 16 || pm
->assume_16_bit_calculations
)
6775 /* The computations have the form:
6777 * r * rc + g * gc + b * bc
6779 * Each component of which is +/-1/65535 from the gamma_to_1 table
6780 * lookup, resulting in a base error of +/-6. The gamma_from_1
6781 * conversion adds another +/-2 in the 16-bit case and
6782 * +/-(1<<(15-PNG_MAX_GAMMA_8)) in the 8-bit case.
6784 that
->pm
->limit
+= pow(
6785 # if PNG_MAX_GAMMA_8 < 14
6786 (that
->this.bit_depth
== 16 ?
8. :
6787 6. + (1<<(15-PNG_MAX_GAMMA_8
)))
6791 /65535, data
.gamma
);
6796 /* Rounding to 8 bits in the linear space causes massive errors which
6797 * will trigger the error check in transform_range_check. Fix that
6798 * here by taking the gamma encoding into account.
6800 * When DIGITIZE is set because a pre-1.7 version of libpng is being
6801 * tested allow a bigger slack.
6803 * NOTE: this magic number was determined by experiment to be 1.1 (when
6804 * using fixed point arithmetic). There's no great merit to the value
6805 * below, however it only affects the limit used for checking for
6806 * internal calculation errors, not the actual limit imposed by
6807 * pngvalid on the output errors.
6809 that
->pm
->limit
+= pow(
6821 /* With no gamma correction a large error comes from the truncation of the
6822 * calculation in the 8 bit case, allow for that here.
6824 if (that
->this.bit_depth
!= 16 && !pm
->assume_16_bit_calculations
)
6825 that
->pm
->limit
+= 4E-3;
6830 image_transform_png_set_rgb_to_gray_set(PNG_CONST image_transform
*this,
6831 transform_display
*that
, png_structp pp
, png_infop pi
)
6833 PNG_CONST
int error_action
= 1; /* no error, no defines in png.h */
6835 # ifdef PNG_FLOATING_POINT_SUPPORTED
6836 png_set_rgb_to_gray(pp
, error_action
, data
.red_to_set
, data
.green_to_set
);
6838 png_set_rgb_to_gray_fixed(pp
, error_action
, data
.red_to_set
,
6842 # ifdef PNG_READ_cHRM_SUPPORTED
6843 if (that
->pm
->current_encoding
!= 0)
6845 /* We have an encoding so a cHRM chunk may have been set; if so then
6846 * check that the libpng APIs give the correct (X,Y,Z) values within
6847 * some margin of error for the round trip through the chromaticity
6850 # ifdef PNG_FLOATING_POINT_SUPPORTED
6851 # define API_function png_get_cHRM_XYZ
6852 # define API_form "FP"
6853 # define API_type double
6854 # define API_cvt(x) (x)
6856 # define API_function png_get_cHRM_XYZ_fixed
6857 # define API_form "fixed"
6858 # define API_type png_fixed_point
6859 # define API_cvt(x) ((double)(x)/PNG_FP_1)
6862 API_type rX
, gX
, bX
;
6863 API_type rY
, gY
, bY
;
6864 API_type rZ
, gZ
, bZ
;
6866 if ((API_function(pp
, pi
, &rX
, &rY
, &rZ
, &gX
, &gY
, &gZ
, &bX
, &bY
, &bZ
)
6867 & PNG_INFO_cHRM
) != 0)
6871 color_encoding e
, o
;
6873 /* Expect libpng to return a normalized result, but the original
6874 * color space encoding may not be normalized.
6876 modifier_current_encoding(that
->pm
, &o
);
6877 normalize_color_encoding(&o
);
6879 /* Sanity check the pngvalid code - the coefficients should match
6880 * the normalized Y values of the encoding unless they were
6883 if (data
.red_to_set
== -1 && data
.green_to_set
== -1 &&
6884 (fabs(o
.red
.Y
- data
.red_coefficient
) > DBL_EPSILON
||
6885 fabs(o
.green
.Y
- data
.green_coefficient
) > DBL_EPSILON
||
6886 fabs(o
.blue
.Y
- data
.blue_coefficient
) > DBL_EPSILON
))
6887 png_error(pp
, "internal pngvalid cHRM coefficient error");
6889 /* Generate a colour space encoding. */
6890 e
.gamma
= o
.gamma
; /* not used */
6891 e
.red
.X
= API_cvt(rX
);
6892 e
.red
.Y
= API_cvt(rY
);
6893 e
.red
.Z
= API_cvt(rZ
);
6894 e
.green
.X
= API_cvt(gX
);
6895 e
.green
.Y
= API_cvt(gY
);
6896 e
.green
.Z
= API_cvt(gZ
);
6897 e
.blue
.X
= API_cvt(bX
);
6898 e
.blue
.Y
= API_cvt(bY
);
6899 e
.blue
.Z
= API_cvt(bZ
);
6901 /* This should match the original one from the png_modifier, within
6902 * the range permitted by the libpng fixed point representation.
6905 el
= "-"; /* Set to element name with error */
6907 # define CHECK(col,x)\
6909 double err = fabs(o.col.x - e.col.x);\
6913 el = #col "(" #x ")";\
6927 /* Here in both fixed and floating cases to check the values read
6928 * from the cHRm chunk. PNG uses fixed point in the cHRM chunk, so
6929 * we can't expect better than +/-.5E-5 on the result, allow 1E-5.
6936 pos
= safecat(buffer
, sizeof buffer
, pos
, API_form
);
6937 pos
= safecat(buffer
, sizeof buffer
, pos
, " cHRM ");
6938 pos
= safecat(buffer
, sizeof buffer
, pos
, el
);
6939 pos
= safecat(buffer
, sizeof buffer
, pos
, " error: ");
6940 pos
= safecatd(buffer
, sizeof buffer
, pos
, maxe
, 7);
6941 pos
= safecat(buffer
, sizeof buffer
, pos
, " ");
6942 /* Print the color space without the gamma value: */
6943 pos
= safecat_color_encoding(buffer
, sizeof buffer
, pos
, &o
, 0);
6944 pos
= safecat(buffer
, sizeof buffer
, pos
, " -> ");
6945 pos
= safecat_color_encoding(buffer
, sizeof buffer
, pos
, &e
, 0);
6947 png_error(pp
, buffer
);
6951 # endif /* READ_cHRM */
6953 this->next
->set(this->next
, that
, pp
, pi
);
6957 image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform
*this,
6958 image_pixel
*that
, png_const_structp pp
,
6959 PNG_CONST transform_display
*display
)
6961 if ((that
->colour_type
& PNG_COLOR_MASK_COLOR
) != 0)
6965 if (that
->colour_type
== PNG_COLOR_TYPE_PALETTE
)
6966 image_pixel_convert_PLTE(that
);
6968 /* Image now has RGB channels... */
6971 PNG_CONST png_modifier
*pm
= display
->pm
;
6972 const unsigned int sample_depth
= that
->sample_depth
;
6973 const unsigned int calc_depth
= (pm
->assume_16_bit_calculations ?
16 :
6975 const unsigned int gamma_depth
= (sample_depth
== 16 ?
16 :
6976 (pm
->assume_16_bit_calculations ? PNG_MAX_GAMMA_8
: sample_depth
));
6979 double rlo
, rhi
, glo
, ghi
, blo
, bhi
, graylo
, grayhi
;
6981 /* Do this using interval arithmetic, otherwise it is too difficult to
6982 * handle the errors correctly.
6984 * To handle the gamma correction work out the upper and lower bounds
6985 * of the digitized value. Assume rounding here - normally the values
6986 * will be identical after this operation if there is only one
6987 * transform, feel free to delete the png_error checks on this below in
6988 * the future (this is just me trying to ensure it works!)
6990 r
= rlo
= rhi
= that
->redf
;
6992 rlo
= digitize(rlo
, calc_depth
, 1/*round*/);
6994 rhi
= digitize(rhi
, calc_depth
, 1/*round*/);
6996 g
= glo
= ghi
= that
->greenf
;
6997 glo
-= that
->greene
;
6998 glo
= digitize(glo
, calc_depth
, 1/*round*/);
6999 ghi
+= that
->greene
;
7000 ghi
= digitize(ghi
, calc_depth
, 1/*round*/);
7002 b
= blo
= bhi
= that
->bluef
;
7004 blo
= digitize(blo
, calc_depth
, 1/*round*/);
7005 bhi
+= that
->greene
;
7006 bhi
= digitize(bhi
, calc_depth
, 1/*round*/);
7008 isgray
= r
==g
&& g
==b
;
7010 if (data
.gamma
!= 1)
7012 PNG_CONST
double power
= 1/data
.gamma
;
7013 PNG_CONST
double abse
= calc_depth
== 16 ?
.5/65535 : .5/255;
7015 /* 'abse' is the absolute error permitted in linear calculations. It
7016 * is used here to capture the error permitted in the handling
7017 * (undoing) of the gamma encoding. Once again digitization occurs
7018 * to handle the upper and lower bounds of the values. This is
7019 * where the real errors are introduced.
7022 rlo
= digitize(pow(rlo
, power
)-abse
, calc_depth
, 1);
7023 rhi
= digitize(pow(rhi
, power
)+abse
, calc_depth
, 1);
7026 glo
= digitize(pow(glo
, power
)-abse
, calc_depth
, 1);
7027 ghi
= digitize(pow(ghi
, power
)+abse
, calc_depth
, 1);
7030 blo
= digitize(pow(blo
, power
)-abse
, calc_depth
, 1);
7031 bhi
= digitize(pow(bhi
, power
)+abse
, calc_depth
, 1);
7034 /* Now calculate the actual gray values. Although the error in the
7035 * coefficients depends on whether they were specified on the command
7036 * line (in which case truncation to 15 bits happened) or not (rounding
7037 * was used) the maxium error in an individual coefficient is always
7038 * 1/32768, because even in the rounding case the requirement that
7039 * coefficients add up to 32768 can cause a larger rounding error.
7041 * The only time when rounding doesn't occur in 1.5.5 and later is when
7042 * the non-gamma code path is used for less than 16 bit data.
7044 gray
= r
* data
.red_coefficient
+ g
* data
.green_coefficient
+
7045 b
* data
.blue_coefficient
;
7048 PNG_CONST
int do_round
= data
.gamma
!= 1 || calc_depth
== 16;
7049 PNG_CONST
double ce
= 1. / 32768;
7051 graylo
= digitize(rlo
* (data
.red_coefficient
-ce
) +
7052 glo
* (data
.green_coefficient
-ce
) +
7053 blo
* (data
.blue_coefficient
-ce
), gamma_depth
, do_round
);
7057 grayhi
= digitize(rhi
* (data
.red_coefficient
+ce
) +
7058 ghi
* (data
.green_coefficient
+ce
) +
7059 bhi
* (data
.blue_coefficient
+ce
), gamma_depth
, do_round
);
7064 /* And invert the gamma. */
7065 if (data
.gamma
!= 1)
7067 PNG_CONST
double power
= data
.gamma
;
7069 gray
= pow(gray
, power
);
7070 graylo
= digitize(pow(graylo
, power
), sample_depth
, 1);
7071 grayhi
= digitize(pow(grayhi
, power
), sample_depth
, 1);
7074 /* Now the error can be calculated.
7076 * If r==g==b because there is no overall gamma correction libpng
7077 * currently preserves the original value.
7080 err
= (that
->rede
+ that
->greene
+ that
->bluee
)/3;
7084 err
= fabs(grayhi
-gray
);
7085 if (fabs(gray
- graylo
) > err
)
7086 err
= fabs(graylo
-gray
);
7088 /* Check that this worked: */
7089 if (err
> pm
->limit
)
7094 pos
= safecat(buffer
, sizeof buffer
, pos
, "rgb_to_gray error ");
7095 pos
= safecatd(buffer
, sizeof buffer
, pos
, err
, 6);
7096 pos
= safecat(buffer
, sizeof buffer
, pos
, " exceeds limit ");
7097 pos
= safecatd(buffer
, sizeof buffer
, pos
, pm
->limit
, 6);
7098 png_error(pp
, buffer
);
7102 # else /* DIGITIZE */
7104 double r
= that
->redf
;
7105 double re
= that
->rede
;
7106 double g
= that
->greenf
;
7107 double ge
= that
->greene
;
7108 double b
= that
->bluef
;
7109 double be
= that
->bluee
;
7111 /* The true gray case involves no math. */
7112 if (r
== g
&& r
== b
)
7116 if (err
< ge
) err
= ge
;
7117 if (err
< be
) err
= be
;
7120 else if (data
.gamma
== 1)
7122 /* There is no need to do the conversions to and from linear space,
7123 * so the calculation should be a lot more accurate. There is a
7124 * built in 1/32768 error in the coefficients because they only have
7125 * 15 bits and are adjusted to make sure they add up to 32768, so
7126 * the result may have an additional error up to 1/32768. (Note
7127 * that adding the 1/32768 here avoids needing to increase the
7128 * global error limits to take this into account.)
7130 gray
= r
* data
.red_coefficient
+ g
* data
.green_coefficient
+
7131 b
* data
.blue_coefficient
;
7132 err
= re
* data
.red_coefficient
+ ge
* data
.green_coefficient
+
7133 be
* data
.blue_coefficient
+ 1./32768 + gray
* 5 * DBL_EPSILON
;
7138 /* The calculation happens in linear space, and this produces much
7139 * wider errors in the encoded space. These are handled here by
7140 * factoring the errors in to the calculation. There are two table
7141 * lookups in the calculation and each introduces a quantization
7142 * error defined by the table size.
7144 PNG_CONST png_modifier
*pm
= display
->pm
;
7145 double in_qe
= (that
->sample_depth
> 8 ?
.5/65535 : .5/255);
7146 double out_qe
= (that
->sample_depth
> 8 ?
.5/65535 :
7147 (pm
->assume_16_bit_calculations ?
.5/(1<<PNG_MAX_GAMMA_8
) :
7149 double rhi
, ghi
, bhi
, grayhi
;
7150 double g1
= 1/data
.gamma
;
7152 rhi
= r
+ re
+ in_qe
; if (rhi
> 1) rhi
= 1;
7153 r
-= re
+ in_qe
; if (r
< 0) r
= 0;
7154 ghi
= g
+ ge
+ in_qe
; if (ghi
> 1) ghi
= 1;
7155 g
-= ge
+ in_qe
; if (g
< 0) g
= 0;
7156 bhi
= b
+ be
+ in_qe
; if (bhi
> 1) bhi
= 1;
7157 b
-= be
+ in_qe
; if (b
< 0) b
= 0;
7159 r
= pow(r
, g1
)*(1-DBL_EPSILON
); rhi
= pow(rhi
, g1
)*(1+DBL_EPSILON
);
7160 g
= pow(g
, g1
)*(1-DBL_EPSILON
); ghi
= pow(ghi
, g1
)*(1+DBL_EPSILON
);
7161 b
= pow(b
, g1
)*(1-DBL_EPSILON
); bhi
= pow(bhi
, g1
)*(1+DBL_EPSILON
);
7163 /* Work out the lower and upper bounds for the gray value in the
7164 * encoded space, then work out an average and error. Remove the
7165 * previously added input quantization error at this point.
7167 gray
= r
* data
.red_coefficient
+ g
* data
.green_coefficient
+
7168 b
* data
.blue_coefficient
- 1./32768 - out_qe
;
7173 gray
*= (1 - 6 * DBL_EPSILON
);
7174 gray
= pow(gray
, data
.gamma
) * (1-DBL_EPSILON
);
7177 grayhi
= rhi
* data
.red_coefficient
+ ghi
* data
.green_coefficient
+
7178 bhi
* data
.blue_coefficient
+ 1./32768 + out_qe
;
7179 grayhi
*= (1 + 6 * DBL_EPSILON
);
7183 grayhi
= pow(grayhi
, data
.gamma
) * (1+DBL_EPSILON
);
7185 err
= (grayhi
- gray
) / 2;
7186 gray
= (grayhi
+ gray
) / 2;
7189 err
= gray
* DBL_EPSILON
;
7194 /* Validate that the error is within limits (this has caused
7195 * problems before, it's much easier to detect them here.)
7197 if (err
> pm
->limit
)
7202 pos
= safecat(buffer
, sizeof buffer
, pos
, "rgb_to_gray error ");
7203 pos
= safecatd(buffer
, sizeof buffer
, pos
, err
, 6);
7204 pos
= safecat(buffer
, sizeof buffer
, pos
, " exceeds limit ");
7205 pos
= safecatd(buffer
, sizeof buffer
, pos
, pm
->limit
, 6);
7206 png_error(pp
, buffer
);
7210 # endif /* !DIGITIZE */
7212 that
->bluef
= that
->greenf
= that
->redf
= gray
;
7213 that
->bluee
= that
->greene
= that
->rede
= err
;
7215 /* The sBIT is the minium of the three colour channel sBITs. */
7216 if (that
->red_sBIT
> that
->green_sBIT
)
7217 that
->red_sBIT
= that
->green_sBIT
;
7218 if (that
->red_sBIT
> that
->blue_sBIT
)
7219 that
->red_sBIT
= that
->blue_sBIT
;
7220 that
->blue_sBIT
= that
->green_sBIT
= that
->red_sBIT
;
7222 /* And remove the colour bit in the type: */
7223 if (that
->colour_type
== PNG_COLOR_TYPE_RGB
)
7224 that
->colour_type
= PNG_COLOR_TYPE_GRAY
;
7225 else if (that
->colour_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
7226 that
->colour_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
7229 this->next
->mod(this->next
, that
, pp
, display
);
7233 image_transform_png_set_rgb_to_gray_add(image_transform
*this,
7234 PNG_CONST image_transform
**that
, png_byte colour_type
, png_byte bit_depth
)
7241 return (colour_type
& PNG_COLOR_MASK_COLOR
) != 0;
7247 #define PT ITSTRUCT(rgb_to_gray)
7248 #undef image_transform_ini
7249 #define image_transform_ini image_transform_default_ini
7250 #endif /* PNG_READ_RGB_TO_GRAY_SUPPORTED */
7252 #ifdef PNG_READ_BACKGROUND_SUPPORTED
7253 /* png_set_background(png_structp, png_const_color_16p background_color,
7254 * int background_gamma_code, int need_expand, double background_gamma)
7255 * png_set_background_fixed(png_structp, png_const_color_16p background_color,
7256 * int background_gamma_code, int need_expand,
7257 * png_fixed_point background_gamma)
7259 * This ignores the gamma (at present.)
7261 #define data ITDATA(background)
7262 static image_pixel data
;
7265 image_transform_png_set_background_set(PNG_CONST image_transform
*this,
7266 transform_display
*that
, png_structp pp
, png_infop pi
)
7268 png_byte colour_type
, bit_depth
;
7269 png_byte random_bytes
[8]; /* 8 bytes - 64 bits - the biggest pixel */
7273 /* We need a background colour, because we don't know exactly what transforms
7274 * have been set we have to supply the colour in the original file format and
7275 * so we need to know what that is! The background colour is stored in the
7276 * transform_display.
7278 RANDOMIZE(random_bytes
);
7280 /* Read the random value, for colour type 3 the background colour is actually
7281 * expressed as a 24bit rgb, not an index.
7283 colour_type
= that
->this.colour_type
;
7284 if (colour_type
== 3)
7286 colour_type
= PNG_COLOR_TYPE_RGB
;
7288 expand
= 0; /* passing in an RGB not a pixel index */
7293 bit_depth
= that
->this.bit_depth
;
7297 image_pixel_init(&data
, random_bytes
, colour_type
,
7298 bit_depth
, 0/*x*/, 0/*unused: palette*/);
7300 /* Extract the background colour from this image_pixel, but make sure the
7301 * unused fields of 'back' are garbage.
7305 if (colour_type
& PNG_COLOR_MASK_COLOR
)
7307 back
.red
= (png_uint_16
)data
.red
;
7308 back
.green
= (png_uint_16
)data
.green
;
7309 back
.blue
= (png_uint_16
)data
.blue
;
7313 back
.gray
= (png_uint_16
)data
.red
;
7315 # ifdef PNG_FLOATING_POINT_SUPPORTED
7316 png_set_background(pp
, &back
, PNG_BACKGROUND_GAMMA_FILE
, expand
, 0);
7318 png_set_background_fixed(pp
, &back
, PNG_BACKGROUND_GAMMA_FILE
, expand
, 0);
7321 this->next
->set(this->next
, that
, pp
, pi
);
7325 image_transform_png_set_background_mod(PNG_CONST image_transform
*this,
7326 image_pixel
*that
, png_const_structp pp
,
7327 PNG_CONST transform_display
*display
)
7329 /* Check for tRNS first: */
7330 if (that
->have_tRNS
&& that
->colour_type
!= PNG_COLOR_TYPE_PALETTE
)
7331 image_pixel_add_alpha(that
, &display
->this);
7333 /* This is only necessary if the alpha value is less than 1. */
7334 if (that
->alphaf
< 1)
7336 /* Now we do the background calculation without any gamma correction. */
7337 if (that
->alphaf
<= 0)
7339 that
->redf
= data
.redf
;
7340 that
->greenf
= data
.greenf
;
7341 that
->bluef
= data
.bluef
;
7343 that
->rede
= data
.rede
;
7344 that
->greene
= data
.greene
;
7345 that
->bluee
= data
.bluee
;
7347 that
->red_sBIT
= data
.red_sBIT
;
7348 that
->green_sBIT
= data
.green_sBIT
;
7349 that
->blue_sBIT
= data
.blue_sBIT
;
7352 else /* 0 < alpha < 1 */
7354 double alf
= 1 - that
->alphaf
;
7356 that
->redf
= that
->redf
* that
->alphaf
+ data
.redf
* alf
;
7357 that
->rede
= that
->rede
* that
->alphaf
+ data
.rede
* alf
+
7359 that
->greenf
= that
->greenf
* that
->alphaf
+ data
.greenf
* alf
;
7360 that
->greene
= that
->greene
* that
->alphaf
+ data
.greene
* alf
+
7362 that
->bluef
= that
->bluef
* that
->alphaf
+ data
.bluef
* alf
;
7363 that
->bluee
= that
->bluee
* that
->alphaf
+ data
.bluee
* alf
+
7367 /* Remove the alpha type and set the alpha (not in that order.) */
7371 if (that
->colour_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
7372 that
->colour_type
= PNG_COLOR_TYPE_RGB
;
7373 else if (that
->colour_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
7374 that
->colour_type
= PNG_COLOR_TYPE_GRAY
;
7375 /* PNG_COLOR_TYPE_PALETTE is not changed */
7378 this->next
->mod(this->next
, that
, pp
, display
);
7381 #define image_transform_png_set_background_add image_transform_default_add
7386 #define PT ITSTRUCT(background)
7387 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
7389 /* This may just be 'end' if all the transforms are disabled! */
7390 static image_transform
*PNG_CONST image_transform_first
= &PT
;
7393 transform_enable(PNG_CONST
char *name
)
7395 /* Everything starts out enabled, so if we see an 'enable' disabled
7396 * everything else the first time round.
7398 static int all_disabled
= 0;
7400 image_transform
*list
= image_transform_first
;
7402 while (list
!= &image_transform_end
)
7404 if (strcmp(list
->name
, name
) == 0)
7409 else if (!all_disabled
)
7419 fprintf(stderr
, "pngvalid: --transform-enable=%s: unknown transform\n",
7426 transform_disable(PNG_CONST
char *name
)
7428 image_transform
*list
= image_transform_first
;
7430 while (list
!= &image_transform_end
)
7432 if (strcmp(list
->name
, name
) == 0)
7441 fprintf(stderr
, "pngvalid: --transform-disable=%s: unknown transform\n",
7447 image_transform_reset_count(void)
7449 image_transform
*next
= image_transform_first
;
7452 while (next
!= &image_transform_end
)
7454 next
->local_use
= 0;
7460 /* This can only happen if we every have more than 32 transforms (excluding
7461 * the end) in the list.
7463 if (count
> 32) abort();
7467 image_transform_test_counter(png_uint_32 counter
, unsigned int max
)
7469 /* Test the list to see if there is any point contining, given a current
7470 * counter and a 'max' value.
7472 image_transform
*next
= image_transform_first
;
7474 while (next
!= &image_transform_end
)
7476 /* For max 0 or 1 continue until the counter overflows: */
7479 /* Continue if any entry hasn't reacked the max. */
7480 if (max
> 1 && next
->local_use
< max
)
7485 return max
<= 1 && counter
== 0;
7489 image_transform_add(PNG_CONST image_transform
**this, unsigned int max
,
7490 png_uint_32 counter
, char *name
, size_t sizeof_name
, size_t *pos
,
7491 png_byte colour_type
, png_byte bit_depth
)
7493 for (;;) /* until we manage to add something */
7496 image_transform
*list
;
7498 /* Find the next counter value, if the counter is zero this is the start
7499 * of the list. This routine always returns the current counter (not the
7500 * next) so it returns 0 at the end and expects 0 at the beginning.
7502 if (counter
== 0) /* first time */
7504 image_transform_reset_count();
7508 counter
= random_32();
7510 else /* advance the counter */
7514 case 0: ++counter
; break;
7515 case 1: counter
<<= 1; break;
7516 default: counter
= random_32(); break;
7520 /* Now add all these items, if possible */
7521 *this = &image_transform_end
;
7522 list
= image_transform_first
;
7525 /* Go through the whole list adding anything that the counter selects: */
7526 while (list
!= &image_transform_end
)
7528 if ((counter
& mask
) != 0 && list
->enable
&&
7529 (max
== 0 || list
->local_use
< max
))
7531 /* Candidate to add: */
7532 if (list
->add(list
, this, colour_type
, bit_depth
) || max
== 0)
7534 /* Added, so add to the name too. */
7535 *pos
= safecat(name
, sizeof_name
, *pos
, " +");
7536 *pos
= safecat(name
, sizeof_name
, *pos
, list
->name
);
7541 /* Not useful and max>0, so remove it from *this: */
7545 /* And, since we know it isn't useful, stop it being added again
7548 list
->local_use
= max
;
7556 /* Now if anything was added we have something to do. */
7557 if (*this != &image_transform_end
)
7560 /* Nothing added, but was there anything in there to add? */
7561 if (!image_transform_test_counter(counter
, max
))
7566 #ifdef THIS_IS_THE_PROFORMA
7568 image_transform_png_set_@
_set(PNG_CONST image_transform
*this,
7569 transform_display
*that
, png_structp pp
, png_infop pi
)
7572 this->next
->set(this->next
, that
, pp
, pi
);
7576 image_transform_png_set_@
_mod(PNG_CONST image_transform
*this,
7577 image_pixel
*that
, png_const_structp pp
,
7578 PNG_CONST transform_display
*display
)
7580 this->next
->mod(this->next
, that
, pp
, display
);
7584 image_transform_png_set_@
_add(image_transform
*this,
7585 PNG_CONST image_transform
**that
, char *name
, size_t sizeof_name
,
7586 size_t *pos
, png_byte colour_type
, png_byte bit_depth
)
7591 *pos
= safecat(name
, sizeof_name
, *pos
, " +@");
7599 /* png_set_quantize(png_structp, png_colorp palette, int num_palette,
7600 * int maximum_colors, png_const_uint_16p histogram, int full_quantize)
7602 * Very difficult to validate this!
7606 /* The data layout transforms are handled by swapping our own channel data,
7607 * necessarily these need to happen at the end of the transform list because the
7608 * semantic of the channels changes after these are executed. Some of these,
7609 * like set_shift and set_packing, can't be done at present because they change
7610 * the layout of the data at the sub-sample level so sample() won't get the
7613 /* png_set_invert_alpha */
7619 /* png_set_swap_alpha */
7625 /* png_set_filler, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7628 /* png_set_add_alpha, (png_structp png_ptr, png_uint_32 filler, int flags)); */
7631 /* png_set_packing */
7634 /* png_set_packswap */
7637 /* png_set_invert_mono */
7640 /* png_set_shift(png_structp, png_const_color_8p true_bits) */
7644 perform_transform_test(png_modifier
*pm
)
7646 png_byte colour_type
= 0;
7647 png_byte bit_depth
= 0;
7648 unsigned int palette_number
= 0;
7650 while (next_format(&colour_type
, &bit_depth
, &palette_number
, 0))
7652 png_uint_32 counter
= 0;
7656 base_pos
= safecat(name
, sizeof name
, 0, "transform:");
7660 size_t pos
= base_pos
;
7661 PNG_CONST image_transform
*list
= 0;
7663 /* 'max' is currently hardwired to '1'; this should be settable on the
7666 counter
= image_transform_add(&list
, 1/*max*/, counter
,
7667 name
, sizeof name
, &pos
, colour_type
, bit_depth
);
7672 /* The command line can change this to checking interlaced images. */
7676 transform_test(pm
, FILEID(colour_type
, bit_depth
, palette_number
,
7677 pm
->interlace_type
, 0, 0, 0), list
, name
);
7686 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
7688 /********************************* GAMMA TESTS ********************************/
7689 #ifdef PNG_READ_GAMMA_SUPPORTED
7690 /* Reader callbacks and implementations, where they differ from the standard
7693 typedef struct gamma_display
7695 standard_display
this;
7700 double screen_gamma
;
7701 double background_gamma
;
7704 int use_input_precision
;
7708 png_color_16 background_color
;
7710 /* Local variables */
7716 #define ALPHA_MODE_OFFSET 4
7719 gamma_display_init(gamma_display
*dp
, png_modifier
*pm
, png_uint_32 id
,
7720 double file_gamma
, double screen_gamma
, png_byte sbit
, int threshold_test
,
7721 int use_input_precision
, int scale16
, int expand16
,
7722 int do_background
, PNG_CONST png_color_16
*pointer_to_the_background_color
,
7723 double background_gamma
)
7725 /* Standard fields */
7726 standard_display_init(&dp
->this, &pm
->this, id
, 0/*do_interlace*/,
7727 pm
->use_update_info
);
7729 /* Parameter fields */
7731 dp
->file_gamma
= file_gamma
;
7732 dp
->screen_gamma
= screen_gamma
;
7733 dp
->background_gamma
= background_gamma
;
7735 dp
->threshold_test
= threshold_test
;
7736 dp
->use_input_precision
= use_input_precision
;
7737 dp
->scale16
= scale16
;
7738 dp
->expand16
= expand16
;
7739 dp
->do_background
= do_background
;
7740 if (do_background
&& pointer_to_the_background_color
!= 0)
7741 dp
->background_color
= *pointer_to_the_background_color
;
7743 memset(&dp
->background_color
, 0, sizeof dp
->background_color
);
7745 /* Local variable fields */
7746 dp
->maxerrout
= dp
->maxerrpc
= dp
->maxerrabs
= 0;
7750 gamma_info_imp(gamma_display
*dp
, png_structp pp
, png_infop pi
)
7752 /* Reuse the standard stuff as appropriate. */
7753 standard_info_part1(&dp
->this, pp
, pi
);
7755 /* If requested strip 16 to 8 bits - this is handled automagically below
7756 * because the output bit depth is read from the library. Note that there
7757 * are interactions with sBIT but, internally, libpng makes sbit at most
7758 * PNG_MAX_GAMMA_8 when doing the following.
7761 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
7762 png_set_scale_16(pp
);
7764 /* The following works both in 1.5.4 and earlier versions: */
7765 # ifdef PNG_READ_16_TO_8_SUPPORTED
7766 png_set_strip_16(pp
);
7768 png_error(pp
, "scale16 (16 to 8 bit conversion) not supported");
7773 # ifdef PNG_READ_EXPAND_16_SUPPORTED
7774 png_set_expand_16(pp
);
7776 png_error(pp
, "expand16 (8 to 16 bit conversion) not supported");
7779 if (dp
->do_background
>= ALPHA_MODE_OFFSET
)
7781 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
7783 /* This tests the alpha mode handling, if supported. */
7784 int mode
= dp
->do_background
- ALPHA_MODE_OFFSET
;
7786 /* The gamma value is the output gamma, and is in the standard,
7787 * non-inverted, represenation. It provides a default for the PNG file
7788 * gamma, but since the file has a gAMA chunk this does not matter.
7790 PNG_CONST
double sg
= dp
->screen_gamma
;
7791 # ifndef PNG_FLOATING_POINT_SUPPORTED
7792 PNG_CONST png_fixed_point g
= fix(sg
);
7795 # ifdef PNG_FLOATING_POINT_SUPPORTED
7796 png_set_alpha_mode(pp
, mode
, sg
);
7798 png_set_alpha_mode_fixed(pp
, mode
, g
);
7801 /* However, for the standard Porter-Duff algorithm the output defaults
7802 * to be linear, so if the test requires non-linear output it must be
7805 if (mode
== PNG_ALPHA_STANDARD
&& sg
!= 1)
7807 # ifdef PNG_FLOATING_POINT_SUPPORTED
7808 png_set_gamma(pp
, sg
, dp
->file_gamma
);
7810 png_fixed_point f
= fix(dp
->file_gamma
);
7811 png_set_gamma_fixed(pp
, g
, f
);
7816 png_error(pp
, "alpha mode handling not supported");
7822 /* Set up gamma processing. */
7823 # ifdef PNG_FLOATING_POINT_SUPPORTED
7824 png_set_gamma(pp
, dp
->screen_gamma
, dp
->file_gamma
);
7827 png_fixed_point s
= fix(dp
->screen_gamma
);
7828 png_fixed_point f
= fix(dp
->file_gamma
);
7829 png_set_gamma_fixed(pp
, s
, f
);
7833 if (dp
->do_background
)
7835 # ifdef PNG_READ_BACKGROUND_SUPPORTED
7836 /* NOTE: this assumes the caller provided the correct background gamma!
7838 PNG_CONST
double bg
= dp
->background_gamma
;
7839 # ifndef PNG_FLOATING_POINT_SUPPORTED
7840 PNG_CONST png_fixed_point g
= fix(bg
);
7843 # ifdef PNG_FLOATING_POINT_SUPPORTED
7844 png_set_background(pp
, &dp
->background_color
, dp
->do_background
,
7845 0/*need_expand*/, bg
);
7847 png_set_background_fixed(pp
, &dp
->background_color
,
7848 dp
->do_background
, 0/*need_expand*/, g
);
7851 png_error(pp
, "png_set_background not supported");
7857 int i
= dp
->this.use_update_info
;
7858 /* Always do one call, even if use_update_info is 0. */
7860 png_read_update_info(pp
, pi
);
7864 /* Now we may get a different cbRow: */
7865 standard_info_part2(&dp
->this, pp
, pi
, 1 /*images*/);
7869 gamma_info(png_structp pp
, png_infop pi
)
7871 gamma_info_imp(voidcast(gamma_display
*, png_get_progressive_ptr(pp
)), pp
,
7875 /* Validate a single component value - the routine gets the input and output
7876 * sample values as unscaled PNG component values along with a cache of all the
7877 * information required to validate the values.
7879 typedef struct validate_info
7881 png_const_structp pp
;
7884 int use_input_precision
;
7887 unsigned int sbit_max
;
7888 unsigned int isbit_shift
;
7889 unsigned int outmax
;
7891 double gamma_correction
; /* Overall correction required. */
7892 double file_inverse
; /* Inverse of file gamma. */
7893 double screen_gamma
;
7894 double screen_inverse
; /* Inverse of screen gamma. */
7896 double background_red
; /* Linear background value, red or gray. */
7897 double background_green
;
7898 double background_blue
;
7904 double maxout_total
; /* Total including quantization error */
7911 init_validate_info(validate_info
*vi
, gamma_display
*dp
, png_const_structp pp
,
7912 int in_depth
, int out_depth
)
7914 PNG_CONST
unsigned int outmax
= (1U<<out_depth
)-1;
7919 if (dp
->sbit
> 0 && dp
->sbit
< in_depth
)
7921 vi
->sbit
= dp
->sbit
;
7922 vi
->isbit_shift
= in_depth
- dp
->sbit
;
7927 vi
->sbit
= (png_byte
)in_depth
;
7928 vi
->isbit_shift
= 0;
7931 vi
->sbit_max
= (1U << vi
->sbit
)-1;
7933 /* This mimics the libpng threshold test, '0' is used to prevent gamma
7934 * correction in the validation test.
7936 vi
->screen_gamma
= dp
->screen_gamma
;
7937 if (fabs(vi
->screen_gamma
-1) < PNG_GAMMA_THRESHOLD
)
7938 vi
->screen_gamma
= vi
->screen_inverse
= 0;
7940 vi
->screen_inverse
= 1/vi
->screen_gamma
;
7942 vi
->use_input_precision
= dp
->use_input_precision
;
7943 vi
->outmax
= outmax
;
7944 vi
->maxabs
= abserr(dp
->pm
, in_depth
, out_depth
);
7945 vi
->maxpc
= pcerr(dp
->pm
, in_depth
, out_depth
);
7946 vi
->maxcalc
= calcerr(dp
->pm
, in_depth
, out_depth
);
7947 vi
->maxout
= outerr(dp
->pm
, in_depth
, out_depth
);
7948 vi
->outquant
= output_quantization_factor(dp
->pm
, in_depth
, out_depth
);
7949 vi
->maxout_total
= vi
->maxout
+ vi
->outquant
* .5;
7950 vi
->outlog
= outlog(dp
->pm
, in_depth
, out_depth
);
7952 if ((dp
->this.colour_type
& PNG_COLOR_MASK_ALPHA
) != 0 ||
7953 (dp
->this.colour_type
== 3 && dp
->this.is_transparent
))
7955 vi
->do_background
= dp
->do_background
;
7957 if (vi
->do_background
!= 0)
7959 PNG_CONST
double bg_inverse
= 1/dp
->background_gamma
;
7962 /* Caller must at least put the gray value into the red channel */
7963 r
= dp
->background_color
.red
; r
/= outmax
;
7964 g
= dp
->background_color
.green
; g
/= outmax
;
7965 b
= dp
->background_color
.blue
; b
/= outmax
;
7968 /* libpng doesn't do this optimization, if we do pngvalid will fail.
7970 if (fabs(bg_inverse
-1) >= PNG_GAMMA_THRESHOLD
)
7973 r
= pow(r
, bg_inverse
);
7974 g
= pow(g
, bg_inverse
);
7975 b
= pow(b
, bg_inverse
);
7978 vi
->background_red
= r
;
7979 vi
->background_green
= g
;
7980 vi
->background_blue
= b
;
7984 vi
->do_background
= 0;
7986 if (vi
->do_background
== 0)
7987 vi
->background_red
= vi
->background_green
= vi
->background_blue
= 0;
7989 vi
->gamma_correction
= 1/(dp
->file_gamma
*dp
->screen_gamma
);
7990 if (fabs(vi
->gamma_correction
-1) < PNG_GAMMA_THRESHOLD
)
7991 vi
->gamma_correction
= 0;
7993 vi
->file_inverse
= 1/dp
->file_gamma
;
7994 if (fabs(vi
->file_inverse
-1) < PNG_GAMMA_THRESHOLD
)
7995 vi
->file_inverse
= 0;
7997 vi
->scale16
= dp
->scale16
;
8000 /* This function handles composition of a single non-alpha component. The
8001 * argument is the input sample value, in the range 0..1, and the alpha value.
8002 * The result is the composed, linear, input sample. If alpha is less than zero
8003 * this is the alpha component and the function should not be called!
8006 gamma_component_compose(int do_background
, double input_sample
, double alpha
,
8007 double background
, int *compose
)
8009 switch (do_background
)
8011 #ifdef PNG_READ_BACKGROUND_SUPPORTED
8012 case PNG_BACKGROUND_GAMMA_SCREEN
:
8013 case PNG_BACKGROUND_GAMMA_FILE
:
8014 case PNG_BACKGROUND_GAMMA_UNIQUE
:
8015 /* Standard PNG background processing. */
8020 input_sample
= input_sample
* alpha
+ background
* (1-alpha
);
8021 if (compose
!= NULL
)
8026 input_sample
= background
;
8031 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8032 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_STANDARD
:
8033 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
:
8034 /* The components are premultiplied in either case and the output is
8035 * gamma encoded (to get standard Porter-Duff we expect the output
8036 * gamma to be set to 1.0!)
8038 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_OPTIMIZED
:
8039 /* The optimization is that the partial-alpha entries are linear
8040 * while the opaque pixels are gamma encoded, but this only affects the
8047 input_sample
*= alpha
;
8048 if (compose
!= NULL
)
8059 /* Standard cases where no compositing is done (so the component
8060 * value is already correct.)
8068 return input_sample
;
8071 /* This API returns the encoded *input* component, in the range 0..1 */
8073 gamma_component_validate(PNG_CONST
char *name
, PNG_CONST validate_info
*vi
,
8074 PNG_CONST
unsigned int id
, PNG_CONST
unsigned int od
,
8075 PNG_CONST
double alpha
/* <0 for the alpha channel itself */,
8076 PNG_CONST
double background
/* component background value */)
8078 PNG_CONST
unsigned int isbit
= id
>> vi
->isbit_shift
;
8079 PNG_CONST
unsigned int sbit_max
= vi
->sbit_max
;
8080 PNG_CONST
unsigned int outmax
= vi
->outmax
;
8081 PNG_CONST
int do_background
= vi
->do_background
;
8085 /* First check on the 'perfect' result obtained from the digitized input
8086 * value, id, and compare this against the actual digitized result, 'od'.
8087 * 'i' is the input result in the range 0..1:
8089 i
= isbit
; i
/= sbit_max
;
8091 /* Check for the fast route: if we don't do any background composition or if
8092 * this is the alpha channel ('alpha' < 0) or if the pixel is opaque then
8093 * just use the gamma_correction field to correct to the final output gamma.
8095 if (alpha
== 1 /* opaque pixel component */ || !do_background
8096 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8097 || do_background
== ALPHA_MODE_OFFSET
+ PNG_ALPHA_PNG
8099 || (alpha
< 0 /* alpha channel */
8100 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8101 && do_background
!= ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
8105 /* Then get the gamma corrected version of 'i' and compare to 'od', any
8106 * error less than .5 is insignificant - just quantization of the output
8107 * value to the nearest digital value (nevertheless the error is still
8108 * recorded - it's interesting ;-)
8110 double encoded_sample
= i
;
8111 double encoded_error
;
8113 /* alpha less than 0 indicates the alpha channel, which is always linear
8115 if (alpha
>= 0 && vi
->gamma_correction
> 0)
8116 encoded_sample
= pow(encoded_sample
, vi
->gamma_correction
);
8117 encoded_sample
*= outmax
;
8119 encoded_error
= fabs(od
-encoded_sample
);
8121 if (encoded_error
> vi
->dp
->maxerrout
)
8122 vi
->dp
->maxerrout
= encoded_error
;
8124 if (encoded_error
< vi
->maxout_total
&& encoded_error
< vi
->outlog
)
8128 /* The slow route - attempt to do linear calculations. */
8129 /* There may be an error, or background processing is required, so calculate
8130 * the actual sample values - unencoded light intensity values. Note that in
8131 * practice these are not completely unencoded because they include a
8132 * 'viewing correction' to decrease or (normally) increase the perceptual
8133 * contrast of the image. There's nothing we can do about this - we don't
8134 * know what it is - so assume the unencoded value is perceptually linear.
8137 double input_sample
= i
; /* In range 0..1 */
8138 double output
, error
, encoded_sample
, encoded_error
;
8139 double es_lo
, es_hi
;
8140 int compose
= 0; /* Set to one if composition done */
8141 int output_is_encoded
; /* Set if encoded to screen gamma */
8142 int log_max_error
= 1; /* Check maximum error values */
8143 png_const_charp pass
= 0; /* Reason test passes (or 0 for fail) */
8145 /* Convert to linear light (with the above caveat.) The alpha channel is
8152 if (vi
->file_inverse
> 0)
8153 input_sample
= pow(input_sample
, vi
->file_inverse
);
8155 /* Handle the compose processing: */
8157 input_sample
= gamma_component_compose(do_background
, input_sample
,
8158 alpha
, background
, &tcompose
);
8164 /* And similarly for the output value, but we need to check the background
8165 * handling to linearize it correctly.
8170 output_is_encoded
= vi
->screen_gamma
> 0;
8172 if (alpha
< 0) /* The alpha channel */
8174 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8175 if (do_background
!= ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
)
8178 /* In all other cases the output alpha channel is linear already,
8179 * don't log errors here, they are much larger in linear data.
8181 output_is_encoded
= 0;
8186 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8187 else /* A component */
8189 if (do_background
== ALPHA_MODE_OFFSET
+ PNG_ALPHA_OPTIMIZED
&&
8190 alpha
< 1) /* the optimized case - linear output */
8192 if (alpha
> 0) log_max_error
= 0;
8193 output_is_encoded
= 0;
8198 if (output_is_encoded
)
8199 output
= pow(output
, vi
->screen_gamma
);
8201 /* Calculate (or recalculate) the encoded_sample value and repeat the
8202 * check above (unnecessary if we took the fast route, but harmless.)
8204 encoded_sample
= input_sample
;
8205 if (output_is_encoded
)
8206 encoded_sample
= pow(encoded_sample
, vi
->screen_inverse
);
8207 encoded_sample
*= outmax
;
8209 encoded_error
= fabs(od
-encoded_sample
);
8211 /* Don't log errors in the alpha channel, or the 'optimized' case,
8212 * neither are significant to the overall perception.
8214 if (log_max_error
&& encoded_error
> vi
->dp
->maxerrout
)
8215 vi
->dp
->maxerrout
= encoded_error
;
8217 if (encoded_error
< vi
->maxout_total
)
8219 if (encoded_error
< vi
->outlog
)
8222 /* Test passed but error is bigger than the log limit, record why the
8225 pass
= "less than maxout:\n";
8228 /* i: the original input value in the range 0..1
8230 * pngvalid calculations:
8231 * input_sample: linear result; i linearized and composed, range 0..1
8232 * encoded_sample: encoded result; input_sample scaled to ouput bit depth
8234 * libpng calculations:
8235 * output: linear result; od scaled to 0..1 and linearized
8236 * od: encoded result from libpng
8239 /* Now we have the numbers for real errors, both absolute values as as a
8240 * percentage of the correct value (output):
8242 error
= fabs(input_sample
-output
);
8244 if (log_max_error
&& error
> vi
->dp
->maxerrabs
)
8245 vi
->dp
->maxerrabs
= error
;
8247 /* The following is an attempt to ignore the tendency of quantization to
8248 * dominate the percentage errors for lower result values:
8250 if (log_max_error
&& input_sample
> .5)
8252 double percentage_error
= error
/input_sample
;
8253 if (percentage_error
> vi
->dp
->maxerrpc
)
8254 vi
->dp
->maxerrpc
= percentage_error
;
8257 /* Now calculate the digitization limits for 'encoded_sample' using the
8258 * 'max' values. Note that maxout is in the encoded space but maxpc and
8259 * maxabs are in linear light space.
8261 * First find the maximum error in linear light space, range 0..1:
8264 double tmp
= input_sample
* vi
->maxpc
;
8265 if (tmp
< vi
->maxabs
) tmp
= vi
->maxabs
;
8266 /* If 'compose' is true the composition was done in linear space using
8267 * integer arithmetic. This introduces an extra error of +/- 0.5 (at
8268 * least) in the integer space used. 'maxcalc' records this, taking
8269 * into account the possibility that even for 16 bit output 8 bit space
8270 * may have been used.
8272 if (compose
&& tmp
< vi
->maxcalc
) tmp
= vi
->maxcalc
;
8274 /* The 'maxout' value refers to the encoded result, to compare with
8275 * this encode input_sample adjusted by the maximum error (tmp) above.
8277 es_lo
= encoded_sample
- vi
->maxout
;
8279 if (es_lo
> 0 && input_sample
-tmp
> 0)
8281 double low_value
= input_sample
-tmp
;
8282 if (output_is_encoded
)
8283 low_value
= pow(low_value
, vi
->screen_inverse
);
8284 low_value
*= outmax
;
8285 if (low_value
< es_lo
) es_lo
= low_value
;
8287 /* Quantize this appropriately: */
8288 es_lo
= ceil(es_lo
/ vi
->outquant
- .5) * vi
->outquant
;
8294 es_hi
= encoded_sample
+ vi
->maxout
;
8296 if (es_hi
< outmax
&& input_sample
+tmp
< 1)
8298 double high_value
= input_sample
+tmp
;
8299 if (output_is_encoded
)
8300 high_value
= pow(high_value
, vi
->screen_inverse
);
8301 high_value
*= outmax
;
8302 if (high_value
> es_hi
) es_hi
= high_value
;
8304 es_hi
= floor(es_hi
/ vi
->outquant
+ .5) * vi
->outquant
;
8311 /* The primary test is that the final encoded value returned by the
8312 * library should be between the two limits (inclusive) that were
8315 if (od
>= es_lo
&& od
<= es_hi
)
8317 /* The value passes, but we may need to log the information anyway. */
8318 if (encoded_error
< vi
->outlog
)
8322 pass
= "within digitization limits:\n";
8326 /* There has been an error in processing, or we need to log this
8329 double is_lo
, is_hi
;
8331 /* pass is set at this point if either of the tests above would have
8332 * passed. Don't do these additional tests here - just log the
8333 * original [es_lo..es_hi] values.
8335 if (pass
== 0 && vi
->use_input_precision
&& vi
->dp
->sbit
)
8337 /* Ok, something is wrong - this actually happens in current libpng
8338 * 16-to-8 processing. Assume that the input value (id, adjusted
8339 * for sbit) can be anywhere between value-.5 and value+.5 - quite a
8340 * large range if sbit is low.
8342 * NOTE: at present because the libpng gamma table stuff has been
8343 * changed to use a rounding algorithm to correct errors in 8-bit
8344 * calculations the precise sbit calculation (a shift) has been
8345 * lost. This can result in up to a +/-1 error in the presence of
8346 * an sbit less than the bit depth.
8348 # if PNG_LIBPNG_VER < 10700
8349 # define SBIT_ERROR .5
8351 # define SBIT_ERROR 1.
8353 double tmp
= (isbit
- SBIT_ERROR
)/sbit_max
;
8358 else if (alpha
>= 0 && vi
->file_inverse
> 0 && tmp
< 1)
8359 tmp
= pow(tmp
, vi
->file_inverse
);
8361 tmp
= gamma_component_compose(do_background
, tmp
, alpha
, background
,
8364 if (output_is_encoded
&& tmp
> 0 && tmp
< 1)
8365 tmp
= pow(tmp
, vi
->screen_inverse
);
8367 is_lo
= ceil(outmax
* tmp
- vi
->maxout_total
);
8372 tmp
= (isbit
+ SBIT_ERROR
)/sbit_max
;
8377 else if (alpha
>= 0 && vi
->file_inverse
> 0 && tmp
< 1)
8378 tmp
= pow(tmp
, vi
->file_inverse
);
8380 tmp
= gamma_component_compose(do_background
, tmp
, alpha
, background
,
8383 if (output_is_encoded
&& tmp
> 0 && tmp
< 1)
8384 tmp
= pow(tmp
, vi
->screen_inverse
);
8386 is_hi
= floor(outmax
* tmp
+ vi
->maxout_total
);
8391 if (!(od
< is_lo
|| od
> is_hi
))
8393 if (encoded_error
< vi
->outlog
)
8396 pass
= "within input precision limits:\n";
8399 /* One last chance. If this is an alpha channel and the 16to8
8400 * option has been used and 'inaccurate' scaling is used then the
8401 * bit reduction is obtained by simply using the top 8 bits of the
8404 * This is only done for older libpng versions when the 'inaccurate'
8405 * (chop) method of scaling was used.
8407 # ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
8408 # if PNG_LIBPNG_VER < 10504
8409 /* This may be required for other components in the future,
8410 * but at present the presence of gamma correction effectively
8411 * prevents the errors in the component scaling (I don't quite
8412 * understand why, but since it's better this way I care not
8413 * to ask, JB 20110419.)
8415 if (pass
== 0 && alpha
< 0 && vi
->scale16
&& vi
->sbit
> 8 &&
8416 vi
->sbit
+ vi
->isbit_shift
== 16)
8418 tmp
= ((id
>> 8) - .5)/255;
8422 is_lo
= ceil(outmax
* tmp
- vi
->maxout_total
);
8423 if (is_lo
< 0) is_lo
= 0;
8429 tmp
= ((id
>> 8) + .5)/255;
8433 is_hi
= floor(outmax
* tmp
+ vi
->maxout_total
);
8434 if (is_hi
> outmax
) is_hi
= outmax
;
8440 if (!(od
< is_lo
|| od
> is_hi
))
8442 if (encoded_error
< vi
->outlog
)
8445 pass
= "within 8 bit limits:\n";
8451 else /* !use_input_precision */
8452 is_lo
= es_lo
, is_hi
= es_hi
;
8454 /* Attempt to output a meaningful error/warning message: the message
8455 * output depends on the background/composite operation being performed
8456 * because this changes what parameters were actually used above.
8460 /* Need either 1/255 or 1/65535 precision here; 3 or 6 decimal
8461 * places. Just use outmax to work out which.
8463 int precision
= (outmax
>= 1000 ?
6 : 3);
8464 int use_input
=1, use_background
=0, do_compose
=0;
8468 pos
= safecat(msg
, sizeof msg
, pos
, "\n\t");
8470 /* Set up the various flags, the output_is_encoded flag above
8471 * is also used below. do_compose is just a double check.
8473 switch (do_background
)
8475 # ifdef PNG_READ_BACKGROUND_SUPPORTED
8476 case PNG_BACKGROUND_GAMMA_SCREEN
:
8477 case PNG_BACKGROUND_GAMMA_FILE
:
8478 case PNG_BACKGROUND_GAMMA_UNIQUE
:
8479 use_background
= (alpha
>= 0 && alpha
< 1);
8482 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
8483 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_STANDARD
:
8484 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
:
8485 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_OPTIMIZED
:
8486 # endif /* ALPHA_MODE_SUPPORTED */
8487 do_compose
= (alpha
> 0 && alpha
< 1);
8488 use_input
= (alpha
!= 0);
8495 /* Check the 'compose' flag */
8496 if (compose
!= do_compose
)
8497 png_error(vi
->pp
, "internal error (compose)");
8499 /* 'name' is the component name */
8500 pos
= safecat(msg
, sizeof msg
, pos
, name
);
8501 pos
= safecat(msg
, sizeof msg
, pos
, "(");
8502 pos
= safecatn(msg
, sizeof msg
, pos
, id
);
8503 if (use_input
|| pass
!= 0/*logging*/)
8507 /* sBIT has reduced the precision of the input: */
8508 pos
= safecat(msg
, sizeof msg
, pos
, ", sbit(");
8509 pos
= safecatn(msg
, sizeof msg
, pos
, vi
->sbit
);
8510 pos
= safecat(msg
, sizeof msg
, pos
, "): ");
8511 pos
= safecatn(msg
, sizeof msg
, pos
, isbit
);
8513 pos
= safecat(msg
, sizeof msg
, pos
, "/");
8514 /* The output is either "id/max" or "id sbit(sbit): isbit/max" */
8515 pos
= safecatn(msg
, sizeof msg
, pos
, vi
->sbit_max
);
8517 pos
= safecat(msg
, sizeof msg
, pos
, ")");
8519 /* A component may have been multiplied (in linear space) by the
8520 * alpha value, 'compose' says whether this is relevant.
8522 if (compose
|| pass
!= 0)
8524 /* If any form of composition is being done report our
8525 * calculated linear value here (the code above doesn't record
8526 * the input value before composition is performed, so what
8527 * gets reported is the value after composition.)
8529 if (use_input
|| pass
!= 0)
8531 if (vi
->file_inverse
> 0)
8533 pos
= safecat(msg
, sizeof msg
, pos
, "^");
8534 pos
= safecatd(msg
, sizeof msg
, pos
, vi
->file_inverse
, 2);
8538 pos
= safecat(msg
, sizeof msg
, pos
, "[linear]");
8540 pos
= safecat(msg
, sizeof msg
, pos
, "*(alpha)");
8541 pos
= safecatd(msg
, sizeof msg
, pos
, alpha
, precision
);
8544 /* Now record the *linear* background value if it was used
8545 * (this function is not passed the original, non-linear,
8546 * value but it is contained in the test name.)
8550 pos
= safecat(msg
, sizeof msg
, pos
, use_input ?
"+" : " ");
8551 pos
= safecat(msg
, sizeof msg
, pos
, "(background)");
8552 pos
= safecatd(msg
, sizeof msg
, pos
, background
, precision
);
8553 pos
= safecat(msg
, sizeof msg
, pos
, "*");
8554 pos
= safecatd(msg
, sizeof msg
, pos
, 1-alpha
, precision
);
8558 /* Report the calculated value (input_sample) and the linearized
8559 * libpng value (output) unless this is just a component gamma
8562 if (compose
|| alpha
< 0 || pass
!= 0)
8564 pos
= safecat(msg
, sizeof msg
, pos
,
8565 pass
!= 0 ?
" =\n\t" : " = ");
8566 pos
= safecatd(msg
, sizeof msg
, pos
, input_sample
, precision
);
8567 pos
= safecat(msg
, sizeof msg
, pos
, " (libpng: ");
8568 pos
= safecatd(msg
, sizeof msg
, pos
, output
, precision
);
8569 pos
= safecat(msg
, sizeof msg
, pos
, ")");
8571 /* Finally report the output gamma encoding, if any. */
8572 if (output_is_encoded
)
8574 pos
= safecat(msg
, sizeof msg
, pos
, " ^");
8575 pos
= safecatd(msg
, sizeof msg
, pos
, vi
->screen_inverse
, 2);
8576 pos
= safecat(msg
, sizeof msg
, pos
, "(to screen) =");
8580 pos
= safecat(msg
, sizeof msg
, pos
, " [screen is linear] =");
8583 if ((!compose
&& alpha
>= 0) || pass
!= 0)
8585 if (pass
!= 0) /* logging */
8586 pos
= safecat(msg
, sizeof msg
, pos
, "\n\t[overall:");
8588 /* This is the non-composition case, the internal linear
8589 * values are irrelevant (though the log below will reveal
8590 * them.) Output a much shorter warning/error message and report
8591 * the overall gamma correction.
8593 if (vi
->gamma_correction
> 0)
8595 pos
= safecat(msg
, sizeof msg
, pos
, " ^");
8596 pos
= safecatd(msg
, sizeof msg
, pos
, vi
->gamma_correction
, 2);
8597 pos
= safecat(msg
, sizeof msg
, pos
, "(gamma correction) =");
8601 pos
= safecat(msg
, sizeof msg
, pos
,
8602 " [no gamma correction] =");
8605 pos
= safecat(msg
, sizeof msg
, pos
, "]");
8608 /* This is our calculated encoded_sample which should (but does
8611 pos
= safecat(msg
, sizeof msg
, pos
, pass
!= 0 ?
"\n\t" : " ");
8612 pos
= safecatd(msg
, sizeof msg
, pos
, is_lo
, 1);
8613 pos
= safecat(msg
, sizeof msg
, pos
, " < ");
8614 pos
= safecatd(msg
, sizeof msg
, pos
, encoded_sample
, 1);
8615 pos
= safecat(msg
, sizeof msg
, pos
, " (libpng: ");
8616 pos
= safecatn(msg
, sizeof msg
, pos
, od
);
8617 pos
= safecat(msg
, sizeof msg
, pos
, ")");
8618 pos
= safecat(msg
, sizeof msg
, pos
, "/");
8619 pos
= safecatn(msg
, sizeof msg
, pos
, outmax
);
8620 pos
= safecat(msg
, sizeof msg
, pos
, " < ");
8621 pos
= safecatd(msg
, sizeof msg
, pos
, is_hi
, 1);
8623 if (pass
== 0) /* The error condition */
8625 # ifdef PNG_WARNINGS_SUPPORTED
8626 png_warning(vi
->pp
, msg
);
8628 store_warning(vi
->pp
, msg
);
8632 else /* logging this value */
8633 store_verbose(&vi
->dp
->pm
->this, vi
->pp
, pass
, msg
);
8642 gamma_image_validate(gamma_display
*dp
, png_const_structp pp
,
8645 /* Get some constants derived from the input and output file formats: */
8646 PNG_CONST png_store
* PNG_CONST ps
= dp
->this.ps
;
8647 PNG_CONST png_byte in_ct
= dp
->this.colour_type
;
8648 PNG_CONST png_byte in_bd
= dp
->this.bit_depth
;
8649 PNG_CONST png_uint_32 w
= dp
->this.w
;
8650 PNG_CONST png_uint_32 h
= dp
->this.h
;
8651 PNG_CONST
size_t cbRow
= dp
->this.cbRow
;
8652 PNG_CONST png_byte out_ct
= png_get_color_type(pp
, pi
);
8653 PNG_CONST png_byte out_bd
= png_get_bit_depth(pp
, pi
);
8655 /* There are three sources of error, firstly the quantization in the
8656 * file encoding, determined by sbit and/or the file depth, secondly
8657 * the output (screen) gamma and thirdly the output file encoding.
8659 * Since this API receives the screen and file gamma in double
8660 * precision it is possible to calculate an exact answer given an input
8661 * pixel value. Therefore we assume that the *input* value is exact -
8662 * sample/maxsample - calculate the corresponding gamma corrected
8663 * output to the limits of double precision arithmetic and compare with
8664 * what libpng returns.
8666 * Since the library must quantize the output to 8 or 16 bits there is
8667 * a fundamental limit on the accuracy of the output of +/-.5 - this
8668 * quantization limit is included in addition to the other limits
8669 * specified by the paramaters to the API. (Effectively, add .5
8672 * The behavior of the 'sbit' paramter is defined by section 12.5
8673 * (sample depth scaling) of the PNG spec. That section forces the
8674 * decoder to assume that the PNG values have been scaled if sBIT is
8677 * png-sample = floor( input-sample * (max-out/max-in) + .5);
8679 * This means that only a subset of the possible PNG values should
8680 * appear in the input. However, the spec allows the encoder to use a
8681 * variety of approximations to the above and doesn't require any
8682 * restriction of the values produced.
8684 * Nevertheless the spec requires that the upper 'sBIT' bits of the
8685 * value stored in a PNG file be the original sample bits.
8686 * Consequently the code below simply scales the top sbit bits by
8687 * (1<<sbit)-1 to obtain an original sample value.
8689 * Because there is limited precision in the input it is arguable that
8690 * an acceptable result is any valid result from input-.5 to input+.5.
8691 * The basic tests below do not do this, however if 'use_input_precision'
8692 * is set a subsequent test is performed above.
8694 PNG_CONST
unsigned int samples_per_pixel
= (out_ct
& 2U) ?
3U : 1U;
8697 PNG_CONST store_palette_entry
*in_palette
= dp
->this.palette
;
8698 PNG_CONST
int in_is_transparent
= dp
->this.is_transparent
;
8699 int out_npalette
= -1;
8700 int out_is_transparent
= 0; /* Just refers to the palette case */
8701 store_palette out_palette
;
8704 /* Check for row overwrite errors */
8705 store_image_check(dp
->this.ps
, pp
, 0);
8707 /* Supply the input and output sample depths here - 8 for an indexed image,
8708 * otherwise the bit depth.
8710 init_validate_info(&vi
, dp
, pp
, in_ct
==3?
8:in_bd
, out_ct
==3?
8:out_bd
);
8712 processing
= (vi
.gamma_correction
> 0 && !dp
->threshold_test
)
8713 || in_bd
!= out_bd
|| in_ct
!= out_ct
|| vi
.do_background
;
8715 /* TODO: FIX THIS: MAJOR BUG! If the transformations all happen inside
8716 * the palette there is no way of finding out, because libpng fails to
8717 * update the palette on png_read_update_info. Indeed, libpng doesn't
8718 * even do the required work until much later, when it doesn't have any
8719 * info pointer. Oops. For the moment 'processing' is turned off if
8720 * out_ct is palette.
8722 if (in_ct
== 3 && out_ct
== 3)
8725 if (processing
&& out_ct
== 3)
8726 out_is_transparent
= read_palette(out_palette
, &out_npalette
, pp
, pi
);
8730 png_const_bytep pRow
= store_image_row(ps
, pp
, 0, y
);
8731 png_byte std
[STANDARD_ROWMAX
];
8733 transform_row(pp
, std
, in_ct
, in_bd
, y
);
8741 double alpha
= 1; /* serves as a flag value */
8743 /* Record the palette index for index images. */
8744 PNG_CONST
unsigned int in_index
=
8745 in_ct
== 3 ?
sample(std
, 3, in_bd
, x
, 0) : 256;
8746 PNG_CONST
unsigned int out_index
=
8747 out_ct
== 3 ?
sample(std
, 3, out_bd
, x
, 0) : 256;
8749 /* Handle input alpha - png_set_background will cause the output
8750 * alpha to disappear so there is nothing to check.
8752 if ((in_ct
& PNG_COLOR_MASK_ALPHA
) != 0 || (in_ct
== 3 &&
8755 PNG_CONST
unsigned int input_alpha
= in_ct
== 3 ?
8756 dp
->this.palette
[in_index
].alpha
:
8757 sample(std
, in_ct
, in_bd
, x
, samples_per_pixel
);
8759 unsigned int output_alpha
= 65536 /* as a flag value */;
8763 if (out_is_transparent
)
8764 output_alpha
= out_palette
[out_index
].alpha
;
8767 else if ((out_ct
& PNG_COLOR_MASK_ALPHA
) != 0)
8768 output_alpha
= sample(pRow
, out_ct
, out_bd
, x
,
8771 if (output_alpha
!= 65536)
8772 alpha
= gamma_component_validate("alpha", &vi
, input_alpha
,
8773 output_alpha
, -1/*alpha*/, 0/*background*/);
8775 else /* no alpha in output */
8777 /* This is a copy of the calculation of 'i' above in order to
8778 * have the alpha value to use in the background calculation.
8780 alpha
= input_alpha
>> vi
.isbit_shift
;
8781 alpha
/= vi
.sbit_max
;
8785 /* Handle grayscale or RGB components. */
8786 if ((in_ct
& PNG_COLOR_MASK_COLOR
) == 0) /* grayscale */
8787 (void)gamma_component_validate("gray", &vi
,
8788 sample(std
, in_ct
, in_bd
, x
, 0),
8789 sample(pRow
, out_ct
, out_bd
, x
, 0), alpha
/*component*/,
8791 else /* RGB or palette */
8793 (void)gamma_component_validate("red", &vi
,
8794 in_ct
== 3 ? in_palette
[in_index
].red
:
8795 sample(std
, in_ct
, in_bd
, x
, 0),
8796 out_ct
== 3 ? out_palette
[out_index
].red
:
8797 sample(pRow
, out_ct
, out_bd
, x
, 0),
8798 alpha
/*component*/, vi
.background_red
);
8800 (void)gamma_component_validate("green", &vi
,
8801 in_ct
== 3 ? in_palette
[in_index
].green
:
8802 sample(std
, in_ct
, in_bd
, x
, 1),
8803 out_ct
== 3 ? out_palette
[out_index
].green
:
8804 sample(pRow
, out_ct
, out_bd
, x
, 1),
8805 alpha
/*component*/, vi
.background_green
);
8807 (void)gamma_component_validate("blue", &vi
,
8808 in_ct
== 3 ? in_palette
[in_index
].blue
:
8809 sample(std
, in_ct
, in_bd
, x
, 2),
8810 out_ct
== 3 ? out_palette
[out_index
].blue
:
8811 sample(pRow
, out_ct
, out_bd
, x
, 2),
8812 alpha
/*component*/, vi
.background_blue
);
8817 else if (memcmp(std
, pRow
, cbRow
) != 0)
8821 /* No transform is expected on the threshold tests. */
8822 sprintf(msg
, "gamma: below threshold row %lu changed",
8827 } /* row (y) loop */
8829 dp
->this.ps
->validated
= 1;
8833 gamma_end(png_structp ppIn
, png_infop pi
)
8835 png_const_structp pp
= ppIn
;
8836 gamma_display
*dp
= voidcast(gamma_display
*, png_get_progressive_ptr(pp
));
8838 if (!dp
->this.speed
)
8839 gamma_image_validate(dp
, pp
, pi
);
8841 dp
->this.ps
->validated
= 1;
8844 /* A single test run checking a gamma transformation.
8846 * maxabs: maximum absolute error as a fraction
8847 * maxout: maximum output error in the output units
8848 * maxpc: maximum percentage error (as a percentage)
8851 gamma_test(png_modifier
*pmIn
, PNG_CONST png_byte colour_typeIn
,
8852 PNG_CONST png_byte bit_depthIn
, PNG_CONST
int palette_numberIn
,
8853 PNG_CONST
int interlace_typeIn
,
8854 PNG_CONST
double file_gammaIn
, PNG_CONST
double screen_gammaIn
,
8855 PNG_CONST png_byte sbitIn
, PNG_CONST
int threshold_testIn
,
8856 PNG_CONST
char *name
,
8857 PNG_CONST
int use_input_precisionIn
, PNG_CONST
int scale16In
,
8858 PNG_CONST
int expand16In
, PNG_CONST
int do_backgroundIn
,
8859 PNG_CONST png_color_16
*bkgd_colorIn
, double bkgd_gammaIn
)
8862 context(&pmIn
->this, fault
);
8864 gamma_display_init(&d
, pmIn
, FILEID(colour_typeIn
, bit_depthIn
,
8865 palette_numberIn
, interlace_typeIn
, 0, 0, 0),
8866 file_gammaIn
, screen_gammaIn
, sbitIn
,
8867 threshold_testIn
, use_input_precisionIn
, scale16In
,
8868 expand16In
, do_backgroundIn
, bkgd_colorIn
, bkgd_gammaIn
);
8874 gama_modification gama_mod
;
8875 srgb_modification srgb_mod
;
8876 sbit_modification sbit_mod
;
8878 /* For the moment don't use the png_modifier support here. */
8879 d
.pm
->encoding_counter
= 0;
8880 modifier_set_encoding(d
.pm
); /* Just resets everything */
8881 d
.pm
->current_gamma
= d
.file_gamma
;
8883 /* Make an appropriate modifier to set the PNG file gamma to the
8884 * given gamma value and the sBIT chunk to the given precision.
8886 d
.pm
->modifications
= NULL
;
8887 gama_modification_init(&gama_mod
, d
.pm
, d
.file_gamma
);
8888 srgb_modification_init(&srgb_mod
, d
.pm
, 127 /*delete*/);
8890 sbit_modification_init(&sbit_mod
, d
.pm
, d
.sbit
);
8892 modification_reset(d
.pm
->modifications
);
8894 /* Get a png_struct for writing the image. */
8895 pp
= set_modifier_for_read(d
.pm
, &pi
, d
.this.id
, name
);
8896 standard_palette_init(&d
.this);
8898 /* Introduce the correct read function. */
8899 if (d
.pm
->this.progressive
)
8901 /* Share the row function with the standard implementation. */
8902 png_set_progressive_read_fn(pp
, &d
, gamma_info
, progressive_row
,
8905 /* Now feed data into the reader until we reach the end: */
8906 modifier_progressive_read(d
.pm
, pp
, pi
);
8910 /* modifier_read expects a png_modifier* */
8911 png_set_read_fn(pp
, d
.pm
, modifier_read
);
8913 /* Check the header values: */
8914 png_read_info(pp
, pi
);
8916 /* Process the 'info' requirements. Only one image is generated */
8917 gamma_info_imp(&d
, pp
, pi
);
8919 sequential_row(&d
.this, pp
, pi
, -1, 0);
8922 gamma_image_validate(&d
, pp
, pi
);
8924 d
.this.ps
->validated
= 1;
8927 modifier_reset(d
.pm
);
8929 if (d
.pm
->log
&& !d
.threshold_test
&& !d
.this.speed
)
8930 fprintf(stderr
, "%d bit %s %s: max error %f (%.2g, %2g%%)\n",
8931 d
.this.bit_depth
, colour_types
[d
.this.colour_type
], name
,
8932 d
.maxerrout
, d
.maxerrabs
, 100*d
.maxerrpc
);
8934 /* Log the summary values too. */
8935 if (d
.this.colour_type
== 0 || d
.this.colour_type
== 4)
8937 switch (d
.this.bit_depth
)
8943 if (d
.maxerrout
> d
.pm
->error_gray_2
)
8944 d
.pm
->error_gray_2
= d
.maxerrout
;
8949 if (d
.maxerrout
> d
.pm
->error_gray_4
)
8950 d
.pm
->error_gray_4
= d
.maxerrout
;
8955 if (d
.maxerrout
> d
.pm
->error_gray_8
)
8956 d
.pm
->error_gray_8
= d
.maxerrout
;
8961 if (d
.maxerrout
> d
.pm
->error_gray_16
)
8962 d
.pm
->error_gray_16
= d
.maxerrout
;
8967 png_error(pp
, "bad bit depth (internal: 1)");
8971 else if (d
.this.colour_type
== 2 || d
.this.colour_type
== 6)
8973 switch (d
.this.bit_depth
)
8977 if (d
.maxerrout
> d
.pm
->error_color_8
)
8978 d
.pm
->error_color_8
= d
.maxerrout
;
8984 if (d
.maxerrout
> d
.pm
->error_color_16
)
8985 d
.pm
->error_color_16
= d
.maxerrout
;
8990 png_error(pp
, "bad bit depth (internal: 2)");
8994 else if (d
.this.colour_type
== 3)
8996 if (d
.maxerrout
> d
.pm
->error_indexed
)
8997 d
.pm
->error_indexed
= d
.maxerrout
;
9002 modifier_reset(voidcast(png_modifier
*,(void*)fault
));
9005 static void gamma_threshold_test(png_modifier
*pm
, png_byte colour_type
,
9006 png_byte bit_depth
, int interlace_type
, double file_gamma
,
9007 double screen_gamma
)
9011 pos
= safecat(name
, sizeof name
, pos
, "threshold ");
9012 pos
= safecatd(name
, sizeof name
, pos
, file_gamma
, 3);
9013 pos
= safecat(name
, sizeof name
, pos
, "/");
9014 pos
= safecatd(name
, sizeof name
, pos
, screen_gamma
, 3);
9016 (void)gamma_test(pm
, colour_type
, bit_depth
, 0/*palette*/, interlace_type
,
9017 file_gamma
, screen_gamma
, 0/*sBIT*/, 1/*threshold test*/, name
,
9018 0 /*no input precision*/,
9019 0 /*no scale16*/, 0 /*no expand16*/, 0 /*no background*/, 0 /*hence*/,
9020 0 /*no background gamma*/);
9024 perform_gamma_threshold_tests(png_modifier
*pm
)
9026 png_byte colour_type
= 0;
9027 png_byte bit_depth
= 0;
9028 unsigned int palette_number
= 0;
9030 /* Don't test more than one instance of each palette - it's pointless, in
9031 * fact this test is somewhat excessive since libpng doesn't make this
9032 * decision based on colour type or bit depth!
9034 while (next_format(&colour_type
, &bit_depth
, &palette_number
, 1/*gamma*/))
9035 if (palette_number
== 0)
9037 double test_gamma
= 1.0;
9038 while (test_gamma
>= .4)
9040 /* There's little point testing the interlacing vs non-interlacing,
9041 * but this can be set from the command line.
9043 gamma_threshold_test(pm
, colour_type
, bit_depth
, pm
->interlace_type
,
9044 test_gamma
, 1/test_gamma
);
9048 /* And a special test for sRGB */
9049 gamma_threshold_test(pm
, colour_type
, bit_depth
, pm
->interlace_type
,
9057 static void gamma_transform_test(png_modifier
*pm
,
9058 PNG_CONST png_byte colour_type
, PNG_CONST png_byte bit_depth
,
9059 PNG_CONST
int palette_number
,
9060 PNG_CONST
int interlace_type
, PNG_CONST
double file_gamma
,
9061 PNG_CONST
double screen_gamma
, PNG_CONST png_byte sbit
,
9062 PNG_CONST
int use_input_precision
, PNG_CONST
int scale16
)
9067 if (sbit
!= bit_depth
&& sbit
!= 0)
9069 pos
= safecat(name
, sizeof name
, pos
, "sbit(");
9070 pos
= safecatn(name
, sizeof name
, pos
, sbit
);
9071 pos
= safecat(name
, sizeof name
, pos
, ") ");
9075 pos
= safecat(name
, sizeof name
, pos
, "gamma ");
9078 pos
= safecat(name
, sizeof name
, pos
, "16to8 ");
9080 pos
= safecatd(name
, sizeof name
, pos
, file_gamma
, 3);
9081 pos
= safecat(name
, sizeof name
, pos
, "->");
9082 pos
= safecatd(name
, sizeof name
, pos
, screen_gamma
, 3);
9084 gamma_test(pm
, colour_type
, bit_depth
, palette_number
, interlace_type
,
9085 file_gamma
, screen_gamma
, sbit
, 0, name
, use_input_precision
,
9086 scale16
, pm
->test_gamma_expand16
, 0 , 0, 0);
9089 static void perform_gamma_transform_tests(png_modifier
*pm
)
9091 png_byte colour_type
= 0;
9092 png_byte bit_depth
= 0;
9093 unsigned int palette_number
= 0;
9095 while (next_format(&colour_type
, &bit_depth
, &palette_number
, 1/*gamma*/))
9099 for (i
=0; i
<pm
->ngamma_tests
; ++i
) for (j
=0; j
<pm
->ngamma_tests
; ++j
)
9102 gamma_transform_test(pm
, colour_type
, bit_depth
, palette_number
,
9103 pm
->interlace_type
, 1/pm
->gammas
[i
], pm
->gammas
[j
], 0/*sBIT*/,
9104 pm
->use_input_precision
, 0 /*do not scale16*/);
9112 static void perform_gamma_sbit_tests(png_modifier
*pm
)
9116 /* The only interesting cases are colour and grayscale, alpha is ignored here
9117 * for overall speed. Only bit depths where sbit is less than the bit depth
9120 for (sbit
=pm
->sbitlow
; sbit
<(1<<READ_BDHI
); ++sbit
)
9122 png_byte colour_type
= 0, bit_depth
= 0;
9123 unsigned int npalette
= 0;
9125 while (next_format(&colour_type
, &bit_depth
, &npalette
, 1/*gamma*/))
9126 if ((colour_type
& PNG_COLOR_MASK_ALPHA
) == 0 &&
9127 ((colour_type
== 3 && sbit
< 8) ||
9128 (colour_type
!= 3 && sbit
< bit_depth
)))
9132 for (i
=0; i
<pm
->ngamma_tests
; ++i
)
9136 for (j
=0; j
<pm
->ngamma_tests
; ++j
) if (i
!= j
)
9138 gamma_transform_test(pm
, colour_type
, bit_depth
, npalette
,
9139 pm
->interlace_type
, 1/pm
->gammas
[i
], pm
->gammas
[j
],
9140 sbit
, pm
->use_input_precision_sbit
, 0 /*scale16*/);
9150 /* Note that this requires a 16 bit source image but produces 8 bit output, so
9151 * we only need the 16bit write support, but the 16 bit images are only
9152 * generated if DO_16BIT is defined.
9155 static void perform_gamma_scale16_tests(png_modifier
*pm
)
9157 # ifndef PNG_MAX_GAMMA_8
9158 # define PNG_MAX_GAMMA_8 11
9160 # define SBIT_16_TO_8 PNG_MAX_GAMMA_8
9161 /* Include the alpha cases here. Note that sbit matches the internal value
9162 * used by the library - otherwise we will get spurious errors from the
9163 * internal sbit style approximation.
9165 * The threshold test is here because otherwise the 16 to 8 conversion will
9166 * proceed *without* gamma correction, and the tests above will fail (but not
9167 * by much) - this could be fixed, it only appears with the -g option.
9170 for (i
=0; i
<pm
->ngamma_tests
; ++i
)
9172 for (j
=0; j
<pm
->ngamma_tests
; ++j
)
9175 fabs(pm
->gammas
[j
]/pm
->gammas
[i
]-1) >= PNG_GAMMA_THRESHOLD
)
9177 gamma_transform_test(pm
, 0, 16, 0, pm
->interlace_type
,
9178 1/pm
->gammas
[i
], pm
->gammas
[j
], SBIT_16_TO_8
,
9179 pm
->use_input_precision_16to8
, 1 /*scale16*/);
9184 gamma_transform_test(pm
, 2, 16, 0, pm
->interlace_type
,
9185 1/pm
->gammas
[i
], pm
->gammas
[j
], SBIT_16_TO_8
,
9186 pm
->use_input_precision_16to8
, 1 /*scale16*/);
9191 gamma_transform_test(pm
, 4, 16, 0, pm
->interlace_type
,
9192 1/pm
->gammas
[i
], pm
->gammas
[j
], SBIT_16_TO_8
,
9193 pm
->use_input_precision_16to8
, 1 /*scale16*/);
9198 gamma_transform_test(pm
, 6, 16, 0, pm
->interlace_type
,
9199 1/pm
->gammas
[i
], pm
->gammas
[j
], SBIT_16_TO_8
,
9200 pm
->use_input_precision_16to8
, 1 /*scale16*/);
9208 #endif /* 16 to 8 bit conversion */
9210 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
9211 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
9212 static void gamma_composition_test(png_modifier
*pm
,
9213 PNG_CONST png_byte colour_type
, PNG_CONST png_byte bit_depth
,
9214 PNG_CONST
int palette_number
,
9215 PNG_CONST
int interlace_type
, PNG_CONST
double file_gamma
,
9216 PNG_CONST
double screen_gamma
,
9217 PNG_CONST
int use_input_precision
, PNG_CONST
int do_background
,
9218 PNG_CONST
int expand_16
)
9221 png_const_charp base
;
9224 png_color_16 background
;
9226 /* Make up a name and get an appropriate background gamma value. */
9227 switch (do_background
)
9231 bg
= 4; /* should not be used */
9233 case PNG_BACKGROUND_GAMMA_SCREEN
:
9234 base
= " bckg(Screen):";
9235 bg
= 1/screen_gamma
;
9237 case PNG_BACKGROUND_GAMMA_FILE
:
9238 base
= " bckg(File):";
9241 case PNG_BACKGROUND_GAMMA_UNIQUE
:
9242 base
= " bckg(Unique):";
9243 /* This tests the handling of a unique value, the math is such that the
9244 * value tends to be <1, but is neither screen nor file (even if they
9247 bg
= (file_gamma
+ screen_gamma
) / 3;
9249 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9250 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_PNG
:
9251 base
= " alpha(PNG)";
9252 bg
= 4; /* should not be used */
9254 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_STANDARD
:
9255 base
= " alpha(Porter-Duff)";
9256 bg
= 4; /* should not be used */
9258 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_OPTIMIZED
:
9259 base
= " alpha(Optimized)";
9260 bg
= 4; /* should not be used */
9262 case ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
:
9263 base
= " alpha(Broken)";
9264 bg
= 4; /* should not be used */
9269 /* Use random background values - the background is always presented in the
9270 * output space (8 or 16 bit components).
9272 if (expand_16
|| bit_depth
== 16)
9274 png_uint_32 r
= random_32();
9276 background
.red
= (png_uint_16
)r
;
9277 background
.green
= (png_uint_16
)(r
>> 16);
9279 background
.blue
= (png_uint_16
)r
;
9280 background
.gray
= (png_uint_16
)(r
>> 16);
9282 /* In earlier libpng versions, those where DIGITIZE is set, any background
9283 * gamma correction in the expand16 case was done using 8-bit gamma
9284 * correction tables, resulting in larger errors. To cope with those
9285 * cases use a 16-bit background value which will handle this gamma
9289 if (expand_16
&& (do_background
== PNG_BACKGROUND_GAMMA_UNIQUE
||
9290 do_background
== PNG_BACKGROUND_GAMMA_FILE
) &&
9291 fabs(bg
*screen_gamma
-1) > PNG_GAMMA_THRESHOLD
)
9293 /* The background values will be looked up in an 8-bit table to do
9294 * the gamma correction, so only select values which are an exact
9295 * match for the 8-bit table entries:
9297 background
.red
= (png_uint_16
)((background
.red
>> 8) * 257);
9298 background
.green
= (png_uint_16
)((background
.green
>> 8) * 257);
9299 background
.blue
= (png_uint_16
)((background
.blue
>> 8) * 257);
9300 background
.gray
= (png_uint_16
)((background
.gray
>> 8) * 257);
9305 else /* 8 bit colors */
9307 png_uint_32 r
= random_32();
9309 background
.red
= (png_byte
)r
;
9310 background
.green
= (png_byte
)(r
>> 8);
9311 background
.blue
= (png_byte
)(r
>> 16);
9312 background
.gray
= (png_byte
)(r
>> 24);
9315 background
.index
= 193; /* rgb(193,193,193) to detect errors */
9316 if (!(colour_type
& PNG_COLOR_MASK_COLOR
))
9318 /* Grayscale input, we do not convert to RGB (TBD), so we must set the
9319 * background to gray - else libpng seems to fail.
9321 background
.red
= background
.green
= background
.blue
= background
.gray
;
9324 pos
= safecat(name
, sizeof name
, pos
, "gamma ");
9325 pos
= safecatd(name
, sizeof name
, pos
, file_gamma
, 3);
9326 pos
= safecat(name
, sizeof name
, pos
, "->");
9327 pos
= safecatd(name
, sizeof name
, pos
, screen_gamma
, 3);
9329 pos
= safecat(name
, sizeof name
, pos
, base
);
9330 if (do_background
< ALPHA_MODE_OFFSET
)
9332 /* Include the background color and gamma in the name: */
9333 pos
= safecat(name
, sizeof name
, pos
, "(");
9334 /* This assumes no expand gray->rgb - the current code won't handle that!
9336 if (colour_type
& PNG_COLOR_MASK_COLOR
)
9338 pos
= safecatn(name
, sizeof name
, pos
, background
.red
);
9339 pos
= safecat(name
, sizeof name
, pos
, ",");
9340 pos
= safecatn(name
, sizeof name
, pos
, background
.green
);
9341 pos
= safecat(name
, sizeof name
, pos
, ",");
9342 pos
= safecatn(name
, sizeof name
, pos
, background
.blue
);
9345 pos
= safecatn(name
, sizeof name
, pos
, background
.gray
);
9346 pos
= safecat(name
, sizeof name
, pos
, ")^");
9347 pos
= safecatd(name
, sizeof name
, pos
, bg
, 3);
9350 gamma_test(pm
, colour_type
, bit_depth
, palette_number
, interlace_type
,
9351 file_gamma
, screen_gamma
, 0/*sBIT*/, 0, name
, use_input_precision
,
9352 0/*strip 16*/, expand_16
, do_background
, &background
, bg
);
9357 perform_gamma_composition_tests(png_modifier
*pm
, int do_background
,
9360 png_byte colour_type
= 0;
9361 png_byte bit_depth
= 0;
9362 unsigned int palette_number
= 0;
9364 /* Skip the non-alpha cases - there is no setting of a transparency colour at
9367 while (next_format(&colour_type
, &bit_depth
, &palette_number
, 1/*gamma*/))
9368 if ((colour_type
& PNG_COLOR_MASK_ALPHA
) != 0)
9372 /* Don't skip the i==j case here - it's relevant. */
9373 for (i
=0; i
<pm
->ngamma_tests
; ++i
) for (j
=0; j
<pm
->ngamma_tests
; ++j
)
9375 gamma_composition_test(pm
, colour_type
, bit_depth
, palette_number
,
9376 pm
->interlace_type
, 1/pm
->gammas
[i
], pm
->gammas
[j
],
9377 pm
->use_input_precision
, do_background
, expand_16
);
9384 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
9387 init_gamma_errors(png_modifier
*pm
)
9389 /* Use -1 to catch tests that were not actually run */
9390 pm
->error_gray_2
= pm
->error_gray_4
= pm
->error_gray_8
= -1.;
9391 pm
->error_color_8
= -1.;
9392 pm
->error_indexed
= -1.;
9393 pm
->error_gray_16
= pm
->error_color_16
= -1.;
9397 print_one(const char *leader
, double err
)
9400 printf(" %s %.5f\n", leader
, err
);
9404 summarize_gamma_errors(png_modifier
*pm
, png_const_charp who
, int low_bit_depth
,
9410 printf("\nGamma correction with %s:\n", who
);
9413 printf("\nBasic gamma correction:\n");
9417 print_one(" 2 bit gray: ", pm
->error_gray_2
);
9418 print_one(" 4 bit gray: ", pm
->error_gray_4
);
9419 print_one(" 8 bit gray: ", pm
->error_gray_8
);
9420 print_one(" 8 bit color:", pm
->error_color_8
);
9422 print_one(" indexed: ", pm
->error_indexed
);
9425 print_one("16 bit gray: ", pm
->error_gray_16
);
9426 print_one("16 bit color:", pm
->error_color_16
);
9432 perform_gamma_test(png_modifier
*pm
, int summary
)
9434 /*TODO: remove this*/
9435 /* Save certain values for the temporary overrides below. */
9436 unsigned int calculations_use_input_precision
=
9437 pm
->calculations_use_input_precision
;
9438 # ifdef PNG_READ_BACKGROUND_SUPPORTED
9439 double maxout8
= pm
->maxout8
;
9442 /* First some arbitrary no-transform tests: */
9443 if (!pm
->this.speed
&& pm
->test_gamma_threshold
)
9445 perform_gamma_threshold_tests(pm
);
9451 /* Now some real transforms. */
9452 if (pm
->test_gamma_transform
)
9457 printf("Gamma correction error summary\n\n");
9458 printf("The printed value is the maximum error in the pixel values\n");
9459 printf("calculated by the libpng gamma correction code. The error\n");
9460 printf("is calculated as the difference between the output pixel\n");
9461 printf("value (always an integer) and the ideal value from the\n");
9462 printf("libpng specification (typically not an integer).\n\n");
9464 printf("Expect this value to be less than .5 for 8 bit formats,\n");
9465 printf("less than 1 for formats with fewer than 8 bits and a small\n");
9466 printf("number (typically less than 5) for the 16 bit formats.\n");
9467 printf("For performance reasons the value for 16 bit formats\n");
9468 printf("increases when the image file includes an sBIT chunk.\n");
9472 init_gamma_errors(pm
);
9473 /*TODO: remove this. Necessary because the current libpng
9474 * implementation works in 8 bits:
9476 if (pm
->test_gamma_expand16
)
9477 pm
->calculations_use_input_precision
= 1;
9478 perform_gamma_transform_tests(pm
);
9479 if (!calculations_use_input_precision
)
9480 pm
->calculations_use_input_precision
= 0;
9483 summarize_gamma_errors(pm
, 0/*who*/, 1/*low bit depth*/, 1/*indexed*/);
9489 /* The sbit tests produce much larger errors: */
9490 if (pm
->test_gamma_sbit
)
9492 init_gamma_errors(pm
);
9493 perform_gamma_sbit_tests(pm
);
9496 summarize_gamma_errors(pm
, "sBIT", pm
->sbitlow
< 8U, 1/*indexed*/);
9502 #ifdef DO_16BIT /* Should be READ_16BIT_SUPPORTED */
9503 if (pm
->test_gamma_scale16
)
9505 /* The 16 to 8 bit strip operations: */
9506 init_gamma_errors(pm
);
9507 perform_gamma_scale16_tests(pm
);
9512 printf("\nGamma correction with 16 to 8 bit reduction:\n");
9513 printf(" 16 bit gray: %.5f\n", pm
->error_gray_16
);
9514 printf(" 16 bit color: %.5f\n", pm
->error_color_16
);
9523 #ifdef PNG_READ_BACKGROUND_SUPPORTED
9524 if (pm
->test_gamma_background
)
9526 init_gamma_errors(pm
);
9528 /*TODO: remove this. Necessary because the current libpng
9529 * implementation works in 8 bits:
9531 if (pm
->test_gamma_expand16
)
9533 pm
->calculations_use_input_precision
= 1;
9534 pm
->maxout8
= .499; /* because the 16 bit background is smashed */
9536 perform_gamma_composition_tests(pm
, PNG_BACKGROUND_GAMMA_UNIQUE
,
9537 pm
->test_gamma_expand16
);
9538 if (!calculations_use_input_precision
)
9539 pm
->calculations_use_input_precision
= 0;
9540 pm
->maxout8
= maxout8
;
9543 summarize_gamma_errors(pm
, "background", 1, 0/*indexed*/);
9550 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
9551 if (pm
->test_gamma_alpha_mode
)
9555 init_gamma_errors(pm
);
9557 /*TODO: remove this. Necessary because the current libpng
9558 * implementation works in 8 bits:
9560 if (pm
->test_gamma_expand16
)
9561 pm
->calculations_use_input_precision
= 1;
9562 for (do_background
= ALPHA_MODE_OFFSET
+ PNG_ALPHA_STANDARD
;
9563 do_background
<= ALPHA_MODE_OFFSET
+ PNG_ALPHA_BROKEN
&& !fail(pm
);
9565 perform_gamma_composition_tests(pm
, do_background
,
9566 pm
->test_gamma_expand16
);
9567 if (!calculations_use_input_precision
)
9568 pm
->calculations_use_input_precision
= 0;
9571 summarize_gamma_errors(pm
, "alpha mode", 1, 0/*indexed*/);
9578 #endif /* PNG_READ_GAMMA_SUPPORTED */
9579 #endif /* PNG_READ_SUPPORTED */
9581 /* INTERLACE MACRO VALIDATION */
9582 /* This is copied verbatim from the specification, it is simply the pass
9583 * number in which each pixel in each 8x8 tile appears. The array must
9584 * be indexed adam7[y][x] and notice that the pass numbers are based at
9585 * 1, not 0 - the base libpng uses.
9588 png_byte adam7
[8][8] =
9590 { 1,6,4,6,2,6,4,6 },
9591 { 7,7,7,7,7,7,7,7 },
9592 { 5,6,5,6,5,6,5,6 },
9593 { 7,7,7,7,7,7,7,7 },
9594 { 3,6,4,6,3,6,4,6 },
9595 { 7,7,7,7,7,7,7,7 },
9596 { 5,6,5,6,5,6,5,6 },
9600 /* This routine validates all the interlace support macros in png.h for
9601 * a variety of valid PNG widths and heights. It uses a number of similarly
9602 * named internal routines that feed off the above array.
9605 png_pass_start_row(int pass
)
9609 for (y
=0; y
<8; ++y
) for (x
=0; x
<8; ++x
) if (adam7
[y
][x
] == pass
)
9615 png_pass_start_col(int pass
)
9619 for (x
=0; x
<8; ++x
) for (y
=0; y
<8; ++y
) if (adam7
[y
][x
] == pass
)
9625 png_pass_row_shift(int pass
)
9627 int x
, y
, base
=(-1), inc
=8;
9629 for (y
=0; y
<8; ++y
) for (x
=0; x
<8; ++x
) if (adam7
[y
][x
] == pass
)
9635 else if (inc
== y
-base
)
9638 inc
= y
-base
, base
=y
;
9639 else if (inc
!= y
-base
)
9640 return 0xff; /* error - more than one 'inc' value! */
9643 if (base
== (-1)) return 0xfe; /* error - no row in pass! */
9645 /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9654 /* error - unrecognized 'inc' */
9655 return (inc
<< 8) + 0xfd;
9659 png_pass_col_shift(int pass
)
9661 int x
, y
, base
=(-1), inc
=8;
9663 for (x
=0; x
<8; ++x
) for (y
=0; y
<8; ++y
) if (adam7
[y
][x
] == pass
)
9669 else if (inc
== x
-base
)
9672 inc
= x
-base
, base
=x
;
9673 else if (inc
!= x
-base
)
9674 return 0xff; /* error - more than one 'inc' value! */
9677 if (base
== (-1)) return 0xfe; /* error - no row in pass! */
9679 /* The shift is always 1, 2 or 3 - no pass has all the rows! */
9682 case 1: return 0; /* pass 7 has all the columns */
9689 /* error - unrecognized 'inc' */
9690 return (inc
<< 8) + 0xfd;
9694 png_row_from_pass_row(png_uint_32 yIn
, int pass
)
9696 /* By examination of the array: */
9699 case 0: return yIn
* 8;
9700 case 1: return yIn
* 8;
9701 case 2: return yIn
* 8 + 4;
9702 case 3: return yIn
* 4;
9703 case 4: return yIn
* 4 + 2;
9704 case 5: return yIn
* 2;
9705 case 6: return yIn
* 2 + 1;
9709 return 0xff; /* bad pass number */
9713 png_col_from_pass_col(png_uint_32 xIn
, int pass
)
9715 /* By examination of the array: */
9718 case 0: return xIn
* 8;
9719 case 1: return xIn
* 8 + 4;
9720 case 2: return xIn
* 4;
9721 case 3: return xIn
* 4 + 2;
9722 case 4: return xIn
* 2;
9723 case 5: return xIn
* 2 + 1;
9728 return 0xff; /* bad pass number */
9732 png_row_in_interlace_pass(png_uint_32 y
, int pass
)
9734 /* Is row 'y' in pass 'pass'? */
9738 for (x
=0; x
<8; ++x
) if (adam7
[y
][x
] == pass
)
9745 png_col_in_interlace_pass(png_uint_32 x
, int pass
)
9747 /* Is column 'x' in pass 'pass'? */
9751 for (y
=0; y
<8; ++y
) if (adam7
[y
][x
] == pass
)
9758 png_pass_rows(png_uint_32 height
, int pass
)
9760 png_uint_32 tiles
= height
>>3;
9761 png_uint_32 rows
= 0;
9766 for (y
=0; y
<8; ++y
) for (x
=0; x
<8; ++x
) if (adam7
[y
][x
] == pass
)
9769 if (y
< height
) ++rows
;
9770 break; /* i.e. break the 'x', column, loop. */
9777 png_pass_cols(png_uint_32 width
, int pass
)
9779 png_uint_32 tiles
= width
>>3;
9780 png_uint_32 cols
= 0;
9785 for (x
=0; x
<8; ++x
) for (y
=0; y
<8; ++y
) if (adam7
[y
][x
] == pass
)
9788 if (x
< width
) ++cols
;
9789 break; /* i.e. break the 'y', row, loop. */
9796 perform_interlace_macro_validation(void)
9798 /* The macros to validate, first those that depend only on pass:
9800 * PNG_PASS_START_ROW(pass)
9801 * PNG_PASS_START_COL(pass)
9802 * PNG_PASS_ROW_SHIFT(pass)
9803 * PNG_PASS_COL_SHIFT(pass)
9807 for (pass
=0; pass
<7; ++pass
)
9809 png_uint_32 m
, f
, v
;
9811 m
= PNG_PASS_START_ROW(pass
);
9812 f
= png_pass_start_row(pass
);
9815 fprintf(stderr
, "PNG_PASS_START_ROW(%d) = %u != %x\n", pass
, m
, f
);
9819 m
= PNG_PASS_START_COL(pass
);
9820 f
= png_pass_start_col(pass
);
9823 fprintf(stderr
, "PNG_PASS_START_COL(%d) = %u != %x\n", pass
, m
, f
);
9827 m
= PNG_PASS_ROW_SHIFT(pass
);
9828 f
= png_pass_row_shift(pass
);
9831 fprintf(stderr
, "PNG_PASS_ROW_SHIFT(%d) = %u != %x\n", pass
, m
, f
);
9835 m
= PNG_PASS_COL_SHIFT(pass
);
9836 f
= png_pass_col_shift(pass
);
9839 fprintf(stderr
, "PNG_PASS_COL_SHIFT(%d) = %u != %x\n", pass
, m
, f
);
9843 /* Macros that depend on the image or sub-image height too:
9845 * PNG_PASS_ROWS(height, pass)
9846 * PNG_PASS_COLS(width, pass)
9847 * PNG_ROW_FROM_PASS_ROW(yIn, pass)
9848 * PNG_COL_FROM_PASS_COL(xIn, pass)
9849 * PNG_ROW_IN_INTERLACE_PASS(y, pass)
9850 * PNG_COL_IN_INTERLACE_PASS(x, pass)
9854 /* First the base 0 stuff: */
9855 m
= PNG_ROW_FROM_PASS_ROW(v
, pass
);
9856 f
= png_row_from_pass_row(v
, pass
);
9859 fprintf(stderr
, "PNG_ROW_FROM_PASS_ROW(%u, %d) = %u != %x\n",
9864 m
= PNG_COL_FROM_PASS_COL(v
, pass
);
9865 f
= png_col_from_pass_col(v
, pass
);
9868 fprintf(stderr
, "PNG_COL_FROM_PASS_COL(%u, %d) = %u != %x\n",
9873 m
= PNG_ROW_IN_INTERLACE_PASS(v
, pass
);
9874 f
= png_row_in_interlace_pass(v
, pass
);
9877 fprintf(stderr
, "PNG_ROW_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9882 m
= PNG_COL_IN_INTERLACE_PASS(v
, pass
);
9883 f
= png_col_in_interlace_pass(v
, pass
);
9886 fprintf(stderr
, "PNG_COL_IN_INTERLACE_PASS(%u, %d) = %u != %x\n",
9891 /* Then the base 1 stuff: */
9893 m
= PNG_PASS_ROWS(v
, pass
);
9894 f
= png_pass_rows(v
, pass
);
9897 fprintf(stderr
, "PNG_PASS_ROWS(%u, %d) = %u != %x\n",
9902 m
= PNG_PASS_COLS(v
, pass
);
9903 f
= png_pass_cols(v
, pass
);
9906 fprintf(stderr
, "PNG_PASS_COLS(%u, %d) = %u != %x\n",
9911 /* Move to the next v - the stepping algorithm starts skipping
9912 * values above 1024.
9916 if (v
== PNG_UINT_31_MAX
)
9920 if (v
>= PNG_UINT_31_MAX
)
9921 v
= PNG_UINT_31_MAX
-1;
9927 /* Test color encodings. These values are back-calculated from the published
9928 * chromaticities. The values are accurate to about 14 decimal places; 15 are
9929 * given. These values are much more accurate than the ones given in the spec,
9930 * which typically don't exceed 4 decimal places. This allows testing of the
9931 * libpng code to its theoretical accuracy of 4 decimal places. (If pngvalid
9932 * used the published errors the 'slack' permitted would have to be +/-.5E-4 or
9935 * The png_modifier code assumes that encodings[0] is sRGB and treats it
9936 * specially: do not change the first entry in this list!
9938 static PNG_CONST color_encoding test_encodings
[] =
9940 /* sRGB: must be first in this list! */
9942 /*red: */ { 0.412390799265959, 0.212639005871510, 0.019330818715592 },
9943 /*green:*/ { 0.357584339383878, 0.715168678767756, 0.119194779794626 },
9944 /*blue: */ { 0.180480788401834, 0.072192315360734, 0.950532152249660} },
9945 /* Kodak ProPhoto (wide gamut) */
9946 /*gamma:*/ { 1/1.6 /*approximate: uses 1.8 power law compared to sRGB 2.4*/,
9947 /*red: */ { 0.797760489672303, 0.288071128229293, 0.000000000000000 },
9948 /*green:*/ { 0.135185837175740, 0.711843217810102, 0.000000000000000 },
9949 /*blue: */ { 0.031349349581525, 0.000085653960605, 0.825104602510460} },
9950 /* Adobe RGB (1998) */
9951 /*gamma:*/ { 1/(2+51./256),
9952 /*red: */ { 0.576669042910131, 0.297344975250536, 0.027031361386412 },
9953 /*green:*/ { 0.185558237906546, 0.627363566255466, 0.070688852535827 },
9954 /*blue: */ { 0.188228646234995, 0.075291458493998, 0.991337536837639} },
9955 /* Adobe Wide Gamut RGB */
9956 /*gamma:*/ { 1/(2+51./256),
9957 /*red: */ { 0.716500716779386, 0.258728243040113, 0.000000000000000 },
9958 /*green:*/ { 0.101020574397477, 0.724682314948566, 0.051211818965388 },
9959 /*blue: */ { 0.146774385252705, 0.016589442011321, 0.773892783545073} },
9964 * This attempts to trap signals and escape without crashing. It needs a
9965 * context pointer so that it can throw an exception (call longjmp) to recover
9966 * from the condition; this is handled by making the png_modifier used by 'main'
9967 * into a global variable.
9969 static png_modifier pm
;
9971 static void signal_handler(int signum
)
9977 pos
= safecat(msg
, sizeof msg
, pos
, "caught signal: ");
9982 pos
= safecat(msg
, sizeof msg
, pos
, "abort");
9986 pos
= safecat(msg
, sizeof msg
, pos
, "floating point exception");
9990 pos
= safecat(msg
, sizeof msg
, pos
, "illegal instruction");
9994 pos
= safecat(msg
, sizeof msg
, pos
, "interrupt");
9998 pos
= safecat(msg
, sizeof msg
, pos
, "invalid memory access");
10002 pos
= safecat(msg
, sizeof msg
, pos
, "termination request");
10006 pos
= safecat(msg
, sizeof msg
, pos
, "unknown ");
10007 pos
= safecatn(msg
, sizeof msg
, pos
, signum
);
10011 store_log(&pm
.this, NULL
/*png_structp*/, msg
, 1/*error*/);
10013 /* And finally throw an exception so we can keep going, unless this is
10014 * SIGTERM in which case stop now.
10016 if (signum
!= SIGTERM
)
10018 struct exception_context
*the_exception_context
=
10019 &pm
.this.exception_context
;
10029 int main(int argc
, char **argv
)
10031 volatile int summary
= 1; /* Print the error summary at the end */
10032 volatile int memstats
= 0; /* Print memory statistics at the end */
10034 /* Create the given output file on success: */
10035 PNG_CONST
char *volatile touch
= NULL
;
10037 /* This is an array of standard gamma values (believe it or not I've seen
10038 * every one of these mentioned somewhere.)
10040 * In the following list the most useful values are first!
10043 gammas
[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9};
10045 /* This records the command and arguments: */
10047 char command
[1024];
10049 anon_context(&pm
.this);
10051 /* Add appropriate signal handlers, just the ANSI specified ones: */
10052 signal(SIGABRT
, signal_handler
);
10053 signal(SIGFPE
, signal_handler
);
10054 signal(SIGILL
, signal_handler
);
10055 signal(SIGINT
, signal_handler
);
10056 signal(SIGSEGV
, signal_handler
);
10057 signal(SIGTERM
, signal_handler
);
10059 #ifdef HAVE_FEENABLEEXCEPT
10060 /* Only required to enable FP exceptions on platforms where they start off
10061 * disabled; this is not necessary but if it is not done pngvalid will likely
10062 * end up ignoring FP conditions that other platforms fault.
10064 feenableexcept(FE_DIVBYZERO
| FE_INVALID
| FE_OVERFLOW
);
10067 modifier_init(&pm
);
10069 /* Preallocate the image buffer, because we know how big it needs to be,
10070 * note that, for testing purposes, it is deliberately mis-aligned by tag
10071 * bytes either side. All rows have an additional five bytes of padding for
10072 * overwrite checking.
10074 store_ensure_image(&pm
.this, NULL
, 2, TRANSFORM_ROWMAX
, TRANSFORM_HEIGHTMAX
);
10076 /* Don't give argv[0], it's normally some horrible libtool string: */
10077 cp
= safecat(command
, sizeof command
, cp
, "pngvalid");
10079 /* Default to error on warning: */
10080 pm
.this.treat_warnings_as_errors
= 1;
10082 /* Default assume_16_bit_calculations appropriately; this tells the checking
10083 * code that 16-bit arithmetic is used for 8-bit samples when it would make a
10086 pm
.assume_16_bit_calculations
= PNG_LIBPNG_VER
>= 10700;
10088 /* Currently 16 bit expansion happens at the end of the pipeline, so the
10089 * calculations are done in the input bit depth not the output.
10093 pm
.calculations_use_input_precision
= 1U;
10095 /* Store the test gammas */
10096 pm
.gammas
= gammas
;
10097 pm
.ngammas
= (sizeof gammas
) / (sizeof gammas
[0]);
10098 pm
.ngamma_tests
= 0; /* default to off */
10100 /* And the test encodings */
10101 pm
.encodings
= test_encodings
;
10102 pm
.nencodings
= (sizeof test_encodings
) / (sizeof test_encodings
[0]);
10104 pm
.sbitlow
= 8U; /* because libpng doesn't do sBIT below 8! */
10106 /* The following allows results to pass if they correspond to anything in the
10107 * transformed range [input-.5,input+.5]; this is is required because of the
10108 * way libpng treates the 16_TO_8 flag when building the gamma tables in
10109 * releases up to 1.6.0.
10111 * TODO: review this
10113 pm
.use_input_precision_16to8
= 1U;
10114 pm
.use_input_precision_sbit
= 1U; /* because libpng now rounds sBIT */
10116 /* Some default values (set the behavior for 'make check' here).
10117 * These values simply control the maximum error permitted in the gamma
10118 * transformations. The practial limits for human perception are described
10119 * below (the setting for maxpc16), however for 8 bit encodings it isn't
10120 * possible to meet the accepted capabilities of human vision - i.e. 8 bit
10121 * images can never be good enough, regardless of encoding.
10123 pm
.maxout8
= .1; /* Arithmetic error in *encoded* value */
10124 pm
.maxabs8
= .00005; /* 1/20000 */
10125 pm
.maxcalc8
= 1./255; /* +/-1 in 8 bits for compose errors */
10126 pm
.maxpc8
= .499; /* I.e., .499% fractional error */
10127 pm
.maxout16
= .499; /* Error in *encoded* value */
10128 pm
.maxabs16
= .00005;/* 1/20000 */
10129 pm
.maxcalc16
=1./65535;/* +/-1 in 16 bits for compose errors */
10130 pm
.maxcalcG
= 1./((1<<PNG_MAX_GAMMA_8
)-1);
10132 /* NOTE: this is a reasonable perceptual limit. We assume that humans can
10133 * perceive light level differences of 1% over a 100:1 range, so we need to
10134 * maintain 1 in 10000 accuracy (in linear light space), which is what the
10135 * following guarantees. It also allows significantly higher errors at
10136 * higher 16 bit values, which is important for performance. The actual
10137 * maximum 16 bit error is about +/-1.9 in the fixed point implementation but
10138 * this is only allowed for values >38149 by the following:
10140 pm
.maxpc16
= .005; /* I.e., 1/200% - 1/20000 */
10142 /* Now parse the command line options. */
10143 while (--argc
>= 1)
10145 int catmore
= 0; /* Set if the argument has an argument. */
10147 /* Record each argument for posterity: */
10148 cp
= safecat(command
, sizeof command
, cp
, " ");
10149 cp
= safecat(command
, sizeof command
, cp
, *++argv
);
10151 if (strcmp(*argv
, "-v") == 0)
10152 pm
.this.verbose
= 1;
10154 else if (strcmp(*argv
, "-l") == 0)
10157 else if (strcmp(*argv
, "-q") == 0)
10158 summary
= pm
.this.verbose
= pm
.log
= 0;
10160 else if (strcmp(*argv
, "-w") == 0)
10161 pm
.this.treat_warnings_as_errors
= 0;
10163 else if (strcmp(*argv
, "--speed") == 0)
10164 pm
.this.speed
= 1, pm
.ngamma_tests
= pm
.ngammas
, pm
.test_standard
= 0,
10167 else if (strcmp(*argv
, "--memory") == 0)
10170 else if (strcmp(*argv
, "--size") == 0)
10173 else if (strcmp(*argv
, "--nosize") == 0)
10176 else if (strcmp(*argv
, "--standard") == 0)
10177 pm
.test_standard
= 1;
10179 else if (strcmp(*argv
, "--nostandard") == 0)
10180 pm
.test_standard
= 0;
10182 else if (strcmp(*argv
, "--transform") == 0)
10183 pm
.test_transform
= 1;
10185 else if (strcmp(*argv
, "--notransform") == 0)
10186 pm
.test_transform
= 0;
10188 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10189 else if (strncmp(*argv
, "--transform-disable=",
10190 sizeof "--transform-disable") == 0)
10192 pm
.test_transform
= 1;
10193 transform_disable(*argv
+ sizeof "--transform-disable");
10196 else if (strncmp(*argv
, "--transform-enable=",
10197 sizeof "--transform-enable") == 0)
10199 pm
.test_transform
= 1;
10200 transform_enable(*argv
+ sizeof "--transform-enable");
10202 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10204 else if (strcmp(*argv
, "--gamma") == 0)
10206 /* Just do two gamma tests here (2.2 and linear) for speed: */
10207 pm
.ngamma_tests
= 2U;
10208 pm
.test_gamma_threshold
= 1;
10209 pm
.test_gamma_transform
= 1;
10210 pm
.test_gamma_sbit
= 1;
10211 pm
.test_gamma_scale16
= 1;
10212 pm
.test_gamma_background
= 1;
10213 pm
.test_gamma_alpha_mode
= 1;
10216 else if (strcmp(*argv
, "--nogamma") == 0)
10217 pm
.ngamma_tests
= 0;
10219 else if (strcmp(*argv
, "--gamma-threshold") == 0)
10220 pm
.ngamma_tests
= 2U, pm
.test_gamma_threshold
= 1;
10222 else if (strcmp(*argv
, "--nogamma-threshold") == 0)
10223 pm
.test_gamma_threshold
= 0;
10225 else if (strcmp(*argv
, "--gamma-transform") == 0)
10226 pm
.ngamma_tests
= 2U, pm
.test_gamma_transform
= 1;
10228 else if (strcmp(*argv
, "--nogamma-transform") == 0)
10229 pm
.test_gamma_transform
= 0;
10231 else if (strcmp(*argv
, "--gamma-sbit") == 0)
10232 pm
.ngamma_tests
= 2U, pm
.test_gamma_sbit
= 1;
10234 else if (strcmp(*argv
, "--nogamma-sbit") == 0)
10235 pm
.test_gamma_sbit
= 0;
10237 else if (strcmp(*argv
, "--gamma-16-to-8") == 0)
10238 pm
.ngamma_tests
= 2U, pm
.test_gamma_scale16
= 1;
10240 else if (strcmp(*argv
, "--nogamma-16-to-8") == 0)
10241 pm
.test_gamma_scale16
= 0;
10243 else if (strcmp(*argv
, "--gamma-background") == 0)
10244 pm
.ngamma_tests
= 2U, pm
.test_gamma_background
= 1;
10246 else if (strcmp(*argv
, "--nogamma-background") == 0)
10247 pm
.test_gamma_background
= 0;
10249 else if (strcmp(*argv
, "--gamma-alpha-mode") == 0)
10250 pm
.ngamma_tests
= 2U, pm
.test_gamma_alpha_mode
= 1;
10252 else if (strcmp(*argv
, "--nogamma-alpha-mode") == 0)
10253 pm
.test_gamma_alpha_mode
= 0;
10255 else if (strcmp(*argv
, "--expand16") == 0)
10256 pm
.test_gamma_expand16
= 1;
10258 else if (strcmp(*argv
, "--noexpand16") == 0)
10259 pm
.test_gamma_expand16
= 0;
10261 else if (strcmp(*argv
, "--more-gammas") == 0)
10262 pm
.ngamma_tests
= 3U;
10264 else if (strcmp(*argv
, "--all-gammas") == 0)
10265 pm
.ngamma_tests
= pm
.ngammas
;
10267 else if (strcmp(*argv
, "--progressive-read") == 0)
10268 pm
.this.progressive
= 1;
10270 else if (strcmp(*argv
, "--use-update-info") == 0)
10271 ++pm
.use_update_info
; /* Can call multiple times */
10273 else if (strcmp(*argv
, "--interlace") == 0)
10275 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
10276 pm
.interlace_type
= PNG_INTERLACE_ADAM7
;
10278 fprintf(stderr
, "pngvalid: no write interlace support\n");
10283 else if (strcmp(*argv
, "--use-input-precision") == 0)
10284 pm
.use_input_precision
= 1U;
10286 else if (strcmp(*argv
, "--use-calculation-precision") == 0)
10287 pm
.use_input_precision
= 0;
10289 else if (strcmp(*argv
, "--calculations-use-input-precision") == 0)
10290 pm
.calculations_use_input_precision
= 1U;
10292 else if (strcmp(*argv
, "--assume-16-bit-calculations") == 0)
10293 pm
.assume_16_bit_calculations
= 1U;
10295 else if (strcmp(*argv
, "--calculations-follow-bit-depth") == 0)
10296 pm
.calculations_use_input_precision
=
10297 pm
.assume_16_bit_calculations
= 0;
10299 else if (strcmp(*argv
, "--exhaustive") == 0)
10300 pm
.test_exhaustive
= 1;
10302 else if (argc
> 1 && strcmp(*argv
, "--sbitlow") == 0)
10303 --argc
, pm
.sbitlow
= (png_byte
)atoi(*++argv
), catmore
= 1;
10305 else if (argc
> 1 && strcmp(*argv
, "--touch") == 0)
10306 --argc
, touch
= *++argv
, catmore
= 1;
10308 else if (argc
> 1 && strncmp(*argv
, "--max", 5) == 0)
10312 if (strcmp(5+*argv
, "abs8") == 0)
10313 pm
.maxabs8
= atof(*++argv
);
10315 else if (strcmp(5+*argv
, "abs16") == 0)
10316 pm
.maxabs16
= atof(*++argv
);
10318 else if (strcmp(5+*argv
, "calc8") == 0)
10319 pm
.maxcalc8
= atof(*++argv
);
10321 else if (strcmp(5+*argv
, "calc16") == 0)
10322 pm
.maxcalc16
= atof(*++argv
);
10324 else if (strcmp(5+*argv
, "out8") == 0)
10325 pm
.maxout8
= atof(*++argv
);
10327 else if (strcmp(5+*argv
, "out16") == 0)
10328 pm
.maxout16
= atof(*++argv
);
10330 else if (strcmp(5+*argv
, "pc8") == 0)
10331 pm
.maxpc8
= atof(*++argv
);
10333 else if (strcmp(5+*argv
, "pc16") == 0)
10334 pm
.maxpc16
= atof(*++argv
);
10338 fprintf(stderr
, "pngvalid: %s: unknown 'max' option\n", *argv
);
10345 else if (strcmp(*argv
, "--log8") == 0)
10346 --argc
, pm
.log8
= atof(*++argv
), catmore
= 1;
10348 else if (strcmp(*argv
, "--log16") == 0)
10349 --argc
, pm
.log16
= atof(*++argv
), catmore
= 1;
10351 #ifdef PNG_SET_OPTION_SUPPORTED
10352 else if (strncmp(*argv
, "--option=", 9) == 0)
10354 /* Syntax of the argument is <option>:{on|off} */
10355 const char *arg
= 9+*argv
;
10356 unsigned char option
=0, setting
=0;
10358 #ifdef PNG_ARM_NEON_API_SUPPORTED
10359 if (strncmp(arg
, "arm-neon:", 9) == 0)
10360 option
= PNG_ARM_NEON
, arg
+= 9;
10364 #ifdef PNG_MAXIMUM_INFLATE_WINDOW
10365 if (strncmp(arg
, "max-inflate-window:", 19) == 0)
10366 option
= PNG_MAXIMUM_INFLATE_WINDOW
, arg
+= 19;
10371 fprintf(stderr
, "pngvalid: %s: %s: unknown option\n", *argv
, arg
);
10375 if (strcmp(arg
, "off") == 0)
10376 setting
= PNG_OPTION_OFF
;
10378 else if (strcmp(arg
, "on") == 0)
10379 setting
= PNG_OPTION_ON
;
10384 "pngvalid: %s: %s: unknown setting (use 'on' or 'off')\n",
10389 pm
.this.options
[pm
.this.noptions
].option
= option
;
10390 pm
.this.options
[pm
.this.noptions
++].setting
= setting
;
10392 #endif /* PNG_SET_OPTION_SUPPORTED */
10396 fprintf(stderr
, "pngvalid: %s: unknown argument\n", *argv
);
10400 if (catmore
) /* consumed an extra *argv */
10402 cp
= safecat(command
, sizeof command
, cp
, " ");
10403 cp
= safecat(command
, sizeof command
, cp
, *argv
);
10407 /* If pngvalid is run with no arguments default to a reasonable set of the
10410 if (pm
.test_standard
== 0 && pm
.test_size
== 0 && pm
.test_transform
== 0 &&
10411 pm
.ngamma_tests
== 0)
10413 /* Make this do all the tests done in the test shell scripts with the same
10414 * parameters, where possible. The limitation is that all the progressive
10415 * read and interlace stuff has to be done in separate runs, so only the
10416 * basic 'standard' and 'size' tests are done.
10418 pm
.test_standard
= 1;
10420 pm
.test_transform
= 1;
10421 pm
.ngamma_tests
= 2U;
10424 if (pm
.ngamma_tests
> 0 &&
10425 pm
.test_gamma_threshold
== 0 && pm
.test_gamma_transform
== 0 &&
10426 pm
.test_gamma_sbit
== 0 && pm
.test_gamma_scale16
== 0 &&
10427 pm
.test_gamma_background
== 0 && pm
.test_gamma_alpha_mode
== 0)
10429 pm
.test_gamma_threshold
= 1;
10430 pm
.test_gamma_transform
= 1;
10431 pm
.test_gamma_sbit
= 1;
10432 pm
.test_gamma_scale16
= 1;
10433 pm
.test_gamma_background
= 1;
10434 pm
.test_gamma_alpha_mode
= 1;
10437 else if (pm
.ngamma_tests
== 0)
10439 /* Nothing to test so turn everything off: */
10440 pm
.test_gamma_threshold
= 0;
10441 pm
.test_gamma_transform
= 0;
10442 pm
.test_gamma_sbit
= 0;
10443 pm
.test_gamma_scale16
= 0;
10444 pm
.test_gamma_background
= 0;
10445 pm
.test_gamma_alpha_mode
= 0;
10450 /* Make useful base images */
10451 make_transform_images(&pm
.this);
10453 /* Perform the standard and gamma tests. */
10454 if (pm
.test_standard
)
10456 perform_interlace_macro_validation();
10457 perform_formatting_test(&pm
.this);
10458 # ifdef PNG_READ_SUPPORTED
10459 perform_standard_test(&pm
);
10461 perform_error_test(&pm
);
10464 /* Various oddly sized images: */
10467 make_size_images(&pm
.this);
10468 # ifdef PNG_READ_SUPPORTED
10469 perform_size_test(&pm
);
10473 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
10474 /* Combinatorial transforms: */
10475 if (pm
.test_transform
)
10476 perform_transform_test(&pm
);
10477 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
10479 #ifdef PNG_READ_GAMMA_SUPPORTED
10480 if (pm
.ngamma_tests
> 0)
10481 perform_gamma_test(&pm
, summary
);
10487 fprintf(stderr
, "pngvalid: test aborted (probably failed in cleanup)\n");
10488 if (!pm
.this.verbose
)
10490 if (pm
.this.error
[0] != 0)
10491 fprintf(stderr
, "pngvalid: first error: %s\n", pm
.this.error
);
10493 fprintf(stderr
, "pngvalid: run with -v to see what happened\n");
10500 printf("%s: %s (%s point arithmetic)\n",
10501 (pm
.this.nerrors
|| (pm
.this.treat_warnings_as_errors
&&
10502 pm
.this.nwarnings
)) ?
"FAIL" : "PASS",
10504 #if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500
10514 printf("Allocated memory statistics (in bytes):\n"
10515 "\tread %lu maximum single, %lu peak, %lu total\n"
10516 "\twrite %lu maximum single, %lu peak, %lu total\n",
10517 (unsigned long)pm
.this.read_memory_pool
.max_max
,
10518 (unsigned long)pm
.this.read_memory_pool
.max_limit
,
10519 (unsigned long)pm
.this.read_memory_pool
.max_total
,
10520 (unsigned long)pm
.this.write_memory_pool
.max_max
,
10521 (unsigned long)pm
.this.write_memory_pool
.max_limit
,
10522 (unsigned long)pm
.this.write_memory_pool
.max_total
);
10525 /* Do this here to provoke memory corruption errors in memory not directly
10526 * allocated by libpng - not a complete test, but better than nothing.
10528 store_delete(&pm
.this);
10530 /* Error exit if there are any errors, and maybe if there are any
10533 if (pm
.this.nerrors
|| (pm
.this.treat_warnings_as_errors
&&
10534 pm
.this.nwarnings
))
10536 if (!pm
.this.verbose
)
10537 fprintf(stderr
, "pngvalid: %s\n", pm
.this.error
);
10539 fprintf(stderr
, "pngvalid: %d errors, %d warnings\n", pm
.this.nerrors
,
10540 pm
.this.nwarnings
);
10545 /* Success case. */
10548 FILE *fsuccess
= fopen(touch
, "wt");
10550 if (fsuccess
!= NULL
)
10553 fprintf(fsuccess
, "PNG validation succeeded\n");
10555 error
= ferror(fsuccess
);
10557 if (fclose(fsuccess
) || error
)
10559 fprintf(stderr
, "%s: write failed\n", touch
);
10566 fprintf(stderr
, "%s: open failed\n", touch
);
10571 /* This is required because some very minimal configurations do not use it:
10576 #else /* write or low level APIs not supported */
10580 "pngvalid: no low level write support in libpng, all tests skipped\n");
10581 /* So the test is skipped: */