blob: f69ed2b3e811e58a1da386cda8b586c720691ac0 [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-Pehrson35f2e172012-03-10 19:42:03 -0600748 if (strict)
749 {
750 /* Treat png_benign_error() as errors on read */
751 png_set_benign_errors(read_ptr, 0);
752
753 /* Treat them as errors on write */
754 png_set_benign_errors(write_ptr, 0);
755
756 /* if strict is not set, then both are treated as warnings. */
757 }
758
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600759 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500760#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500761 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500762# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500763 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500764# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600765#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500766 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500767# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500768 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500769# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500770 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500771# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600772 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500773# endif
774# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600775#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500776
John Bowlerb2bee332011-06-10 23:24:58 -0500777#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500778 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
779 * This is here just to make pngtest replicate the results from libpng
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500780 * versions prior to 1.5.4, and to test this new API.
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500781 */
Glenn Randers-Pehrson6bdea982011-03-31 21:14:55 -0500782 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
783#endif
784
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500785 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600786 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500787#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600788 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500789#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600790 png_set_read_status_fn(read_ptr, read_row_callback);
791 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500792
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600793 else
794 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500795#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500796 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500797#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500798 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600799 }
800
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500801#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500802 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500803 int i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500804
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 for (i = 0; i<256; i++)
806 filters_used[i] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500808 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500809 }
810#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500811#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500812 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500813 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
814#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500816#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500817# ifndef PNG_HANDLE_CHUNK_ALWAYS
818# define PNG_HANDLE_CHUNK_ALWAYS 3
819# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500820 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
821 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600822#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500823#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500824# ifndef PNG_HANDLE_CHUNK_IF_SAFE
825# define PNG_HANDLE_CHUNK_IF_SAFE 2
826# endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
828 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829#endif
830
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600831 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500833
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600834 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 {
836 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500837
Andreas Dilger47a0c421997-05-16 02:46:07 -0500838 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
839 &color_type, &interlace_type, &compression_type, &filter_type))
840 {
841 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500842#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600844#else
845 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
846#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 }
848 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500849#ifdef PNG_FIXED_POINT_SUPPORTED
850#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600852 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600853 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500854
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600855 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
856 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500857 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600858 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500859 red_y, green_x, green_y, blue_x, blue_y);
860 }
861 }
862#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500863#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600865 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500866
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600868 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869 }
870#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500871#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500872#ifdef PNG_FLOATING_POINT_SUPPORTED
873#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500874 {
875 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
876 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500877
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500878 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
879 &red_y, &green_x, &green_y, &blue_x, &blue_y))
880 {
881 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
882 red_y, green_x, green_y, blue_x, blue_y);
883 }
884 }
885#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500886#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500887 {
888 double gamma;
889
890 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500891 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500892 }
893#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500894#endif /* Floating point */
895#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500896#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600897 {
898 png_charp name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500899 png_bytep profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600900 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901 int compression_type;
902
903 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
904 &profile, &proflen))
905 {
906 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
907 profile, proflen);
908 }
909 }
910#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500911#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600912 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600913 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600914
915 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600916 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600917 }
918#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600919 {
920 png_colorp palette;
921 int num_palette;
922
923 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600924 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600925 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500926#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600927 {
928 png_color_16p background;
929
930 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
931 {
932 png_set_bKGD(write_ptr, write_info_ptr, background);
933 }
934 }
935#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500936#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 {
938 png_uint_16p hist;
939
940 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942 }
943#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500944#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500945 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600946 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 int unit_type;
948
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500949 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500950 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 {
952 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
953 }
954 }
955#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500956#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500957 {
958 png_charp purpose, units;
959 png_charpp params;
960 png_int_32 X0, X1;
961 int type, nparams;
962
963 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
964 &nparams, &units, &params))
965 {
966 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
967 nparams, units, params);
968 }
969 }
970#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500971#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500972 {
973 png_uint_32 res_x, res_y;
974 int unit_type;
975
976 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500977 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978 }
979#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500980#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500981 {
982 png_color_8p sig_bit;
983
984 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500985 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 }
987#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -0500988#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989#ifdef PNG_FLOATING_POINT_SUPPORTED
990 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600991 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500992 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500994 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
995 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600996 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500997 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998 }
999 }
1000#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001003 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001004 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001006 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1007 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001009 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1010 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011 }
1012 }
1013#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001014#endif
1015#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001016#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 {
1018 png_textp text_ptr;
1019 int num_text;
1020
1021 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1022 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001023 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001024
1025 if (verbose)
Glenn Randers-Pehrsonf2715a52012-03-15 19:52:03 -05001026 {
1027 int i;
1028
1029 printf("\n");
1030 for (i=0; i<num_text; i++)
1031 {
1032 printf(" Text compression[%d]=%d\n",
1033 i, text_ptr[i].compression);
1034 }
1035 }
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001036
Andreas Dilger47a0c421997-05-16 02:46:07 -05001037 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1038 }
1039 }
1040#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001041#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001042 {
1043 png_timep mod_time;
1044
1045 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1046 {
1047 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001048#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001049 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
1050 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001051
John Bowler40b26032011-12-22 08:09:15 -06001052 else
1053 strcpy(tIME_string, "*** invalid time ***");
1054
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001055 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001056#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001057 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058 }
1059#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001060#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001062 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001063 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001064 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001066 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001067 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001068 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001069 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001070 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001071 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001072 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001073 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074 ((int)trans_color->red > sample_max ||
1075 (int)trans_color->green > sample_max ||
1076 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001077 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001078 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001079 }
1080 }
1081#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001082#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001083 {
1084 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001085 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001086 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001087
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001088 if (num_unknowns)
1089 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001090 int i;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001091 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1092 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001093 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001094 * generated locations in write_info_ptr are wrong because we
1095 * haven't written anything yet.
1096 */
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001097 for (i = 0; i < num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001098 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1099 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001100 }
1101 }
1102#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001104#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001105 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001106
1107/* If we wanted, we could write info in two steps:
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001108 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001109 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001111
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001112#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001113 if (user_chunk_data[0] != 0)
1114 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001116
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001117 unsigned char
1118 ster_chunk_data[1];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001119
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001120 if (verbose)
1121 fprintf(STDERR, "\n stereo mode = %lu\n",
1122 (unsigned long)(user_chunk_data[0] - 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001123
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001124 ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
1125 png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001126 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001127
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001128 if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
1129 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001130 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001131
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001132 unsigned char
1133 vpag_chunk_data[9];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001134
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001135 if (verbose)
1136 fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
1137 (unsigned long)user_chunk_data[1],
1138 (unsigned long)user_chunk_data[2],
1139 (unsigned long)user_chunk_data[3]);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001140
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001141 png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
1142 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
1143 vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
1144 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001145 }
1146
1147#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001148#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001150#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001151 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001152 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001153 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001154
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001155 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001156#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001157 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001158
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001159#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1160 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001162# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001163 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001164# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001165#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001166 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001167#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001168
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001169#ifdef PNGTEST_TIMING
1170 t_stop = (float)clock();
1171 t_misc += (t_stop - t_start);
1172 t_start = t_stop;
1173#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001174 for (pass = 0; pass < num_pass; pass++)
1175 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001176 pngtest_debug1("Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001178 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001179#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001180 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001181 row_buf = (png_bytep)png_malloc(read_ptr,
1182 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001183
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001184 pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001185 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001186
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001187#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001188 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001189
1190#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001191#ifdef PNGTEST_TIMING
1192 t_stop = (float)clock();
1193 t_decode += (t_stop - t_start);
1194 t_start = t_stop;
1195#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001196 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001197#ifdef PNGTEST_TIMING
1198 t_stop = (float)clock();
1199 t_encode += (t_stop - t_start);
1200 t_start = t_stop;
1201#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001202#endif /* PNG_WRITE_SUPPORTED */
1203
1204#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001205 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001206 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001207 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001208#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001209 }
1210 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001211
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001212#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001213 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001214#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001215#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001216 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001217#endif
1218
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001219 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001220
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001222#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001223 {
1224 png_textp text_ptr;
1225 int num_text;
1226
1227 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1228 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001229 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrsonf2715a52012-03-15 19:52:03 -05001230
1231 if (verbose)
1232 {
1233 int i;
1234
1235 printf("\n");
1236 for (i=0; i<num_text; i++)
1237 {
1238 printf(" Text compression[%d]=%d\n",
1239 i, text_ptr[i].compression);
1240 }
1241 }
1242
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001243 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1244 }
1245 }
1246#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001247#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001248 {
1249 png_timep mod_time;
1250
1251 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1252 {
1253 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001254#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001255 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
1256 tIME_string[png_sizeof(tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001257
John Bowler40b26032011-12-22 08:09:15 -06001258 else
1259 strcpy(tIME_string, "*** invalid time ***");
1260
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001261 tIME_chunk_present++;
1262#endif /* PNG_TIME_RFC1123_SUPPORTED */
1263 }
1264 }
1265#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001266#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001267 {
1268 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001269 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001270 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001271
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001272 if (num_unknowns)
1273 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001274 int i;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001275 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1276 num_unknowns);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001277 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001278 * generated locations in write_end_info_ptr are wrong because we
1279 * haven't written the end_info yet.
1280 */
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001281 for (i = 0; i < num_unknowns; i++)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001282 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1283 unknowns[i].location);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001284 }
1285 }
1286#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001287#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001288 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001289#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001290
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001291#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001292 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001293 {
1294 png_uint_32 iwidth, iheight;
1295 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1296 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001297 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001298 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001299 }
1300#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001301
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001302 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001303#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001304 pngtest_debug("destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001305 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001306 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001307#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001308 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001310#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001311 pngtest_debug("destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001312 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001313 pngtest_debug("destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001315#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001316 pngtest_debug("Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001317
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001318 FCLOSE(fpin);
1319 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001320
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001321 pngtest_debug("Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001323 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001324 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001325 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001326 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001327
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001329 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001330 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001331 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001332 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001333 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001335 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001336 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001337 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001339 num_in = fread(inbuf, 1, 1, fpin);
1340 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001341
Guy Schalnat0f716451995-11-28 11:22:13 -06001342 if (num_in != num_out)
1343 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001344 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Guy Schalnate5a37791996-06-05 15:50:50 -05001345 inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001346
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001347 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001348 {
1349 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001350 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001351 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001352 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001353 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001354 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001356 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001357 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001358 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001359
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001360 FCLOSE(fpin);
1361 FCLOSE(fpout);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001362
1363 if (strict != 0)
1364 return (1);
1365
1366 else
1367 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001368 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001369
Guy Schalnat0f716451995-11-28 11:22:13 -06001370 if (!num_in)
1371 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001372
John Bowlerbaeb6d12011-11-26 18:21:02 -06001373 if (memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -06001374 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001375 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001376
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001377 if (wrote_question == 0)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001378 {
1379 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001380 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001381 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001382 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001383 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001384 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001385 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001386 ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001387 wrote_question = 1;
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001388 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001389
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001390 FCLOSE(fpin);
1391 FCLOSE(fpout);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001392
1393 if (strict != 0)
1394 return (1);
1395
1396 else
1397 return (0);
Guy Schalnat0f716451995-11-28 11:22:13 -06001398 }
1399 }
1400
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001401 FCLOSE(fpin);
1402 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001403
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001404 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001405}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001406
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001407/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001408#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001409static PNG_CONST char *inname = "pngtest/png";
1410static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001411#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001412static PNG_CONST char *inname = "pngtest.png";
1413static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001414#endif
1415
1416int
1417main(int argc, char *argv[])
1418{
1419 int multiple = 0;
1420 int ierror = 0;
1421
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001422 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001423 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001424 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001425 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001426 fprintf(STDERR, " library (%lu):%s",
1427 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001428 png_get_header_version(NULL));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001429
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001430 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001431 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001432 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001433
1434 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001435 * not sure this matters, but it is nice to know, the first of these
1436 * tests should be impossible because of the way the macros are set
1437 * in pngconf.h
1438 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001439#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001440 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001441#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001442 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001443#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001444 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001445#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001446
1447 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1448 {
1449 fprintf(STDERR,
1450 "Warning: versions are different between png.h and png.c\n");
1451 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1452 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1453 ++ierror;
1454 }
1455
1456 if (argc > 1)
1457 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001458 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001459 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001460 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001461 status_dots_requested = 0;
1462 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001463
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001464 else if (strcmp(argv[1], "-mv") == 0 ||
1465 strcmp(argv[1], "-vm") == 0 )
1466 {
1467 multiple = 1;
1468 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001469 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001470 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001471
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001472 else if (strcmp(argv[1], "-v") == 0)
1473 {
1474 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001475 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001476 inname = argv[2];
1477 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001478
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001479 else if (strcmp(argv[1], "--strict") == 0)
1480 {
1481 status_dots_requested = 0;
1482 verbose = 1;
1483 inname = argv[2];
1484 strict++;
1485 }
1486
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001487 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001488 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001489 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001490 status_dots_requested = 0;
1491 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001492 }
1493
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001494 if (!multiple && argc == 3 + verbose)
1495 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001496
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001497 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001498 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001499 fprintf(STDERR,
1500 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001501 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001502 fprintf(STDERR,
1503 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1504 fprintf(STDERR,
1505 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001506 exit(1);
1507 }
1508
1509 if (multiple)
1510 {
1511 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001512#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001513 int allocation_now = current_allocation;
1514#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001515 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001516 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001517 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001518 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001519 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001520 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001521 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001522#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1523 int k;
1524#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001525#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001526 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001527 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001528#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001529 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001530#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001531#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001532 for (k = 0; k<256; k++)
1533 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001534 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001535 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001536#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001537#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001538 if (tIME_chunk_present != 0)
1539 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001540
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001541 tIME_chunk_present = 0;
1542#endif /* PNG_TIME_RFC1123_SUPPORTED */
1543 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001544
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001545 else
1546 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001547 fprintf(STDERR, " FAIL\n");
1548 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001549 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001550#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001551 if (allocation_now != current_allocation)
1552 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001553 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001554
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001555 if (current_allocation != 0)
1556 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001557 memory_infop pinfo = pinformation;
1558
1559 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1560 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001561
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001562 while (pinfo != NULL)
1563 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001564 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001565 (unsigned long)pinfo->size,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001566 (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001567 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001568 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001569 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001570#endif
1571 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001572#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001573 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001574 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001575 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001576 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001577 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1578 total_allocation);
1579 fprintf(STDERR, " Number of allocations: %10d\n",
1580 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001581#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001582 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001583
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001584 else
1585 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001586 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001587 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001588 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001589 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001590#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001591 int allocation_now = current_allocation;
1592#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001593 if (i == 1)
1594 status_dots_requested = 1;
1595
1596 else if (verbose == 0)
1597 status_dots_requested = 0;
1598
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001599 if (i == 0 || verbose == 1 || ierror != 0)
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 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001603
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001604 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001605 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001606 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001607 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001608#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001609 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001610#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001611#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001612 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001613 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001614#else
1615 fprintf(STDERR, " PASS\n");
1616#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001617#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001618 for (k = 0; k<256; k++)
1619 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001620 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001621 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001622#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001623#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001624 if (tIME_chunk_present != 0)
1625 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001626#endif /* PNG_TIME_RFC1123_SUPPORTED */
1627 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001628 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001629
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001630 else
1631 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001632 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001633 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001634
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001635 fprintf(STDERR, " FAIL\n");
1636 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001637 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001638#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001639 if (allocation_now != current_allocation)
1640 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001641 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001642
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001643 if (current_allocation != 0)
1644 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001645 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001646
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001647 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1648 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001649
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001650 while (pinfo != NULL)
1651 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001652 fprintf(STDERR, " %lu bytes at %x\n",
1653 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001654 pinfo = pinfo->next;
1655 }
1656 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001657#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001658 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001659#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001660 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001661 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001662 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001663 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001664 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1665 total_allocation);
1666 fprintf(STDERR, " Number of allocations: %10d\n",
1667 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001668#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001669 }
1670
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001671#ifdef PNGTEST_TIMING
1672 t_stop = (float)clock();
1673 t_misc += (t_stop - t_start);
1674 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001675 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001676 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001677 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001678 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001679 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001680 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001681 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001682 t_misc/(float)CLOCKS_PER_SEC);
1683#endif
1684
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001685 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001686 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001687
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001688 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001689 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001690
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001691 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001692}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001693
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001694/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson71db1312012-03-09 22:09:46 -06001695typedef png_libpng_version_1_6_0beta18 Your_png_h_is_not_version_1_6_0beta18;