minor tidy up
[AROS.git] / workbench / libs / png / pngtest.c
blobf8f76bfc986553026b65a303f27b395b84988c61
2 /* pngtest.c - a simple test program to test libpng
4 * Last changed in libpng 1.6.26 [October 20, 2016]
5 * Copyright (c) 1998-2002,2004,2006-2016 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 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
968 if (strict != 0)
970 /* Treat png_benign_error() as errors on read */
971 png_set_benign_errors(read_ptr, 0);
973 # ifdef PNG_WRITE_SUPPORTED
974 /* Treat them as errors on write */
975 png_set_benign_errors(write_ptr, 0);
976 # endif
978 /* if strict is not set, then app warnings and errors are treated as
979 * warnings in release builds, but not in unstable builds; this can be
980 * changed with '--relaxed'.
984 else if (relaxed != 0)
986 /* Allow application (pngtest) errors and warnings to pass */
987 png_set_benign_errors(read_ptr, 1);
989 /* Turn off CRC checking while reading */
990 png_set_crc_action(read_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
992 #ifdef PNG_IGNORE_ADLER32
993 /* Turn off ADLER32 checking while reading */
994 png_set_option(read_ptr, PNG_IGNORE_ADLER32, PNG_OPTION_ON);
995 #endif
997 # ifdef PNG_WRITE_SUPPORTED
998 png_set_benign_errors(write_ptr, 1);
999 # endif
1002 #endif /* BENIGN_ERRORS */
1004 pngtest_debug("Initializing input and output streams");
1005 #ifdef PNG_STDIO_SUPPORTED
1006 png_init_io(read_ptr, fpin);
1007 # ifdef PNG_WRITE_SUPPORTED
1008 png_init_io(write_ptr, fpout);
1009 # endif
1010 #else
1011 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1012 # ifdef PNG_WRITE_SUPPORTED
1013 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
1014 # ifdef PNG_WRITE_FLUSH_SUPPORTED
1015 pngtest_flush);
1016 # else
1017 NULL);
1018 # endif
1019 # endif
1020 #endif
1022 if (status_dots_requested == 1)
1024 #ifdef PNG_WRITE_SUPPORTED
1025 png_set_write_status_fn(write_ptr, write_row_callback);
1026 #endif
1027 png_set_read_status_fn(read_ptr, read_row_callback);
1030 else
1032 #ifdef PNG_WRITE_SUPPORTED
1033 png_set_write_status_fn(write_ptr, NULL);
1034 #endif
1035 png_set_read_status_fn(read_ptr, NULL);
1038 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1039 png_set_read_user_transform_fn(read_ptr, read_user_callback);
1040 #endif
1041 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1042 zero_samples = 0;
1043 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1044 #endif
1046 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1047 /* Preserve all the unknown chunks, if possible. If this is disabled then,
1048 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1049 * libpng to *save* the unknown chunks on read (because we can't switch the
1050 * save option on!)
1052 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1053 * unknown chunks and write will write them all.
1055 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1056 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1057 NULL, 0);
1058 #endif
1059 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1060 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1061 NULL, 0);
1062 #endif
1063 #endif
1065 pngtest_debug("Reading info struct");
1066 png_read_info(read_ptr, read_info_ptr);
1068 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1069 /* This is a bit of a hack; there is no obvious way in the callback function
1070 * to determine that the chunks before the first IDAT have been read, so
1071 * remove the info_ptr (which is only used to determine position relative to
1072 * PLTE) here to indicate that we are after the IDAT.
1074 user_chunk_data.info_ptr = NULL;
1075 #endif
1077 pngtest_debug("Transferring info struct");
1079 int interlace_type, compression_type, filter_type;
1081 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1082 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
1084 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
1085 color_type, interlace_type, compression_type, filter_type);
1086 /* num_passes may not be available below if interlace support is not
1087 * provided by libpng for both read and write.
1089 switch (interlace_type)
1091 case PNG_INTERLACE_NONE:
1092 num_passes = 1;
1093 break;
1095 case PNG_INTERLACE_ADAM7:
1096 num_passes = 7;
1097 break;
1099 default:
1100 png_error(read_ptr, "invalid interlace type");
1101 /*NOT REACHED*/
1105 else
1106 png_error(read_ptr, "png_get_IHDR failed");
1108 #ifdef PNG_FIXED_POINT_SUPPORTED
1109 #ifdef PNG_cHRM_SUPPORTED
1111 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1112 blue_y;
1114 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1115 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1117 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1118 red_y, green_x, green_y, blue_x, blue_y);
1121 #endif
1122 #ifdef PNG_gAMA_SUPPORTED
1124 png_fixed_point gamma;
1126 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
1127 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
1129 #endif
1130 #else /* Use floating point versions */
1131 #ifdef PNG_FLOATING_POINT_SUPPORTED
1132 #ifdef PNG_cHRM_SUPPORTED
1134 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1135 blue_y;
1137 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1138 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1140 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1141 red_y, green_x, green_y, blue_x, blue_y);
1144 #endif
1145 #ifdef PNG_gAMA_SUPPORTED
1147 double gamma;
1149 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
1150 png_set_gAMA(write_ptr, write_info_ptr, gamma);
1152 #endif
1153 #endif /* Floating point */
1154 #endif /* Fixed point */
1155 #ifdef PNG_iCCP_SUPPORTED
1157 png_charp name;
1158 png_bytep profile;
1159 png_uint_32 proflen;
1160 int compression_type;
1162 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1163 &profile, &proflen) != 0)
1165 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1166 profile, proflen);
1169 #endif
1170 #ifdef PNG_sRGB_SUPPORTED
1172 int intent;
1174 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
1175 png_set_sRGB(write_ptr, write_info_ptr, intent);
1177 #endif
1179 png_colorp palette;
1180 int num_palette;
1182 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
1183 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
1185 #ifdef PNG_bKGD_SUPPORTED
1187 png_color_16p background;
1189 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
1191 png_set_bKGD(write_ptr, write_info_ptr, background);
1194 #endif
1195 #ifdef PNG_hIST_SUPPORTED
1197 png_uint_16p hist;
1199 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
1200 png_set_hIST(write_ptr, write_info_ptr, hist);
1202 #endif
1203 #ifdef PNG_oFFs_SUPPORTED
1205 png_int_32 offset_x, offset_y;
1206 int unit_type;
1208 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1209 &unit_type) != 0)
1211 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1214 #endif
1215 #ifdef PNG_pCAL_SUPPORTED
1217 png_charp purpose, units;
1218 png_charpp params;
1219 png_int_32 X0, X1;
1220 int type, nparams;
1222 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1223 &nparams, &units, &params) != 0)
1225 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1226 nparams, units, params);
1229 #endif
1230 #ifdef PNG_pHYs_SUPPORTED
1232 png_uint_32 res_x, res_y;
1233 int unit_type;
1235 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1236 &unit_type) != 0)
1237 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
1239 #endif
1240 #ifdef PNG_sBIT_SUPPORTED
1242 png_color_8p sig_bit;
1244 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
1245 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
1247 #endif
1248 #ifdef PNG_sCAL_SUPPORTED
1249 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1250 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
1252 int unit;
1253 double scal_width, scal_height;
1255 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1256 &scal_height) != 0)
1258 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1261 #else
1262 #ifdef PNG_FIXED_POINT_SUPPORTED
1264 int unit;
1265 png_charp scal_width, scal_height;
1267 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1268 &scal_height) != 0)
1270 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1271 scal_height);
1274 #endif
1275 #endif
1276 #endif
1278 #ifdef PNG_sPLT_SUPPORTED
1280 png_sPLT_tp entries;
1282 int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1283 if (num_entries)
1285 png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1288 #endif
1290 #ifdef PNG_TEXT_SUPPORTED
1292 png_textp text_ptr;
1293 int num_text;
1295 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1297 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1299 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1301 if (verbose != 0)
1303 int i;
1305 printf("\n");
1306 for (i=0; i<num_text; i++)
1308 printf(" Text compression[%d]=%d\n",
1309 i, text_ptr[i].compression);
1313 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1316 #endif
1317 #ifdef PNG_tIME_SUPPORTED
1319 png_timep mod_time;
1321 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
1323 png_set_tIME(write_ptr, write_info_ptr, mod_time);
1324 #ifdef PNG_TIME_RFC1123_SUPPORTED
1325 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1326 tIME_string[(sizeof tIME_string) - 1] = '\0';
1328 else
1330 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1331 tIME_string[(sizeof tIME_string) - 1] = '\0';
1334 tIME_chunk_present++;
1335 #endif /* TIME_RFC1123 */
1338 #endif
1339 #ifdef PNG_tRNS_SUPPORTED
1341 png_bytep trans_alpha;
1342 int num_trans;
1343 png_color_16p trans_color;
1345 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1346 &trans_color) != 0)
1348 int sample_max = (1 << bit_depth);
1349 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1350 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1351 (int)trans_color->gray > sample_max) ||
1352 (color_type == PNG_COLOR_TYPE_RGB &&
1353 ((int)trans_color->red > sample_max ||
1354 (int)trans_color->green > sample_max ||
1355 (int)trans_color->blue > sample_max))))
1356 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1357 trans_color);
1360 #endif
1361 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1363 png_unknown_chunkp unknowns;
1364 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1365 &unknowns);
1367 if (num_unknowns != 0)
1369 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1370 num_unknowns);
1371 #if PNG_LIBPNG_VER < 10600
1372 /* Copy the locations from the read_info_ptr. The automatically
1373 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1374 * because they are reset from the write pointer (removed in 1.6.0).
1377 int i;
1378 for (i = 0; i < num_unknowns; i++)
1379 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1380 unknowns[i].location);
1382 #endif
1385 #endif
1387 #ifdef PNG_WRITE_SUPPORTED
1388 pngtest_debug("Writing info struct");
1390 /* Write the info in two steps so that if we write the 'unknown' chunks here
1391 * they go to the correct place.
1393 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1395 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1397 png_write_info(write_ptr, write_info_ptr);
1399 write_chunks(write_ptr, before_IDAT); /* after PLTE */
1401 #ifdef PNG_COMPRESSION_COMPAT
1402 /* Test the 'compatibility' setting here, if it is available. */
1403 png_set_compression(write_ptr, PNG_COMPRESSION_COMPAT);
1404 #endif
1405 #endif
1407 #ifdef SINGLE_ROWBUF_ALLOC
1408 pngtest_debug("Allocating row buffer...");
1409 row_buf = (png_bytep)png_malloc(read_ptr,
1410 png_get_rowbytes(read_ptr, read_info_ptr));
1412 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1413 #endif /* SINGLE_ROWBUF_ALLOC */
1414 pngtest_debug("Writing row data");
1416 #if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1417 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1418 /* Both must be defined for libpng to be able to handle the interlace,
1419 * otherwise it gets handled below by simply reading and writing the passes
1420 * directly.
1422 if (png_set_interlace_handling(read_ptr) != num_passes)
1423 png_error(write_ptr,
1424 "png_set_interlace_handling(read): wrong pass count ");
1425 if (png_set_interlace_handling(write_ptr) != num_passes)
1426 png_error(write_ptr,
1427 "png_set_interlace_handling(write): wrong pass count ");
1428 #else /* png_set_interlace_handling not called on either read or write */
1429 # define calc_pass_height
1430 #endif /* not using libpng interlace handling */
1432 #ifdef PNGTEST_TIMING
1433 t_stop = (float)clock();
1434 t_misc += (t_stop - t_start);
1435 t_start = t_stop;
1436 #endif
1437 for (pass = 0; pass < num_passes; pass++)
1439 # ifdef calc_pass_height
1440 png_uint_32 pass_height;
1442 if (num_passes == 7) /* interlaced */
1444 if (PNG_PASS_COLS(width, pass) > 0)
1445 pass_height = PNG_PASS_ROWS(height, pass);
1447 else
1448 pass_height = 0;
1451 else /* not interlaced */
1452 pass_height = height;
1453 # else
1454 # define pass_height height
1455 # endif
1457 pngtest_debug1("Writing row data for pass %d", pass);
1458 for (y = 0; y < pass_height; y++)
1460 #ifndef SINGLE_ROWBUF_ALLOC
1461 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1463 row_buf = (png_bytep)png_malloc(read_ptr,
1464 png_get_rowbytes(read_ptr, read_info_ptr));
1466 pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1467 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
1469 #endif /* !SINGLE_ROWBUF_ALLOC */
1470 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1472 #ifdef PNG_WRITE_SUPPORTED
1473 #ifdef PNGTEST_TIMING
1474 t_stop = (float)clock();
1475 t_decode += (t_stop - t_start);
1476 t_start = t_stop;
1477 #endif
1478 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1479 #ifdef PNGTEST_TIMING
1480 t_stop = (float)clock();
1481 t_encode += (t_stop - t_start);
1482 t_start = t_stop;
1483 #endif
1484 #endif /* WRITE */
1486 #ifndef SINGLE_ROWBUF_ALLOC
1487 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1488 png_free(read_ptr, row_buf);
1489 row_buf = NULL;
1490 #endif /* !SINGLE_ROWBUF_ALLOC */
1494 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1495 # ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1496 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1497 # endif
1498 # ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1499 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1500 # endif
1501 #endif
1503 pngtest_debug("Reading and writing end_info data");
1505 png_read_end(read_ptr, end_info_ptr);
1506 #ifdef PNG_TEXT_SUPPORTED
1508 png_textp text_ptr;
1509 int num_text;
1511 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1513 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1515 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1517 if (verbose != 0)
1519 int i;
1521 printf("\n");
1522 for (i=0; i<num_text; i++)
1524 printf(" Text compression[%d]=%d\n",
1525 i, text_ptr[i].compression);
1529 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1532 #endif
1533 #ifdef PNG_tIME_SUPPORTED
1535 png_timep mod_time;
1537 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
1539 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1540 #ifdef PNG_TIME_RFC1123_SUPPORTED
1541 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1542 tIME_string[(sizeof tIME_string) - 1] = '\0';
1544 else
1546 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1547 tIME_string[(sizeof tIME_string)-1] = '\0';
1550 tIME_chunk_present++;
1551 #endif /* TIME_RFC1123 */
1554 #endif
1555 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1557 png_unknown_chunkp unknowns;
1558 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1559 &unknowns);
1561 if (num_unknowns != 0)
1563 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1564 num_unknowns);
1565 #if PNG_LIBPNG_VER < 10600
1566 /* Copy the locations from the read_info_ptr. The automatically
1567 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1568 * because they are reset from the write pointer (removed in 1.6.0).
1571 int i;
1572 for (i = 0; i < num_unknowns; i++)
1573 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1574 unknowns[i].location);
1576 #endif
1579 #endif
1581 #ifdef PNG_WRITE_SUPPORTED
1582 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1583 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1584 * This is here just to make pngtest replicate the results from libpng
1585 * versions prior to 1.5.4, and to test this new API.
1587 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1588 #endif
1590 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1591 * do it is to write them *before* calling png_write_end. When unknown
1592 * chunks are written by libpng, however, they are written just before IEND.
1593 * There seems to be no way round this, however vpAg/sTER are not expected
1594 * after IDAT.
1596 write_chunks(write_ptr, after_IDAT);
1598 png_write_end(write_ptr, write_end_info_ptr);
1599 #endif
1601 #ifdef PNG_EASY_ACCESS_SUPPORTED
1602 if (verbose != 0)
1604 png_uint_32 iwidth, iheight;
1605 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1606 iheight = png_get_image_height(write_ptr, write_info_ptr);
1607 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1608 (unsigned long)iwidth, (unsigned long)iheight);
1610 #endif
1612 pngtest_debug("Destroying data structs");
1613 #ifdef SINGLE_ROWBUF_ALLOC
1614 pngtest_debug("destroying row_buf for read_ptr");
1615 png_free(read_ptr, row_buf);
1616 row_buf = NULL;
1617 #endif /* SINGLE_ROWBUF_ALLOC */
1618 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
1619 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1620 #ifdef PNG_WRITE_SUPPORTED
1621 pngtest_debug("destroying write_end_info_ptr");
1622 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1623 pngtest_debug("destroying write_ptr, write_info_ptr");
1624 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1625 #endif
1626 pngtest_debug("Destruction complete.");
1628 FCLOSE(fpin);
1629 FCLOSE(fpout);
1631 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1632 * Unsupported chunks can result in warnings, in that case ignore the strict
1633 * setting, otherwise fail the test on warnings as well as errors.
1635 if (error_count > 0)
1637 /* We don't really expect to get here because of the setjmp handling
1638 * above, but this is safe.
1640 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
1641 inname, error_count, warning_count);
1643 if (strict != 0)
1644 return (1);
1647 # ifdef PNG_WRITE_SUPPORTED
1648 /* If there is no write support nothing was written! */
1649 else if (unsupported_chunks > 0)
1651 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1652 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1654 # endif
1656 else if (warning_count > 0)
1658 fprintf(STDERR, "\n %s: %d libpng warnings found",
1659 inname, warning_count);
1661 if (strict != 0)
1662 return (1);
1665 pngtest_debug("Opening files for comparison");
1666 if ((fpin = fopen(inname, "rb")) == NULL)
1668 fprintf(STDERR, "Could not find file %s\n", inname);
1669 return (1);
1672 if ((fpout = fopen(outname, "rb")) == NULL)
1674 fprintf(STDERR, "Could not find file %s\n", outname);
1675 FCLOSE(fpin);
1676 return (1);
1679 #if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1680 defined (PNG_WRITE_FILTER_SUPPORTED)
1681 if (interlace_preserved != 0) /* else the files will be changed */
1683 for (;;)
1685 static int wrote_question = 0;
1686 png_size_t num_in, num_out;
1687 char inbuf[256], outbuf[256];
1689 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1690 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1692 if (num_in != num_out)
1694 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1695 inname, outname);
1697 if (wrote_question == 0 && unsupported_chunks == 0)
1699 fprintf(STDERR,
1700 " Was %s written with the same maximum IDAT"
1701 " chunk size (%d bytes),",
1702 inname, PNG_ZBUF_SIZE);
1703 fprintf(STDERR,
1704 "\n filtering heuristic (libpng default), compression");
1705 fprintf(STDERR,
1706 " level (zlib default),\n and zlib version (%s)?\n\n",
1707 ZLIB_VERSION);
1708 wrote_question = 1;
1711 FCLOSE(fpin);
1712 FCLOSE(fpout);
1714 if (strict != 0 && unsupported_chunks == 0)
1715 return (1);
1717 else
1718 return (0);
1721 if (num_in == 0)
1722 break;
1724 if (memcmp(inbuf, outbuf, num_in))
1726 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1727 outname);
1729 if (wrote_question == 0 && unsupported_chunks == 0)
1731 fprintf(STDERR,
1732 " Was %s written with the same maximum"
1733 " IDAT chunk size (%d bytes),",
1734 inname, PNG_ZBUF_SIZE);
1735 fprintf(STDERR,
1736 "\n filtering heuristic (libpng default), compression");
1737 fprintf(STDERR,
1738 " level (zlib default),\n and zlib version (%s)?\n\n",
1739 ZLIB_VERSION);
1740 wrote_question = 1;
1743 FCLOSE(fpin);
1744 FCLOSE(fpout);
1746 /* NOTE: the unsupported_chunks escape is permitted here because
1747 * unsupported text chunk compression will result in the compression
1748 * mode being changed (to NONE) yet, in the test case, the result
1749 * can be exactly the same size!
1751 if (strict != 0 && unsupported_chunks == 0)
1752 return (1);
1754 else
1755 return (0);
1759 #endif /* WRITE && WRITE_FILTER */
1761 FCLOSE(fpin);
1762 FCLOSE(fpout);
1764 return (0);
1767 /* Input and output filenames */
1768 #ifdef RISCOS
1769 static PNG_CONST char *inname = "pngtest/png";
1770 static PNG_CONST char *outname = "pngout/png";
1771 #else
1772 static PNG_CONST char *inname = "pngtest.png";
1773 static PNG_CONST char *outname = "pngout.png";
1774 #endif
1777 main(int argc, char *argv[])
1779 int multiple = 0;
1780 int ierror = 0;
1782 png_structp dummy_ptr;
1784 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1785 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
1786 fprintf(STDERR, "%s", png_get_copyright(NULL));
1787 /* Show the version of libpng used in building the library */
1788 fprintf(STDERR, " library (%lu):%s",
1789 (unsigned long)png_access_version_number(),
1790 png_get_header_version(NULL));
1792 /* Show the version of libpng used in building the application */
1793 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1794 PNG_HEADER_VERSION_STRING);
1796 /* Do some consistency checking on the memory allocation settings, I'm
1797 * not sure this matters, but it is nice to know, the first of these
1798 * tests should be impossible because of the way the macros are set
1799 * in pngconf.h
1801 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1802 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1803 #endif
1804 /* I think the following can happen. */
1805 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1806 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1807 #endif
1809 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1811 fprintf(STDERR,
1812 "Warning: versions are different between png.h and png.c\n");
1813 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1814 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1815 ++ierror;
1818 if (argc > 1)
1820 if (strcmp(argv[1], "-m") == 0)
1822 multiple = 1;
1823 status_dots_requested = 0;
1826 else if (strcmp(argv[1], "-mv") == 0 ||
1827 strcmp(argv[1], "-vm") == 0 )
1829 multiple = 1;
1830 verbose = 1;
1831 status_dots_requested = 1;
1834 else if (strcmp(argv[1], "-v") == 0)
1836 verbose = 1;
1837 status_dots_requested = 1;
1838 inname = argv[2];
1841 else if (strcmp(argv[1], "--strict") == 0)
1843 status_dots_requested = 0;
1844 verbose = 1;
1845 inname = argv[2];
1846 strict++;
1847 relaxed = 0;
1850 else if (strcmp(argv[1], "--relaxed") == 0)
1852 status_dots_requested = 0;
1853 verbose = 1;
1854 inname = argv[2];
1855 strict = 0;
1856 relaxed++;
1859 else
1861 inname = argv[1];
1862 status_dots_requested = 0;
1866 if (multiple == 0 && argc == 3 + verbose)
1867 outname = argv[2 + verbose];
1869 if ((multiple == 0 && argc > 3 + verbose) ||
1870 (multiple != 0 && argc < 2))
1872 fprintf(STDERR,
1873 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1874 argv[0], argv[0]);
1875 fprintf(STDERR,
1876 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1877 fprintf(STDERR,
1878 " with -m %s is used as a temporary file\n", outname);
1879 exit(1);
1882 if (multiple != 0)
1884 int i;
1885 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1886 int allocation_now = current_allocation;
1887 #endif
1888 for (i=2; i<argc; ++i)
1890 int kerror;
1891 fprintf(STDERR, "\n Testing %s:", argv[i]);
1892 #if PNG_DEBUG > 0
1893 fprintf(STDERR, "\n");
1894 #endif
1895 kerror = test_one_file(argv[i], outname);
1896 if (kerror == 0)
1898 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1899 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1900 (unsigned long)zero_samples);
1901 #else
1902 fprintf(STDERR, " PASS\n");
1903 #endif
1904 #ifdef PNG_TIME_RFC1123_SUPPORTED
1905 if (tIME_chunk_present != 0)
1906 fprintf(STDERR, " tIME = %s\n", tIME_string);
1908 tIME_chunk_present = 0;
1909 #endif /* TIME_RFC1123 */
1912 else
1914 fprintf(STDERR, " FAIL\n");
1915 ierror += kerror;
1917 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1918 if (allocation_now != current_allocation)
1919 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1920 current_allocation - allocation_now);
1922 if (current_allocation != 0)
1924 memory_infop pinfo = pinformation;
1926 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1927 current_allocation);
1929 while (pinfo != NULL)
1931 fprintf(STDERR, " %lu bytes at %p\n",
1932 (unsigned long)pinfo->size,
1933 pinfo->pointer);
1934 pinfo = pinfo->next;
1937 #endif
1939 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1940 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1941 current_allocation);
1942 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1943 maximum_allocation);
1944 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1945 total_allocation);
1946 fprintf(STDERR, " Number of allocations: %10d\n",
1947 num_allocations);
1948 #endif
1951 else
1953 int i;
1954 for (i = 0; i<3; ++i)
1956 int kerror;
1957 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1958 int allocation_now = current_allocation;
1959 #endif
1960 if (i == 1)
1961 status_dots_requested = 1;
1963 else if (verbose == 0)
1964 status_dots_requested = 0;
1966 if (i == 0 || verbose == 1 || ierror != 0)
1968 fprintf(STDERR, "\n Testing %s:", inname);
1969 #if PNG_DEBUG > 0
1970 fprintf(STDERR, "\n");
1971 #endif
1974 kerror = test_one_file(inname, outname);
1976 if (kerror == 0)
1978 if (verbose == 1 || i == 2)
1980 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1981 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1982 (unsigned long)zero_samples);
1983 #else
1984 fprintf(STDERR, " PASS\n");
1985 #endif
1986 #ifdef PNG_TIME_RFC1123_SUPPORTED
1987 if (tIME_chunk_present != 0)
1988 fprintf(STDERR, " tIME = %s\n", tIME_string);
1989 #endif /* TIME_RFC1123 */
1993 else
1995 if (verbose == 0 && i != 2)
1997 fprintf(STDERR, "\n Testing %s:", inname);
1998 #if PNG_DEBUG > 0
1999 fprintf(STDERR, "\n");
2000 #endif
2003 fprintf(STDERR, " FAIL\n");
2004 ierror += kerror;
2006 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2007 if (allocation_now != current_allocation)
2008 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
2009 current_allocation - allocation_now);
2011 if (current_allocation != 0)
2013 memory_infop pinfo = pinformation;
2015 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
2016 current_allocation);
2018 while (pinfo != NULL)
2020 fprintf(STDERR, " %lu bytes at %p\n",
2021 (unsigned long)pinfo->size, pinfo->pointer);
2022 pinfo = pinfo->next;
2025 #endif
2027 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2028 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2029 current_allocation);
2030 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2031 maximum_allocation);
2032 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2033 total_allocation);
2034 fprintf(STDERR, " Number of allocations: %10d\n",
2035 num_allocations);
2036 #endif
2039 #ifdef PNGTEST_TIMING
2040 t_stop = (float)clock();
2041 t_misc += (t_stop - t_start);
2042 t_start = t_stop;
2043 fprintf(STDERR, " CPU time used = %.3f seconds",
2044 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
2045 fprintf(STDERR, " (decoding %.3f,\n",
2046 t_decode/(float)CLOCKS_PER_SEC);
2047 fprintf(STDERR, " encoding %.3f ,",
2048 t_encode/(float)CLOCKS_PER_SEC);
2049 fprintf(STDERR, " other %.3f seconds)\n\n",
2050 t_misc/(float)CLOCKS_PER_SEC);
2051 #endif
2053 if (ierror == 0)
2054 fprintf(STDERR, " libpng passes test\n");
2056 else
2057 fprintf(STDERR, " libpng FAILS test\n");
2059 dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2060 fprintf(STDERR, " Default limits:\n");
2061 fprintf(STDERR, " width_max = %lu\n",
2062 (unsigned long) png_get_user_width_max(dummy_ptr));
2063 fprintf(STDERR, " height_max = %lu\n",
2064 (unsigned long) png_get_user_height_max(dummy_ptr));
2065 if (png_get_chunk_cache_max(dummy_ptr) == 0)
2066 fprintf(STDERR, " cache_max = unlimited\n");
2067 else
2068 fprintf(STDERR, " cache_max = %lu\n",
2069 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
2070 if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2071 fprintf(STDERR, " malloc_max = unlimited\n");
2072 else
2073 fprintf(STDERR, " malloc_max = %lu\n",
2074 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2075 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2077 return (int)(ierror != 0);
2079 #else
2081 main(void)
2083 fprintf(STDERR,
2084 " test ignored because libpng was not built with read support\n");
2085 /* And skip this test */
2086 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
2088 #endif
2090 /* Generate a compiler error if there is an old png.h in the search path. */
2091 typedef png_libpng_version_1_6_28 Your_png_h_is_not_version_1_6_28;