blob: 091f619a4232f0a887cb35990bb0c87d1c4e0364 [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-Pehrson67ee8ce2011-12-22 08:21:00 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 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-Pehrsonb3b71682011-05-03 22:30:19 -050034#define _POSIX_SOURCE 1
35
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050036#include "zlib.h"
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050037#include "png.h"
Glenn Randers-Pehrson72531442010-04-17 08:17:51 -050038/* Copied from pngpriv.h but only used in error messages below. */
39#ifndef PNG_ZBUF_SIZE
40# define PNG_ZBUF_SIZE 8192
41#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050042# include <stdio.h>
43# include <stdlib.h>
Glenn Randers-Pehrson72531442010-04-17 08:17:51 -050044# include <string.h>
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050045# define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050046
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050047#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050048typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060049#endif
50
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060051/* Makes pngtest verbose so we can find problems. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050052#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050053# define PNG_DEBUG 0
54#endif
55
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -060056#if PNG_DEBUG > 1
57# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
58# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
59# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
60#else
61# define pngtest_debug(m) ((void)0)
62# define pngtest_debug1(m,p1) ((void)0)
63# define pngtest_debug2(m,p1,p2) ((void)0)
64#endif
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060065
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050066#if !PNG_DEBUG
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050067# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060068#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050069
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050070/* Turn on CPU timing
71#define PNGTEST_TIMING
72*/
73
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050074#ifndef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060075#undef PNGTEST_TIMING
76#endif
77
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050078#ifdef PNGTEST_TIMING
79static float t_start, t_stop, t_decode, t_encode, t_misc;
80#include <time.h>
81#endif
82
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050083#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050084#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050085static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050086static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050087#endif
88
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050089static int verbose = 0;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -050090static int strict = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050091
Guy Schalnat0d580581995-07-20 02:43:20 -050092#ifdef __TURBOC__
93#include <mem.h>
94#endif
95
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050096/* Defined so I can write to a file on gui/windowing platforms */
Guy Schalnat6d764711995-12-19 03:22:19 -060097/* #define STDERR stderr */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050098#define STDERR stdout /* For DOS */
Guy Schalnat0d580581995-07-20 02:43:20 -050099
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -0500100/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
101#ifndef png_jmpbuf
102# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
103#endif
104
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500105/* Example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500106static int status_pass = 1;
107static int status_dots_requested = 0;
108static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600109
John Bowler8fb6c6a2012-01-25 07:47:44 -0600110static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600111read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600112{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500113 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
114 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500115
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500116 if (status_pass != pass)
117 {
118 fprintf(stdout, "\n Pass %d: ", pass);
119 status_pass = pass;
120 status_dots = 31;
121 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500122
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 status_dots--;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500124
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 if (status_dots == 0)
126 {
127 fprintf(stdout, "\n ");
128 status_dots=30;
129 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500130
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500131 fprintf(stdout, "r");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600132}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600133
John Bowler8fb6c6a2012-01-25 07:47:44 -0600134static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600135write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600136{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500137 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
138 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 fprintf(stdout, "w");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600141}
142
143
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500144#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500145/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500146 * but merely examine the row filters. We set this to 256 rather than
147 * 5 in case illegal filter values are present.)
148 */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500149static png_uint_32 filters_used[256];
John Bowler8fb6c6a2012-01-25 07:47:44 -0600150static void PNGCBAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500151count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
152{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153 if (png_ptr != NULL && row_info != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500154 ++filters_used[*(data - 1)];
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500155}
156#endif
157
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500158#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500159/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500160 * but merely count the zero samples)
161 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600162
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500163static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600164
John Bowler8fb6c6a2012-01-25 07:47:44 -0600165static void PNGCBAPI
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500166count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600167{
168 png_bytep dp = data;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500169 if (png_ptr == NULL)
170 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600171
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500172 /* Contents of row_info:
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600173 * png_uint_32 width width of row
174 * png_uint_32 rowbytes number of bytes in row
175 * png_byte color_type color type of pixels
176 * png_byte bit_depth bit depth of samples
177 * png_byte channels number of channels (1-4)
178 * png_byte pixel_depth bits per pixel (depth*channels)
179 */
180
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500181 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600182
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500183 if (row_info->color_type == 0 || row_info->color_type == 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600184 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500185 int pos = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500186 png_uint_32 n, nstop;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500187
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500188 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600189 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500190 if (row_info->bit_depth == 1)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500191 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500192 if (((*dp << pos++ ) & 0x80) == 0)
193 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
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-Pehrsonf24daf22010-05-06 09:44:04 -0500201
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500202 if (row_info->bit_depth == 2)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500203 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204 if (((*dp << (pos+=2)) & 0xc0) == 0)
205 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500206
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500207 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600208 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500209 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600210 dp++;
211 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500212 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500213
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500214 if (row_info->bit_depth == 4)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500215 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500216 if (((*dp << (pos+=4)) & 0xf0) == 0)
217 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500219 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600220 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500221 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600222 dp++;
223 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500224 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500225
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500226 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500227 if (*dp++ == 0)
228 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500229
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500230 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600231 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500232 if ((*dp | *(dp+1)) == 0)
233 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600234 dp+=2;
235 }
236 }
237 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500238 else /* Other color types */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600239 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500240 png_uint_32 n, nstop;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600241 int channel;
242 int color_channels = row_info->channels;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 if (row_info->color_type > 3)color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600244
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500245 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600246 {
247 for (channel = 0; channel < color_channels; channel++)
248 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500249 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500250 if (*dp++ == 0)
251 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500252
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500253 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600254 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500255 if ((*dp | *(dp+1)) == 0)
256 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500257
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600258 dp+=2;
259 }
260 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500261 if (row_info->color_type > 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600262 {
263 dp++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500264 if (row_info->bit_depth == 16)
265 dp++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600266 }
267 }
268 }
269}
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600270#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600271
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600272static int wrote_question = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600273
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500274#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600275/* START of code to validate stdio-free compilation */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500276/* These copies of the default read/write functions come from pngrio.c and
277 * pngwio.c. They allow "don't include stdio" testing of the library.
278 * This is the function that does the actual reading of data. If you are
279 * not reading from a standard C stream, you should create a replacement
280 * read_data function and use it at run time with png_set_read_fn(), rather
281 * than changing the library.
282 */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600283
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600284#ifdef PNG_IO_STATE_SUPPORTED
285void
286pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
287 png_uint_32 io_op);
288void
289pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
290 png_uint_32 io_op)
291{
292 png_uint_32 io_state = png_get_io_state(png_ptr);
293 int err = 0;
294
295 /* Check if the current operation (reading / writing) is as expected. */
296 if ((io_state & PNG_IO_MASK_OP) != io_op)
297 png_error(png_ptr, "Incorrect operation in I/O state");
298
299 /* Check if the buffer size specific to the current location
300 * (file signature / header / data / crc) is as expected.
301 */
302 switch (io_state & PNG_IO_MASK_LOC)
303 {
304 case PNG_IO_SIGNATURE:
305 if (data_length > 8)
306 err = 1;
307 break;
308 case PNG_IO_CHUNK_HDR:
309 if (data_length != 8)
310 err = 1;
311 break;
312 case PNG_IO_CHUNK_DATA:
313 break; /* no restrictions here */
314 case PNG_IO_CHUNK_CRC:
315 if (data_length != 4)
316 err = 1;
317 break;
318 default:
319 err = 1; /* uninitialized */
320 }
321 if (err)
322 png_error(png_ptr, "Bad I/O state or buffer size");
323}
324#endif
325
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600326static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500327pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600328{
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600329 png_size_t check = 0;
330 png_voidp io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600331
332 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
333 * instead of an int, which is what fread() actually returns.
334 */
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600335 io_ptr = png_get_io_ptr(png_ptr);
336 if (io_ptr != NULL)
337 {
338 check = fread(data, 1, length, (png_FILE_p)io_ptr);
339 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500340
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600341 if (check != length)
342 {
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600343 png_error(png_ptr, "Read Error");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600344 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600345
346#ifdef PNG_IO_STATE_SUPPORTED
347 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
348#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600349}
Guy Schalnat0d580581995-07-20 02:43:20 -0500350
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500351#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600352static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500353pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600354{
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500355 /* Do nothing; fflush() is said to be just a waste of energy. */
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600356 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600357}
358#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500359
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500360/* 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 -0500361 * not writing to a standard C stream, you should create a replacement
362 * write_data function and use it at run time with png_set_write_fn(), rather
363 * than changing the library.
364 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600365static void PNGCBAPI
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
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600370 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500371
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600372 if (check != length)
373 {
374 png_error(png_ptr, "Write Error");
375 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600376
377#ifdef PNG_IO_STATE_SUPPORTED
378 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
379#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600380}
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600381
382/* This function is called when there is a warning, but the library thinks
383 * it can continue anyway. Replacement functions don't have to do anything
384 * here if you don't want to. In the default configuration, png_ptr is
385 * not used, but it is passed in case it may be useful.
386 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600387static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500388pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600389{
390 PNG_CONST char *name = "UNKNOWN (ERROR!)";
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600391 char *test;
392 test = png_get_error_ptr(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500393
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600394 if (test == NULL)
395 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500396
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600397 else
398 fprintf(STDERR, "%s: libpng warning: %s\n", test, message);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600399}
400
401/* This is the default error handling function. Note that replacements for
402 * this function MUST NOT RETURN, or the program will likely crash. This
403 * function is used by default, or if the program supplies NULL for the
404 * error function pointer in png_set_error_fn().
405 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600406static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500407pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600408{
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500409 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500410 /* We can return because png_error calls the default handler, which is
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500411 * actually OK in this case.
412 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600413}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500414#endif /* !PNG_STDIO_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600415/* END of code to validate stdio-free compilation */
416
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600417/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500418#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600419
420/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500421 * 64K. However, zlib may allocate more then 64K if you don't tell
422 * it not to. See zconf.h and png.h for more information. zlib does
423 * need to allocate exactly 64K, so whatever you call here must
424 * have the ability to do that.
425 *
426 * This piece of code can be compiled to validate max 64K allocations
427 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
428 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600429typedef struct memory_information
430{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500431 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500432 png_voidp pointer;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600433 struct memory_information *next;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600434} memory_information;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600435typedef memory_information *memory_infop;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600436
437static memory_infop pinformation = NULL;
438static int current_allocation = 0;
439static int maximum_allocation = 0;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500440static int total_allocation = 0;
441static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600442
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600443png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
444 png_alloc_size_t size));
445void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600446
447png_voidp
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600448PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600449{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500450
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500451 /* png_malloc has already tested for NULL; png_create_struct calls
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500452 * png_debug_malloc directly, with png_ptr == NULL which is OK
453 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500454
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600455 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500456 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600457
458 /* This calls the library allocator twice, once to get the requested
459 buffer and once to get a new free list entry. */
460 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500461 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500462 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500463 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500464 pinfo = (memory_infop)png_malloc(png_ptr,
465 png_sizeof(*pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600466 pinfo->size = size;
467 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500468 total_allocation += size;
469 num_allocations ++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500470
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600471 if (current_allocation > maximum_allocation)
472 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500473
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500474 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500475 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500476
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500477 png_set_mem_fn(png_ptr,
478 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500479
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-Pehrsonf24daf22010-05-06 09:44:04 -0500487
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600488 pinfo->next = pinformation;
489 pinformation = pinfo;
490 /* Make sure the caller isn't assuming zeroed memory. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600491 memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500492
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500493 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600494 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500495 pinfo->pointer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500496
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600497 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600498 }
499}
500
501/* Free a pointer. It is removed from the list at the same time. */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600502void PNGCBAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500503png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600504{
505 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500506 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500507
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600508 if (ptr == 0)
509 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600510#if 0 /* This happens all the time. */
511 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
512#endif
513 return;
514 }
515
516 /* Unlink the element from the list. */
517 {
John Bowlerbaeb6d12011-11-26 18:21:02 -0600518 memory_infop *ppinfo = &pinformation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500519
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600520 for (;;)
521 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600522 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500523
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600524 if (pinfo->pointer == ptr)
525 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600526 *ppinfo = pinfo->next;
527 current_allocation -= pinfo->size;
528 if (current_allocation < 0)
529 fprintf(STDERR, "Duplicate free of memory\n");
530 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500531 the memory that is to be freed. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600532 memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500533 png_free_default(png_ptr, pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500534 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600535 break;
536 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500537
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600538 if (pinfo->next == NULL)
539 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500540 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600541 break;
542 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500543
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600544 ppinfo = &pinfo->next;
545 }
546 }
547
548 /* Finally free the data. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500549 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600550 printf("Freeing %p\n", ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500551
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500552 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500553 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600554}
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500555#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600556/* END of code to test memory allocation/deallocation */
557
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500558
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500559/* Demonstration of user chunk support of the sTER and vpAg chunks */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500560#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500561
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500562/* (sTER is a public chunk not yet known by libpng. vpAg is a private
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500563chunk used in ImageMagick to store "virtual page" size). */
564
565static png_uint_32 user_chunk_data[4];
566
Glenn Randers-Pehrsone826d7e2006-07-03 00:21:58 -0500567 /* 0: sTER mode + 1
568 * 1: vpAg width
569 * 2: vpAg height
570 * 3: vpAg units
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500571 */
572
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600573static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr,
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500574 png_unknown_chunkp chunk)
575{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500576 png_uint_32
577 *my_user_chunk_data;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500578
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500579 /* Return one of the following:
580 * return (-n); chunk had an error
581 * return (0); did not recognize
582 * return (n); success
583 *
584 * The unknown chunk structure contains the chunk data:
585 * png_byte name[5];
586 * png_byte *data;
587 * png_size_t size;
588 *
589 * Note that libpng has already taken care of the CRC handling.
590 */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500591
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500592 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
593 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
594 {
595 /* Found sTER chunk */
596 if (chunk->size != 1)
597 return (-1); /* Error return */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500598
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500599 if (chunk->data[0] != 0 && chunk->data[0] != 1)
600 return (-1); /* Invalid mode */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500601
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500602 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
603 my_user_chunk_data[0]=chunk->data[0]+1;
604 return (1);
605 }
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500606
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500607 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
608 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
609 return (0); /* Did not recognize */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500610
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500611 /* Found ImageMagick vpAg chunk */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500612
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613 if (chunk->size != 9)
614 return (-1); /* Error return */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500615
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500616 my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500617
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500618 my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);
619 my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);
620 my_user_chunk_data[3]=(png_uint_32)chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500621
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500622 return (1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500623
624}
625#endif
626/* END of code to demonstrate user chunk support */
627
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600628/* Test one file */
John Bowler8fb6c6a2012-01-25 07:47:44 -0600629static int
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600630test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500631{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500632 static png_FILE_p fpin;
633 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500634 png_structp read_ptr;
635 png_infop read_info_ptr, end_info_ptr;
636#ifdef PNG_WRITE_SUPPORTED
637 png_structp write_ptr;
638 png_infop write_info_ptr;
639 png_infop write_end_info_ptr;
640#else
641 png_structp write_ptr = NULL;
642 png_infop write_info_ptr = NULL;
643 png_infop write_end_info_ptr = NULL;
644#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600645 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500646 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500647 png_uint_32 width, height;
648 int num_pass, pass;
649 int bit_depth, color_type;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600650
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600651 char inbuf[256], outbuf[256];
652
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500653 row_buf = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500654
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600656 {
657 fprintf(STDERR, "Could not find input file %s\n", inname);
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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600662 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500663 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500664 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600665 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600666 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500667
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600668 pngtest_debug("Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500669#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500670 read_ptr =
671 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600672 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500673#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500674 read_ptr =
675 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500676#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500677#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500678 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
679 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600680#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500681
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500682#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500683 user_chunk_data[0] = 0;
684 user_chunk_data[1] = 0;
685 user_chunk_data[2] = 0;
686 user_chunk_data[3] = 0;
687 png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
688 read_user_chunk_callback);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500689
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500690#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500691#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500692#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 write_ptr =
694 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
695 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500696#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500697 write_ptr =
698 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500699#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500700#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500701 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
702 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600703#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500704#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600705 pngtest_debug("Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500706 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500707 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500708#ifdef PNG_WRITE_SUPPORTED
709 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500710 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500711#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500712
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600713#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600714 pngtest_debug("Setting jmpbuf for read struct");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600715 if (setjmp(png_jmpbuf(read_ptr)))
Guy Schalnat0f716451995-11-28 11:22:13 -0600716 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600717 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500718 png_free(read_ptr, row_buf);
719 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500721#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500722 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500724#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500725 FCLOSE(fpin);
726 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600727 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600728 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500730#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600731 pngtest_debug("Setting jmpbuf for write struct");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500732
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600733 if (setjmp(png_jmpbuf(write_ptr)))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600734 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600735 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500737 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500738#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500740#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500741 FCLOSE(fpin);
742 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600743 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600744 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600745#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500746#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600747
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600748 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500749#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500750 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500751# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500752 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500753# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600754#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500755 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500756# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500757 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500758# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500759 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500760# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600761 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500762# endif
763# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600764#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500765
John Bowlerb2bee332011-06-10 23:24:58 -0500766#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500767 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
768 * This is here just to make pngtest replicate the results from libpng
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500769 * versions prior to 1.5.4, and to test this new API.
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500770 */
Glenn Randers-Pehrson6bdea982011-03-31 21:14:55 -0500771 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
772#endif
773
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500774 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600775 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500776#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600777 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500778#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600779 png_set_read_status_fn(read_ptr, read_row_callback);
780 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600782 else
783 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500784#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500785 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500786#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500787 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600788 }
789
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500790#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500791 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500792 int i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500794 for (i = 0; i<256; i++)
795 filters_used[i] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500796
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500797 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500798 }
799#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500800#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500801 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500802 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
803#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500804
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500805#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500806# ifndef PNG_HANDLE_CHUNK_ALWAYS
807# define PNG_HANDLE_CHUNK_ALWAYS 3
808# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500809 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
810 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600811#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500812#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500813# ifndef PNG_HANDLE_CHUNK_IF_SAFE
814# define PNG_HANDLE_CHUNK_IF_SAFE 2
815# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500816 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
817 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600818#endif
819
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600820 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500821 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500822
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600823 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 {
825 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500826
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
828 &color_type, &interlace_type, &compression_type, &filter_type))
829 {
830 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500831#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600833#else
834 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
835#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500836 }
837 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500838#ifdef PNG_FIXED_POINT_SUPPORTED
839#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500840 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841 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 -0600842 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500843
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600844 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
845 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500846 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600847 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500848 red_y, green_x, green_y, blue_x, blue_y);
849 }
850 }
851#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500852#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600854 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 }
859#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500860#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500861#ifdef PNG_FLOATING_POINT_SUPPORTED
862#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500863 {
864 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
865 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500866
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500867 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
868 &red_y, &green_x, &green_y, &blue_x, &blue_y))
869 {
870 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
871 red_y, green_x, green_y, blue_x, blue_y);
872 }
873 }
874#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500875#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500876 {
877 double gamma;
878
879 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500880 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500881 }
882#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500883#endif /* Floating point */
884#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500885#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600886 {
887 png_charp name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500888 png_bytep profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600889 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600890 int compression_type;
891
892 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
893 &profile, &proflen))
894 {
895 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
896 profile, proflen);
897 }
898 }
899#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500900#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600901 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600902 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600903
904 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600905 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600906 }
907#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600908 {
909 png_colorp palette;
910 int num_palette;
911
912 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600913 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500915#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600916 {
917 png_color_16p background;
918
919 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
920 {
921 png_set_bKGD(write_ptr, write_info_ptr, background);
922 }
923 }
924#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500925#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500926 {
927 png_uint_16p hist;
928
929 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500930 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500931 }
932#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500933#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500934 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600935 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 int unit_type;
937
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500938 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500939 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 {
941 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
942 }
943 }
944#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500945#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500946 {
947 png_charp purpose, units;
948 png_charpp params;
949 png_int_32 X0, X1;
950 int type, nparams;
951
952 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
953 &nparams, &units, &params))
954 {
955 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
956 nparams, units, params);
957 }
958 }
959#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500960#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500961 {
962 png_uint_32 res_x, res_y;
963 int unit_type;
964
965 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500966 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500967 }
968#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500969#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500970 {
971 png_color_8p sig_bit;
972
973 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 }
976#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -0500977#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600978#ifdef PNG_FLOATING_POINT_SUPPORTED
979 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600980 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500981 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600982
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500983 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
984 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600985 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500986 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600987 }
988 }
989#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600990#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600991 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600992 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500993 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500995 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
996 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600997 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600998 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
999 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001000 }
1001 }
1002#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001003#endif
1004#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001005#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 {
1007 png_textp text_ptr;
1008 int num_text;
1009
1010 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1011 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001012 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001013
1014 if (verbose)
1015 printf("\nText compression=%d\n", text_ptr->compression);
1016
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1018 }
1019 }
1020#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001021#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001022 {
1023 png_timep mod_time;
1024
1025 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1026 {
1027 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001028#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001029 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
1030 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001031
John Bowler40b26032011-12-22 08:09:15 -06001032 else
1033 strcpy(tIME_string, "*** invalid time ***");
1034
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001035 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001036#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001037 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 }
1039#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001040#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001042 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001044 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001046 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001047 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001049 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001050 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001051 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001052 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001053 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001054 ((int)trans_color->red > sample_max ||
1055 (int)trans_color->green > sample_max ||
1056 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001057 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001058 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001059 }
1060 }
1061#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001062#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001063 {
1064 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001065 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001066 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001067
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001068 if (num_unknowns)
1069 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001070 int i;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001071 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1072 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001073 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074 * generated locations in write_info_ptr are wrong because we
1075 * haven't written anything yet.
1076 */
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001077 for (i = 0; i < num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001078 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1079 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001080 }
1081 }
1082#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001084#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001085 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001086
1087/* If we wanted, we could write info in two steps:
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001088 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001089 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001091
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001092#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001093 if (user_chunk_data[0] != 0)
1094 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001095 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001096
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001097 unsigned char
1098 ster_chunk_data[1];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001099
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100 if (verbose)
1101 fprintf(STDERR, "\n stereo mode = %lu\n",
1102 (unsigned long)(user_chunk_data[0] - 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001103
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001104 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 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001107
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001108 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1109 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001110 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001111
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001112 unsigned char
1113 vpag_chunk_data[9];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 if (verbose)
1116 fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
1117 (unsigned long)user_chunk_data[1],
1118 (unsigned long)user_chunk_data[2],
1119 (unsigned long)user_chunk_data[3]);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001120
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001121 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1122 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1123 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1124 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001125 }
1126
1127#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001128#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001130#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001131 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001132 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001134
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001135 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001136#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001137 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001138
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001139#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1140 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001142# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001144# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001145#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001146 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001147#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001148
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001149#ifdef PNGTEST_TIMING
1150 t_stop = (float)clock();
1151 t_misc += (t_stop - t_start);
1152 t_start = t_stop;
1153#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001154 for (pass = 0; pass < num_pass; pass++)
1155 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001156 pngtest_debug1("Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001158 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001159#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001160 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001161 row_buf = (png_bytep)png_malloc(read_ptr,
1162 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001163
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001164 pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001165 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001166
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001167#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001168 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001169
1170#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001171#ifdef PNGTEST_TIMING
1172 t_stop = (float)clock();
1173 t_decode += (t_stop - t_start);
1174 t_start = t_stop;
1175#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001176 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001177#ifdef PNGTEST_TIMING
1178 t_stop = (float)clock();
1179 t_encode += (t_stop - t_start);
1180 t_start = t_stop;
1181#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001182#endif /* PNG_WRITE_SUPPORTED */
1183
1184#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001185 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001186 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001187 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001188#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001189 }
1190 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001191
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001192#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001193 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001194#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001195#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001196 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001197#endif
1198
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001199 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001200
Andreas Dilger47a0c421997-05-16 02:46:07 -05001201 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001202#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001203 {
1204 png_textp text_ptr;
1205 int num_text;
1206
1207 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1208 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001209 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001210 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1211 }
1212 }
1213#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001214#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001215 {
1216 png_timep mod_time;
1217
1218 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1219 {
1220 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001221#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001222 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
1223 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001224
John Bowler40b26032011-12-22 08:09:15 -06001225 else
1226 strcpy(tIME_string, "*** invalid time ***");
1227
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001228 tIME_chunk_present++;
1229#endif /* PNG_TIME_RFC1123_SUPPORTED */
1230 }
1231 }
1232#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001233#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001234 {
1235 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001236 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001237 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001238
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001239 if (num_unknowns)
1240 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001241 int i;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001242 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-Pehrson67439c42010-08-19 07:01:09 -05001248 for (i = 0; i < 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-Pehrson31f92b02010-03-09 16:47:59 -06001269 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001270#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001271 pngtest_debug("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-Pehrson31f92b02010-03-09 16:47:59 -06001275 pngtest_debug("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-Pehrson31f92b02010-03-09 16:47:59 -06001278 pngtest_debug("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-Pehrson31f92b02010-03-09 16:47:59 -06001280 pngtest_debug("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-Pehrson31f92b02010-03-09 16:47:59 -06001283 pngtest_debug("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-Pehrson31f92b02010-03-09 16:47:59 -06001288 pngtest_debug("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-Pehrsonf24daf22010-05-06 09:44:04 -05001313
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001314 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001315 {
1316 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001317 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001318 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001319 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001320 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001321 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001322 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001323 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001324 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001325 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001326
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001327 FCLOSE(fpin);
1328 FCLOSE(fpout);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001329
1330 if (strict != 0)
1331 return (1);
1332
1333 else
1334 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001335 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001336
Guy Schalnat0f716451995-11-28 11:22:13 -06001337 if (!num_in)
1338 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001339
John Bowlerbaeb6d12011-11-26 18:21:02 -06001340 if (memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001341 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001342 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001343
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001344 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001345 {
1346 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001348 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001349 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001350 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001351 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001352 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001353 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001354 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001355 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001356
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001357 FCLOSE(fpin);
1358 FCLOSE(fpout);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001359
1360 if (strict != 0)
1361 return (1);
1362
1363 else
1364 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001365 }
1366 }
1367
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001368 FCLOSE(fpin);
1369 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001370
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001371 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001372}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001373
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001374/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001375#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001376static PNG_CONST char *inname = "pngtest/png";
1377static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001378#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001379static PNG_CONST char *inname = "pngtest.png";
1380static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001381#endif
1382
1383int
1384main(int argc, char *argv[])
1385{
1386 int multiple = 0;
1387 int ierror = 0;
1388
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001389 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001390 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001391 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001392 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001393 fprintf(STDERR, " library (%lu):%s",
1394 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001395 png_get_header_version(NULL));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001396
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001397 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001398 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001399 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001400
1401 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001402 * not sure this matters, but it is nice to know, the first of these
1403 * tests should be impossible because of the way the macros are set
1404 * in pngconf.h
1405 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001406#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001407 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001408#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001409 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001410#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001411 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001412#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001413
1414 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1415 {
1416 fprintf(STDERR,
1417 "Warning: versions are different between png.h and png.c\n");
1418 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1419 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1420 ++ierror;
1421 }
1422
1423 if (argc > 1)
1424 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001425 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001426 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001427 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001428 status_dots_requested = 0;
1429 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001430
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001431 else if (strcmp(argv[1], "-mv") == 0 ||
1432 strcmp(argv[1], "-vm") == 0 )
1433 {
1434 multiple = 1;
1435 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001436 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001437 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001438
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001439 else if (strcmp(argv[1], "-v") == 0)
1440 {
1441 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001442 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001443 inname = argv[2];
1444 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001445
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001446 else if (strcmp(argv[1], "--strict") == 0)
1447 {
1448 status_dots_requested = 0;
1449 verbose = 1;
1450 inname = argv[2];
1451 strict++;
1452 }
1453
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001454 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001455 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001456 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001457 status_dots_requested = 0;
1458 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001459 }
1460
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001461 if (!multiple && argc == 3 + verbose)
1462 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001463
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001464 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001465 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001466 fprintf(STDERR,
1467 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001468 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001469 fprintf(STDERR,
1470 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1471 fprintf(STDERR,
1472 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001473 exit(1);
1474 }
1475
1476 if (multiple)
1477 {
1478 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001479#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001480 int allocation_now = current_allocation;
1481#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001482 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001483 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001484 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001485 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001486 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001487 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001488 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001489#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1490 int k;
1491#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001492#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001493 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001494 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001495#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001496 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001497#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001498#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001499 for (k = 0; k<256; k++)
1500 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001501 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001502 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001503#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001504#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001505 if (tIME_chunk_present != 0)
1506 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001507
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001508 tIME_chunk_present = 0;
1509#endif /* PNG_TIME_RFC1123_SUPPORTED */
1510 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001511
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001512 else
1513 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001514 fprintf(STDERR, " FAIL\n");
1515 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001516 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001517#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001518 if (allocation_now != current_allocation)
1519 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001520 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001521
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001522 if (current_allocation != 0)
1523 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001524 memory_infop pinfo = pinformation;
1525
1526 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1527 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001528
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001529 while (pinfo != NULL)
1530 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001531 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001532 (unsigned long)pinfo->size,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001533 (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001534 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001535 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001536 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001537#endif
1538 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001539#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001540 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001541 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001542 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001543 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001544 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1545 total_allocation);
1546 fprintf(STDERR, " Number of allocations: %10d\n",
1547 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001548#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001549 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001550
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001551 else
1552 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001553 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001554 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001555 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001556 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001557#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001558 int allocation_now = current_allocation;
1559#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001560 if (i == 1)
1561 status_dots_requested = 1;
1562
1563 else if (verbose == 0)
1564 status_dots_requested = 0;
1565
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001566 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001567 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001568
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001569 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001570
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001571 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001572 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001573 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001574 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001575#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001576 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001577#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001578#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001579 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001580 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001581#else
1582 fprintf(STDERR, " PASS\n");
1583#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001584#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001585 for (k = 0; k<256; k++)
1586 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001587 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001588 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001589#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001590#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001591 if (tIME_chunk_present != 0)
1592 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001593#endif /* PNG_TIME_RFC1123_SUPPORTED */
1594 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001595 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001596
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001597 else
1598 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001599 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001600 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001601
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001602 fprintf(STDERR, " FAIL\n");
1603 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001604 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001605#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001606 if (allocation_now != current_allocation)
1607 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001608 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001609
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001610 if (current_allocation != 0)
1611 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001612 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001613
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001614 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1615 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001616
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001617 while (pinfo != NULL)
1618 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001619 fprintf(STDERR, " %lu bytes at %x\n",
1620 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001621 pinfo = pinfo->next;
1622 }
1623 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001624#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001625 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001626#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001627 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001628 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001629 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001630 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001631 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1632 total_allocation);
1633 fprintf(STDERR, " Number of allocations: %10d\n",
1634 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001635#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001636 }
1637
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001638#ifdef PNGTEST_TIMING
1639 t_stop = (float)clock();
1640 t_misc += (t_stop - t_start);
1641 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001642 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001643 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001644 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001645 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001646 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001647 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001648 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001649 t_misc/(float)CLOCKS_PER_SEC);
1650#endif
1651
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001652 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001653 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001654
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001655 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001656 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001657
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001658 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001659}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001660
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001661/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson99c7ad22012-01-24 07:58:30 -06001662typedef png_libpng_version_1_6_0beta07 Your_png_h_is_not_version_1_6_0beta07;