blob: 510f99f588f2daa3f9ebd438af26dee309edf074 [file] [log] [blame]
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001
2/* pngvalid.c - validate libpng by constructing then reading png files.
3 *
Glenn Randers-Pehrsonf0eda4e2010-10-15 15:01:57 -05004 * Last changed in libpng 1.5.0 [(PENDING RELEASE)]
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05005 * Copyright (c) 2010 Glenn Randers-Pehrson
6 * Written by John C. Bowler
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05007 *
8 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h
11 *
12 * NOTES:
13 * This is a C program that is intended to be linked against libpng. It
14 * generates bitmaps internally, stores them as PNG files (using the
15 * sequential write code) then reads them back (using the sequential
16 * read code) and validates that the result has the correct data.
17 *
18 * The program can be modified and extended to test the correctness of
19 * transformations performed by libpng.
20 */
21
22#include "png.h"
23#include "zlib.h" /* For crc32 */
24
25#include <stdlib.h> /* For malloc */
26#include <string.h> /* For memcpy, memset */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050027#include <math.h> /* For floor */
28
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050029/* Unused formal parameter errors are removed using the following macro which is
30 * expected to have no bad effects on performance.
31 */
32#ifndef UNUSED
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050033# define UNUSED(param) param = param;
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050034#endif
35
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050036/***************************** EXCEPTION HANDLING *****************************/
37#include "contrib/visupng/cexcept.h"
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -050038struct png_store;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050039define_exception_type(struct png_store*);
40
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -050041/* The following are macros to reduce typing everywhere where the well known
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050042 * name 'the_exception_context' must be defined.
43 */
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -050044#define anon_context(ps) struct exception_context *the_exception_context = \
45 &(ps)->exception_context
46#define context(ps,fault) anon_context(ps); png_store *fault
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050047
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050048/******************************* ERROR UTILITIES ******************************/
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050049static size_t safecat(char *buffer, size_t bufsize, size_t pos,
50 PNG_CONST char *cat)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050051{
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -050052 while (pos < bufsize && cat != NULL && *cat != 0)
53 buffer[pos++] = *cat++;
54
55 if (pos >= bufsize)
56 pos = bufsize-1;
57
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050058 buffer[pos] = 0;
59 return pos;
60}
61
62static size_t safecatn(char *buffer, size_t bufsize, size_t pos, int n)
63{
64 char number[64];
65 sprintf(number, "%d", n);
66 return safecat(buffer, bufsize, pos, number);
67}
68
69static size_t safecatd(char *buffer, size_t bufsize, size_t pos, double d,
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -050070 int precision)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050071{
72 char number[64];
73 sprintf(number, "%.*f", precision, d);
74 return safecat(buffer, bufsize, pos, number);
75}
76
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050077static PNG_CONST char invalid[] = "invalid";
78static PNG_CONST char sep[] = ": ";
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050079
80/* NOTE: this is indexed by ln2(bit_depth)! */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050081static PNG_CONST char *bit_depths[8] =
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050082{
83 "1", "2", "4", "8", "16", invalid, invalid, invalid
84};
85
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050086static PNG_CONST char *colour_types[8] =
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050087{
88 "greyscale", invalid, "truecolour", "indexed-colour",
89 "greyscale with alpha", invalid, "truecolour with alpha", invalid
90};
91
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -050092/* To get log-bit-depth from bit depth, returns 0 to 7 (7 on error). */
93static unsigned int
94log2depth(png_byte bit_depth)
95{
96 switch (bit_depth)
97 {
98 case 1:
99 return 0;
100
101 case 2:
102 return 1;
103
104 case 4:
105 return 2;
106
107 case 8:
108 return 3;
109
110 case 16:
111 return 4;
112
113 default:
114 return 7;
115 }
116}
117
118/* A numeric ID based on PNG file characteristics: */
119#define FILEID(col, depth, interlace) \
120 ((png_uint_32)((col) + ((depth)<<3)) + ((interlace)<<8))
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500121#define COL_FROM_ID(id) ((png_byte)((id)& 0x7U))
122#define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU))
123#define INTERLACE_FROM_ID(id) ((int)(((id) >> 8) & 0xff))
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500124
125/* Utility to construct a standard name for a standard image. */
126static size_t
127standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type,
128 int log_bit_depth, int interlace_type)
129{
130 pos = safecat(buffer, bufsize, pos, colour_types[colour_type]);
131 pos = safecat(buffer, bufsize, pos, " ");
132 pos = safecat(buffer, bufsize, pos, bit_depths[log_bit_depth]);
133 pos = safecat(buffer, bufsize, pos, " bit");
134
135 if (interlace_type != PNG_INTERLACE_NONE)
136 pos = safecat(buffer, bufsize, pos, " interlaced");
137
138 return pos;
139}
140
141static size_t
142standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id)
143{
144 return standard_name(buffer, bufsize, pos, COL_FROM_ID(id),
145 log2depth(DEPTH_FROM_ID(id)), INTERLACE_FROM_ID(id));
146}
147
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -0500148/* Convenience API and defines to list valid formats. Note that 16 bit read and
149 * write support is required to do 16 bit read tests (we must be able to make a
150 * 16 bit image to test!)
151 */
152#ifdef PNG_WRITE_16BIT_SUPPORTED
153# define WRITE_BDHI 4
154# ifdef PNG_READ_16BIT_SUPPORTED
155# define READ_BDHI 4
156# define DO_16BIT
157# endif
158#else
159# define WRITE_BDHI 3
160#endif
161#ifndef DO_16BIT
162# define READ_BDHI 3
163#endif
164
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500165static int
166next_format(png_bytep colour_type, png_bytep bit_depth)
167{
168 if (*bit_depth == 0)
169 {
170 *colour_type = 0, *bit_depth = 1;
171 return 1;
172 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500173
174 *bit_depth = (png_byte)(*bit_depth << 1);
175
176 /* Palette images are restricted to 8 bit depth */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -0500177 if (*bit_depth <= 8
178# ifdef DO_16BIT
179 || (*colour_type != 3 && *bit_depth <= 16)
180# endif
181 )
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500182 return 1;
183
184 /* Move to the next color type, or return 0 at the end. */
185 switch (*colour_type)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500186 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500187 case 0:
188 *colour_type = 2;
189 *bit_depth = 8;
190 return 1;
191
192 case 2:
193 *colour_type = 3;
194 *bit_depth = 1;
195 return 1;
196
197 case 3:
198 *colour_type = 4;
199 *bit_depth = 8;
200 return 1;
201
202 case 4:
203 *colour_type = 6;
204 *bit_depth = 8;
205 return 1;
206
207 default:
208 return 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500209 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500210}
211
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500212static unsigned int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500213sample(png_const_bytep row, png_byte colour_type, png_byte bit_depth,
214 png_uint_32 x, unsigned int sample)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500215{
216 png_uint_32 index, result;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600217
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500218 /* Find a sample index for the desired sample: */
219 x *= bit_depth;
220 index = x;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500221
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500222 if ((colour_type & 1) == 0) /* !palette */
223 {
224 if (colour_type & 2)
225 index *= 3, index += sample; /* Colour channels; select one */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500226
227 if (colour_type & 4)
228 index += x; /* Alpha channel */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500229 }
230
231 /* Return the sample from the row as an integer. */
232 row += index >> 3;
233 result = *row;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500234
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500235 if (bit_depth == 8)
236 return result;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500237
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500238 else if (bit_depth > 8)
239 return (result << 8) + *++row;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500240
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500241 /* Less than 8 bits per sample. */
242 index &= 7;
243 return (result >> (8-index-bit_depth)) & ((1U<<bit_depth)-1);
244}
245
246/*************************** BASIC PNG FILE WRITING ***************************/
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500247/* A png_store takes data from the sequential writer or provides data
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500248 * to the sequential reader. It can also store the result of a PNG
249 * write for later retrieval.
250 */
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500251#define STORE_BUFFER_SIZE 500 /* arbitrary */
252typedef struct png_store_buffer
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500253{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500254 struct png_store_buffer* prev; /* NOTE: stored in reverse order */
255 png_byte buffer[STORE_BUFFER_SIZE];
256} png_store_buffer;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500257
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500258#define FILE_NAME_SIZE 64
259
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500260typedef struct png_store_file
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500261{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500262 struct png_store_file* next; /* as many as you like... */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500263 char name[FILE_NAME_SIZE];
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500264 png_uint_32 id; /* must be correct (see FILEID) */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500265 png_size_t datacount; /* In this (the last) buffer */
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500266 png_store_buffer data; /* Last buffer in file */
267} png_store_file;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500268
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500269/* The following is a pool of memory allocated by a single libpng read or write
270 * operation.
271 */
272typedef struct store_pool
273{
274 struct png_store *store; /* Back pointer */
275 struct store_memory *list; /* List of allocated memory */
276 png_byte mark[4]; /* Before and after data */
277
278 /* Statistics for this run. */
279 png_alloc_size_t max; /* Maximum single allocation */
280 png_alloc_size_t current; /* Current allocation */
281 png_alloc_size_t limit; /* Highest current allocation */
282 png_alloc_size_t total; /* Total allocation */
283
284 /* Overall statistics (retained across successive runs). */
285 png_alloc_size_t max_max;
286 png_alloc_size_t max_limit;
287 png_alloc_size_t max_total;
288} store_pool;
289
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500290typedef struct png_store
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500291{
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500292 /* For cexcept.h exception handling - simply store one of these;
293 * the context is a self pointer but it may point to a different
294 * png_store (in fact it never does in this program.)
295 */
296 struct exception_context
297 exception_context;
298
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500299 unsigned int verbose :1;
300 unsigned int treat_warnings_as_errors :1;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500301 unsigned int expect_error :1;
302 unsigned int expect_warning :1;
303 unsigned int saw_warning :1;
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500304 unsigned int speed :1;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500305 unsigned int progressive :1; /* use progressive read */
306 unsigned int validated :1; /* used as a temporary flag */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500307 int nerrors;
308 int nwarnings;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500309 char test[64]; /* Name of test */
310 char error[128];
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500311
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500312 /* Read fields */
313 png_structp pread; /* Used to read a saved file */
314 png_infop piread;
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500315 png_store_file* current; /* Set when reading */
316 png_store_buffer* next; /* Set when reading */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500317 png_size_t readpos; /* Position in *next */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500318 png_byte* image; /* Buffer for reading interlaced images */
319 size_t cb_image; /* Size of this buffer */
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500320 store_pool read_memory_pool;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500321
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500322 /* Write fields */
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500323 png_store_file* saved;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500324 png_structp pwrite; /* Used when writing a new file */
325 png_infop piwrite;
326 png_size_t writepos; /* Position in .new */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500327 char wname[FILE_NAME_SIZE];
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500328 png_store_buffer new; /* The end of the new PNG file being written. */
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500329 store_pool write_memory_pool;
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500330} png_store;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500331
332/* Initialization and cleanup */
333static void
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500334store_pool_mark(png_byte *mark)
335{
336 /* Generate a new mark. This uses a boring repeatable algorihtm and it is
337 * implemented here so that it gives the same set of numbers on every
338 * architecture. It's a linear congruential generator (Knuth or Sedgewick
339 * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
340 * Hill, "The Art of Electronics".
341 */
342 static png_uint_32 u0 = 0x12345678, u1 = 1;
343
344 /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
345 * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
346 */
347 int i;
348 for (i=0; i<4; ++i)
349 {
350 /* First generate 8 new bits then shift them in at the end. */
351 png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff;
352 u1 <<= 8;
353 u1 |= u0 >> 24;
354 u0 <<= 8;
355 u0 |= u;
356 *mark++ = (png_byte)u;
357 }
358}
359
360static void
361store_pool_init(png_store *ps, store_pool *pool)
362{
363 memset(pool, 0, sizeof *pool);
364
365 pool->store = ps;
366 pool->list = NULL;
367 pool->max = pool->current = pool->limit = pool->total = 0;
368 pool->max_max = pool->max_limit = pool->max_total = 0;
369 store_pool_mark(pool->mark);
370}
371
372static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500373store_init(png_store* ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500374{
375 memset(ps, 0, sizeof *ps);
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500376 init_exception_context(&ps->exception_context);
377 store_pool_init(ps, &ps->read_memory_pool);
378 store_pool_init(ps, &ps->write_memory_pool);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500379 ps->verbose = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500380 ps->treat_warnings_as_errors = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500381 ps->expect_error = 0;
382 ps->expect_warning = 0;
383 ps->saw_warning = 0;
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500384 ps->speed = 0;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500385 ps->progressive = 0;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500386 ps->validated = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500387 ps->nerrors = ps->nwarnings = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500388 ps->pread = NULL;
389 ps->piread = NULL;
390 ps->saved = ps->current = NULL;
391 ps->next = NULL;
392 ps->readpos = 0;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500393 ps->image = NULL;
394 ps->cb_image = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500395 ps->pwrite = NULL;
396 ps->piwrite = NULL;
397 ps->writepos = 0;
398 ps->new.prev = NULL;
399}
400
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -0500401/* This somewhat odd function is used when reading an image to ensure that the
402 * buffer is big enough - this is why a png_structp is available.
403 */
404static void
405store_ensure_image(png_store *ps, png_structp pp, size_t cb)
406{
407 if (ps->cb_image < cb)
408 {
409 if (ps->image != NULL)
410 {
411 free(ps->image-1);
412 ps->cb_image = 0;
413 }
414
415 /* The buffer is deliberately mis-aligned. */
416 ps->image = malloc(cb+1);
417 if (ps->image == NULL)
418 png_error(pp, "OOM allocating image buffer");
419
420 ++(ps->image);
421 ps->cb_image = cb;
422 }
423}
424
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500425static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500426store_freebuffer(png_store_buffer* psb)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500427{
428 if (psb->prev)
429 {
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500430 store_freebuffer(psb->prev);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500431 free(psb->prev);
432 psb->prev = NULL;
433 }
434}
435
436static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500437store_freenew(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500438{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500439 store_freebuffer(&ps->new);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500440 ps->writepos = 0;
441}
442
443static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500444store_storenew(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500445{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500446 png_store_buffer *pb;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500447
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500448 if (ps->writepos != STORE_BUFFER_SIZE)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500449 png_error(ps->pwrite, "invalid store call");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500450
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500451 pb = malloc(sizeof *pb);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500452
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500453 if (pb == NULL)
454 png_error(ps->pwrite, "store new: OOM");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500455
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500456 *pb = ps->new;
457 ps->new.prev = pb;
458 ps->writepos = 0;
459}
460
461static void
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500462store_freefile(png_store_file **ppf)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500463{
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500464 if (*ppf != NULL)
465 {
466 store_freefile(&(*ppf)->next);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500467
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500468 store_freebuffer(&(*ppf)->data);
469 (*ppf)->datacount = 0;
470 free(*ppf);
471 *ppf = NULL;
472 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500473}
474
475/* Main interface to file storeage, after writing a new PNG file (see the API
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500476 * below) call store_storefile to store the result with the given name and id.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500477 */
478static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500479store_storefile(png_store *ps, png_uint_32 id)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500480{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500481 png_store_file *pf = malloc(sizeof *pf);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500482 if (pf == NULL)
483 png_error(ps->pwrite, "storefile: OOM");
484 safecat(pf->name, sizeof pf->name, 0, ps->wname);
485 pf->id = id;
486 pf->data = ps->new;
487 pf->datacount = ps->writepos;
488 ps->new.prev = NULL;
489 ps->writepos = 0;
490
491 /* And save it. */
492 pf->next = ps->saved;
493 ps->saved = pf;
494}
495
496/* Generate an error message (in the given buffer) */
497static size_t
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500498store_message(png_store *ps, png_structp pp, char *buffer, size_t bufsize,
499 size_t pos, PNG_CONST char *msg)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500500{
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500501 if (pp != NULL && pp == ps->pread)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500502 {
503 /* Reading a file */
504 pos = safecat(buffer, bufsize, pos, "read: ");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500505
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500506 if (ps->current != NULL)
507 {
508 pos = safecat(buffer, bufsize, pos, ps->current->name);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500509 pos = safecat(buffer, bufsize, pos, sep);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500510 }
511 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500512
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500513 else if (pp != NULL && pp == ps->pwrite)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500514 {
515 /* Writing a file */
516 pos = safecat(buffer, bufsize, pos, "write: ");
517 pos = safecat(buffer, bufsize, pos, ps->wname);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500518 pos = safecat(buffer, bufsize, pos, sep);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500519 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500520
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500521 else
522 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500523 /* Neither reading nor writing (or a memory error in struct delete) */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500524 pos = safecat(buffer, bufsize, pos, "pngvalid: ");
525 }
526
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500527 if (ps->test[0] != 0)
528 {
529 pos = safecat(buffer, bufsize, pos, ps->test);
530 pos = safecat(buffer, bufsize, pos, sep);
531 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500532 pos = safecat(buffer, bufsize, pos, msg);
533 return pos;
534}
535
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500536/* Log an error or warning - the relevant count is always incremented. */
537static void
538store_log(png_store* ps, png_structp pp, png_const_charp message, int is_error)
539{
540 /* The warning is copied to the error buffer if there are no errors and it is
541 * the first warning. The error is copied to the error buffer if it is the
542 * first error (overwriting any prior warnings).
543 */
544 if (is_error ? (ps->nerrors)++ == 0 :
545 (ps->nwarnings)++ == 0 && ps->nerrors == 0)
546 store_message(ps, pp, ps->error, sizeof ps->error, 0, message);
547
548 if (ps->verbose)
549 {
550 char buffer[256];
551 size_t pos;
552
553 if (is_error)
554 pos = safecat(buffer, sizeof buffer, 0, "error: ");
555 else
556 pos = safecat(buffer, sizeof buffer, 0, "warning: ");
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600557
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500558 store_message(ps, pp, buffer, sizeof buffer, pos, message);
559 fputs(buffer, stderr);
560 fputc('\n', stderr);
561 }
562}
563
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500564/* Functions to use as PNG callbacks. */
565static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500566store_error(png_structp pp, png_const_charp message) /* PNG_NORETURN */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500567{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500568 png_store *ps = png_get_error_ptr(pp);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500569
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500570 if (!ps->expect_error)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500571 store_log(ps, pp, message, 1/*error*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500572
573 /* And finally throw an exception. */
574 {
575 struct exception_context *the_exception_context = &ps->exception_context;
576 Throw ps;
577 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500578}
579
580static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500581store_warning(png_structp pp, png_const_charp message)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500582{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500583 png_store *ps = png_get_error_ptr(pp);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500584
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500585 if (!ps->expect_warning)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500586 store_log(ps, pp, message, 0/*warning*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500587 else
588 ps->saw_warning = 1;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500589}
590
591static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500592store_write(png_structp pp, png_bytep pb, png_size_t st)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500593{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500594 png_store *ps = png_get_io_ptr(pp);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500595
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500596 if (ps->pwrite != pp)
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500597 png_error(pp, "store state damaged");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500598
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500599 while (st > 0)
600 {
601 size_t cb;
602
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500603 if (ps->writepos >= STORE_BUFFER_SIZE)
604 store_storenew(ps);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500605
606 cb = st;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500607
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500608 if (cb > STORE_BUFFER_SIZE - ps->writepos)
609 cb = STORE_BUFFER_SIZE - ps->writepos;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500610
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500611 memcpy(ps->new.buffer + ps->writepos, pb, cb);
612 pb += cb;
613 st -= cb;
614 ps->writepos += cb;
615 }
616}
617
618static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500619store_flush(png_structp pp)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500620{
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500621 pp = pp;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500622 /*DOES NOTHING*/
623}
624
625static size_t
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500626store_read_buffer_size(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500627{
628 /* Return the bytes available for read in the current buffer. */
629 if (ps->next != &ps->current->data)
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500630 return STORE_BUFFER_SIZE;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500631
632 return ps->current->datacount;
633}
634
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500635/* Return total bytes available for read. */
636static size_t
637store_read_buffer_avail(png_store *ps)
638{
639 if (ps->current != NULL && ps->next != NULL)
640 {
641 png_store_buffer *next = &ps->current->data;
642 size_t cbAvail = ps->current->datacount;
643
644 while (next != ps->next && next != NULL)
645 {
646 next = next->prev;
647 cbAvail += STORE_BUFFER_SIZE;
648 }
649
650 if (next != ps->next)
651 png_error(ps->pread, "buffer read error");
652
653 if (cbAvail > ps->readpos)
654 return cbAvail - ps->readpos;
655 }
656
657 return 0;
658}
659
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500660static int
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500661store_read_buffer_next(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500662{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500663 png_store_buffer *pbOld = ps->next;
664 png_store_buffer *pbNew = &ps->current->data;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500665 if (pbOld != pbNew)
666 {
667 while (pbNew != NULL && pbNew->prev != pbOld)
668 pbNew = pbNew->prev;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500669
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500670 if (pbNew != NULL)
671 {
672 ps->next = pbNew;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500673 ps->readpos = 0;
674 return 1;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500675 }
676
677 png_error(ps->pread, "buffer lost");
678 }
679
680 return 0; /* EOF or error */
681}
682
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500683/* Need separate implementation and callback to allow use of the same code
684 * during progressive read, where the io_ptr is set internally by libpng.
685 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500686static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500687store_read_imp(png_store *ps, png_bytep pb, png_size_t st)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500688{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500689 if (ps->current == NULL || ps->next == NULL)
690 png_error(ps->pread, "store state damaged");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500691
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500692 while (st > 0)
693 {
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500694 size_t cbAvail = store_read_buffer_size(ps) - ps->readpos;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500695
696 if (cbAvail > 0)
697 {
698 if (cbAvail > st) cbAvail = st;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500699 memcpy(pb, ps->next->buffer + ps->readpos, cbAvail);
700 st -= cbAvail;
701 pb += cbAvail;
702 ps->readpos += cbAvail;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500703 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500704
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500705 else if (!store_read_buffer_next(ps))
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500706 png_error(ps->pread, "read beyond end of file");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500707 }
708}
709
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500710static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500711store_read(png_structp pp, png_bytep pb, png_size_t st)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500712{
713 png_store *ps = png_get_io_ptr(pp);
714
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500715 if (ps == NULL || ps->pread != pp)
716 png_error(pp, "bad store read call");
717
718 store_read_imp(ps, pb, st);
719}
720
721static void
722store_progressive_read(png_store *ps, png_structp pp, png_infop pi)
723{
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500724 /* Notice that a call to store_read will cause this function to fail because
725 * readpos will be set.
726 */
727 if (ps->pread != pp || ps->current == NULL || ps->next == NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500728 png_error(pp, "store state damaged (progressive)");
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500729
730 do
731 {
732 if (ps->readpos != 0)
733 png_error(pp, "store_read called during progressive read");
734
735 png_process_data(pp, pi, ps->next->buffer, store_read_buffer_size(ps));
736 }
737 while (store_read_buffer_next(ps));
738}
739
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500740/***************************** MEMORY MANAGEMENT*** ***************************/
741/* A store_memory is simply the header for an allocated block of memory. The
742 * pointer returned to libpng is just after the end of the header block, the
743 * allocated memory is followed by a second copy of the 'mark'.
744 */
745typedef struct store_memory
746{
747 store_pool *pool; /* Originating pool */
748 struct store_memory *next; /* Singly linked list */
749 png_alloc_size_t size; /* Size of memory allocated */
750 png_byte mark[4]; /* ID marker */
751} store_memory;
752
753/* Handle a fatal error in memory allocation. This calls png_error if the
754 * libpng struct is non-NULL, else it outputs a message and returns. This means
755 * that a memory problem while libpng is running will abort (png_error) the
756 * handling of particular file while one in cleanup (after the destroy of the
757 * struct has returned) will simply keep going and free (or attempt to free)
758 * all the memory.
759 */
760static void
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500761store_pool_error(png_store *ps, png_structp pp, PNG_CONST char *msg)
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500762{
763 if (pp != NULL)
764 png_error(pp, msg);
765
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500766 /* Else we have to do it ourselves. png_error eventually calls store_log,
767 * above. store_log accepts a NULL png_structp - it just changes what gets
768 * output by store_message.
769 */
770 store_log(ps, pp, msg, 1/*error*/);
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500771}
772
773static void
774store_memory_free(png_structp pp, store_pool *pool, store_memory *memory)
775{
776 /* Note that pp may be NULL (see store_pool_delete below), the caller has
777 * found 'memory' in pool->list *and* unlinked this entry, so this is a valid
778 * pointer (for sure), but the contents may have been trashed.
779 */
780 if (memory->pool != pool)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500781 store_pool_error(pool->store, pp, "memory corrupted (pool)");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500782
783 else if (memcmp(memory->mark, pool->mark, sizeof memory->mark) != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500784 store_pool_error(pool->store, pp, "memory corrupted (start)");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500785
786 /* It should be safe to read the size field now. */
787 else
788 {
789 png_alloc_size_t cb = memory->size;
790
791 if (cb > pool->max)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500792 store_pool_error(pool->store, pp, "memory corrupted (size)");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500793
794 else if (memcmp((png_bytep)(memory+1)+cb, pool->mark, sizeof pool->mark)
795 != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500796 store_pool_error(pool->store, pp, "memory corrupted (end)");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500797
798 /* Finally give the library a chance to find problems too: */
799 else
800 {
801 pool->current -= cb;
802 free(memory);
803 }
804 }
805}
806
807static void
808store_pool_delete(png_store *ps, store_pool *pool)
809{
810 if (pool->list != NULL)
811 {
812 fprintf(stderr, "%s: %s %s: memory lost (list follows):\n", ps->test,
813 pool == &ps->read_memory_pool ? "read" : "write",
814 pool == &ps->read_memory_pool ? (ps->current != NULL ?
815 ps->current->name : "unknown file") : ps->wname);
816 ++ps->nerrors;
817
818 do
819 {
820 store_memory *next = pool->list;
821 pool->list = next->next;
822 next->next = NULL;
823
Glenn Randers-Pehrson9a75d992010-10-08 16:27:14 -0500824 fprintf(stderr, "\t%lu bytes @ %p\n",
825 (unsigned long)next->size, next+1);
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500826 /* The NULL means this will always return, even if the memory is
827 * corrupted.
828 */
829 store_memory_free(NULL, pool, next);
830 }
831 while (pool->list != NULL);
832 }
833
834 /* And reset the other fields too for the next time. */
835 if (pool->max > pool->max_max) pool->max_max = pool->max;
836 pool->max = 0;
837 if (pool->current != 0) /* unexpected internal error */
838 fprintf(stderr, "%s: %s %s: memory counter mismatch (internal error)\n",
839 ps->test, pool == &ps->read_memory_pool ? "read" : "write",
840 pool == &ps->read_memory_pool ? (ps->current != NULL ?
841 ps->current->name : "unknown file") : ps->wname);
842 pool->current = 0;
843 if (pool->limit > pool->max_limit) pool->max_limit = pool->limit;
844 pool->limit = 0;
845 if (pool->total > pool->max_total) pool->max_total = pool->total;
846 pool->total = 0;
847
848 /* Get a new mark too. */
849 store_pool_mark(pool->mark);
850}
851
852/* The memory callbacks: */
853static png_voidp
854store_malloc(png_structp pp, png_alloc_size_t cb)
855{
856 store_pool *pool = png_get_mem_ptr(pp);
857 store_memory *new = malloc(cb + (sizeof *new) + (sizeof pool->mark));
858
859 if (new != NULL)
860 {
861 if (cb > pool->max) pool->max = cb;
862 pool->current += cb;
863 if (pool->current > pool->limit) pool->limit = pool->current;
864 pool->total += cb;
865
866 new->size = cb;
867 memcpy(new->mark, pool->mark, sizeof new->mark);
868 memcpy((png_byte*)(new+1) + cb, pool->mark, sizeof pool->mark);
869 new->pool = pool;
870 new->next = pool->list;
871 pool->list = new;
872 ++new;
873 }
874 else
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500875 store_pool_error(pool->store, pp, "out of memory");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500876
877 return new;
878}
879
880static void
881store_free(png_structp pp, png_voidp memory)
882{
883 store_pool *pool = png_get_mem_ptr(pp);
884 store_memory *this = memory, **test;
885
886 /* First check that this 'memory' really is valid memory - it must be in the
887 * pool list. If it is use the shared memory_free function to free it.
888 */
889 --this;
890 for (test = &pool->list; *test != this; test = &(*test)->next)
891 {
892 if (*test == NULL)
893 {
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500894 store_pool_error(pool->store, pp, "bad pointer to free");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500895 return;
896 }
897 }
898
899 /* Unlink this entry, *test == this. */
900 *test = this->next;
901 this->next = NULL;
902 store_memory_free(pp, pool, this);
903}
904
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500905/* Setup functions. */
906/* Cleanup when aborting a write or after storing the new file. */
907static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500908store_write_reset(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500909{
910 if (ps->pwrite != NULL)
911 {
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500912 anon_context(ps);
913
914 Try
915 png_destroy_write_struct(&ps->pwrite, &ps->piwrite);
916
917 Catch_anonymous
918 {
919 /* memory corruption: continue. */
920 }
921
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500922 ps->pwrite = NULL;
923 ps->piwrite = NULL;
924 }
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500925
926 /* And make sure that all the memory has been freed - this will output
927 * spurious errors in the case of memory corruption above, but this is safe.
928 */
929 store_pool_delete(ps, &ps->write_memory_pool);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600930
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500931 store_freenew(ps);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500932}
933
934/* The following is the main write function, it returns a png_struct and,
935 * optionally, a png)info suitable for writiing a new PNG file. Use
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500936 * store_storefile above to record this file after it has been written. The
937 * returned libpng structures as destroyed by store_write_reset above.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500938 */
939static png_structp
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500940set_store_for_write(png_store *ps, png_infopp ppi,
941 PNG_CONST char * volatile name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500942{
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500943 anon_context(ps);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500944
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500945 Try
946 {
947 if (ps->pwrite != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500948 png_error(ps->pwrite, "write store already in use");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500949
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500950 store_write_reset(ps);
951 safecat(ps->wname, sizeof ps->wname, 0, name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500952
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500953 /* Don't do the slow memory checks if doing a speed test. */
954 if (ps->speed)
955 ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING,
956 ps, store_error, store_warning);
957 else
958 ps->pwrite = png_create_write_struct_2(PNG_LIBPNG_VER_STRING,
959 ps, store_error, store_warning, &ps->write_memory_pool,
960 store_malloc, store_free);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500961 png_set_write_fn(ps->pwrite, ps, store_write, store_flush);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500962
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500963 if (ppi != NULL)
964 *ppi = ps->piwrite = png_create_info_struct(ps->pwrite);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500965 }
966
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500967 Catch_anonymous
968 return NULL;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500969
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500970 return ps->pwrite;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500971}
972
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -0500973/* Cleanup when finished reading (either due to error or in the success case).
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500974 */
975static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500976store_read_reset(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500977{
978 if (ps->pread != NULL)
979 {
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500980 anon_context(ps);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600981
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500982 Try
983 png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
984
985 Catch_anonymous
986 {
987 /*error already output: continue*/
988 }
989
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500990 ps->pread = NULL;
991 ps->piread = NULL;
992 }
993
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500994 /* Always do this to be safe. */
995 store_pool_delete(ps, &ps->read_memory_pool);
996
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500997 ps->current = NULL;
998 ps->next = NULL;
999 ps->readpos = 0;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001000 ps->validated = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001001}
1002
1003static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001004store_read_set(png_store *ps, png_uint_32 id)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001005{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001006 png_store_file *pf = ps->saved;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001007
1008 while (pf != NULL)
1009 {
1010 if (pf->id == id)
1011 {
1012 ps->current = pf;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001013 ps->next = NULL;
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001014 store_read_buffer_next(ps);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001015 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001016 }
1017
1018 pf = pf->next;
1019 }
1020
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001021 {
1022 size_t pos;
1023 char msg[FILE_NAME_SIZE+64];
1024
1025 pos = standard_name_from_id(msg, sizeof msg, 0, id);
1026 pos = safecat(msg, sizeof msg, pos, ": file not found");
1027 png_error(ps->pread, msg);
1028 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001029}
1030
1031/* The main interface for reading a saved file - pass the id number of the file
1032 * to retrieve. Ids must be unique or the earlier file will be hidden. The API
1033 * returns a png_struct and, optionally, a png_info. Both of these will be
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001034 * destroyed by store_read_reset above.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001035 */
1036static png_structp
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001037set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001038 PNG_CONST char *name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001039{
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001040 /* Set the name for png_error */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001041 safecat(ps->test, sizeof ps->test, 0, name);
1042
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001043 if (ps->pread != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001044 png_error(ps->pread, "read store already in use");
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001045
1046 store_read_reset(ps);
1047
1048 /* Both the create APIs can return NULL if used in their default mode
1049 * (because there is no other way of handling an error because the jmp_buf by
1050 * default is stored in png_struct and that has not been allocated!)
1051 * However, given that store_error works correctly in these circumstances we
1052 * don't ever expect NULL in this program.
1053 */
1054 if (ps->speed)
1055 ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps,
1056 store_error, store_warning);
1057 else
1058 ps->pread = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, ps,
1059 store_error, store_warning, &ps->read_memory_pool, store_malloc,
1060 store_free);
1061
1062 if (ps->pread == NULL)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001063 {
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001064 struct exception_context *the_exception_context = &ps->exception_context;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001065
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05001066 store_log(ps, NULL, "png_create_read_struct returned NULL (unexpected)",
1067 1/*error*/);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001068
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001069 Throw ps;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001070 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001071
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001072 store_read_set(ps, id);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001073
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001074 if (ppi != NULL)
1075 *ppi = ps->piread = png_create_info_struct(ps->pread);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001076
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001077 return ps->pread;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001078}
1079
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05001080/* The overall cleanup of a store simply calls the above then removes all the
1081 * saved files. This does not delete the store itself.
1082 */
1083static void
1084store_delete(png_store *ps)
1085{
1086 store_write_reset(ps);
1087 store_read_reset(ps);
1088 store_freefile(&ps->saved);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001089 if (ps->image != NULL)
1090 {
1091 free(ps->image-1);
1092 ps->image = NULL;
1093 ps->cb_image = 0;
1094 }
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05001095}
1096
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001097/*********************** PNG FILE MODIFICATION ON READ ************************/
1098/* Files may be modified on read. The following structure contains a complete
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001099 * png_store together with extra members to handle modification and a special
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001100 * read callback for libpng. To use this the 'modifications' field must be set
1101 * to a list of png_modification structures that actually perform the
1102 * modification, otherwise a png_modifier is functionally equivalent to a
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001103 * png_store. There is a special read function, set_modifier_for_read, which
1104 * replaces set_store_for_read.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001105 */
1106typedef struct png_modifier
1107{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001108 png_store this; /* I am a png_store */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001109 struct png_modification *modifications; /* Changes to make */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001110
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001111 enum modifier_state
1112 {
1113 modifier_start, /* Initial value */
1114 modifier_signature, /* Have a signature */
1115 modifier_IHDR /* Have an IHDR */
1116 } state; /* My state */
1117
1118 /* Information from IHDR: */
1119 png_byte bit_depth; /* From IHDR */
1120 png_byte colour_type; /* From IHDR */
1121
1122 /* While handling PLTE, IDAT and IEND these chunks may be pended to allow
1123 * other chunks to be inserted.
1124 */
1125 png_uint_32 pending_len;
1126 png_uint_32 pending_chunk;
1127
1128 /* Test values */
1129 double *gammas;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001130 unsigned int ngammas;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001131
1132 /* Lowest sbit to test (libpng fails for sbit < 8) */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001133 png_byte sbitlow;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001134
1135 /* Error control - these are the limits on errors accepted by the gamma tests
1136 * below.
1137 */
1138 double maxout8; /* Maximum output value error */
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05001139 double maxabs8; /* Absolute sample error 0..1 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001140 double maxpc8; /* Percentage sample error 0..100% */
1141 double maxout16; /* Maximum output value error */
1142 double maxabs16; /* Absolute sample error 0..1 */
1143 double maxpc16; /* Percentage sample error 0..100% */
1144
1145 /* Logged 8 and 16 bit errors ('output' values): */
1146 double error_gray_2;
1147 double error_gray_4;
1148 double error_gray_8;
1149 double error_gray_16;
1150 double error_color_8;
1151 double error_color_16;
1152
1153 /* Flags: */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001154 /* Whether or not to interlace. */
1155 int interlace_type :9; /* int, but must store '1' */
1156
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001157 /* When to use the use_input_precision option: */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001158 unsigned int use_input_precision :1;
1159 unsigned int use_input_precision_sbit :1;
1160 unsigned int use_input_precision_16to8 :1;
1161 unsigned int log :1; /* Log max error */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001162
1163 /* Buffer information, the buffer size limits the size of the chunks that can
1164 * be modified - they must fit (including header and CRC) into the buffer!
1165 */
1166 size_t flush; /* Count of bytes to flush */
1167 size_t buffer_count; /* Bytes in buffer */
1168 size_t buffer_position; /* Position in buffer */
1169 png_byte buffer[1024];
1170} png_modifier;
1171
1172static double abserr(png_modifier *pm, png_byte bit_depth)
1173{
1174 return bit_depth == 16 ? pm->maxabs16 : pm->maxabs8;
1175}
1176
1177static double pcerr(png_modifier *pm, png_byte bit_depth)
1178{
1179 return (bit_depth == 16 ? pm->maxpc16 : pm->maxpc8) * .01;
1180}
1181
1182static double outerr(png_modifier *pm, png_byte bit_depth)
1183{
1184 /* There is a serious error in the 2 and 4 bit grayscale transform because
1185 * the gamma table value (8 bits) is simply shifted, not rouned, so the
1186 * error in 4 bit greyscale gamma is up to the value below. This is a hack
1187 * to allow pngvalid to succeed:
1188 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001189 if (bit_depth == 2)
1190 return .73182-.5;
1191
1192 if (bit_depth == 4)
1193 return .90644-.5;
1194
1195 if (bit_depth == 16)
1196 return pm->maxout16;
1197
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001198 return pm->maxout8;
1199}
1200
1201/* This returns true if the test should be stopped now because it has already
1202 * failed and it is running silently.
1203 */
1204static int fail(png_modifier *pm)
1205{
1206 return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 ||
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001207 (pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001208}
1209
1210static void
1211modifier_init(png_modifier *pm)
1212{
1213 memset(pm, 0, sizeof *pm);
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001214 store_init(&pm->this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001215 pm->modifications = NULL;
1216 pm->state = modifier_start;
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001217 pm->sbitlow = 1U;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001218 pm->maxout8 = pm->maxpc8 = pm->maxabs8 = 0;
1219 pm->maxout16 = pm->maxpc16 = pm->maxabs16 = 0;
1220 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0;
1221 pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001222 pm->interlace_type = PNG_INTERLACE_NONE;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001223 pm->use_input_precision = 0;
1224 pm->use_input_precision_sbit = 0;
1225 pm->use_input_precision_16to8 = 0;
1226 pm->log = 0;
1227
1228 /* Rely on the memset for all the other fields - there are no pointers */
1229}
1230
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05001231/* One modification structure must be provided for each chunk to be modified (in
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001232 * fact more than one can be provided if multiple separate changes are desired
1233 * for a single chunk.) Modifications include adding a new chunk when a
1234 * suitable chunk does not exist.
1235 *
1236 * The caller of modify_fn will reset the CRC of the chunk and record 'modified'
1237 * or 'added' as appropriate if the modify_fn returns 1 (true). If the
1238 * modify_fn is NULL the chunk is simply removed.
1239 */
1240typedef struct png_modification
1241{
1242 struct png_modification *next;
1243 png_uint_32 chunk;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001244
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001245 /* If the following is NULL all matching chunks will be removed: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001246 int (*modify_fn)(struct png_modifier *pm,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001247 struct png_modification *me, int add);
1248
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001249 /* If the following is set to PLTE, IDAT or IEND and the chunk has not been
1250 * found and modified (and there is a modify_fn) the modify_fn will be called
1251 * to add the chunk before the relevant chunk.
1252 */
1253 png_uint_32 add;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001254 unsigned int modified :1; /* Chunk was modified */
1255 unsigned int added :1; /* Chunk was added */
1256 unsigned int removed :1; /* Chunk was removed */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001257} png_modification;
1258
1259static void modification_reset(png_modification *pmm)
1260{
1261 if (pmm != NULL)
1262 {
1263 pmm->modified = 0;
1264 pmm->added = 0;
1265 pmm->removed = 0;
1266 modification_reset(pmm->next);
1267 }
1268}
1269
1270static void
1271modification_init(png_modification *pmm)
1272{
1273 memset(pmm, 0, sizeof *pmm);
1274 pmm->next = NULL;
1275 pmm->chunk = 0;
1276 pmm->modify_fn = NULL;
1277 pmm->add = 0;
1278 modification_reset(pmm);
1279}
1280
1281static void
1282modifier_reset(png_modifier *pm)
1283{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001284 store_read_reset(&pm->this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001285 pm->modifications = NULL;
1286 pm->state = modifier_start;
1287 pm->bit_depth = pm->colour_type = 0;
1288 pm->pending_len = pm->pending_chunk = 0;
1289 pm->flush = pm->buffer_count = pm->buffer_position = 0;
1290}
1291
1292/* Convenience macros. */
1293#define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
1294#define CHUNK_IHDR CHUNK(73,72,68,82)
1295#define CHUNK_PLTE CHUNK(80,76,84,69)
1296#define CHUNK_IDAT CHUNK(73,68,65,84)
1297#define CHUNK_IEND CHUNK(73,69,78,68)
1298#define CHUNK_cHRM CHUNK(99,72,82,77)
1299#define CHUNK_gAMA CHUNK(103,65,77,65)
1300#define CHUNK_sBIT CHUNK(115,66,73,84)
1301#define CHUNK_sRGB CHUNK(115,82,71,66)
1302
1303/* The guts of modification are performed during a read. */
1304static void
1305modifier_crc(png_bytep buffer)
1306{
1307 /* Recalculate the chunk CRC - a complete chunk must be in
1308 * the buffer, at the start.
1309 */
1310 uInt datalen = png_get_uint_32(buffer);
1311 png_save_uint_32(buffer+datalen+8, crc32(0L, buffer+4, datalen+4));
1312}
1313
1314static void
1315modifier_setbuffer(png_modifier *pm)
1316{
1317 modifier_crc(pm->buffer);
1318 pm->buffer_count = png_get_uint_32(pm->buffer)+12;
1319 pm->buffer_position = 0;
1320}
1321
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001322/* Separate the callback into the actual implementation (which is passed the
1323 * png_modifier explicitly) and the callback, which gets the modifier from the
1324 * png_struct.
1325 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001326static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001327modifier_read_imp(png_modifier *pm, png_bytep pb, png_size_t st)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001328{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001329 while (st > 0)
1330 {
1331 size_t cb;
1332 png_uint_32 len, chunk;
1333 png_modification *mod;
1334
1335 if (pm->buffer_position >= pm->buffer_count) switch (pm->state)
1336 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001337 static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
1338 case modifier_start:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001339 store_read_imp(&pm->this, pm->buffer, 8); /* size of signature. */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001340 pm->buffer_count = 8;
1341 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001342
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001343 if (memcmp(pm->buffer, sign, 8) != 0)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001344 png_error(pm->this.pread, "invalid PNG file signature");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001345 pm->state = modifier_signature;
1346 break;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001347
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001348 case modifier_signature:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001349 store_read_imp(&pm->this, pm->buffer, 13+12); /* size of IHDR */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001350 pm->buffer_count = 13+12;
1351 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001352
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001353 if (png_get_uint_32(pm->buffer) != 13 ||
1354 png_get_uint_32(pm->buffer+4) != CHUNK_IHDR)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001355 png_error(pm->this.pread, "invalid IHDR");
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001356
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001357 /* Check the list of modifiers for modifications to the IHDR. */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001358 mod = pm->modifications;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001359 while (mod != NULL)
1360 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001361 if (mod->chunk == CHUNK_IHDR && mod->modify_fn &&
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001362 (*mod->modify_fn)(pm, mod, 0))
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001363 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001364 mod->modified = 1;
1365 modifier_setbuffer(pm);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001366 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001367
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001368 /* Ignore removal or add if IHDR! */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001369 mod = mod->next;
1370 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001371
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001372 /* Cache information from the IHDR (the modified one.) */
1373 pm->bit_depth = pm->buffer[8+8];
1374 pm->colour_type = pm->buffer[8+8+1];
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001375
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001376 pm->state = modifier_IHDR;
1377 pm->flush = 0;
1378 break;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001379
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001380 case modifier_IHDR:
1381 default:
1382 /* Read a new chunk and process it until we see PLTE, IDAT or
1383 * IEND. 'flush' indicates that there is still some data to
1384 * output from the preceding chunk.
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001385 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001386 if ((cb = pm->flush) > 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001387 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001388 if (cb > st) cb = st;
1389 pm->flush -= cb;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001390 store_read_imp(&pm->this, pb, cb);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001391 pb += cb;
1392 st -= cb;
1393 if (st <= 0) return;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001394 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001395
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001396 /* No more bytes to flush, read a header, or handle a pending
1397 * chunk.
1398 */
1399 if (pm->pending_chunk != 0)
1400 {
1401 png_save_uint_32(pm->buffer, pm->pending_len);
1402 png_save_uint_32(pm->buffer+4, pm->pending_chunk);
1403 pm->pending_len = 0;
1404 pm->pending_chunk = 0;
1405 }
1406 else
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001407 store_read_imp(&pm->this, pm->buffer, 8);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001408
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001409 pm->buffer_count = 8;
1410 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001411
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001412 /* Check for something to modify or a terminator chunk. */
1413 len = png_get_uint_32(pm->buffer);
1414 chunk = png_get_uint_32(pm->buffer+4);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001415
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001416 /* Terminators first, they may have to be delayed for added
1417 * chunks
1418 */
1419 if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT ||
1420 chunk == CHUNK_IEND)
1421 {
1422 mod = pm->modifications;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001423
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001424 while (mod != NULL)
1425 {
1426 if ((mod->add == chunk ||
1427 (mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT)) &&
1428 mod->modify_fn != NULL && !mod->modified && !mod->added)
1429 {
1430 /* Regardless of what the modify function does do not run
1431 * this again.
1432 */
1433 mod->added = 1;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001434
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001435 if ((*mod->modify_fn)(pm, mod, 1/*add*/))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001436 {
1437 /* Reset the CRC on a new chunk */
1438 if (pm->buffer_count > 0)
1439 modifier_setbuffer(pm);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001440
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001441 else
1442 {
1443 pm->buffer_position = 0;
1444 mod->removed = 1;
1445 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001446
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001447 /* The buffer has been filled with something (we assume)
1448 * so output this. Pend the current chunk.
1449 */
1450 pm->pending_len = len;
1451 pm->pending_chunk = chunk;
1452 break; /* out of while */
1453 }
1454 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001455
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001456 mod = mod->next;
1457 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001458
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001459 /* Don't do any further processing if the buffer was modified -
1460 * otherwise the code will end up modifying a chunk that was just
1461 * added.
1462 */
1463 if (mod != NULL)
1464 break; /* out of switch */
1465 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001466
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001467 /* If we get to here then this chunk may need to be modified. To do
1468 * this is must be less than 1024 bytes in total size, otherwise
1469 * it just gets flushed.
1470 */
1471 if (len+12 <= sizeof pm->buffer)
1472 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001473 store_read_imp(&pm->this, pm->buffer+pm->buffer_count,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001474 len+12-pm->buffer_count);
1475 pm->buffer_count = len+12;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001476
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001477 /* Check for a modification, else leave it be. */
1478 mod = pm->modifications;
1479 while (mod != NULL)
1480 {
1481 if (mod->chunk == chunk)
1482 {
1483 if (mod->modify_fn == NULL)
1484 {
1485 /* Remove this chunk */
1486 pm->buffer_count = pm->buffer_position = 0;
1487 mod->removed = 1;
1488 break; /* Terminate the while loop */
1489 }
1490
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001491 else if ((*mod->modify_fn)(pm, mod, 0))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001492 {
1493 mod->modified = 1;
1494 /* The chunk may have been removed: */
1495 if (pm->buffer_count == 0)
1496 {
1497 pm->buffer_position = 0;
1498 break;
1499 }
1500 modifier_setbuffer(pm);
1501 }
1502 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001503
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001504 mod = mod->next;
1505 }
1506 }
1507
1508 else
1509 pm->flush = len+12 - pm->buffer_count; /* data + crc */
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001510
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001511 /* Take the data from the buffer (if there is any). */
1512 break;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001513 }
1514
1515 /* Here to read from the modifier buffer (not directly from
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001516 * the store, as in the flush case above.)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001517 */
1518 cb = pm->buffer_count - pm->buffer_position;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001519
1520 if (cb > st)
1521 cb = st;
1522
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001523 memcpy(pb, pm->buffer + pm->buffer_position, cb);
1524 st -= cb;
1525 pb += cb;
1526 pm->buffer_position += cb;
1527 }
1528}
1529
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001530/* The callback: */
1531static void
1532modifier_read(png_structp pp, png_bytep pb, png_size_t st)
1533{
1534 png_modifier *pm = png_get_io_ptr(pp);
1535
1536 if (pm == NULL || pm->this.pread != pp)
1537 png_error(pp, "bad modifier_read call");
1538
1539 modifier_read_imp(pm, pb, st);
1540}
1541
1542/* Like store_progressive_read but the data is getting changed as we go so we
1543 * need a local buffer.
1544 */
1545static void
1546modifier_progressive_read(png_modifier *pm, png_structp pp, png_infop pi)
1547{
1548 if (pm->this.pread != pp || pm->this.current == NULL ||
1549 pm->this.next == NULL)
1550 png_error(pp, "store state damaged (progressive)");
1551
1552 /* This is another Horowitz and Hill random noise generator. In this case
1553 * the aim is to stress the progressive reader with truely horrible variable
1554 * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers is
1555 * generated. We could probably just count from 1 to 32767 and get as good
1556 * a result.
1557 */
1558 for (;;)
1559 {
1560 static png_uint_32 noise = 1;
1561 png_size_t cb, cbAvail;
1562 png_byte buffer[512];
1563
1564 /* Generate 15 more bits of stuff: */
1565 noise = (noise << 9) | ((noise ^ (noise >> (9-5))) & 0x1ff);
1566 cb = noise & 0x1ff;
1567
1568 /* Check that this number of bytes are available (in the current buffer.)
1569 * (This doesn't quite work - the modifier might delete a chunk; unlikely
1570 * but possible, it doesn't happen at present because the modifier only
1571 * adds chunks to standard images.)
1572 */
1573 cbAvail = store_read_buffer_avail(&pm->this);
1574 if (pm->buffer_count > pm->buffer_position)
1575 cbAvail += pm->buffer_count - pm->buffer_position;
1576
1577 if (cb > cbAvail)
1578 {
1579 /* Check for EOF: */
1580 if (cbAvail == 0)
1581 break;
1582
1583 cb = cbAvail;
1584 }
1585
1586 modifier_read_imp(pm, buffer, cb);
1587 png_process_data(pp, pi, buffer, cb);
1588 }
1589
1590 /* Check the invariants at the end (if this fails it's a problem in this
1591 * file!)
1592 */
1593 if (pm->buffer_count > pm->buffer_position ||
1594 pm->this.next != &pm->this.current->data ||
1595 pm->this.readpos < pm->this.current->datacount)
1596 png_error(pp, "progressive read implementation error");
1597}
1598
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001599/* Set up a modifier. */
1600static png_structp
1601set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001602 PNG_CONST char *name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001603{
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001604 /* Do this first so that the modifier fields are cleared even if an error
1605 * happens allocating the png_struct. No allocation is done here so no
1606 * cleanup is required.
1607 */
1608 pm->state = modifier_start;
1609 pm->bit_depth = 0;
1610 pm->colour_type = 255;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001611
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001612 pm->pending_len = 0;
1613 pm->pending_chunk = 0;
1614 pm->flush = 0;
1615 pm->buffer_count = 0;
1616 pm->buffer_position = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001617
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001618 return set_store_for_read(&pm->this, ppi, id, name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001619}
1620
1621/***************************** STANDARD PNG FILES *****************************/
1622/* Standard files - write and save standard files. */
1623/* The standard files are constructed with rows which fit into a 1024 byte row
1624 * buffer. This makes allocation easier below. Further regardless of the file
1625 * format every file has 128 pixels (giving 1024 bytes for 64bpp formats).
1626 *
1627 * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001628 * and with an ID derived from the colour type, bit depth and interlace type
1629 * as above (FILEID).
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001630 */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001631
1632/* The number of passes is related to the interlace type, there's no libpng API
1633 * to determine this so we need an inquiry function:
1634 */
1635static int
1636npasses_from_interlace_type(png_structp pp, int interlace_type)
1637{
1638 switch (interlace_type)
1639 {
1640 default:
1641 png_error(pp, "invalid interlace type");
1642
1643 case PNG_INTERLACE_NONE:
1644 return 1;
1645
1646 case PNG_INTERLACE_ADAM7:
1647 return 7;
1648 }
1649}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001650
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001651#define STD_WIDTH 128U
1652#define STD_ROWMAX (STD_WIDTH*8U)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001653
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001654static unsigned int
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001655bit_size(png_structp pp, png_byte colour_type, png_byte bit_depth)
1656{
1657 switch (colour_type)
1658 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001659 case 0: return bit_depth;
1660
1661 case 2: return 3*bit_depth;
1662
1663 case 3: return bit_depth;
1664
1665 case 4: return 2*bit_depth;
1666
1667 case 6: return 4*bit_depth;
1668
1669 default: png_error(pp, "invalid color type");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001670 }
1671}
1672
1673static size_t
1674standard_rowsize(png_structp pp, png_byte colour_type, png_byte bit_depth)
1675{
1676 return (STD_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8;
1677}
1678
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001679/* standard_wdith(pp, colour_type, bit_depth) current returns the same number
1680 * every time, so just use a macro:
1681 */
1682#define standard_width(pp, colour_type, bit_depth) STD_WIDTH
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001683
1684static png_uint_32
1685standard_height(png_structp pp, png_byte colour_type, png_byte bit_depth)
1686{
1687 switch (bit_size(pp, colour_type, bit_depth))
1688 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001689 case 1:
1690 case 2:
1691 case 4:
1692 return 1; /* Total of 128 pixels */
1693
1694 case 8:
1695 return 2; /* Total of 256 pixels/bytes */
1696
1697 case 16:
1698 return 512; /* Total of 65536 pixels */
1699
1700 case 24:
1701 case 32:
1702 return 512; /* 65536 pixels */
1703
1704 case 48:
1705 case 64:
1706 return 2048;/* 4 x 65536 pixels. */
1707
1708 default:
1709 return 0; /* Error, will be caught later */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001710 }
1711}
1712
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001713/* So the maximum standard image size is: */
1714#define STD_IMAGEMAX (STD_ROWMAX * 2048)
1715
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001716static void
1717standard_row(png_structp pp, png_byte buffer[STD_ROWMAX], png_byte colour_type,
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001718 png_byte bit_depth, png_uint_32 y)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001719{
1720 png_uint_32 v = y << 7;
1721 png_uint_32 i = 0;
1722
1723 switch (bit_size(pp, colour_type, bit_depth))
1724 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001725 case 1:
1726 while (i<128/8) buffer[i] = v & 0xff, v += 17, ++i;
1727 return;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001728
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001729 case 2:
1730 while (i<128/4) buffer[i] = v & 0xff, v += 33, ++i;
1731 return;
1732
1733 case 4:
1734 while (i<128/2) buffer[i] = v & 0xff, v += 65, ++i;
1735 return;
1736
1737 case 8:
1738 /* 256 bytes total, 128 bytes in each row set as follows: */
1739 while (i<128) buffer[i] = v & 0xff, ++v, ++i;
1740 return;
1741
1742 case 16:
1743 /* Generate all 65536 pixel values in order, this includes the 8 bit GA
1744 * case as we as the 16 bit G case.
1745 */
1746 while (i<128)
1747 buffer[2*i] = (v>>8) & 0xff, buffer[2*i+1] = v & 0xff, ++v, ++i;
1748
1749 return;
1750
1751 case 24:
1752 /* 65535 pixels, but rotate the values. */
1753 while (i<128)
1754 {
1755 /* Three bytes per pixel, r, g, b, make b by r^g */
1756 buffer[3*i+0] = (v >> 8) & 0xff;
1757 buffer[3*i+1] = v & 0xff;
1758 buffer[3*i+2] = ((v >> 8) ^ v) & 0xff;
1759 ++v;
1760 ++i;
1761 }
1762
1763 return;
1764
1765 case 32:
1766 /* 65535 pixels, r, g, b, a; just replicate */
1767 while (i<128)
1768 {
1769 buffer[4*i+0] = (v >> 8) & 0xff;
1770 buffer[4*i+1] = v & 0xff;
1771 buffer[4*i+2] = (v >> 8) & 0xff;
1772 buffer[4*i+3] = v & 0xff;
1773 ++v;
1774 ++i;
1775 }
1776
1777 return;
1778
1779 case 48:
1780 /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at
1781 * each pixel, g increase by 257 (0x101) and 'b' by 0x1111:
1782 */
1783 while (i<128)
1784 {
1785 png_uint_32 t = v++;
1786 buffer[6*i+0] = (t >> 8) & 0xff;
1787 buffer[6*i+1] = t & 0xff;
1788 t *= 257;
1789 buffer[6*i+2] = (t >> 8) & 0xff;
1790 buffer[6*i+3] = t & 0xff;
1791 t *= 17;
1792 buffer[6*i+4] = (t >> 8) & 0xff;
1793 buffer[6*i+5] = t & 0xff;
1794 ++i;
1795 }
1796
1797 return;
1798
1799 case 64:
1800 /* As above in the 32 bit case. */
1801 while (i<128)
1802 {
1803 png_uint_32 t = v++;
1804 buffer[8*i+0] = (t >> 8) & 0xff;
1805 buffer[8*i+1] = t & 0xff;
1806 buffer[8*i+4] = (t >> 8) & 0xff;
1807 buffer[8*i+5] = t & 0xff;
1808 t *= 257;
1809 buffer[8*i+2] = (t >> 8) & 0xff;
1810 buffer[8*i+3] = t & 0xff;
1811 buffer[8*i+6] = (t >> 8) & 0xff;
1812 buffer[8*i+7] = t & 0xff;
1813 ++i;
1814 }
1815 return;
1816
1817 default:
1818 break;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001819 }
1820
1821 png_error(pp, "internal error");
1822}
1823
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001824/* This is just to do the right cast - could be changed to a function to check
1825 * 'bd' but there isn't much point.
1826 */
1827#define DEPTH(bd) ((png_byte)(1U << (bd)))
1828
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001829static void
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001830make_standard_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
1831 png_byte PNG_CONST bit_depth, int interlace_type, png_const_charp name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001832{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001833 context(ps, fault);
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001834
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001835 Try
1836 {
1837 png_infop pi;
1838 png_structp pp = set_store_for_write(ps, &pi, name);
1839 png_uint_32 h;
1840
1841 /* In the event of a problem return control to the Catch statement below
1842 * to do the clean up - it is not possible to 'return' directly from a Try
1843 * block.
1844 */
1845 if (pp == NULL)
1846 Throw ps;
1847
1848 h = standard_height(pp, colour_type, bit_depth);
1849
1850 png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth), h,
1851 bit_depth, colour_type, interlace_type,
1852 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1853
1854 if (colour_type == 3) /* palette */
1855 {
1856 unsigned int i = 0;
1857 png_color pal[256];
1858
1859 do
1860 pal[i].red = pal[i].green = pal[i].blue = (png_byte)i;
1861 while(++i < 256U);
1862
1863 png_set_PLTE(pp, pi, pal, 256);
1864 }
1865
1866 png_write_info(pp, pi);
1867
1868 if (png_get_rowbytes(pp, pi) !=
1869 standard_rowsize(pp, colour_type, bit_depth))
1870 png_error(pp, "row size incorrect");
1871
1872 else
1873 {
1874 /* Somewhat confusingly this must be called *after* png_write_info
1875 * because, if it is called before, the information in *pp has not been
1876 * updated to reflect the interlaced image.
1877 */
1878 int npasses = png_set_interlace_handling(pp);
1879 int pass;
1880
1881 if (npasses != npasses_from_interlace_type(pp, interlace_type))
1882 png_error(pp, "write: png_set_interlace_handling failed");
1883
1884 for (pass=1; pass<=npasses; ++pass)
1885 {
1886 png_uint_32 y;
1887
1888 for (y=0; y<h; ++y)
1889 {
1890 png_byte buffer[STD_ROWMAX];
1891
1892 standard_row(pp, buffer, colour_type, bit_depth, y);
1893 png_write_row(pp, buffer);
1894 }
1895 }
1896 }
1897
1898 png_write_end(pp, pi);
1899
1900 /* And store this under the appropriate id, then clean up. */
1901 store_storefile(ps, FILEID(colour_type, bit_depth, interlace_type));
1902
1903 store_write_reset(ps);
1904 }
1905
1906 Catch(fault)
1907 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05001908 /* Use the png_store retuned by the exception, this may help the compiler
1909 * because 'ps' is not used in this branch of the setjmp. Note that fault
1910 * and ps will always be the same value.
1911 */
1912 store_write_reset(fault);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001913 }
1914}
1915
1916static void
1917make_standard(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, int bdlo,
1918 int PNG_CONST bdhi)
1919{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001920 for (; bdlo <= bdhi; ++bdlo)
1921 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001922 int interlace_type;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001923
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001924 for (interlace_type = PNG_INTERLACE_NONE;
1925 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001926 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001927 char name[FILE_NAME_SIZE];
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001928
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001929 standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type);
1930 make_standard_image(ps, colour_type, DEPTH(bdlo), interlace_type,
1931 name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001932 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001933 }
1934}
1935
1936static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001937make_standard_images(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001938{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001939 /* This is in case of errors. */
1940 safecat(ps->test, sizeof ps->test, 0, "make standard images");
1941
1942 /* Arguments are colour_type, low bit depth, high bit depth
1943 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05001944 make_standard(ps, 0, 0, WRITE_BDHI);
1945 make_standard(ps, 2, 3, WRITE_BDHI);
1946 make_standard(ps, 3, 0, 3/*palette: max 8 bits*/);
1947 make_standard(ps, 4, 3, WRITE_BDHI);
1948 make_standard(ps, 6, 3, WRITE_BDHI);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001949}
1950
1951/* Tests - individual test cases */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001952/* Like 'make_standard' but errors are deliberately introduced into the calls
1953 * to ensure that they get detected - it should not be possible to write an
1954 * invalid image with libpng!
1955 */
1956static void
1957sBIT0_error_fn(png_structp pp, png_infop pi)
1958{
1959 /* 0 is invalid... */
1960 png_color_8 bad;
1961 bad.red = bad.green = bad.blue = bad.gray = bad.alpha = 0;
1962 png_set_sBIT(pp, pi, &bad);
1963}
1964
1965static void
1966sBIT_error_fn(png_structp pp, png_infop pi)
1967{
1968 png_byte bit_depth;
1969 png_color_8 bad;
1970
1971 if (png_get_color_type(pp, pi) == PNG_COLOR_TYPE_PALETTE)
1972 bit_depth = 8;
1973
1974 else
1975 bit_depth = png_get_bit_depth(pp, pi);
1976
1977 /* Now we know the bit depth we can easily generate an invalid sBIT entry */
1978 bad.red = bad.green = bad.blue = bad.gray = bad.alpha =
1979 (png_byte)(bit_depth+1);
1980 png_set_sBIT(pp, pi, &bad);
1981}
1982
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001983static PNG_CONST struct
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001984{
1985 void (*fn)(png_structp, png_infop);
1986 PNG_CONST char *msg;
1987 unsigned int warning :1; /* the error is a warning... */
1988} error_test[] =
1989 {
1990 { sBIT0_error_fn, "sBIT(0): failed to detect error", 1 },
1991 { sBIT_error_fn, "sBIT(too big): failed to detect error", 1 },
1992 };
1993
1994static void
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05001995make_error(png_store* ps, png_byte PNG_CONST colour_type, png_byte bit_depth,
1996 int interlace_type, int test, png_const_charp name)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001997{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001998 context(ps, fault);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001999
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002000 Try
2001 {
2002 png_structp pp;
2003 png_infop pi;
2004
2005 pp = set_store_for_write(ps, &pi, name);
2006
2007 if (pp == NULL)
2008 Throw ps;
2009
2010 png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth),
2011 standard_height(pp, colour_type, bit_depth), bit_depth, colour_type,
2012 interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2013
2014 if (colour_type == 3) /* palette */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002015 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002016 unsigned int i = 0;
2017 png_color pal[256];
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002018
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002019 do
2020 pal[i].red = pal[i].green = pal[i].blue = (png_byte)i;
2021 while(++i < 256U);
2022
2023 png_set_PLTE(pp, pi, pal, 256);
2024 }
2025
2026 /* Time for a few errors, these are in various optional chunks, the
2027 * standard tests test the standard chunks pretty well.
2028 */
2029 Try
2030 {
2031 /* Expect this to throw: */
2032 ps->expect_error = !error_test[test].warning;
2033 ps->expect_warning = error_test[test].warning;
2034 ps->saw_warning = 0;
2035 error_test[test].fn(pp, pi);
2036
2037 /* Normally the error is only detected here: */
2038 png_write_info(pp, pi);
2039
2040 /* And handle the case where it was only a warning: */
2041 if (ps->expect_warning && ps->saw_warning)
2042 Throw ps;
2043
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002044 /* If we get here there is a problem, we have success - no error or
2045 * no warning - when we shouldn't have success. Log an error.
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002046 */
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002047 store_log(ps, pp, error_test[test].msg, 1/*error*/);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002048 }
2049
2050 Catch (fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002051 ps = fault; /* expected exit, make sure ps is not clobbered */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002052
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002053 /* And clear these flags */
2054 ps->expect_error = 0;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002055 ps->expect_warning = 0;
2056
2057 /* Now write the whole image, just to make sure that the detected, or
2058 * undetected, errro has not created problems inside libpng.
2059 */
2060 if (png_get_rowbytes(pp, pi) !=
2061 standard_rowsize(pp, colour_type, bit_depth))
2062 png_error(pp, "row size incorrect");
2063
2064 else
2065 {
2066 png_uint_32 h = standard_height(pp, colour_type, bit_depth);
2067 int npasses = png_set_interlace_handling(pp);
2068 int pass;
2069
2070 if (npasses != npasses_from_interlace_type(pp, interlace_type))
2071 png_error(pp, "write: png_set_interlace_handling failed");
2072
2073 for (pass=1; pass<=npasses; ++pass)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002074 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002075 png_uint_32 y;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002076
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002077 for (y=0; y<h; ++y)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002078 {
2079 png_byte buffer[STD_ROWMAX];
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002080
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002081 standard_row(pp, buffer, colour_type, bit_depth, y);
2082 png_write_row(pp, buffer);
2083 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002084 }
2085 }
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002086
2087 png_write_end(pp, pi);
2088
2089 /* The following deletes the file that was just written. */
2090 store_write_reset(ps);
2091 }
2092
2093 Catch(fault)
2094 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002095 store_write_reset(fault);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002096 }
2097}
2098
2099static int
2100make_errors(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
2101 int bdlo, int PNG_CONST bdhi)
2102{
2103 for (; bdlo <= bdhi; ++bdlo)
2104 {
2105 int interlace_type;
2106
2107 for (interlace_type = PNG_INTERLACE_NONE;
2108 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
2109 {
2110 unsigned int test;
2111 char name[FILE_NAME_SIZE];
2112
2113 standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type);
2114
2115 for (test=0; test<(sizeof error_test)/(sizeof error_test[0]); ++test)
2116 {
2117 make_error(&pm->this, colour_type, DEPTH(bdlo), interlace_type,
2118 test, name);
2119
2120 if (fail(pm))
2121 return 0;
2122 }
2123 }
2124 }
2125
2126 return 1; /* keep going */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002127}
2128
2129static void
2130perform_error_test(png_modifier *pm)
2131{
2132 /* Need to do this here because we just write in this test. */
2133 safecat(pm->this.test, sizeof pm->this.test, 0, "error test");
2134
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002135 if (!make_errors(pm, 0, 0, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002136 return;
2137
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002138 if (!make_errors(pm, 2, 3, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002139 return;
2140
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002141 if (!make_errors(pm, 3, 0, 3))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002142 return;
2143
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002144 if (!make_errors(pm, 4, 3, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002145 return;
2146
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002147 if (!make_errors(pm, 6, 3, WRITE_BDHI))
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002148 return;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002149}
2150
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002151/* Because we want to use the same code in both the progressive reader and the
2152 * sequential reader it is necessary to deal with the fact that the progressive
2153 * reader callbacks only have one parameter (png_get_progressive_ptr()), so this
2154 * must contain all the test parameters and all the local variables directly
2155 * accessible to the sequential reader implementation.
2156 *
2157 * The techinque adopted is to reinvent part of what Dijkstra termed a
2158 * 'display'; an array of pointers to the stack frames of enclosing functions so
2159 * that a nested function definition can access the local (C auto) variables of
2160 * the functions that contain its definition. In fact C provides the first
2161 * pointer (the local variables - the stack frame pointer) and the last (the
2162 * global variables - the BCPL global vector typically implemented as global
2163 * addresses), this code requires one more pointer to make the display - the
2164 * local variables (and function call parameters) of the function that actually
2165 * invokes either the progressive or sequential reader.
2166 *
2167 * Perhaps confusingly this technique is confounded with classes - the
2168 * 'standard_display' defined here is sub-classed as the 'gamma_display' below.
2169 * A gamma_display is a standard_display, taking advantage of the ANSI-C
2170 * requirement that the pointer to the first member of a structure must be the
2171 * same as the pointer to the structure. This allows us to reuse standard_
2172 * functions in the gamma test code; something that could not be done with
2173 * nested funtions!
2174 */
2175typedef struct standard_display
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002176{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002177 png_store* ps; /* Test parameters (passed to the function) */
2178 png_byte colour_type;
2179 png_byte bit_depth;
2180 int interlace_type;
2181 png_uint_32 id; /* Calculated file ID */
2182 png_uint_32 w; /* Width of image */
2183 png_uint_32 h; /* Height of image */
2184 int npasses; /* Number of interlaced passes */
2185 size_t cbRow; /* Bytes in a row of the output image. */
2186} standard_display;
2187
2188static void
2189standard_display_init(standard_display *dp, png_store* ps, png_byte colour_type,
2190 png_byte bit_depth, int interlace_type)
2191{
2192 dp->ps = ps;
2193 dp->colour_type = colour_type;
2194 dp->bit_depth = bit_depth;
2195 dp->interlace_type = interlace_type;
2196 dp->id = FILEID(colour_type, bit_depth, interlace_type);
2197 dp->w = 0;
2198 dp->h = 0;
2199 dp->npasses = 0;
2200 dp->cbRow = 0;
2201}
2202
2203/* By passing a 'standard_display' the progressive callbacks can be used
2204 * directly by the sequential code, the functions suffixed _imp are the
2205 * implementations, the functions without the suffix are the callbacks.
2206 *
2207 * The code for the info callback is split into two because this callback calls
2208 * png_read_update_info or png_start_read_image and what gets called depends on
2209 * whether the info needs updating (we want to test both calls in pngvalid.)
2210 */
2211static void
2212standard_info_part1(standard_display *dp, png_structp pp, png_infop pi)
2213{
2214 if (png_get_bit_depth(pp, pi) != dp->bit_depth)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002215 png_error(pp, "validate: bit depth changed");
2216
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002217 if (png_get_color_type(pp, pi) != dp->colour_type)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002218 png_error(pp, "validate: color type changed");
2219
2220 if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE)
2221 png_error(pp, "validate: filter type changed");
2222
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002223 if (png_get_interlace_type(pp, pi) != dp->interlace_type)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002224 png_error(pp, "validate: interlacing changed");
2225
2226 if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE)
2227 png_error(pp, "validate: compression type changed");
2228
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002229 dp->w = png_get_image_width(pp, pi);
2230
2231 if (dp->w != standard_width(pp, dp->colour_type, dp->bit_depth))
2232 png_error(pp, "validate: image width changed");
2233
2234 dp->h = png_get_image_height(pp, pi);
2235
2236 if (dp->h != standard_height(pp, dp->colour_type, dp->bit_depth))
2237 png_error(pp, "validate: image height changed");
2238
2239 /* Important: this is validating the value *before* any transforms have been
2240 * put in place. It doesn't matter for the standard tests, where there are
2241 * no transforms, it does for other tests where rowbytes may change after
2242 * png_read_update_info.
2243 */
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002244 if (png_get_rowbytes(pp, pi) !=
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002245 standard_rowsize(pp, dp->colour_type, dp->bit_depth))
2246 png_error(pp, "validate: row size changed");
2247
2248 if (dp->colour_type == 3) /* palette */
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002249 {
2250 png_colorp pal;
2251 int num;
2252
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002253 /* This could be passed in but isn't - the values set above when the
2254 * standard images were made are just repeated here.
2255 */
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002256 if (png_get_PLTE(pp, pi, &pal, &num) & PNG_INFO_PLTE)
2257 {
2258 int i;
2259
2260 if (num != 256)
2261 png_error(pp, "validate: color type 3 PLTE chunk size changed");
2262
2263 for (i=0; i<num; ++i)
2264 if (pal[i].red != i || pal[i].green != i || pal[i].blue != i)
2265 png_error(pp, "validate: color type 3 PLTE chunk changed");
2266 }
2267
2268 else
2269 png_error(pp, "validate: missing PLTE with color type 3");
2270 }
2271
2272 /* Read the number of passes - expected to match the value used when
2273 * creating the image (interlaced or not). This has the side effect of
2274 * turning ono interlace handling.
2275 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002276 dp->npasses = png_set_interlace_handling(pp);
2277
2278 if (dp->npasses != npasses_from_interlace_type(pp, dp->interlace_type))
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002279 png_error(pp, "validate: file changed interlace type");
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002280
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002281 /* Caller calls png_read_update_info or png_start_read_image now, then calls
2282 * part2.
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002283 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002284}
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002285
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002286/* This must be called *after* the png_read_update_info call to get the correct
2287 * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed
2288 * image.
2289 */
2290static void
2291standard_info_part2(standard_display *dp, png_structp pp, png_infop pi,
2292 int nImages)
2293{
2294 /* Record cbRow now that it can be found. */
2295 dp->cbRow = png_get_rowbytes(pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002296
2297 /* Then ensure there is enough space for the output image(s). */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002298 store_ensure_image(dp->ps, pp, nImages * dp->cbRow * dp->h);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002299}
2300
2301static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002302standard_info_imp(standard_display *dp, png_structp pp, png_infop pi,
2303 int nImages)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002304{
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002305 /* Note that the validation routine has the side effect of turning on
2306 * interlace handling in the subsequent code.
2307 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002308 standard_info_part1(dp, pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002309
2310 /* And the info callback has to call this (or png_read_update_info - see
2311 * below in the png_modifier code for that variant.
2312 */
2313 png_start_read_image(pp);
2314
2315 /* Validate the height, width and rowbytes plus ensure that sufficient buffer
2316 * exists for decoding the image.
2317 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002318 standard_info_part2(dp, pp, pi, nImages);
2319}
2320
2321static void
2322standard_info(png_structp pp, png_infop pi)
2323{
2324 standard_display *dp = png_get_progressive_ptr(pp);
2325
2326 /* Call with nImages==1 because the progressive reader can only produce one
2327 * image.
2328 */
2329 standard_info_imp(dp, pp, pi, 1/*only one image*/);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002330}
2331
2332static void
2333progressive_row(png_structp pp, png_bytep new_row, png_uint_32 y, int pass)
2334{
2335 UNUSED(pass);
2336
2337 /* When handling interlacing some rows will be absent in each pass, the
2338 * callback still gets called, but with a NULL pointer. We need our own
2339 * 'cbRow', but we can't call png_get_rowbytes because we got no info
2340 * structure.
2341 */
2342 if (new_row != NULL)
2343 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002344 PNG_CONST standard_display *dp = png_get_progressive_ptr(pp);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002345
2346 /* Combine the new row into the old: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002347 png_progressive_combine_row(pp, dp->ps->image + y * dp->cbRow, new_row);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002348 }
2349}
2350
2351static void
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002352sequential_row(standard_display *dp, png_structp pp, png_infop pi,
2353 PNG_CONST png_bytep pImage, PNG_CONST png_bytep pDisplay)
2354{
2355 PNG_CONST int npasses = dp->npasses;
2356 PNG_CONST png_uint_32 h = dp->h;
2357 PNG_CONST size_t cbRow = dp->cbRow;
2358 int pass;
2359
2360 for (pass=1; pass <= npasses; ++pass)
2361 {
2362 png_uint_32 y;
2363 png_bytep pRow1 = pImage;
2364 png_bytep pRow2 = pDisplay;
2365
2366 for (y=0; y<h; ++y)
2367 {
2368 png_read_row(pp, pRow1, pRow2);
2369
2370 if (pRow1 != NULL)
2371 pRow1 += cbRow;
2372
2373 if (pRow2 != NULL)
2374 pRow2 += cbRow;
2375 }
2376 }
2377
2378 /* And finish the read operation (only really necessary if the caller wants
2379 * to find additional data in png_info from chunks after the last IDAT.)
2380 */
2381 png_read_end(pp, pi);
2382}
2383
2384static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002385standard_row_validate(standard_display *dp, png_structp pp, png_const_bytep row,
2386 png_const_bytep display, png_uint_32 y)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002387{
2388 png_byte std[STD_ROWMAX];
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002389 standard_row(pp, std, dp->colour_type, dp->bit_depth, y);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002390
2391 /* At the end both the 'read' and 'display' arrays should end up identical.
2392 * In earlier passes 'read' will be narrow, containing only the columns that
2393 * were read, and display will be full width but populated with garbage where
2394 * pixels have not been filled in.
2395 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002396 if (row != NULL && memcmp(std, row, dp->cbRow) != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002397 {
2398 char msg[64];
2399 sprintf(msg, "PNG image row %d changed", y);
2400 png_error(pp, msg);
2401 }
2402
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002403 if (display != NULL && memcmp(std, display, dp->cbRow) != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002404 {
2405 char msg[64];
2406 sprintf(msg, "display row %d changed", y);
2407 png_error(pp, msg);
2408 }
2409}
2410
2411static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002412standard_image_validate(standard_display *dp, png_structp pp,
2413 png_const_bytep pImage, png_const_bytep pDisplay)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002414{
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002415 png_uint_32 y;
2416
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002417 for (y=0; y<dp->h; ++y)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002418 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002419 standard_row_validate(dp, pp, pImage, pDisplay, y);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002420
2421 if (pImage != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002422 pImage += dp->cbRow;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002423
2424 if (pDisplay != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002425 pDisplay += dp->cbRow;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002426 }
2427
2428 /* This avoids false positives if the validation code is never called! */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002429 dp->ps->validated = 1;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002430}
2431
2432static void
2433standard_end(png_structp pp, png_infop pi)
2434{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002435 standard_display *dp = png_get_progressive_ptr(pp);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002436
2437 UNUSED(pi);
2438
2439 /* Validate the image - progressive reading only produces one variant for
2440 * interlaced images.
2441 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002442 standard_image_validate(dp, pp, dp->ps->image, NULL);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002443}
2444
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002445/* A single test run checking the standard image to ensure it is not damaged. */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002446static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002447standard_test(png_store* PNG_CONST psIn, png_byte PNG_CONST colour_typeIn,
2448 png_byte PNG_CONST bit_depthIn, int interlace_typeIn)
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002449{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002450 standard_display d;
2451 context(psIn, fault);
2452
2453 /* Set up the display (stack frame) variables from the arguments to the
2454 * function and initialize the locals that are filled in later.
2455 */
2456 standard_display_init(&d, psIn, colour_typeIn, bit_depthIn,
2457 interlace_typeIn);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002458
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002459 /* Everything is protected by a Try/Catch. The functions called also
2460 * typically have local Try/Catch blocks.
2461 */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002462 Try
2463 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002464 png_structp pp;
2465 png_infop pi;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002466
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05002467 /* Get a png_struct for writing the image, this will throw an error if it
2468 * fails, so we don't need to check the result.
2469 */
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002470 pp = set_store_for_read(d.ps, &pi, d.id,
2471 d.ps->progressive ? "progressive reader" : "sequential reader");
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002472
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002473 /* Introduce the correct read function. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002474 if (d.ps->progressive)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002475 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002476 png_set_progressive_read_fn(pp, &d, standard_info, progressive_row,
2477 standard_end);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002478
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002479 /* Now feed data into the reader until we reach the end: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002480 store_progressive_read(d.ps, pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002481 }
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002482 else
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002483 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002484 /* Note that this takes the store, not the display. */
2485 png_set_read_fn(pp, d.ps, store_read);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002486
2487 /* Check the header values: */
2488 png_read_info(pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002489
2490 /* The code tests both versions of the images that the sequential
2491 * reader can produce.
2492 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002493 standard_info_imp(&d, pp, pi, 2/*images*/);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002494
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002495 /* Need the total bytes in the image below; we can't get to this point
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002496 * unless the PNG file values have been checked against the expected
2497 * values.
2498 */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002499 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002500 PNG_CONST png_bytep pImage = d.ps->image;
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002501 PNG_CONST png_bytep pDisplay = pImage + d.cbRow * d.h;
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002502
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002503 sequential_row(&d, pp, pi, pImage, pDisplay);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002504
2505 /* After the last pass loop over the rows again to check that the
2506 * image is correct.
2507 */
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002508 standard_image_validate(&d, pp, pImage, pDisplay);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002509 }
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002510 }
2511
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002512 /* Check for validation. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002513 if (!d.ps->validated)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002514 png_error(pp, "image read failed silently");
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002515
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002516 /* Successful completion. */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002517 }
2518
2519 Catch(fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002520 d.ps = fault; /* make sure this hasn't been clobbered. */
2521
2522 /* In either case clean up the store. */
2523 store_read_reset(d.ps);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002524}
2525
2526static int
2527test_standard(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002528 int bdlo, int PNG_CONST bdhi)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002529{
2530 for (; bdlo <= bdhi; ++bdlo)
2531 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002532 int interlace_type;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002533
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002534 for (interlace_type = PNG_INTERLACE_NONE;
2535 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
2536 {
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002537 /* Test both sequential and standard readers here. */
2538 pm->this.progressive = !pm->this.progressive;
2539 standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type);
2540
2541 if (fail(pm))
2542 return 0;
2543
2544 pm->this.progressive = !pm->this.progressive;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002545 standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type);
2546
2547 if (fail(pm))
2548 return 0;
2549 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002550 }
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002551
2552 return 1; /*keep going*/
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002553}
2554
2555static void
2556perform_standard_test(png_modifier *pm)
2557{
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002558 /* Test each colour type over the valid range of bit depths (expressed as
2559 * log2(bit_depth) in turn, stop as soon as any error is detected.
2560 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002561 if (!test_standard(pm, 0, 0, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002562 return;
2563
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002564 if (!test_standard(pm, 2, 3, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002565 return;
2566
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002567 if (!test_standard(pm, 3, 0, 3))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002568 return;
2569
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002570 if (!test_standard(pm, 4, 3, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002571 return;
2572
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002573 if (!test_standard(pm, 6, 3, READ_BDHI))
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002574 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002575}
2576
2577
2578/********************************* GAMMA TESTS ********************************/
2579/* Gamma test images. */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002580typedef struct gamma_modification
2581{
2582 png_modification this;
2583 png_fixed_point gamma;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002584} gamma_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002585
2586static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002587gamma_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002588{
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05002589 UNUSED(add);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002590 /* This simply dumps the given gamma value into the buffer. */
2591 png_save_uint_32(pm->buffer, 4);
2592 png_save_uint_32(pm->buffer+4, CHUNK_gAMA);
2593 png_save_uint_32(pm->buffer+8, ((gamma_modification*)me)->gamma);
2594 return 1;
2595}
2596
2597static void
2598gamma_modification_init(gamma_modification *me, png_modifier *pm, double gamma)
2599{
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05002600 double g;
2601
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002602 modification_init(&me->this);
2603 me->this.chunk = CHUNK_gAMA;
2604 me->this.modify_fn = gamma_modify;
2605 me->this.add = CHUNK_PLTE;
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05002606 g = floor(gamma * 100000 + .5);
2607 me->gamma = (png_fixed_point)g;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002608 me->this.next = pm->modifications;
2609 pm->modifications = &me->this;
2610}
2611
2612typedef struct srgb_modification
2613{
2614 png_modification this;
2615 png_byte intent;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002616} srgb_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002617
2618static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002619srgb_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002620{
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05002621 UNUSED(add);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002622 /* As above, ignore add and just make a new chunk */
2623 png_save_uint_32(pm->buffer, 1);
2624 png_save_uint_32(pm->buffer+4, CHUNK_sRGB);
2625 pm->buffer[8] = ((srgb_modification*)me)->intent;
2626 return 1;
2627}
2628
2629static void
2630srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent)
2631{
2632 modification_init(&me->this);
2633 me->this.chunk = CHUNK_sBIT;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002634
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002635 if (intent <= 3) /* if valid, else *delete* sRGB chunks */
2636 {
2637 me->this.modify_fn = srgb_modify;
2638 me->this.add = CHUNK_PLTE;
2639 me->intent = intent;
2640 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002641
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002642 else
2643 {
2644 me->this.modify_fn = 0;
2645 me->this.add = 0;
2646 me->intent = 0;
2647 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002648
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002649 me->this.next = pm->modifications;
2650 pm->modifications = &me->this;
2651}
2652
2653typedef struct sbit_modification
2654{
2655 png_modification this;
2656 png_byte sbit;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002657} sbit_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002658
2659static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002660sbit_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002661{
2662 png_byte sbit = ((sbit_modification*)me)->sbit;
2663 if (pm->bit_depth > sbit)
2664 {
2665 int cb = 0;
2666 switch (pm->colour_type)
2667 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002668 case 0:
2669 cb = 1;
2670 break;
2671
2672 case 2:
2673 case 3:
2674 cb = 3;
2675 break;
2676
2677 case 4:
2678 cb = 2;
2679 break;
2680
2681 case 6:
2682 cb = 4;
2683 break;
2684
2685 default:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002686 png_error(pm->this.pread,
2687 "unexpected colour type in sBIT modification");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002688 }
2689
2690 png_save_uint_32(pm->buffer, cb);
2691 png_save_uint_32(pm->buffer+4, CHUNK_sBIT);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002692
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002693 while (cb > 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05002694 (pm->buffer+8)[--cb] = sbit;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002695
2696 return 1;
2697 }
2698 else if (!add)
2699 {
2700 /* Remove the sBIT chunk */
2701 pm->buffer_count = pm->buffer_position = 0;
2702 return 1;
2703 }
2704 else
2705 return 0; /* do nothing */
2706}
2707
2708static void
2709sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit)
2710{
2711 modification_init(&me->this);
2712 me->this.chunk = CHUNK_sBIT;
2713 me->this.modify_fn = sbit_modify;
2714 me->this.add = CHUNK_PLTE;
2715 me->sbit = sbit;
2716 me->this.next = pm->modifications;
2717 pm->modifications = &me->this;
2718}
2719
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002720/* Reader callbacks and implementations, where they differ from the standard
2721 * ones.
2722 */
2723typedef struct gamma_display
2724{
2725 standard_display this;
2726
2727 /* Parameters */
2728 png_modifier* pm;
2729 double file_gamma;
2730 double screen_gamma;
2731 png_byte sbit;
2732 int threshold_test;
2733 PNG_CONST char* name;
2734 int speed;
2735 int use_input_precision;
2736 int strip16;
2737
2738 /* Local variables */
2739 double maxerrout;
2740 double maxerrpc;
2741 double maxerrabs;
2742} gamma_display;
2743
2744static void
2745gamma_display_init(gamma_display *dp, png_modifier *pm, png_byte colour_type,
2746 png_byte bit_depth, int interlace_type, double file_gamma,
2747 double screen_gamma, png_byte sbit, int threshold_test, int speed,
2748 int use_input_precision, int strip16)
2749{
2750 /* Standard fields */
2751 standard_display_init(&dp->this, &pm->this, colour_type, bit_depth,
2752 interlace_type);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002753
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002754 /* Parameter fields */
2755 dp->pm = pm;
2756 dp->file_gamma = file_gamma;
2757 dp->screen_gamma = screen_gamma;
2758 dp->sbit = sbit;
2759 dp->threshold_test = threshold_test;
2760 dp->speed = speed;
2761 dp->use_input_precision = use_input_precision;
2762 dp->strip16 = strip16;
2763
2764 /* Local variable fields */
2765 dp->maxerrout = dp->maxerrpc = dp->maxerrabs = 0;
2766}
2767
2768static void
2769gamma_info_imp(gamma_display *dp, png_structp pp, png_infop pi)
2770{
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002771 /* Reuse the standard stuff as appropriate. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002772 standard_info_part1(&dp->this, pp, pi);
2773
2774 /* If requested strip 16 to 8 bits - this is handled automagically below
2775 * because the output bit depth is read from the library. Note that there
2776 * are interactions with sBIT but, internally, libpng makes sbit at most
2777 * PNG_MAX_GAMMA_8 when doing the following.
2778 */
2779 if (dp->strip16)
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05002780# ifdef PNG_READ_16_TO_8
2781 png_set_strip_16(pp);
2782# else
2783 png_error(pp, "strip16 (16 to 8 bit conversion) not supported");
2784# endif
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002785
2786 png_read_update_info(pp, pi);
2787
2788 /* Now we may get a different cbRow: */
2789 standard_info_part2(&dp->this, pp, pi, 1/*images*/);
2790}
2791
2792static void
2793gamma_info(png_structp pp, png_infop pi)
2794{
2795 gamma_info_imp(png_get_progressive_ptr(pp), pp, pi);
2796}
2797
2798static void
2799gamma_image_validate(gamma_display *dp, png_structp pp, png_infop pi,
2800 png_const_bytep pRow)
2801{
2802 /* Get some constants derived from the input and output file formats: */
2803 PNG_CONST png_byte sbit = dp->sbit;
2804 PNG_CONST double file_gamma = dp->file_gamma;
2805 PNG_CONST double screen_gamma = dp->screen_gamma;
2806 PNG_CONST int use_input_precision = dp->use_input_precision;
2807 PNG_CONST int speed = dp->speed;
2808 PNG_CONST png_byte in_ct = dp->this.colour_type;
2809 PNG_CONST png_byte in_bd = dp->this.bit_depth;
2810 PNG_CONST png_uint_32 w = dp->this.w;
2811 PNG_CONST png_uint_32 h = dp->this.h;
2812 PNG_CONST size_t cbRow = dp->this.cbRow;
2813 PNG_CONST png_byte out_ct = png_get_color_type(pp, pi);
2814 PNG_CONST png_byte out_bd = png_get_bit_depth(pp, pi);
2815 PNG_CONST unsigned int outmax = (1U<<out_bd)-1;
2816 PNG_CONST double maxabs = abserr(dp->pm, out_bd);
2817 PNG_CONST double maxout = outerr(dp->pm, out_bd);
2818 PNG_CONST double maxpc = pcerr(dp->pm, out_bd);
2819
2820 /* There are three sources of error, firstly the quantization in the
2821 * file encoding, determined by sbit and/or the file depth, secondly
2822 * the output (screen) gamma and thirdly the output file encoding.
2823 * Since this API receives the screen and file gamma in double
2824 * precision it is possible to calculate an exact answer given an input
2825 * pixel value. Therefore we assume that the *input* value is exact -
2826 * sample/maxsample - calculate the corresponding gamma corrected
2827 * output to the limits of double precision arithmetic and compare with
2828 * what libpng returns.
2829 *
2830 * Since the library must quantise the output to 8 or 16 bits there is
2831 * a fundamental limit on the accuracy of the output of +/-.5 - this
2832 * quantisation limit is included in addition to the other limits
2833 * specified by the paramaters to the API. (Effectively, add .5
2834 * everywhere.)
2835 *
2836 * The behavior of the 'sbit' paramter is defined by section 12.5
2837 * (sample depth scaling) of the PNG spec. That section forces the
2838 * decoder to assume that the PNG values have been scaled if sBIT is
2839 * presence:
2840 *
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05002841 * png-sample = floor( input-sample * (max-out/max-in) + .5);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002842 *
2843 * This means that only a subset of the possible PNG values should
2844 * appear in the input, however the spec allows the encoder to use a
2845 * variety of approximations to the above and doesn't require any
2846 * restriction of the values produced.
2847 *
2848 * Nevertheless the spec requires that the upper 'sBIT' bits of the
2849 * value stored in a PNG file be the original sample bits.
2850 * Consequently the code below simply scales the top sbit bits by
2851 * (1<<sbit)-1 to obtain an original sample value.
2852 *
2853 * Because there is limited precision in the input it is arguable that
2854 * an acceptable result is any valid result from input-.5 to input+.5.
2855 * The basic tests below do not do this, however if
2856 * 'use_input_precision' is set a subsequent test is performed below.
2857 */
2858 PNG_CONST int processing = (fabs(screen_gamma*file_gamma-1) >=
2859 PNG_GAMMA_THRESHOLD && !dp->threshold_test && !speed && in_ct != 3) ||
2860 in_bd != out_bd;
2861
2862 PNG_CONST unsigned int samples_per_pixel = (out_ct & 2U) ? 3U : 1U;
2863
2864 PNG_CONST double gamma = 1/(file_gamma*screen_gamma); /* Overall */
2865
2866 double maxerrout = 0, maxerrabs = 0, maxerrpc = 0;
2867 png_uint_32 y;
2868
2869 for (y=0; y<h; ++y, pRow += cbRow)
2870 {
2871 unsigned int s, x;
2872 png_byte std[STD_ROWMAX];
2873
2874 standard_row(pp, std, in_ct, in_bd, y);
2875
2876 if (processing)
2877 {
2878 for (x=0; x<w; ++x) for (s=0; s<samples_per_pixel; ++s)
2879 {
2880 /* Input sample values: */
2881 PNG_CONST unsigned int
2882 id = sample(std, in_ct, in_bd, x, s);
2883
2884 PNG_CONST unsigned int
2885 od = sample(pRow, out_ct, out_bd, x, s);
2886
2887 PNG_CONST unsigned int
2888 isbit = id >> (in_bd-sbit);
2889
2890 double i, sample, encoded_sample, output, encoded_error, error;
2891 double es_lo, es_hi;
2892
2893 /* First check on the 'perfect' result obtained from the
2894 * digitized input value, id, and compare this against the
2895 * actual digitized result, 'od'. 'i' is the input result
2896 * in the range 0..1:
2897 *
2898 * NOTE: sBIT should be taken into account here but isn't,
2899 * as described above.
2900 */
2901 i = isbit; i /= (1U<<sbit)-1;
2902
2903 /* Then get the gamma corrected version of 'i' and compare
2904 * to 'od', any error less than .5 is insignificant - just
2905 * quantization of the output value to the nearest digital
2906 * value (nevertheless the error is still recorded - it's
2907 * interesting ;-)
2908 */
2909 encoded_sample = pow(i, gamma) * outmax;
2910 encoded_error = fabs(od-encoded_sample);
2911
2912 if (encoded_error > maxerrout)
2913 maxerrout = encoded_error;
2914
2915 if (encoded_error < .5+maxout)
2916 continue;
2917
2918 /* There may be an error, calculate the actual sample
2919 * values - unencoded light intensity values. Note that
2920 * in practice these are not unencoded because they
2921 * include a 'viewing correction' to decrease or
2922 * (normally) increase the perceptual contrast of the
2923 * image. There's nothing we can do about this - we don't
2924 * know what it is - so assume the unencoded value is
2925 * perceptually linear.
2926 */
2927 sample = pow(i, 1/file_gamma); /* In range 0..1 */
2928 output = od;
2929 output /= outmax;
2930 output = pow(output, screen_gamma);
2931
2932 /* Now we have the numbers for real errors, both absolute
2933 * values as as a percentage of the correct value (output):
2934 */
2935 error = fabs(sample-output);
2936
2937 if (error > maxerrabs)
2938 maxerrabs = error;
2939
2940 /* The following is an attempt to ignore the tendency of
2941 * quantization to dominate the percentage errors for low
2942 * output sample values:
2943 */
2944 if (sample*maxpc > .5+maxabs)
2945 {
2946 double pcerr = error/sample;
2947 if (pcerr > maxerrpc) maxerrpc = pcerr;
2948 }
2949
2950 /* Now calculate the digitization limits for
2951 * 'encoded_sample' using the 'max' values. Note that
2952 * maxout is in the encoded space but maxpc and maxabs are
2953 * in linear light space.
2954 *
2955 * First find the maximum error in linear light space,
2956 * range 0..1:
2957 */
2958 {
2959 double tmp = sample * maxpc;
2960 if (tmp < maxabs) tmp = maxabs;
2961
2962 /* Low bound - the minimum of the three: */
2963 es_lo = encoded_sample - maxout;
2964 if (es_lo > 0 && sample-tmp > 0)
2965 {
2966 double l = outmax * pow(sample-tmp, 1/screen_gamma);
2967 if (l < es_lo) es_lo = l;
2968 }
2969 else
2970 es_lo = 0;
2971
2972 es_hi = encoded_sample + maxout;
2973 if (es_hi < outmax && sample+tmp < 1)
2974 {
2975 double h = outmax * pow(sample+tmp, 1/screen_gamma);
2976 if (h > es_hi) es_hi = h;
2977 }
2978 else
2979 es_hi = outmax;
2980 }
2981
2982 /* The primary test is that the final encoded value
2983 * returned by the library should be between the two limits
2984 * (inclusive) that were calculated above. At this point
2985 * quantization of the output must be taken into account.
2986 */
2987 if (od+.5 < es_lo || od-.5 > es_hi)
2988 {
2989 /* There has been an error in processing. */
2990 double is_lo, is_hi;
2991
2992 if (use_input_precision)
2993 {
2994 /* Ok, something is wrong - this actually happens in
2995 * current libpng sbit processing. Assume that the
2996 * input value (id, adjusted for sbit) can be
2997 * anywhere between value-.5 and value+.5 - quite a
2998 * large range if sbit is low.
2999 */
3000 double tmp = (isbit - .5)/((1U<<sbit)-1);
3001
3002 if (tmp > 0)
3003 {
3004 is_lo = outmax * pow(tmp, gamma) - maxout;
3005 if (is_lo < 0) is_lo = 0;
3006 }
3007
3008 else
3009 is_lo = 0;
3010
3011 tmp = (isbit + .5)/((1U<<sbit)-1);
3012
3013 if (tmp < 1)
3014 {
3015 is_hi = outmax * pow(tmp, gamma) + maxout;
3016 if (is_hi > outmax) is_hi = outmax;
3017 }
3018
3019 else
3020 is_hi = outmax;
3021
3022 if (!(od+.5 < is_lo || od-.5 > is_hi))
3023 continue;
3024 }
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003025 else
3026 is_lo = es_lo, is_hi = es_hi;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003027
3028 {
3029 char msg[256];
3030 sprintf(msg,
3031 "error: %.3f; %u{%u;%u} -> %u not %.2f (%.1f-%.1f)",
3032 od-encoded_sample, id, sbit, isbit, od,
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003033 encoded_sample, is_lo, is_hi);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003034 png_warning(pp, msg);
3035 }
3036 }
3037 }
3038 }
3039
3040 else if (!speed && memcmp(std, pRow, cbRow) != 0)
3041 {
3042 char msg[64];
3043 /* No transform is expected on the threshold tests. */
3044 sprintf(msg, "gamma: below threshold row %d changed", y);
3045 png_error(pp, msg);
3046 }
3047 } /* row (y) loop */
3048
3049 dp->maxerrout = maxerrout;
3050 dp->maxerrabs = maxerrabs;
3051 dp->maxerrpc = maxerrpc;
3052 dp->this.ps->validated = 1;
3053}
3054
3055static void
3056gamma_end(png_structp pp, png_infop pi)
3057{
3058 gamma_display *dp = png_get_progressive_ptr(pp);
3059
3060 gamma_image_validate(dp, pp, pi, dp->this.ps->image);
3061}
3062
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05003063/* A single test run checking a gamma transformation.
3064 *
3065 * maxabs: maximum absolute error as a fraction
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003066 * maxout: maximum output error in the output units
3067 * maxpc: maximum percentage error (as a percentage)
3068 */
3069static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003070gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn,
3071 PNG_CONST png_byte bit_depthIn, PNG_CONST int interlace_typeIn,
3072 PNG_CONST double file_gammaIn, PNG_CONST double screen_gammaIn,
3073 PNG_CONST png_byte sbitIn, PNG_CONST int threshold_testIn,
3074 PNG_CONST char *name, PNG_CONST int speedIn,
3075 PNG_CONST int use_input_precisionIn, PNG_CONST int strip16In)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003076{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003077 gamma_display d;
3078 context(&pmIn->this, fault);
3079
3080 gamma_display_init(&d, pmIn, colour_typeIn, bit_depthIn, interlace_typeIn,
3081 file_gammaIn, screen_gammaIn, sbitIn, threshold_testIn, speedIn,
3082 use_input_precisionIn, strip16In);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003083
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003084 Try
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003085 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003086 png_structp pp;
3087 png_infop pi;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003088 gamma_modification gamma_mod;
3089 srgb_modification srgb_mod;
3090 sbit_modification sbit_mod;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003091
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003092 /* Make an appropriate modifier to set the PNG file gamma to the
3093 * given gamma value and the sBIT chunk to the given precision.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003094 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003095 d.pm->modifications = NULL;
3096 gamma_modification_init(&gamma_mod, d.pm, d.file_gamma);
3097 srgb_modification_init(&srgb_mod, d.pm, 127/*delete*/);
3098 sbit_modification_init(&sbit_mod, d.pm, d.sbit);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003099
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003100 modification_reset(d.pm->modifications);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003101
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05003102 /* Get a png_struct for writing the image. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003103 pp = set_modifier_for_read(d.pm, &pi, d.this.id, name);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05003104
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003105 /* Set up gamma processing. */
Glenn Randers-Pehrson15333cd2010-08-24 15:29:52 -05003106#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003107 png_set_gamma(pp, d.screen_gamma, d.file_gamma);
Glenn Randers-Pehrson15333cd2010-08-24 15:29:52 -05003108#else
3109 {
3110 png_fixed_point s = floor(d.screen_gamma*100000+.5);
3111 png_fixed_point f = floor(d.file_gamma*100000+.5);
3112 png_set_gamma_fixed(pp, s, f);
3113 }
3114#endif
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003115
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003116 /* Introduce the correct read function. */
3117 if (d.pm->this.progressive)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003118 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003119 /* Share the row function with the standard implementation. */
3120 png_set_progressive_read_fn(pp, &d, gamma_info, progressive_row,
3121 gamma_end);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003122
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003123 /* Now feed data into the reader until we reach the end: */
3124 modifier_progressive_read(d.pm, pp, pi);
3125 }
3126 else
3127 {
3128 /* modifier_read expects a png_modifier* */
3129 png_set_read_fn(pp, d.pm, modifier_read);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003130
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003131 /* Check the header values: */
3132 png_read_info(pp, pi);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003133
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003134 /* Process the 'info' requirements. only one image is generated */
3135 gamma_info_imp(&d, pp, pi);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003136
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05003137 sequential_row(&d.this, pp, pi, NULL, d.this.ps->image);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003138
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05003139 gamma_image_validate(&d, pp, pi, d.this.ps->image);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003140 }
3141
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003142 modifier_reset(d.pm);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003143
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003144 if (d.pm->log && !d.threshold_test && !d.speed)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003145 fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n",
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003146 d.this.bit_depth, colour_types[d.this.colour_type], d.name,
3147 d.maxerrout, d.maxerrabs, 100*d.maxerrpc);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003148
3149 /* Log the summary values too. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003150 if (d.this.colour_type == 0 || d.this.colour_type == 4)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003151 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003152 switch (d.this.bit_depth)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003153 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003154 case 1:
3155 break;
3156
3157 case 2:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003158 if (d.maxerrout > d.pm->error_gray_2)
3159 d.pm->error_gray_2 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003160 break;
3161
3162 case 4:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003163 if (d.maxerrout > d.pm->error_gray_4)
3164 d.pm->error_gray_4 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003165 break;
3166
3167 case 8:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003168 if (d.maxerrout > d.pm->error_gray_8)
3169 d.pm->error_gray_8 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003170 break;
3171
3172 case 16:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003173 if (d.maxerrout > d.pm->error_gray_16)
3174 d.pm->error_gray_16 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003175 break;
3176
3177 default:
3178 png_error(pp, "bad bit depth (internal: 1)");
3179 }
3180 }
3181
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003182 else if (d.this.colour_type == 2 || d.this.colour_type == 6)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003183 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003184 switch (d.this.bit_depth)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003185 {
3186 case 8:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003187 if (d.maxerrout > d.pm->error_color_8)
3188 d.pm->error_color_8 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003189
3190 break;
3191
3192 case 16:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003193 if (d.maxerrout > d.pm->error_color_16)
3194 d.pm->error_color_16 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003195
3196 break;
3197
3198 default:
3199 png_error(pp, "bad bit depth (internal: 2)");
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003200 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003201 }
3202 }
3203
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003204 Catch(fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003205 modifier_reset((png_modifier*)fault);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003206}
3207
3208static void gamma_threshold_test(png_modifier *pm, png_byte colour_type,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003209 png_byte bit_depth, int interlace_type, double file_gamma,
3210 double screen_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003211{
3212 size_t pos = 0;
3213 char name[64];
3214 pos = safecat(name, sizeof name, pos, "threshold ");
3215 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
3216 pos = safecat(name, sizeof name, pos, "/");
3217 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
3218
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003219 (void)gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma,
3220 screen_gamma, bit_depth, 1, name, 0/*speed*/, 0/*no input precision*/,
3221 0/*no strip16*/);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003222}
3223
3224static void
3225perform_gamma_threshold_tests(png_modifier *pm)
3226{
3227 png_byte colour_type = 0;
3228 png_byte bit_depth = 0;
3229
3230 while (next_format(&colour_type, &bit_depth))
3231 {
3232 double gamma = 1.0;
3233 while (gamma >= .4)
3234 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003235 /* There's little point testing the interlacing vs non-interlacing,
3236 * but this can be set from the command line.
3237 */
3238 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
3239 gamma, 1/gamma);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003240 gamma *= .95;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003241 }
3242
3243 /* And a special test for sRGB */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003244 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
3245 .45455, 2.2);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003246
3247 if (fail(pm))
3248 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003249 }
3250}
3251
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003252static void gamma_transform_test(png_modifier *pm,
3253 PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003254 PNG_CONST int interlace_type, PNG_CONST double file_gamma,
3255 PNG_CONST double screen_gamma, PNG_CONST png_byte sbit, PNG_CONST int speed,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003256 PNG_CONST int use_input_precision, PNG_CONST int strip16)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003257{
3258 size_t pos = 0;
3259 char name[64];
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003260
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003261 if (sbit != bit_depth)
3262 {
3263 pos = safecat(name, sizeof name, pos, "sbit(");
3264 pos = safecatn(name, sizeof name, pos, sbit);
3265 pos = safecat(name, sizeof name, pos, ") ");
3266 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003267
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003268 else
3269 pos = safecat(name, sizeof name, pos, "gamma ");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003270
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003271 if (strip16)
3272 pos = safecat(name, sizeof name, pos, "16to8 ");
3273 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
3274 pos = safecat(name, sizeof name, pos, "->");
3275 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
3276
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003277 gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma,
3278 screen_gamma, sbit, 0, name, speed, use_input_precision, strip16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003279}
3280
3281static void perform_gamma_transform_tests(png_modifier *pm, int speed)
3282{
3283 png_byte colour_type = 0;
3284 png_byte bit_depth = 0;
3285
3286 /* Ignore palette images - the gamma correction happens on the palette entry,
3287 * haven't got the tests for this yet.
3288 */
3289 while (next_format(&colour_type, &bit_depth)) if (colour_type != 3)
3290 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003291 unsigned int i, j;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003292
3293 for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) if (i != j)
3294 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003295 gamma_transform_test(pm, colour_type, bit_depth, pm->interlace_type,
3296 1/pm->gammas[i], pm->gammas[j], bit_depth, speed,
3297 pm->use_input_precision, 0/*do not strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003298
3299 if (fail(pm))
3300 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003301 }
3302 }
3303}
3304
3305static void perform_gamma_sbit_tests(png_modifier *pm, int speed)
3306{
3307 png_byte sbit;
3308
3309 /* The only interesting cases are colour and grayscale, alpha is ignored here
3310 * for overall speed. Only bit depths 8 and 16 are tested.
3311 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003312 for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003313 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003314 unsigned int i, j;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003315
3316 for (i=0; i<pm->ngammas; ++i)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003317 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003318 for (j=0; j<pm->ngammas; ++j)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003319 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003320 if (i != j)
3321 {
3322 if (sbit < 8)
3323 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003324 gamma_transform_test(pm, 0, 8, pm->interlace_type,
3325 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3326 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003327
3328 if (fail(pm))
3329 return;
3330
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003331 gamma_transform_test(pm, 2, 8, pm->interlace_type,
3332 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3333 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003334
3335 if (fail(pm))
3336 return;
3337 }
3338
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003339#ifdef DO_16BIT
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003340 gamma_transform_test(pm, 0, 16, pm->interlace_type,
3341 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3342 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003343
3344 if (fail(pm))
3345 return;
3346
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003347 gamma_transform_test(pm, 2, 16, pm->interlace_type,
3348 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3349 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003350
3351 if (fail(pm))
3352 return;
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003353#endif
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003354 }
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003355 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003356 }
3357 }
3358}
3359
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003360/* Note that this requires a 16 bit source image but produces 8 bit output, so
3361 * we only need the 16bit write support.
3362 */
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003363#ifdef PNG_16_TO_8_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003364static void perform_gamma_strip16_tests(png_modifier *pm, int speed)
3365{
3366# ifndef PNG_MAX_GAMMA_8
3367# define PNG_MAX_GAMMA_8 11
3368# endif
3369 /* Include the alpha cases here, not that sbit matches the internal value
3370 * used by the library - otherwise we will get spurious errors from the
3371 * internal sbit style approximation.
3372 *
Glenn Randers-Pehrson233357e2010-07-29 21:49:38 -05003373 * The threshold test is here because otherwise the 16 to 8 conversion will
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003374 * proceed *without* gamma correction, and the tests above will fail (but not
3375 * by much) - this could be fixed, it only appears with the -g option.
3376 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003377 unsigned int i, j;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003378 for (i=0; i<pm->ngammas; ++i)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003379 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003380 for (j=0; j<pm->ngammas; ++j)
3381 {
3382 if (i != j &&
3383 fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD)
3384 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003385 gamma_transform_test(pm, 0, 16, pm->interlace_type, 1/pm->gammas[i],
3386 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3387 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003388
3389 if (fail(pm))
3390 return;
3391
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003392 gamma_transform_test(pm, 2, 16, pm->interlace_type, 1/pm->gammas[i],
3393 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3394 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003395
3396 if (fail(pm))
3397 return;
3398
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003399 gamma_transform_test(pm, 4, 16, pm->interlace_type, 1/pm->gammas[i],
3400 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3401 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003402
3403 if (fail(pm))
3404 return;
3405
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003406 gamma_transform_test(pm, 6, 16, pm->interlace_type, 1/pm->gammas[i],
3407 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3408 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003409
3410 if (fail(pm))
3411 return;
3412 }
3413 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003414 }
3415}
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003416#endif /* 16 to 8 bit conversion */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003417
3418static void
3419perform_gamma_test(png_modifier *pm, int speed, int summary)
3420{
3421 /* First some arbitrary no-transform tests: */
3422 if (!speed)
3423 {
3424 perform_gamma_threshold_tests(pm);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003425
3426 if (fail(pm))
3427 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003428 }
3429
3430 /* Now some real transforms. */
3431 perform_gamma_transform_tests(pm, speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003432
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003433 if (summary)
3434 {
3435 printf("Gamma correction error summary (output value error):\n");
3436 printf(" 2 bit gray: %.5f\n", pm->error_gray_2);
3437 printf(" 4 bit gray: %.5f\n", pm->error_gray_4);
3438 printf(" 8 bit gray: %.5f\n", pm->error_gray_8);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003439 printf(" 8 bit color: %.5f\n", pm->error_color_8);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003440#ifdef DO_16BIT
3441 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003442 printf(" 16 bit color: %.5f\n", pm->error_color_16);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003443#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003444 }
3445
3446 /* The sbit tests produce much larger errors: */
3447 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 =
3448 pm->error_color_8 = pm->error_color_16 = 0;
3449 perform_gamma_sbit_tests(pm, speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003450
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003451 if (summary)
3452 {
3453 printf("Gamma correction with sBIT:\n");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003454
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003455 if (pm->sbitlow < 8U)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003456 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003457 printf(" 2 bit gray: %.5f\n", pm->error_gray_2);
3458 printf(" 4 bit gray: %.5f\n", pm->error_gray_4);
3459 printf(" 8 bit gray: %.5f\n", pm->error_gray_8);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003460 printf(" 8 bit color: %.5f\n", pm->error_color_8);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003461 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003462
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003463#ifdef DO_16BIT
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003464 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003465 printf(" 16 bit color: %.5f\n", pm->error_color_16);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003466#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003467 }
3468
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003469#ifdef PNG_16_TO_8_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003470 /* The 16 to 8 bit strip operations: */
3471 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 =
3472 pm->error_color_8 = pm->error_color_16 = 0;
3473 perform_gamma_strip16_tests(pm, speed);
3474 if (summary)
3475 {
3476 printf("Gamma correction with 16 to 8 bit reduction:\n");
3477 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
3478 printf(" 16 bit color: %.5f\n", pm->error_color_16);
3479 }
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003480#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003481}
3482
3483/* main program */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003484int main(int argc, PNG_CONST char **argv)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003485{
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003486 volatile int summary = 1; /* Print the error sumamry at the end */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003487
3488 /* Create the given output file on success: */
3489 PNG_CONST char *volatile touch = NULL;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003490
3491 /* This is an array of standard gamma values (believe it or not I've seen
3492 * every one of these mentioned somewhere.)
3493 *
3494 * In the following list the most useful values are first!
3495 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003496 static double
3497 gammas[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9};
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003498
3499 png_modifier pm;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003500 context(&pm.this, fault);
3501
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003502 modifier_init(&pm);
3503
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003504 /* Preallocate the image buffer, because we know how big it needs to be,
3505 * note that, for testing purposes, it is deliberately mis-aligned.
3506 */
3507 pm.this.image = malloc(2*STD_IMAGEMAX+1);
3508 if (pm.this.image != NULL)
3509 {
3510 /* Ignore OOM at this point - the 'ensure' routine above will allocate the
3511 * array appropriately.
3512 */
3513 ++(pm.this.image);
3514 pm.this.cb_image = 2*STD_IMAGEMAX;
3515 }
3516
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003517 /* Default to error on warning: */
3518 pm.this.treat_warnings_as_errors = 1;
3519
3520 /* Store the test gammas */
3521 pm.gammas = gammas;
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003522 pm.ngammas = 3U; /* for speed */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003523 pm.sbitlow = 8U; /* because libpng doesn't do sBIT below 8! */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003524 pm.use_input_precision_16to8 = 1U; /* Because of the way libpng does it */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003525
3526 /* Some default values (set the behavior for 'make check' here) */
3527 pm.maxout8 = .1; /* Arithmetic error in *encoded* value */
3528 pm.maxabs8 = .00005; /* 1/20000 */
3529 pm.maxpc8 = .499; /* I.e. .499% fractional error */
3530 pm.maxout16 = .499; /* Error in *encoded* value */
3531 pm.maxabs16 = .00005;/* 1/20000 */
3532 /* NOTE: this is a reasonable perceptual limit, we assume that humans can
3533 * perceive light level differences of 1% over a 100:1 range, so we need to
3534 * maintain 1 in 10000 accuracy (in linear light space), this is what the
3535 * following guarantees. It also allows significantly higher errors at
3536 * higher 16 bit values, which is important for performance. The actual
3537 * maximum 16 bit error is about +/-1.9 in the fixed point implementation but
3538 * this is only allowed for values >38149 by the following:
3539 */
3540 pm.maxpc16 = .005; /* I.e. 1/200% - 1/20000 */
3541
3542 /* Now parse the command line options. */
3543 while (--argc >= 1)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003544 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003545 if (strcmp(*++argv, "-v") == 0)
3546 pm.this.verbose = 1;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003547
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003548 else if (strcmp(*argv, "-l") == 0)
3549 pm.log = 1;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003550
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003551 else if (strcmp(*argv, "-q") == 0)
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05003552 summary = pm.this.verbose = pm.log = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003553
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003554 else if (strcmp(*argv, "-g") == 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003555 pm.ngammas = (sizeof gammas)/(sizeof gammas[0]);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003556
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003557 else if (strcmp(*argv, "-w") == 0)
3558 pm.this.treat_warnings_as_errors = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003559
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003560 else if (strcmp(*argv, "--speed") == 0)
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003561 pm.this.speed = 1, pm.ngammas = (sizeof gammas)/(sizeof gammas[0]);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003562
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003563 else if (strcmp(*argv, "--nogamma") == 0)
3564 pm.ngammas = 0;
3565
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05003566 else if (strcmp(*argv, "--progressive-read") == 0)
3567 pm.this.progressive = 1;
3568
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003569 else if (strcmp(*argv, "--interlace") == 0)
3570 pm.interlace_type = PNG_INTERLACE_ADAM7;
3571
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003572 else if (argc >= 1 && strcmp(*argv, "--sbitlow") == 0)
3573 --argc, pm.sbitlow = (png_byte)atoi(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003574
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003575 else if (argc >= 1 && strcmp(*argv, "--touch") == 0)
3576 --argc, touch = *++argv;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003577
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003578 else if (argc >= 1 && strncmp(*argv, "--max", 4) == 0)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003579 {
3580 --argc;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003581
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003582 if (strcmp(4+*argv, "abs8") == 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003583 pm.maxabs8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003584
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003585 else if (strcmp(4+*argv, "abs16") == 0)
3586 pm.maxabs16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003587
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003588 else if (strcmp(4+*argv, "out8") == 0)
3589 pm.maxout8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003590
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003591 else if (strcmp(4+*argv, "out16") == 0)
3592 pm.maxout16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003593
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003594 else if (strcmp(4+*argv, "pc8") == 0)
3595 pm.maxpc8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003596
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003597 else if (strcmp(4+*argv, "pc16") == 0)
3598 pm.maxpc16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003599
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003600 else
3601 {
3602 fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv);
3603 exit(1);
3604 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003605 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003606
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003607 else
3608 {
3609 fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003610 exit(1);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003611 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003612 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003613
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003614 Try
3615 {
3616 /* Make useful base images */
3617 make_standard_images(&pm.this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003618
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003619 /* Perform the standard and gamma tests. */
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003620 if (!pm.this.speed)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003621 {
3622 perform_standard_test(&pm);
3623 perform_error_test(&pm);
3624 }
3625
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003626 perform_gamma_test(&pm, pm.this.speed != 0, summary && !pm.this.speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003627 }
3628
3629 Catch(fault)
3630 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003631 fprintf(stderr, "pngvalid: test aborted (probably failed in cleanup)\n");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003632 if (!pm.this.verbose)
3633 {
3634 if (pm.this.error[0] != 0)
3635 fprintf(stderr, "pngvalid: first error: %s\n", pm.this.error);
3636 fprintf(stderr, "pngvalid: run with -v to see what happened\n");
3637 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003638 exit(1);
3639 }
3640
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003641 if (summary && !pm.this.speed)
3642 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003643 printf("Results using %s point arithmetic %s\n",
3644#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500
3645 "floating",
3646#else
3647 "fixed",
3648#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003649 (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
3650 pm.this.nwarnings)) ? "(errors)" : (pm.this.nwarnings ?
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003651 "(warnings)" : "(no errors or warnings)")
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003652 );
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003653 printf("Allocated memory statistics (in bytes):\n"
Glenn Randers-Pehrson9a75d992010-10-08 16:27:14 -05003654 "\tread %lu maximum single, %lu peak, %lu total\n"
3655 "\twrite %lu maximum single, %lu peak, %lu total\n",
3656 (unsigned long)pm.this.read_memory_pool.max_max,
3657 (unsigned long)pm.this.read_memory_pool.max_limit,
3658 (unsigned long)pm.this.read_memory_pool.max_total,
3659 (unsigned long)pm.this.write_memory_pool.max_max,
3660 (unsigned long)pm.this.write_memory_pool.max_limit,
3661 (unsigned long)pm.this.write_memory_pool.max_total);
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003662 }
3663
3664 /* Do this here to provoke memory corruption errors in memory not directly
3665 * allocated by libpng - not a complete test, but better than nothing.
3666 */
3667 store_delete(&pm.this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003668
3669 /* Error exit if there are any errors, and maybe if there are any
3670 * warnings.
3671 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003672 if (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
3673 pm.this.nwarnings))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003674 {
3675 if (!pm.this.verbose)
3676 fprintf(stderr, "pngvalid: %s\n", pm.this.error);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003677
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003678 fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003679 pm.this.nwarnings);
3680
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003681 exit(1);
3682 }
3683
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003684 /* Success case. */
3685 if (touch != NULL)
3686 {
3687 FILE *fsuccess = fopen(touch, "wt");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003688
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003689 if (fsuccess != NULL)
3690 {
3691 int error = 0;
3692 fprintf(fsuccess, "PNG validation succeeded\n");
3693 fflush(fsuccess);
3694 error = ferror(fsuccess);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003695
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003696 if (fclose(fsuccess) || error)
3697 {
3698 fprintf(stderr, "%s: write failed\n", touch);
3699 exit(1);
3700 }
3701 }
3702 }
3703
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003704 return 0;
3705}