beta-0.89.2
[luatex.git] / source / libs / libpng / libpng-src / pngtest.c
blob57724e0651d6901808dcc0204ac3af200669359c
2 /* pngtest.c - a simple test program to test libpng
4 * Last changed in libpng 1.5.25 [December 3, 2015]
5 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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
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.
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.
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 ..."
34 #define _POSIX_SOURCE 1
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 /* Defined so I can write to a file on gui/windowing platforms */
41 /* #define STDERR stderr */
42 #define STDERR stdout /* For DOS */
44 #include "png.h"
46 /* Known chunks that exist in pngtest.png must be supported or pngtest will fail
47 * simply as a result of re-ordering them. This may be fixed in 1.7
49 * pngtest allocates a single row buffer for each row and overwrites it,
50 * therefore if the write side doesn't support the writing of interlaced images
51 * nothing can be done for an interlaced image (and the code below will fail
52 * horribly trying to write extra data after writing garbage).
54 #if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
55 defined PNG_READ_bKGD_SUPPORTED &&\
56 defined PNG_READ_cHRM_SUPPORTED &&\
57 defined PNG_READ_gAMA_SUPPORTED &&\
58 defined PNG_READ_oFFs_SUPPORTED &&\
59 defined PNG_READ_pCAL_SUPPORTED &&\
60 defined PNG_READ_pHYs_SUPPORTED &&\
61 defined PNG_READ_sBIT_SUPPORTED &&\
62 defined PNG_READ_sCAL_SUPPORTED &&\
63 defined PNG_READ_sRGB_SUPPORTED &&\
64 defined PNG_READ_sPLT_SUPPORTED &&\
65 defined PNG_READ_tEXt_SUPPORTED &&\
66 defined PNG_READ_tIME_SUPPORTED &&\
67 defined PNG_READ_zTXt_SUPPORTED &&\
68 (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
70 #ifdef PNG_ZLIB_HEADER
71 # include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
72 #else
73 # include "zlib.h"
74 #endif
76 /* Copied from pngpriv.h but only used in error messages below. */
77 #ifndef PNG_ZBUF_SIZE
78 # define PNG_ZBUF_SIZE 8192
79 #endif
80 #define FCLOSE(file) fclose(file)
82 #ifndef PNG_STDIO_SUPPORTED
83 typedef FILE * png_FILE_p;
84 #endif
86 /* Makes pngtest verbose so we can find problems. */
87 #ifndef PNG_DEBUG
88 # define PNG_DEBUG 0
89 #endif
91 #if PNG_DEBUG > 1
92 # define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
93 # define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
94 # define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
95 #else
96 # define pngtest_debug(m) ((void)0)
97 # define pngtest_debug1(m,p1) ((void)0)
98 # define pngtest_debug2(m,p1,p2) ((void)0)
99 #endif
101 #if !PNG_DEBUG
102 # define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
103 #endif
105 #ifndef PNG_UNUSED
106 # define PNG_UNUSED(param) (void)param;
107 #endif
109 /* Turn on CPU timing
110 #define PNGTEST_TIMING
113 #ifndef PNG_FLOATING_POINT_SUPPORTED
114 #undef PNGTEST_TIMING
115 #endif
117 #ifdef PNGTEST_TIMING
118 static float t_start, t_stop, t_decode, t_encode, t_misc;
119 #include <time.h>
120 #endif
122 #ifdef PNG_TIME_RFC1123_SUPPORTED
123 #define PNG_tIME_STRING_LENGTH 29
124 static int tIME_chunk_present = 0;
125 static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
127 #if PNG_LIBPNG_VER < 10619
128 #define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
130 static int
131 tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
133 png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
135 if (str == NULL)
136 return 0;
138 strcpy(ts, str);
139 return 1;
141 #endif /* older libpng */
142 #endif
144 static int verbose = 0;
145 static int strict = 0;
146 static int relaxed = 0;
147 static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
148 static int error_count = 0; /* count calls to png_error */
149 static int warning_count = 0; /* count calls to png_warning */
151 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
152 #ifndef png_jmpbuf
153 # define png_jmpbuf(png_ptr) png_ptr->jmpbuf
154 #endif
156 /* Defines for unknown chunk handling if required. */
157 #ifndef PNG_HANDLE_CHUNK_ALWAYS
158 # define PNG_HANDLE_CHUNK_ALWAYS 3
159 #endif
160 #ifndef PNG_HANDLE_CHUNK_IF_SAFE
161 # define PNG_HANDLE_CHUNK_IF_SAFE 2
162 #endif
164 /* Utility to save typing/errors, the argument must be a name */
165 #define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
167 /* Example of using row callbacks to make a simple progress meter */
168 static int status_pass = 1;
169 static int status_dots_requested = 0;
170 static int status_dots = 1;
172 static void PNGCBAPI
173 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
175 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
176 return;
178 if (status_pass != pass)
180 fprintf(stdout, "\n Pass %d: ", pass);
181 status_pass = pass;
182 status_dots = 31;
185 status_dots--;
187 if (status_dots == 0)
189 fprintf(stdout, "\n ");
190 status_dots=30;
193 fprintf(stdout, "r");
196 #ifdef PNG_WRITE_SUPPORTED
197 static void PNGCBAPI
198 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
200 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
201 return;
203 fprintf(stdout, "w");
205 #endif
208 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
209 /* Example of using a user transform callback (doesn't do anything at present).
211 static void PNGCBAPI
212 read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
214 PNG_UNUSED(png_ptr)
215 PNG_UNUSED(row_info)
216 PNG_UNUSED(data)
218 #endif
220 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
221 /* Example of using user transform callback (we don't transform anything,
222 * but merely count the zero samples)
225 static png_uint_32 zero_samples;
227 static void PNGCBAPI
228 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
230 png_bytep dp = data;
231 if (png_ptr == NULL)
232 return;
234 /* Contents of row_info:
235 * png_uint_32 width width of row
236 * png_uint_32 rowbytes number of bytes in row
237 * png_byte color_type color type of pixels
238 * png_byte bit_depth bit depth of samples
239 * png_byte channels number of channels (1-4)
240 * png_byte pixel_depth bits per pixel (depth*channels)
243 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
245 if (row_info->color_type == 0 || row_info->color_type == 3)
247 int pos = 0;
248 png_uint_32 n, nstop;
250 for (n = 0, nstop=row_info->width; n<nstop; n++)
252 if (row_info->bit_depth == 1)
254 if (((*dp << pos++ ) & 0x80) == 0)
255 zero_samples++;
257 if (pos == 8)
259 pos = 0;
260 dp++;
264 if (row_info->bit_depth == 2)
266 if (((*dp << (pos+=2)) & 0xc0) == 0)
267 zero_samples++;
269 if (pos == 8)
271 pos = 0;
272 dp++;
276 if (row_info->bit_depth == 4)
278 if (((*dp << (pos+=4)) & 0xf0) == 0)
279 zero_samples++;
281 if (pos == 8)
283 pos = 0;
284 dp++;
288 if (row_info->bit_depth == 8)
289 if (*dp++ == 0)
290 zero_samples++;
292 if (row_info->bit_depth == 16)
294 if ((*dp | *(dp+1)) == 0)
295 zero_samples++;
296 dp+=2;
300 else /* Other color types */
302 png_uint_32 n, nstop;
303 int channel;
304 int color_channels = row_info->channels;
305 if (row_info->color_type > 3)
306 color_channels--;
308 for (n = 0, nstop=row_info->width; n<nstop; n++)
310 for (channel = 0; channel < color_channels; channel++)
312 if (row_info->bit_depth == 8)
313 if (*dp++ == 0)
314 zero_samples++;
316 if (row_info->bit_depth == 16)
318 if ((*dp | *(dp+1)) == 0)
319 zero_samples++;
321 dp+=2;
324 if (row_info->color_type > 3)
326 dp++;
327 if (row_info->bit_depth == 16)
328 dp++;
333 #endif /* WRITE_USER_TRANSFORM */
335 #ifndef PNG_STDIO_SUPPORTED
336 /* START of code to validate stdio-free compilation */
337 /* These copies of the default read/write functions come from pngrio.c and
338 * pngwio.c. They allow "don't include stdio" testing of the library.
339 * This is the function that does the actual reading of data. If you are
340 * not reading from a standard C stream, you should create a replacement
341 * read_data function and use it at run time with png_set_read_fn(), rather
342 * than changing the library.
345 #ifdef PNG_IO_STATE_SUPPORTED
346 void
347 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
348 png_uint_32 io_op);
349 void
350 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
351 png_uint_32 io_op)
353 png_uint_32 io_state = png_get_io_state(png_ptr);
354 int err = 0;
356 /* Check if the current operation (reading / writing) is as expected. */
357 if ((io_state & PNG_IO_MASK_OP) != io_op)
358 png_error(png_ptr, "Incorrect operation in I/O state");
360 /* Check if the buffer size specific to the current location
361 * (file signature / header / data / crc) is as expected.
363 switch (io_state & PNG_IO_MASK_LOC)
365 case PNG_IO_SIGNATURE:
366 if (data_length > 8)
367 err = 1;
368 break;
369 case PNG_IO_CHUNK_HDR:
370 if (data_length != 8)
371 err = 1;
372 break;
373 case PNG_IO_CHUNK_DATA:
374 break; /* no restrictions here */
375 case PNG_IO_CHUNK_CRC:
376 if (data_length != 4)
377 err = 1;
378 break;
379 default:
380 err = 1; /* uninitialized */
382 if (err != 0)
383 png_error(png_ptr, "Bad I/O state or buffer size");
385 #endif
387 static void PNGCBAPI
388 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
390 png_size_t check = 0;
391 png_voidp io_ptr;
393 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
394 * instead of an int, which is what fread() actually returns.
396 io_ptr = png_get_io_ptr(png_ptr);
397 if (io_ptr != NULL)
399 check = fread(data, 1, length, (png_FILE_p)io_ptr);
402 if (check != length)
404 png_error(png_ptr, "Read Error");
407 #ifdef PNG_IO_STATE_SUPPORTED
408 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
409 #endif
412 #ifdef PNG_WRITE_FLUSH_SUPPORTED
413 static void PNGCBAPI
414 pngtest_flush(png_structp png_ptr)
416 /* Do nothing; fflush() is said to be just a waste of energy. */
417 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
419 #endif
421 /* This is the function that does the actual writing of data. If you are
422 * not writing to a standard C stream, you should create a replacement
423 * write_data function and use it at run time with png_set_write_fn(), rather
424 * than changing the library.
426 static void PNGCBAPI
427 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
429 png_size_t check;
431 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
433 if (check != length)
435 png_error(png_ptr, "Write Error");
438 #ifdef PNG_IO_STATE_SUPPORTED
439 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
440 #endif
442 #endif /* !STDIO */
444 /* This function is called when there is a warning, but the library thinks
445 * it can continue anyway. Replacement functions don't have to do anything
446 * here if you don't want to. In the default configuration, png_ptr is
447 * not used, but it is passed in case it may be useful.
449 typedef struct
451 PNG_CONST char *file_name;
452 } pngtest_error_parameters;
454 static void PNGCBAPI
455 pngtest_warning(png_structp png_ptr, png_const_charp message)
457 PNG_CONST char *name = "UNKNOWN (ERROR!)";
458 pngtest_error_parameters *test =
459 (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
461 ++warning_count;
463 if (test != NULL && test->file_name != NULL)
464 name = test->file_name;
466 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
469 /* This is the default error handling function. Note that replacements for
470 * this function MUST NOT RETURN, or the program will likely crash. This
471 * function is used by default, or if the program supplies NULL for the
472 * error function pointer in png_set_error_fn().
474 static void PNGCBAPI
475 pngtest_error(png_structp png_ptr, png_const_charp message)
477 ++error_count;
479 pngtest_warning(png_ptr, message);
480 /* We can return because png_error calls the default handler, which is
481 * actually OK in this case.
485 /* END of code to validate stdio-free compilation */
487 /* START of code to validate memory allocation and deallocation */
488 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
490 /* Allocate memory. For reasonable files, size should never exceed
491 * 64K. However, zlib may allocate more than 64K if you don't tell
492 * it not to. See zconf.h and png.h for more information. zlib does
493 * need to allocate exactly 64K, so whatever you call here must
494 * have the ability to do that.
496 * This piece of code can be compiled to validate max 64K allocations
497 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
499 typedef struct memory_information
501 png_alloc_size_t size;
502 png_voidp pointer;
503 struct memory_information *next;
504 } memory_information;
505 typedef memory_information *memory_infop;
507 static memory_infop pinformation = NULL;
508 static int current_allocation = 0;
509 static int maximum_allocation = 0;
510 static int total_allocation = 0;
511 static int num_allocations = 0;
513 png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
514 png_alloc_size_t size));
515 void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
517 png_voidp
518 PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
521 /* png_malloc has already tested for NULL; png_create_struct calls
522 * png_debug_malloc directly, with png_ptr == NULL which is OK
525 if (size == 0)
526 return (NULL);
528 /* This calls the library allocator twice, once to get the requested
529 buffer and once to get a new free list entry. */
531 /* Disable malloc_fn and free_fn */
532 memory_infop pinfo;
533 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
534 pinfo = (memory_infop)png_malloc(png_ptr,
535 (sizeof *pinfo));
536 pinfo->size = size;
537 current_allocation += size;
538 total_allocation += size;
539 num_allocations ++;
541 if (current_allocation > maximum_allocation)
542 maximum_allocation = current_allocation;
544 pinfo->pointer = png_malloc(png_ptr, size);
545 /* Restore malloc_fn and free_fn */
547 png_set_mem_fn(png_ptr,
548 NULL, png_debug_malloc, png_debug_free);
550 if (size != 0 && pinfo->pointer == NULL)
552 current_allocation -= size;
553 total_allocation -= size;
554 png_error(png_ptr,
555 "out of memory in pngtest->png_debug_malloc");
558 pinfo->next = pinformation;
559 pinformation = pinfo;
560 /* Make sure the caller isn't assuming zeroed memory. */
561 memset(pinfo->pointer, 0xdd, pinfo->size);
563 if (verbose != 0)
564 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
565 pinfo->pointer);
567 return (png_voidp)(pinfo->pointer);
571 /* Free a pointer. It is removed from the list at the same time. */
572 void PNGCBAPI
573 png_debug_free(png_structp png_ptr, png_voidp ptr)
575 if (png_ptr == NULL)
576 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
578 if (ptr == 0)
580 #if 0 /* This happens all the time. */
581 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
582 #endif
583 return;
586 /* Unlink the element from the list. */
587 if (pinformation != NULL)
589 memory_infop *ppinfo = &pinformation;
591 for (;;)
593 memory_infop pinfo = *ppinfo;
595 if (pinfo->pointer == ptr)
597 *ppinfo = pinfo->next;
598 current_allocation -= pinfo->size;
599 if (current_allocation < 0)
600 fprintf(STDERR, "Duplicate free of memory\n");
601 /* We must free the list element too, but first kill
602 the memory that is to be freed. */
603 memset(ptr, 0x55, pinfo->size);
604 free(pinfo);
605 pinfo = NULL;
606 break;
609 if (pinfo->next == NULL)
611 fprintf(STDERR, "Pointer %p not found\n", ptr);
612 break;
615 ppinfo = &pinfo->next;
619 /* Finally free the data. */
620 if (verbose != 0)
621 printf("Freeing %p\n", ptr);
623 if (ptr != NULL)
624 free(ptr);
625 ptr = NULL;
627 #endif /* USER_MEM && DEBUG */
628 /* END of code to test memory allocation/deallocation */
631 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
632 /* Demonstration of user chunk support of the sTER and vpAg chunks */
634 /* (sTER is a public chunk not yet known by libpng. vpAg is a private
635 chunk used in ImageMagick to store "virtual page" size). */
637 static struct user_chunk_data
639 png_const_infop info_ptr;
640 png_uint_32 vpAg_width, vpAg_height;
641 png_byte vpAg_units;
642 png_byte sTER_mode;
643 int location[2];
645 user_chunk_data;
647 /* Used for location and order; zero means nothing. */
648 #define have_sTER 0x01
649 #define have_vpAg 0x02
650 #define before_PLTE 0x10
651 #define before_IDAT 0x20
652 #define after_IDAT 0x40
654 static void
655 init_callback_info(png_const_infop info_ptr)
657 MEMZERO(user_chunk_data);
658 user_chunk_data.info_ptr = info_ptr;
661 static int
662 set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
664 int location;
666 if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
667 return 0; /* already have one of these */
669 /* Find where we are (the code below zeroes info_ptr to indicate that the
670 * chunks before the first IDAT have been read.)
672 if (data->info_ptr == NULL) /* after IDAT */
673 location = what | after_IDAT;
675 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
676 location = what | before_IDAT;
678 else
679 location = what | before_PLTE;
681 if (data->location[0] == 0)
682 data->location[0] = location;
684 else
685 data->location[1] = location;
687 return 1; /* handled */
690 static int PNGCBAPI
691 read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
693 struct user_chunk_data *my_user_chunk_data =
694 (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
696 if (my_user_chunk_data == NULL)
697 png_error(png_ptr, "lost user chunk pointer");
699 /* Return one of the following:
700 * return (-n); chunk had an error
701 * return (0); did not recognize
702 * return (n); success
704 * The unknown chunk structure contains the chunk data:
705 * png_byte name[5];
706 * png_byte *data;
707 * png_size_t size;
709 * Note that libpng has already taken care of the CRC handling.
712 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
713 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
715 /* Found sTER chunk */
716 if (chunk->size != 1)
717 return (-1); /* Error return */
719 if (chunk->data[0] != 0 && chunk->data[0] != 1)
720 return (-1); /* Invalid mode */
722 if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
724 my_user_chunk_data->sTER_mode=chunk->data[0];
725 return (1);
728 else
729 return (0); /* duplicate sTER - give it to libpng */
732 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
733 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
734 return (0); /* Did not recognize */
736 /* Found ImageMagick vpAg chunk */
738 if (chunk->size != 9)
739 return (-1); /* Error return */
741 if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
742 return (0); /* duplicate vpAg */
744 my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
745 my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
746 my_user_chunk_data->vpAg_units = chunk->data[8];
748 return (1);
751 #ifdef PNG_WRITE_SUPPORTED
752 static void
753 write_sTER_chunk(png_structp write_ptr)
755 png_byte sTER[5] = {115, 84, 69, 82, '\0'};
757 if (verbose != 0)
758 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
760 png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
763 static void
764 write_vpAg_chunk(png_structp write_ptr)
766 png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
768 png_byte vpag_chunk_data[9];
770 if (verbose != 0)
771 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
772 (unsigned long)user_chunk_data.vpAg_width,
773 (unsigned long)user_chunk_data.vpAg_height,
774 user_chunk_data.vpAg_units);
776 png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
777 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
778 vpag_chunk_data[8] = user_chunk_data.vpAg_units;
779 png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
782 static void
783 write_chunks(png_structp write_ptr, int location)
785 int i;
787 /* Notice that this preserves the original chunk order, however chunks
788 * intercepted by the callback will be written *after* chunks passed to
789 * libpng. This will actually reverse a pair of sTER chunks or a pair of
790 * vpAg chunks, resulting in an error later. This is not worth worrying
791 * about - the chunks should not be duplicated!
793 for (i=0; i<2; ++i)
795 if (user_chunk_data.location[i] == (location | have_sTER))
796 write_sTER_chunk(write_ptr);
798 else if (user_chunk_data.location[i] == (location | have_vpAg))
799 write_vpAg_chunk(write_ptr);
802 #endif /* WRITE */
803 #else /* !READ_USER_CHUNKS */
804 # define write_chunks(pp,loc) ((void)0)
805 #endif
806 /* END of code to demonstrate user chunk support */
808 /* START of code to check that libpng has the required text support; this only
809 * checks for the write support because if read support is missing the chunk
810 * will simply not be reported back to pngtest.
812 #ifdef PNG_TEXT_SUPPORTED
813 static void
814 pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
815 int num_text)
817 while (num_text > 0)
819 switch (text_ptr[--num_text].compression)
821 case PNG_TEXT_COMPRESSION_NONE:
822 break;
824 case PNG_TEXT_COMPRESSION_zTXt:
825 # ifndef PNG_WRITE_zTXt_SUPPORTED
826 ++unsupported_chunks;
827 /* In libpng 1.7 this now does an app-error, so stop it: */
828 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
829 # endif
830 break;
832 case PNG_ITXT_COMPRESSION_NONE:
833 case PNG_ITXT_COMPRESSION_zTXt:
834 # ifndef PNG_WRITE_iTXt_SUPPORTED
835 ++unsupported_chunks;
836 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
837 # endif
838 break;
840 default:
841 /* This is an error */
842 png_error(png_ptr, "invalid text chunk compression field");
843 break;
847 #endif
848 /* END of code to check that libpng has the required text support */
850 /* Test one file */
851 static int
852 test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
854 static png_FILE_p fpin;
855 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
856 pngtest_error_parameters error_parameters;
857 png_structp read_ptr;
858 png_infop read_info_ptr, end_info_ptr;
859 #ifdef PNG_WRITE_SUPPORTED
860 png_structp write_ptr;
861 png_infop write_info_ptr;
862 png_infop write_end_info_ptr;
863 #ifdef PNG_WRITE_FILTER_SUPPORTED
864 int interlace_preserved = 1;
865 #endif /* WRITE_FILTER */
866 #else /* !WRITE */
867 png_structp write_ptr = NULL;
868 png_infop write_info_ptr = NULL;
869 png_infop write_end_info_ptr = NULL;
870 #endif /* !WRITE */
871 png_bytep row_buf;
872 png_uint_32 y;
873 png_uint_32 width, height;
874 volatile int num_passes;
875 int pass;
876 int bit_depth, color_type;
878 row_buf = NULL;
879 error_parameters.file_name = inname;
881 if ((fpin = fopen(inname, "rb")) == NULL)
883 fprintf(STDERR, "Could not find input file %s\n", inname);
884 return (1);
887 if ((fpout = fopen(outname, "wb")) == NULL)
889 fprintf(STDERR, "Could not open output file %s\n", outname);
890 FCLOSE(fpin);
891 return (1);
894 pngtest_debug("Allocating read and write structures");
895 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
896 read_ptr =
897 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
898 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
899 #else
900 read_ptr =
901 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
902 #endif
903 png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
904 pngtest_warning);
906 #ifdef PNG_WRITE_SUPPORTED
907 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
908 write_ptr =
909 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
910 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
911 #else
912 write_ptr =
913 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
914 #endif
915 png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
916 pngtest_warning);
917 #endif
918 pngtest_debug("Allocating read_info, write_info and end_info structures");
919 read_info_ptr = png_create_info_struct(read_ptr);
920 end_info_ptr = png_create_info_struct(read_ptr);
921 #ifdef PNG_WRITE_SUPPORTED
922 write_info_ptr = png_create_info_struct(write_ptr);
923 write_end_info_ptr = png_create_info_struct(write_ptr);
924 #endif
926 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
927 init_callback_info(read_info_ptr);
928 png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
929 read_user_chunk_callback);
930 #endif
932 #ifdef PNG_SETJMP_SUPPORTED
933 pngtest_debug("Setting jmpbuf for read struct");
934 if (setjmp(png_jmpbuf(read_ptr)))
936 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
937 png_free(read_ptr, row_buf);
938 row_buf = NULL;
939 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
940 #ifdef PNG_WRITE_SUPPORTED
941 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
942 png_destroy_write_struct(&write_ptr, &write_info_ptr);
943 #endif
944 FCLOSE(fpin);
945 FCLOSE(fpout);
946 return (1);
949 #ifdef PNG_WRITE_SUPPORTED
950 pngtest_debug("Setting jmpbuf for write struct");
952 if (setjmp(png_jmpbuf(write_ptr)))
954 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
955 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
956 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
957 #ifdef PNG_WRITE_SUPPORTED
958 png_destroy_write_struct(&write_ptr, &write_info_ptr);
959 #endif
960 FCLOSE(fpin);
961 FCLOSE(fpout);
962 return (1);
964 #endif
965 #endif
967 if (strict != 0)
969 /* Treat png_benign_error() as errors on read */
970 png_set_benign_errors(read_ptr, 0);
972 #ifdef PNG_WRITE_SUPPORTED
973 /* Treat them as errors on write */
974 png_set_benign_errors(write_ptr, 0);
975 #endif
977 /* if strict is not set, then app warnings and errors are treated as
978 * warnings in release builds, but not in unstable builds; this can be
979 * changed with '--relaxed'.
983 else if (relaxed != 0)
985 /* Allow application (pngtest) errors and warnings to pass */
986 png_set_benign_errors(read_ptr, 1);
988 #ifdef PNG_WRITE_SUPPORTED
989 png_set_benign_errors(write_ptr, 1);
990 #endif
993 pngtest_debug("Initializing input and output streams");
994 #ifdef PNG_STDIO_SUPPORTED
995 png_init_io(read_ptr, fpin);
996 # ifdef PNG_WRITE_SUPPORTED
997 png_init_io(write_ptr, fpout);
998 # endif
999 #else
1000 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1001 # ifdef PNG_WRITE_SUPPORTED
1002 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
1003 # ifdef PNG_WRITE_FLUSH_SUPPORTED
1004 pngtest_flush);
1005 # else
1006 NULL);
1007 # endif
1008 # endif
1009 #endif
1011 if (status_dots_requested == 1)
1013 #ifdef PNG_WRITE_SUPPORTED
1014 png_set_write_status_fn(write_ptr, write_row_callback);
1015 #endif
1016 png_set_read_status_fn(read_ptr, read_row_callback);
1019 else
1021 #ifdef PNG_WRITE_SUPPORTED
1022 png_set_write_status_fn(write_ptr, NULL);
1023 #endif
1024 png_set_read_status_fn(read_ptr, NULL);
1027 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1028 png_set_read_user_transform_fn(read_ptr, read_user_callback);
1029 #endif
1030 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1031 zero_samples = 0;
1032 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1033 #endif
1035 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1036 /* Preserve all the unknown chunks, if possible. If this is disabled then,
1037 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1038 * libpng to *save* the unknown chunks on read (because we can't switch the
1039 * save option on!)
1041 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1042 * unknown chunks and write will write them all.
1044 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1045 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1046 NULL, 0);
1047 #endif
1048 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1049 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1050 NULL, 0);
1051 #endif
1052 #endif
1054 pngtest_debug("Reading info struct");
1055 png_read_info(read_ptr, read_info_ptr);
1057 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1058 /* This is a bit of a hack; there is no obvious way in the callback function
1059 * to determine that the chunks before the first IDAT have been read, so
1060 * remove the info_ptr (which is only used to determine position relative to
1061 * PLTE) here to indicate that we are after the IDAT.
1063 user_chunk_data.info_ptr = NULL;
1064 #endif
1066 pngtest_debug("Transferring info struct");
1068 int interlace_type, compression_type, filter_type;
1070 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1071 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
1073 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
1074 color_type, interlace_type, compression_type, filter_type);
1075 /* num_passes may not be available below if interlace support is not
1076 * provided by libpng for both read and write.
1078 switch (interlace_type)
1080 case PNG_INTERLACE_NONE:
1081 num_passes = 1;
1082 break;
1084 case PNG_INTERLACE_ADAM7:
1085 num_passes = 7;
1086 break;
1088 default:
1089 png_error(read_ptr, "invalid interlace type");
1090 /*NOT REACHED*/
1094 else
1095 png_error(read_ptr, "png_get_IHDR failed");
1097 #ifdef PNG_FIXED_POINT_SUPPORTED
1098 #ifdef PNG_cHRM_SUPPORTED
1100 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1101 blue_y;
1103 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1104 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1106 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1107 red_y, green_x, green_y, blue_x, blue_y);
1110 #endif
1111 #ifdef PNG_gAMA_SUPPORTED
1113 png_fixed_point gamma;
1115 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
1116 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
1118 #endif
1119 #else /* Use floating point versions */
1120 #ifdef PNG_FLOATING_POINT_SUPPORTED
1121 #ifdef PNG_cHRM_SUPPORTED
1123 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1124 blue_y;
1126 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1127 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1129 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1130 red_y, green_x, green_y, blue_x, blue_y);
1133 #endif
1134 #ifdef PNG_gAMA_SUPPORTED
1136 double gamma;
1138 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
1139 png_set_gAMA(write_ptr, write_info_ptr, gamma);
1141 #endif
1142 #endif /* Floating point */
1143 #endif /* Fixed point */
1144 #ifdef PNG_iCCP_SUPPORTED
1146 png_charp name;
1147 png_bytep profile;
1148 png_uint_32 proflen;
1149 int compression_type;
1151 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1152 &profile, &proflen) != 0)
1154 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1155 profile, proflen);
1158 #endif
1159 #ifdef PNG_sRGB_SUPPORTED
1161 int intent;
1163 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
1164 png_set_sRGB(write_ptr, write_info_ptr, intent);
1166 #endif
1168 png_colorp palette;
1169 int num_palette;
1171 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
1172 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
1174 #ifdef PNG_bKGD_SUPPORTED
1176 png_color_16p background;
1178 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
1180 png_set_bKGD(write_ptr, write_info_ptr, background);
1183 #endif
1184 #ifdef PNG_hIST_SUPPORTED
1186 png_uint_16p hist;
1188 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
1189 png_set_hIST(write_ptr, write_info_ptr, hist);
1191 #endif
1192 #ifdef PNG_oFFs_SUPPORTED
1194 png_int_32 offset_x, offset_y;
1195 int unit_type;
1197 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1198 &unit_type) != 0)
1200 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1203 #endif
1204 #ifdef PNG_pCAL_SUPPORTED
1206 png_charp purpose, units;
1207 png_charpp params;
1208 png_int_32 X0, X1;
1209 int type, nparams;
1211 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1212 &nparams, &units, &params) != 0)
1214 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1215 nparams, units, params);
1218 #endif
1219 #ifdef PNG_pHYs_SUPPORTED
1221 png_uint_32 res_x, res_y;
1222 int unit_type;
1224 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1225 &unit_type) != 0)
1226 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
1228 #endif
1229 #ifdef PNG_sBIT_SUPPORTED
1231 png_color_8p sig_bit;
1233 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
1234 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
1236 #endif
1237 #ifdef PNG_sCAL_SUPPORTED
1238 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1239 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
1241 int unit;
1242 double scal_width, scal_height;
1244 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1245 &scal_height) != 0)
1247 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1250 #else
1251 #ifdef PNG_FIXED_POINT_SUPPORTED
1253 int unit;
1254 png_charp scal_width, scal_height;
1256 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1257 &scal_height) != 0)
1259 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1260 scal_height);
1263 #endif
1264 #endif
1265 #endif
1267 #ifdef PNG_sPLT_SUPPORTED
1269 png_sPLT_tp entries;
1271 int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1272 if (num_entries)
1274 png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1277 #endif
1279 #ifdef PNG_TEXT_SUPPORTED
1281 png_textp text_ptr;
1282 int num_text;
1284 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1286 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1288 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1290 if (verbose != 0)
1292 int i;
1294 printf("\n");
1295 for (i=0; i<num_text; i++)
1297 printf(" Text compression[%d]=%d\n",
1298 i, text_ptr[i].compression);
1302 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1305 #endif
1306 #ifdef PNG_tIME_SUPPORTED
1308 png_timep mod_time;
1310 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
1312 png_set_tIME(write_ptr, write_info_ptr, mod_time);
1313 #ifdef PNG_TIME_RFC1123_SUPPORTED
1314 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1315 tIME_string[(sizeof tIME_string) - 1] = '\0';
1317 else
1319 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1320 tIME_string[(sizeof tIME_string) - 1] = '\0';
1323 tIME_chunk_present++;
1324 #endif /* TIME_RFC1123 */
1327 #endif
1328 #ifdef PNG_tRNS_SUPPORTED
1330 png_bytep trans_alpha;
1331 int num_trans;
1332 png_color_16p trans_color;
1334 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1335 &trans_color) != 0)
1337 int sample_max = (1 << bit_depth);
1338 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1339 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1340 (int)trans_color->gray > sample_max) ||
1341 (color_type == PNG_COLOR_TYPE_RGB &&
1342 ((int)trans_color->red > sample_max ||
1343 (int)trans_color->green > sample_max ||
1344 (int)trans_color->blue > sample_max))))
1345 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1346 trans_color);
1349 #endif
1350 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1352 png_unknown_chunkp unknowns;
1353 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1354 &unknowns);
1356 if (num_unknowns != 0)
1358 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1359 num_unknowns);
1360 #if PNG_LIBPNG_VER < 10600
1361 /* Copy the locations from the read_info_ptr. The automatically
1362 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1363 * because they are reset from the write pointer (removed in 1.6.0).
1366 int i;
1367 for (i = 0; i < num_unknowns; i++)
1368 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1369 unknowns[i].location);
1371 #endif
1374 #endif
1376 #ifdef PNG_WRITE_SUPPORTED
1377 pngtest_debug("Writing info struct");
1379 /* Write the info in two steps so that if we write the 'unknown' chunks here
1380 * they go to the correct place.
1382 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1384 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1386 png_write_info(write_ptr, write_info_ptr);
1388 write_chunks(write_ptr, before_IDAT); /* after PLTE */
1389 #endif
1391 #ifdef SINGLE_ROWBUF_ALLOC
1392 pngtest_debug("Allocating row buffer...");
1393 row_buf = (png_bytep)png_malloc(read_ptr,
1394 png_get_rowbytes(read_ptr, read_info_ptr));
1396 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1397 #endif /* SINGLE_ROWBUF_ALLOC */
1398 pngtest_debug("Writing row data");
1400 #if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1401 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1402 /* Both must be defined for libpng to be able to handle the interlace,
1403 * otherwise it gets handled below by simply reading and writing the passes
1404 * directly.
1406 if (png_set_interlace_handling(read_ptr) != num_passes)
1407 png_error(write_ptr,
1408 "png_set_interlace_handling(read): wrong pass count ");
1409 if (png_set_interlace_handling(write_ptr) != num_passes)
1410 png_error(write_ptr,
1411 "png_set_interlace_handling(write): wrong pass count ");
1412 #else /* png_set_interlace_handling not called on either read or write */
1413 # define calc_pass_height
1414 #endif /* not using libpng interlace handling */
1416 #ifdef PNGTEST_TIMING
1417 t_stop = (float)clock();
1418 t_misc += (t_stop - t_start);
1419 t_start = t_stop;
1420 #endif
1421 for (pass = 0; pass < num_passes; pass++)
1423 # ifdef calc_pass_height
1424 png_uint_32 pass_height;
1426 if (num_passes == 7) /* interlaced */
1428 if (PNG_PASS_COLS(width, pass) > 0)
1429 pass_height = PNG_PASS_ROWS(height, pass);
1431 else
1432 pass_height = 0;
1435 else /* not interlaced */
1436 pass_height = height;
1437 # else
1438 # define pass_height height
1439 # endif
1441 pngtest_debug1("Writing row data for pass %d", pass);
1442 for (y = 0; y < pass_height; y++)
1444 #ifndef SINGLE_ROWBUF_ALLOC
1445 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1447 row_buf = (png_bytep)png_malloc(read_ptr,
1448 png_get_rowbytes(read_ptr, read_info_ptr));
1450 pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1451 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
1453 #endif /* !SINGLE_ROWBUF_ALLOC */
1454 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1456 #ifdef PNG_WRITE_SUPPORTED
1457 #ifdef PNGTEST_TIMING
1458 t_stop = (float)clock();
1459 t_decode += (t_stop - t_start);
1460 t_start = t_stop;
1461 #endif
1462 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1463 #ifdef PNGTEST_TIMING
1464 t_stop = (float)clock();
1465 t_encode += (t_stop - t_start);
1466 t_start = t_stop;
1467 #endif
1468 #endif /* WRITE */
1470 #ifndef SINGLE_ROWBUF_ALLOC
1471 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1472 png_free(read_ptr, row_buf);
1473 row_buf = NULL;
1474 #endif /* !SINGLE_ROWBUF_ALLOC */
1478 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1479 # ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1480 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1481 # endif
1482 # ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1483 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1484 # endif
1485 #endif
1487 pngtest_debug("Reading and writing end_info data");
1489 png_read_end(read_ptr, end_info_ptr);
1490 #ifdef PNG_TEXT_SUPPORTED
1492 png_textp text_ptr;
1493 int num_text;
1495 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1497 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1499 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1501 if (verbose != 0)
1503 int i;
1505 printf("\n");
1506 for (i=0; i<num_text; i++)
1508 printf(" Text compression[%d]=%d\n",
1509 i, text_ptr[i].compression);
1513 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1516 #endif
1517 #ifdef PNG_tIME_SUPPORTED
1519 png_timep mod_time;
1521 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
1523 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1524 #ifdef PNG_TIME_RFC1123_SUPPORTED
1525 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1526 tIME_string[(sizeof tIME_string) - 1] = '\0';
1528 else
1530 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1531 tIME_string[(sizeof tIME_string)-1] = '\0';
1534 tIME_chunk_present++;
1535 #endif /* TIME_RFC1123 */
1538 #endif
1539 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1541 png_unknown_chunkp unknowns;
1542 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1543 &unknowns);
1545 if (num_unknowns != 0)
1547 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1548 num_unknowns);
1549 #if PNG_LIBPNG_VER < 10600
1550 /* Copy the locations from the read_info_ptr. The automatically
1551 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1552 * because they are reset from the write pointer (removed in 1.6.0).
1555 int i;
1556 for (i = 0; i < num_unknowns; i++)
1557 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1558 unknowns[i].location);
1560 #endif
1563 #endif
1565 #ifdef PNG_WRITE_SUPPORTED
1566 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1567 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1568 * This is here just to make pngtest replicate the results from libpng
1569 * versions prior to 1.5.4, and to test this new API.
1571 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1572 #endif
1574 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1575 * do it is to write them *before* calling png_write_end. When unknown
1576 * chunks are written by libpng, however, they are written just before IEND.
1577 * There seems to be no way round this, however vpAg/sTER are not expected
1578 * after IDAT.
1580 write_chunks(write_ptr, after_IDAT);
1582 png_write_end(write_ptr, write_end_info_ptr);
1583 #endif
1585 #ifdef PNG_EASY_ACCESS_SUPPORTED
1586 if (verbose != 0)
1588 png_uint_32 iwidth, iheight;
1589 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1590 iheight = png_get_image_height(write_ptr, write_info_ptr);
1591 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1592 (unsigned long)iwidth, (unsigned long)iheight);
1594 #endif
1596 pngtest_debug("Destroying data structs");
1597 #ifdef SINGLE_ROWBUF_ALLOC
1598 pngtest_debug("destroying row_buf for read_ptr");
1599 png_free(read_ptr, row_buf);
1600 row_buf = NULL;
1601 #endif /* SINGLE_ROWBUF_ALLOC */
1602 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
1603 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1604 #ifdef PNG_WRITE_SUPPORTED
1605 pngtest_debug("destroying write_end_info_ptr");
1606 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1607 pngtest_debug("destroying write_ptr, write_info_ptr");
1608 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1609 #endif
1610 pngtest_debug("Destruction complete.");
1612 FCLOSE(fpin);
1613 FCLOSE(fpout);
1615 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1616 * Unsupported chunks can result in warnings, in that case ignore the strict
1617 * setting, otherwise fail the test on warnings as well as errors.
1619 if (error_count > 0)
1621 /* We don't really expect to get here because of the setjmp handling
1622 * above, but this is safe.
1624 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
1625 inname, error_count, warning_count);
1627 if (strict != 0)
1628 return (1);
1631 # ifdef PNG_WRITE_SUPPORTED
1632 /* If there is no write support nothing was written! */
1633 else if (unsupported_chunks > 0)
1635 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1636 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1638 # endif
1640 else if (warning_count > 0)
1642 fprintf(STDERR, "\n %s: %d libpng warnings found",
1643 inname, warning_count);
1645 if (strict != 0)
1646 return (1);
1649 pngtest_debug("Opening files for comparison");
1650 if ((fpin = fopen(inname, "rb")) == NULL)
1652 fprintf(STDERR, "Could not find file %s\n", inname);
1653 return (1);
1656 if ((fpout = fopen(outname, "rb")) == NULL)
1658 fprintf(STDERR, "Could not find file %s\n", outname);
1659 FCLOSE(fpin);
1660 return (1);
1663 #if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1664 defined (PNG_WRITE_FILTER_SUPPORTED)
1665 if (interlace_preserved != 0) /* else the files will be changed */
1667 for (;;)
1669 static int wrote_question = 0;
1670 png_size_t num_in, num_out;
1671 char inbuf[256], outbuf[256];
1673 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1674 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1676 if (num_in != num_out)
1678 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1679 inname, outname);
1681 if (wrote_question == 0 && unsupported_chunks == 0)
1683 fprintf(STDERR,
1684 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1685 inname, PNG_ZBUF_SIZE);
1686 fprintf(STDERR,
1687 "\n filtering heuristic (libpng default), compression");
1688 fprintf(STDERR,
1689 " level (zlib default),\n and zlib version (%s)?\n\n",
1690 ZLIB_VERSION);
1691 wrote_question = 1;
1694 FCLOSE(fpin);
1695 FCLOSE(fpout);
1697 if (strict != 0 && unsupported_chunks == 0)
1698 return (1);
1700 else
1701 return (0);
1704 if (num_in == 0)
1705 break;
1707 if (memcmp(inbuf, outbuf, num_in))
1709 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1710 outname);
1712 if (wrote_question == 0 && unsupported_chunks == 0)
1714 fprintf(STDERR,
1715 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1716 inname, PNG_ZBUF_SIZE);
1717 fprintf(STDERR,
1718 "\n filtering heuristic (libpng default), compression");
1719 fprintf(STDERR,
1720 " level (zlib default),\n and zlib version (%s)?\n\n",
1721 ZLIB_VERSION);
1722 wrote_question = 1;
1725 FCLOSE(fpin);
1726 FCLOSE(fpout);
1728 /* NOTE: the unsupported_chunks escape is permitted here because
1729 * unsupported text chunk compression will result in the compression
1730 * mode being changed (to NONE) yet, in the test case, the result
1731 * can be exactly the same size!
1733 if (strict != 0 && unsupported_chunks == 0)
1734 return (1);
1736 else
1737 return (0);
1741 #endif /* WRITE && WRITE_FILTER */
1743 FCLOSE(fpin);
1744 FCLOSE(fpout);
1746 return (0);
1749 /* Input and output filenames */
1750 #ifdef RISCOS
1751 static PNG_CONST char *inname = "pngtest/png";
1752 static PNG_CONST char *outname = "pngout/png";
1753 #else
1754 static PNG_CONST char *inname = "pngtest.png";
1755 static PNG_CONST char *outname = "pngout.png";
1756 #endif
1759 main(int argc, char *argv[])
1761 int multiple = 0;
1762 int ierror = 0;
1764 png_structp dummy_ptr;
1766 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1767 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
1768 fprintf(STDERR, "%s", png_get_copyright(NULL));
1769 /* Show the version of libpng used in building the library */
1770 fprintf(STDERR, " library (%lu):%s",
1771 (unsigned long)png_access_version_number(),
1772 png_get_header_version(NULL));
1774 /* Show the version of libpng used in building the application */
1775 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1776 PNG_HEADER_VERSION_STRING);
1778 /* Do some consistency checking on the memory allocation settings, I'm
1779 * not sure this matters, but it is nice to know, the first of these
1780 * tests should be impossible because of the way the macros are set
1781 * in pngconf.h
1783 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1784 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1785 #endif
1786 /* I think the following can happen. */
1787 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1788 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1789 #endif
1791 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1793 fprintf(STDERR,
1794 "Warning: versions are different between png.h and png.c\n");
1795 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1796 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1797 ++ierror;
1800 if (argc > 1)
1802 if (strcmp(argv[1], "-m") == 0)
1804 multiple = 1;
1805 status_dots_requested = 0;
1808 else if (strcmp(argv[1], "-mv") == 0 ||
1809 strcmp(argv[1], "-vm") == 0 )
1811 multiple = 1;
1812 verbose = 1;
1813 status_dots_requested = 1;
1816 else if (strcmp(argv[1], "-v") == 0)
1818 verbose = 1;
1819 status_dots_requested = 1;
1820 inname = argv[2];
1823 else if (strcmp(argv[1], "--strict") == 0)
1825 status_dots_requested = 0;
1826 verbose = 1;
1827 inname = argv[2];
1828 strict++;
1829 relaxed = 0;
1832 else if (strcmp(argv[1], "--relaxed") == 0)
1834 status_dots_requested = 0;
1835 verbose = 1;
1836 inname = argv[2];
1837 strict = 0;
1838 relaxed++;
1841 else
1843 inname = argv[1];
1844 status_dots_requested = 0;
1848 if (multiple == 0 && argc == 3 + verbose)
1849 outname = argv[2 + verbose];
1851 if ((multiple == 0 && argc > 3 + verbose) ||
1852 (multiple != 0 && argc < 2))
1854 fprintf(STDERR,
1855 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1856 argv[0], argv[0]);
1857 fprintf(STDERR,
1858 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1859 fprintf(STDERR,
1860 " with -m %s is used as a temporary file\n", outname);
1861 exit(1);
1864 if (multiple != 0)
1866 int i;
1867 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1868 int allocation_now = current_allocation;
1869 #endif
1870 for (i=2; i<argc; ++i)
1872 int kerror;
1873 fprintf(STDERR, "\n Testing %s:", argv[i]);
1874 #if PNG_DEBUG > 0
1875 fprintf(STDERR, "\n");
1876 #endif
1877 kerror = test_one_file(argv[i], outname);
1878 if (kerror == 0)
1880 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1881 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1882 (unsigned long)zero_samples);
1883 #else
1884 fprintf(STDERR, " PASS\n");
1885 #endif
1886 #ifdef PNG_TIME_RFC1123_SUPPORTED
1887 if (tIME_chunk_present != 0)
1888 fprintf(STDERR, " tIME = %s\n", tIME_string);
1890 tIME_chunk_present = 0;
1891 #endif /* TIME_RFC1123 */
1894 else
1896 fprintf(STDERR, " FAIL\n");
1897 ierror += kerror;
1899 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1900 if (allocation_now != current_allocation)
1901 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1902 current_allocation - allocation_now);
1904 if (current_allocation != 0)
1906 memory_infop pinfo = pinformation;
1908 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1909 current_allocation);
1911 while (pinfo != NULL)
1913 fprintf(STDERR, " %lu bytes at %p\n",
1914 (unsigned long)pinfo->size,
1915 pinfo->pointer);
1916 pinfo = pinfo->next;
1919 #endif
1921 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1922 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1923 current_allocation);
1924 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1925 maximum_allocation);
1926 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1927 total_allocation);
1928 fprintf(STDERR, " Number of allocations: %10d\n",
1929 num_allocations);
1930 #endif
1933 else
1935 int i;
1936 for (i = 0; i<3; ++i)
1938 int kerror;
1939 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1940 int allocation_now = current_allocation;
1941 #endif
1942 if (i == 1)
1943 status_dots_requested = 1;
1945 else if (verbose == 0)
1946 status_dots_requested = 0;
1948 if (i == 0 || verbose == 1 || ierror != 0)
1950 fprintf(STDERR, "\n Testing %s:", inname);
1951 #if PNG_DEBUG > 0
1952 fprintf(STDERR, "\n");
1953 #endif
1956 kerror = test_one_file(inname, outname);
1958 if (kerror == 0)
1960 if (verbose == 1 || i == 2)
1962 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1963 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1964 (unsigned long)zero_samples);
1965 #else
1966 fprintf(STDERR, " PASS\n");
1967 #endif
1968 #ifdef PNG_TIME_RFC1123_SUPPORTED
1969 if (tIME_chunk_present != 0)
1970 fprintf(STDERR, " tIME = %s\n", tIME_string);
1971 #endif /* TIME_RFC1123 */
1975 else
1977 if (verbose == 0 && i != 2)
1979 fprintf(STDERR, "\n Testing %s:", inname);
1980 #if PNG_DEBUG > 0
1981 fprintf(STDERR, "\n");
1982 #endif
1985 fprintf(STDERR, " FAIL\n");
1986 ierror += kerror;
1988 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1989 if (allocation_now != current_allocation)
1990 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1991 current_allocation - allocation_now);
1993 if (current_allocation != 0)
1995 memory_infop pinfo = pinformation;
1997 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1998 current_allocation);
2000 while (pinfo != NULL)
2002 fprintf(STDERR, " %lu bytes at %p\n",
2003 (unsigned long)pinfo->size, pinfo->pointer);
2004 pinfo = pinfo->next;
2007 #endif
2009 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2010 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2011 current_allocation);
2012 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2013 maximum_allocation);
2014 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2015 total_allocation);
2016 fprintf(STDERR, " Number of allocations: %10d\n",
2017 num_allocations);
2018 #endif
2021 #ifdef PNGTEST_TIMING
2022 t_stop = (float)clock();
2023 t_misc += (t_stop - t_start);
2024 t_start = t_stop;
2025 fprintf(STDERR, " CPU time used = %.3f seconds",
2026 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
2027 fprintf(STDERR, " (decoding %.3f,\n",
2028 t_decode/(float)CLOCKS_PER_SEC);
2029 fprintf(STDERR, " encoding %.3f ,",
2030 t_encode/(float)CLOCKS_PER_SEC);
2031 fprintf(STDERR, " other %.3f seconds)\n\n",
2032 t_misc/(float)CLOCKS_PER_SEC);
2033 #endif
2035 if (ierror == 0)
2036 fprintf(STDERR, " libpng passes test\n");
2038 else
2039 fprintf(STDERR, " libpng FAILS test\n");
2041 dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2042 fprintf(STDERR, " Default limits:\n");
2043 fprintf(STDERR, " width_max = %lu\n",
2044 (unsigned long) png_get_user_width_max(dummy_ptr));
2045 fprintf(STDERR, " height_max = %lu\n",
2046 (unsigned long) png_get_user_height_max(dummy_ptr));
2047 if (png_get_chunk_cache_max(dummy_ptr) == 0)
2048 fprintf(STDERR, " cache_max = unlimited\n");
2049 else
2050 fprintf(STDERR, " cache_max = %lu\n",
2051 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
2052 if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2053 fprintf(STDERR, " malloc_max = unlimited\n");
2054 else
2055 fprintf(STDERR, " malloc_max = %lu\n",
2056 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2057 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2059 return (int)(ierror != 0);
2061 #else
2063 main(void)
2065 fprintf(STDERR,
2066 " test ignored because libpng was not built with read support\n");
2067 /* And skip this test */
2068 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
2070 #endif
2072 /* Generate a compiler error if there is an old png.h in the search path. */
2073 typedef png_libpng_version_1_6_21 Your_png_h_is_not_version_1_6_21;