blob: a237b8e2808f9ea44be10ee87d604a54d89509a0 [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-Pehrsonbfbf8652009-06-26 21:46:52 -05004 * Last changed in libpng 1.4.0 [June 27, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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"
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050035#include "pngpriv.h"
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050036
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050037# include <stdio.h>
38# include <stdlib.h>
39# define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050040
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050041#if defined(PNG_NO_STDIO)
42 typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060043#endif
44
Andreas Dilger47a0c421997-05-16 02:46:07 -050045/* Makes pngtest verbose so we can find problems (needs to be before png.h) */
46#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050047# define PNG_DEBUG 0
48#endif
49
50#if !PNG_DEBUG
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050051# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060052#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050053
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050054/* Turn on CPU timing
55#define PNGTEST_TIMING
56*/
57
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060058#ifdef PNG_NO_FLOATING_POINT_SUPPORTED
59#undef PNGTEST_TIMING
60#endif
61
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050062#ifdef PNGTEST_TIMING
63static float t_start, t_stop, t_decode, t_encode, t_misc;
64#include <time.h>
65#endif
66
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050067#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050068#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050069static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050070static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050071#endif
72
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050073static int verbose = 0;
74
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060075int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060076
Guy Schalnat0d580581995-07-20 02:43:20 -050077#ifdef __TURBOC__
78#include <mem.h>
79#endif
80
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050081/* Defined so I can write to a file on gui/windowing platforms */
Guy Schalnat6d764711995-12-19 03:22:19 -060082/* #define STDERR stderr */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050083#define STDERR stdout /* For DOS */
Guy Schalnat0d580581995-07-20 02:43:20 -050084
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -050085/* In case a system header (e.g., on AIX) defined jmpbuf */
86#ifdef jmpbuf
87# undef jmpbuf
88#endif
89
90/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
91#ifndef png_jmpbuf
92# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
93#endif
94
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050095/* Example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050096static int status_pass = 1;
97static int status_dots_requested = 0;
98static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060099
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600100void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500101read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -0500102void
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600103read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600104{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500105 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
106 return;
107 if (status_pass != pass)
108 {
109 fprintf(stdout, "\n Pass %d: ", pass);
110 status_pass = pass;
111 status_dots = 31;
112 }
113 status_dots--;
114 if (status_dots == 0)
115 {
116 fprintf(stdout, "\n ");
117 status_dots=30;
118 }
119 fprintf(stdout, "r");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600120}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600121
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -0500122void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500123write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -0500124void
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600125write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600126{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
128 return;
129 fprintf(stdout, "w");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600130}
131
132
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500133#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
134/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500135 * but merely examine the row filters. We set this to 256 rather than
136 * 5 in case illegal filter values are present.)
137 */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500138static png_uint_32 filters_used[256];
139void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500140count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
141void
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500142count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
143{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500144 if (png_ptr != NULL && row_info != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500145 ++filters_used[*(data - 1)];
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500146}
147#endif
148
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600149#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500150/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500151 * but merely count the zero samples)
152 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600153
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500154static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600155
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600156void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500157count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
158void
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500159count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600160{
161 png_bytep dp = data;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500162 if (png_ptr == NULL)return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600163
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500164 /* Contents of row_info:
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600165 * png_uint_32 width width of row
166 * png_uint_32 rowbytes number of bytes in row
167 * png_byte color_type color type of pixels
168 * png_byte bit_depth bit depth of samples
169 * png_byte channels number of channels (1-4)
170 * png_byte pixel_depth bits per pixel (depth*channels)
171 */
172
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500173 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600174
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500175 if (row_info->color_type == 0 || row_info->color_type == 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600176 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500177 int pos = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500178 png_uint_32 n, nstop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500179 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600180 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500181 if (row_info->bit_depth == 1)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500182 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500183 if (((*dp << pos++ ) & 0x80) == 0)
184 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600186 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500187 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600188 dp++;
189 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500190 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500191 if (row_info->bit_depth == 2)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500192 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500193 if (((*dp << (pos+=2)) & 0xc0) == 0)
194 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500195 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600196 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500197 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600198 dp++;
199 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500200 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500201 if (row_info->bit_depth == 4)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500202 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500203 if (((*dp << (pos+=4)) & 0xf0) == 0)
204 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500205 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600206 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500207 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600208 dp++;
209 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500210 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500211 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500212 if (*dp++ == 0)
213 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500214 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600215 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500216 if ((*dp | *(dp+1)) == 0)
217 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600218 dp+=2;
219 }
220 }
221 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500222 else /* Other color types */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600223 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500224 png_uint_32 n, nstop;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600225 int channel;
226 int color_channels = row_info->channels;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500227 if (row_info->color_type > 3)color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600228
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500229 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600230 {
231 for (channel = 0; channel < color_channels; channel++)
232 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500233 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500234 if (*dp++ == 0)
235 zero_samples++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500236 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600237 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500238 if ((*dp | *(dp+1)) == 0)
239 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600240 dp+=2;
241 }
242 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 if (row_info->color_type > 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600244 {
245 dp++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500246 if (row_info->bit_depth == 16)
247 dp++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600248 }
249 }
250 }
251}
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600252#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600253
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600254static int wrote_question = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600255
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600256#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600257/* START of code to validate stdio-free compilation */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500258/* These copies of the default read/write functions come from pngrio.c and
259 * pngwio.c. They allow "don't include stdio" testing of the library.
260 * This is the function that does the actual reading of data. If you are
261 * not reading from a standard C stream, you should create a replacement
262 * read_data function and use it at run time with png_set_read_fn(), rather
263 * than changing the library.
264 */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600265
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600266#ifndef USE_FAR_KEYWORD
267static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500268pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600269{
270 png_size_t check;
271
272 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
273 * instead of an int, which is what fread() actually returns.
274 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500275 check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500276
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600277 if (check != length)
278 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500279 png_error(png_ptr, "Read Error!");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600280 }
281}
Guy Schalnate5a37791996-06-05 15:50:50 -0500282#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500283/* This is the model-independent version. Since the standard I/O library
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600284 can't handle far buffers in the medium and small models, we have to copy
285 the data.
286*/
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600287
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600288#define NEAR_BUF_SIZE 1024
289#define MIN(a,b) (a <= b ? a : b)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600290
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600291static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500292pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600293{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500294 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600295 png_byte *n_data;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500296 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600297
298 /* Check if data really is near. If so, use usual code. */
299 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500300 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600301 if ((png_bytep)n_data == data)
302 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500303 check = fread(n_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600304 }
305 else
306 {
307 png_byte buf[NEAR_BUF_SIZE];
308 png_size_t read, remaining, err;
309 check = 0;
310 remaining = length;
311 do
312 {
313 read = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500314 err = fread(buf, 1, 1, io_ptr);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500315 png_memcpy(data, buf, read); /* Copy far buffer to near buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500316 if (err != read)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600317 break;
318 else
319 check += err;
320 data += read;
321 remaining -= read;
322 }
323 while (remaining != 0);
324 }
325 if (check != length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600326 png_error(png_ptr, "read Error");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600327}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600328#endif /* USE_FAR_KEYWORD */
Guy Schalnat0d580581995-07-20 02:43:20 -0500329
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600330#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600331static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500332pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600333{
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500334 /* Do nothing; fflush() is said to be just a waste of energy. */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500335 png_ptr = png_ptr; /* Stifle compiler warning */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600336}
337#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500338
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500339/* 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 -0500340 * not writing to a standard C stream, you should create a replacement
341 * write_data function and use it at run time with png_set_write_fn(), rather
342 * than changing the library.
343 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600344#ifndef USE_FAR_KEYWORD
345static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500346pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600347{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600349
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500350 check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600351 if (check != length)
352 {
353 png_error(png_ptr, "Write Error");
354 }
355}
356#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500357/* This is the model-independent version. Since the standard I/O library
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600358 can't handle far buffers in the medium and small models, we have to copy
359 the data.
360*/
361
362#define NEAR_BUF_SIZE 1024
363#define MIN(a,b) (a <= b ? a : b)
364
365static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500366pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600367{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500368 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600369 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500370 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600371
372 /* Check if data really is near. If so, use usual code. */
373 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500374 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600375 if ((png_bytep)near_data == data)
376 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500377 check = fwrite(near_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600378 }
379 else
380 {
381 png_byte buf[NEAR_BUF_SIZE];
382 png_size_t written, remaining, err;
383 check = 0;
384 remaining = length;
385 do
386 {
387 written = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500388 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500389 err = fwrite(buf, 1, written, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600390 if (err != written)
391 break;
392 else
393 check += err;
394 data += written;
395 remaining -= written;
396 }
397 while (remaining != 0);
398 }
399 if (check != length)
400 {
401 png_error(png_ptr, "Write Error");
402 }
403}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600404#endif /* USE_FAR_KEYWORD */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600405
406/* This function is called when there is a warning, but the library thinks
407 * it can continue anyway. Replacement functions don't have to do anything
408 * here if you don't want to. In the default configuration, png_ptr is
409 * not used, but it is passed in case it may be useful.
410 */
411static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500412pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600413{
414 PNG_CONST char *name = "UNKNOWN (ERROR!)";
415 if (png_ptr != NULL && png_ptr->error_ptr != NULL)
416 name = png_ptr->error_ptr;
417 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
418}
419
420/* This is the default error handling function. Note that replacements for
421 * this function MUST NOT RETURN, or the program will likely crash. This
422 * function is used by default, or if the program supplies NULL for the
423 * error function pointer in png_set_error_fn().
424 */
425static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500426pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600427{
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500428 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500429 /* We can return because png_error calls the default handler, which is
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500430 * actually OK in this case.
431 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600432}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600433#endif /* PNG_NO_STDIO */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434/* END of code to validate stdio-free compilation */
435
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600436/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500437#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600438
439/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500440 * 64K. However, zlib may allocate more then 64K if you don't tell
441 * it not to. See zconf.h and png.h for more information. zlib does
442 * need to allocate exactly 64K, so whatever you call here must
443 * have the ability to do that.
444 *
445 * This piece of code can be compiled to validate max 64K allocations
446 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
447 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600448typedef struct memory_information
449{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500450 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500451 png_voidp pointer;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600452 struct memory_information FAR *next;
453} memory_information;
454typedef memory_information FAR *memory_infop;
455
456static memory_infop pinformation = NULL;
457static int current_allocation = 0;
458static int maximum_allocation = 0;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500459static int total_allocation = 0;
460static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600461
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500462png_voidp png_debug_malloc
463 PNGARG((png_structp png_ptr, png_alloc_size_t size));
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500464void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600465
466png_voidp
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500467png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600468{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500469
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500470 /* png_malloc has already tested for NULL; png_create_struct calls
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500471 * png_debug_malloc directly, with png_ptr == NULL which is OK
472 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500473
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600474 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500475 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600476
477 /* This calls the library allocator twice, once to get the requested
478 buffer and once to get a new free list entry. */
479 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500480 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500481 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500482 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500483 pinfo = (memory_infop)png_malloc(png_ptr,
484 png_sizeof(*pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600485 pinfo->size = size;
486 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500487 total_allocation += size;
488 num_allocations ++;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600489 if (current_allocation > maximum_allocation)
490 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500491 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500492 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500493 png_set_mem_fn(png_ptr,
494 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500495 if (size != 0 && pinfo->pointer == NULL)
496 {
497 current_allocation -= size;
498 total_allocation -= size;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500499 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500500 "out of memory in pngtest->png_debug_malloc");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500501 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600502 pinfo->next = pinformation;
503 pinformation = pinfo;
504 /* Make sure the caller isn't assuming zeroed memory. */
505 png_memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500506 if (verbose)
507 printf("png_malloc %lu bytes at %x\n", (unsigned long)size,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500508 pinfo->pointer);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600509 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600510 }
511}
512
513/* Free a pointer. It is removed from the list at the same time. */
514void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500515png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600516{
517 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500518 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600519 if (ptr == 0)
520 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600521#if 0 /* This happens all the time. */
522 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
523#endif
524 return;
525 }
526
527 /* Unlink the element from the list. */
528 {
529 memory_infop FAR *ppinfo = &pinformation;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600530 for (;;)
531 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600532 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600533 if (pinfo->pointer == ptr)
534 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600535 *ppinfo = pinfo->next;
536 current_allocation -= pinfo->size;
537 if (current_allocation < 0)
538 fprintf(STDERR, "Duplicate free of memory\n");
539 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500540 the memory that is to be freed. */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500541 png_memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500542 png_free_default(png_ptr, pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500543 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600544 break;
545 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600546 if (pinfo->next == NULL)
547 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500548 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600549 break;
550 }
551 ppinfo = &pinfo->next;
552 }
553 }
554
555 /* Finally free the data. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500556 if (verbose)
557 printf("Freeing %x\n", ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500558 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500559 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600560}
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500561#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600562/* END of code to test memory allocation/deallocation */
563
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500564
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500565/* Demonstration of user chunk support of the sTER and vpAg chunks */
566#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
567
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500568/* (sTER is a public chunk not yet known by libpng. vpAg is a private
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500569chunk used in ImageMagick to store "virtual page" size). */
570
571static png_uint_32 user_chunk_data[4];
572
Glenn Randers-Pehrsone826d7e2006-07-03 00:21:58 -0500573 /* 0: sTER mode + 1
574 * 1: vpAg width
575 * 2: vpAg height
576 * 3: vpAg units
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500577 */
578
579static int read_user_chunk_callback(png_struct *png_ptr,
580 png_unknown_chunkp chunk)
581{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500582 png_uint_32
583 *my_user_chunk_data;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500584
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500585 /* Return one of the following:
586 * return (-n); chunk had an error
587 * return (0); did not recognize
588 * return (n); success
589 *
590 * The unknown chunk structure contains the chunk data:
591 * png_byte name[5];
592 * png_byte *data;
593 * png_size_t size;
594 *
595 * Note that libpng has already taken care of the CRC handling.
596 */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500597
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500598 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
599 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
600 {
601 /* Found sTER chunk */
602 if (chunk->size != 1)
603 return (-1); /* Error return */
604 if (chunk->data[0] != 0 && chunk->data[0] != 1)
605 return (-1); /* Invalid mode */
606 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
607 my_user_chunk_data[0]=chunk->data[0]+1;
608 return (1);
609 }
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500610
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500611 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
612 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
613 return (0); /* Did not recognize */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500614
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500615 /* Found ImageMagick vpAg chunk */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500616
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500617 if (chunk->size != 9)
618 return (-1); /* Error return */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500619
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500620 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500621
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500622 my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);
623 my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);
624 my_user_chunk_data[3]=(png_uint_32)chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500625
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500626 return (1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500627
628}
629#endif
630/* END of code to demonstrate user chunk support */
631
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600632/* Test one file */
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600633int
634test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500635{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500636 static png_FILE_p fpin;
637 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500638 png_structp read_ptr;
639 png_infop read_info_ptr, end_info_ptr;
640#ifdef PNG_WRITE_SUPPORTED
641 png_structp write_ptr;
642 png_infop write_info_ptr;
643 png_infop write_end_info_ptr;
644#else
645 png_structp write_ptr = NULL;
646 png_infop write_info_ptr = NULL;
647 png_infop write_end_info_ptr = NULL;
648#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600649 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 png_uint_32 width, height;
652 int num_pass, pass;
653 int bit_depth, color_type;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600654#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600655#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600656 jmp_buf jmpbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600657#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600658#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600659
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600660 char inbuf[256], outbuf[256];
661
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500662 row_buf = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500663
Andreas Dilger47a0c421997-05-16 02:46:07 -0500664 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600665 {
666 fprintf(STDERR, "Could not find input file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600667 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600668 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500669
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600671 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500672 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500673 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600674 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600675 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500676
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500677 png_debug(0, "Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500678#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500679 read_ptr =
680 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
681 NULL, NULL, NULL,
682 (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500683#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500684 read_ptr =
685 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500686#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600687#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500688 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
689 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600690#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500691
692#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500693 user_chunk_data[0] = 0;
694 user_chunk_data[1] = 0;
695 user_chunk_data[2] = 0;
696 user_chunk_data[3] = 0;
697 png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
698 read_user_chunk_callback);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500699
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500700#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500701#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500702#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500703 write_ptr =
704 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
705 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500706#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500707 write_ptr =
708 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500709#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600710#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500711 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
712 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600713#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500714#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500715 png_debug(0, "Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500717 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500718#ifdef PNG_WRITE_SUPPORTED
719 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500720 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500721#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500722
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500724 png_debug(0, "Setting jmpbuf for read struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600725#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600726 if (setjmp(jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600727#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600728 if (setjmp(png_jmpbuf(read_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600729#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600730 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600731 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500732 png_free(read_ptr, row_buf);
733 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500735#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500736 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500738#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500739 FCLOSE(fpin);
740 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600741 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600742 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600743#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500744 png_memcpy(png_jmpbuf(read_ptr), jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600745#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500747#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500748 png_debug(0, "Setting jmpbuf for write struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600749#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600750 if (setjmp(jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600751#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600752 if (setjmp(png_jmpbuf(write_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600753#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600754 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600755 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500757 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500758#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500760#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500761 FCLOSE(fpin);
762 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600763 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600764 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600765#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500766 png_memcpy(png_jmpbuf(write_ptr), jmpbuf, png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600767#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600768#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500769#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600770
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500771 png_debug(0, "Initializing input and output streams");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600772#if !defined(PNG_NO_STDIO)
Guy Schalnate5a37791996-06-05 15:50:50 -0500773 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500774# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500775 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500776# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600777#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500778 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500779# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500780 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500781# if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500782 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500783# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600784 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500785# endif
786# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600787#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500788 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600789 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500790#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600791 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500792#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600793 png_set_read_status_fn(read_ptr, read_row_callback);
794 }
795 else
796 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500797#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500798 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500799#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500800 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600801 }
802
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500803#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
804 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 int i;
806 for (i = 0; i<256; i++)
807 filters_used[i] = 0;
808 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500809 }
810#endif
811#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500812 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500813 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
814#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600816#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500817# ifndef PNG_HANDLE_CHUNK_ALWAYS
818# define PNG_HANDLE_CHUNK_ALWAYS 3
819# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500820 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
821 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600822#endif
823#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500824# ifndef PNG_HANDLE_CHUNK_IF_SAFE
825# define PNG_HANDLE_CHUNK_IF_SAFE 2
826# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
828 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829#endif
830
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500831 png_debug(0, "Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500833
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500834 png_debug(0, "Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 {
836 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500837
Andreas Dilger47a0c421997-05-16 02:46:07 -0500838 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
839 &color_type, &interlace_type, &compression_type, &filter_type))
840 {
841 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600842#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600844#else
845 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
846#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 }
848 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500849#if defined(PNG_FIXED_POINT_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850#if defined(PNG_cHRM_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600852 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 -0600853 blue_y;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600854 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855 &red_y, &green_x, &green_y, &blue_x, &blue_y))
856 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 red_y, green_x, green_y, blue_x, blue_y);
859 }
860 }
861#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600862#if defined(PNG_gAMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600864 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600866 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 }
869#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500870#else /* Use floating point versions */
871#if defined(PNG_FLOATING_POINT_SUPPORTED)
872#if defined(PNG_cHRM_SUPPORTED)
873 {
874 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
875 blue_y;
876 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
877 &red_y, &green_x, &green_y, &blue_x, &blue_y))
878 {
879 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
880 red_y, green_x, green_y, blue_x, blue_y);
881 }
882 }
883#endif
884#if defined(PNG_gAMA_SUPPORTED)
885 {
886 double gamma;
887
888 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500889 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500890 }
891#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500892#endif /* Floating point */
893#endif /* Fixed point */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600894#if defined(PNG_iCCP_SUPPORTED)
895 {
896 png_charp name;
897 png_charp profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600898 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600899 int compression_type;
900
901 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
902 &profile, &proflen))
903 {
904 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
905 profile, proflen);
906 }
907 }
908#endif
909#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600910 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600911 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600912
913 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600914 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600915 }
916#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600917 {
918 png_colorp palette;
919 int num_palette;
920
921 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600922 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600923 }
924#if defined(PNG_bKGD_SUPPORTED)
925 {
926 png_color_16p background;
927
928 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
929 {
930 png_set_bKGD(write_ptr, write_info_ptr, background);
931 }
932 }
933#endif
934#if defined(PNG_hIST_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500935 {
936 png_uint_16p hist;
937
938 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 }
941#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600942#if defined(PNG_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500943 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500945 int unit_type;
946
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500947 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500948 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500949 {
950 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
951 }
952 }
953#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954#if defined(PNG_pCAL_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500955 {
956 png_charp purpose, units;
957 png_charpp params;
958 png_int_32 X0, X1;
959 int type, nparams;
960
961 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
962 &nparams, &units, &params))
963 {
964 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
965 nparams, units, params);
966 }
967 }
968#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600969#if defined(PNG_pHYs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500970 {
971 png_uint_32 res_x, res_y;
972 int unit_type;
973
974 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500976 }
977#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600978#if defined(PNG_sBIT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500979 {
980 png_color_8p sig_bit;
981
982 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500983 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500984 }
985#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986#if defined(PNG_sCAL_SUPPORTED)
987#ifdef PNG_FLOATING_POINT_SUPPORTED
988 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500990 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600991
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500992 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
993 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500995 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600996 }
997 }
998#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600999#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001000 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001002 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001004 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1005 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001007 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008 }
1009 }
1010#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001011#endif
1012#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013#if defined(PNG_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001014 {
1015 png_textp text_ptr;
1016 int num_text;
1017
1018 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1019 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001020 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001021 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1022 }
1023 }
1024#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001025#if defined(PNG_tIME_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 {
1027 png_timep mod_time;
1028
1029 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1030 {
1031 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001032#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001033 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001034 * pointed to by png_convert_to_rfc1123() gets free'ed before
1035 * we use it.
1036 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001037 png_memcpy(tIME_string,
1038 png_convert_to_rfc1123(read_ptr, mod_time),
1039 png_sizeof(tIME_string));
1040 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001041 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001042#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001043 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044 }
1045#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001046#if defined(PNG_tRNS_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001047 {
1048 png_bytep trans;
1049 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001050 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051
1052 if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001053 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001054 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001055 int sample_max = (1 << read_info_ptr->bit_depth);
1056 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1057 if (!((read_info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001058 (int)trans_color->gray > sample_max) ||
1059 (read_info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1060 ((int)trans_color->red > sample_max ||
1061 (int)trans_color->green > sample_max ||
1062 (int)trans_color->blue > sample_max))))
1063 png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
1064 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065 }
1066 }
1067#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001068#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1069 {
1070 png_unknown_chunkp unknowns;
1071 int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
1072 &unknowns);
1073 if (num_unknowns)
1074 {
1075 png_size_t i;
1076 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1077 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001078 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001079 * generated locations in write_info_ptr are wrong because we
1080 * haven't written anything yet.
1081 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001082 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001083 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1084 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001085 }
1086 }
1087#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001089#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001090 png_debug(0, "Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001091
1092/* If we wanted, we could write info in two steps:
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001093 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001094 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001096
1097#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1098 if (user_chunk_data[0] != 0)
1099 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001101
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001102 unsigned char
1103 ster_chunk_data[1];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001104
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001105 if (verbose)
1106 fprintf(STDERR, "\n stereo mode = %lu\n",
1107 (unsigned long)(user_chunk_data[0] - 1));
1108 ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
1109 png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001110 }
1111 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1112 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001113 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 unsigned char
1116 vpag_chunk_data[9];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 if (verbose)
1119 fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
1120 (unsigned long)user_chunk_data[1],
1121 (unsigned long)user_chunk_data[2],
1122 (unsigned long)user_chunk_data[3]);
1123 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1124 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1125 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1126 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001127 }
1128
1129#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001130#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001131
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001132#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001133 png_debug(0, "Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001134 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001136 png_debug1(0, "0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001137#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001138 png_debug(0, "Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001139
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001140#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1141 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001143# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001145# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001146#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001147 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001148#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001149
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001150#ifdef PNGTEST_TIMING
1151 t_stop = (float)clock();
1152 t_misc += (t_stop - t_start);
1153 t_start = t_stop;
1154#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001155 for (pass = 0; pass < num_pass; pass++)
1156 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001157 png_debug1(0, "Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001159 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001160#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001161 png_debug2(0, "Allocating row buffer (pass %d, y = %ld)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001162 row_buf = (png_bytep)png_malloc(read_ptr,
1163 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001164 png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001165 png_get_rowbytes(read_ptr, read_info_ptr));
1166#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001167 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001168
1169#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001170#ifdef PNGTEST_TIMING
1171 t_stop = (float)clock();
1172 t_decode += (t_stop - t_start);
1173 t_start = t_stop;
1174#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001175 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001176#ifdef PNGTEST_TIMING
1177 t_stop = (float)clock();
1178 t_encode += (t_stop - t_start);
1179 t_start = t_stop;
1180#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001181#endif /* PNG_WRITE_SUPPORTED */
1182
1183#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001184 png_debug2(0, "Freeing row buffer (pass %d, y = %ld)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001185 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001186 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001187#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001188 }
1189 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001190
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001191#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001192 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001193#endif
1194#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001195 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001196#endif
1197
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001198 png_debug(0, "Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001199
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001201#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001202 {
1203 png_textp text_ptr;
1204 int num_text;
1205
1206 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1207 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001208 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001209 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1210 }
1211 }
1212#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001213#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001214 {
1215 png_timep mod_time;
1216
1217 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1218 {
1219 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1220#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001221 /* We have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001222 pointed to by png_convert_to_rfc1123() gets free'ed before
1223 we use it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001224 png_memcpy(tIME_string,
1225 png_convert_to_rfc1123(read_ptr, mod_time),
1226 png_sizeof(tIME_string));
1227 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001228 tIME_chunk_present++;
1229#endif /* PNG_TIME_RFC1123_SUPPORTED */
1230 }
1231 }
1232#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001233#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1234 {
1235 png_unknown_chunkp unknowns;
1236 int num_unknowns;
1237 num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1238 &unknowns);
1239 if (num_unknowns)
1240 {
1241 png_size_t i;
1242 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1243 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001244 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001245 * generated locations in write_end_info_ptr are wrong because we
1246 * haven't written the end_info yet.
1247 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001248 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001249 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1250 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001251 }
1252 }
1253#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001254#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001255 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001256#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001257
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001258#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001259 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001260 {
1261 png_uint_32 iwidth, iheight;
1262 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1263 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001264 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001265 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001266 }
1267#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001268
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001269 png_debug(0, "Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001270#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001271 png_debug(1, "destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001272 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001273 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001274#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001275 png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001277#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001278 png_debug(1, "destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001279 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001280 png_debug(1, "destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001282#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001283 png_debug(0, "Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001284
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001285 FCLOSE(fpin);
1286 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001287
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001288 png_debug(0, "Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001290 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001291 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001292 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001293 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001294
Andreas Dilger47a0c421997-05-16 02:46:07 -05001295 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001296 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001297 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001298 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001299 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001300 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001302 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001303 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -05001305
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001306 num_in = fread(inbuf, 1, 1, fpin);
1307 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001308
Guy Schalnat0f716451995-11-28 11:22:13 -06001309 if (num_in != num_out)
1310 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Guy Schalnate5a37791996-06-05 15:50:50 -05001312 inname, outname);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001313 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001314 {
1315 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001316 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001317 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001318 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001319 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001320 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001321 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001322 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001323 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001324 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001325 FCLOSE(fpin);
1326 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001327 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001328 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001329
Guy Schalnat0f716451995-11-28 11:22:13 -06001330 if (!num_in)
1331 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001332
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 if (png_memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001334 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001335 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001336 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001337 {
1338 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001340 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001341 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001342 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001343 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001344 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001345 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001346 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001347 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001348 FCLOSE(fpin);
1349 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001350 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001351 }
1352 }
1353
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001354 FCLOSE(fpin);
1355 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001356
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001357 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001358}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001359
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001360/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001361#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001362static PNG_CONST char *inname = "pngtest/png";
1363static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001364#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001365static PNG_CONST char *inname = "pngtest.png";
1366static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001367#endif
1368
1369int
1370main(int argc, char *argv[])
1371{
1372 int multiple = 0;
1373 int ierror = 0;
1374
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001375 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001376 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001377 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001378 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001379 fprintf(STDERR, " library (%lu):%s",
1380 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001381 png_get_header_version(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001382 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001383 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001384 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001385 fprintf(STDERR, " sizeof(png_struct)=%ld, sizeof(png_info)=%ld\n",
1386 (long)png_sizeof(png_struct), (long)png_sizeof(png_info));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001387
1388 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001389 * not sure this matters, but it is nice to know, the first of these
1390 * tests should be impossible because of the way the macros are set
1391 * in pngconf.h
1392 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001393#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001394 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001395#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001396 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001397#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001398 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001399#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001400
1401 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1402 {
1403 fprintf(STDERR,
1404 "Warning: versions are different between png.h and png.c\n");
1405 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1406 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1407 ++ierror;
1408 }
1409
1410 if (argc > 1)
1411 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001412 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001413 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001414 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001415 status_dots_requested = 0;
1416 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001417 else if (strcmp(argv[1], "-mv") == 0 ||
1418 strcmp(argv[1], "-vm") == 0 )
1419 {
1420 multiple = 1;
1421 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001422 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001423 }
1424 else if (strcmp(argv[1], "-v") == 0)
1425 {
1426 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001427 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001428 inname = argv[2];
1429 }
1430 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001431 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001432 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001433 status_dots_requested = 0;
1434 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001435 }
1436
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001437 if (!multiple && argc == 3 + verbose)
1438 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001439
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001440 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001441 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001442 fprintf(STDERR,
1443 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001444 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001445 fprintf(STDERR,
1446 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1447 fprintf(STDERR,
1448 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001449 exit(1);
1450 }
1451
1452 if (multiple)
1453 {
1454 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001455#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001456 int allocation_now = current_allocation;
1457#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001458 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001459 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001460#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1461 int k;
1462#endif
1463 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001464 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001465 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001466 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001467 {
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -06001468#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001469 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001470 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001471#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001472 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001473#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001474#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001475 for (k = 0; k<256; k++)
1476 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001477 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001478 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001479#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001480#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001481 if (tIME_chunk_present != 0)
1482 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001483 tIME_chunk_present = 0;
1484#endif /* PNG_TIME_RFC1123_SUPPORTED */
1485 }
1486 else
1487 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001488 fprintf(STDERR, " FAIL\n");
1489 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001490 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001491#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001492 if (allocation_now != current_allocation)
1493 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001494 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001495 if (current_allocation != 0)
1496 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001497 memory_infop pinfo = pinformation;
1498
1499 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1500 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001501 while (pinfo != NULL)
1502 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001503 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001504 (unsigned long)pinfo->size,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001505 (unsigned int) pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001506 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001507 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001508 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001509#endif
1510 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001511#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001512 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001513 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001514 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001515 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001516 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1517 total_allocation);
1518 fprintf(STDERR, " Number of allocations: %10d\n",
1519 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001520#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001521 }
1522 else
1523 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001524 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001525 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001526 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001527 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001528#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001529 int allocation_now = current_allocation;
1530#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001531 if (i == 1) status_dots_requested = 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001532 else if (verbose == 0)status_dots_requested = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001533 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001534 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001535 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001536 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001537 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001538 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001539 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001540#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001541 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001542#endif
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -06001543#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001544 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001545 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001546#else
1547 fprintf(STDERR, " PASS\n");
1548#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001549#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001550 for (k = 0; k<256; k++)
1551 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001552 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001553 k,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001554 (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001555#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001556#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001557 if (tIME_chunk_present != 0)
1558 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001559#endif /* PNG_TIME_RFC1123_SUPPORTED */
1560 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001561 }
1562 else
1563 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001564 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001565 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001566 fprintf(STDERR, " FAIL\n");
1567 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001568 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001569#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001570 if (allocation_now != current_allocation)
1571 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001572 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001573 if (current_allocation != 0)
1574 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001575 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001576
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001577 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1578 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001579 while (pinfo != NULL)
1580 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001581 fprintf(STDERR, " %lu bytes at %x\n",
1582 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001583 pinfo = pinfo->next;
1584 }
1585 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001586#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001587 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001588#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001589 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001590 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001591 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001592 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001593 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1594 total_allocation);
1595 fprintf(STDERR, " Number of allocations: %10d\n",
1596 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001597#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001598 }
1599
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001600#ifdef PNGTEST_TIMING
1601 t_stop = (float)clock();
1602 t_misc += (t_stop - t_start);
1603 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001604 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001605 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001606 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001607 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001608 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001609 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001610 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001611 t_misc/(float)CLOCKS_PER_SEC);
1612#endif
1613
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001614 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001615 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001616 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001617 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001618 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001619}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001620
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001621/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05001622typedef version_1_4_0beta66 your_png_h_is_not_version_1_4_0beta66;