Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / libpng / pngtest.c
blobbb0a0b534ddc5ece2f08171d0b73a2c6bacdaaef
2 /* pngtest.c - a simple test program to test libpng
4 * Last changed in libpng 1.6.19 [November 12, 2015]
5 * Copyright (c) 1998-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_sPLT_SUPPORTED &&\
64 defined PNG_READ_sRGB_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
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 /* Turn on CPU timing
106 #define PNGTEST_TIMING
109 #ifndef PNG_FLOATING_POINT_SUPPORTED
110 #undef PNGTEST_TIMING
111 #endif
113 #ifdef PNGTEST_TIMING
114 static float t_start, t_stop, t_decode, t_encode, t_misc;
115 #include <time.h>
116 #endif
118 #ifdef PNG_TIME_RFC1123_SUPPORTED
119 #define PNG_tIME_STRING_LENGTH 29
120 static int tIME_chunk_present = 0;
121 static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
122 #endif
124 static int verbose = 0;
125 static int strict = 0;
126 static int relaxed = 0;
127 static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
128 static int error_count = 0; /* count calls to png_error */
129 static int warning_count = 0; /* count calls to png_warning */
131 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
132 #ifndef png_jmpbuf
133 # define png_jmpbuf(png_ptr) png_ptr->jmpbuf
134 #endif
136 /* Defines for unknown chunk handling if required. */
137 #ifndef PNG_HANDLE_CHUNK_ALWAYS
138 # define PNG_HANDLE_CHUNK_ALWAYS 3
139 #endif
140 #ifndef PNG_HANDLE_CHUNK_IF_SAFE
141 # define PNG_HANDLE_CHUNK_IF_SAFE 2
142 #endif
144 /* Utility to save typing/errors, the argument must be a name */
145 #define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
147 /* Example of using row callbacks to make a simple progress meter */
148 static int status_pass = 1;
149 static int status_dots_requested = 0;
150 static int status_dots = 1;
152 static void PNGCBAPI
153 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
155 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
156 return;
158 if (status_pass != pass)
160 fprintf(stdout, "\n Pass %d: ", pass);
161 status_pass = pass;
162 status_dots = 31;
165 status_dots--;
167 if (status_dots == 0)
169 fprintf(stdout, "\n ");
170 status_dots=30;
173 fprintf(stdout, "r");
176 #ifdef PNG_WRITE_SUPPORTED
177 static void PNGCBAPI
178 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
180 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
181 return;
183 fprintf(stdout, "w");
185 #endif
188 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
189 /* Example of using user transform callback (we don't transform anything,
190 * but merely examine the row filters. We set this to 256 rather than
191 * 5 in case illegal filter values are present.)
193 static png_uint_32 filters_used[256];
194 static void PNGCBAPI
195 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
197 if (png_ptr != NULL && row_info != NULL)
198 ++filters_used[*(data - 1)];
200 #endif
202 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
203 /* Example of using user transform callback (we don't transform anything,
204 * but merely count the zero samples)
207 static png_uint_32 zero_samples;
209 static void PNGCBAPI
210 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
212 png_bytep dp = data;
213 if (png_ptr == NULL)
214 return;
216 /* Contents of row_info:
217 * png_uint_32 width width of row
218 * png_uint_32 rowbytes number of bytes in row
219 * png_byte color_type color type of pixels
220 * png_byte bit_depth bit depth of samples
221 * png_byte channels number of channels (1-4)
222 * png_byte pixel_depth bits per pixel (depth*channels)
225 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
227 if (row_info->color_type == 0 || row_info->color_type == 3)
229 int pos = 0;
230 png_uint_32 n, nstop;
232 for (n = 0, nstop=row_info->width; n<nstop; n++)
234 if (row_info->bit_depth == 1)
236 if (((*dp << pos++ ) & 0x80) == 0)
237 zero_samples++;
239 if (pos == 8)
241 pos = 0;
242 dp++;
246 if (row_info->bit_depth == 2)
248 if (((*dp << (pos+=2)) & 0xc0) == 0)
249 zero_samples++;
251 if (pos == 8)
253 pos = 0;
254 dp++;
258 if (row_info->bit_depth == 4)
260 if (((*dp << (pos+=4)) & 0xf0) == 0)
261 zero_samples++;
263 if (pos == 8)
265 pos = 0;
266 dp++;
270 if (row_info->bit_depth == 8)
271 if (*dp++ == 0)
272 zero_samples++;
274 if (row_info->bit_depth == 16)
276 if ((*dp | *(dp+1)) == 0)
277 zero_samples++;
278 dp+=2;
282 else /* Other color types */
284 png_uint_32 n, nstop;
285 int channel;
286 int color_channels = row_info->channels;
287 if (row_info->color_type > 3)
288 color_channels--;
290 for (n = 0, nstop=row_info->width; n<nstop; n++)
292 for (channel = 0; channel < color_channels; channel++)
294 if (row_info->bit_depth == 8)
295 if (*dp++ == 0)
296 zero_samples++;
298 if (row_info->bit_depth == 16)
300 if ((*dp | *(dp+1)) == 0)
301 zero_samples++;
303 dp+=2;
306 if (row_info->color_type > 3)
308 dp++;
309 if (row_info->bit_depth == 16)
310 dp++;
315 #endif /* WRITE_USER_TRANSFORM */
317 #ifndef PNG_STDIO_SUPPORTED
318 /* START of code to validate stdio-free compilation */
319 /* These copies of the default read/write functions come from pngrio.c and
320 * pngwio.c. They allow "don't include stdio" testing of the library.
321 * This is the function that does the actual reading of data. If you are
322 * not reading from a standard C stream, you should create a replacement
323 * read_data function and use it at run time with png_set_read_fn(), rather
324 * than changing the library.
327 #ifdef PNG_IO_STATE_SUPPORTED
328 void
329 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
330 png_uint_32 io_op);
331 void
332 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
333 png_uint_32 io_op)
335 png_uint_32 io_state = png_get_io_state(png_ptr);
336 int err = 0;
338 /* Check if the current operation (reading / writing) is as expected. */
339 if ((io_state & PNG_IO_MASK_OP) != io_op)
340 png_error(png_ptr, "Incorrect operation in I/O state");
342 /* Check if the buffer size specific to the current location
343 * (file signature / header / data / crc) is as expected.
345 switch (io_state & PNG_IO_MASK_LOC)
347 case PNG_IO_SIGNATURE:
348 if (data_length > 8)
349 err = 1;
350 break;
351 case PNG_IO_CHUNK_HDR:
352 if (data_length != 8)
353 err = 1;
354 break;
355 case PNG_IO_CHUNK_DATA:
356 break; /* no restrictions here */
357 case PNG_IO_CHUNK_CRC:
358 if (data_length != 4)
359 err = 1;
360 break;
361 default:
362 err = 1; /* uninitialized */
364 if (err != 0)
365 png_error(png_ptr, "Bad I/O state or buffer size");
367 #endif
369 static void PNGCBAPI
370 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
372 png_size_t check = 0;
373 png_voidp io_ptr;
375 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
376 * instead of an int, which is what fread() actually returns.
378 io_ptr = png_get_io_ptr(png_ptr);
379 if (io_ptr != NULL)
381 check = fread(data, 1, length, (png_FILE_p)io_ptr);
384 if (check != length)
386 png_error(png_ptr, "Read Error");
389 #ifdef PNG_IO_STATE_SUPPORTED
390 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
391 #endif
394 #ifdef PNG_WRITE_FLUSH_SUPPORTED
395 static void PNGCBAPI
396 pngtest_flush(png_structp png_ptr)
398 /* Do nothing; fflush() is said to be just a waste of energy. */
399 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
401 #endif
403 /* This is the function that does the actual writing of data. If you are
404 * not writing to a standard C stream, you should create a replacement
405 * write_data function and use it at run time with png_set_write_fn(), rather
406 * than changing the library.
408 static void PNGCBAPI
409 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
411 png_size_t check;
413 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
415 if (check != length)
417 png_error(png_ptr, "Write Error");
420 #ifdef PNG_IO_STATE_SUPPORTED
421 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
422 #endif
424 #endif /* !STDIO */
426 /* This function is called when there is a warning, but the library thinks
427 * it can continue anyway. Replacement functions don't have to do anything
428 * here if you don't want to. In the default configuration, png_ptr is
429 * not used, but it is passed in case it may be useful.
431 typedef struct
433 PNG_CONST char *file_name;
434 } pngtest_error_parameters;
436 static void PNGCBAPI
437 pngtest_warning(png_structp png_ptr, png_const_charp message)
439 PNG_CONST char *name = "UNKNOWN (ERROR!)";
440 pngtest_error_parameters *test =
441 (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
443 ++warning_count;
445 if (test != NULL && test->file_name != NULL)
446 name = test->file_name;
448 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
451 /* This is the default error handling function. Note that replacements for
452 * this function MUST NOT RETURN, or the program will likely crash. This
453 * function is used by default, or if the program supplies NULL for the
454 * error function pointer in png_set_error_fn().
456 static void PNGCBAPI
457 pngtest_error(png_structp png_ptr, png_const_charp message)
459 ++error_count;
461 pngtest_warning(png_ptr, message);
462 /* We can return because png_error calls the default handler, which is
463 * actually OK in this case.
467 /* END of code to validate stdio-free compilation */
469 /* START of code to validate memory allocation and deallocation */
470 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
472 /* Allocate memory. For reasonable files, size should never exceed
473 * 64K. However, zlib may allocate more than 64K if you don't tell
474 * it not to. See zconf.h and png.h for more information. zlib does
475 * need to allocate exactly 64K, so whatever you call here must
476 * have the ability to do that.
478 * This piece of code can be compiled to validate max 64K allocations
479 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
481 typedef struct memory_information
483 png_alloc_size_t size;
484 png_voidp pointer;
485 struct memory_information *next;
486 } memory_information;
487 typedef memory_information *memory_infop;
489 static memory_infop pinformation = NULL;
490 static int current_allocation = 0;
491 static int maximum_allocation = 0;
492 static int total_allocation = 0;
493 static int num_allocations = 0;
495 png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
496 png_alloc_size_t size));
497 void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
499 png_voidp
500 PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
503 /* png_malloc has already tested for NULL; png_create_struct calls
504 * png_debug_malloc directly, with png_ptr == NULL which is OK
507 if (size == 0)
508 return (NULL);
510 /* This calls the library allocator twice, once to get the requested
511 buffer and once to get a new free list entry. */
513 /* Disable malloc_fn and free_fn */
514 memory_infop pinfo;
515 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
516 pinfo = (memory_infop)png_malloc(png_ptr,
517 (sizeof *pinfo));
518 pinfo->size = size;
519 current_allocation += size;
520 total_allocation += size;
521 num_allocations ++;
523 if (current_allocation > maximum_allocation)
524 maximum_allocation = current_allocation;
526 pinfo->pointer = png_malloc(png_ptr, size);
527 /* Restore malloc_fn and free_fn */
529 png_set_mem_fn(png_ptr,
530 NULL, png_debug_malloc, png_debug_free);
532 if (size != 0 && pinfo->pointer == NULL)
534 current_allocation -= size;
535 total_allocation -= size;
536 png_error(png_ptr,
537 "out of memory in pngtest->png_debug_malloc");
540 pinfo->next = pinformation;
541 pinformation = pinfo;
542 /* Make sure the caller isn't assuming zeroed memory. */
543 memset(pinfo->pointer, 0xdd, pinfo->size);
545 if (verbose != 0)
546 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
547 pinfo->pointer);
549 return (png_voidp)(pinfo->pointer);
553 /* Free a pointer. It is removed from the list at the same time. */
554 void PNGCBAPI
555 png_debug_free(png_structp png_ptr, png_voidp ptr)
557 if (png_ptr == NULL)
558 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
560 if (ptr == 0)
562 #if 0 /* This happens all the time. */
563 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
564 #endif
565 return;
568 /* Unlink the element from the list. */
569 if (pinformation != NULL)
571 memory_infop *ppinfo = &pinformation;
573 for (;;)
575 memory_infop pinfo = *ppinfo;
577 if (pinfo->pointer == ptr)
579 *ppinfo = pinfo->next;
580 current_allocation -= pinfo->size;
581 if (current_allocation < 0)
582 fprintf(STDERR, "Duplicate free of memory\n");
583 /* We must free the list element too, but first kill
584 the memory that is to be freed. */
585 memset(ptr, 0x55, pinfo->size);
586 free(pinfo);
587 pinfo = NULL;
588 break;
591 if (pinfo->next == NULL)
593 fprintf(STDERR, "Pointer %p not found\n", ptr);
594 break;
597 ppinfo = &pinfo->next;
601 /* Finally free the data. */
602 if (verbose != 0)
603 printf("Freeing %p\n", ptr);
605 if (ptr != NULL)
606 free(ptr);
607 ptr = NULL;
609 #endif /* USER_MEM && DEBUG */
610 /* END of code to test memory allocation/deallocation */
613 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
614 /* Demonstration of user chunk support of the sTER and vpAg chunks */
616 /* (sTER is a public chunk not yet known by libpng. vpAg is a private
617 chunk used in ImageMagick to store "virtual page" size). */
619 static struct user_chunk_data
621 png_const_infop info_ptr;
622 png_uint_32 vpAg_width, vpAg_height;
623 png_byte vpAg_units;
624 png_byte sTER_mode;
625 int location[2];
627 user_chunk_data;
629 /* Used for location and order; zero means nothing. */
630 #define have_sTER 0x01
631 #define have_vpAg 0x02
632 #define before_PLTE 0x10
633 #define before_IDAT 0x20
634 #define after_IDAT 0x40
636 static void
637 init_callback_info(png_const_infop info_ptr)
639 MEMZERO(user_chunk_data);
640 user_chunk_data.info_ptr = info_ptr;
643 static int
644 set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
646 int location;
648 if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
649 return 0; /* already have one of these */
651 /* Find where we are (the code below zeroes info_ptr to indicate that the
652 * chunks before the first IDAT have been read.)
654 if (data->info_ptr == NULL) /* after IDAT */
655 location = what | after_IDAT;
657 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
658 location = what | before_IDAT;
660 else
661 location = what | before_PLTE;
663 if (data->location[0] == 0)
664 data->location[0] = location;
666 else
667 data->location[1] = location;
669 return 1; /* handled */
672 static int PNGCBAPI
673 read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
675 struct user_chunk_data *my_user_chunk_data =
676 (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
678 if (my_user_chunk_data == NULL)
679 png_error(png_ptr, "lost user chunk pointer");
681 /* Return one of the following:
682 * return (-n); chunk had an error
683 * return (0); did not recognize
684 * return (n); success
686 * The unknown chunk structure contains the chunk data:
687 * png_byte name[5];
688 * png_byte *data;
689 * png_size_t size;
691 * Note that libpng has already taken care of the CRC handling.
694 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
695 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
697 /* Found sTER chunk */
698 if (chunk->size != 1)
699 return (-1); /* Error return */
701 if (chunk->data[0] != 0 && chunk->data[0] != 1)
702 return (-1); /* Invalid mode */
704 if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
706 my_user_chunk_data->sTER_mode=chunk->data[0];
707 return (1);
710 else
711 return (0); /* duplicate sTER - give it to libpng */
714 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
715 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
716 return (0); /* Did not recognize */
718 /* Found ImageMagick vpAg chunk */
720 if (chunk->size != 9)
721 return (-1); /* Error return */
723 if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
724 return (0); /* duplicate vpAg */
726 my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
727 my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
728 my_user_chunk_data->vpAg_units = chunk->data[8];
730 return (1);
733 #ifdef PNG_WRITE_SUPPORTED
734 static void
735 write_sTER_chunk(png_structp write_ptr)
737 png_byte sTER[5] = {115, 84, 69, 82, '\0'};
739 if (verbose != 0)
740 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
742 png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
745 static void
746 write_vpAg_chunk(png_structp write_ptr)
748 png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
750 png_byte vpag_chunk_data[9];
752 if (verbose != 0)
753 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
754 (unsigned long)user_chunk_data.vpAg_width,
755 (unsigned long)user_chunk_data.vpAg_height,
756 user_chunk_data.vpAg_units);
758 png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
759 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
760 vpag_chunk_data[8] = user_chunk_data.vpAg_units;
761 png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
764 static void
765 write_chunks(png_structp write_ptr, int location)
767 int i;
769 /* Notice that this preserves the original chunk order, however chunks
770 * intercepted by the callback will be written *after* chunks passed to
771 * libpng. This will actually reverse a pair of sTER chunks or a pair of
772 * vpAg chunks, resulting in an error later. This is not worth worrying
773 * about - the chunks should not be duplicated!
775 for (i=0; i<2; ++i)
777 if (user_chunk_data.location[i] == (location | have_sTER))
778 write_sTER_chunk(write_ptr);
780 else if (user_chunk_data.location[i] == (location | have_vpAg))
781 write_vpAg_chunk(write_ptr);
784 #endif /* WRITE */
785 #else /* !READ_USER_CHUNKS */
786 # define write_chunks(pp,loc) ((void)0)
787 #endif
788 /* END of code to demonstrate user chunk support */
790 /* START of code to check that libpng has the required text support; this only
791 * checks for the write support because if read support is missing the chunk
792 * will simply not be reported back to pngtest.
794 #ifdef PNG_TEXT_SUPPORTED
795 static void
796 pngtest_check_text_support(png_const_structp png_ptr, png_textp text_ptr,
797 int num_text)
799 while (num_text > 0)
801 switch (text_ptr[--num_text].compression)
803 case PNG_TEXT_COMPRESSION_NONE:
804 break;
806 case PNG_TEXT_COMPRESSION_zTXt:
807 # ifndef PNG_WRITE_zTXt_SUPPORTED
808 ++unsupported_chunks;
809 # endif
810 break;
812 case PNG_ITXT_COMPRESSION_NONE:
813 case PNG_ITXT_COMPRESSION_zTXt:
814 # ifndef PNG_WRITE_iTXt_SUPPORTED
815 ++unsupported_chunks;
816 # endif
817 break;
819 default:
820 /* This is an error */
821 png_error(png_ptr, "invalid text chunk compression field");
822 break;
826 #endif
827 /* END of code to check that libpng has the required text support */
829 /* Test one file */
830 static int
831 test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
833 static png_FILE_p fpin;
834 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
835 pngtest_error_parameters error_parameters;
836 png_structp read_ptr;
837 png_infop read_info_ptr, end_info_ptr;
838 #ifdef PNG_WRITE_SUPPORTED
839 png_structp write_ptr;
840 png_infop write_info_ptr;
841 png_infop write_end_info_ptr;
842 int interlace_preserved = 1;
843 #else
844 png_structp write_ptr = NULL;
845 png_infop write_info_ptr = NULL;
846 png_infop write_end_info_ptr = NULL;
847 #endif
848 png_bytep row_buf;
849 png_uint_32 y;
850 png_uint_32 width, height;
851 int num_pass = 1, pass;
852 int bit_depth, color_type;
854 row_buf = NULL;
855 error_parameters.file_name = inname;
857 if ((fpin = fopen(inname, "rb")) == NULL)
859 fprintf(STDERR, "Could not find input file %s\n", inname);
860 return (1);
863 if ((fpout = fopen(outname, "wb")) == NULL)
865 fprintf(STDERR, "Could not open output file %s\n", outname);
866 FCLOSE(fpin);
867 return (1);
870 pngtest_debug("Allocating read and write structures");
871 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
872 read_ptr =
873 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
874 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
875 #else
876 read_ptr =
877 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
878 #endif
879 png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
880 pngtest_warning);
882 #ifdef PNG_WRITE_SUPPORTED
883 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
884 write_ptr =
885 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
886 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
887 #else
888 write_ptr =
889 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
890 #endif
891 png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
892 pngtest_warning);
893 #endif
894 pngtest_debug("Allocating read_info, write_info and end_info structures");
895 read_info_ptr = png_create_info_struct(read_ptr);
896 end_info_ptr = png_create_info_struct(read_ptr);
897 #ifdef PNG_WRITE_SUPPORTED
898 write_info_ptr = png_create_info_struct(write_ptr);
899 write_end_info_ptr = png_create_info_struct(write_ptr);
900 #endif
902 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
903 init_callback_info(read_info_ptr);
904 png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
905 read_user_chunk_callback);
906 #endif
908 #ifdef PNG_SETJMP_SUPPORTED
909 pngtest_debug("Setting jmpbuf for read struct");
910 if (setjmp(png_jmpbuf(read_ptr)))
912 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
913 png_free(read_ptr, row_buf);
914 row_buf = NULL;
915 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
916 #ifdef PNG_WRITE_SUPPORTED
917 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
918 png_destroy_write_struct(&write_ptr, &write_info_ptr);
919 #endif
920 FCLOSE(fpin);
921 FCLOSE(fpout);
922 return (1);
925 #ifdef PNG_WRITE_SUPPORTED
926 pngtest_debug("Setting jmpbuf for write struct");
928 if (setjmp(png_jmpbuf(write_ptr)))
930 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
931 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
932 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
933 #ifdef PNG_WRITE_SUPPORTED
934 png_destroy_write_struct(&write_ptr, &write_info_ptr);
935 #endif
936 FCLOSE(fpin);
937 FCLOSE(fpout);
938 return (1);
940 #endif
941 #endif
943 if (strict != 0)
945 /* Treat png_benign_error() as errors on read */
946 png_set_benign_errors(read_ptr, 0);
948 #ifdef PNG_WRITE_SUPPORTED
949 /* Treat them as errors on write */
950 png_set_benign_errors(write_ptr, 0);
951 #endif
953 /* if strict is not set, then app warnings and errors are treated as
954 * warnings in release builds, but not in unstable builds; this can be
955 * changed with '--relaxed'.
959 else if (relaxed != 0)
961 /* Allow application (pngtest) errors and warnings to pass */
962 png_set_benign_errors(read_ptr, 1);
964 #ifdef PNG_WRITE_SUPPORTED
965 png_set_benign_errors(write_ptr, 1);
966 #endif
969 pngtest_debug("Initializing input and output streams");
970 #ifdef PNG_STDIO_SUPPORTED
971 png_init_io(read_ptr, fpin);
972 # ifdef PNG_WRITE_SUPPORTED
973 png_init_io(write_ptr, fpout);
974 # endif
975 #else
976 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
977 # ifdef PNG_WRITE_SUPPORTED
978 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
979 # ifdef PNG_WRITE_FLUSH_SUPPORTED
980 pngtest_flush);
981 # else
982 NULL);
983 # endif
984 # endif
985 #endif
987 if (status_dots_requested == 1)
989 #ifdef PNG_WRITE_SUPPORTED
990 png_set_write_status_fn(write_ptr, write_row_callback);
991 #endif
992 png_set_read_status_fn(read_ptr, read_row_callback);
995 else
997 #ifdef PNG_WRITE_SUPPORTED
998 png_set_write_status_fn(write_ptr, NULL);
999 #endif
1000 png_set_read_status_fn(read_ptr, NULL);
1003 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1005 int i;
1007 for (i = 0; i<256; i++)
1008 filters_used[i] = 0;
1010 png_set_read_user_transform_fn(read_ptr, count_filters);
1012 #endif
1013 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1014 zero_samples = 0;
1015 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1016 #endif
1018 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1019 /* Preserve all the unknown chunks, if possible. If this is disabled then,
1020 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1021 * libpng to *save* the unknown chunks on read (because we can't switch the
1022 * save option on!)
1024 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1025 * unknown chunks and write will write them all.
1027 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1028 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1029 NULL, 0);
1030 #endif
1031 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1032 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1033 NULL, 0);
1034 #endif
1035 #endif
1037 pngtest_debug("Reading info struct");
1038 png_read_info(read_ptr, read_info_ptr);
1040 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1041 /* This is a bit of a hack; there is no obvious way in the callback function
1042 * to determine that the chunks before the first IDAT have been read, so
1043 * remove the info_ptr (which is only used to determine position relative to
1044 * PLTE) here to indicate that we are after the IDAT.
1046 user_chunk_data.info_ptr = NULL;
1047 #endif
1049 pngtest_debug("Transferring info struct");
1051 int interlace_type, compression_type, filter_type;
1053 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1054 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
1056 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
1057 color_type, interlace_type, compression_type, filter_type);
1058 #ifndef PNG_READ_INTERLACING_SUPPORTED
1059 /* num_pass will not be set below, set it here if the image is
1060 * interlaced: what happens is that write interlacing is *not* turned
1061 * on and the partial interlaced rows are written directly.
1063 switch (interlace_type)
1065 case PNG_INTERLACE_NONE:
1066 num_pass = 1;
1067 break;
1069 case PNG_INTERLACE_ADAM7:
1070 num_pass = 7;
1071 break;
1073 default:
1074 png_error(read_ptr, "invalid interlace type");
1075 /*NOT REACHED*/
1077 #endif
1081 #ifdef PNG_FIXED_POINT_SUPPORTED
1082 #ifdef PNG_cHRM_SUPPORTED
1084 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1085 blue_y;
1087 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1088 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1090 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1091 red_y, green_x, green_y, blue_x, blue_y);
1094 #endif
1096 #ifdef PNG_gAMA_SUPPORTED
1098 png_fixed_point gamma;
1100 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
1101 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
1103 #endif
1104 #else /* Use floating point versions */
1105 #ifdef PNG_FLOATING_POINT_SUPPORTED
1106 #ifdef PNG_cHRM_SUPPORTED
1108 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1109 blue_y;
1111 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1112 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1114 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1115 red_y, green_x, green_y, blue_x, blue_y);
1118 #endif
1119 #ifdef PNG_gAMA_SUPPORTED
1121 double gamma;
1123 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
1124 png_set_gAMA(write_ptr, write_info_ptr, gamma);
1126 #endif
1127 #endif /* Floating point */
1128 #endif /* Fixed point */
1130 #ifdef PNG_iCCP_SUPPORTED
1132 png_charp name;
1133 png_bytep profile;
1134 png_uint_32 proflen;
1135 int compression_type;
1137 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1138 &profile, &proflen) != 0)
1140 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1141 profile, proflen);
1144 #endif
1146 #ifdef PNG_sRGB_SUPPORTED
1148 int intent;
1150 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
1151 png_set_sRGB(write_ptr, write_info_ptr, intent);
1153 #endif
1156 png_colorp palette;
1157 int num_palette;
1159 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
1160 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
1163 #ifdef PNG_bKGD_SUPPORTED
1165 png_color_16p background;
1167 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
1169 png_set_bKGD(write_ptr, write_info_ptr, background);
1172 #endif
1174 #ifdef PNG_hIST_SUPPORTED
1176 png_uint_16p hist;
1178 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
1179 png_set_hIST(write_ptr, write_info_ptr, hist);
1181 #endif
1183 #ifdef PNG_oFFs_SUPPORTED
1185 png_int_32 offset_x, offset_y;
1186 int unit_type;
1188 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1189 &unit_type) != 0)
1191 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1194 #endif
1196 #ifdef PNG_pCAL_SUPPORTED
1198 png_charp purpose, units;
1199 png_charpp params;
1200 png_int_32 X0, X1;
1201 int type, nparams;
1203 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1204 &nparams, &units, &params) != 0)
1206 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1207 nparams, units, params);
1210 #endif
1212 #ifdef PNG_pHYs_SUPPORTED
1214 png_uint_32 res_x, res_y;
1215 int unit_type;
1217 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1218 &unit_type) != 0)
1219 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
1221 #endif
1223 #ifdef PNG_sBIT_SUPPORTED
1225 png_color_8p sig_bit;
1227 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
1228 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
1230 #endif
1232 #ifdef PNG_sCAL_SUPPORTED
1233 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1234 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
1236 int unit;
1237 double scal_width, scal_height;
1239 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1240 &scal_height) != 0)
1242 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1245 #else
1246 #ifdef PNG_FIXED_POINT_SUPPORTED
1248 int unit;
1249 png_charp scal_width, scal_height;
1251 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1252 &scal_height) != 0)
1254 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1255 scal_height);
1258 #endif /* FIXED_POINT */
1259 #endif /* FLOATING_POINT */
1260 #endif /* sCAL */
1262 #ifdef PNG_sPLT_SUPPORTED
1264 png_sPLT_tp entries;
1266 int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1267 if (num_entries)
1269 png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1272 #endif /* sPLT */
1274 #ifdef PNG_TEXT_SUPPORTED
1276 png_textp text_ptr;
1277 int num_text;
1279 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1281 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1283 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1285 if (verbose != 0)
1287 int i;
1289 printf("\n");
1290 for (i=0; i<num_text; i++)
1292 printf(" Text compression[%d]=%d\n",
1293 i, text_ptr[i].compression);
1297 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1300 #endif /* TEXT */
1302 #ifdef PNG_tIME_SUPPORTED
1304 png_timep mod_time;
1306 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
1308 png_set_tIME(write_ptr, write_info_ptr, mod_time);
1309 #ifdef PNG_TIME_RFC1123_SUPPORTED
1310 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1311 tIME_string[(sizeof tIME_string) - 1] = '\0';
1313 else
1315 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1316 tIME_string[(sizeof tIME_string) - 1] = '\0';
1319 tIME_chunk_present++;
1320 #endif /* TIME_RFC1123 */
1323 #endif /* tIME */
1325 #ifdef PNG_tRNS_SUPPORTED
1327 png_bytep trans_alpha;
1328 int num_trans;
1329 png_color_16p trans_color;
1331 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1332 &trans_color) != 0)
1334 int sample_max = (1 << bit_depth);
1335 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1336 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1337 (int)trans_color->gray > sample_max) ||
1338 (color_type == PNG_COLOR_TYPE_RGB &&
1339 ((int)trans_color->red > sample_max ||
1340 (int)trans_color->green > sample_max ||
1341 (int)trans_color->blue > sample_max))))
1342 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1343 trans_color);
1346 #endif /* tRNS */
1348 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1350 png_unknown_chunkp unknowns;
1351 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1352 &unknowns);
1354 if (num_unknowns != 0)
1356 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1357 num_unknowns);
1358 #if PNG_LIBPNG_VER < 10600
1359 /* Copy the locations from the read_info_ptr. The automatically
1360 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1361 * because they are reset from the write pointer (removed in 1.6.0).
1364 int i;
1365 for (i = 0; i < num_unknowns; i++)
1366 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1367 unknowns[i].location);
1369 #endif
1372 #endif
1374 #ifdef PNG_WRITE_SUPPORTED
1375 pngtest_debug("Writing info struct");
1377 /* Write the info in two steps so that if we write the 'unknown' chunks here
1378 * they go to the correct place.
1380 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1382 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1384 png_write_info(write_ptr, write_info_ptr);
1386 write_chunks(write_ptr, before_IDAT); /* after PLTE */
1387 #endif
1389 #ifdef SINGLE_ROWBUF_ALLOC
1390 pngtest_debug("Allocating row buffer...");
1391 row_buf = (png_bytep)png_malloc(read_ptr,
1392 png_get_rowbytes(read_ptr, read_info_ptr));
1394 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1395 #endif /* SINGLE_ROWBUF_ALLOC */
1396 pngtest_debug("Writing row data");
1398 #ifdef PNG_READ_INTERLACING_SUPPORTED
1399 num_pass = png_set_interlace_handling(read_ptr);
1400 if (png_set_interlace_handling(write_ptr) != num_pass)
1401 png_error(write_ptr, "png_set_interlace_handling: inconsistent num_pass");
1402 #endif
1404 #ifdef PNGTEST_TIMING
1405 t_stop = (float)clock();
1406 t_misc += (t_stop - t_start);
1407 t_start = t_stop;
1408 #endif
1409 for (pass = 0; pass < num_pass; pass++)
1411 pngtest_debug1("Writing row data for pass %d", pass);
1412 for (y = 0; y < height; y++)
1414 #ifndef SINGLE_ROWBUF_ALLOC
1415 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1417 row_buf = (png_bytep)png_malloc(read_ptr,
1418 png_get_rowbytes(read_ptr, read_info_ptr));
1420 pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1421 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
1423 #endif /* !SINGLE_ROWBUF_ALLOC */
1424 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1426 #ifdef PNG_WRITE_SUPPORTED
1427 #ifdef PNGTEST_TIMING
1428 t_stop = (float)clock();
1429 t_decode += (t_stop - t_start);
1430 t_start = t_stop;
1431 #endif
1432 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1433 #ifdef PNGTEST_TIMING
1434 t_stop = (float)clock();
1435 t_encode += (t_stop - t_start);
1436 t_start = t_stop;
1437 #endif
1438 #endif /* WRITE */
1440 #ifndef SINGLE_ROWBUF_ALLOC
1441 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1442 png_free(read_ptr, row_buf);
1443 row_buf = NULL;
1444 #endif /* !SINGLE_ROWBUF_ALLOC */
1448 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1449 # ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1450 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1451 # endif
1452 # ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1453 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1454 # endif
1455 #endif
1457 pngtest_debug("Reading and writing end_info data");
1459 png_read_end(read_ptr, end_info_ptr);
1460 #ifdef PNG_TEXT_SUPPORTED
1462 png_textp text_ptr;
1463 int num_text;
1465 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1467 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1469 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1471 if (verbose != 0)
1473 int i;
1475 printf("\n");
1476 for (i=0; i<num_text; i++)
1478 printf(" Text compression[%d]=%d\n",
1479 i, text_ptr[i].compression);
1483 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1486 #endif
1487 #ifdef PNG_tIME_SUPPORTED
1489 png_timep mod_time;
1491 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
1493 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1494 #ifdef PNG_TIME_RFC1123_SUPPORTED
1495 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1496 tIME_string[(sizeof tIME_string) - 1] = '\0';
1498 else
1500 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1501 tIME_string[(sizeof tIME_string)-1] = '\0';
1504 tIME_chunk_present++;
1505 #endif /* TIME_RFC1123 */
1508 #endif
1509 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1511 png_unknown_chunkp unknowns;
1512 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1513 &unknowns);
1515 if (num_unknowns != 0)
1517 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1518 num_unknowns);
1519 #if PNG_LIBPNG_VER < 10600
1520 /* Copy the locations from the read_info_ptr. The automatically
1521 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1522 * because they are reset from the write pointer (removed in 1.6.0).
1525 int i;
1526 for (i = 0; i < num_unknowns; i++)
1527 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1528 unknowns[i].location);
1530 #endif
1533 #endif
1535 #ifdef PNG_WRITE_SUPPORTED
1536 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1537 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1538 * This is here just to make pngtest replicate the results from libpng
1539 * versions prior to 1.5.4, and to test this new API.
1541 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1542 #endif
1544 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1545 * do it is to write them *before* calling png_write_end. When unknown
1546 * chunks are written by libpng, however, they are written just before IEND.
1547 * There seems to be no way round this, however vpAg/sTER are not expected
1548 * after IDAT.
1550 write_chunks(write_ptr, after_IDAT);
1552 png_write_end(write_ptr, write_end_info_ptr);
1553 #endif
1555 #ifdef PNG_EASY_ACCESS_SUPPORTED
1556 if (verbose != 0)
1558 png_uint_32 iwidth, iheight;
1559 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1560 iheight = png_get_image_height(write_ptr, write_info_ptr);
1561 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1562 (unsigned long)iwidth, (unsigned long)iheight);
1564 #endif
1566 pngtest_debug("Destroying data structs");
1567 #ifdef SINGLE_ROWBUF_ALLOC
1568 pngtest_debug("destroying row_buf for read_ptr");
1569 png_free(read_ptr, row_buf);
1570 row_buf = NULL;
1571 #endif /* SINGLE_ROWBUF_ALLOC */
1572 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
1573 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1574 #ifdef PNG_WRITE_SUPPORTED
1575 pngtest_debug("destroying write_end_info_ptr");
1576 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1577 pngtest_debug("destroying write_ptr, write_info_ptr");
1578 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1579 #endif
1580 pngtest_debug("Destruction complete.");
1582 FCLOSE(fpin);
1583 FCLOSE(fpout);
1585 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1586 * Unsupported chunks can result in warnings, in that case ignore the strict
1587 * setting, otherwise fail the test on warnings as well as errors.
1589 if (error_count > 0)
1591 /* We don't really expect to get here because of the setjmp handling
1592 * above, but this is safe.
1594 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
1595 inname, error_count, warning_count);
1597 if (strict != 0)
1598 return (1);
1601 # ifdef PNG_WRITE_SUPPORTED
1602 /* If there we no write support nothing was written! */
1603 else if (unsupported_chunks > 0)
1605 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1606 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1608 # endif
1610 else if (warning_count > 0)
1612 fprintf(STDERR, "\n %s: %d libpng warnings found",
1613 inname, warning_count);
1615 if (strict != 0)
1616 return (1);
1619 pngtest_debug("Opening files for comparison");
1620 if ((fpin = fopen(inname, "rb")) == NULL)
1622 fprintf(STDERR, "Could not find file %s\n", inname);
1623 return (1);
1626 if ((fpout = fopen(outname, "rb")) == NULL)
1628 fprintf(STDERR, "Could not find file %s\n", outname);
1629 FCLOSE(fpin);
1630 return (1);
1633 #ifdef PNG_WRITE_SUPPORTED /* else nothing was written */
1634 if (interlace_preserved != 0) /* else the files will be changed */
1636 for (;;)
1638 static int wrote_question = 0;
1639 png_size_t num_in, num_out;
1640 char inbuf[256], outbuf[256];
1642 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1643 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1645 if (num_in != num_out)
1647 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1648 inname, outname);
1650 if (wrote_question == 0 && unsupported_chunks == 0)
1652 fprintf(STDERR,
1653 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1654 inname, PNG_ZBUF_SIZE);
1655 fprintf(STDERR,
1656 "\n filtering heuristic (libpng default), compression");
1657 fprintf(STDERR,
1658 " level (zlib default),\n and zlib version (%s)?\n\n",
1659 ZLIB_VERSION);
1660 wrote_question = 1;
1663 FCLOSE(fpin);
1664 FCLOSE(fpout);
1666 if (strict != 0 && unsupported_chunks == 0)
1667 return (1);
1669 else
1670 return (0);
1673 if (num_in == 0)
1674 break;
1676 if (memcmp(inbuf, outbuf, num_in))
1678 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1679 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 /* NOTE: the unsupported_chunks escape is permitted here because
1698 * unsupported text chunk compression will result in the compression
1699 * mode being changed (to NONE) yet, in the test case, the result
1700 * can be exactly the same size!
1702 if (strict != 0 && unsupported_chunks == 0)
1703 return (1);
1705 else
1706 return (0);
1710 #endif /* WRITE */
1712 FCLOSE(fpin);
1713 FCLOSE(fpout);
1715 return (0);
1718 /* Input and output filenames */
1719 #ifdef RISCOS
1720 static PNG_CONST char *inname = "pngtest/png";
1721 static PNG_CONST char *outname = "pngout/png";
1722 #else
1723 static PNG_CONST char *inname = "pngtest.png";
1724 static PNG_CONST char *outname = "pngout.png";
1725 #endif
1728 main(int argc, char *argv[])
1730 int multiple = 0;
1731 int ierror = 0;
1733 png_structp dummy_ptr;
1735 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1736 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
1737 fprintf(STDERR, "%s", png_get_copyright(NULL));
1738 /* Show the version of libpng used in building the library */
1739 fprintf(STDERR, " library (%lu):%s",
1740 (unsigned long)png_access_version_number(),
1741 png_get_header_version(NULL));
1743 /* Show the version of libpng used in building the application */
1744 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1745 PNG_HEADER_VERSION_STRING);
1747 /* Do some consistency checking on the memory allocation settings, I'm
1748 * not sure this matters, but it is nice to know, the first of these
1749 * tests should be impossible because of the way the macros are set
1750 * in pngconf.h
1752 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1753 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1754 #endif
1755 /* I think the following can happen. */
1756 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1757 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1758 #endif
1760 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1762 fprintf(STDERR,
1763 "Warning: versions are different between png.h and png.c\n");
1764 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1765 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1766 ++ierror;
1769 if (argc > 1)
1771 if (strcmp(argv[1], "-m") == 0)
1773 multiple = 1;
1774 status_dots_requested = 0;
1777 else if (strcmp(argv[1], "-mv") == 0 ||
1778 strcmp(argv[1], "-vm") == 0 )
1780 multiple = 1;
1781 verbose = 1;
1782 status_dots_requested = 1;
1785 else if (strcmp(argv[1], "-v") == 0)
1787 verbose = 1;
1788 status_dots_requested = 1;
1789 inname = argv[2];
1792 else if (strcmp(argv[1], "--strict") == 0)
1794 status_dots_requested = 0;
1795 verbose = 1;
1796 inname = argv[2];
1797 strict++;
1798 relaxed = 0;
1801 else if (strcmp(argv[1], "--relaxed") == 0)
1803 status_dots_requested = 0;
1804 verbose = 1;
1805 inname = argv[2];
1806 strict = 0;
1807 relaxed++;
1810 else
1812 inname = argv[1];
1813 status_dots_requested = 0;
1817 if (multiple == 0 && argc == 3 + verbose)
1818 outname = argv[2 + verbose];
1820 if ((multiple == 0 && argc > 3 + verbose) ||
1821 (multiple != 0 && argc < 2))
1823 fprintf(STDERR,
1824 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1825 argv[0], argv[0]);
1826 fprintf(STDERR,
1827 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1828 fprintf(STDERR,
1829 " with -m %s is used as a temporary file\n", outname);
1830 exit(1);
1833 if (multiple != 0)
1835 int i;
1836 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1837 int allocation_now = current_allocation;
1838 #endif
1839 for (i=2; i<argc; ++i)
1841 int kerror;
1842 fprintf(STDERR, "\n Testing %s:", argv[i]);
1843 #if PNG_DEBUG > 0
1844 fprintf(STDERR, "\n");
1845 #endif
1846 kerror = test_one_file(argv[i], outname);
1847 if (kerror == 0)
1849 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1850 int k;
1851 #endif
1852 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1853 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1854 (unsigned long)zero_samples);
1855 #else
1856 fprintf(STDERR, " PASS\n");
1857 #endif
1858 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1859 for (k = 0; k<256; k++)
1860 if (filters_used[k] != 0)
1861 fprintf(STDERR, " Filter %d was used %lu times\n",
1862 k, (unsigned long)filters_used[k]);
1863 #endif
1864 #ifdef PNG_TIME_RFC1123_SUPPORTED
1865 if (tIME_chunk_present != 0)
1866 fprintf(STDERR, " tIME = %s\n", tIME_string);
1868 tIME_chunk_present = 0;
1869 #endif /* TIME_RFC1123 */
1872 else
1874 fprintf(STDERR, " FAIL\n");
1875 ierror += kerror;
1877 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1878 if (allocation_now != current_allocation)
1879 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1880 current_allocation - allocation_now);
1882 if (current_allocation != 0)
1884 memory_infop pinfo = pinformation;
1886 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1887 current_allocation);
1889 while (pinfo != NULL)
1891 fprintf(STDERR, " %lu bytes at %p\n",
1892 (unsigned long)pinfo->size,
1893 pinfo->pointer);
1894 pinfo = pinfo->next;
1897 #endif
1899 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1900 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1901 current_allocation);
1902 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1903 maximum_allocation);
1904 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1905 total_allocation);
1906 fprintf(STDERR, " Number of allocations: %10d\n",
1907 num_allocations);
1908 #endif
1911 else
1913 int i;
1914 for (i = 0; i<3; ++i)
1916 int kerror;
1917 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1918 int allocation_now = current_allocation;
1919 #endif
1920 if (i == 1)
1921 status_dots_requested = 1;
1923 else if (verbose == 0)
1924 status_dots_requested = 0;
1926 if (i == 0 || verbose == 1 || ierror != 0)
1928 fprintf(STDERR, "\n Testing %s:", inname);
1929 #if PNG_DEBUG > 0
1930 fprintf(STDERR, "\n");
1931 #endif
1934 kerror = test_one_file(inname, outname);
1936 if (kerror == 0)
1938 if (verbose == 1 || i == 2)
1940 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1941 int k;
1942 #endif
1943 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1944 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1945 (unsigned long)zero_samples);
1946 #else
1947 fprintf(STDERR, " PASS\n");
1948 #endif
1949 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1950 for (k = 0; k<256; k++)
1951 if (filters_used[k] != 0)
1952 fprintf(STDERR, " Filter %d was used %lu times\n",
1953 k, (unsigned long)filters_used[k]);
1954 #endif
1955 #ifdef PNG_TIME_RFC1123_SUPPORTED
1956 if (tIME_chunk_present != 0)
1957 fprintf(STDERR, " tIME = %s\n", tIME_string);
1958 #endif /* TIME_RFC1123 */
1962 else
1964 if (verbose == 0 && i != 2)
1966 fprintf(STDERR, "\n Testing %s:", inname);
1967 #if PNG_DEBUG > 0
1968 fprintf(STDERR, "\n");
1969 #endif
1972 fprintf(STDERR, " FAIL\n");
1973 ierror += kerror;
1975 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1976 if (allocation_now != current_allocation)
1977 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1978 current_allocation - allocation_now);
1980 if (current_allocation != 0)
1982 memory_infop pinfo = pinformation;
1984 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1985 current_allocation);
1987 while (pinfo != NULL)
1989 fprintf(STDERR, " %lu bytes at %p\n",
1990 (unsigned long)pinfo->size, pinfo->pointer);
1991 pinfo = pinfo->next;
1994 #endif
1996 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1997 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1998 current_allocation);
1999 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2000 maximum_allocation);
2001 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2002 total_allocation);
2003 fprintf(STDERR, " Number of allocations: %10d\n",
2004 num_allocations);
2005 #endif
2008 #ifdef PNGTEST_TIMING
2009 t_stop = (float)clock();
2010 t_misc += (t_stop - t_start);
2011 t_start = t_stop;
2012 fprintf(STDERR, " CPU time used = %.3f seconds",
2013 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
2014 fprintf(STDERR, " (decoding %.3f,\n",
2015 t_decode/(float)CLOCKS_PER_SEC);
2016 fprintf(STDERR, " encoding %.3f ,",
2017 t_encode/(float)CLOCKS_PER_SEC);
2018 fprintf(STDERR, " other %.3f seconds)\n\n",
2019 t_misc/(float)CLOCKS_PER_SEC);
2020 #endif
2022 if (ierror == 0)
2023 fprintf(STDERR, " libpng passes test\n");
2025 else
2026 fprintf(STDERR, " libpng FAILS test\n");
2028 dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2029 fprintf(STDERR, " Default limits:\n");
2030 fprintf(STDERR, " width_max = %lu\n",
2031 (unsigned long) png_get_user_width_max(dummy_ptr));
2032 fprintf(STDERR, " height_max = %lu\n",
2033 (unsigned long) png_get_user_height_max(dummy_ptr));
2034 if (png_get_chunk_cache_max(dummy_ptr) == 0)
2035 fprintf(STDERR, " cache_max = unlimited\n");
2036 else
2037 fprintf(STDERR, " cache_max = %lu\n",
2038 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
2039 if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2040 fprintf(STDERR, " malloc_max = unlimited\n");
2041 else
2042 fprintf(STDERR, " malloc_max = %lu\n",
2043 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2044 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2046 return (int)(ierror != 0);
2048 #else
2050 main(void)
2052 fprintf(STDERR,
2053 " test ignored because libpng was not built with read support\n");
2054 /* And skip this test */
2055 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
2057 #endif
2059 /* Generate a compiler error if there is an old png.h in the search path. */
2060 typedef png_libpng_version_1_6_19 Your_png_h_is_not_version_1_6_19;