blob: a277b07de528ad3319f02de313150764eaeadd3e [file] [log] [blame]
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001
Guy Schalnat4ee97b01996-01-16 01:51:56 -06002/* pngtest.c - a simple test program to test libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -06004 * Last changed in libpng 1.5.0 [March 12, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This program reads in a PNG image, writes it out again, and then
14 * compares the two files. If the files are identical, this shows that
15 * the basic chunk handling, filtering, and (de)compression code is working
16 * properly. It does not currently test all of the transforms, although
17 * it probably should.
18 *
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050019 * The program will report "FAIL" in certain legitimate cases:
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060020 * 1) when the compression level or filter selection method is changed.
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060021 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -060022 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
23 * exist in the input file.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060024 * 4) others not listed here...
25 * In these cases, it is best to check with another tool such as "pngcheck"
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060026 * to see what the differences between the two files are.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060027 *
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
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030 * 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 ..."
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060032 */
Guy Schalnat0d580581995-07-20 02:43:20 -050033
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050034#include "png.h"
35
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050036# include <stdio.h>
37# include <stdlib.h>
38# define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050039
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050040#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050041 typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060042#endif
43
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060044/* Makes pngtest verbose so we can find problems. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050045#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050046# define PNG_DEBUG 0
47#endif
48
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -060049#if PNG_DEBUG > 1
50# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
51# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
52# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
53#else
54# define pngtest_debug(m) ((void)0)
55# define pngtest_debug1(m,p1) ((void)0)
56# define pngtest_debug2(m,p1,p2) ((void)0)
57#endif
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060058
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050059#if !PNG_DEBUG
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050060# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060061#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050062
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050063/* Turn on CPU timing
64#define PNGTEST_TIMING
65*/
66
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050067#ifndef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068#undef PNGTEST_TIMING
69#endif
70
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050071#ifdef PNGTEST_TIMING
72static float t_start, t_stop, t_decode, t_encode, t_misc;
73#include <time.h>
74#endif
75
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050076#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050077#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050078static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050079static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050080#endif
81
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050082static int verbose = 0;
83
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060084int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060085
Guy Schalnat0d580581995-07-20 02:43:20 -050086#ifdef __TURBOC__
87#include <mem.h>
88#endif
89
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050090/* Defined so I can write to a file on gui/windowing platforms */
Guy Schalnat6d764711995-12-19 03:22:19 -060091/* #define STDERR stderr */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050092#define STDERR stdout /* For DOS */
Guy Schalnat0d580581995-07-20 02:43:20 -050093
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -050094/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
95#ifndef png_jmpbuf
96# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
97#endif
98
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050099/* Example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500100static int status_pass = 1;
101static int status_dots_requested = 0;
102static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600103
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600104void PNGCBAPI
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500105read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600106void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600107read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600108{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500109 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
110 return;
111 if (status_pass != pass)
112 {
113 fprintf(stdout, "\n Pass %d: ", pass);
114 status_pass = pass;
115 status_dots = 31;
116 }
117 status_dots--;
118 if (status_dots == 0)
119 {
120 fprintf(stdout, "\n ");
121 status_dots=30;
122 }
123 fprintf(stdout, "r");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600124}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600125
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600126void PNGCBAPI
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500127write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600128void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600129write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600130{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500131 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
132 return;
133 fprintf(stdout, "w");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600134}
135
136
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500137#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500138/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500139 * but merely examine the row filters. We set this to 256 rather than
140 * 5 in case illegal filter values are present.)
141 */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500142static png_uint_32 filters_used[256];
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600143void PNGCBAPI
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500144count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600145void PNGCBAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500146count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
147{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 if (png_ptr != NULL && row_info != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500149 ++filters_used[*(data - 1)];
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500150}
151#endif
152
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500153#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500154/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500155 * but merely count the zero samples)
156 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600157
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500158static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600159
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600160void PNGCBAPI
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500161count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600162void PNGCBAPI
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500163count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600164{
165 png_bytep dp = data;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500166 if (png_ptr == NULL)return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600167
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500168 /* Contents of row_info:
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600169 * png_uint_32 width width of row
170 * png_uint_32 rowbytes number of bytes in row
171 * png_byte color_type color type of pixels
172 * png_byte bit_depth bit depth of samples
173 * png_byte channels number of channels (1-4)
174 * png_byte pixel_depth bits per pixel (depth*channels)
175 */
176
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500177 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600178
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500179 if (row_info->color_type == 0 || row_info->color_type == 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600180 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500181 int pos = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500182 png_uint_32 n, nstop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500183 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600184 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185 if (row_info->bit_depth == 1)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500186 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500187 if (((*dp << pos++ ) & 0x80) == 0)
188 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500189 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600190 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500191 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600192 dp++;
193 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500194 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500195 if (row_info->bit_depth == 2)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500196 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500197 if (((*dp << (pos+=2)) & 0xc0) == 0)
198 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500199 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600200 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500201 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600202 dp++;
203 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500204 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500205 if (row_info->bit_depth == 4)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500206 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 if (((*dp << (pos+=4)) & 0xf0) == 0)
208 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500209 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600210 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500211 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600212 dp++;
213 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500214 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500215 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500216 if (*dp++ == 0)
217 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500218 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600219 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500220 if ((*dp | *(dp+1)) == 0)
221 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600222 dp+=2;
223 }
224 }
225 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500226 else /* Other color types */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600227 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500228 png_uint_32 n, nstop;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600229 int channel;
230 int color_channels = row_info->channels;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500231 if (row_info->color_type > 3)color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600232
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500233 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600234 {
235 for (channel = 0; channel < color_channels; channel++)
236 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500237 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500238 if (*dp++ == 0)
239 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500240 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600241 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500242 if ((*dp | *(dp+1)) == 0)
243 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600244 dp+=2;
245 }
246 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500247 if (row_info->color_type > 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600248 {
249 dp++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500250 if (row_info->bit_depth == 16)
251 dp++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600252 }
253 }
254 }
255}
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600256#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600257
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600258static int wrote_question = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600259
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500260#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600261/* START of code to validate stdio-free compilation */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500262/* These copies of the default read/write functions come from pngrio.c and
263 * pngwio.c. They allow "don't include stdio" testing of the library.
264 * This is the function that does the actual reading of data. If you are
265 * not reading from a standard C stream, you should create a replacement
266 * read_data function and use it at run time with png_set_read_fn(), rather
267 * than changing the library.
268 */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600269
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600270#ifndef USE_FAR_KEYWORD
271static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500272pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600273{
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600274 png_size_t check = 0;
275 png_voidp io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600276
277 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
278 * instead of an int, which is what fread() actually returns.
279 */
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600280 io_ptr = png_get_io_ptr(png_ptr);
281 if (io_ptr != NULL)
282 {
283 check = fread(data, 1, length, (png_FILE_p)io_ptr);
284 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600286 if (check != length)
287 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500288 png_error(png_ptr, "Read Error!");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600289 }
290}
Guy Schalnate5a37791996-06-05 15:50:50 -0500291#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500292/* This is the model-independent version. Since the standard I/O library
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600293 can't handle far buffers in the medium and small models, we have to copy
294 the data.
295*/
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600296
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600297#define NEAR_BUF_SIZE 1024
298#define MIN(a,b) (a <= b ? a : b)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600299
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600300static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500301pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600302{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500303 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600304 png_byte *n_data;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500305 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600306
307 /* Check if data really is near. If so, use usual code. */
308 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600309 io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr));
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600310 if ((png_bytep)n_data == data)
311 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500312 check = fread(n_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600313 }
314 else
315 {
316 png_byte buf[NEAR_BUF_SIZE];
317 png_size_t read, remaining, err;
318 check = 0;
319 remaining = length;
320 do
321 {
322 read = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 err = fread(buf, 1, 1, io_ptr);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500324 png_memcpy(data, buf, read); /* Copy far buffer to near buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500325 if (err != read)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600326 break;
327 else
328 check += err;
329 data += read;
330 remaining -= read;
331 }
332 while (remaining != 0);
333 }
334 if (check != length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600335 png_error(png_ptr, "read Error");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600336}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600337#endif /* USE_FAR_KEYWORD */
Guy Schalnat0d580581995-07-20 02:43:20 -0500338
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500339#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600340static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500341pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600342{
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500343 /* Do nothing; fflush() is said to be just a waste of energy. */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500344 png_ptr = png_ptr; /* Stifle compiler warning */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600345}
346#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500347
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500348/* This is the function that does the actual writing of data. If you are
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500349 * not writing to a standard C stream, you should create a replacement
350 * write_data function and use it at run time with png_set_write_fn(), rather
351 * than changing the library.
352 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600353#ifndef USE_FAR_KEYWORD
354static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500355pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600356{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500357 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600358
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600359 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600360 if (check != length)
361 {
362 png_error(png_ptr, "Write Error");
363 }
364}
365#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500366/* This is the model-independent version. Since the standard I/O library
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600367 can't handle far buffers in the medium and small models, we have to copy
368 the data.
369*/
370
371#define NEAR_BUF_SIZE 1024
372#define MIN(a,b) (a <= b ? a : b)
373
374static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500375pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600376{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500377 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600378 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500379 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600380
381 /* Check if data really is near. If so, use usual code. */
382 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600383 io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr));
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600384 if ((png_bytep)near_data == data)
385 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500386 check = fwrite(near_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600387 }
388 else
389 {
390 png_byte buf[NEAR_BUF_SIZE];
391 png_size_t written, remaining, err;
392 check = 0;
393 remaining = length;
394 do
395 {
396 written = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500397 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500398 err = fwrite(buf, 1, written, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600399 if (err != written)
400 break;
401 else
402 check += err;
403 data += written;
404 remaining -= written;
405 }
406 while (remaining != 0);
407 }
408 if (check != length)
409 {
410 png_error(png_ptr, "Write Error");
411 }
412}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600413#endif /* USE_FAR_KEYWORD */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600414
415/* This function is called when there is a warning, but the library thinks
416 * it can continue anyway. Replacement functions don't have to do anything
417 * here if you don't want to. In the default configuration, png_ptr is
418 * not used, but it is passed in case it may be useful.
419 */
420static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500421pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600422{
423 PNG_CONST char *name = "UNKNOWN (ERROR!)";
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600424 char *test;
425 test = png_get_error_ptr(png_ptr);
426 if (test == NULL)
427 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
428 else
429 fprintf(STDERR, "%s: libpng warning: %s\n", test, message);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600430}
431
432/* This is the default error handling function. Note that replacements for
433 * this function MUST NOT RETURN, or the program will likely crash. This
434 * function is used by default, or if the program supplies NULL for the
435 * error function pointer in png_set_error_fn().
436 */
437static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500438pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600439{
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500440 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500441 /* We can return because png_error calls the default handler, which is
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500442 * actually OK in this case.
443 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600444}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500445#endif /* !PNG_STDIO_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600446/* END of code to validate stdio-free compilation */
447
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600448/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500449#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600450
451/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500452 * 64K. However, zlib may allocate more then 64K if you don't tell
453 * it not to. See zconf.h and png.h for more information. zlib does
454 * need to allocate exactly 64K, so whatever you call here must
455 * have the ability to do that.
456 *
457 * This piece of code can be compiled to validate max 64K allocations
458 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
459 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600460typedef struct memory_information
461{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500462 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500463 png_voidp pointer;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600464 struct memory_information FAR *next;
465} memory_information;
466typedef memory_information FAR *memory_infop;
467
468static memory_infop pinformation = NULL;
469static int current_allocation = 0;
470static int maximum_allocation = 0;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500471static int total_allocation = 0;
472static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600473
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500474png_voidp png_debug_malloc
475 PNGARG((png_structp png_ptr, png_alloc_size_t size));
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500476void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600477
478png_voidp
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500479png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600480{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500481
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500482 /* png_malloc has already tested for NULL; png_create_struct calls
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500483 * png_debug_malloc directly, with png_ptr == NULL which is OK
484 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500485
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600486 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500487 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600488
489 /* This calls the library allocator twice, once to get the requested
490 buffer and once to get a new free list entry. */
491 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500492 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500493 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500494 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500495 pinfo = (memory_infop)png_malloc(png_ptr,
496 png_sizeof(*pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600497 pinfo->size = size;
498 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500499 total_allocation += size;
500 num_allocations ++;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600501 if (current_allocation > maximum_allocation)
502 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500503 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500504 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500505 png_set_mem_fn(png_ptr,
506 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500507 if (size != 0 && pinfo->pointer == NULL)
508 {
509 current_allocation -= size;
510 total_allocation -= size;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500511 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500512 "out of memory in pngtest->png_debug_malloc");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500513 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600514 pinfo->next = pinformation;
515 pinformation = pinfo;
516 /* Make sure the caller isn't assuming zeroed memory. */
517 png_memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500518 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600519 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500520 pinfo->pointer);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600521 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600522 }
523}
524
525/* Free a pointer. It is removed from the list at the same time. */
526void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500527png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600528{
529 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500530 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600531 if (ptr == 0)
532 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600533#if 0 /* This happens all the time. */
534 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
535#endif
536 return;
537 }
538
539 /* Unlink the element from the list. */
540 {
541 memory_infop FAR *ppinfo = &pinformation;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600542 for (;;)
543 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600544 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600545 if (pinfo->pointer == ptr)
546 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600547 *ppinfo = pinfo->next;
548 current_allocation -= pinfo->size;
549 if (current_allocation < 0)
550 fprintf(STDERR, "Duplicate free of memory\n");
551 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500552 the memory that is to be freed. */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500553 png_memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500554 png_free_default(png_ptr, pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500555 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600556 break;
557 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600558 if (pinfo->next == NULL)
559 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500560 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600561 break;
562 }
563 ppinfo = &pinfo->next;
564 }
565 }
566
567 /* Finally free the data. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500568 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600569 printf("Freeing %p\n", ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500570 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500571 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600572}
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500573#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600574/* END of code to test memory allocation/deallocation */
575
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500576
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500577/* Demonstration of user chunk support of the sTER and vpAg chunks */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500578#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500579
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500580/* (sTER is a public chunk not yet known by libpng. vpAg is a private
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500581chunk used in ImageMagick to store "virtual page" size). */
582
583static png_uint_32 user_chunk_data[4];
584
Glenn Randers-Pehrsone826d7e2006-07-03 00:21:58 -0500585 /* 0: sTER mode + 1
586 * 1: vpAg width
587 * 2: vpAg height
588 * 3: vpAg units
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500589 */
590
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600591static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr,
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500592 png_unknown_chunkp chunk)
593{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500594 png_uint_32
595 *my_user_chunk_data;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500596
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500597 /* Return one of the following:
598 * return (-n); chunk had an error
599 * return (0); did not recognize
600 * return (n); success
601 *
602 * The unknown chunk structure contains the chunk data:
603 * png_byte name[5];
604 * png_byte *data;
605 * png_size_t size;
606 *
607 * Note that libpng has already taken care of the CRC handling.
608 */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500609
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500610 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
611 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
612 {
613 /* Found sTER chunk */
614 if (chunk->size != 1)
615 return (-1); /* Error return */
616 if (chunk->data[0] != 0 && chunk->data[0] != 1)
617 return (-1); /* Invalid mode */
618 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
619 my_user_chunk_data[0]=chunk->data[0]+1;
620 return (1);
621 }
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500622
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500623 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
624 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
625 return (0); /* Did not recognize */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500626
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500627 /* Found ImageMagick vpAg chunk */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500628
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500629 if (chunk->size != 9)
630 return (-1); /* Error return */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500631
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500632 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500633
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500634 my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);
635 my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);
636 my_user_chunk_data[3]=(png_uint_32)chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500637
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500638 return (1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500639
640}
641#endif
642/* END of code to demonstrate user chunk support */
643
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600644/* Test one file */
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600645int
646test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500647{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500648 static png_FILE_p fpin;
649 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500650 png_structp read_ptr;
651 png_infop read_info_ptr, end_info_ptr;
652#ifdef PNG_WRITE_SUPPORTED
653 png_structp write_ptr;
654 png_infop write_info_ptr;
655 png_infop write_end_info_ptr;
656#else
657 png_structp write_ptr = NULL;
658 png_infop write_info_ptr = NULL;
659 png_infop write_end_info_ptr = NULL;
660#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600661 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500662 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663 png_uint_32 width, height;
664 int num_pass, pass;
665 int bit_depth, color_type;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600666#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600667#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600668 jmp_buf png_jmpbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600669#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600670#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600671
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600672 char inbuf[256], outbuf[256];
673
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500674 row_buf = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500675
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600677 {
678 fprintf(STDERR, "Could not find input file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600679 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600680 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500681
Andreas Dilger47a0c421997-05-16 02:46:07 -0500682 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600683 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500684 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500685 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600686 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600687 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500688
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600689 pngtest_debug("Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500690#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500691 read_ptr =
692 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
693 NULL, NULL, NULL,
694 (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500695#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500696 read_ptr =
697 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500698#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500699#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500700 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
701 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600702#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500703
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500704#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500705 user_chunk_data[0] = 0;
706 user_chunk_data[1] = 0;
707 user_chunk_data[2] = 0;
708 user_chunk_data[3] = 0;
709 png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
710 read_user_chunk_callback);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500711
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500712#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500713#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500714#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500715 write_ptr =
716 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
717 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500718#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500719 write_ptr =
720 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500721#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500722#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500723 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
724 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600725#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500726#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600727 pngtest_debug("Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500729 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500730#ifdef PNG_WRITE_SUPPORTED
731 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500732 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500733#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500734
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600735#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600736 pngtest_debug("Setting jmpbuf for read struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600737#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600738 if (setjmp(png_jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600739#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600740 if (setjmp(png_jmpbuf(read_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600741#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600742 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600743 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500744 png_free(read_ptr, row_buf);
745 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500747#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500748 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500749 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500750#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500751 FCLOSE(fpin);
752 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600753 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600754 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600755#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600756 png_memcpy(png_jmpbuf(read_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600757#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500759#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600760 pngtest_debug("Setting jmpbuf for write struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600761#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600762 if (setjmp(png_jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600763#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600764 if (setjmp(png_jmpbuf(write_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600765#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600766 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600767 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500768 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500769 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500770#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500771 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500772#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500773 FCLOSE(fpin);
774 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600775 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600776 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600777#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600778 png_memcpy(png_jmpbuf(write_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600779#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600780#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500781#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600782
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600783 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500784#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500785 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500786# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500787 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500788# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600789#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500790 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500791# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500792 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500793# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500794 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500795# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600796 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500797# endif
798# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600799#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500800 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600801 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500802#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600803 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500804#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600805 png_set_read_status_fn(read_ptr, read_row_callback);
806 }
807 else
808 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500809#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500810 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500811#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500812 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600813 }
814
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500815#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500816 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500817 int i;
818 for (i = 0; i<256; i++)
819 filters_used[i] = 0;
820 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500821 }
822#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500823#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500824 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500825 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
826#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500827
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500828#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500829# ifndef PNG_HANDLE_CHUNK_ALWAYS
830# define PNG_HANDLE_CHUNK_ALWAYS 3
831# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500832 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
833 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600834#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500835#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500836# ifndef PNG_HANDLE_CHUNK_IF_SAFE
837# define PNG_HANDLE_CHUNK_IF_SAFE 2
838# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500839 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
840 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841#endif
842
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600843 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500845
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600846 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 {
848 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500849
Andreas Dilger47a0c421997-05-16 02:46:07 -0500850 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
851 &color_type, &interlace_type, &compression_type, &filter_type))
852 {
853 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500854#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600856#else
857 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
858#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500859 }
860 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500861#ifdef PNG_FIXED_POINT_SUPPORTED
862#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600864 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600865 blue_y;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600866 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
867 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600869 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500870 red_y, green_x, green_y, blue_x, blue_y);
871 }
872 }
873#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500874#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500875 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600876 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600879 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 }
881#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500882#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500883#ifdef PNG_FLOATING_POINT_SUPPORTED
884#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500885 {
886 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
887 blue_y;
888 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
889 &red_y, &green_x, &green_y, &blue_x, &blue_y))
890 {
891 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
892 red_y, green_x, green_y, blue_x, blue_y);
893 }
894 }
895#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500896#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500897 {
898 double gamma;
899
900 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500901 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500902 }
903#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500904#endif /* Floating point */
905#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500906#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600907 {
908 png_charp name;
909 png_charp profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600910 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600911 int compression_type;
912
913 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
914 &profile, &proflen))
915 {
916 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
917 profile, proflen);
918 }
919 }
920#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600922 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600923 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600924
925 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600926 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600927 }
928#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600929 {
930 png_colorp palette;
931 int num_palette;
932
933 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600934 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600935 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500936#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937 {
938 png_color_16p background;
939
940 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
941 {
942 png_set_bKGD(write_ptr, write_info_ptr, background);
943 }
944 }
945#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500946#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 {
948 png_uint_16p hist;
949
950 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500952 }
953#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500954#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500955 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600956 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500957 int unit_type;
958
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500959 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500960 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500961 {
962 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
963 }
964 }
965#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500966#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500967 {
968 png_charp purpose, units;
969 png_charpp params;
970 png_int_32 X0, X1;
971 int type, nparams;
972
973 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
974 &nparams, &units, &params))
975 {
976 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
977 nparams, units, params);
978 }
979 }
980#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500981#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500982 {
983 png_uint_32 res_x, res_y;
984 int unit_type;
985
986 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500988 }
989#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500990#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 {
992 png_color_8p sig_bit;
993
994 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500995 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 }
997#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -0500998#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600999#ifdef PNG_FLOATING_POINT_SUPPORTED
1000 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001002 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001004 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1005 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001007 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008 }
1009 }
1010#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001011#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001012 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001014 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001016 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1017 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001018 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001019 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1020 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001021 }
1022 }
1023#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001024#endif
1025#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001026#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027 {
1028 png_textp text_ptr;
1029 int num_text;
1030
1031 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1032 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001033 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001034 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1035 }
1036 }
1037#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001038#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039 {
1040 png_timep mod_time;
1041
1042 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1043 {
1044 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001045#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001046 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001047 * pointed to by png_convert_to_rfc1123() gets free'ed before
1048 * we use it.
1049 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001050 png_memcpy(tIME_string,
1051 png_convert_to_rfc1123(read_ptr, mod_time),
1052 png_sizeof(tIME_string));
1053 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001054 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001055#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001056 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057 }
1058#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001059#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001061 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001062 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001063 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001065 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001066 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001068 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001069 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001070 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001072 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073 ((int)trans_color->red > sample_max ||
1074 (int)trans_color->green > sample_max ||
1075 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001076 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001077 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078 }
1079 }
1080#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001081#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001082 {
1083 png_unknown_chunkp unknowns;
1084 int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
1085 &unknowns);
1086 if (num_unknowns)
1087 {
1088 png_size_t i;
1089 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1090 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001091 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001092 * generated locations in write_info_ptr are wrong because we
1093 * haven't written anything yet.
1094 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001095 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001096 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1097 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001098 }
1099 }
1100#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001101
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001102#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001103 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001104
1105/* If we wanted, we could write info in two steps:
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001106 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001107 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001109
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001110#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001111 if (user_chunk_data[0] != 0)
1112 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001113 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 unsigned char
1116 ster_chunk_data[1];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 if (verbose)
1119 fprintf(STDERR, "\n stereo mode = %lu\n",
1120 (unsigned long)(user_chunk_data[0] - 1));
1121 ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
1122 png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001123 }
1124 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1125 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001126 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001127
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128 unsigned char
1129 vpag_chunk_data[9];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001130
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001131 if (verbose)
1132 fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
1133 (unsigned long)user_chunk_data[1],
1134 (unsigned long)user_chunk_data[2],
1135 (unsigned long)user_chunk_data[3]);
1136 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1137 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1138 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1139 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001140 }
1141
1142#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001143#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001145#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001146 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001147 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001148 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001149 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001150#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001151 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001152
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001153#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1154 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001156# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001158# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001159#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001160 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001161#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001162
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001163#ifdef PNGTEST_TIMING
1164 t_stop = (float)clock();
1165 t_misc += (t_stop - t_start);
1166 t_start = t_stop;
1167#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001168 for (pass = 0; pass < num_pass; pass++)
1169 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001170 pngtest_debug1("Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001171 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001172 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001173#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001174 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001175 row_buf = (png_bytep)png_malloc(read_ptr,
1176 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001177 pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001178 png_get_rowbytes(read_ptr, read_info_ptr));
1179#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001180 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001181
1182#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001183#ifdef PNGTEST_TIMING
1184 t_stop = (float)clock();
1185 t_decode += (t_stop - t_start);
1186 t_start = t_stop;
1187#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001188 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001189#ifdef PNGTEST_TIMING
1190 t_stop = (float)clock();
1191 t_encode += (t_stop - t_start);
1192 t_start = t_stop;
1193#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001194#endif /* PNG_WRITE_SUPPORTED */
1195
1196#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001197 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001198 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001199 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001200#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001201 }
1202 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001203
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001204#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001205 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001206#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001207#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001208 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001209#endif
1210
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001211 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001212
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001214#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001215 {
1216 png_textp text_ptr;
1217 int num_text;
1218
1219 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1220 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001221 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001222 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1223 }
1224 }
1225#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001226#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001227 {
1228 png_timep mod_time;
1229
1230 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1231 {
1232 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001233#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001234 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001235 pointed to by png_convert_to_rfc1123() gets free'ed before
1236 we use it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001237 png_memcpy(tIME_string,
1238 png_convert_to_rfc1123(read_ptr, mod_time),
1239 png_sizeof(tIME_string));
1240 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001241 tIME_chunk_present++;
1242#endif /* PNG_TIME_RFC1123_SUPPORTED */
1243 }
1244 }
1245#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001246#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001247 {
1248 png_unknown_chunkp unknowns;
1249 int num_unknowns;
1250 num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1251 &unknowns);
1252 if (num_unknowns)
1253 {
1254 png_size_t i;
1255 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1256 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001257 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001258 * generated locations in write_end_info_ptr are wrong because we
1259 * haven't written the end_info yet.
1260 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001261 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001262 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1263 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001264 }
1265 }
1266#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001267#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001268 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001269#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001270
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001271#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001272 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001273 {
1274 png_uint_32 iwidth, iheight;
1275 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1276 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001277 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001278 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001279 }
1280#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001281
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001282 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001283#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001284 pngtest_debug("destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001285 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001286 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001287#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001288 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001290#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001291 pngtest_debug("destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001292 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001293 pngtest_debug("destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001295#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001296 pngtest_debug("Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001297
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001298 FCLOSE(fpin);
1299 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001300
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001301 pngtest_debug("Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001303 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001304 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001305 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001306 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001307
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001309 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001310 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001311 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001312 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001313 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001315 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001316 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001317 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001319 num_in = fread(inbuf, 1, 1, fpin);
1320 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001321
Guy Schalnat0f716451995-11-28 11:22:13 -06001322 if (num_in != num_out)
1323 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001324 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Guy Schalnate5a37791996-06-05 15:50:50 -05001325 inname, outname);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001326 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001327 {
1328 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001329 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001330 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001331 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001332 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001333 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001334 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001335 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001336 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001337 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001338 FCLOSE(fpin);
1339 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001340 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001341 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001342
Guy Schalnat0f716451995-11-28 11:22:13 -06001343 if (!num_in)
1344 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001345
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 if (png_memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001347 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001348 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001349 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001350 {
1351 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001352 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001353 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001354 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001356 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001357 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001358 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001359 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001360 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001361 FCLOSE(fpin);
1362 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001363 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001364 }
1365 }
1366
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001367 FCLOSE(fpin);
1368 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001369
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001370 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001371}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001372
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001373/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001374#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001375static PNG_CONST char *inname = "pngtest/png";
1376static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001377#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001378static PNG_CONST char *inname = "pngtest.png";
1379static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001380#endif
1381
1382int
1383main(int argc, char *argv[])
1384{
1385 int multiple = 0;
1386 int ierror = 0;
1387
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001388 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001389 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001390 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001391 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001392 fprintf(STDERR, " library (%lu):%s",
1393 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001394 png_get_header_version(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001395 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001396 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001397 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001398
1399 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001400 * not sure this matters, but it is nice to know, the first of these
1401 * tests should be impossible because of the way the macros are set
1402 * in pngconf.h
1403 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001404#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001405 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001406#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001407 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001408#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001409 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001410#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001411
1412 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1413 {
1414 fprintf(STDERR,
1415 "Warning: versions are different between png.h and png.c\n");
1416 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1417 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1418 ++ierror;
1419 }
1420
1421 if (argc > 1)
1422 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001423 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001424 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001425 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001426 status_dots_requested = 0;
1427 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001428 else if (strcmp(argv[1], "-mv") == 0 ||
1429 strcmp(argv[1], "-vm") == 0 )
1430 {
1431 multiple = 1;
1432 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001433 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001434 }
1435 else if (strcmp(argv[1], "-v") == 0)
1436 {
1437 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001438 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001439 inname = argv[2];
1440 }
1441 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001442 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001443 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001444 status_dots_requested = 0;
1445 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001446 }
1447
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001448 if (!multiple && argc == 3 + verbose)
1449 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001450
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001451 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001452 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001453 fprintf(STDERR,
1454 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001455 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001456 fprintf(STDERR,
1457 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1458 fprintf(STDERR,
1459 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001460 exit(1);
1461 }
1462
1463 if (multiple)
1464 {
1465 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001466#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001467 int allocation_now = current_allocation;
1468#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001469 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001470 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001471 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001472 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001473 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001474 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001475 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001476#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1477 int k;
1478#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001479#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001480 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001481 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001482#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001483 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001484#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001485#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001486 for (k = 0; k<256; k++)
1487 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001488 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001489 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001490#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001491#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001492 if (tIME_chunk_present != 0)
1493 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001494 tIME_chunk_present = 0;
1495#endif /* PNG_TIME_RFC1123_SUPPORTED */
1496 }
1497 else
1498 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001499 fprintf(STDERR, " FAIL\n");
1500 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001501 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001502#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001503 if (allocation_now != current_allocation)
1504 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001505 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506 if (current_allocation != 0)
1507 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001508 memory_infop pinfo = pinformation;
1509
1510 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1511 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001512 while (pinfo != NULL)
1513 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001514 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001515 (unsigned long)pinfo->size,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001516 (unsigned int) pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001517 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001518 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001519 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001520#endif
1521 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001522#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001523 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001524 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001525 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001526 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001527 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1528 total_allocation);
1529 fprintf(STDERR, " Number of allocations: %10d\n",
1530 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001531#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001532 }
1533 else
1534 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001535 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001536 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001537 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001538 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001539#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001540 int allocation_now = current_allocation;
1541#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001542 if (i == 1) status_dots_requested = 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001543 else if (verbose == 0)status_dots_requested = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001544 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001545 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001546 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001547 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001548 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001549 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001550 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001551#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001552 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001553#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001554#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001555 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001556 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001557#else
1558 fprintf(STDERR, " PASS\n");
1559#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001560#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001561 for (k = 0; k<256; k++)
1562 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001563 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001564 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001565#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001566#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001567 if (tIME_chunk_present != 0)
1568 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001569#endif /* PNG_TIME_RFC1123_SUPPORTED */
1570 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001571 }
1572 else
1573 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001574 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001575 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001576 fprintf(STDERR, " FAIL\n");
1577 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001578 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001579#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001580 if (allocation_now != current_allocation)
1581 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001582 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001583 if (current_allocation != 0)
1584 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001585 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001586
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001587 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1588 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001589 while (pinfo != NULL)
1590 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001591 fprintf(STDERR, " %lu bytes at %x\n",
1592 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001593 pinfo = pinfo->next;
1594 }
1595 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001596#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001597 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001598#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001599 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001600 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001601 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001602 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001603 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1604 total_allocation);
1605 fprintf(STDERR, " Number of allocations: %10d\n",
1606 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001607#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001608 }
1609
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001610#ifdef PNGTEST_TIMING
1611 t_stop = (float)clock();
1612 t_misc += (t_stop - t_start);
1613 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001614 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001615 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001616 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001617 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001618 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001619 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001620 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001621 t_misc/(float)CLOCKS_PER_SEC);
1622#endif
1623
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001624 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001625 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001626 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001627 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001628 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001629}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001630
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001631/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson2f7cbab2010-03-10 09:10:16 -06001632typedef version_1_5_0beta14 your_png_h_is_not_version_1_5_0beta14;