]>
Commit | Line | Data |
---|---|---|
0272a10d VZ |
1 | |
2 | /* pngtest.c - a simple test program to test libpng | |
3 | * | |
9c0d9ce3 DS |
4 | * Last changed in libpng 1.5.6 [November 3, 2011] |
5 | * Copyright (c) 1998-2011 Glenn Randers-Pehrson | |
0272a10d VZ |
6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | |
8 | * | |
b61cc19c PC |
9 | * This code is released under the libpng license. |
10 | * For conditions of distribution and use, see the disclaimer | |
11 | * and license in png.h | |
12 | * | |
0272a10d VZ |
13 | * This program reads in a PNG image, writes it out again, and then |
14 | * compares the two files. If the files are identical, this shows that | |
15 | * the basic chunk handling, filtering, and (de)compression code is working | |
16 | * properly. It does not currently test all of the transforms, although | |
17 | * it probably should. | |
18 | * | |
19 | * The program will report "FAIL" in certain legitimate cases: | |
20 | * 1) when the compression level or filter selection method is changed. | |
21 | * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192. | |
22 | * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks | |
23 | * exist in the input file. | |
24 | * 4) others not listed here... | |
25 | * In these cases, it is best to check with another tool such as "pngcheck" | |
26 | * to see what the differences between the two files are. | |
27 | * | |
28 | * If a filename is given on the command-line, then this file is used | |
29 | * for the input, rather than the default "pngtest.png". This allows | |
30 | * testing a wide variety of files easily. You can also test a number | |
31 | * of files at once by typing "pngtest -m file1.png file2.png ..." | |
32 | */ | |
33 | ||
9c0d9ce3 | 34 | #define _POSIX_SOURCE 1 |
0272a10d | 35 | |
9c0d9ce3 DS |
36 | #include "zlib.h" |
37 | #include "png.h" | |
38 | /* Copied from pngpriv.h but only used in error messages below. */ | |
39 | #ifndef PNG_ZBUF_SIZE | |
40 | # define PNG_ZBUF_SIZE 8192 | |
41 | #endif | |
0272a10d VZ |
42 | # include <stdio.h> |
43 | # include <stdlib.h> | |
9c0d9ce3 | 44 | # include <string.h> |
0272a10d | 45 | # define FCLOSE(file) fclose(file) |
0272a10d | 46 | |
b61cc19c | 47 | #ifndef PNG_STDIO_SUPPORTED |
9c0d9ce3 | 48 | typedef FILE * png_FILE_p; |
0272a10d VZ |
49 | #endif |
50 | ||
9c0d9ce3 | 51 | /* Makes pngtest verbose so we can find problems. */ |
0272a10d VZ |
52 | #ifndef PNG_DEBUG |
53 | # define PNG_DEBUG 0 | |
54 | #endif | |
55 | ||
9c0d9ce3 DS |
56 | #if PNG_DEBUG > 1 |
57 | # define pngtest_debug(m) ((void)fprintf(stderr, m "\n")) | |
58 | # define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1)) | |
59 | # define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2)) | |
60 | #else | |
61 | # define pngtest_debug(m) ((void)0) | |
62 | # define pngtest_debug1(m,p1) ((void)0) | |
63 | # define pngtest_debug2(m,p1,p2) ((void)0) | |
64 | #endif | |
65 | ||
0272a10d | 66 | #if !PNG_DEBUG |
b61cc19c | 67 | # define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */ |
0272a10d VZ |
68 | #endif |
69 | ||
9c0d9ce3 DS |
70 | /* The code uses memcmp and memcpy on large objects (typically row pointers) so |
71 | * it is necessary to do soemthing special on certain architectures, note that | |
72 | * the actual support for this was effectively removed in 1.4, so only the | |
73 | * memory remains in this program: | |
74 | */ | |
75 | #define CVT_PTR(ptr) (ptr) | |
76 | #define CVT_PTR_NOCHECK(ptr) (ptr) | |
77 | #define png_memcmp memcmp | |
78 | #define png_memcpy memcpy | |
79 | #define png_memset memset | |
80 | ||
0272a10d VZ |
81 | /* Turn on CPU timing |
82 | #define PNGTEST_TIMING | |
83 | */ | |
84 | ||
b61cc19c | 85 | #ifndef PNG_FLOATING_POINT_SUPPORTED |
0272a10d VZ |
86 | #undef PNGTEST_TIMING |
87 | #endif | |
88 | ||
89 | #ifdef PNGTEST_TIMING | |
90 | static float t_start, t_stop, t_decode, t_encode, t_misc; | |
91 | #include <time.h> | |
92 | #endif | |
93 | ||
b61cc19c | 94 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
970f6abe VZ |
95 | #define PNG_tIME_STRING_LENGTH 29 |
96 | static int tIME_chunk_present = 0; | |
97 | static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present"; | |
0272a10d VZ |
98 | #endif |
99 | ||
100 | static int verbose = 0; | |
9c0d9ce3 | 101 | static int strict = 0; |
0272a10d VZ |
102 | |
103 | int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname)); | |
104 | ||
105 | #ifdef __TURBOC__ | |
106 | #include <mem.h> | |
107 | #endif | |
108 | ||
b61cc19c | 109 | /* Defined so I can write to a file on gui/windowing platforms */ |
0272a10d | 110 | /* #define STDERR stderr */ |
b61cc19c | 111 | #define STDERR stdout /* For DOS */ |
0272a10d | 112 | |
0272a10d VZ |
113 | /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */ |
114 | #ifndef png_jmpbuf | |
115 | # define png_jmpbuf(png_ptr) png_ptr->jmpbuf | |
116 | #endif | |
117 | ||
b61cc19c | 118 | /* Example of using row callbacks to make a simple progress meter */ |
970f6abe VZ |
119 | static int status_pass = 1; |
120 | static int status_dots_requested = 0; | |
121 | static int status_dots = 1; | |
122 | ||
9c0d9ce3 | 123 | void PNGCBAPI |
0272a10d | 124 | read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass); |
9c0d9ce3 | 125 | void PNGCBAPI |
0272a10d VZ |
126 | read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
127 | { | |
b61cc19c PC |
128 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX) |
129 | return; | |
9c0d9ce3 | 130 | |
b61cc19c PC |
131 | if (status_pass != pass) |
132 | { | |
133 | fprintf(stdout, "\n Pass %d: ", pass); | |
134 | status_pass = pass; | |
135 | status_dots = 31; | |
136 | } | |
9c0d9ce3 | 137 | |
b61cc19c | 138 | status_dots--; |
9c0d9ce3 | 139 | |
b61cc19c PC |
140 | if (status_dots == 0) |
141 | { | |
142 | fprintf(stdout, "\n "); | |
143 | status_dots=30; | |
144 | } | |
9c0d9ce3 | 145 | |
b61cc19c | 146 | fprintf(stdout, "r"); |
0272a10d VZ |
147 | } |
148 | ||
9c0d9ce3 | 149 | void PNGCBAPI |
0272a10d | 150 | write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass); |
9c0d9ce3 | 151 | void PNGCBAPI |
0272a10d VZ |
152 | write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
153 | { | |
b61cc19c PC |
154 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) |
155 | return; | |
9c0d9ce3 | 156 | |
b61cc19c | 157 | fprintf(stdout, "w"); |
0272a10d VZ |
158 | } |
159 | ||
160 | ||
b61cc19c | 161 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
0272a10d | 162 | /* Example of using user transform callback (we don't transform anything, |
b61cc19c PC |
163 | * but merely examine the row filters. We set this to 256 rather than |
164 | * 5 in case illegal filter values are present.) | |
165 | */ | |
0272a10d | 166 | static png_uint_32 filters_used[256]; |
9c0d9ce3 | 167 | void PNGCBAPI |
0272a10d | 168 | count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data); |
9c0d9ce3 | 169 | void PNGCBAPI |
0272a10d VZ |
170 | count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
171 | { | |
b61cc19c | 172 | if (png_ptr != NULL && row_info != NULL) |
970f6abe | 173 | ++filters_used[*(data - 1)]; |
0272a10d VZ |
174 | } |
175 | #endif | |
176 | ||
b61cc19c PC |
177 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
178 | /* Example of using user transform callback (we don't transform anything, | |
179 | * but merely count the zero samples) | |
180 | */ | |
0272a10d VZ |
181 | |
182 | static png_uint_32 zero_samples; | |
183 | ||
9c0d9ce3 | 184 | void PNGCBAPI |
0272a10d | 185 | count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data); |
9c0d9ce3 | 186 | void PNGCBAPI |
0272a10d VZ |
187 | count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
188 | { | |
189 | png_bytep dp = data; | |
9c0d9ce3 DS |
190 | if (png_ptr == NULL) |
191 | return; | |
0272a10d | 192 | |
b61cc19c | 193 | /* Contents of row_info: |
0272a10d VZ |
194 | * png_uint_32 width width of row |
195 | * png_uint_32 rowbytes number of bytes in row | |
196 | * png_byte color_type color type of pixels | |
197 | * png_byte bit_depth bit depth of samples | |
198 | * png_byte channels number of channels (1-4) | |
199 | * png_byte pixel_depth bits per pixel (depth*channels) | |
200 | */ | |
201 | ||
b61cc19c | 202 | /* Counts the number of zero samples (or zero pixels if color_type is 3 */ |
0272a10d | 203 | |
970f6abe | 204 | if (row_info->color_type == 0 || row_info->color_type == 3) |
0272a10d | 205 | { |
970f6abe | 206 | int pos = 0; |
0272a10d | 207 | png_uint_32 n, nstop; |
9c0d9ce3 | 208 | |
970f6abe | 209 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
0272a10d | 210 | { |
970f6abe | 211 | if (row_info->bit_depth == 1) |
0272a10d | 212 | { |
b61cc19c PC |
213 | if (((*dp << pos++ ) & 0x80) == 0) |
214 | zero_samples++; | |
9c0d9ce3 | 215 | |
970f6abe | 216 | if (pos == 8) |
0272a10d VZ |
217 | { |
218 | pos = 0; | |
219 | dp++; | |
220 | } | |
221 | } | |
9c0d9ce3 | 222 | |
970f6abe | 223 | if (row_info->bit_depth == 2) |
0272a10d | 224 | { |
b61cc19c PC |
225 | if (((*dp << (pos+=2)) & 0xc0) == 0) |
226 | zero_samples++; | |
9c0d9ce3 | 227 | |
970f6abe | 228 | if (pos == 8) |
0272a10d VZ |
229 | { |
230 | pos = 0; | |
231 | dp++; | |
232 | } | |
233 | } | |
9c0d9ce3 | 234 | |
970f6abe | 235 | if (row_info->bit_depth == 4) |
0272a10d | 236 | { |
b61cc19c PC |
237 | if (((*dp << (pos+=4)) & 0xf0) == 0) |
238 | zero_samples++; | |
9c0d9ce3 | 239 | |
970f6abe | 240 | if (pos == 8) |
0272a10d VZ |
241 | { |
242 | pos = 0; | |
243 | dp++; | |
244 | } | |
245 | } | |
9c0d9ce3 | 246 | |
970f6abe | 247 | if (row_info->bit_depth == 8) |
b61cc19c PC |
248 | if (*dp++ == 0) |
249 | zero_samples++; | |
9c0d9ce3 | 250 | |
970f6abe | 251 | if (row_info->bit_depth == 16) |
0272a10d | 252 | { |
b61cc19c PC |
253 | if ((*dp | *(dp+1)) == 0) |
254 | zero_samples++; | |
0272a10d VZ |
255 | dp+=2; |
256 | } | |
257 | } | |
258 | } | |
b61cc19c | 259 | else /* Other color types */ |
0272a10d VZ |
260 | { |
261 | png_uint_32 n, nstop; | |
262 | int channel; | |
263 | int color_channels = row_info->channels; | |
970f6abe | 264 | if (row_info->color_type > 3)color_channels--; |
0272a10d | 265 | |
970f6abe | 266 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
0272a10d VZ |
267 | { |
268 | for (channel = 0; channel < color_channels; channel++) | |
269 | { | |
970f6abe | 270 | if (row_info->bit_depth == 8) |
b61cc19c PC |
271 | if (*dp++ == 0) |
272 | zero_samples++; | |
9c0d9ce3 | 273 | |
970f6abe | 274 | if (row_info->bit_depth == 16) |
0272a10d | 275 | { |
b61cc19c PC |
276 | if ((*dp | *(dp+1)) == 0) |
277 | zero_samples++; | |
9c0d9ce3 | 278 | |
0272a10d VZ |
279 | dp+=2; |
280 | } | |
281 | } | |
970f6abe | 282 | if (row_info->color_type > 3) |
0272a10d VZ |
283 | { |
284 | dp++; | |
b61cc19c PC |
285 | if (row_info->bit_depth == 16) |
286 | dp++; | |
0272a10d VZ |
287 | } |
288 | } | |
289 | } | |
290 | } | |
291 | #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */ | |
292 | ||
293 | static int wrote_question = 0; | |
294 | ||
b61cc19c | 295 | #ifndef PNG_STDIO_SUPPORTED |
0272a10d | 296 | /* START of code to validate stdio-free compilation */ |
b61cc19c PC |
297 | /* These copies of the default read/write functions come from pngrio.c and |
298 | * pngwio.c. They allow "don't include stdio" testing of the library. | |
299 | * This is the function that does the actual reading of data. If you are | |
300 | * not reading from a standard C stream, you should create a replacement | |
301 | * read_data function and use it at run time with png_set_read_fn(), rather | |
302 | * than changing the library. | |
303 | */ | |
0272a10d | 304 | |
9c0d9ce3 DS |
305 | #ifdef PNG_IO_STATE_SUPPORTED |
306 | void | |
307 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, | |
308 | png_uint_32 io_op); | |
309 | void | |
310 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, | |
311 | png_uint_32 io_op) | |
312 | { | |
313 | png_uint_32 io_state = png_get_io_state(png_ptr); | |
314 | int err = 0; | |
315 | ||
316 | /* Check if the current operation (reading / writing) is as expected. */ | |
317 | if ((io_state & PNG_IO_MASK_OP) != io_op) | |
318 | png_error(png_ptr, "Incorrect operation in I/O state"); | |
319 | ||
320 | /* Check if the buffer size specific to the current location | |
321 | * (file signature / header / data / crc) is as expected. | |
322 | */ | |
323 | switch (io_state & PNG_IO_MASK_LOC) | |
324 | { | |
325 | case PNG_IO_SIGNATURE: | |
326 | if (data_length > 8) | |
327 | err = 1; | |
328 | break; | |
329 | case PNG_IO_CHUNK_HDR: | |
330 | if (data_length != 8) | |
331 | err = 1; | |
332 | break; | |
333 | case PNG_IO_CHUNK_DATA: | |
334 | break; /* no restrictions here */ | |
335 | case PNG_IO_CHUNK_CRC: | |
336 | if (data_length != 4) | |
337 | err = 1; | |
338 | break; | |
339 | default: | |
340 | err = 1; /* uninitialized */ | |
341 | } | |
342 | if (err) | |
343 | png_error(png_ptr, "Bad I/O state or buffer size"); | |
344 | } | |
345 | #endif | |
346 | ||
0272a10d | 347 | #ifndef USE_FAR_KEYWORD |
9c0d9ce3 | 348 | static void PNGCBAPI |
0272a10d VZ |
349 | pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
350 | { | |
b61cc19c PC |
351 | png_size_t check = 0; |
352 | png_voidp io_ptr; | |
0272a10d VZ |
353 | |
354 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t | |
355 | * instead of an int, which is what fread() actually returns. | |
356 | */ | |
b61cc19c PC |
357 | io_ptr = png_get_io_ptr(png_ptr); |
358 | if (io_ptr != NULL) | |
359 | { | |
360 | check = fread(data, 1, length, (png_FILE_p)io_ptr); | |
361 | } | |
0272a10d VZ |
362 | |
363 | if (check != length) | |
364 | { | |
9c0d9ce3 | 365 | png_error(png_ptr, "Read Error"); |
0272a10d | 366 | } |
9c0d9ce3 DS |
367 | |
368 | #ifdef PNG_IO_STATE_SUPPORTED | |
369 | pngtest_check_io_state(png_ptr, length, PNG_IO_READING); | |
370 | #endif | |
0272a10d VZ |
371 | } |
372 | #else | |
b61cc19c | 373 | /* This is the model-independent version. Since the standard I/O library |
0272a10d VZ |
374 | can't handle far buffers in the medium and small models, we have to copy |
375 | the data. | |
376 | */ | |
377 | ||
378 | #define NEAR_BUF_SIZE 1024 | |
379 | #define MIN(a,b) (a <= b ? a : b) | |
380 | ||
9c0d9ce3 | 381 | static void PNGCBAPI |
0272a10d VZ |
382 | pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
383 | { | |
b61cc19c | 384 | png_size_t check; |
0272a10d VZ |
385 | png_byte *n_data; |
386 | png_FILE_p io_ptr; | |
387 | ||
388 | /* Check if data really is near. If so, use usual code. */ | |
389 | n_data = (png_byte *)CVT_PTR_NOCHECK(data); | |
b61cc19c | 390 | io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr)); |
0272a10d VZ |
391 | if ((png_bytep)n_data == data) |
392 | { | |
b61cc19c | 393 | check = fread(n_data, 1, length, io_ptr); |
0272a10d VZ |
394 | } |
395 | else | |
396 | { | |
397 | png_byte buf[NEAR_BUF_SIZE]; | |
398 | png_size_t read, remaining, err; | |
399 | check = 0; | |
400 | remaining = length; | |
9c0d9ce3 | 401 | |
0272a10d VZ |
402 | do |
403 | { | |
404 | read = MIN(NEAR_BUF_SIZE, remaining); | |
b61cc19c PC |
405 | err = fread(buf, 1, 1, io_ptr); |
406 | png_memcpy(data, buf, read); /* Copy far buffer to near buffer */ | |
970f6abe | 407 | if (err != read) |
0272a10d VZ |
408 | break; |
409 | else | |
410 | check += err; | |
411 | data += read; | |
412 | remaining -= read; | |
413 | } | |
414 | while (remaining != 0); | |
415 | } | |
9c0d9ce3 | 416 | |
0272a10d | 417 | if (check != length) |
9c0d9ce3 DS |
418 | png_error(png_ptr, "Read Error"); |
419 | ||
420 | #ifdef PNG_IO_STATE_SUPPORTED | |
421 | pngtest_check_io_state(png_ptr, length, PNG_IO_READING); | |
422 | #endif | |
0272a10d VZ |
423 | } |
424 | #endif /* USE_FAR_KEYWORD */ | |
425 | ||
b61cc19c | 426 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
9c0d9ce3 | 427 | static void PNGCBAPI |
0272a10d VZ |
428 | pngtest_flush(png_structp png_ptr) |
429 | { | |
b61cc19c | 430 | /* Do nothing; fflush() is said to be just a waste of energy. */ |
9c0d9ce3 | 431 | PNG_UNUSED(png_ptr) /* Stifle compiler warning */ |
0272a10d VZ |
432 | } |
433 | #endif | |
434 | ||
435 | /* This is the function that does the actual writing of data. If you are | |
b61cc19c PC |
436 | * not writing to a standard C stream, you should create a replacement |
437 | * write_data function and use it at run time with png_set_write_fn(), rather | |
438 | * than changing the library. | |
439 | */ | |
0272a10d | 440 | #ifndef USE_FAR_KEYWORD |
9c0d9ce3 | 441 | static void PNGCBAPI |
0272a10d VZ |
442 | pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
443 | { | |
b61cc19c | 444 | png_size_t check; |
0272a10d | 445 | |
9c0d9ce3 DS |
446 | check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr)); |
447 | ||
0272a10d VZ |
448 | if (check != length) |
449 | { | |
450 | png_error(png_ptr, "Write Error"); | |
451 | } | |
9c0d9ce3 DS |
452 | |
453 | #ifdef PNG_IO_STATE_SUPPORTED | |
454 | pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING); | |
455 | #endif | |
0272a10d VZ |
456 | } |
457 | #else | |
b61cc19c | 458 | /* This is the model-independent version. Since the standard I/O library |
0272a10d VZ |
459 | can't handle far buffers in the medium and small models, we have to copy |
460 | the data. | |
461 | */ | |
462 | ||
463 | #define NEAR_BUF_SIZE 1024 | |
464 | #define MIN(a,b) (a <= b ? a : b) | |
465 | ||
9c0d9ce3 | 466 | static void PNGCBAPI |
0272a10d VZ |
467 | pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
468 | { | |
b61cc19c | 469 | png_size_t check; |
0272a10d VZ |
470 | png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ |
471 | png_FILE_p io_ptr; | |
472 | ||
473 | /* Check if data really is near. If so, use usual code. */ | |
474 | near_data = (png_byte *)CVT_PTR_NOCHECK(data); | |
9c0d9ce3 DS |
475 | io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr)); |
476 | ||
0272a10d VZ |
477 | if ((png_bytep)near_data == data) |
478 | { | |
b61cc19c | 479 | check = fwrite(near_data, 1, length, io_ptr); |
0272a10d | 480 | } |
9c0d9ce3 | 481 | |
0272a10d VZ |
482 | else |
483 | { | |
484 | png_byte buf[NEAR_BUF_SIZE]; | |
485 | png_size_t written, remaining, err; | |
486 | check = 0; | |
487 | remaining = length; | |
9c0d9ce3 | 488 | |
0272a10d VZ |
489 | do |
490 | { | |
491 | written = MIN(NEAR_BUF_SIZE, remaining); | |
b61cc19c PC |
492 | png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ |
493 | err = fwrite(buf, 1, written, io_ptr); | |
0272a10d VZ |
494 | if (err != written) |
495 | break; | |
496 | else | |
497 | check += err; | |
498 | data += written; | |
499 | remaining -= written; | |
500 | } | |
501 | while (remaining != 0); | |
502 | } | |
9c0d9ce3 | 503 | |
0272a10d VZ |
504 | if (check != length) |
505 | { | |
506 | png_error(png_ptr, "Write Error"); | |
507 | } | |
9c0d9ce3 DS |
508 | |
509 | #ifdef PNG_IO_STATE_SUPPORTED | |
510 | pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING); | |
511 | #endif | |
0272a10d VZ |
512 | } |
513 | #endif /* USE_FAR_KEYWORD */ | |
0272a10d VZ |
514 | |
515 | /* This function is called when there is a warning, but the library thinks | |
516 | * it can continue anyway. Replacement functions don't have to do anything | |
517 | * here if you don't want to. In the default configuration, png_ptr is | |
518 | * not used, but it is passed in case it may be useful. | |
519 | */ | |
9c0d9ce3 | 520 | static void PNGCBAPI |
0272a10d VZ |
521 | pngtest_warning(png_structp png_ptr, png_const_charp message) |
522 | { | |
523 | PNG_CONST char *name = "UNKNOWN (ERROR!)"; | |
b61cc19c PC |
524 | char *test; |
525 | test = png_get_error_ptr(png_ptr); | |
9c0d9ce3 | 526 | |
b61cc19c PC |
527 | if (test == NULL) |
528 | fprintf(STDERR, "%s: libpng warning: %s\n", name, message); | |
9c0d9ce3 | 529 | |
b61cc19c PC |
530 | else |
531 | fprintf(STDERR, "%s: libpng warning: %s\n", test, message); | |
0272a10d VZ |
532 | } |
533 | ||
534 | /* This is the default error handling function. Note that replacements for | |
535 | * this function MUST NOT RETURN, or the program will likely crash. This | |
536 | * function is used by default, or if the program supplies NULL for the | |
537 | * error function pointer in png_set_error_fn(). | |
538 | */ | |
9c0d9ce3 | 539 | static void PNGCBAPI |
0272a10d VZ |
540 | pngtest_error(png_structp png_ptr, png_const_charp message) |
541 | { | |
542 | pngtest_warning(png_ptr, message); | |
543 | /* We can return because png_error calls the default handler, which is | |
b61cc19c PC |
544 | * actually OK in this case. |
545 | */ | |
0272a10d | 546 | } |
b61cc19c | 547 | #endif /* !PNG_STDIO_SUPPORTED */ |
970f6abe | 548 | /* END of code to validate stdio-free compilation */ |
0272a10d VZ |
549 | |
550 | /* START of code to validate memory allocation and deallocation */ | |
551 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
552 | ||
553 | /* Allocate memory. For reasonable files, size should never exceed | |
b61cc19c PC |
554 | * 64K. However, zlib may allocate more then 64K if you don't tell |
555 | * it not to. See zconf.h and png.h for more information. zlib does | |
556 | * need to allocate exactly 64K, so whatever you call here must | |
557 | * have the ability to do that. | |
558 | * | |
559 | * This piece of code can be compiled to validate max 64K allocations | |
560 | * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. | |
561 | */ | |
0272a10d VZ |
562 | typedef struct memory_information |
563 | { | |
b61cc19c | 564 | png_alloc_size_t size; |
0272a10d VZ |
565 | png_voidp pointer; |
566 | struct memory_information FAR *next; | |
567 | } memory_information; | |
568 | typedef memory_information FAR *memory_infop; | |
569 | ||
570 | static memory_infop pinformation = NULL; | |
571 | static int current_allocation = 0; | |
572 | static int maximum_allocation = 0; | |
573 | static int total_allocation = 0; | |
574 | static int num_allocations = 0; | |
575 | ||
9c0d9ce3 DS |
576 | png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr, |
577 | png_alloc_size_t size)); | |
578 | void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr)); | |
0272a10d VZ |
579 | |
580 | png_voidp | |
9c0d9ce3 | 581 | PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size) |
0272a10d VZ |
582 | { |
583 | ||
584 | /* png_malloc has already tested for NULL; png_create_struct calls | |
b61cc19c PC |
585 | * png_debug_malloc directly, with png_ptr == NULL which is OK |
586 | */ | |
0272a10d VZ |
587 | |
588 | if (size == 0) | |
589 | return (NULL); | |
590 | ||
591 | /* This calls the library allocator twice, once to get the requested | |
592 | buffer and once to get a new free list entry. */ | |
593 | { | |
594 | /* Disable malloc_fn and free_fn */ | |
595 | memory_infop pinfo; | |
596 | png_set_mem_fn(png_ptr, NULL, NULL, NULL); | |
597 | pinfo = (memory_infop)png_malloc(png_ptr, | |
b61cc19c | 598 | png_sizeof(*pinfo)); |
0272a10d VZ |
599 | pinfo->size = size; |
600 | current_allocation += size; | |
601 | total_allocation += size; | |
602 | num_allocations ++; | |
9c0d9ce3 | 603 | |
0272a10d VZ |
604 | if (current_allocation > maximum_allocation) |
605 | maximum_allocation = current_allocation; | |
9c0d9ce3 | 606 | |
b61cc19c | 607 | pinfo->pointer = png_malloc(png_ptr, size); |
0272a10d | 608 | /* Restore malloc_fn and free_fn */ |
9c0d9ce3 | 609 | |
970f6abe | 610 | png_set_mem_fn(png_ptr, |
b61cc19c | 611 | NULL, png_debug_malloc, png_debug_free); |
9c0d9ce3 | 612 | |
0272a10d VZ |
613 | if (size != 0 && pinfo->pointer == NULL) |
614 | { | |
615 | current_allocation -= size; | |
616 | total_allocation -= size; | |
617 | png_error(png_ptr, | |
b61cc19c | 618 | "out of memory in pngtest->png_debug_malloc"); |
0272a10d | 619 | } |
9c0d9ce3 | 620 | |
0272a10d VZ |
621 | pinfo->next = pinformation; |
622 | pinformation = pinfo; | |
623 | /* Make sure the caller isn't assuming zeroed memory. */ | |
624 | png_memset(pinfo->pointer, 0xdd, pinfo->size); | |
9c0d9ce3 | 625 | |
970f6abe | 626 | if (verbose) |
9c0d9ce3 | 627 | printf("png_malloc %lu bytes at %p\n", (unsigned long)size, |
970f6abe | 628 | pinfo->pointer); |
9c0d9ce3 | 629 | |
0272a10d VZ |
630 | return (png_voidp)(pinfo->pointer); |
631 | } | |
632 | } | |
633 | ||
634 | /* Free a pointer. It is removed from the list at the same time. */ | |
9c0d9ce3 | 635 | void PNGCBAPI |
0272a10d VZ |
636 | png_debug_free(png_structp png_ptr, png_voidp ptr) |
637 | { | |
638 | if (png_ptr == NULL) | |
639 | fprintf(STDERR, "NULL pointer to png_debug_free.\n"); | |
9c0d9ce3 | 640 | |
0272a10d VZ |
641 | if (ptr == 0) |
642 | { | |
643 | #if 0 /* This happens all the time. */ | |
644 | fprintf(STDERR, "WARNING: freeing NULL pointer\n"); | |
645 | #endif | |
646 | return; | |
647 | } | |
648 | ||
649 | /* Unlink the element from the list. */ | |
650 | { | |
651 | memory_infop FAR *ppinfo = &pinformation; | |
9c0d9ce3 | 652 | |
0272a10d VZ |
653 | for (;;) |
654 | { | |
655 | memory_infop pinfo = *ppinfo; | |
9c0d9ce3 | 656 | |
0272a10d VZ |
657 | if (pinfo->pointer == ptr) |
658 | { | |
659 | *ppinfo = pinfo->next; | |
660 | current_allocation -= pinfo->size; | |
661 | if (current_allocation < 0) | |
662 | fprintf(STDERR, "Duplicate free of memory\n"); | |
663 | /* We must free the list element too, but first kill | |
664 | the memory that is to be freed. */ | |
665 | png_memset(ptr, 0x55, pinfo->size); | |
666 | png_free_default(png_ptr, pinfo); | |
970f6abe | 667 | pinfo = NULL; |
0272a10d VZ |
668 | break; |
669 | } | |
9c0d9ce3 | 670 | |
0272a10d VZ |
671 | if (pinfo->next == NULL) |
672 | { | |
673 | fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr); | |
674 | break; | |
675 | } | |
9c0d9ce3 | 676 | |
0272a10d VZ |
677 | ppinfo = &pinfo->next; |
678 | } | |
679 | } | |
680 | ||
681 | /* Finally free the data. */ | |
970f6abe | 682 | if (verbose) |
9c0d9ce3 DS |
683 | printf("Freeing %p\n", ptr); |
684 | ||
0272a10d | 685 | png_free_default(png_ptr, ptr); |
970f6abe | 686 | ptr = NULL; |
0272a10d VZ |
687 | } |
688 | #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */ | |
689 | /* END of code to test memory allocation/deallocation */ | |
690 | ||
970f6abe VZ |
691 | |
692 | /* Demonstration of user chunk support of the sTER and vpAg chunks */ | |
b61cc19c | 693 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
970f6abe | 694 | |
b61cc19c | 695 | /* (sTER is a public chunk not yet known by libpng. vpAg is a private |
970f6abe VZ |
696 | chunk used in ImageMagick to store "virtual page" size). */ |
697 | ||
698 | static png_uint_32 user_chunk_data[4]; | |
699 | ||
700 | /* 0: sTER mode + 1 | |
701 | * 1: vpAg width | |
702 | * 2: vpAg height | |
703 | * 3: vpAg units | |
704 | */ | |
705 | ||
9c0d9ce3 | 706 | static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr, |
970f6abe VZ |
707 | png_unknown_chunkp chunk) |
708 | { | |
b61cc19c PC |
709 | png_uint_32 |
710 | *my_user_chunk_data; | |
711 | ||
712 | /* Return one of the following: | |
713 | * return (-n); chunk had an error | |
714 | * return (0); did not recognize | |
715 | * return (n); success | |
716 | * | |
717 | * The unknown chunk structure contains the chunk data: | |
718 | * png_byte name[5]; | |
719 | * png_byte *data; | |
720 | * png_size_t size; | |
721 | * | |
722 | * Note that libpng has already taken care of the CRC handling. | |
723 | */ | |
724 | ||
725 | if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */ | |
726 | chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */ | |
727 | { | |
728 | /* Found sTER chunk */ | |
729 | if (chunk->size != 1) | |
730 | return (-1); /* Error return */ | |
9c0d9ce3 | 731 | |
b61cc19c PC |
732 | if (chunk->data[0] != 0 && chunk->data[0] != 1) |
733 | return (-1); /* Invalid mode */ | |
9c0d9ce3 | 734 | |
b61cc19c PC |
735 | my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr); |
736 | my_user_chunk_data[0]=chunk->data[0]+1; | |
737 | return (1); | |
738 | } | |
739 | ||
740 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */ | |
741 | chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */ | |
742 | return (0); /* Did not recognize */ | |
743 | ||
744 | /* Found ImageMagick vpAg chunk */ | |
745 | ||
746 | if (chunk->size != 9) | |
747 | return (-1); /* Error return */ | |
748 | ||
749 | my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr); | |
750 | ||
751 | my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data); | |
752 | my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4); | |
753 | my_user_chunk_data[3]=(png_uint_32)chunk->data[8]; | |
754 | ||
755 | return (1); | |
970f6abe VZ |
756 | |
757 | } | |
758 | #endif | |
759 | /* END of code to demonstrate user chunk support */ | |
760 | ||
0272a10d VZ |
761 | /* Test one file */ |
762 | int | |
763 | test_one_file(PNG_CONST char *inname, PNG_CONST char *outname) | |
764 | { | |
765 | static png_FILE_p fpin; | |
766 | static png_FILE_p fpout; /* "static" prevents setjmp corruption */ | |
767 | png_structp read_ptr; | |
768 | png_infop read_info_ptr, end_info_ptr; | |
769 | #ifdef PNG_WRITE_SUPPORTED | |
770 | png_structp write_ptr; | |
771 | png_infop write_info_ptr; | |
772 | png_infop write_end_info_ptr; | |
773 | #else | |
774 | png_structp write_ptr = NULL; | |
775 | png_infop write_info_ptr = NULL; | |
776 | png_infop write_end_info_ptr = NULL; | |
777 | #endif | |
778 | png_bytep row_buf; | |
779 | png_uint_32 y; | |
780 | png_uint_32 width, height; | |
781 | int num_pass, pass; | |
782 | int bit_depth, color_type; | |
783 | #ifdef PNG_SETJMP_SUPPORTED | |
784 | #ifdef USE_FAR_KEYWORD | |
9c0d9ce3 | 785 | jmp_buf tmp_jmpbuf; |
0272a10d VZ |
786 | #endif |
787 | #endif | |
788 | ||
0272a10d VZ |
789 | char inbuf[256], outbuf[256]; |
790 | ||
791 | row_buf = NULL; | |
792 | ||
0272a10d | 793 | if ((fpin = fopen(inname, "rb")) == NULL) |
0272a10d VZ |
794 | { |
795 | fprintf(STDERR, "Could not find input file %s\n", inname); | |
796 | return (1); | |
797 | } | |
798 | ||
0272a10d | 799 | if ((fpout = fopen(outname, "wb")) == NULL) |
0272a10d VZ |
800 | { |
801 | fprintf(STDERR, "Could not open output file %s\n", outname); | |
802 | FCLOSE(fpin); | |
803 | return (1); | |
804 | } | |
805 | ||
9c0d9ce3 | 806 | pngtest_debug("Allocating read and write structures"); |
0272a10d | 807 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
970f6abe | 808 | read_ptr = |
b61cc19c | 809 | png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
9c0d9ce3 | 810 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); |
0272a10d | 811 | #else |
970f6abe | 812 | read_ptr = |
b61cc19c | 813 | png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
0272a10d | 814 | #endif |
b61cc19c | 815 | #ifndef PNG_STDIO_SUPPORTED |
0272a10d VZ |
816 | png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error, |
817 | pngtest_warning); | |
970f6abe VZ |
818 | #endif |
819 | ||
b61cc19c PC |
820 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
821 | user_chunk_data[0] = 0; | |
822 | user_chunk_data[1] = 0; | |
823 | user_chunk_data[2] = 0; | |
824 | user_chunk_data[3] = 0; | |
825 | png_set_read_user_chunk_fn(read_ptr, user_chunk_data, | |
826 | read_user_chunk_callback); | |
970f6abe VZ |
827 | |
828 | #endif | |
0272a10d VZ |
829 | #ifdef PNG_WRITE_SUPPORTED |
830 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
970f6abe | 831 | write_ptr = |
b61cc19c PC |
832 | png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
833 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); | |
0272a10d | 834 | #else |
970f6abe | 835 | write_ptr = |
b61cc19c | 836 | png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
0272a10d | 837 | #endif |
b61cc19c | 838 | #ifndef PNG_STDIO_SUPPORTED |
0272a10d VZ |
839 | png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error, |
840 | pngtest_warning); | |
841 | #endif | |
970f6abe | 842 | #endif |
9c0d9ce3 | 843 | pngtest_debug("Allocating read_info, write_info and end_info structures"); |
0272a10d VZ |
844 | read_info_ptr = png_create_info_struct(read_ptr); |
845 | end_info_ptr = png_create_info_struct(read_ptr); | |
846 | #ifdef PNG_WRITE_SUPPORTED | |
847 | write_info_ptr = png_create_info_struct(write_ptr); | |
848 | write_end_info_ptr = png_create_info_struct(write_ptr); | |
849 | #endif | |
850 | ||
851 | #ifdef PNG_SETJMP_SUPPORTED | |
9c0d9ce3 | 852 | pngtest_debug("Setting jmpbuf for read struct"); |
0272a10d | 853 | #ifdef USE_FAR_KEYWORD |
9c0d9ce3 | 854 | if (setjmp(tmp_jmpbuf)) |
0272a10d VZ |
855 | #else |
856 | if (setjmp(png_jmpbuf(read_ptr))) | |
857 | #endif | |
858 | { | |
859 | fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); | |
970f6abe VZ |
860 | png_free(read_ptr, row_buf); |
861 | row_buf = NULL; | |
0272a10d VZ |
862 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
863 | #ifdef PNG_WRITE_SUPPORTED | |
864 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); | |
865 | png_destroy_write_struct(&write_ptr, &write_info_ptr); | |
866 | #endif | |
867 | FCLOSE(fpin); | |
868 | FCLOSE(fpout); | |
869 | return (1); | |
870 | } | |
871 | #ifdef USE_FAR_KEYWORD | |
9c0d9ce3 | 872 | png_memcpy(png_jmpbuf(read_ptr), tmp_jmpbuf, png_sizeof(jmp_buf)); |
0272a10d VZ |
873 | #endif |
874 | ||
875 | #ifdef PNG_WRITE_SUPPORTED | |
9c0d9ce3 | 876 | pngtest_debug("Setting jmpbuf for write struct"); |
0272a10d | 877 | #ifdef USE_FAR_KEYWORD |
9c0d9ce3 DS |
878 | |
879 | if (setjmp(tmp_jmpbuf)) | |
0272a10d VZ |
880 | #else |
881 | if (setjmp(png_jmpbuf(write_ptr))) | |
882 | #endif | |
883 | { | |
884 | fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname); | |
885 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); | |
886 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); | |
887 | #ifdef PNG_WRITE_SUPPORTED | |
888 | png_destroy_write_struct(&write_ptr, &write_info_ptr); | |
889 | #endif | |
890 | FCLOSE(fpin); | |
891 | FCLOSE(fpout); | |
892 | return (1); | |
893 | } | |
9c0d9ce3 | 894 | |
0272a10d | 895 | #ifdef USE_FAR_KEYWORD |
9c0d9ce3 | 896 | png_memcpy(png_jmpbuf(write_ptr), tmp_jmpbuf, png_sizeof(jmp_buf)); |
0272a10d VZ |
897 | #endif |
898 | #endif | |
899 | #endif | |
900 | ||
9c0d9ce3 | 901 | pngtest_debug("Initializing input and output streams"); |
b61cc19c | 902 | #ifdef PNG_STDIO_SUPPORTED |
0272a10d VZ |
903 | png_init_io(read_ptr, fpin); |
904 | # ifdef PNG_WRITE_SUPPORTED | |
905 | png_init_io(write_ptr, fpout); | |
906 | # endif | |
907 | #else | |
908 | png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data); | |
909 | # ifdef PNG_WRITE_SUPPORTED | |
910 | png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data, | |
b61cc19c | 911 | # ifdef PNG_WRITE_FLUSH_SUPPORTED |
0272a10d VZ |
912 | pngtest_flush); |
913 | # else | |
914 | NULL); | |
915 | # endif | |
916 | # endif | |
917 | #endif | |
9c0d9ce3 DS |
918 | |
919 | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED | |
920 | /* Normally one would use Z_DEFAULT_STRATEGY for text compression. | |
921 | * This is here just to make pngtest replicate the results from libpng | |
922 | * versions prior to 1.5.4, and to test this new API. | |
923 | */ | |
924 | png_set_text_compression_strategy(write_ptr, Z_FILTERED); | |
925 | #endif | |
926 | ||
970f6abe | 927 | if (status_dots_requested == 1) |
0272a10d VZ |
928 | { |
929 | #ifdef PNG_WRITE_SUPPORTED | |
930 | png_set_write_status_fn(write_ptr, write_row_callback); | |
931 | #endif | |
932 | png_set_read_status_fn(read_ptr, read_row_callback); | |
933 | } | |
9c0d9ce3 | 934 | |
0272a10d VZ |
935 | else |
936 | { | |
937 | #ifdef PNG_WRITE_SUPPORTED | |
b61cc19c | 938 | png_set_write_status_fn(write_ptr, NULL); |
0272a10d | 939 | #endif |
b61cc19c | 940 | png_set_read_status_fn(read_ptr, NULL); |
0272a10d VZ |
941 | } |
942 | ||
b61cc19c | 943 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
0272a10d | 944 | { |
b61cc19c | 945 | int i; |
9c0d9ce3 | 946 | |
b61cc19c PC |
947 | for (i = 0; i<256; i++) |
948 | filters_used[i] = 0; | |
9c0d9ce3 | 949 | |
b61cc19c | 950 | png_set_read_user_transform_fn(read_ptr, count_filters); |
0272a10d VZ |
951 | } |
952 | #endif | |
b61cc19c | 953 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
970f6abe | 954 | zero_samples = 0; |
0272a10d VZ |
955 | png_set_write_user_transform_fn(write_ptr, count_zero_samples); |
956 | #endif | |
957 | ||
b61cc19c | 958 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
959 | # ifndef PNG_HANDLE_CHUNK_ALWAYS |
960 | # define PNG_HANDLE_CHUNK_ALWAYS 3 | |
961 | # endif | |
962 | png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS, | |
b61cc19c | 963 | NULL, 0); |
0272a10d | 964 | #endif |
b61cc19c | 965 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
966 | # ifndef PNG_HANDLE_CHUNK_IF_SAFE |
967 | # define PNG_HANDLE_CHUNK_IF_SAFE 2 | |
968 | # endif | |
969 | png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE, | |
b61cc19c | 970 | NULL, 0); |
0272a10d VZ |
971 | #endif |
972 | ||
9c0d9ce3 | 973 | pngtest_debug("Reading info struct"); |
0272a10d VZ |
974 | png_read_info(read_ptr, read_info_ptr); |
975 | ||
9c0d9ce3 | 976 | pngtest_debug("Transferring info struct"); |
0272a10d VZ |
977 | { |
978 | int interlace_type, compression_type, filter_type; | |
979 | ||
980 | if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth, | |
981 | &color_type, &interlace_type, &compression_type, &filter_type)) | |
982 | { | |
983 | png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth, | |
b61cc19c | 984 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
0272a10d VZ |
985 | color_type, interlace_type, compression_type, filter_type); |
986 | #else | |
987 | color_type, PNG_INTERLACE_NONE, compression_type, filter_type); | |
988 | #endif | |
989 | } | |
990 | } | |
b61cc19c PC |
991 | #ifdef PNG_FIXED_POINT_SUPPORTED |
992 | #ifdef PNG_cHRM_SUPPORTED | |
0272a10d VZ |
993 | { |
994 | png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x, | |
995 | blue_y; | |
9c0d9ce3 | 996 | |
b61cc19c PC |
997 | if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, |
998 | &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y)) | |
0272a10d VZ |
999 | { |
1000 | png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x, | |
1001 | red_y, green_x, green_y, blue_x, blue_y); | |
1002 | } | |
1003 | } | |
1004 | #endif | |
b61cc19c | 1005 | #ifdef PNG_gAMA_SUPPORTED |
0272a10d VZ |
1006 | { |
1007 | png_fixed_point gamma; | |
1008 | ||
1009 | if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma)) | |
0272a10d | 1010 | png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma); |
0272a10d VZ |
1011 | } |
1012 | #endif | |
1013 | #else /* Use floating point versions */ | |
b61cc19c PC |
1014 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
1015 | #ifdef PNG_cHRM_SUPPORTED | |
0272a10d VZ |
1016 | { |
1017 | double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, | |
1018 | blue_y; | |
9c0d9ce3 | 1019 | |
0272a10d VZ |
1020 | if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x, |
1021 | &red_y, &green_x, &green_y, &blue_x, &blue_y)) | |
1022 | { | |
1023 | png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x, | |
1024 | red_y, green_x, green_y, blue_x, blue_y); | |
1025 | } | |
1026 | } | |
1027 | #endif | |
b61cc19c | 1028 | #ifdef PNG_gAMA_SUPPORTED |
0272a10d VZ |
1029 | { |
1030 | double gamma; | |
1031 | ||
1032 | if (png_get_gAMA(read_ptr, read_info_ptr, &gamma)) | |
0272a10d | 1033 | png_set_gAMA(write_ptr, write_info_ptr, gamma); |
0272a10d VZ |
1034 | } |
1035 | #endif | |
b61cc19c PC |
1036 | #endif /* Floating point */ |
1037 | #endif /* Fixed point */ | |
1038 | #ifdef PNG_iCCP_SUPPORTED | |
0272a10d VZ |
1039 | { |
1040 | png_charp name; | |
9c0d9ce3 | 1041 | png_bytep profile; |
0272a10d VZ |
1042 | png_uint_32 proflen; |
1043 | int compression_type; | |
1044 | ||
1045 | if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type, | |
1046 | &profile, &proflen)) | |
1047 | { | |
1048 | png_set_iCCP(write_ptr, write_info_ptr, name, compression_type, | |
1049 | profile, proflen); | |
1050 | } | |
1051 | } | |
1052 | #endif | |
b61cc19c | 1053 | #ifdef PNG_sRGB_SUPPORTED |
0272a10d VZ |
1054 | { |
1055 | int intent; | |
1056 | ||
1057 | if (png_get_sRGB(read_ptr, read_info_ptr, &intent)) | |
0272a10d | 1058 | png_set_sRGB(write_ptr, write_info_ptr, intent); |
0272a10d VZ |
1059 | } |
1060 | #endif | |
1061 | { | |
1062 | png_colorp palette; | |
1063 | int num_palette; | |
1064 | ||
1065 | if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette)) | |
0272a10d | 1066 | png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette); |
0272a10d | 1067 | } |
b61cc19c | 1068 | #ifdef PNG_bKGD_SUPPORTED |
0272a10d VZ |
1069 | { |
1070 | png_color_16p background; | |
1071 | ||
1072 | if (png_get_bKGD(read_ptr, read_info_ptr, &background)) | |
1073 | { | |
1074 | png_set_bKGD(write_ptr, write_info_ptr, background); | |
1075 | } | |
1076 | } | |
1077 | #endif | |
b61cc19c | 1078 | #ifdef PNG_hIST_SUPPORTED |
0272a10d VZ |
1079 | { |
1080 | png_uint_16p hist; | |
1081 | ||
1082 | if (png_get_hIST(read_ptr, read_info_ptr, &hist)) | |
0272a10d | 1083 | png_set_hIST(write_ptr, write_info_ptr, hist); |
0272a10d VZ |
1084 | } |
1085 | #endif | |
b61cc19c | 1086 | #ifdef PNG_oFFs_SUPPORTED |
0272a10d VZ |
1087 | { |
1088 | png_int_32 offset_x, offset_y; | |
1089 | int unit_type; | |
1090 | ||
970f6abe | 1091 | if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y, |
b61cc19c | 1092 | &unit_type)) |
0272a10d VZ |
1093 | { |
1094 | png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type); | |
1095 | } | |
1096 | } | |
1097 | #endif | |
b61cc19c | 1098 | #ifdef PNG_pCAL_SUPPORTED |
0272a10d VZ |
1099 | { |
1100 | png_charp purpose, units; | |
1101 | png_charpp params; | |
1102 | png_int_32 X0, X1; | |
1103 | int type, nparams; | |
1104 | ||
1105 | if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type, | |
1106 | &nparams, &units, ¶ms)) | |
1107 | { | |
1108 | png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type, | |
1109 | nparams, units, params); | |
1110 | } | |
1111 | } | |
1112 | #endif | |
b61cc19c | 1113 | #ifdef PNG_pHYs_SUPPORTED |
0272a10d VZ |
1114 | { |
1115 | png_uint_32 res_x, res_y; | |
1116 | int unit_type; | |
1117 | ||
1118 | if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type)) | |
0272a10d | 1119 | png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type); |
0272a10d VZ |
1120 | } |
1121 | #endif | |
b61cc19c | 1122 | #ifdef PNG_sBIT_SUPPORTED |
0272a10d VZ |
1123 | { |
1124 | png_color_8p sig_bit; | |
1125 | ||
1126 | if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit)) | |
0272a10d | 1127 | png_set_sBIT(write_ptr, write_info_ptr, sig_bit); |
0272a10d VZ |
1128 | } |
1129 | #endif | |
b61cc19c | 1130 | #ifdef PNG_sCAL_SUPPORTED |
0272a10d VZ |
1131 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
1132 | { | |
1133 | int unit; | |
1134 | double scal_width, scal_height; | |
1135 | ||
1136 | if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width, | |
1137 | &scal_height)) | |
1138 | { | |
1139 | png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height); | |
1140 | } | |
1141 | } | |
1142 | #else | |
1143 | #ifdef PNG_FIXED_POINT_SUPPORTED | |
1144 | { | |
1145 | int unit; | |
1146 | png_charp scal_width, scal_height; | |
1147 | ||
1148 | if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width, | |
1149 | &scal_height)) | |
1150 | { | |
b61cc19c PC |
1151 | png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, |
1152 | scal_height); | |
0272a10d VZ |
1153 | } |
1154 | } | |
1155 | #endif | |
1156 | #endif | |
1157 | #endif | |
b61cc19c | 1158 | #ifdef PNG_TEXT_SUPPORTED |
0272a10d VZ |
1159 | { |
1160 | png_textp text_ptr; | |
1161 | int num_text; | |
1162 | ||
1163 | if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0) | |
1164 | { | |
9c0d9ce3 | 1165 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
0272a10d VZ |
1166 | png_set_text(write_ptr, write_info_ptr, text_ptr, num_text); |
1167 | } | |
1168 | } | |
1169 | #endif | |
b61cc19c | 1170 | #ifdef PNG_tIME_SUPPORTED |
0272a10d VZ |
1171 | { |
1172 | png_timep mod_time; | |
1173 | ||
1174 | if (png_get_tIME(read_ptr, read_info_ptr, &mod_time)) | |
1175 | { | |
1176 | png_set_tIME(write_ptr, write_info_ptr, mod_time); | |
b61cc19c PC |
1177 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
1178 | /* We have to use png_memcpy instead of "=" because the string | |
1179 | * pointed to by png_convert_to_rfc1123() gets free'ed before | |
1180 | * we use it. | |
1181 | */ | |
970f6abe VZ |
1182 | png_memcpy(tIME_string, |
1183 | png_convert_to_rfc1123(read_ptr, mod_time), | |
1184 | png_sizeof(tIME_string)); | |
9c0d9ce3 | 1185 | |
970f6abe | 1186 | tIME_string[png_sizeof(tIME_string) - 1] = '\0'; |
0272a10d VZ |
1187 | tIME_chunk_present++; |
1188 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ | |
1189 | } | |
1190 | } | |
1191 | #endif | |
b61cc19c | 1192 | #ifdef PNG_tRNS_SUPPORTED |
0272a10d | 1193 | { |
b61cc19c | 1194 | png_bytep trans_alpha; |
0272a10d | 1195 | int num_trans; |
b61cc19c | 1196 | png_color_16p trans_color; |
0272a10d | 1197 | |
b61cc19c PC |
1198 | if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans, |
1199 | &trans_color)) | |
0272a10d | 1200 | { |
b61cc19c | 1201 | int sample_max = (1 << bit_depth); |
970f6abe | 1202 | /* libpng doesn't reject a tRNS chunk with out-of-range samples */ |
b61cc19c PC |
1203 | if (!((color_type == PNG_COLOR_TYPE_GRAY && |
1204 | (int)trans_color->gray > sample_max) || | |
1205 | (color_type == PNG_COLOR_TYPE_RGB && | |
1206 | ((int)trans_color->red > sample_max || | |
1207 | (int)trans_color->green > sample_max || | |
1208 | (int)trans_color->blue > sample_max)))) | |
1209 | png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans, | |
1210 | trans_color); | |
0272a10d VZ |
1211 | } |
1212 | } | |
1213 | #endif | |
b61cc19c | 1214 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
1215 | { |
1216 | png_unknown_chunkp unknowns; | |
9c0d9ce3 | 1217 | int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr, |
0272a10d | 1218 | &unknowns); |
9c0d9ce3 | 1219 | |
0272a10d VZ |
1220 | if (num_unknowns) |
1221 | { | |
9c0d9ce3 | 1222 | int i; |
0272a10d VZ |
1223 | png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns, |
1224 | num_unknowns); | |
b61cc19c PC |
1225 | /* Copy the locations from the read_info_ptr. The automatically |
1226 | * generated locations in write_info_ptr are wrong because we | |
1227 | * haven't written anything yet. | |
1228 | */ | |
9c0d9ce3 | 1229 | for (i = 0; i < num_unknowns; i++) |
0272a10d VZ |
1230 | png_set_unknown_chunk_location(write_ptr, write_info_ptr, i, |
1231 | unknowns[i].location); | |
1232 | } | |
1233 | } | |
1234 | #endif | |
1235 | ||
1236 | #ifdef PNG_WRITE_SUPPORTED | |
9c0d9ce3 | 1237 | pngtest_debug("Writing info struct"); |
0272a10d VZ |
1238 | |
1239 | /* If we wanted, we could write info in two steps: | |
b61cc19c | 1240 | * png_write_info_before_PLTE(write_ptr, write_info_ptr); |
0272a10d VZ |
1241 | */ |
1242 | png_write_info(write_ptr, write_info_ptr); | |
970f6abe | 1243 | |
b61cc19c | 1244 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
970f6abe VZ |
1245 | if (user_chunk_data[0] != 0) |
1246 | { | |
b61cc19c | 1247 | png_byte png_sTER[5] = {115, 84, 69, 82, '\0'}; |
970f6abe | 1248 | |
b61cc19c PC |
1249 | unsigned char |
1250 | ster_chunk_data[1]; | |
970f6abe | 1251 | |
b61cc19c PC |
1252 | if (verbose) |
1253 | fprintf(STDERR, "\n stereo mode = %lu\n", | |
1254 | (unsigned long)(user_chunk_data[0] - 1)); | |
9c0d9ce3 | 1255 | |
b61cc19c PC |
1256 | ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1); |
1257 | png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1); | |
970f6abe | 1258 | } |
9c0d9ce3 | 1259 | |
970f6abe VZ |
1260 | if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0) |
1261 | { | |
b61cc19c PC |
1262 | png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'}; |
1263 | ||
1264 | unsigned char | |
1265 | vpag_chunk_data[9]; | |
1266 | ||
1267 | if (verbose) | |
1268 | fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n", | |
1269 | (unsigned long)user_chunk_data[1], | |
1270 | (unsigned long)user_chunk_data[2], | |
1271 | (unsigned long)user_chunk_data[3]); | |
9c0d9ce3 | 1272 | |
b61cc19c PC |
1273 | png_save_uint_32(vpag_chunk_data, user_chunk_data[1]); |
1274 | png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]); | |
1275 | vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff); | |
1276 | png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9); | |
970f6abe VZ |
1277 | } |
1278 | ||
1279 | #endif | |
0272a10d VZ |
1280 | #endif |
1281 | ||
1282 | #ifdef SINGLE_ROWBUF_ALLOC | |
9c0d9ce3 | 1283 | pngtest_debug("Allocating row buffer..."); |
0272a10d VZ |
1284 | row_buf = (png_bytep)png_malloc(read_ptr, |
1285 | png_get_rowbytes(read_ptr, read_info_ptr)); | |
9c0d9ce3 DS |
1286 | |
1287 | pngtest_debug1("\t0x%08lx", (unsigned long)row_buf); | |
0272a10d | 1288 | #endif /* SINGLE_ROWBUF_ALLOC */ |
9c0d9ce3 | 1289 | pngtest_debug("Writing row data"); |
0272a10d VZ |
1290 | |
1291 | #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ | |
1292 | defined(PNG_WRITE_INTERLACING_SUPPORTED) | |
1293 | num_pass = png_set_interlace_handling(read_ptr); | |
1294 | # ifdef PNG_WRITE_SUPPORTED | |
1295 | png_set_interlace_handling(write_ptr); | |
1296 | # endif | |
1297 | #else | |
970f6abe | 1298 | num_pass = 1; |
0272a10d VZ |
1299 | #endif |
1300 | ||
1301 | #ifdef PNGTEST_TIMING | |
1302 | t_stop = (float)clock(); | |
1303 | t_misc += (t_stop - t_start); | |
1304 | t_start = t_stop; | |
1305 | #endif | |
1306 | for (pass = 0; pass < num_pass; pass++) | |
1307 | { | |
9c0d9ce3 | 1308 | pngtest_debug1("Writing row data for pass %d", pass); |
0272a10d VZ |
1309 | for (y = 0; y < height; y++) |
1310 | { | |
1311 | #ifndef SINGLE_ROWBUF_ALLOC | |
9c0d9ce3 | 1312 | pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y); |
0272a10d VZ |
1313 | row_buf = (png_bytep)png_malloc(read_ptr, |
1314 | png_get_rowbytes(read_ptr, read_info_ptr)); | |
9c0d9ce3 DS |
1315 | |
1316 | pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf, | |
0272a10d | 1317 | png_get_rowbytes(read_ptr, read_info_ptr)); |
9c0d9ce3 | 1318 | |
0272a10d | 1319 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
b61cc19c | 1320 | png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1); |
0272a10d VZ |
1321 | |
1322 | #ifdef PNG_WRITE_SUPPORTED | |
1323 | #ifdef PNGTEST_TIMING | |
1324 | t_stop = (float)clock(); | |
1325 | t_decode += (t_stop - t_start); | |
1326 | t_start = t_stop; | |
1327 | #endif | |
1328 | png_write_rows(write_ptr, (png_bytepp)&row_buf, 1); | |
1329 | #ifdef PNGTEST_TIMING | |
1330 | t_stop = (float)clock(); | |
1331 | t_encode += (t_stop - t_start); | |
1332 | t_start = t_stop; | |
1333 | #endif | |
1334 | #endif /* PNG_WRITE_SUPPORTED */ | |
1335 | ||
1336 | #ifndef SINGLE_ROWBUF_ALLOC | |
9c0d9ce3 | 1337 | pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y); |
0272a10d | 1338 | png_free(read_ptr, row_buf); |
970f6abe | 1339 | row_buf = NULL; |
0272a10d VZ |
1340 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
1341 | } | |
1342 | } | |
1343 | ||
b61cc19c | 1344 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
1345 | png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1); |
1346 | #endif | |
b61cc19c | 1347 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
1348 | png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1); |
1349 | #endif | |
1350 | ||
9c0d9ce3 | 1351 | pngtest_debug("Reading and writing end_info data"); |
0272a10d VZ |
1352 | |
1353 | png_read_end(read_ptr, end_info_ptr); | |
b61cc19c | 1354 | #ifdef PNG_TEXT_SUPPORTED |
0272a10d VZ |
1355 | { |
1356 | png_textp text_ptr; | |
1357 | int num_text; | |
1358 | ||
1359 | if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0) | |
1360 | { | |
9c0d9ce3 | 1361 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
0272a10d VZ |
1362 | png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text); |
1363 | } | |
1364 | } | |
1365 | #endif | |
b61cc19c | 1366 | #ifdef PNG_tIME_SUPPORTED |
0272a10d VZ |
1367 | { |
1368 | png_timep mod_time; | |
1369 | ||
1370 | if (png_get_tIME(read_ptr, end_info_ptr, &mod_time)) | |
1371 | { | |
1372 | png_set_tIME(write_ptr, write_end_info_ptr, mod_time); | |
b61cc19c PC |
1373 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
1374 | /* We have to use png_memcpy instead of "=" because the string | |
0272a10d VZ |
1375 | pointed to by png_convert_to_rfc1123() gets free'ed before |
1376 | we use it */ | |
970f6abe VZ |
1377 | png_memcpy(tIME_string, |
1378 | png_convert_to_rfc1123(read_ptr, mod_time), | |
1379 | png_sizeof(tIME_string)); | |
9c0d9ce3 | 1380 | |
970f6abe | 1381 | tIME_string[png_sizeof(tIME_string) - 1] = '\0'; |
0272a10d VZ |
1382 | tIME_chunk_present++; |
1383 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ | |
1384 | } | |
1385 | } | |
1386 | #endif | |
b61cc19c | 1387 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
0272a10d VZ |
1388 | { |
1389 | png_unknown_chunkp unknowns; | |
9c0d9ce3 | 1390 | int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr, |
0272a10d | 1391 | &unknowns); |
9c0d9ce3 | 1392 | |
0272a10d VZ |
1393 | if (num_unknowns) |
1394 | { | |
9c0d9ce3 | 1395 | int i; |
0272a10d VZ |
1396 | png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns, |
1397 | num_unknowns); | |
b61cc19c PC |
1398 | /* Copy the locations from the read_info_ptr. The automatically |
1399 | * generated locations in write_end_info_ptr are wrong because we | |
1400 | * haven't written the end_info yet. | |
1401 | */ | |
9c0d9ce3 | 1402 | for (i = 0; i < num_unknowns; i++) |
0272a10d VZ |
1403 | png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i, |
1404 | unknowns[i].location); | |
1405 | } | |
1406 | } | |
1407 | #endif | |
1408 | #ifdef PNG_WRITE_SUPPORTED | |
1409 | png_write_end(write_ptr, write_end_info_ptr); | |
1410 | #endif | |
1411 | ||
1412 | #ifdef PNG_EASY_ACCESS_SUPPORTED | |
970f6abe | 1413 | if (verbose) |
0272a10d VZ |
1414 | { |
1415 | png_uint_32 iwidth, iheight; | |
1416 | iwidth = png_get_image_width(write_ptr, write_info_ptr); | |
1417 | iheight = png_get_image_height(write_ptr, write_info_ptr); | |
b61cc19c | 1418 | fprintf(STDERR, "\n Image width = %lu, height = %lu\n", |
0272a10d VZ |
1419 | (unsigned long)iwidth, (unsigned long)iheight); |
1420 | } | |
1421 | #endif | |
1422 | ||
9c0d9ce3 | 1423 | pngtest_debug("Destroying data structs"); |
0272a10d | 1424 | #ifdef SINGLE_ROWBUF_ALLOC |
9c0d9ce3 | 1425 | pngtest_debug("destroying row_buf for read_ptr"); |
0272a10d | 1426 | png_free(read_ptr, row_buf); |
970f6abe | 1427 | row_buf = NULL; |
0272a10d | 1428 | #endif /* SINGLE_ROWBUF_ALLOC */ |
9c0d9ce3 | 1429 | pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr"); |
0272a10d VZ |
1430 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
1431 | #ifdef PNG_WRITE_SUPPORTED | |
9c0d9ce3 | 1432 | pngtest_debug("destroying write_end_info_ptr"); |
0272a10d | 1433 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
9c0d9ce3 | 1434 | pngtest_debug("destroying write_ptr, write_info_ptr"); |
0272a10d VZ |
1435 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
1436 | #endif | |
9c0d9ce3 | 1437 | pngtest_debug("Destruction complete."); |
0272a10d VZ |
1438 | |
1439 | FCLOSE(fpin); | |
1440 | FCLOSE(fpout); | |
1441 | ||
9c0d9ce3 | 1442 | pngtest_debug("Opening files for comparison"); |
0272a10d | 1443 | if ((fpin = fopen(inname, "rb")) == NULL) |
0272a10d VZ |
1444 | { |
1445 | fprintf(STDERR, "Could not find file %s\n", inname); | |
1446 | return (1); | |
1447 | } | |
1448 | ||
0272a10d | 1449 | if ((fpout = fopen(outname, "rb")) == NULL) |
0272a10d VZ |
1450 | { |
1451 | fprintf(STDERR, "Could not find file %s\n", outname); | |
1452 | FCLOSE(fpin); | |
1453 | return (1); | |
1454 | } | |
1455 | ||
970f6abe | 1456 | for (;;) |
0272a10d VZ |
1457 | { |
1458 | png_size_t num_in, num_out; | |
1459 | ||
b61cc19c PC |
1460 | num_in = fread(inbuf, 1, 1, fpin); |
1461 | num_out = fread(outbuf, 1, 1, fpout); | |
0272a10d VZ |
1462 | |
1463 | if (num_in != num_out) | |
1464 | { | |
1465 | fprintf(STDERR, "\nFiles %s and %s are of a different size\n", | |
1466 | inname, outname); | |
9c0d9ce3 | 1467 | |
970f6abe | 1468 | if (wrote_question == 0) |
0272a10d VZ |
1469 | { |
1470 | fprintf(STDERR, | |
1471 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", | |
970f6abe | 1472 | inname, PNG_ZBUF_SIZE); |
0272a10d VZ |
1473 | fprintf(STDERR, |
1474 | "\n filtering heuristic (libpng default), compression"); | |
1475 | fprintf(STDERR, | |
1476 | " level (zlib default),\n and zlib version (%s)?\n\n", | |
1477 | ZLIB_VERSION); | |
970f6abe | 1478 | wrote_question = 1; |
0272a10d | 1479 | } |
9c0d9ce3 | 1480 | |
0272a10d VZ |
1481 | FCLOSE(fpin); |
1482 | FCLOSE(fpout); | |
9c0d9ce3 DS |
1483 | |
1484 | if (strict != 0) | |
1485 | return (1); | |
1486 | ||
1487 | else | |
1488 | return (0); | |
0272a10d VZ |
1489 | } |
1490 | ||
1491 | if (!num_in) | |
1492 | break; | |
1493 | ||
1494 | if (png_memcmp(inbuf, outbuf, num_in)) | |
1495 | { | |
1496 | fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname); | |
9c0d9ce3 | 1497 | |
970f6abe | 1498 | if (wrote_question == 0) |
0272a10d VZ |
1499 | { |
1500 | fprintf(STDERR, | |
1501 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", | |
970f6abe | 1502 | inname, PNG_ZBUF_SIZE); |
0272a10d VZ |
1503 | fprintf(STDERR, |
1504 | "\n filtering heuristic (libpng default), compression"); | |
1505 | fprintf(STDERR, | |
1506 | " level (zlib default),\n and zlib version (%s)?\n\n", | |
1507 | ZLIB_VERSION); | |
970f6abe | 1508 | wrote_question = 1; |
0272a10d | 1509 | } |
9c0d9ce3 | 1510 | |
0272a10d VZ |
1511 | FCLOSE(fpin); |
1512 | FCLOSE(fpout); | |
9c0d9ce3 DS |
1513 | |
1514 | if (strict != 0) | |
1515 | return (1); | |
1516 | ||
1517 | else | |
1518 | return (0); | |
0272a10d VZ |
1519 | } |
1520 | } | |
1521 | ||
1522 | FCLOSE(fpin); | |
1523 | FCLOSE(fpout); | |
1524 | ||
1525 | return (0); | |
1526 | } | |
1527 | ||
b61cc19c | 1528 | /* Input and output filenames */ |
0272a10d VZ |
1529 | #ifdef RISCOS |
1530 | static PNG_CONST char *inname = "pngtest/png"; | |
1531 | static PNG_CONST char *outname = "pngout/png"; | |
1532 | #else | |
1533 | static PNG_CONST char *inname = "pngtest.png"; | |
1534 | static PNG_CONST char *outname = "pngout.png"; | |
1535 | #endif | |
1536 | ||
1537 | int | |
1538 | main(int argc, char *argv[]) | |
1539 | { | |
1540 | int multiple = 0; | |
1541 | int ierror = 0; | |
1542 | ||
b61cc19c | 1543 | fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING); |
0272a10d | 1544 | fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION); |
970f6abe | 1545 | fprintf(STDERR, "%s", png_get_copyright(NULL)); |
0272a10d | 1546 | /* Show the version of libpng used in building the library */ |
970f6abe | 1547 | fprintf(STDERR, " library (%lu):%s", |
0272a10d VZ |
1548 | (unsigned long)png_access_version_number(), |
1549 | png_get_header_version(NULL)); | |
9c0d9ce3 | 1550 | |
0272a10d | 1551 | /* Show the version of libpng used in building the application */ |
970f6abe | 1552 | fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER, |
0272a10d | 1553 | PNG_HEADER_VERSION_STRING); |
0272a10d VZ |
1554 | |
1555 | /* Do some consistency checking on the memory allocation settings, I'm | |
b61cc19c PC |
1556 | * not sure this matters, but it is nice to know, the first of these |
1557 | * tests should be impossible because of the way the macros are set | |
1558 | * in pngconf.h | |
1559 | */ | |
0272a10d VZ |
1560 | #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) |
1561 | fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n"); | |
1562 | #endif | |
1563 | /* I think the following can happen. */ | |
1564 | #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K) | |
1565 | fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n"); | |
1566 | #endif | |
1567 | ||
1568 | if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING)) | |
1569 | { | |
1570 | fprintf(STDERR, | |
1571 | "Warning: versions are different between png.h and png.c\n"); | |
1572 | fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING); | |
1573 | fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver); | |
1574 | ++ierror; | |
1575 | } | |
1576 | ||
1577 | if (argc > 1) | |
1578 | { | |
1579 | if (strcmp(argv[1], "-m") == 0) | |
1580 | { | |
1581 | multiple = 1; | |
1582 | status_dots_requested = 0; | |
1583 | } | |
9c0d9ce3 | 1584 | |
0272a10d VZ |
1585 | else if (strcmp(argv[1], "-mv") == 0 || |
1586 | strcmp(argv[1], "-vm") == 0 ) | |
1587 | { | |
1588 | multiple = 1; | |
1589 | verbose = 1; | |
1590 | status_dots_requested = 1; | |
1591 | } | |
9c0d9ce3 | 1592 | |
0272a10d VZ |
1593 | else if (strcmp(argv[1], "-v") == 0) |
1594 | { | |
1595 | verbose = 1; | |
1596 | status_dots_requested = 1; | |
1597 | inname = argv[2]; | |
1598 | } | |
9c0d9ce3 DS |
1599 | |
1600 | else if (strcmp(argv[1], "--strict") == 0) | |
1601 | { | |
1602 | status_dots_requested = 0; | |
1603 | verbose = 1; | |
1604 | inname = argv[2]; | |
1605 | strict++; | |
1606 | } | |
1607 | ||
0272a10d VZ |
1608 | else |
1609 | { | |
1610 | inname = argv[1]; | |
1611 | status_dots_requested = 0; | |
1612 | } | |
1613 | } | |
1614 | ||
970f6abe VZ |
1615 | if (!multiple && argc == 3 + verbose) |
1616 | outname = argv[2 + verbose]; | |
0272a10d | 1617 | |
970f6abe | 1618 | if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2)) |
0272a10d VZ |
1619 | { |
1620 | fprintf(STDERR, | |
1621 | "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n", | |
1622 | argv[0], argv[0]); | |
1623 | fprintf(STDERR, | |
1624 | " reads/writes one PNG file (without -m) or multiple files (-m)\n"); | |
1625 | fprintf(STDERR, | |
1626 | " with -m %s is used as a temporary file\n", outname); | |
1627 | exit(1); | |
1628 | } | |
1629 | ||
1630 | if (multiple) | |
1631 | { | |
1632 | int i; | |
1633 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1634 | int allocation_now = current_allocation; | |
1635 | #endif | |
1636 | for (i=2; i<argc; ++i) | |
1637 | { | |
0272a10d | 1638 | int kerror; |
b61cc19c | 1639 | fprintf(STDERR, "\n Testing %s:", argv[i]); |
0272a10d VZ |
1640 | kerror = test_one_file(argv[i], outname); |
1641 | if (kerror == 0) | |
1642 | { | |
b61cc19c PC |
1643 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
1644 | int k; | |
1645 | #endif | |
1646 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED | |
0272a10d VZ |
1647 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
1648 | (unsigned long)zero_samples); | |
1649 | #else | |
1650 | fprintf(STDERR, " PASS\n"); | |
1651 | #endif | |
b61cc19c | 1652 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
970f6abe VZ |
1653 | for (k = 0; k<256; k++) |
1654 | if (filters_used[k]) | |
0272a10d | 1655 | fprintf(STDERR, " Filter %d was used %lu times\n", |
970f6abe | 1656 | k, (unsigned long)filters_used[k]); |
0272a10d | 1657 | #endif |
b61cc19c | 1658 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
970f6abe VZ |
1659 | if (tIME_chunk_present != 0) |
1660 | fprintf(STDERR, " tIME = %s\n", tIME_string); | |
9c0d9ce3 | 1661 | |
0272a10d VZ |
1662 | tIME_chunk_present = 0; |
1663 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ | |
1664 | } | |
9c0d9ce3 | 1665 | |
0272a10d VZ |
1666 | else |
1667 | { | |
1668 | fprintf(STDERR, " FAIL\n"); | |
1669 | ierror += kerror; | |
1670 | } | |
1671 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1672 | if (allocation_now != current_allocation) | |
1673 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", | |
970f6abe | 1674 | current_allocation - allocation_now); |
9c0d9ce3 | 1675 | |
0272a10d VZ |
1676 | if (current_allocation != 0) |
1677 | { | |
1678 | memory_infop pinfo = pinformation; | |
1679 | ||
1680 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", | |
1681 | current_allocation); | |
9c0d9ce3 | 1682 | |
0272a10d VZ |
1683 | while (pinfo != NULL) |
1684 | { | |
970f6abe VZ |
1685 | fprintf(STDERR, " %lu bytes at %x\n", |
1686 | (unsigned long)pinfo->size, | |
9c0d9ce3 | 1687 | (unsigned int)pinfo->pointer); |
0272a10d VZ |
1688 | pinfo = pinfo->next; |
1689 | } | |
1690 | } | |
1691 | #endif | |
1692 | } | |
1693 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1694 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", | |
1695 | current_allocation); | |
1696 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", | |
1697 | maximum_allocation); | |
1698 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", | |
1699 | total_allocation); | |
1700 | fprintf(STDERR, " Number of allocations: %10d\n", | |
1701 | num_allocations); | |
1702 | #endif | |
1703 | } | |
9c0d9ce3 | 1704 | |
0272a10d VZ |
1705 | else |
1706 | { | |
1707 | int i; | |
970f6abe | 1708 | for (i = 0; i<3; ++i) |
0272a10d VZ |
1709 | { |
1710 | int kerror; | |
1711 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1712 | int allocation_now = current_allocation; | |
1713 | #endif | |
9c0d9ce3 DS |
1714 | if (i == 1) |
1715 | status_dots_requested = 1; | |
1716 | ||
1717 | else if (verbose == 0) | |
1718 | status_dots_requested = 0; | |
1719 | ||
0272a10d | 1720 | if (i == 0 || verbose == 1 || ierror != 0) |
b61cc19c | 1721 | fprintf(STDERR, "\n Testing %s:", inname); |
9c0d9ce3 | 1722 | |
0272a10d | 1723 | kerror = test_one_file(inname, outname); |
9c0d9ce3 | 1724 | |
970f6abe | 1725 | if (kerror == 0) |
0272a10d | 1726 | { |
970f6abe | 1727 | if (verbose == 1 || i == 2) |
0272a10d | 1728 | { |
b61cc19c | 1729 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
0272a10d VZ |
1730 | int k; |
1731 | #endif | |
b61cc19c | 1732 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
0272a10d VZ |
1733 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
1734 | (unsigned long)zero_samples); | |
1735 | #else | |
1736 | fprintf(STDERR, " PASS\n"); | |
1737 | #endif | |
b61cc19c | 1738 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
970f6abe VZ |
1739 | for (k = 0; k<256; k++) |
1740 | if (filters_used[k]) | |
0272a10d | 1741 | fprintf(STDERR, " Filter %d was used %lu times\n", |
b61cc19c | 1742 | k, (unsigned long)filters_used[k]); |
0272a10d | 1743 | #endif |
b61cc19c | 1744 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
970f6abe VZ |
1745 | if (tIME_chunk_present != 0) |
1746 | fprintf(STDERR, " tIME = %s\n", tIME_string); | |
0272a10d VZ |
1747 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
1748 | } | |
1749 | } | |
9c0d9ce3 | 1750 | |
0272a10d VZ |
1751 | else |
1752 | { | |
970f6abe | 1753 | if (verbose == 0 && i != 2) |
b61cc19c | 1754 | fprintf(STDERR, "\n Testing %s:", inname); |
9c0d9ce3 | 1755 | |
0272a10d VZ |
1756 | fprintf(STDERR, " FAIL\n"); |
1757 | ierror += kerror; | |
1758 | } | |
1759 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1760 | if (allocation_now != current_allocation) | |
1761 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", | |
970f6abe | 1762 | current_allocation - allocation_now); |
9c0d9ce3 | 1763 | |
0272a10d VZ |
1764 | if (current_allocation != 0) |
1765 | { | |
1766 | memory_infop pinfo = pinformation; | |
1767 | ||
1768 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", | |
1769 | current_allocation); | |
9c0d9ce3 | 1770 | |
0272a10d VZ |
1771 | while (pinfo != NULL) |
1772 | { | |
970f6abe | 1773 | fprintf(STDERR, " %lu bytes at %x\n", |
0272a10d VZ |
1774 | (unsigned long)pinfo->size, (unsigned int)pinfo->pointer); |
1775 | pinfo = pinfo->next; | |
1776 | } | |
1777 | } | |
1778 | #endif | |
1779 | } | |
1780 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG | |
1781 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", | |
1782 | current_allocation); | |
1783 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", | |
1784 | maximum_allocation); | |
1785 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", | |
1786 | total_allocation); | |
1787 | fprintf(STDERR, " Number of allocations: %10d\n", | |
1788 | num_allocations); | |
1789 | #endif | |
1790 | } | |
1791 | ||
1792 | #ifdef PNGTEST_TIMING | |
1793 | t_stop = (float)clock(); | |
1794 | t_misc += (t_stop - t_start); | |
1795 | t_start = t_stop; | |
970f6abe | 1796 | fprintf(STDERR, " CPU time used = %.3f seconds", |
0272a10d | 1797 | (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC); |
970f6abe | 1798 | fprintf(STDERR, " (decoding %.3f,\n", |
0272a10d | 1799 | t_decode/(float)CLOCKS_PER_SEC); |
970f6abe | 1800 | fprintf(STDERR, " encoding %.3f ,", |
0272a10d | 1801 | t_encode/(float)CLOCKS_PER_SEC); |
970f6abe | 1802 | fprintf(STDERR, " other %.3f seconds)\n\n", |
0272a10d VZ |
1803 | t_misc/(float)CLOCKS_PER_SEC); |
1804 | #endif | |
1805 | ||
1806 | if (ierror == 0) | |
b61cc19c | 1807 | fprintf(STDERR, " libpng passes test\n"); |
9c0d9ce3 | 1808 | |
0272a10d | 1809 | else |
b61cc19c | 1810 | fprintf(STDERR, " libpng FAILS test\n"); |
9c0d9ce3 | 1811 | |
0272a10d VZ |
1812 | return (int)(ierror != 0); |
1813 | } | |
1814 | ||
1815 | /* Generate a compiler error if there is an old png.h in the search path. */ | |
9c0d9ce3 | 1816 | typedef png_libpng_version_1_5_6 Your_png_h_is_not_version_1_5_6; |