blob: c472e0686fb9d5c585225a9ee9e6b655da7496a3 [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-Pehrsond7da8bb2010-03-13 20:30:10 -06004 * Last changed in libpng 1.5.0 [March 14, 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
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600271static void PNGCBAPI
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-Pehrsond7da8bb2010-03-13 20:30:10 -0600300static void PNGCBAPI
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-Pehrsond7da8bb2010-03-13 20:30:10 -0600340static void PNGCBAPI
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
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600354static void PNGCBAPI
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
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600374static void PNGCBAPI
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 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600420static void PNGCBAPI
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 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600437static void PNGCBAPI
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-Pehrsond7da8bb2010-03-13 20:30:10 -0600474png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
475 png_alloc_size_t size));
476void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600477
478png_voidp
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600479PNGCBAPI png_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. */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600526void PNGCBAPI
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,
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600693 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500694#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500695 read_ptr =
696 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500697#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500698#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500699 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
700 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600701#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500702
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500703#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 user_chunk_data[0] = 0;
705 user_chunk_data[1] = 0;
706 user_chunk_data[2] = 0;
707 user_chunk_data[3] = 0;
708 png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
709 read_user_chunk_callback);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500710
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500711#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500712#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500713#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500714 write_ptr =
715 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
716 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500717#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500718 write_ptr =
719 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500720#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500721#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500722 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
723 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600724#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500725#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600726 pngtest_debug("Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500728 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500729#ifdef PNG_WRITE_SUPPORTED
730 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500731 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500732#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500733
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600734#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600735 pngtest_debug("Setting jmpbuf for read struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600736#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600737 if (setjmp(png_jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600738#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600739 if (setjmp(png_jmpbuf(read_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600740#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600741 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600742 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500743 png_free(read_ptr, row_buf);
744 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500745 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500746#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500747 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500749#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 FCLOSE(fpin);
751 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600752 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600753 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600754#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600755 png_memcpy(png_jmpbuf(read_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600756#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500757
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500758#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600759 pngtest_debug("Setting jmpbuf for write struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600760#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600761 if (setjmp(png_jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600762#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600763 if (setjmp(png_jmpbuf(write_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600764#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600765 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600766 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500767 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500768 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500769#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500771#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500772 FCLOSE(fpin);
773 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600774 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600775 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600776#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600777 png_memcpy(png_jmpbuf(write_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600778#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600779#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500780#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600781
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600782 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500783#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500784 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500785# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500786 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500787# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600788#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500789 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500790# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500791 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500792# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500793 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500794# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600795 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500796# endif
797# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600798#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500799 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600800 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500801#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600802 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500803#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600804 png_set_read_status_fn(read_ptr, read_row_callback);
805 }
806 else
807 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500808#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500809 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500810#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500811 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600812 }
813
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500814#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500815 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500816 int i;
817 for (i = 0; i<256; i++)
818 filters_used[i] = 0;
819 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500820 }
821#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500822#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500824 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
825#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500826
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500827#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500828# ifndef PNG_HANDLE_CHUNK_ALWAYS
829# define PNG_HANDLE_CHUNK_ALWAYS 3
830# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500831 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
832 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600833#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500834#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500835# ifndef PNG_HANDLE_CHUNK_IF_SAFE
836# define PNG_HANDLE_CHUNK_IF_SAFE 2
837# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500838 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
839 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600840#endif
841
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600842 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500844
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600845 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500846 {
847 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500848
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
850 &color_type, &interlace_type, &compression_type, &filter_type))
851 {
852 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500853#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500854 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600855#else
856 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
857#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 }
859 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500860#ifdef PNG_FIXED_POINT_SUPPORTED
861#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500862 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600863 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 -0600864 blue_y;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600865 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
866 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600868 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869 red_y, green_x, green_y, blue_x, blue_y);
870 }
871 }
872#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500873#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500874 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600875 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600877 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879 }
880#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500881#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500882#ifdef PNG_FLOATING_POINT_SUPPORTED
883#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500884 {
885 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
886 blue_y;
887 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
888 &red_y, &green_x, &green_y, &blue_x, &blue_y))
889 {
890 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
891 red_y, green_x, green_y, blue_x, blue_y);
892 }
893 }
894#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500895#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500896 {
897 double gamma;
898
899 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500900 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500901 }
902#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500903#endif /* Floating point */
904#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600906 {
907 png_charp name;
908 png_charp profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600909 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600910 int compression_type;
911
912 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
913 &profile, &proflen))
914 {
915 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
916 profile, proflen);
917 }
918 }
919#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500920#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600921 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600922 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600923
924 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600925 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600926 }
927#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600928 {
929 png_colorp palette;
930 int num_palette;
931
932 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600933 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600934 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500935#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600936 {
937 png_color_16p background;
938
939 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
940 {
941 png_set_bKGD(write_ptr, write_info_ptr, background);
942 }
943 }
944#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500945#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500946 {
947 png_uint_16p hist;
948
949 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500950 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 }
952#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500953#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600955 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500956 int unit_type;
957
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500958 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500959 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500960 {
961 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
962 }
963 }
964#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500965#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500966 {
967 png_charp purpose, units;
968 png_charpp params;
969 png_int_32 X0, X1;
970 int type, nparams;
971
972 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
973 &nparams, &units, &params))
974 {
975 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
976 nparams, units, params);
977 }
978 }
979#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500980#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500981 {
982 png_uint_32 res_x, res_y;
983 int unit_type;
984
985 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 }
988#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500989#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 {
991 png_color_8p sig_bit;
992
993 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500994 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500995 }
996#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -0500997#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998#ifdef PNG_FLOATING_POINT_SUPPORTED
999 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001000 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001001 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001003 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1004 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001006 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007 }
1008 }
1009#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001010#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001012 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001013 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001015 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1016 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001018 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1019 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001020 }
1021 }
1022#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001023#endif
1024#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001025#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 {
1027 png_textp text_ptr;
1028 int num_text;
1029
1030 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1031 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001032 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001033 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1034 }
1035 }
1036#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001037#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 {
1039 png_timep mod_time;
1040
1041 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1042 {
1043 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001044#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001045 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001046 * pointed to by png_convert_to_rfc1123() gets free'ed before
1047 * we use it.
1048 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001049 png_memcpy(tIME_string,
1050 png_convert_to_rfc1123(read_ptr, mod_time),
1051 png_sizeof(tIME_string));
1052 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001053 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001054#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001055 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001056 }
1057#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001058#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001059 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001060 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001062 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001063
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001064 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001065 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001066 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001067 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001068 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001069 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001070 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001071 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001072 ((int)trans_color->red > sample_max ||
1073 (int)trans_color->green > sample_max ||
1074 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001075 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001076 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001077 }
1078 }
1079#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001080#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081 {
1082 png_unknown_chunkp unknowns;
1083 int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
1084 &unknowns);
1085 if (num_unknowns)
1086 {
1087 png_size_t i;
1088 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1089 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001090 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091 * generated locations in write_info_ptr are wrong because we
1092 * haven't written anything yet.
1093 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001094 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001095 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1096 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001097 }
1098 }
1099#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001101#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001102 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001103
1104/* If we wanted, we could write info in two steps:
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001105 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001106 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001108
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001109#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001110 if (user_chunk_data[0] != 0)
1111 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001112 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001113
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001114 unsigned char
1115 ster_chunk_data[1];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001116
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001117 if (verbose)
1118 fprintf(STDERR, "\n stereo mode = %lu\n",
1119 (unsigned long)(user_chunk_data[0] - 1));
1120 ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
1121 png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001122 }
1123 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1124 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001125 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001126
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001127 unsigned char
1128 vpag_chunk_data[9];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001129
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001130 if (verbose)
1131 fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
1132 (unsigned long)user_chunk_data[1],
1133 (unsigned long)user_chunk_data[2],
1134 (unsigned long)user_chunk_data[3]);
1135 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1136 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1137 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1138 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001139 }
1140
1141#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001142#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001144#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001145 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001146 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001148 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001149#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001150 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001151
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001152#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1153 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001155# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001156 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001157# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001158#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001159 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001160#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001161
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001162#ifdef PNGTEST_TIMING
1163 t_stop = (float)clock();
1164 t_misc += (t_stop - t_start);
1165 t_start = t_stop;
1166#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001167 for (pass = 0; pass < num_pass; pass++)
1168 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001169 pngtest_debug1("Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001170 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001171 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001172#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001173 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001174 row_buf = (png_bytep)png_malloc(read_ptr,
1175 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001176 pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001177 png_get_rowbytes(read_ptr, read_info_ptr));
1178#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001179 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001180
1181#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001182#ifdef PNGTEST_TIMING
1183 t_stop = (float)clock();
1184 t_decode += (t_stop - t_start);
1185 t_start = t_stop;
1186#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001187 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001188#ifdef PNGTEST_TIMING
1189 t_stop = (float)clock();
1190 t_encode += (t_stop - t_start);
1191 t_start = t_stop;
1192#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001193#endif /* PNG_WRITE_SUPPORTED */
1194
1195#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001196 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001197 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001198 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001199#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001200 }
1201 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001202
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001203#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001204 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001205#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001206#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001207 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001208#endif
1209
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001210 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001211
Andreas Dilger47a0c421997-05-16 02:46:07 -05001212 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001213#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001214 {
1215 png_textp text_ptr;
1216 int num_text;
1217
1218 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1219 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001220 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001221 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1222 }
1223 }
1224#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001225#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001226 {
1227 png_timep mod_time;
1228
1229 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1230 {
1231 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001232#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001233 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001234 pointed to by png_convert_to_rfc1123() gets free'ed before
1235 we use it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001236 png_memcpy(tIME_string,
1237 png_convert_to_rfc1123(read_ptr, mod_time),
1238 png_sizeof(tIME_string));
1239 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001240 tIME_chunk_present++;
1241#endif /* PNG_TIME_RFC1123_SUPPORTED */
1242 }
1243 }
1244#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001245#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001246 {
1247 png_unknown_chunkp unknowns;
1248 int num_unknowns;
1249 num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1250 &unknowns);
1251 if (num_unknowns)
1252 {
1253 png_size_t i;
1254 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1255 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001256 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001257 * generated locations in write_end_info_ptr are wrong because we
1258 * haven't written the end_info yet.
1259 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001260 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001261 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1262 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001263 }
1264 }
1265#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001266#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001267 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001268#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001269
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001270#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001271 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001272 {
1273 png_uint_32 iwidth, iheight;
1274 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1275 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001276 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001277 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001278 }
1279#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001280
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001281 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001282#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001283 pngtest_debug("destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001284 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001285 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001286#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001287 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001288 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001289#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001290 pngtest_debug("destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001291 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001292 pngtest_debug("destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001294#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001295 pngtest_debug("Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001296
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001297 FCLOSE(fpin);
1298 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001299
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001300 pngtest_debug("Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001302 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001303 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001304 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001305 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001306
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001308 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001309 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001310 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001311 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001312 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001314 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001315 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -05001317
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001318 num_in = fread(inbuf, 1, 1, fpin);
1319 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001320
Guy Schalnat0f716451995-11-28 11:22:13 -06001321 if (num_in != num_out)
1322 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001323 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Guy Schalnate5a37791996-06-05 15:50:50 -05001324 inname, outname);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001325 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001326 {
1327 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001328 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001329 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001330 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001331 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001332 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001333 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001334 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001335 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001336 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001337 FCLOSE(fpin);
1338 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001339 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001340 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001341
Guy Schalnat0f716451995-11-28 11:22:13 -06001342 if (!num_in)
1343 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001344
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345 if (png_memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001346 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001347 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001348 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001349 {
1350 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001351 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001352 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001353 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001354 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001355 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001356 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001357 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001358 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001359 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001360 FCLOSE(fpin);
1361 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001362 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001363 }
1364 }
1365
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001366 FCLOSE(fpin);
1367 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001368
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001369 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001370}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001371
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001372/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001373#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001374static PNG_CONST char *inname = "pngtest/png";
1375static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001376#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001377static PNG_CONST char *inname = "pngtest.png";
1378static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001379#endif
1380
1381int
1382main(int argc, char *argv[])
1383{
1384 int multiple = 0;
1385 int ierror = 0;
1386
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001387 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001388 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001389 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001390 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001391 fprintf(STDERR, " library (%lu):%s",
1392 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001393 png_get_header_version(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001394 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001395 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001396 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001397
1398 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001399 * not sure this matters, but it is nice to know, the first of these
1400 * tests should be impossible because of the way the macros are set
1401 * in pngconf.h
1402 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001403#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001404 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001405#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001406 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001407#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001408 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001409#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001410
1411 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1412 {
1413 fprintf(STDERR,
1414 "Warning: versions are different between png.h and png.c\n");
1415 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1416 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1417 ++ierror;
1418 }
1419
1420 if (argc > 1)
1421 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001422 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001423 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001424 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001425 status_dots_requested = 0;
1426 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001427 else if (strcmp(argv[1], "-mv") == 0 ||
1428 strcmp(argv[1], "-vm") == 0 )
1429 {
1430 multiple = 1;
1431 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001432 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001433 }
1434 else if (strcmp(argv[1], "-v") == 0)
1435 {
1436 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001437 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001438 inname = argv[2];
1439 }
1440 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001441 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001442 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001443 status_dots_requested = 0;
1444 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001445 }
1446
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001447 if (!multiple && argc == 3 + verbose)
1448 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001449
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001450 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001451 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001452 fprintf(STDERR,
1453 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001454 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001455 fprintf(STDERR,
1456 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1457 fprintf(STDERR,
1458 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001459 exit(1);
1460 }
1461
1462 if (multiple)
1463 {
1464 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001465#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001466 int allocation_now = current_allocation;
1467#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001468 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001469 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001470 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001471 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001472 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001473 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001474 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001475#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1476 int k;
1477#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001478#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001479 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001480 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001481#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001482 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001483#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001484#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001485 for (k = 0; k<256; k++)
1486 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001487 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001488 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001489#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001490#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001491 if (tIME_chunk_present != 0)
1492 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001493 tIME_chunk_present = 0;
1494#endif /* PNG_TIME_RFC1123_SUPPORTED */
1495 }
1496 else
1497 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001498 fprintf(STDERR, " FAIL\n");
1499 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001500 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001501#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001502 if (allocation_now != current_allocation)
1503 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001504 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505 if (current_allocation != 0)
1506 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001507 memory_infop pinfo = pinformation;
1508
1509 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1510 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001511 while (pinfo != NULL)
1512 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001513 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001514 (unsigned long)pinfo->size,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001515 (unsigned int) pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001516 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001517 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001518 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001519#endif
1520 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001521#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001522 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001523 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001524 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001525 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001526 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1527 total_allocation);
1528 fprintf(STDERR, " Number of allocations: %10d\n",
1529 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001530#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001531 }
1532 else
1533 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001534 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001535 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001536 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001537 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001538#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001539 int allocation_now = current_allocation;
1540#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001541 if (i == 1) status_dots_requested = 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001542 else if (verbose == 0)status_dots_requested = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001543 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001544 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001545 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001546 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001547 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001548 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001549 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001550#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001551 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001552#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001553#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001554 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001555 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001556#else
1557 fprintf(STDERR, " PASS\n");
1558#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001559#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001560 for (k = 0; k<256; k++)
1561 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001562 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001563 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001564#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001565#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001566 if (tIME_chunk_present != 0)
1567 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001568#endif /* PNG_TIME_RFC1123_SUPPORTED */
1569 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001570 }
1571 else
1572 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001573 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001574 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001575 fprintf(STDERR, " FAIL\n");
1576 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001577 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001578#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001579 if (allocation_now != current_allocation)
1580 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001581 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001582 if (current_allocation != 0)
1583 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001584 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001585
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001586 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1587 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001588 while (pinfo != NULL)
1589 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001590 fprintf(STDERR, " %lu bytes at %x\n",
1591 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001592 pinfo = pinfo->next;
1593 }
1594 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001595#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001596 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001597#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001598 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001599 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001600 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001601 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001602 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1603 total_allocation);
1604 fprintf(STDERR, " Number of allocations: %10d\n",
1605 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001606#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001607 }
1608
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001609#ifdef PNGTEST_TIMING
1610 t_stop = (float)clock();
1611 t_misc += (t_stop - t_start);
1612 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001613 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001614 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001615 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001616 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001617 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001618 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001619 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001620 t_misc/(float)CLOCKS_PER_SEC);
1621#endif
1622
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001623 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001624 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001625 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001626 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001627 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001628}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001629
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001630/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson2f7cbab2010-03-10 09:10:16 -06001631typedef version_1_5_0beta14 your_png_h_is_not_version_1_5_0beta14;