blob: 79984f607fd5d2a8429e0228952405ddfd4f5bde [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-Pehrson56f63962008-10-06 10:16:17 -05004 * Last changed in libpng 1.4.0 [October 6, 2008]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05006 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
10 * This program reads in a PNG image, writes it out again, and then
11 * compares the two files. If the files are identical, this shows that
12 * the basic chunk handling, filtering, and (de)compression code is working
13 * properly. It does not currently test all of the transforms, although
14 * it probably should.
15 *
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050016 * The program will report "FAIL" in certain legitimate cases:
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060017 * 1) when the compression level or filter selection method is changed.
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060018 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -060019 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
20 * exist in the input file.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060021 * 4) others not listed here...
22 * In these cases, it is best to check with another tool such as "pngcheck"
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060023 * to see what the differences between the two files are.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060024 *
25 * If a filename is given on the command-line, then this file is used
26 * for the input, rather than the default "pngtest.png". This allows
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050027 * testing a wide variety of files easily. You can also test a number
28 * of files at once by typing "pngtest -m file1.png file2.png ..."
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 */
Guy Schalnat0d580581995-07-20 02:43:20 -050030
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050031#include "png.h"
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032#include "pngpriv.h"
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050033
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050034# include <stdio.h>
35# include <stdlib.h>
36# define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050037
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050038#if defined(PNG_NO_STDIO)
39 typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060040#endif
41
Andreas Dilger47a0c421997-05-16 02:46:07 -050042/* Makes pngtest verbose so we can find problems (needs to be before png.h) */
43#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050044# define PNG_DEBUG 0
45#endif
46
47#if !PNG_DEBUG
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -050048# define SINGLE_ROWBUF_ALLOC /* makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060049#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050050
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050051/* Turn on CPU timing
52#define PNGTEST_TIMING
53*/
54
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060055#ifdef PNG_NO_FLOATING_POINT_SUPPORTED
56#undef PNGTEST_TIMING
57#endif
58
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050059#ifdef PNGTEST_TIMING
60static float t_start, t_stop, t_decode, t_encode, t_misc;
61#include <time.h>
62#endif
63
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050064#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050065#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050066static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050067static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050068#endif
69
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050070static int verbose = 0;
71
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060072int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060073
Guy Schalnat0d580581995-07-20 02:43:20 -050074#ifdef __TURBOC__
75#include <mem.h>
76#endif
77
78/* defined so I can write to a file on gui/windowing platforms */
Guy Schalnat6d764711995-12-19 03:22:19 -060079/* #define STDERR stderr */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060080#define STDERR stdout /* for DOS */
Guy Schalnat0d580581995-07-20 02:43:20 -050081
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -050082/* In case a system header (e.g., on AIX) defined jmpbuf */
83#ifdef jmpbuf
84# undef jmpbuf
85#endif
86
87/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
88#ifndef png_jmpbuf
89# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
90#endif
91
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060092/* example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050093static int status_pass = 1;
94static int status_dots_requested = 0;
95static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060096
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -060097void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050098read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -050099void
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600100read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600101{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500102 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX) return;
103 if (status_pass != pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600104 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500105 fprintf(stdout, "\n Pass %d: ", pass);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600106 status_pass = pass;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500107 status_dots = 31;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600108 }
109 status_dots--;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500110 if (status_dots == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600111 {
112 fprintf(stdout, "\n ");
113 status_dots=30;
114 }
115 fprintf(stdout, "r");
116}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600117
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -0500118void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500119write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
Glenn Randers-Pehrson0cc2f952002-10-03 06:32:37 -0500120void
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600121write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600122{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500123 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600124 fprintf(stdout, "w");
125}
126
127
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500128#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
129/* Example of using user transform callback (we don't transform anything,
130 but merely examine the row filters. We set this to 256 rather than
131 5 in case illegal filter values are present.) */
132static png_uint_32 filters_used[256];
133void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500134count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
135void
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500136count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
137{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500138 if (png_ptr != NULL && row_info != NULL)
139 ++filters_used[*(data - 1)];
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500140}
141#endif
142
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600143#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600144/* example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500145 but merely count the zero samples) */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600146
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500147static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600148
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600149void
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500150count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
151void
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500152count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600153{
154 png_bytep dp = data;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500155 if (png_ptr == NULL)return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600156
157 /* contents of row_info:
158 * png_uint_32 width width of row
159 * png_uint_32 rowbytes number of bytes in row
160 * png_byte color_type color type of pixels
161 * png_byte bit_depth bit depth of samples
162 * png_byte channels number of channels (1-4)
163 * png_byte pixel_depth bits per pixel (depth*channels)
164 */
165
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500166
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500167 /* counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600168
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500169 if (row_info->color_type == 0 || row_info->color_type == 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600170 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500171 int pos = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500172 png_uint_32 n, nstop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600174 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500175 if (row_info->bit_depth == 1)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500176 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500177 if (((*dp << pos++ ) & 0x80) == 0) zero_samples++;
178 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600179 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500180 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600181 dp++;
182 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500183 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500184 if (row_info->bit_depth == 2)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500185 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500186 if (((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;
187 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600188 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500189 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600190 dp++;
191 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500192 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500193 if (row_info->bit_depth == 4)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500194 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500195 if (((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;
196 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600197 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500198 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600199 dp++;
200 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500201 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500202 if (row_info->bit_depth == 8)
203 if (*dp++ == 0) zero_samples++;
204 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600205 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500206 if ((*dp | *(dp+1)) == 0) zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600207 dp+=2;
208 }
209 }
210 }
211 else /* other color types */
212 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500213 png_uint_32 n, nstop;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600214 int channel;
215 int color_channels = row_info->channels;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500216 if (row_info->color_type > 3)color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600217
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500218 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600219 {
220 for (channel = 0; channel < color_channels; channel++)
221 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500222 if (row_info->bit_depth == 8)
223 if (*dp++ == 0) zero_samples++;
224 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600225 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500226 if ((*dp | *(dp+1)) == 0) zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600227 dp+=2;
228 }
229 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500230 if (row_info->color_type > 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600231 {
232 dp++;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500233 if (row_info->bit_depth == 16)dp++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600234 }
235 }
236 }
237}
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600238#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600239
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600240static int wrote_question = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600241
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600242#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600243/* START of code to validate stdio-free compilation */
244/* These copies of the default read/write functions come from pngrio.c and */
245/* pngwio.c. They allow "don't include stdio" testing of the library. */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500246/* This is the function that does the actual reading of data. If you are
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600247 not reading from a standard C stream, you should create a replacement
248 read_data function and use it at run time with png_set_read_fn(), rather
249 than changing the library. */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600250
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600251#ifndef USE_FAR_KEYWORD
252static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500253pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600254{
255 png_size_t check;
256
257 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
258 * instead of an int, which is what fread() actually returns.
259 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500260 check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500261
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600262 if (check != length)
263 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500264 png_error(png_ptr, "Read Error!");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600265 }
266}
Guy Schalnate5a37791996-06-05 15:50:50 -0500267#else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600268/* this is the model-independent version. Since the standard I/O library
269 can't handle far buffers in the medium and small models, we have to copy
270 the data.
271*/
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600272
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600273#define NEAR_BUF_SIZE 1024
274#define MIN(a,b) (a <= b ? a : b)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600275
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600276static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500277pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600278{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500279 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600280 png_byte *n_data;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500281 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600282
283 /* Check if data really is near. If so, use usual code. */
284 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500285 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600286 if ((png_bytep)n_data == data)
287 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500288 check = fread(n_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600289 }
290 else
291 {
292 png_byte buf[NEAR_BUF_SIZE];
293 png_size_t read, remaining, err;
294 check = 0;
295 remaining = length;
296 do
297 {
298 read = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500299 err = fread(buf, 1, 1, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600300 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500301 if (err != read)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600302 break;
303 else
304 check += err;
305 data += read;
306 remaining -= read;
307 }
308 while (remaining != 0);
309 }
310 if (check != length)
311 {
312 png_error(png_ptr, "read Error");
313 }
314}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600315#endif /* USE_FAR_KEYWORD */
Guy Schalnat0d580581995-07-20 02:43:20 -0500316
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600317#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600318static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500319pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600320{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500321 png_FILE_p io_ptr;
322 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600323 if (io_ptr != NULL)
324 fflush(io_ptr);
325}
326#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500327
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500328/* This is the function that does the actual writing of data. If you are
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600329 not writing to a standard C stream, you should create a replacement
330 write_data function and use it at run time with png_set_write_fn(), rather
331 than changing the library. */
332#ifndef USE_FAR_KEYWORD
333static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500334pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600335{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500336 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600337
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500338 check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600339 if (check != length)
340 {
341 png_error(png_ptr, "Write Error");
342 }
343}
344#else
345/* this is the model-independent version. Since the standard I/O library
346 can't handle far buffers in the medium and small models, we have to copy
347 the data.
348*/
349
350#define NEAR_BUF_SIZE 1024
351#define MIN(a,b) (a <= b ? a : b)
352
353static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500354pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600355{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500356 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600357 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500358 png_FILE_p io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600359
360 /* Check if data really is near. If so, use usual code. */
361 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500362 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600363 if ((png_bytep)near_data == data)
364 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500365 check = fwrite(near_data, 1, length, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600366 }
367 else
368 {
369 png_byte buf[NEAR_BUF_SIZE];
370 png_size_t written, remaining, err;
371 check = 0;
372 remaining = length;
373 do
374 {
375 written = MIN(NEAR_BUF_SIZE, remaining);
376 png_memcpy(buf, data, written); /* copy far buffer to near buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500377 err = fwrite(buf, 1, written, io_ptr);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600378 if (err != written)
379 break;
380 else
381 check += err;
382 data += written;
383 remaining -= written;
384 }
385 while (remaining != 0);
386 }
387 if (check != length)
388 {
389 png_error(png_ptr, "Write Error");
390 }
391}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600392#endif /* USE_FAR_KEYWORD */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600393
394/* This function is called when there is a warning, but the library thinks
395 * it can continue anyway. Replacement functions don't have to do anything
396 * here if you don't want to. In the default configuration, png_ptr is
397 * not used, but it is passed in case it may be useful.
398 */
399static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500400pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600401{
402 PNG_CONST char *name = "UNKNOWN (ERROR!)";
403 if (png_ptr != NULL && png_ptr->error_ptr != NULL)
404 name = png_ptr->error_ptr;
405 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
406}
407
408/* This is the default error handling function. Note that replacements for
409 * this function MUST NOT RETURN, or the program will likely crash. This
410 * function is used by default, or if the program supplies NULL for the
411 * error function pointer in png_set_error_fn().
412 */
413static void
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500414pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600415{
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500416 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500417 /* We can return because png_error calls the default handler, which is
418 * actually OK in this case. */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600419}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600420#endif /* PNG_NO_STDIO */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600421/* END of code to validate stdio-free compilation */
422
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600423/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500424#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600425
426/* Allocate memory. For reasonable files, size should never exceed
427 64K. However, zlib may allocate more then 64K if you don't tell
428 it not to. See zconf.h and png.h for more information. zlib does
429 need to allocate exactly 64K, so whatever you call here must
430 have the ability to do that.
431
432 This piece of code can be compiled to validate max 64K allocations
433 by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600434typedef struct memory_information
435{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500436 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500437 png_voidp pointer;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600438 struct memory_information FAR *next;
439} memory_information;
440typedef memory_information FAR *memory_infop;
441
442static memory_infop pinformation = NULL;
443static int current_allocation = 0;
444static int maximum_allocation = 0;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500445static int total_allocation = 0;
446static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600447
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500448png_voidp png_debug_malloc
449 PNGARG((png_structp png_ptr, png_alloc_size_t size));
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500450void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600451
452png_voidp
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500453png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600454{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500455
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500456 /* png_malloc has already tested for NULL; png_create_struct calls
457 png_debug_malloc directly, with png_ptr == NULL which is OK */
458
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600459 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500460 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600461
462 /* This calls the library allocator twice, once to get the requested
463 buffer and once to get a new free list entry. */
464 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500465 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500466 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500467 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500468 pinfo = (memory_infop)png_malloc(png_ptr,
469 png_sizeof(*pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600470 pinfo->size = size;
471 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500472 total_allocation += size;
473 num_allocations ++;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600474 if (current_allocation > maximum_allocation)
475 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500476 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500477 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500478 png_set_mem_fn(png_ptr,
479 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500480 if (size != 0 && pinfo->pointer == NULL)
481 {
482 current_allocation -= size;
483 total_allocation -= size;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500484 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500485 "out of memory in pngtest->png_debug_malloc");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500486 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600487 pinfo->next = pinformation;
488 pinformation = pinfo;
489 /* Make sure the caller isn't assuming zeroed memory. */
490 png_memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500491 if (verbose)
492 printf("png_malloc %lu bytes at %x\n", (unsigned long)size,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500493 pinfo->pointer);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600494 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600495 }
496}
497
498/* Free a pointer. It is removed from the list at the same time. */
499void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500500png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600501{
502 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500503 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600504 if (ptr == 0)
505 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600506#if 0 /* This happens all the time. */
507 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
508#endif
509 return;
510 }
511
512 /* Unlink the element from the list. */
513 {
514 memory_infop FAR *ppinfo = &pinformation;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600515 for (;;)
516 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600517 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600518 if (pinfo->pointer == ptr)
519 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600520 *ppinfo = pinfo->next;
521 current_allocation -= pinfo->size;
522 if (current_allocation < 0)
523 fprintf(STDERR, "Duplicate free of memory\n");
524 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500525 the memory that is to be freed. */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500526 png_memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500527 png_free_default(png_ptr, pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500528 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600529 break;
530 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600531 if (pinfo->next == NULL)
532 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500533 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600534 break;
535 }
536 ppinfo = &pinfo->next;
537 }
538 }
539
540 /* Finally free the data. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500541 if (verbose)
542 printf("Freeing %x\n", ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500543 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500544 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600545}
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500546#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600547/* END of code to test memory allocation/deallocation */
548
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500549
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500550/* Demonstration of user chunk support of the sTER and vpAg chunks */
551#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
552
553/* (sTER is a public chunk not yet understood by libpng. vpAg is a private
554chunk used in ImageMagick to store "virtual page" size). */
555
556static png_uint_32 user_chunk_data[4];
557
Glenn Randers-Pehrsone826d7e2006-07-03 00:21:58 -0500558 /* 0: sTER mode + 1
559 * 1: vpAg width
560 * 2: vpAg height
561 * 3: vpAg units
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500562 */
563
564static int read_user_chunk_callback(png_struct *png_ptr,
565 png_unknown_chunkp chunk)
566{
567 png_uint_32
568 *user_chunk_data;
569
570 /* Return one of the following: */
571 /* return (-n); chunk had an error */
572 /* return (0); did not recognize */
573 /* return (n); success */
574
575 /* The unknown chunk structure contains the chunk data:
576 * png_byte name[5];
577 * png_byte *data;
578 * png_size_t size;
579 *
580 * Note that libpng has already taken care of the CRC handling.
581 */
582
583 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
Glenn Randers-Pehrsone826d7e2006-07-03 00:21:58 -0500584 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500585 {
586 /* Found sTER chunk */
587 if (chunk->size != 1)
588 return (-1); /* Error return */
589 if (chunk->data[0] != 0 && chunk->data[0] != 1)
590 return (-1); /* Invalid mode */
591 user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
592 user_chunk_data[0]=chunk->data[0]+1;
593 return (1);
594 }
595 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
596 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
597 return (0); /* Did not recognize */
598
599 /* Found ImageMagick vpAg chunk */
600
601 if (chunk->size != 9)
602 return (-1); /* Error return */
603
604 user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
605
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500606 user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);
607 user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500608 user_chunk_data[3]=(png_uint_32)chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500609
610 return (1);
611
612}
613#endif
614/* END of code to demonstrate user chunk support */
615
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600616/* Test one file */
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600617int
618test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500619{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500620 static png_FILE_p fpin;
621 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500622 png_structp read_ptr;
623 png_infop read_info_ptr, end_info_ptr;
624#ifdef PNG_WRITE_SUPPORTED
625 png_structp write_ptr;
626 png_infop write_info_ptr;
627 png_infop write_end_info_ptr;
628#else
629 png_structp write_ptr = NULL;
630 png_infop write_info_ptr = NULL;
631 png_infop write_end_info_ptr = NULL;
632#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600633 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635 png_uint_32 width, height;
636 int num_pass, pass;
637 int bit_depth, color_type;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600638#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600639#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600640 jmp_buf jmpbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600641#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600642#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600643
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600644 char inbuf[256], outbuf[256];
645
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500646 row_buf = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500647
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600649 {
650 fprintf(STDERR, "Could not find input file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600651 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600652 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500653
Andreas Dilger47a0c421997-05-16 02:46:07 -0500654 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600655 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500656 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500657 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600658 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600659 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500660
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500661 png_debug(0, "Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500662#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500663 read_ptr =
664 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
665 NULL, NULL, NULL,
666 (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500667#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500668 read_ptr =
669 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500670#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600671#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500672 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
673 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600674#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500675
676#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500677 user_chunk_data[0] = 0;
678 user_chunk_data[1] = 0;
679 user_chunk_data[2] = 0;
680 user_chunk_data[3] = 0;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500681 png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
682 read_user_chunk_callback);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500683
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500684#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500685#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500686#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500687 write_ptr =
688 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
689 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500690#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500691 write_ptr =
692 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500693#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600694#if defined(PNG_NO_STDIO)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500695 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
696 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600697#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500698#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500699 png_debug(0, "Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500701 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500702#ifdef PNG_WRITE_SUPPORTED
703 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500704 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500705#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500706
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600707#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500708 png_debug(0, "Setting jmpbuf for read struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600709#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600710 if (setjmp(jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600711#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600712 if (setjmp(png_jmpbuf(read_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600713#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600714 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600715 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500716 png_free(read_ptr, row_buf);
717 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500719#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500720 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500722#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500723 FCLOSE(fpin);
724 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600725 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600726 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600727#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500728 png_memcpy(png_jmpbuf(read_ptr), jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600729#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500730
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500731#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500732 png_debug(0, "Setting jmpbuf for write struct");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600733#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600734 if (setjmp(jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600735#else
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600736 if (setjmp(png_jmpbuf(write_ptr)))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600737#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600738 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600739 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500740 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500741 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500742#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500744#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500745 FCLOSE(fpin);
746 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600747 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600748 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600749#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 png_memcpy(png_jmpbuf(write_ptr), jmpbuf, png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600751#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600752#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500753#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600754
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500755 png_debug(0, "Initializing input and output streams");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600756#if !defined(PNG_NO_STDIO)
Guy Schalnate5a37791996-06-05 15:50:50 -0500757 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500758# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500759 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500760# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600761#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500762 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500763# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500764 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500765# if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500766 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500767# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600768 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500769# endif
770# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600771#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500772 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600773 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500774#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600775 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500776#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600777 png_set_read_status_fn(read_ptr, read_row_callback);
778 }
779 else
780 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500781#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500782 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500783#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500784 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600785 }
786
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500787#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
788 {
789 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500790 for (i = 0; i<256; i++)
791 filters_used[i] = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500792 png_set_read_user_transform_fn(read_ptr, count_filters);
793 }
794#endif
795#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500796 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500797 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
798#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500799
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600800#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500801# ifndef PNG_HANDLE_CHUNK_ALWAYS
802# define PNG_HANDLE_CHUNK_ALWAYS 3
803# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500804 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
805 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600806#endif
807#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500808# ifndef PNG_HANDLE_CHUNK_IF_SAFE
809# define PNG_HANDLE_CHUNK_IF_SAFE 2
810# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500811 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
812 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600813#endif
814
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500815 png_debug(0, "Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500816 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500817
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500818 png_debug(0, "Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 {
820 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500821
Andreas Dilger47a0c421997-05-16 02:46:07 -0500822 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
823 &color_type, &interlace_type, &compression_type, &filter_type))
824 {
825 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600826#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600828#else
829 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
830#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831 }
832 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500833#if defined(PNG_FIXED_POINT_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834#if defined(PNG_cHRM_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600836 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 -0600837 blue_y;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600838 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839 &red_y, &green_x, &green_y, &blue_x, &blue_y))
840 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600841 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500842 red_y, green_x, green_y, blue_x, blue_y);
843 }
844 }
845#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600846#if defined(PNG_gAMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600848 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600852 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 }
854 }
855#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500856#else /* Use floating point versions */
857#if defined(PNG_FLOATING_POINT_SUPPORTED)
858#if defined(PNG_cHRM_SUPPORTED)
859 {
860 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
861 blue_y;
862 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
863 &red_y, &green_x, &green_y, &blue_x, &blue_y))
864 {
865 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
866 red_y, green_x, green_y, blue_x, blue_y);
867 }
868 }
869#endif
870#if defined(PNG_gAMA_SUPPORTED)
871 {
872 double gamma;
873
874 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
875 {
876 png_set_gAMA(write_ptr, write_info_ptr, gamma);
877 }
878 }
879#endif
880#endif /* floating point */
881#endif /* fixed point */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600882#if defined(PNG_iCCP_SUPPORTED)
883 {
884 png_charp name;
885 png_charp profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600886 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600887 int compression_type;
888
889 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
890 &profile, &proflen))
891 {
892 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
893 profile, proflen);
894 }
895 }
896#endif
897#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600898 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600899 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600900
901 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
902 {
903 png_set_sRGB(write_ptr, write_info_ptr, intent);
904 }
905 }
906#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600907 {
908 png_colorp palette;
909 int num_palette;
910
911 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
912 {
913 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
914 }
915 }
916#if defined(PNG_bKGD_SUPPORTED)
917 {
918 png_color_16p background;
919
920 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
921 {
922 png_set_bKGD(write_ptr, write_info_ptr, background);
923 }
924 }
925#endif
926#if defined(PNG_hIST_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 {
928 png_uint_16p hist;
929
930 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
931 {
932 png_set_hIST(write_ptr, write_info_ptr, hist);
933 }
934 }
935#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600936#if defined(PNG_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600938 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 int unit_type;
940
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500941 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
942 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500943 {
944 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
945 }
946 }
947#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948#if defined(PNG_pCAL_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500949 {
950 png_charp purpose, units;
951 png_charpp params;
952 png_int_32 X0, X1;
953 int type, nparams;
954
955 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
956 &nparams, &units, &params))
957 {
958 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
959 nparams, units, params);
960 }
961 }
962#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600963#if defined(PNG_pHYs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 {
965 png_uint_32 res_x, res_y;
966 int unit_type;
967
968 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
969 {
970 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
971 }
972 }
973#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600974#if defined(PNG_sBIT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 {
976 png_color_8p sig_bit;
977
978 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
979 {
980 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
981 }
982 }
983#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600984#if defined(PNG_sCAL_SUPPORTED)
985#ifdef PNG_FLOATING_POINT_SUPPORTED
986 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600987 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500988 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500990 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
991 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500993 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994 }
995 }
996#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600999 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001000 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001002 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1003 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001004 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001005 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006 }
1007 }
1008#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001009#endif
1010#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011#if defined(PNG_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001012 {
1013 png_textp text_ptr;
1014 int num_text;
1015
1016 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1017 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001018 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1020 }
1021 }
1022#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001023#if defined(PNG_tIME_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 {
1025 png_timep mod_time;
1026
1027 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1028 {
1029 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001030#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001031 /* we have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001032 pointed to by png_convert_to_rfc1123() gets free'ed before
1033 we use it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001034 png_memcpy(tIME_string,
1035 png_convert_to_rfc1123(read_ptr, mod_time),
1036 png_sizeof(tIME_string));
1037 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001038 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001039#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001040 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 }
1042#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043#if defined(PNG_tRNS_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044 {
1045 png_bytep trans;
1046 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001047 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048
1049 if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001050 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001052 int sample_max = (1 << read_info_ptr->bit_depth);
1053 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1054 if (!((read_info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001055 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001056 (read_info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001057 ((int)trans_color->red > sample_max ||
1058 (int)trans_color->green > sample_max ||
1059 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001060 png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001061 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001062 }
1063 }
1064#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001065#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1066 {
1067 png_unknown_chunkp unknowns;
1068 int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
1069 &unknowns);
1070 if (num_unknowns)
1071 {
1072 png_size_t i;
1073 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1074 num_unknowns);
1075 /* copy the locations from the read_info_ptr. The automatically
1076 generated locations in write_info_ptr are wrong because we
1077 haven't written anything yet */
1078 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001079 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1080 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081 }
1082 }
1083#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001085#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001086 png_debug(0, "\nWriting info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001087
1088/* If we wanted, we could write info in two steps:
1089 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1090 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001091 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001092
1093#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1094 if (user_chunk_data[0] != 0)
1095 {
1096 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
1097
1098 unsigned char
1099 ster_chunk_data[1];
1100
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001101 if (verbose)
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001102 fprintf(STDERR, "stereo mode = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001103 (unsigned long)(user_chunk_data[0] - 1));
1104 ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
1105 png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001106 }
1107 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1108 {
1109 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
1110
1111 unsigned char
1112 vpag_chunk_data[9];
1113
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001114 if (verbose)
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001115 fprintf(STDERR, "vpAg = %lu x %lu, units=%lu\n",
1116 (unsigned long)user_chunk_data[1],
1117 (unsigned long)user_chunk_data[2],
1118 (unsigned long)user_chunk_data[3]);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001119 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1120 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1121 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1122 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001123 }
1124
1125#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001126#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001127
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001128#ifdef SINGLE_ROWBUF_ALLOC
1129 png_debug(0, "\nAllocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001130 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001131 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001132 png_debug1(0, "0x%08lx\n", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001133#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001134 png_debug(0, "Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001135
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001136#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1137 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001138 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001139# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001140 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001141# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001142#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001143 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001144#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001145
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001146#ifdef PNGTEST_TIMING
1147 t_stop = (float)clock();
1148 t_misc += (t_stop - t_start);
1149 t_start = t_stop;
1150#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001151 for (pass = 0; pass < num_pass; pass++)
1152 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001153 png_debug1(0, "Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001155 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001156#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001157 png_debug2(0, "\nAllocating row buffer (pass %d, y = %ld)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001158 row_buf = (png_bytep)png_malloc(read_ptr,
1159 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001160 png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001161 png_get_rowbytes(read_ptr, read_info_ptr));
1162#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001163 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001164
1165#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001166#ifdef PNGTEST_TIMING
1167 t_stop = (float)clock();
1168 t_decode += (t_stop - t_start);
1169 t_start = t_stop;
1170#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001171 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001172#ifdef PNGTEST_TIMING
1173 t_stop = (float)clock();
1174 t_encode += (t_stop - t_start);
1175 t_start = t_stop;
1176#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001177#endif /* PNG_WRITE_SUPPORTED */
1178
1179#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001180 png_debug2(0, "Freeing row buffer (pass %d, y = %ld)\n", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001181 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001182 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001183#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001184 }
1185 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001186
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001187#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001188 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001189#endif
1190#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001191 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001192#endif
1193
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001194 png_debug(0, "Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001195
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001197#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001198 {
1199 png_textp text_ptr;
1200 int num_text;
1201
1202 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1203 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001204 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001205 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1206 }
1207 }
1208#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001209#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001210 {
1211 png_timep mod_time;
1212
1213 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1214 {
1215 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1216#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001217 /* we have to use png_memcpy instead of "=" because the string
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001218 pointed to by png_convert_to_rfc1123() gets free'ed before
1219 we use it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001220 png_memcpy(tIME_string,
1221 png_convert_to_rfc1123(read_ptr, mod_time),
1222 png_sizeof(tIME_string));
1223 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001224 tIME_chunk_present++;
1225#endif /* PNG_TIME_RFC1123_SUPPORTED */
1226 }
1227 }
1228#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001229#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1230 {
1231 png_unknown_chunkp unknowns;
1232 int num_unknowns;
1233 num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1234 &unknowns);
1235 if (num_unknowns)
1236 {
1237 png_size_t i;
1238 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1239 num_unknowns);
1240 /* copy the locations from the read_info_ptr. The automatically
1241 generated locations in write_end_info_ptr are wrong because we
1242 haven't written the end_info yet */
1243 for (i = 0; i < (png_size_t)num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001244 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1245 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001246 }
1247 }
1248#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001249#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001250 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001251#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001252
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001253#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001254 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001255 {
1256 png_uint_32 iwidth, iheight;
1257 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1258 iheight = png_get_image_height(write_ptr, write_info_ptr);
1259 fprintf(STDERR, "Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001260 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001261 }
1262#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001263
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001264 png_debug(0, "Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001265#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001266 png_debug(1, "destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001267 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001268 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001269#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001270 png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001272#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001273 png_debug(1, "destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001274 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001275 png_debug(1, "destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001277#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001278 png_debug(0, "Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001279
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001280 FCLOSE(fpin);
1281 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001282
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001283 png_debug(0, "Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001284 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001285 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001286 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001287 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001288 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001289
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001291 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001292 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001293 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001294 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001295 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001297 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001298 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -05001300
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001301 num_in = fread(inbuf, 1, 1, fpin);
1302 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001303
Guy Schalnat0f716451995-11-28 11:22:13 -06001304 if (num_in != num_out)
1305 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001306 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Guy Schalnate5a37791996-06-05 15:50:50 -05001307 inname, outname);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001308 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001309 {
1310 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001312 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001313 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001314 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001315 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001316 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001317 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001318 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001319 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001320 FCLOSE(fpin);
1321 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001322 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001323 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001324
Guy Schalnat0f716451995-11-28 11:22:13 -06001325 if (!num_in)
1326 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001327
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 if (png_memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001329 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001330 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001331 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001332 {
1333 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001334 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001335 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001336 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001337 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001338 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001340 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001341 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001342 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001343 FCLOSE(fpin);
1344 FCLOSE(fpout);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001345 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001346 }
1347 }
1348
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001349 FCLOSE(fpin);
1350 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001351
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001352 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001353}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001354
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001355/* input and output filenames */
1356#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001357static PNG_CONST char *inname = "pngtest/png";
1358static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001359#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001360static PNG_CONST char *inname = "pngtest.png";
1361static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001362#endif
1363
1364int
1365main(int argc, char *argv[])
1366{
1367 int multiple = 0;
1368 int ierror = 0;
1369
1370 fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001371 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001372 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001373 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001374 fprintf(STDERR, " library (%lu):%s",
1375 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001376 png_get_header_version(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001377 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001378 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001379 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001380 fprintf(STDERR, " sizeof(png_struct)=%ld, sizeof(png_info)=%ld\n",
1381 (long)png_sizeof(png_struct), (long)png_sizeof(png_info));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001382
1383 /* Do some consistency checking on the memory allocation settings, I'm
1384 not sure this matters, but it is nice to know, the first of these
1385 tests should be impossible because of the way the macros are set
1386 in pngconf.h */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001387#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001388 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001389#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001390 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001391#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001392 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001393#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001394
1395 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1396 {
1397 fprintf(STDERR,
1398 "Warning: versions are different between png.h and png.c\n");
1399 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1400 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1401 ++ierror;
1402 }
1403
1404 if (argc > 1)
1405 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001406 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001407 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001408 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001409 status_dots_requested = 0;
1410 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001411 else if (strcmp(argv[1], "-mv") == 0 ||
1412 strcmp(argv[1], "-vm") == 0 )
1413 {
1414 multiple = 1;
1415 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001416 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001417 }
1418 else if (strcmp(argv[1], "-v") == 0)
1419 {
1420 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001421 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001422 inname = argv[2];
1423 }
1424 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001425 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001426 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001427 status_dots_requested = 0;
1428 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001429 }
1430
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001431 if (!multiple && argc == 3 + verbose)
1432 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001433
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001434 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001435 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001436 fprintf(STDERR,
1437 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001438 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001439 fprintf(STDERR,
1440 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1441 fprintf(STDERR,
1442 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001443 exit(1);
1444 }
1445
1446 if (multiple)
1447 {
1448 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001449#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001450 int allocation_now = current_allocation;
1451#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001452 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001453 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001454#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1455 int k;
1456#endif
1457 int kerror;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001458 fprintf(STDERR, "Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001459 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001460 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001461 {
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -06001462#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001463 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001464 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001465#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001466 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001467#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001468#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001469 for (k = 0; k<256; k++)
1470 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001471 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001472 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001473#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001474#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001475 if (tIME_chunk_present != 0)
1476 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001477 tIME_chunk_present = 0;
1478#endif /* PNG_TIME_RFC1123_SUPPORTED */
1479 }
1480 else
1481 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001482 fprintf(STDERR, " FAIL\n");
1483 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001484 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001485#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001486 if (allocation_now != current_allocation)
1487 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001488 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001489 if (current_allocation != 0)
1490 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001491 memory_infop pinfo = pinformation;
1492
1493 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1494 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001495 while (pinfo != NULL)
1496 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001497 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001498 (unsigned long)pinfo->size,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001499 (unsigned int) pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001500 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001501 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001502 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001503#endif
1504 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001505#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001506 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001507 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001508 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001509 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001510 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1511 total_allocation);
1512 fprintf(STDERR, " Number of allocations: %10d\n",
1513 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001514#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001515 }
1516 else
1517 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001518 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001519 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001520 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001521 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001522#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001523 int allocation_now = current_allocation;
1524#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001525 if (i == 1) status_dots_requested = 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001526 else if (verbose == 0)status_dots_requested = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001527 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001528 fprintf(STDERR, "Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001529 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001530 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001531 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001532 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001533 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001534#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001535 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001536#endif
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -06001537#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001538 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001539 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001540#else
1541 fprintf(STDERR, " PASS\n");
1542#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001543#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001544 for (k = 0; k<256; k++)
1545 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001546 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001547 k,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001548 (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001549#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001550#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001551 if (tIME_chunk_present != 0)
1552 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001553#endif /* PNG_TIME_RFC1123_SUPPORTED */
1554 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001555 }
1556 else
1557 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001558 if (verbose == 0 && i != 2)
1559 fprintf(STDERR, "Testing %s:", inname);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001560 fprintf(STDERR, " FAIL\n");
1561 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001562 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001563#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001564 if (allocation_now != current_allocation)
1565 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001566 current_allocation - allocation_now);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001567 if (current_allocation != 0)
1568 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001569 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001570
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001571 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1572 current_allocation);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001573 while (pinfo != NULL)
1574 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001575 fprintf(STDERR, " %lu bytes at %x\n",
1576 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001577 pinfo = pinfo->next;
1578 }
1579 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001580#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001581 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001582#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001583 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001584 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001585 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001586 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001587 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1588 total_allocation);
1589 fprintf(STDERR, " Number of allocations: %10d\n",
1590 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001591#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001592 }
1593
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001594#ifdef PNGTEST_TIMING
1595 t_stop = (float)clock();
1596 t_misc += (t_stop - t_start);
1597 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001598 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001599 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001600 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001601 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001602 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001603 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001604 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001605 t_misc/(float)CLOCKS_PER_SEC);
1606#endif
1607
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001608 if (ierror == 0)
1609 fprintf(STDERR, "libpng passes test\n");
1610 else
1611 fprintf(STDERR, "libpng FAILS test\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001612 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001613}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001614
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001615/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001616typedef version_1_4_0beta35 your_png_h_is_not_version_1_4_0beta35;