blob: d1201e4a3191aee939a149a4042ac85ed7136051 [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{
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600336 /* Generate a new mark. This uses a boring repeatable algorithm and it is
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500337 * 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-Pehrson38ef3a52010-12-03 11:22:31 -0600571 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-Pehrson38ef3a52010-12-03 11:22:31 -0600586 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 */
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600770 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;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600843
844 if (pool->limit > pool->max_limit)
845 pool->max_limit = pool->limit;
846
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500847 pool->limit = 0;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600848
849 if (pool->total > pool->max_total)
850 pool->max_total = pool->total;
851
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500852 pool->total = 0;
853
854 /* Get a new mark too. */
855 store_pool_mark(pool->mark);
856}
857
858/* The memory callbacks: */
859static png_voidp
860store_malloc(png_structp pp, png_alloc_size_t cb)
861{
862 store_pool *pool = png_get_mem_ptr(pp);
863 store_memory *new = malloc(cb + (sizeof *new) + (sizeof pool->mark));
864
865 if (new != NULL)
866 {
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600867 if (cb > pool->max)
868 pool->max = cb;
869
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500870 pool->current += cb;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600871
872 if (pool->current > pool->limit)
873 pool->limit = pool->current;
874
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500875 pool->total += cb;
876
877 new->size = cb;
878 memcpy(new->mark, pool->mark, sizeof new->mark);
879 memcpy((png_byte*)(new+1) + cb, pool->mark, sizeof pool->mark);
880 new->pool = pool;
881 new->next = pool->list;
882 pool->list = new;
883 ++new;
884 }
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600885
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500886 else
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500887 store_pool_error(pool->store, pp, "out of memory");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500888
889 return new;
890}
891
892static void
893store_free(png_structp pp, png_voidp memory)
894{
895 store_pool *pool = png_get_mem_ptr(pp);
896 store_memory *this = memory, **test;
897
898 /* First check that this 'memory' really is valid memory - it must be in the
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600899 * pool list. If it is, use the shared memory_free function to free it.
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500900 */
901 --this;
902 for (test = &pool->list; *test != this; test = &(*test)->next)
903 {
904 if (*test == NULL)
905 {
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -0500906 store_pool_error(pool->store, pp, "bad pointer to free");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500907 return;
908 }
909 }
910
911 /* Unlink this entry, *test == this. */
912 *test = this->next;
913 this->next = NULL;
914 store_memory_free(pp, pool, this);
915}
916
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500917/* Setup functions. */
918/* Cleanup when aborting a write or after storing the new file. */
919static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500920store_write_reset(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500921{
922 if (ps->pwrite != NULL)
923 {
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500924 anon_context(ps);
925
926 Try
927 png_destroy_write_struct(&ps->pwrite, &ps->piwrite);
928
929 Catch_anonymous
930 {
931 /* memory corruption: continue. */
932 }
933
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500934 ps->pwrite = NULL;
935 ps->piwrite = NULL;
936 }
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500937
938 /* And make sure that all the memory has been freed - this will output
939 * spurious errors in the case of memory corruption above, but this is safe.
940 */
941 store_pool_delete(ps, &ps->write_memory_pool);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600942
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500943 store_freenew(ps);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500944}
945
946/* The following is the main write function, it returns a png_struct and,
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600947 * optionally, a png_info suitable for writiing a new PNG file. Use
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500948 * store_storefile above to record this file after it has been written. The
949 * returned libpng structures as destroyed by store_write_reset above.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500950 */
951static png_structp
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500952set_store_for_write(png_store *ps, png_infopp ppi,
953 PNG_CONST char * volatile name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500954{
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500955 anon_context(ps);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500956
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500957 Try
958 {
959 if (ps->pwrite != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -0500960 png_error(ps->pwrite, "write store already in use");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500961
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500962 store_write_reset(ps);
963 safecat(ps->wname, sizeof ps->wname, 0, name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500964
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500965 /* Don't do the slow memory checks if doing a speed test. */
966 if (ps->speed)
967 ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING,
968 ps, store_error, store_warning);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600969
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500970 else
971 ps->pwrite = png_create_write_struct_2(PNG_LIBPNG_VER_STRING,
972 ps, store_error, store_warning, &ps->write_memory_pool,
973 store_malloc, store_free);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -0600974
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500975 png_set_write_fn(ps->pwrite, ps, store_write, store_flush);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500976
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500977 if (ppi != NULL)
978 *ppi = ps->piwrite = png_create_info_struct(ps->pwrite);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500979 }
980
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500981 Catch_anonymous
982 return NULL;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -0500983
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -0500984 return ps->pwrite;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500985}
986
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -0500987/* Cleanup when finished reading (either due to error or in the success case).
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500988 */
989static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -0500990store_read_reset(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500991{
992 if (ps->pread != NULL)
993 {
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500994 anon_context(ps);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600995
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -0500996 Try
997 png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
998
999 Catch_anonymous
1000 {
1001 /*error already output: continue*/
1002 }
1003
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001004 ps->pread = NULL;
1005 ps->piread = NULL;
1006 }
1007
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05001008 /* Always do this to be safe. */
1009 store_pool_delete(ps, &ps->read_memory_pool);
1010
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001011 ps->current = NULL;
1012 ps->next = NULL;
1013 ps->readpos = 0;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001014 ps->validated = 0;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001015}
1016
1017static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001018store_read_set(png_store *ps, png_uint_32 id)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001019{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001020 png_store_file *pf = ps->saved;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001021
1022 while (pf != NULL)
1023 {
1024 if (pf->id == id)
1025 {
1026 ps->current = pf;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001027 ps->next = NULL;
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001028 store_read_buffer_next(ps);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001029 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001030 }
1031
1032 pf = pf->next;
1033 }
1034
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001035 {
1036 size_t pos;
1037 char msg[FILE_NAME_SIZE+64];
1038
1039 pos = standard_name_from_id(msg, sizeof msg, 0, id);
1040 pos = safecat(msg, sizeof msg, pos, ": file not found");
1041 png_error(ps->pread, msg);
1042 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001043}
1044
1045/* The main interface for reading a saved file - pass the id number of the file
1046 * to retrieve. Ids must be unique or the earlier file will be hidden. The API
1047 * returns a png_struct and, optionally, a png_info. Both of these will be
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001048 * destroyed by store_read_reset above.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001049 */
1050static png_structp
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001051set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001052 PNG_CONST char *name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001053{
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001054 /* Set the name for png_error */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001055 safecat(ps->test, sizeof ps->test, 0, name);
1056
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001057 if (ps->pread != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001058 png_error(ps->pread, "read store already in use");
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001059
1060 store_read_reset(ps);
1061
1062 /* Both the create APIs can return NULL if used in their default mode
1063 * (because there is no other way of handling an error because the jmp_buf by
1064 * default is stored in png_struct and that has not been allocated!)
1065 * However, given that store_error works correctly in these circumstances we
1066 * don't ever expect NULL in this program.
1067 */
1068 if (ps->speed)
1069 ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps,
1070 store_error, store_warning);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001071
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001072 else
1073 ps->pread = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, ps,
1074 store_error, store_warning, &ps->read_memory_pool, store_malloc,
1075 store_free);
1076
1077 if (ps->pread == NULL)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001078 {
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001079 struct exception_context *the_exception_context = &ps->exception_context;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001080
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05001081 store_log(ps, NULL, "png_create_read_struct returned NULL (unexpected)",
1082 1/*error*/);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001083
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001084 Throw ps;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001085 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001086
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001087 store_read_set(ps, id);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001088
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001089 if (ppi != NULL)
1090 *ppi = ps->piread = png_create_info_struct(ps->pread);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001091
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001092 return ps->pread;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001093}
1094
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05001095/* The overall cleanup of a store simply calls the above then removes all the
1096 * saved files. This does not delete the store itself.
1097 */
1098static void
1099store_delete(png_store *ps)
1100{
1101 store_write_reset(ps);
1102 store_read_reset(ps);
1103 store_freefile(&ps->saved);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001104
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001105 if (ps->image != NULL)
1106 {
1107 free(ps->image-1);
1108 ps->image = NULL;
1109 ps->cb_image = 0;
1110 }
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05001111}
1112
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001113/*********************** PNG FILE MODIFICATION ON READ ************************/
1114/* Files may be modified on read. The following structure contains a complete
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001115 * png_store together with extra members to handle modification and a special
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001116 * read callback for libpng. To use this the 'modifications' field must be set
1117 * to a list of png_modification structures that actually perform the
1118 * modification, otherwise a png_modifier is functionally equivalent to a
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001119 * png_store. There is a special read function, set_modifier_for_read, which
1120 * replaces set_store_for_read.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001121 */
1122typedef struct png_modifier
1123{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001124 png_store this; /* I am a png_store */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001125 struct png_modification *modifications; /* Changes to make */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001126
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001127 enum modifier_state
1128 {
1129 modifier_start, /* Initial value */
1130 modifier_signature, /* Have a signature */
1131 modifier_IHDR /* Have an IHDR */
1132 } state; /* My state */
1133
1134 /* Information from IHDR: */
1135 png_byte bit_depth; /* From IHDR */
1136 png_byte colour_type; /* From IHDR */
1137
1138 /* While handling PLTE, IDAT and IEND these chunks may be pended to allow
1139 * other chunks to be inserted.
1140 */
1141 png_uint_32 pending_len;
1142 png_uint_32 pending_chunk;
1143
1144 /* Test values */
1145 double *gammas;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001146 unsigned int ngammas;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001147
1148 /* Lowest sbit to test (libpng fails for sbit < 8) */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001149 png_byte sbitlow;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001150
1151 /* Error control - these are the limits on errors accepted by the gamma tests
1152 * below.
1153 */
1154 double maxout8; /* Maximum output value error */
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05001155 double maxabs8; /* Absolute sample error 0..1 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001156 double maxpc8; /* Percentage sample error 0..100% */
1157 double maxout16; /* Maximum output value error */
1158 double maxabs16; /* Absolute sample error 0..1 */
1159 double maxpc16; /* Percentage sample error 0..100% */
1160
1161 /* Logged 8 and 16 bit errors ('output' values): */
1162 double error_gray_2;
1163 double error_gray_4;
1164 double error_gray_8;
1165 double error_gray_16;
1166 double error_color_8;
1167 double error_color_16;
1168
1169 /* Flags: */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001170 /* Whether or not to interlace. */
1171 int interlace_type :9; /* int, but must store '1' */
1172
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001173 /* When to use the use_input_precision option: */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001174 unsigned int use_input_precision :1;
1175 unsigned int use_input_precision_sbit :1;
1176 unsigned int use_input_precision_16to8 :1;
1177 unsigned int log :1; /* Log max error */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001178
1179 /* Buffer information, the buffer size limits the size of the chunks that can
1180 * be modified - they must fit (including header and CRC) into the buffer!
1181 */
1182 size_t flush; /* Count of bytes to flush */
1183 size_t buffer_count; /* Bytes in buffer */
1184 size_t buffer_position; /* Position in buffer */
1185 png_byte buffer[1024];
1186} png_modifier;
1187
1188static double abserr(png_modifier *pm, png_byte bit_depth)
1189{
1190 return bit_depth == 16 ? pm->maxabs16 : pm->maxabs8;
1191}
1192
1193static double pcerr(png_modifier *pm, png_byte bit_depth)
1194{
1195 return (bit_depth == 16 ? pm->maxpc16 : pm->maxpc8) * .01;
1196}
1197
1198static double outerr(png_modifier *pm, png_byte bit_depth)
1199{
1200 /* There is a serious error in the 2 and 4 bit grayscale transform because
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001201 * the gamma table value (8 bits) is simply shifted, not rounded, so the
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001202 * error in 4 bit greyscale gamma is up to the value below. This is a hack
1203 * to allow pngvalid to succeed:
1204 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001205 if (bit_depth == 2)
1206 return .73182-.5;
1207
1208 if (bit_depth == 4)
1209 return .90644-.5;
1210
1211 if (bit_depth == 16)
1212 return pm->maxout16;
1213
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001214 return pm->maxout8;
1215}
1216
1217/* This returns true if the test should be stopped now because it has already
1218 * failed and it is running silently.
1219 */
1220static int fail(png_modifier *pm)
1221{
1222 return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 ||
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001223 (pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001224}
1225
1226static void
1227modifier_init(png_modifier *pm)
1228{
1229 memset(pm, 0, sizeof *pm);
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001230 store_init(&pm->this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001231 pm->modifications = NULL;
1232 pm->state = modifier_start;
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001233 pm->sbitlow = 1U;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001234 pm->maxout8 = pm->maxpc8 = pm->maxabs8 = 0;
1235 pm->maxout16 = pm->maxpc16 = pm->maxabs16 = 0;
1236 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0;
1237 pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001238 pm->interlace_type = PNG_INTERLACE_NONE;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001239 pm->use_input_precision = 0;
1240 pm->use_input_precision_sbit = 0;
1241 pm->use_input_precision_16to8 = 0;
1242 pm->log = 0;
1243
1244 /* Rely on the memset for all the other fields - there are no pointers */
1245}
1246
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05001247/* One modification structure must be provided for each chunk to be modified (in
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001248 * fact more than one can be provided if multiple separate changes are desired
1249 * for a single chunk.) Modifications include adding a new chunk when a
1250 * suitable chunk does not exist.
1251 *
1252 * The caller of modify_fn will reset the CRC of the chunk and record 'modified'
1253 * or 'added' as appropriate if the modify_fn returns 1 (true). If the
1254 * modify_fn is NULL the chunk is simply removed.
1255 */
1256typedef struct png_modification
1257{
1258 struct png_modification *next;
1259 png_uint_32 chunk;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001260
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001261 /* If the following is NULL all matching chunks will be removed: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001262 int (*modify_fn)(struct png_modifier *pm,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001263 struct png_modification *me, int add);
1264
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001265 /* If the following is set to PLTE, IDAT or IEND and the chunk has not been
1266 * found and modified (and there is a modify_fn) the modify_fn will be called
1267 * to add the chunk before the relevant chunk.
1268 */
1269 png_uint_32 add;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001270 unsigned int modified :1; /* Chunk was modified */
1271 unsigned int added :1; /* Chunk was added */
1272 unsigned int removed :1; /* Chunk was removed */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001273} png_modification;
1274
1275static void modification_reset(png_modification *pmm)
1276{
1277 if (pmm != NULL)
1278 {
1279 pmm->modified = 0;
1280 pmm->added = 0;
1281 pmm->removed = 0;
1282 modification_reset(pmm->next);
1283 }
1284}
1285
1286static void
1287modification_init(png_modification *pmm)
1288{
1289 memset(pmm, 0, sizeof *pmm);
1290 pmm->next = NULL;
1291 pmm->chunk = 0;
1292 pmm->modify_fn = NULL;
1293 pmm->add = 0;
1294 modification_reset(pmm);
1295}
1296
1297static void
1298modifier_reset(png_modifier *pm)
1299{
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001300 store_read_reset(&pm->this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001301 pm->modifications = NULL;
1302 pm->state = modifier_start;
1303 pm->bit_depth = pm->colour_type = 0;
1304 pm->pending_len = pm->pending_chunk = 0;
1305 pm->flush = pm->buffer_count = pm->buffer_position = 0;
1306}
1307
1308/* Convenience macros. */
1309#define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
1310#define CHUNK_IHDR CHUNK(73,72,68,82)
1311#define CHUNK_PLTE CHUNK(80,76,84,69)
1312#define CHUNK_IDAT CHUNK(73,68,65,84)
1313#define CHUNK_IEND CHUNK(73,69,78,68)
1314#define CHUNK_cHRM CHUNK(99,72,82,77)
1315#define CHUNK_gAMA CHUNK(103,65,77,65)
1316#define CHUNK_sBIT CHUNK(115,66,73,84)
1317#define CHUNK_sRGB CHUNK(115,82,71,66)
1318
1319/* The guts of modification are performed during a read. */
1320static void
1321modifier_crc(png_bytep buffer)
1322{
1323 /* Recalculate the chunk CRC - a complete chunk must be in
1324 * the buffer, at the start.
1325 */
1326 uInt datalen = png_get_uint_32(buffer);
1327 png_save_uint_32(buffer+datalen+8, crc32(0L, buffer+4, datalen+4));
1328}
1329
1330static void
1331modifier_setbuffer(png_modifier *pm)
1332{
1333 modifier_crc(pm->buffer);
1334 pm->buffer_count = png_get_uint_32(pm->buffer)+12;
1335 pm->buffer_position = 0;
1336}
1337
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001338/* Separate the callback into the actual implementation (which is passed the
1339 * png_modifier explicitly) and the callback, which gets the modifier from the
1340 * png_struct.
1341 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001342static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001343modifier_read_imp(png_modifier *pm, png_bytep pb, png_size_t st)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001344{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001345 while (st > 0)
1346 {
1347 size_t cb;
1348 png_uint_32 len, chunk;
1349 png_modification *mod;
1350
1351 if (pm->buffer_position >= pm->buffer_count) switch (pm->state)
1352 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001353 static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
1354 case modifier_start:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001355 store_read_imp(&pm->this, pm->buffer, 8); /* size of signature. */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001356 pm->buffer_count = 8;
1357 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001358
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001359 if (memcmp(pm->buffer, sign, 8) != 0)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001360 png_error(pm->this.pread, "invalid PNG file signature");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001361 pm->state = modifier_signature;
1362 break;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001363
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001364 case modifier_signature:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001365 store_read_imp(&pm->this, pm->buffer, 13+12); /* size of IHDR */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001366 pm->buffer_count = 13+12;
1367 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001368
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001369 if (png_get_uint_32(pm->buffer) != 13 ||
1370 png_get_uint_32(pm->buffer+4) != CHUNK_IHDR)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001371 png_error(pm->this.pread, "invalid IHDR");
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001372
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001373 /* Check the list of modifiers for modifications to the IHDR. */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001374 mod = pm->modifications;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001375 while (mod != NULL)
1376 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001377 if (mod->chunk == CHUNK_IHDR && mod->modify_fn &&
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001378 (*mod->modify_fn)(pm, mod, 0))
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001379 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001380 mod->modified = 1;
1381 modifier_setbuffer(pm);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001382 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001383
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001384 /* Ignore removal or add if IHDR! */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001385 mod = mod->next;
1386 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001387
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001388 /* Cache information from the IHDR (the modified one.) */
1389 pm->bit_depth = pm->buffer[8+8];
1390 pm->colour_type = pm->buffer[8+8+1];
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001391
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001392 pm->state = modifier_IHDR;
1393 pm->flush = 0;
1394 break;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001395
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001396 case modifier_IHDR:
1397 default:
1398 /* Read a new chunk and process it until we see PLTE, IDAT or
1399 * IEND. 'flush' indicates that there is still some data to
1400 * output from the preceding chunk.
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001401 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001402 if ((cb = pm->flush) > 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001403 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001404 if (cb > st) cb = st;
1405 pm->flush -= cb;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001406 store_read_imp(&pm->this, pb, cb);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001407 pb += cb;
1408 st -= cb;
1409 if (st <= 0) return;
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001410 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001411
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001412 /* No more bytes to flush, read a header, or handle a pending
1413 * chunk.
1414 */
1415 if (pm->pending_chunk != 0)
1416 {
1417 png_save_uint_32(pm->buffer, pm->pending_len);
1418 png_save_uint_32(pm->buffer+4, pm->pending_chunk);
1419 pm->pending_len = 0;
1420 pm->pending_chunk = 0;
1421 }
1422 else
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001423 store_read_imp(&pm->this, pm->buffer, 8);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001424
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001425 pm->buffer_count = 8;
1426 pm->buffer_position = 0;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001427
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001428 /* Check for something to modify or a terminator chunk. */
1429 len = png_get_uint_32(pm->buffer);
1430 chunk = png_get_uint_32(pm->buffer+4);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001431
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001432 /* Terminators first, they may have to be delayed for added
1433 * chunks
1434 */
1435 if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT ||
1436 chunk == CHUNK_IEND)
1437 {
1438 mod = pm->modifications;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001439
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001440 while (mod != NULL)
1441 {
1442 if ((mod->add == chunk ||
1443 (mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT)) &&
1444 mod->modify_fn != NULL && !mod->modified && !mod->added)
1445 {
1446 /* Regardless of what the modify function does do not run
1447 * this again.
1448 */
1449 mod->added = 1;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001450
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001451 if ((*mod->modify_fn)(pm, mod, 1/*add*/))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001452 {
1453 /* Reset the CRC on a new chunk */
1454 if (pm->buffer_count > 0)
1455 modifier_setbuffer(pm);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001456
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001457 else
1458 {
1459 pm->buffer_position = 0;
1460 mod->removed = 1;
1461 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001462
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001463 /* The buffer has been filled with something (we assume)
1464 * so output this. Pend the current chunk.
1465 */
1466 pm->pending_len = len;
1467 pm->pending_chunk = chunk;
1468 break; /* out of while */
1469 }
1470 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001471
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001472 mod = mod->next;
1473 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001474
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001475 /* Don't do any further processing if the buffer was modified -
1476 * otherwise the code will end up modifying a chunk that was just
1477 * added.
1478 */
1479 if (mod != NULL)
1480 break; /* out of switch */
1481 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001482
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001483 /* If we get to here then this chunk may need to be modified. To do
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001484 * this it must be less than 1024 bytes in total size, otherwise
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001485 * it just gets flushed.
1486 */
1487 if (len+12 <= sizeof pm->buffer)
1488 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001489 store_read_imp(&pm->this, pm->buffer+pm->buffer_count,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001490 len+12-pm->buffer_count);
1491 pm->buffer_count = len+12;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001492
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001493 /* Check for a modification, else leave it be. */
1494 mod = pm->modifications;
1495 while (mod != NULL)
1496 {
1497 if (mod->chunk == chunk)
1498 {
1499 if (mod->modify_fn == NULL)
1500 {
1501 /* Remove this chunk */
1502 pm->buffer_count = pm->buffer_position = 0;
1503 mod->removed = 1;
1504 break; /* Terminate the while loop */
1505 }
1506
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001507 else if ((*mod->modify_fn)(pm, mod, 0))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001508 {
1509 mod->modified = 1;
1510 /* The chunk may have been removed: */
1511 if (pm->buffer_count == 0)
1512 {
1513 pm->buffer_position = 0;
1514 break;
1515 }
1516 modifier_setbuffer(pm);
1517 }
1518 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001519
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001520 mod = mod->next;
1521 }
1522 }
1523
1524 else
1525 pm->flush = len+12 - pm->buffer_count; /* data + crc */
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06001526
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001527 /* Take the data from the buffer (if there is any). */
1528 break;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001529 }
1530
1531 /* Here to read from the modifier buffer (not directly from
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001532 * the store, as in the flush case above.)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001533 */
1534 cb = pm->buffer_count - pm->buffer_position;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001535
1536 if (cb > st)
1537 cb = st;
1538
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001539 memcpy(pb, pm->buffer + pm->buffer_position, cb);
1540 st -= cb;
1541 pb += cb;
1542 pm->buffer_position += cb;
1543 }
1544}
1545
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05001546/* The callback: */
1547static void
1548modifier_read(png_structp pp, png_bytep pb, png_size_t st)
1549{
1550 png_modifier *pm = png_get_io_ptr(pp);
1551
1552 if (pm == NULL || pm->this.pread != pp)
1553 png_error(pp, "bad modifier_read call");
1554
1555 modifier_read_imp(pm, pb, st);
1556}
1557
1558/* Like store_progressive_read but the data is getting changed as we go so we
1559 * need a local buffer.
1560 */
1561static void
1562modifier_progressive_read(png_modifier *pm, png_structp pp, png_infop pi)
1563{
1564 if (pm->this.pread != pp || pm->this.current == NULL ||
1565 pm->this.next == NULL)
1566 png_error(pp, "store state damaged (progressive)");
1567
1568 /* This is another Horowitz and Hill random noise generator. In this case
1569 * the aim is to stress the progressive reader with truely horrible variable
1570 * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers is
1571 * generated. We could probably just count from 1 to 32767 and get as good
1572 * a result.
1573 */
1574 for (;;)
1575 {
1576 static png_uint_32 noise = 1;
1577 png_size_t cb, cbAvail;
1578 png_byte buffer[512];
1579
1580 /* Generate 15 more bits of stuff: */
1581 noise = (noise << 9) | ((noise ^ (noise >> (9-5))) & 0x1ff);
1582 cb = noise & 0x1ff;
1583
1584 /* Check that this number of bytes are available (in the current buffer.)
1585 * (This doesn't quite work - the modifier might delete a chunk; unlikely
1586 * but possible, it doesn't happen at present because the modifier only
1587 * adds chunks to standard images.)
1588 */
1589 cbAvail = store_read_buffer_avail(&pm->this);
1590 if (pm->buffer_count > pm->buffer_position)
1591 cbAvail += pm->buffer_count - pm->buffer_position;
1592
1593 if (cb > cbAvail)
1594 {
1595 /* Check for EOF: */
1596 if (cbAvail == 0)
1597 break;
1598
1599 cb = cbAvail;
1600 }
1601
1602 modifier_read_imp(pm, buffer, cb);
1603 png_process_data(pp, pi, buffer, cb);
1604 }
1605
1606 /* Check the invariants at the end (if this fails it's a problem in this
1607 * file!)
1608 */
1609 if (pm->buffer_count > pm->buffer_position ||
1610 pm->this.next != &pm->this.current->data ||
1611 pm->this.readpos < pm->this.current->datacount)
1612 png_error(pp, "progressive read implementation error");
1613}
1614
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001615/* Set up a modifier. */
1616static png_structp
1617set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001618 PNG_CONST char *name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001619{
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001620 /* Do this first so that the modifier fields are cleared even if an error
1621 * happens allocating the png_struct. No allocation is done here so no
1622 * cleanup is required.
1623 */
1624 pm->state = modifier_start;
1625 pm->bit_depth = 0;
1626 pm->colour_type = 255;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001627
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001628 pm->pending_len = 0;
1629 pm->pending_chunk = 0;
1630 pm->flush = 0;
1631 pm->buffer_count = 0;
1632 pm->buffer_position = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001633
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05001634 return set_store_for_read(&pm->this, ppi, id, name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001635}
1636
1637/***************************** STANDARD PNG FILES *****************************/
1638/* Standard files - write and save standard files. */
1639/* The standard files are constructed with rows which fit into a 1024 byte row
1640 * buffer. This makes allocation easier below. Further regardless of the file
1641 * format every file has 128 pixels (giving 1024 bytes for 64bpp formats).
1642 *
1643 * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001644 * and with an ID derived from the colour type, bit depth and interlace type
1645 * as above (FILEID).
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001646 */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001647
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001648/* The number of passes is related to the interlace type. There's no libpng API
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001649 * to determine this so we need an inquiry function:
1650 */
1651static int
1652npasses_from_interlace_type(png_structp pp, int interlace_type)
1653{
1654 switch (interlace_type)
1655 {
1656 default:
1657 png_error(pp, "invalid interlace type");
1658
1659 case PNG_INTERLACE_NONE:
1660 return 1;
1661
1662 case PNG_INTERLACE_ADAM7:
1663 return 7;
1664 }
1665}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001666
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001667#define STD_WIDTH 128U
1668#define STD_ROWMAX (STD_WIDTH*8U)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001669
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001670static unsigned int
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001671bit_size(png_structp pp, png_byte colour_type, png_byte bit_depth)
1672{
1673 switch (colour_type)
1674 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001675 case 0: return bit_depth;
1676
1677 case 2: return 3*bit_depth;
1678
1679 case 3: return bit_depth;
1680
1681 case 4: return 2*bit_depth;
1682
1683 case 6: return 4*bit_depth;
1684
1685 default: png_error(pp, "invalid color type");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001686 }
1687}
1688
1689static size_t
1690standard_rowsize(png_structp pp, png_byte colour_type, png_byte bit_depth)
1691{
1692 return (STD_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8;
1693}
1694
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001695/* standard_wdith(pp, colour_type, bit_depth) current returns the same number
1696 * every time, so just use a macro:
1697 */
1698#define standard_width(pp, colour_type, bit_depth) STD_WIDTH
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001699
1700static png_uint_32
1701standard_height(png_structp pp, png_byte colour_type, png_byte bit_depth)
1702{
1703 switch (bit_size(pp, colour_type, bit_depth))
1704 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001705 case 1:
1706 case 2:
1707 case 4:
1708 return 1; /* Total of 128 pixels */
1709
1710 case 8:
1711 return 2; /* Total of 256 pixels/bytes */
1712
1713 case 16:
1714 return 512; /* Total of 65536 pixels */
1715
1716 case 24:
1717 case 32:
1718 return 512; /* 65536 pixels */
1719
1720 case 48:
1721 case 64:
1722 return 2048;/* 4 x 65536 pixels. */
1723
1724 default:
1725 return 0; /* Error, will be caught later */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001726 }
1727}
1728
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001729/* So the maximum standard image size is: */
1730#define STD_IMAGEMAX (STD_ROWMAX * 2048)
1731
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001732static void
1733standard_row(png_structp pp, png_byte buffer[STD_ROWMAX], png_byte colour_type,
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001734 png_byte bit_depth, png_uint_32 y)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001735{
1736 png_uint_32 v = y << 7;
1737 png_uint_32 i = 0;
1738
1739 switch (bit_size(pp, colour_type, bit_depth))
1740 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001741 case 1:
1742 while (i<128/8) buffer[i] = v & 0xff, v += 17, ++i;
1743 return;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001744
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001745 case 2:
1746 while (i<128/4) buffer[i] = v & 0xff, v += 33, ++i;
1747 return;
1748
1749 case 4:
1750 while (i<128/2) buffer[i] = v & 0xff, v += 65, ++i;
1751 return;
1752
1753 case 8:
1754 /* 256 bytes total, 128 bytes in each row set as follows: */
1755 while (i<128) buffer[i] = v & 0xff, ++v, ++i;
1756 return;
1757
1758 case 16:
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001759 /* Generate all 65536 pixel values in order, which includes the 8 bit
1760 * GA case as well as the 16 bit G case.
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001761 */
1762 while (i<128)
1763 buffer[2*i] = (v>>8) & 0xff, buffer[2*i+1] = v & 0xff, ++v, ++i;
1764
1765 return;
1766
1767 case 24:
1768 /* 65535 pixels, but rotate the values. */
1769 while (i<128)
1770 {
1771 /* Three bytes per pixel, r, g, b, make b by r^g */
1772 buffer[3*i+0] = (v >> 8) & 0xff;
1773 buffer[3*i+1] = v & 0xff;
1774 buffer[3*i+2] = ((v >> 8) ^ v) & 0xff;
1775 ++v;
1776 ++i;
1777 }
1778
1779 return;
1780
1781 case 32:
1782 /* 65535 pixels, r, g, b, a; just replicate */
1783 while (i<128)
1784 {
1785 buffer[4*i+0] = (v >> 8) & 0xff;
1786 buffer[4*i+1] = v & 0xff;
1787 buffer[4*i+2] = (v >> 8) & 0xff;
1788 buffer[4*i+3] = v & 0xff;
1789 ++v;
1790 ++i;
1791 }
1792
1793 return;
1794
1795 case 48:
1796 /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at
1797 * each pixel, g increase by 257 (0x101) and 'b' by 0x1111:
1798 */
1799 while (i<128)
1800 {
1801 png_uint_32 t = v++;
1802 buffer[6*i+0] = (t >> 8) & 0xff;
1803 buffer[6*i+1] = t & 0xff;
1804 t *= 257;
1805 buffer[6*i+2] = (t >> 8) & 0xff;
1806 buffer[6*i+3] = t & 0xff;
1807 t *= 17;
1808 buffer[6*i+4] = (t >> 8) & 0xff;
1809 buffer[6*i+5] = t & 0xff;
1810 ++i;
1811 }
1812
1813 return;
1814
1815 case 64:
1816 /* As above in the 32 bit case. */
1817 while (i<128)
1818 {
1819 png_uint_32 t = v++;
1820 buffer[8*i+0] = (t >> 8) & 0xff;
1821 buffer[8*i+1] = t & 0xff;
1822 buffer[8*i+4] = (t >> 8) & 0xff;
1823 buffer[8*i+5] = t & 0xff;
1824 t *= 257;
1825 buffer[8*i+2] = (t >> 8) & 0xff;
1826 buffer[8*i+3] = t & 0xff;
1827 buffer[8*i+6] = (t >> 8) & 0xff;
1828 buffer[8*i+7] = t & 0xff;
1829 ++i;
1830 }
1831 return;
1832
1833 default:
1834 break;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001835 }
1836
1837 png_error(pp, "internal error");
1838}
1839
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001840/* This is just to do the right cast - could be changed to a function to check
1841 * 'bd' but there isn't much point.
1842 */
1843#define DEPTH(bd) ((png_byte)(1U << (bd)))
1844
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001845static void
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001846make_standard_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
1847 png_byte PNG_CONST bit_depth, int interlace_type, png_const_charp name)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001848{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001849 context(ps, fault);
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05001850
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001851 Try
1852 {
1853 png_infop pi;
1854 png_structp pp = set_store_for_write(ps, &pi, name);
1855 png_uint_32 h;
1856
1857 /* In the event of a problem return control to the Catch statement below
1858 * to do the clean up - it is not possible to 'return' directly from a Try
1859 * block.
1860 */
1861 if (pp == NULL)
1862 Throw ps;
1863
1864 h = standard_height(pp, colour_type, bit_depth);
1865
1866 png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth), h,
1867 bit_depth, colour_type, interlace_type,
1868 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1869
1870 if (colour_type == 3) /* palette */
1871 {
1872 unsigned int i = 0;
1873 png_color pal[256];
1874
1875 do
1876 pal[i].red = pal[i].green = pal[i].blue = (png_byte)i;
1877 while(++i < 256U);
1878
1879 png_set_PLTE(pp, pi, pal, 256);
1880 }
1881
1882 png_write_info(pp, pi);
1883
1884 if (png_get_rowbytes(pp, pi) !=
1885 standard_rowsize(pp, colour_type, bit_depth))
1886 png_error(pp, "row size incorrect");
1887
1888 else
1889 {
1890 /* Somewhat confusingly this must be called *after* png_write_info
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001891 * because if it is called before, the information in *pp has not been
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001892 * updated to reflect the interlaced image.
1893 */
1894 int npasses = png_set_interlace_handling(pp);
1895 int pass;
1896
1897 if (npasses != npasses_from_interlace_type(pp, interlace_type))
1898 png_error(pp, "write: png_set_interlace_handling failed");
1899
1900 for (pass=1; pass<=npasses; ++pass)
1901 {
1902 png_uint_32 y;
1903
1904 for (y=0; y<h; ++y)
1905 {
1906 png_byte buffer[STD_ROWMAX];
1907
1908 standard_row(pp, buffer, colour_type, bit_depth, y);
1909 png_write_row(pp, buffer);
1910 }
1911 }
1912 }
1913
1914 png_write_end(pp, pi);
1915
1916 /* And store this under the appropriate id, then clean up. */
1917 store_storefile(ps, FILEID(colour_type, bit_depth, interlace_type));
1918
1919 store_write_reset(ps);
1920 }
1921
1922 Catch(fault)
1923 {
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06001924 /* Use the png_store returned by the exception. This may help the compiler
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05001925 * because 'ps' is not used in this branch of the setjmp. Note that fault
1926 * and ps will always be the same value.
1927 */
1928 store_write_reset(fault);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001929 }
1930}
1931
1932static void
1933make_standard(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, int bdlo,
1934 int PNG_CONST bdhi)
1935{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001936 for (; bdlo <= bdhi; ++bdlo)
1937 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001938 int interlace_type;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001939
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001940 for (interlace_type = PNG_INTERLACE_NONE;
1941 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001942 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001943 char name[FILE_NAME_SIZE];
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001944
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001945 standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type);
1946 make_standard_image(ps, colour_type, DEPTH(bdlo), interlace_type,
1947 name);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001948 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001949 }
1950}
1951
1952static void
Glenn Randers-Pehrsonb4e69972010-07-30 10:35:38 -05001953make_standard_images(png_store *ps)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001954{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001955 /* This is in case of errors. */
1956 safecat(ps->test, sizeof ps->test, 0, "make standard images");
1957
1958 /* Arguments are colour_type, low bit depth, high bit depth
1959 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05001960 make_standard(ps, 0, 0, WRITE_BDHI);
1961 make_standard(ps, 2, 3, WRITE_BDHI);
1962 make_standard(ps, 3, 0, 3/*palette: max 8 bits*/);
1963 make_standard(ps, 4, 3, WRITE_BDHI);
1964 make_standard(ps, 6, 3, WRITE_BDHI);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001965}
1966
1967/* Tests - individual test cases */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05001968/* Like 'make_standard' but errors are deliberately introduced into the calls
1969 * to ensure that they get detected - it should not be possible to write an
1970 * invalid image with libpng!
1971 */
1972static void
1973sBIT0_error_fn(png_structp pp, png_infop pi)
1974{
1975 /* 0 is invalid... */
1976 png_color_8 bad;
1977 bad.red = bad.green = bad.blue = bad.gray = bad.alpha = 0;
1978 png_set_sBIT(pp, pi, &bad);
1979}
1980
1981static void
1982sBIT_error_fn(png_structp pp, png_infop pi)
1983{
1984 png_byte bit_depth;
1985 png_color_8 bad;
1986
1987 if (png_get_color_type(pp, pi) == PNG_COLOR_TYPE_PALETTE)
1988 bit_depth = 8;
1989
1990 else
1991 bit_depth = png_get_bit_depth(pp, pi);
1992
1993 /* Now we know the bit depth we can easily generate an invalid sBIT entry */
1994 bad.red = bad.green = bad.blue = bad.gray = bad.alpha =
1995 (png_byte)(bit_depth+1);
1996 png_set_sBIT(pp, pi, &bad);
1997}
1998
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05001999static PNG_CONST struct
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002000{
2001 void (*fn)(png_structp, png_infop);
2002 PNG_CONST char *msg;
2003 unsigned int warning :1; /* the error is a warning... */
2004} error_test[] =
2005 {
2006 { sBIT0_error_fn, "sBIT(0): failed to detect error", 1 },
2007 { sBIT_error_fn, "sBIT(too big): failed to detect error", 1 },
2008 };
2009
2010static void
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002011make_error(png_store* ps, png_byte PNG_CONST colour_type, png_byte bit_depth,
2012 int interlace_type, int test, png_const_charp name)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002013{
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002014 context(ps, fault);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002015
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002016 Try
2017 {
2018 png_structp pp;
2019 png_infop pi;
2020
2021 pp = set_store_for_write(ps, &pi, name);
2022
2023 if (pp == NULL)
2024 Throw ps;
2025
2026 png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth),
2027 standard_height(pp, colour_type, bit_depth), bit_depth, colour_type,
2028 interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2029
2030 if (colour_type == 3) /* palette */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002031 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002032 unsigned int i = 0;
2033 png_color pal[256];
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002034
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002035 do
2036 pal[i].red = pal[i].green = pal[i].blue = (png_byte)i;
2037 while(++i < 256U);
2038
2039 png_set_PLTE(pp, pi, pal, 256);
2040 }
2041
2042 /* Time for a few errors, these are in various optional chunks, the
2043 * standard tests test the standard chunks pretty well.
2044 */
2045 Try
2046 {
2047 /* Expect this to throw: */
2048 ps->expect_error = !error_test[test].warning;
2049 ps->expect_warning = error_test[test].warning;
2050 ps->saw_warning = 0;
2051 error_test[test].fn(pp, pi);
2052
2053 /* Normally the error is only detected here: */
2054 png_write_info(pp, pi);
2055
2056 /* And handle the case where it was only a warning: */
2057 if (ps->expect_warning && ps->saw_warning)
2058 Throw ps;
2059
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002060 /* If we get here there is a problem, we have success - no error or
2061 * no warning - when we shouldn't have success. Log an error.
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002062 */
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002063 store_log(ps, pp, error_test[test].msg, 1/*error*/);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002064 }
2065
2066 Catch (fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002067 ps = fault; /* expected exit, make sure ps is not clobbered */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002068
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002069 /* And clear these flags */
2070 ps->expect_error = 0;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002071 ps->expect_warning = 0;
2072
2073 /* Now write the whole image, just to make sure that the detected, or
2074 * undetected, errro has not created problems inside libpng.
2075 */
2076 if (png_get_rowbytes(pp, pi) !=
2077 standard_rowsize(pp, colour_type, bit_depth))
2078 png_error(pp, "row size incorrect");
2079
2080 else
2081 {
2082 png_uint_32 h = standard_height(pp, colour_type, bit_depth);
2083 int npasses = png_set_interlace_handling(pp);
2084 int pass;
2085
2086 if (npasses != npasses_from_interlace_type(pp, interlace_type))
2087 png_error(pp, "write: png_set_interlace_handling failed");
2088
2089 for (pass=1; pass<=npasses; ++pass)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002090 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002091 png_uint_32 y;
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002092
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002093 for (y=0; y<h; ++y)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002094 {
2095 png_byte buffer[STD_ROWMAX];
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002096
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002097 standard_row(pp, buffer, colour_type, bit_depth, y);
2098 png_write_row(pp, buffer);
2099 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002100 }
2101 }
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002102
2103 png_write_end(pp, pi);
2104
2105 /* The following deletes the file that was just written. */
2106 store_write_reset(ps);
2107 }
2108
2109 Catch(fault)
2110 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002111 store_write_reset(fault);
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002112 }
2113}
2114
2115static int
2116make_errors(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
2117 int bdlo, int PNG_CONST bdhi)
2118{
2119 for (; bdlo <= bdhi; ++bdlo)
2120 {
2121 int interlace_type;
2122
2123 for (interlace_type = PNG_INTERLACE_NONE;
2124 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
2125 {
2126 unsigned int test;
2127 char name[FILE_NAME_SIZE];
2128
2129 standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type);
2130
2131 for (test=0; test<(sizeof error_test)/(sizeof error_test[0]); ++test)
2132 {
2133 make_error(&pm->this, colour_type, DEPTH(bdlo), interlace_type,
2134 test, name);
2135
2136 if (fail(pm))
2137 return 0;
2138 }
2139 }
2140 }
2141
2142 return 1; /* keep going */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002143}
2144
2145static void
2146perform_error_test(png_modifier *pm)
2147{
2148 /* Need to do this here because we just write in this test. */
2149 safecat(pm->this.test, sizeof pm->this.test, 0, "error test");
2150
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002151 if (!make_errors(pm, 0, 0, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002152 return;
2153
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002154 if (!make_errors(pm, 2, 3, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002155 return;
2156
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002157 if (!make_errors(pm, 3, 0, 3))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002158 return;
2159
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002160 if (!make_errors(pm, 4, 3, WRITE_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002161 return;
2162
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002163 if (!make_errors(pm, 6, 3, WRITE_BDHI))
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002164 return;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002165}
2166
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002167/* Because we want to use the same code in both the progressive reader and the
2168 * sequential reader it is necessary to deal with the fact that the progressive
2169 * reader callbacks only have one parameter (png_get_progressive_ptr()), so this
2170 * must contain all the test parameters and all the local variables directly
2171 * accessible to the sequential reader implementation.
2172 *
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002173 * The technique adopted is to reinvent part of what Dijkstra termed a
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002174 * 'display'; an array of pointers to the stack frames of enclosing functions so
2175 * that a nested function definition can access the local (C auto) variables of
2176 * the functions that contain its definition. In fact C provides the first
2177 * pointer (the local variables - the stack frame pointer) and the last (the
2178 * global variables - the BCPL global vector typically implemented as global
2179 * addresses), this code requires one more pointer to make the display - the
2180 * local variables (and function call parameters) of the function that actually
2181 * invokes either the progressive or sequential reader.
2182 *
2183 * Perhaps confusingly this technique is confounded with classes - the
2184 * 'standard_display' defined here is sub-classed as the 'gamma_display' below.
2185 * A gamma_display is a standard_display, taking advantage of the ANSI-C
2186 * requirement that the pointer to the first member of a structure must be the
2187 * same as the pointer to the structure. This allows us to reuse standard_
2188 * functions in the gamma test code; something that could not be done with
2189 * nested funtions!
2190 */
2191typedef struct standard_display
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002192{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002193 png_store* ps; /* Test parameters (passed to the function) */
2194 png_byte colour_type;
2195 png_byte bit_depth;
2196 int interlace_type;
2197 png_uint_32 id; /* Calculated file ID */
2198 png_uint_32 w; /* Width of image */
2199 png_uint_32 h; /* Height of image */
2200 int npasses; /* Number of interlaced passes */
2201 size_t cbRow; /* Bytes in a row of the output image. */
2202} standard_display;
2203
2204static void
2205standard_display_init(standard_display *dp, png_store* ps, png_byte colour_type,
2206 png_byte bit_depth, int interlace_type)
2207{
2208 dp->ps = ps;
2209 dp->colour_type = colour_type;
2210 dp->bit_depth = bit_depth;
2211 dp->interlace_type = interlace_type;
2212 dp->id = FILEID(colour_type, bit_depth, interlace_type);
2213 dp->w = 0;
2214 dp->h = 0;
2215 dp->npasses = 0;
2216 dp->cbRow = 0;
2217}
2218
2219/* By passing a 'standard_display' the progressive callbacks can be used
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002220 * directly by the sequential code, the functions suffixed "_imp" are the
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002221 * implementations, the functions without the suffix are the callbacks.
2222 *
2223 * The code for the info callback is split into two because this callback calls
2224 * png_read_update_info or png_start_read_image and what gets called depends on
2225 * whether the info needs updating (we want to test both calls in pngvalid.)
2226 */
2227static void
2228standard_info_part1(standard_display *dp, png_structp pp, png_infop pi)
2229{
2230 if (png_get_bit_depth(pp, pi) != dp->bit_depth)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002231 png_error(pp, "validate: bit depth changed");
2232
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002233 if (png_get_color_type(pp, pi) != dp->colour_type)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002234 png_error(pp, "validate: color type changed");
2235
2236 if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE)
2237 png_error(pp, "validate: filter type changed");
2238
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002239 if (png_get_interlace_type(pp, pi) != dp->interlace_type)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002240 png_error(pp, "validate: interlacing changed");
2241
2242 if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE)
2243 png_error(pp, "validate: compression type changed");
2244
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002245 dp->w = png_get_image_width(pp, pi);
2246
2247 if (dp->w != standard_width(pp, dp->colour_type, dp->bit_depth))
2248 png_error(pp, "validate: image width changed");
2249
2250 dp->h = png_get_image_height(pp, pi);
2251
2252 if (dp->h != standard_height(pp, dp->colour_type, dp->bit_depth))
2253 png_error(pp, "validate: image height changed");
2254
2255 /* Important: this is validating the value *before* any transforms have been
2256 * put in place. It doesn't matter for the standard tests, where there are
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002257 * no transforms, but it does for other tests where rowbytes may change after
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002258 * png_read_update_info.
2259 */
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002260 if (png_get_rowbytes(pp, pi) !=
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002261 standard_rowsize(pp, dp->colour_type, dp->bit_depth))
2262 png_error(pp, "validate: row size changed");
2263
2264 if (dp->colour_type == 3) /* palette */
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002265 {
2266 png_colorp pal;
2267 int num;
2268
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002269 /* This could be passed in but isn't - the values set above when the
2270 * standard images were made are just repeated here.
2271 */
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002272 if (png_get_PLTE(pp, pi, &pal, &num) & PNG_INFO_PLTE)
2273 {
2274 int i;
2275
2276 if (num != 256)
2277 png_error(pp, "validate: color type 3 PLTE chunk size changed");
2278
2279 for (i=0; i<num; ++i)
2280 if (pal[i].red != i || pal[i].green != i || pal[i].blue != i)
2281 png_error(pp, "validate: color type 3 PLTE chunk changed");
2282 }
2283
2284 else
2285 png_error(pp, "validate: missing PLTE with color type 3");
2286 }
2287
2288 /* Read the number of passes - expected to match the value used when
2289 * creating the image (interlaced or not). This has the side effect of
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002290 * turning on interlace handling.
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002291 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002292 dp->npasses = png_set_interlace_handling(pp);
2293
2294 if (dp->npasses != npasses_from_interlace_type(pp, dp->interlace_type))
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002295 png_error(pp, "validate: file changed interlace type");
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002296
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002297 /* Caller calls png_read_update_info or png_start_read_image now, then calls
2298 * part2.
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002299 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002300}
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002301
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002302/* This must be called *after* the png_read_update_info call to get the correct
2303 * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed
2304 * image.
2305 */
2306static void
2307standard_info_part2(standard_display *dp, png_structp pp, png_infop pi,
2308 int nImages)
2309{
2310 /* Record cbRow now that it can be found. */
2311 dp->cbRow = png_get_rowbytes(pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002312
2313 /* Then ensure there is enough space for the output image(s). */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002314 store_ensure_image(dp->ps, pp, nImages * dp->cbRow * dp->h);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002315}
2316
2317static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002318standard_info_imp(standard_display *dp, png_structp pp, png_infop pi,
2319 int nImages)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002320{
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002321 /* Note that the validation routine has the side effect of turning on
2322 * interlace handling in the subsequent code.
2323 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002324 standard_info_part1(dp, pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002325
2326 /* And the info callback has to call this (or png_read_update_info - see
2327 * below in the png_modifier code for that variant.
2328 */
2329 png_start_read_image(pp);
2330
2331 /* Validate the height, width and rowbytes plus ensure that sufficient buffer
2332 * exists for decoding the image.
2333 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002334 standard_info_part2(dp, pp, pi, nImages);
2335}
2336
2337static void
2338standard_info(png_structp pp, png_infop pi)
2339{
2340 standard_display *dp = png_get_progressive_ptr(pp);
2341
2342 /* Call with nImages==1 because the progressive reader can only produce one
2343 * image.
2344 */
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002345 standard_info_imp(dp, pp, pi, 1 /*only one image*/);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002346}
2347
2348static void
2349progressive_row(png_structp pp, png_bytep new_row, png_uint_32 y, int pass)
2350{
2351 UNUSED(pass);
2352
2353 /* When handling interlacing some rows will be absent in each pass, the
2354 * callback still gets called, but with a NULL pointer. We need our own
2355 * 'cbRow', but we can't call png_get_rowbytes because we got no info
2356 * structure.
2357 */
2358 if (new_row != NULL)
2359 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002360 PNG_CONST standard_display *dp = png_get_progressive_ptr(pp);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002361
2362 /* Combine the new row into the old: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002363 png_progressive_combine_row(pp, dp->ps->image + y * dp->cbRow, new_row);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002364 }
2365}
2366
2367static void
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002368sequential_row(standard_display *dp, png_structp pp, png_infop pi,
2369 PNG_CONST png_bytep pImage, PNG_CONST png_bytep pDisplay)
2370{
2371 PNG_CONST int npasses = dp->npasses;
2372 PNG_CONST png_uint_32 h = dp->h;
2373 PNG_CONST size_t cbRow = dp->cbRow;
2374 int pass;
2375
2376 for (pass=1; pass <= npasses; ++pass)
2377 {
2378 png_uint_32 y;
2379 png_bytep pRow1 = pImage;
2380 png_bytep pRow2 = pDisplay;
2381
2382 for (y=0; y<h; ++y)
2383 {
2384 png_read_row(pp, pRow1, pRow2);
2385
2386 if (pRow1 != NULL)
2387 pRow1 += cbRow;
2388
2389 if (pRow2 != NULL)
2390 pRow2 += cbRow;
2391 }
2392 }
2393
2394 /* And finish the read operation (only really necessary if the caller wants
2395 * to find additional data in png_info from chunks after the last IDAT.)
2396 */
2397 png_read_end(pp, pi);
2398}
2399
2400static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002401standard_row_validate(standard_display *dp, png_structp pp, png_const_bytep row,
2402 png_const_bytep display, png_uint_32 y)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002403{
2404 png_byte std[STD_ROWMAX];
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002405 standard_row(pp, std, dp->colour_type, dp->bit_depth, y);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002406
2407 /* At the end both the 'read' and 'display' arrays should end up identical.
2408 * In earlier passes 'read' will be narrow, containing only the columns that
2409 * were read, and display will be full width but populated with garbage where
2410 * pixels have not been filled in.
2411 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002412 if (row != NULL && memcmp(std, row, dp->cbRow) != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002413 {
2414 char msg[64];
2415 sprintf(msg, "PNG image row %d changed", y);
2416 png_error(pp, msg);
2417 }
2418
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002419 if (display != NULL && memcmp(std, display, dp->cbRow) != 0)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002420 {
2421 char msg[64];
2422 sprintf(msg, "display row %d changed", y);
2423 png_error(pp, msg);
2424 }
2425}
2426
2427static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002428standard_image_validate(standard_display *dp, png_structp pp,
2429 png_const_bytep pImage, png_const_bytep pDisplay)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002430{
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002431 png_uint_32 y;
2432
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002433 for (y=0; y<dp->h; ++y)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002434 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002435 standard_row_validate(dp, pp, pImage, pDisplay, y);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002436
2437 if (pImage != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002438 pImage += dp->cbRow;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002439
2440 if (pDisplay != NULL)
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002441 pDisplay += dp->cbRow;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002442 }
2443
2444 /* This avoids false positives if the validation code is never called! */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002445 dp->ps->validated = 1;
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002446}
2447
2448static void
2449standard_end(png_structp pp, png_infop pi)
2450{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002451 standard_display *dp = png_get_progressive_ptr(pp);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002452
2453 UNUSED(pi);
2454
2455 /* Validate the image - progressive reading only produces one variant for
2456 * interlaced images.
2457 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002458 standard_image_validate(dp, pp, dp->ps->image, NULL);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002459}
2460
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002461/* A single test run checking the standard image to ensure it is not damaged. */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002462static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002463standard_test(png_store* PNG_CONST psIn, png_byte PNG_CONST colour_typeIn,
2464 png_byte PNG_CONST bit_depthIn, int interlace_typeIn)
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002465{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002466 standard_display d;
2467 context(psIn, fault);
2468
2469 /* Set up the display (stack frame) variables from the arguments to the
2470 * function and initialize the locals that are filled in later.
2471 */
2472 standard_display_init(&d, psIn, colour_typeIn, bit_depthIn,
2473 interlace_typeIn);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002474
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002475 /* Everything is protected by a Try/Catch. The functions called also
2476 * typically have local Try/Catch blocks.
2477 */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002478 Try
2479 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002480 png_structp pp;
2481 png_infop pi;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002482
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002483 /* Get a png_struct for writing the image. This will throw an error if it
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05002484 * fails, so we don't need to check the result.
2485 */
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002486 pp = set_store_for_read(d.ps, &pi, d.id,
2487 d.ps->progressive ? "progressive reader" : "sequential reader");
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002488
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002489 /* Introduce the correct read function. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002490 if (d.ps->progressive)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002491 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002492 png_set_progressive_read_fn(pp, &d, standard_info, progressive_row,
2493 standard_end);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002494
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002495 /* Now feed data into the reader until we reach the end: */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002496 store_progressive_read(d.ps, pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002497 }
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002498 else
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002499 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002500 /* Note that this takes the store, not the display. */
2501 png_set_read_fn(pp, d.ps, store_read);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002502
2503 /* Check the header values: */
2504 png_read_info(pp, pi);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002505
2506 /* The code tests both versions of the images that the sequential
2507 * reader can produce.
2508 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002509 standard_info_imp(&d, pp, pi, 2/*images*/);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002510
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002511 /* Need the total bytes in the image below; we can't get to this point
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002512 * unless the PNG file values have been checked against the expected
2513 * values.
2514 */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002515 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002516 PNG_CONST png_bytep pImage = d.ps->image;
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002517 PNG_CONST png_bytep pDisplay = pImage + d.cbRow * d.h;
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002518
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002519 sequential_row(&d, pp, pi, pImage, pDisplay);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002520
2521 /* After the last pass loop over the rows again to check that the
2522 * image is correct.
2523 */
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002524 standard_image_validate(&d, pp, pImage, pDisplay);
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002525 }
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002526 }
2527
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002528 /* Check for validation. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002529 if (!d.ps->validated)
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05002530 png_error(pp, "image read failed silently");
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002531
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002532 /* Successful completion. */
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002533 }
2534
2535 Catch(fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05002536 d.ps = fault; /* make sure this hasn't been clobbered. */
2537
2538 /* In either case clean up the store. */
2539 store_read_reset(d.ps);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002540}
2541
2542static int
2543test_standard(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002544 int bdlo, int PNG_CONST bdhi)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002545{
2546 for (; bdlo <= bdhi; ++bdlo)
2547 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002548 int interlace_type;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002549
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002550 for (interlace_type = PNG_INTERLACE_NONE;
2551 interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
2552 {
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002553 /* Test both sequential and standard readers here. */
2554 pm->this.progressive = !pm->this.progressive;
2555 standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type);
2556
2557 if (fail(pm))
2558 return 0;
2559
2560 pm->this.progressive = !pm->this.progressive;
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05002561 standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type);
2562
2563 if (fail(pm))
2564 return 0;
2565 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002566 }
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002567
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002568 return 1; /* keep going */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002569}
2570
2571static void
2572perform_standard_test(png_modifier *pm)
2573{
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002574 /* Test each colour type over the valid range of bit depths (expressed as
2575 * log2(bit_depth) in turn, stop as soon as any error is detected.
2576 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002577 if (!test_standard(pm, 0, 0, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002578 return;
2579
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002580 if (!test_standard(pm, 2, 3, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002581 return;
2582
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002583 if (!test_standard(pm, 3, 0, 3))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002584 return;
2585
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002586 if (!test_standard(pm, 4, 3, READ_BDHI))
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002587 return;
2588
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05002589 if (!test_standard(pm, 6, 3, READ_BDHI))
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05002590 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002591}
2592
2593
2594/********************************* GAMMA TESTS ********************************/
2595/* Gamma test images. */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002596typedef struct gamma_modification
2597{
2598 png_modification this;
2599 png_fixed_point gamma;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002600} gamma_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002601
2602static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002603gamma_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002604{
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05002605 UNUSED(add);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002606 /* This simply dumps the given gamma value into the buffer. */
2607 png_save_uint_32(pm->buffer, 4);
2608 png_save_uint_32(pm->buffer+4, CHUNK_gAMA);
2609 png_save_uint_32(pm->buffer+8, ((gamma_modification*)me)->gamma);
2610 return 1;
2611}
2612
2613static void
2614gamma_modification_init(gamma_modification *me, png_modifier *pm, double gamma)
2615{
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05002616 double g;
2617
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002618 modification_init(&me->this);
2619 me->this.chunk = CHUNK_gAMA;
2620 me->this.modify_fn = gamma_modify;
2621 me->this.add = CHUNK_PLTE;
Glenn Randers-Pehrson21b4b332010-08-18 07:12:38 -05002622 g = floor(gamma * 100000 + .5);
2623 me->gamma = (png_fixed_point)g;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002624 me->this.next = pm->modifications;
2625 pm->modifications = &me->this;
2626}
2627
2628typedef struct srgb_modification
2629{
2630 png_modification this;
2631 png_byte intent;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002632} srgb_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002633
2634static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002635srgb_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002636{
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05002637 UNUSED(add);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002638 /* As above, ignore add and just make a new chunk */
2639 png_save_uint_32(pm->buffer, 1);
2640 png_save_uint_32(pm->buffer+4, CHUNK_sRGB);
2641 pm->buffer[8] = ((srgb_modification*)me)->intent;
2642 return 1;
2643}
2644
2645static void
2646srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent)
2647{
2648 modification_init(&me->this);
2649 me->this.chunk = CHUNK_sBIT;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002650
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002651 if (intent <= 3) /* if valid, else *delete* sRGB chunks */
2652 {
2653 me->this.modify_fn = srgb_modify;
2654 me->this.add = CHUNK_PLTE;
2655 me->intent = intent;
2656 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002657
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002658 else
2659 {
2660 me->this.modify_fn = 0;
2661 me->this.add = 0;
2662 me->intent = 0;
2663 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002664
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002665 me->this.next = pm->modifications;
2666 pm->modifications = &me->this;
2667}
2668
2669typedef struct sbit_modification
2670{
2671 png_modification this;
2672 png_byte sbit;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002673} sbit_modification;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002674
2675static int
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002676sbit_modify(png_modifier *pm, png_modification *me, int add)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002677{
2678 png_byte sbit = ((sbit_modification*)me)->sbit;
2679 if (pm->bit_depth > sbit)
2680 {
2681 int cb = 0;
2682 switch (pm->colour_type)
2683 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002684 case 0:
2685 cb = 1;
2686 break;
2687
2688 case 2:
2689 case 3:
2690 cb = 3;
2691 break;
2692
2693 case 4:
2694 cb = 2;
2695 break;
2696
2697 case 6:
2698 cb = 4;
2699 break;
2700
2701 default:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002702 png_error(pm->this.pread,
2703 "unexpected colour type in sBIT modification");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002704 }
2705
2706 png_save_uint_32(pm->buffer, cb);
2707 png_save_uint_32(pm->buffer+4, CHUNK_sBIT);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05002708
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002709 while (cb > 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05002710 (pm->buffer+8)[--cb] = sbit;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05002711
2712 return 1;
2713 }
2714 else if (!add)
2715 {
2716 /* Remove the sBIT chunk */
2717 pm->buffer_count = pm->buffer_position = 0;
2718 return 1;
2719 }
2720 else
2721 return 0; /* do nothing */
2722}
2723
2724static void
2725sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit)
2726{
2727 modification_init(&me->this);
2728 me->this.chunk = CHUNK_sBIT;
2729 me->this.modify_fn = sbit_modify;
2730 me->this.add = CHUNK_PLTE;
2731 me->sbit = sbit;
2732 me->this.next = pm->modifications;
2733 pm->modifications = &me->this;
2734}
2735
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002736/* Reader callbacks and implementations, where they differ from the standard
2737 * ones.
2738 */
2739typedef struct gamma_display
2740{
2741 standard_display this;
2742
2743 /* Parameters */
2744 png_modifier* pm;
2745 double file_gamma;
2746 double screen_gamma;
2747 png_byte sbit;
2748 int threshold_test;
2749 PNG_CONST char* name;
2750 int speed;
2751 int use_input_precision;
2752 int strip16;
2753
2754 /* Local variables */
2755 double maxerrout;
2756 double maxerrpc;
2757 double maxerrabs;
2758} gamma_display;
2759
2760static void
2761gamma_display_init(gamma_display *dp, png_modifier *pm, png_byte colour_type,
2762 png_byte bit_depth, int interlace_type, double file_gamma,
2763 double screen_gamma, png_byte sbit, int threshold_test, int speed,
2764 int use_input_precision, int strip16)
2765{
2766 /* Standard fields */
2767 standard_display_init(&dp->this, &pm->this, colour_type, bit_depth,
2768 interlace_type);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -06002769
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002770 /* Parameter fields */
2771 dp->pm = pm;
2772 dp->file_gamma = file_gamma;
2773 dp->screen_gamma = screen_gamma;
2774 dp->sbit = sbit;
2775 dp->threshold_test = threshold_test;
2776 dp->speed = speed;
2777 dp->use_input_precision = use_input_precision;
2778 dp->strip16 = strip16;
2779
2780 /* Local variable fields */
2781 dp->maxerrout = dp->maxerrpc = dp->maxerrabs = 0;
2782}
2783
2784static void
2785gamma_info_imp(gamma_display *dp, png_structp pp, png_infop pi)
2786{
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05002787 /* Reuse the standard stuff as appropriate. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002788 standard_info_part1(&dp->this, pp, pi);
2789
2790 /* If requested strip 16 to 8 bits - this is handled automagically below
2791 * because the output bit depth is read from the library. Note that there
2792 * are interactions with sBIT but, internally, libpng makes sbit at most
2793 * PNG_MAX_GAMMA_8 when doing the following.
2794 */
2795 if (dp->strip16)
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05002796# ifdef PNG_READ_16_TO_8
2797 png_set_strip_16(pp);
2798# else
2799 png_error(pp, "strip16 (16 to 8 bit conversion) not supported");
2800# endif
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002801
2802 png_read_update_info(pp, pi);
2803
2804 /* Now we may get a different cbRow: */
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002805 standard_info_part2(&dp->this, pp, pi, 1 /*images*/);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002806}
2807
2808static void
2809gamma_info(png_structp pp, png_infop pi)
2810{
2811 gamma_info_imp(png_get_progressive_ptr(pp), pp, pi);
2812}
2813
2814static void
2815gamma_image_validate(gamma_display *dp, png_structp pp, png_infop pi,
2816 png_const_bytep pRow)
2817{
2818 /* Get some constants derived from the input and output file formats: */
2819 PNG_CONST png_byte sbit = dp->sbit;
2820 PNG_CONST double file_gamma = dp->file_gamma;
2821 PNG_CONST double screen_gamma = dp->screen_gamma;
2822 PNG_CONST int use_input_precision = dp->use_input_precision;
2823 PNG_CONST int speed = dp->speed;
2824 PNG_CONST png_byte in_ct = dp->this.colour_type;
2825 PNG_CONST png_byte in_bd = dp->this.bit_depth;
2826 PNG_CONST png_uint_32 w = dp->this.w;
2827 PNG_CONST png_uint_32 h = dp->this.h;
2828 PNG_CONST size_t cbRow = dp->this.cbRow;
2829 PNG_CONST png_byte out_ct = png_get_color_type(pp, pi);
2830 PNG_CONST png_byte out_bd = png_get_bit_depth(pp, pi);
2831 PNG_CONST unsigned int outmax = (1U<<out_bd)-1;
2832 PNG_CONST double maxabs = abserr(dp->pm, out_bd);
2833 PNG_CONST double maxout = outerr(dp->pm, out_bd);
2834 PNG_CONST double maxpc = pcerr(dp->pm, out_bd);
2835
2836 /* There are three sources of error, firstly the quantization in the
2837 * file encoding, determined by sbit and/or the file depth, secondly
2838 * the output (screen) gamma and thirdly the output file encoding.
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002839 *
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002840 * Since this API receives the screen and file gamma in double
2841 * precision it is possible to calculate an exact answer given an input
2842 * pixel value. Therefore we assume that the *input* value is exact -
2843 * sample/maxsample - calculate the corresponding gamma corrected
2844 * output to the limits of double precision arithmetic and compare with
2845 * what libpng returns.
2846 *
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002847 * Since the library must quantize the output to 8 or 16 bits there is
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002848 * a fundamental limit on the accuracy of the output of +/-.5 - this
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002849 * quantization limit is included in addition to the other limits
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002850 * specified by the paramaters to the API. (Effectively, add .5
2851 * everywhere.)
2852 *
2853 * The behavior of the 'sbit' paramter is defined by section 12.5
2854 * (sample depth scaling) of the PNG spec. That section forces the
2855 * decoder to assume that the PNG values have been scaled if sBIT is
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002856 * present:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002857 *
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05002858 * png-sample = floor( input-sample * (max-out/max-in) + .5);
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002859 *
2860 * This means that only a subset of the possible PNG values should
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002861 * appear in the input. However, the spec allows the encoder to use a
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002862 * variety of approximations to the above and doesn't require any
2863 * restriction of the values produced.
2864 *
2865 * Nevertheless the spec requires that the upper 'sBIT' bits of the
2866 * value stored in a PNG file be the original sample bits.
2867 * Consequently the code below simply scales the top sbit bits by
2868 * (1<<sbit)-1 to obtain an original sample value.
2869 *
2870 * Because there is limited precision in the input it is arguable that
2871 * an acceptable result is any valid result from input-.5 to input+.5.
2872 * The basic tests below do not do this, however if
2873 * 'use_input_precision' is set a subsequent test is performed below.
2874 */
2875 PNG_CONST int processing = (fabs(screen_gamma*file_gamma-1) >=
2876 PNG_GAMMA_THRESHOLD && !dp->threshold_test && !speed && in_ct != 3) ||
2877 in_bd != out_bd;
2878
2879 PNG_CONST unsigned int samples_per_pixel = (out_ct & 2U) ? 3U : 1U;
2880
2881 PNG_CONST double gamma = 1/(file_gamma*screen_gamma); /* Overall */
2882
2883 double maxerrout = 0, maxerrabs = 0, maxerrpc = 0;
2884 png_uint_32 y;
2885
2886 for (y=0; y<h; ++y, pRow += cbRow)
2887 {
2888 unsigned int s, x;
2889 png_byte std[STD_ROWMAX];
2890
2891 standard_row(pp, std, in_ct, in_bd, y);
2892
2893 if (processing)
2894 {
2895 for (x=0; x<w; ++x) for (s=0; s<samples_per_pixel; ++s)
2896 {
2897 /* Input sample values: */
2898 PNG_CONST unsigned int
2899 id = sample(std, in_ct, in_bd, x, s);
2900
2901 PNG_CONST unsigned int
2902 od = sample(pRow, out_ct, out_bd, x, s);
2903
2904 PNG_CONST unsigned int
2905 isbit = id >> (in_bd-sbit);
2906
2907 double i, sample, encoded_sample, output, encoded_error, error;
2908 double es_lo, es_hi;
2909
2910 /* First check on the 'perfect' result obtained from the
2911 * digitized input value, id, and compare this against the
2912 * actual digitized result, 'od'. 'i' is the input result
2913 * in the range 0..1:
2914 *
2915 * NOTE: sBIT should be taken into account here but isn't,
2916 * as described above.
2917 */
2918 i = isbit; i /= (1U<<sbit)-1;
2919
2920 /* Then get the gamma corrected version of 'i' and compare
2921 * to 'od', any error less than .5 is insignificant - just
2922 * quantization of the output value to the nearest digital
2923 * value (nevertheless the error is still recorded - it's
2924 * interesting ;-)
2925 */
2926 encoded_sample = pow(i, gamma) * outmax;
2927 encoded_error = fabs(od-encoded_sample);
2928
2929 if (encoded_error > maxerrout)
2930 maxerrout = encoded_error;
2931
2932 if (encoded_error < .5+maxout)
2933 continue;
2934
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002935 /* There may be an error, so calculate the actual sample
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002936 * values - unencoded light intensity values. Note that
2937 * in practice these are not unencoded because they
2938 * include a 'viewing correction' to decrease or
2939 * (normally) increase the perceptual contrast of the
2940 * image. There's nothing we can do about this - we don't
2941 * know what it is - so assume the unencoded value is
2942 * perceptually linear.
2943 */
2944 sample = pow(i, 1/file_gamma); /* In range 0..1 */
2945 output = od;
2946 output /= outmax;
2947 output = pow(output, screen_gamma);
2948
2949 /* Now we have the numbers for real errors, both absolute
2950 * values as as a percentage of the correct value (output):
2951 */
2952 error = fabs(sample-output);
2953
2954 if (error > maxerrabs)
2955 maxerrabs = error;
2956
2957 /* The following is an attempt to ignore the tendency of
2958 * quantization to dominate the percentage errors for low
2959 * output sample values:
2960 */
2961 if (sample*maxpc > .5+maxabs)
2962 {
2963 double pcerr = error/sample;
2964 if (pcerr > maxerrpc) maxerrpc = pcerr;
2965 }
2966
2967 /* Now calculate the digitization limits for
2968 * 'encoded_sample' using the 'max' values. Note that
2969 * maxout is in the encoded space but maxpc and maxabs are
2970 * in linear light space.
2971 *
2972 * First find the maximum error in linear light space,
2973 * range 0..1:
2974 */
2975 {
2976 double tmp = sample * maxpc;
2977 if (tmp < maxabs) tmp = maxabs;
2978
2979 /* Low bound - the minimum of the three: */
2980 es_lo = encoded_sample - maxout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002981
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002982 if (es_lo > 0 && sample-tmp > 0)
2983 {
2984 double l = outmax * pow(sample-tmp, 1/screen_gamma);
2985 if (l < es_lo) es_lo = l;
2986 }
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002987
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002988 else
2989 es_lo = 0;
2990
2991 es_hi = encoded_sample + maxout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002992
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002993 if (es_hi < outmax && sample+tmp < 1)
2994 {
2995 double h = outmax * pow(sample+tmp, 1/screen_gamma);
2996 if (h > es_hi) es_hi = h;
2997 }
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06002998
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05002999 else
3000 es_hi = outmax;
3001 }
3002
3003 /* The primary test is that the final encoded value
3004 * returned by the library should be between the two limits
3005 * (inclusive) that were calculated above. At this point
3006 * quantization of the output must be taken into account.
3007 */
3008 if (od+.5 < es_lo || od-.5 > es_hi)
3009 {
3010 /* There has been an error in processing. */
3011 double is_lo, is_hi;
3012
3013 if (use_input_precision)
3014 {
3015 /* Ok, something is wrong - this actually happens in
3016 * current libpng sbit processing. Assume that the
3017 * input value (id, adjusted for sbit) can be
3018 * anywhere between value-.5 and value+.5 - quite a
3019 * large range if sbit is low.
3020 */
3021 double tmp = (isbit - .5)/((1U<<sbit)-1);
3022
3023 if (tmp > 0)
3024 {
3025 is_lo = outmax * pow(tmp, gamma) - maxout;
3026 if (is_lo < 0) is_lo = 0;
3027 }
3028
3029 else
3030 is_lo = 0;
3031
3032 tmp = (isbit + .5)/((1U<<sbit)-1);
3033
3034 if (tmp < 1)
3035 {
3036 is_hi = outmax * pow(tmp, gamma) + maxout;
3037 if (is_hi > outmax) is_hi = outmax;
3038 }
3039
3040 else
3041 is_hi = outmax;
3042
3043 if (!(od+.5 < is_lo || od-.5 > is_hi))
3044 continue;
3045 }
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003046 else
3047 is_lo = es_lo, is_hi = es_hi;
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003048
3049 {
3050 char msg[256];
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003051
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003052 sprintf(msg,
3053 "error: %.3f; %u{%u;%u} -> %u not %.2f (%.1f-%.1f)",
3054 od-encoded_sample, id, sbit, isbit, od,
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003055 encoded_sample, is_lo, is_hi);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003056
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003057 png_warning(pp, msg);
3058 }
3059 }
3060 }
3061 }
3062
3063 else if (!speed && memcmp(std, pRow, cbRow) != 0)
3064 {
3065 char msg[64];
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003066
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003067 /* No transform is expected on the threshold tests. */
3068 sprintf(msg, "gamma: below threshold row %d changed", y);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003069
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003070 png_error(pp, msg);
3071 }
3072 } /* row (y) loop */
3073
3074 dp->maxerrout = maxerrout;
3075 dp->maxerrabs = maxerrabs;
3076 dp->maxerrpc = maxerrpc;
3077 dp->this.ps->validated = 1;
3078}
3079
3080static void
3081gamma_end(png_structp pp, png_infop pi)
3082{
3083 gamma_display *dp = png_get_progressive_ptr(pp);
3084
3085 gamma_image_validate(dp, pp, pi, dp->this.ps->image);
3086}
3087
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05003088/* A single test run checking a gamma transformation.
3089 *
3090 * maxabs: maximum absolute error as a fraction
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003091 * maxout: maximum output error in the output units
3092 * maxpc: maximum percentage error (as a percentage)
3093 */
3094static void
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003095gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn,
3096 PNG_CONST png_byte bit_depthIn, PNG_CONST int interlace_typeIn,
3097 PNG_CONST double file_gammaIn, PNG_CONST double screen_gammaIn,
3098 PNG_CONST png_byte sbitIn, PNG_CONST int threshold_testIn,
3099 PNG_CONST char *name, PNG_CONST int speedIn,
3100 PNG_CONST int use_input_precisionIn, PNG_CONST int strip16In)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003101{
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003102 gamma_display d;
3103 context(&pmIn->this, fault);
3104
3105 gamma_display_init(&d, pmIn, colour_typeIn, bit_depthIn, interlace_typeIn,
3106 file_gammaIn, screen_gammaIn, sbitIn, threshold_testIn, speedIn,
3107 use_input_precisionIn, strip16In);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003108
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003109 Try
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003110 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003111 png_structp pp;
3112 png_infop pi;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003113 gamma_modification gamma_mod;
3114 srgb_modification srgb_mod;
3115 sbit_modification sbit_mod;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003116
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003117 /* Make an appropriate modifier to set the PNG file gamma to the
3118 * given gamma value and the sBIT chunk to the given precision.
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003119 */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003120 d.pm->modifications = NULL;
3121 gamma_modification_init(&gamma_mod, d.pm, d.file_gamma);
3122 srgb_modification_init(&srgb_mod, d.pm, 127/*delete*/);
3123 sbit_modification_init(&sbit_mod, d.pm, d.sbit);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003124
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003125 modification_reset(d.pm->modifications);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003126
Glenn Randers-Pehrsonf18a0ed2010-08-24 08:41:00 -05003127 /* Get a png_struct for writing the image. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003128 pp = set_modifier_for_read(d.pm, &pi, d.this.id, name);
Glenn Randers-Pehrson949d46c2010-08-24 08:29:58 -05003129
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003130 /* Set up gamma processing. */
Glenn Randers-Pehrson15333cd2010-08-24 15:29:52 -05003131#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003132 png_set_gamma(pp, d.screen_gamma, d.file_gamma);
Glenn Randers-Pehrson15333cd2010-08-24 15:29:52 -05003133#else
3134 {
3135 png_fixed_point s = floor(d.screen_gamma*100000+.5);
3136 png_fixed_point f = floor(d.file_gamma*100000+.5);
3137 png_set_gamma_fixed(pp, s, f);
3138 }
3139#endif
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003140
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003141 /* Introduce the correct read function. */
3142 if (d.pm->this.progressive)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003143 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003144 /* Share the row function with the standard implementation. */
3145 png_set_progressive_read_fn(pp, &d, gamma_info, progressive_row,
3146 gamma_end);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003147
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003148 /* Now feed data into the reader until we reach the end: */
3149 modifier_progressive_read(d.pm, pp, pi);
3150 }
3151 else
3152 {
3153 /* modifier_read expects a png_modifier* */
3154 png_set_read_fn(pp, d.pm, modifier_read);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003155
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003156 /* Check the header values: */
3157 png_read_info(pp, pi);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003158
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003159 /* Process the 'info' requirements. Only one image is generated */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003160 gamma_info_imp(&d, pp, pi);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003161
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05003162 sequential_row(&d.this, pp, pi, NULL, d.this.ps->image);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003163
Glenn Randers-Pehrson9b780b82010-08-24 08:50:01 -05003164 gamma_image_validate(&d, pp, pi, d.this.ps->image);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003165 }
3166
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003167 modifier_reset(d.pm);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003168
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003169 if (d.pm->log && !d.threshold_test && !d.speed)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003170 fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n",
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003171 d.this.bit_depth, colour_types[d.this.colour_type], d.name,
3172 d.maxerrout, d.maxerrabs, 100*d.maxerrpc);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003173
3174 /* Log the summary values too. */
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003175 if (d.this.colour_type == 0 || d.this.colour_type == 4)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003176 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003177 switch (d.this.bit_depth)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003178 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003179 case 1:
3180 break;
3181
3182 case 2:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003183 if (d.maxerrout > d.pm->error_gray_2)
3184 d.pm->error_gray_2 = d.maxerrout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003185
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003186 break;
3187
3188 case 4:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003189 if (d.maxerrout > d.pm->error_gray_4)
3190 d.pm->error_gray_4 = d.maxerrout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003191
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003192 break;
3193
3194 case 8:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003195 if (d.maxerrout > d.pm->error_gray_8)
3196 d.pm->error_gray_8 = d.maxerrout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003197
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003198 break;
3199
3200 case 16:
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003201 if (d.maxerrout > d.pm->error_gray_16)
3202 d.pm->error_gray_16 = d.maxerrout;
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003203
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003204 break;
3205
3206 default:
3207 png_error(pp, "bad bit depth (internal: 1)");
3208 }
3209 }
3210
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003211 else if (d.this.colour_type == 2 || d.this.colour_type == 6)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003212 {
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003213 switch (d.this.bit_depth)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003214 {
3215 case 8:
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003216
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003217 if (d.maxerrout > d.pm->error_color_8)
3218 d.pm->error_color_8 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003219
3220 break;
3221
3222 case 16:
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003223
Glenn Randers-Pehrson0f211612010-08-24 08:46:53 -05003224 if (d.maxerrout > d.pm->error_color_16)
3225 d.pm->error_color_16 = d.maxerrout;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003226
3227 break;
3228
3229 default:
3230 png_error(pp, "bad bit depth (internal: 2)");
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003231 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003232 }
3233 }
3234
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003235 Catch(fault)
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003236 modifier_reset((png_modifier*)fault);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003237}
3238
3239static void gamma_threshold_test(png_modifier *pm, png_byte colour_type,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003240 png_byte bit_depth, int interlace_type, double file_gamma,
3241 double screen_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003242{
3243 size_t pos = 0;
3244 char name[64];
3245 pos = safecat(name, sizeof name, pos, "threshold ");
3246 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
3247 pos = safecat(name, sizeof name, pos, "/");
3248 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
3249
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003250 (void)gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma,
3251 screen_gamma, bit_depth, 1, name, 0/*speed*/, 0/*no input precision*/,
3252 0/*no strip16*/);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003253}
3254
3255static void
3256perform_gamma_threshold_tests(png_modifier *pm)
3257{
3258 png_byte colour_type = 0;
3259 png_byte bit_depth = 0;
3260
3261 while (next_format(&colour_type, &bit_depth))
3262 {
3263 double gamma = 1.0;
3264 while (gamma >= .4)
3265 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003266 /* There's little point testing the interlacing vs non-interlacing,
3267 * but this can be set from the command line.
3268 */
3269 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
3270 gamma, 1/gamma);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003271 gamma *= .95;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003272 }
3273
3274 /* And a special test for sRGB */
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003275 gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type,
3276 .45455, 2.2);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003277
3278 if (fail(pm))
3279 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003280 }
3281}
3282
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003283static void gamma_transform_test(png_modifier *pm,
3284 PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003285 PNG_CONST int interlace_type, PNG_CONST double file_gamma,
3286 PNG_CONST double screen_gamma, PNG_CONST png_byte sbit, PNG_CONST int speed,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003287 PNG_CONST int use_input_precision, PNG_CONST int strip16)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003288{
3289 size_t pos = 0;
3290 char name[64];
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003291
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003292 if (sbit != bit_depth)
3293 {
3294 pos = safecat(name, sizeof name, pos, "sbit(");
3295 pos = safecatn(name, sizeof name, pos, sbit);
3296 pos = safecat(name, sizeof name, pos, ") ");
3297 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003298
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003299 else
3300 pos = safecat(name, sizeof name, pos, "gamma ");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003301
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003302 if (strip16)
3303 pos = safecat(name, sizeof name, pos, "16to8 ");
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003304
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003305 pos = safecatd(name, sizeof name, pos, file_gamma, 3);
3306 pos = safecat(name, sizeof name, pos, "->");
3307 pos = safecatd(name, sizeof name, pos, screen_gamma, 3);
3308
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003309 gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma,
3310 screen_gamma, sbit, 0, name, speed, use_input_precision, strip16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003311}
3312
3313static void perform_gamma_transform_tests(png_modifier *pm, int speed)
3314{
3315 png_byte colour_type = 0;
3316 png_byte bit_depth = 0;
3317
3318 /* Ignore palette images - the gamma correction happens on the palette entry,
3319 * haven't got the tests for this yet.
3320 */
3321 while (next_format(&colour_type, &bit_depth)) if (colour_type != 3)
3322 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003323 unsigned int i, j;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003324
3325 for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) if (i != j)
3326 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003327 gamma_transform_test(pm, colour_type, bit_depth, pm->interlace_type,
3328 1/pm->gammas[i], pm->gammas[j], bit_depth, speed,
3329 pm->use_input_precision, 0/*do not strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003330
3331 if (fail(pm))
3332 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003333 }
3334 }
3335}
3336
3337static void perform_gamma_sbit_tests(png_modifier *pm, int speed)
3338{
3339 png_byte sbit;
3340
3341 /* The only interesting cases are colour and grayscale, alpha is ignored here
3342 * for overall speed. Only bit depths 8 and 16 are tested.
3343 */
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003344 for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003345 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003346 unsigned int i, j;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003347
3348 for (i=0; i<pm->ngammas; ++i)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003349 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003350 for (j=0; j<pm->ngammas; ++j)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003351 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003352 if (i != j)
3353 {
3354 if (sbit < 8)
3355 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003356 gamma_transform_test(pm, 0, 8, pm->interlace_type,
3357 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3358 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003359
3360 if (fail(pm))
3361 return;
3362
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003363 gamma_transform_test(pm, 2, 8, pm->interlace_type,
3364 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3365 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003366
3367 if (fail(pm))
3368 return;
3369 }
3370
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003371#ifdef DO_16BIT
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003372 gamma_transform_test(pm, 0, 16, pm->interlace_type,
3373 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3374 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003375
3376 if (fail(pm))
3377 return;
3378
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003379 gamma_transform_test(pm, 2, 16, pm->interlace_type,
3380 1/pm->gammas[i], pm->gammas[j], sbit, speed,
3381 pm->use_input_precision_sbit, 0/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003382
3383 if (fail(pm))
3384 return;
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003385#endif
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003386 }
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003387 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003388 }
3389 }
3390}
3391
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003392/* Note that this requires a 16 bit source image but produces 8 bit output, so
3393 * we only need the 16bit write support.
3394 */
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003395#ifdef PNG_16_TO_8_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003396static void perform_gamma_strip16_tests(png_modifier *pm, int speed)
3397{
3398# ifndef PNG_MAX_GAMMA_8
3399# define PNG_MAX_GAMMA_8 11
3400# endif
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003401 /* Include the alpha cases here. Note that sbit matches the internal value
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003402 * used by the library - otherwise we will get spurious errors from the
3403 * internal sbit style approximation.
3404 *
Glenn Randers-Pehrson233357e2010-07-29 21:49:38 -05003405 * The threshold test is here because otherwise the 16 to 8 conversion will
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003406 * proceed *without* gamma correction, and the tests above will fail (but not
3407 * by much) - this could be fixed, it only appears with the -g option.
3408 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05003409 unsigned int i, j;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003410 for (i=0; i<pm->ngammas; ++i)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003411 {
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003412 for (j=0; j<pm->ngammas; ++j)
3413 {
3414 if (i != j &&
3415 fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD)
3416 {
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003417 gamma_transform_test(pm, 0, 16, pm->interlace_type, 1/pm->gammas[i],
3418 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3419 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003420
3421 if (fail(pm))
3422 return;
3423
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003424 gamma_transform_test(pm, 2, 16, pm->interlace_type, 1/pm->gammas[i],
3425 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3426 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003427
3428 if (fail(pm))
3429 return;
3430
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003431 gamma_transform_test(pm, 4, 16, pm->interlace_type, 1/pm->gammas[i],
3432 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3433 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003434
3435 if (fail(pm))
3436 return;
3437
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003438 gamma_transform_test(pm, 6, 16, pm->interlace_type, 1/pm->gammas[i],
3439 pm->gammas[j], PNG_MAX_GAMMA_8, speed,
3440 pm->use_input_precision_16to8, 1/*strip16*/);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003441
3442 if (fail(pm))
3443 return;
3444 }
3445 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003446 }
3447}
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003448#endif /* 16 to 8 bit conversion */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003449
3450static void
3451perform_gamma_test(png_modifier *pm, int speed, int summary)
3452{
3453 /* First some arbitrary no-transform tests: */
3454 if (!speed)
3455 {
3456 perform_gamma_threshold_tests(pm);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003457
3458 if (fail(pm))
3459 return;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003460 }
3461
3462 /* Now some real transforms. */
3463 perform_gamma_transform_tests(pm, speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003464
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003465 if (summary)
3466 {
3467 printf("Gamma correction error summary (output value error):\n");
3468 printf(" 2 bit gray: %.5f\n", pm->error_gray_2);
3469 printf(" 4 bit gray: %.5f\n", pm->error_gray_4);
3470 printf(" 8 bit gray: %.5f\n", pm->error_gray_8);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003471 printf(" 8 bit color: %.5f\n", pm->error_color_8);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003472#ifdef DO_16BIT
3473 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003474 printf(" 16 bit color: %.5f\n", pm->error_color_16);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003475#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003476 }
3477
3478 /* The sbit tests produce much larger errors: */
3479 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 =
3480 pm->error_color_8 = pm->error_color_16 = 0;
3481 perform_gamma_sbit_tests(pm, speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003482
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003483 if (summary)
3484 {
3485 printf("Gamma correction with sBIT:\n");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003486
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003487 if (pm->sbitlow < 8U)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003488 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003489 printf(" 2 bit gray: %.5f\n", pm->error_gray_2);
3490 printf(" 4 bit gray: %.5f\n", pm->error_gray_4);
3491 printf(" 8 bit gray: %.5f\n", pm->error_gray_8);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003492 printf(" 8 bit color: %.5f\n", pm->error_color_8);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003493 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003494
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003495#ifdef DO_16BIT
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003496 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003497 printf(" 16 bit color: %.5f\n", pm->error_color_16);
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003498#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003499 }
3500
Glenn Randers-Pehrsonfded04f2010-08-27 14:21:21 -05003501#ifdef PNG_16_TO_8_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003502 /* The 16 to 8 bit strip operations: */
3503 pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 =
3504 pm->error_color_8 = pm->error_color_16 = 0;
3505 perform_gamma_strip16_tests(pm, speed);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003506
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003507 if (summary)
3508 {
3509 printf("Gamma correction with 16 to 8 bit reduction:\n");
3510 printf(" 16 bit gray: %.5f\n", pm->error_gray_16);
3511 printf(" 16 bit color: %.5f\n", pm->error_color_16);
3512 }
Glenn Randers-Pehrson2f702822010-08-27 06:39:23 -05003513#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003514}
3515
3516/* main program */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003517int main(int argc, PNG_CONST char **argv)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003518{
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003519 volatile int summary = 1; /* Print the error sumamry at the end */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003520
3521 /* Create the given output file on success: */
3522 PNG_CONST char *volatile touch = NULL;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003523
3524 /* This is an array of standard gamma values (believe it or not I've seen
3525 * every one of these mentioned somewhere.)
3526 *
3527 * In the following list the most useful values are first!
3528 */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003529 static double
3530 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 -05003531
3532 png_modifier pm;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003533 context(&pm.this, fault);
3534
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003535 modifier_init(&pm);
3536
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003537 /* Preallocate the image buffer, because we know how big it needs to be,
3538 * note that, for testing purposes, it is deliberately mis-aligned.
3539 */
3540 pm.this.image = malloc(2*STD_IMAGEMAX+1);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003541
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003542 if (pm.this.image != NULL)
3543 {
3544 /* Ignore OOM at this point - the 'ensure' routine above will allocate the
3545 * array appropriately.
3546 */
3547 ++(pm.this.image);
3548 pm.this.cb_image = 2*STD_IMAGEMAX;
3549 }
3550
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003551 /* Default to error on warning: */
3552 pm.this.treat_warnings_as_errors = 1;
3553
3554 /* Store the test gammas */
3555 pm.gammas = gammas;
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003556 pm.ngammas = 3U; /* for speed */
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003557 pm.sbitlow = 8U; /* because libpng doesn't do sBIT below 8! */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003558 pm.use_input_precision_16to8 = 1U; /* Because of the way libpng does it */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003559
3560 /* Some default values (set the behavior for 'make check' here) */
3561 pm.maxout8 = .1; /* Arithmetic error in *encoded* value */
3562 pm.maxabs8 = .00005; /* 1/20000 */
3563 pm.maxpc8 = .499; /* I.e. .499% fractional error */
3564 pm.maxout16 = .499; /* Error in *encoded* value */
3565 pm.maxabs16 = .00005;/* 1/20000 */
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003566
3567 /* NOTE: this is a reasonable perceptual limit. We assume that humans can
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003568 * perceive light level differences of 1% over a 100:1 range, so we need to
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003569 * maintain 1 in 10000 accuracy (in linear light space), which is what the
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003570 * following guarantees. It also allows significantly higher errors at
3571 * higher 16 bit values, which is important for performance. The actual
3572 * maximum 16 bit error is about +/-1.9 in the fixed point implementation but
3573 * this is only allowed for values >38149 by the following:
3574 */
3575 pm.maxpc16 = .005; /* I.e. 1/200% - 1/20000 */
3576
3577 /* Now parse the command line options. */
3578 while (--argc >= 1)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003579 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003580 if (strcmp(*++argv, "-v") == 0)
3581 pm.this.verbose = 1;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003582
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003583 else if (strcmp(*argv, "-l") == 0)
3584 pm.log = 1;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003585
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003586 else if (strcmp(*argv, "-q") == 0)
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05003587 summary = pm.this.verbose = pm.log = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003588
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003589 else if (strcmp(*argv, "-g") == 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003590 pm.ngammas = (sizeof gammas)/(sizeof gammas[0]);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003591
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003592 else if (strcmp(*argv, "-w") == 0)
3593 pm.this.treat_warnings_as_errors = 0;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003594
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003595 else if (strcmp(*argv, "--speed") == 0)
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003596 pm.this.speed = 1, pm.ngammas = (sizeof gammas)/(sizeof gammas[0]);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003597
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003598 else if (strcmp(*argv, "--nogamma") == 0)
3599 pm.ngammas = 0;
3600
Glenn Randers-Pehrsondb712a92010-08-24 08:44:14 -05003601 else if (strcmp(*argv, "--progressive-read") == 0)
3602 pm.this.progressive = 1;
3603
Glenn Randers-Pehrson21af4cc2010-08-24 08:33:28 -05003604 else if (strcmp(*argv, "--interlace") == 0)
3605 pm.interlace_type = PNG_INTERLACE_ADAM7;
3606
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003607 else if (argc >= 1 && strcmp(*argv, "--sbitlow") == 0)
3608 --argc, pm.sbitlow = (png_byte)atoi(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003609
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003610 else if (argc >= 1 && strcmp(*argv, "--touch") == 0)
3611 --argc, touch = *++argv;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003612
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003613 else if (argc >= 1 && strncmp(*argv, "--max", 4) == 0)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003614 {
3615 --argc;
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003616
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003617 if (strcmp(4+*argv, "abs8") == 0)
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003618 pm.maxabs8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003619
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003620 else if (strcmp(4+*argv, "abs16") == 0)
3621 pm.maxabs16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003622
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003623 else if (strcmp(4+*argv, "out8") == 0)
3624 pm.maxout8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003625
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003626 else if (strcmp(4+*argv, "out16") == 0)
3627 pm.maxout16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003628
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003629 else if (strcmp(4+*argv, "pc8") == 0)
3630 pm.maxpc8 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003631
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003632 else if (strcmp(4+*argv, "pc16") == 0)
3633 pm.maxpc16 = atof(*++argv);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003634
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003635 else
3636 {
3637 fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv);
3638 exit(1);
3639 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003640 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003641
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003642 else
3643 {
3644 fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv);
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003645 exit(1);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003646 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003647 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003648
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003649 Try
3650 {
3651 /* Make useful base images */
3652 make_standard_images(&pm.this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003653
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003654 /* Perform the standard and gamma tests. */
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003655 if (!pm.this.speed)
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003656 {
3657 perform_standard_test(&pm);
3658 perform_error_test(&pm);
3659 }
3660
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003661 perform_gamma_test(&pm, pm.this.speed != 0, summary && !pm.this.speed);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003662 }
3663
3664 Catch(fault)
3665 {
Glenn Randers-Pehrson438b3ca2010-08-24 08:55:40 -05003666 fprintf(stderr, "pngvalid: test aborted (probably failed in cleanup)\n");
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003667 if (!pm.this.verbose)
3668 {
3669 if (pm.this.error[0] != 0)
3670 fprintf(stderr, "pngvalid: first error: %s\n", pm.this.error);
Glenn Randers-Pehrson38ef3a52010-12-03 11:22:31 -06003671
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003672 fprintf(stderr, "pngvalid: run with -v to see what happened\n");
3673 }
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003674 exit(1);
3675 }
3676
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003677 if (summary && !pm.this.speed)
3678 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003679 printf("Results using %s point arithmetic %s\n",
3680#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500
3681 "floating",
3682#else
3683 "fixed",
3684#endif
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003685 (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
3686 pm.this.nwarnings)) ? "(errors)" : (pm.this.nwarnings ?
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05003687 "(warnings)" : "(no errors or warnings)")
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003688 );
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003689 printf("Allocated memory statistics (in bytes):\n"
Glenn Randers-Pehrson9a75d992010-10-08 16:27:14 -05003690 "\tread %lu maximum single, %lu peak, %lu total\n"
3691 "\twrite %lu maximum single, %lu peak, %lu total\n",
3692 (unsigned long)pm.this.read_memory_pool.max_max,
3693 (unsigned long)pm.this.read_memory_pool.max_limit,
3694 (unsigned long)pm.this.read_memory_pool.max_total,
3695 (unsigned long)pm.this.write_memory_pool.max_max,
3696 (unsigned long)pm.this.write_memory_pool.max_limit,
3697 (unsigned long)pm.this.write_memory_pool.max_total);
Glenn Randers-Pehrson921d9152010-08-24 08:26:54 -05003698 }
3699
3700 /* Do this here to provoke memory corruption errors in memory not directly
3701 * allocated by libpng - not a complete test, but better than nothing.
3702 */
3703 store_delete(&pm.this);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003704
3705 /* Error exit if there are any errors, and maybe if there are any
3706 * warnings.
3707 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003708 if (pm.this.nerrors || (pm.this.treat_warnings_as_errors &&
3709 pm.this.nwarnings))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003710 {
3711 if (!pm.this.verbose)
3712 fprintf(stderr, "pngvalid: %s\n", pm.this.error);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003713
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003714 fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors,
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003715 pm.this.nwarnings);
3716
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003717 exit(1);
3718 }
3719
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003720 /* Success case. */
3721 if (touch != NULL)
3722 {
3723 FILE *fsuccess = fopen(touch, "wt");
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003724
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003725 if (fsuccess != NULL)
3726 {
3727 int error = 0;
3728 fprintf(fsuccess, "PNG validation succeeded\n");
3729 fflush(fsuccess);
3730 error = ferror(fsuccess);
Glenn Randers-Pehrsonc08cae12010-08-20 09:55:01 -05003731
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -05003732 if (fclose(fsuccess) || error)
3733 {
3734 fprintf(stderr, "%s: write failed\n", touch);
3735 exit(1);
3736 }
3737 }
3738 }
3739
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05003740 return 0;
3741}