blob: bb4a6d4339b0b2344b98d573c9b5b9ab472e01e9 [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
John Bowlere2098ba2012-08-10 17:04:56 -050036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40/* Defined so I can write to a file on gui/windowing platforms */
41/* #define STDERR stderr */
42#define STDERR stdout /* For DOS */
43
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050044#include "png.h"
John Bowlere2098ba2012-08-10 17:04:56 -050045
46#ifdef PNG_READ_SUPPORTED /* else nothing can be done */
47#include "zlib.h"
Glenn Randers-Pehrson72531442010-04-17 08:17:51 -050048/* Copied from pngpriv.h but only used in error messages below. */
49#ifndef PNG_ZBUF_SIZE
50# define PNG_ZBUF_SIZE 8192
51#endif
John Bowlere2098ba2012-08-10 17:04:56 -050052#define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050053
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050054#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050055typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060056#endif
57
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060058/* Makes pngtest verbose so we can find problems. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050059#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050060# define PNG_DEBUG 0
61#endif
62
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -060063#if PNG_DEBUG > 1
64# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
65# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
66# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
67#else
68# define pngtest_debug(m) ((void)0)
69# define pngtest_debug1(m,p1) ((void)0)
70# define pngtest_debug2(m,p1,p2) ((void)0)
71#endif
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060072
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050073#if !PNG_DEBUG
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050074# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -060075#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050076
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050077/* Turn on CPU timing
78#define PNGTEST_TIMING
79*/
80
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050081#ifndef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060082#undef PNGTEST_TIMING
83#endif
84
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050085#ifdef PNGTEST_TIMING
86static float t_start, t_stop, t_decode, t_encode, t_misc;
87#include <time.h>
88#endif
89
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050090#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050091#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050092static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -050093static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050094#endif
95
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -050096static int verbose = 0;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -050097static int strict = 0;
John Bowlerad5a9932012-08-10 13:15:07 -050098static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
99static int error_count = 0; /* count calls to png_error */
100static int warning_count = 0; /* count calls to png_warning */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500101
Guy Schalnat0d580581995-07-20 02:43:20 -0500102#ifdef __TURBOC__
103#include <mem.h>
104#endif
105
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -0500106/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
107#ifndef png_jmpbuf
108# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
109#endif
110
John Bowler3c1f6982012-08-16 20:47:34 -0500111/* Defines for unknown chunk handling if required. */
112#ifndef PNG_HANDLE_CHUNK_ALWAYS
113# define PNG_HANDLE_CHUNK_ALWAYS 3
114#endif
115#ifndef PNG_HANDLE_CHUNK_IF_SAFE
116# define PNG_HANDLE_CHUNK_IF_SAFE 2
117#endif
118
119/* Utility to save typing/errors, the argument must be a name */
120#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
121
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500122/* Example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500123static int status_pass = 1;
124static int status_dots_requested = 0;
125static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600126
John Bowler8fb6c6a2012-01-25 07:47:44 -0600127static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600128read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600129{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
131 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500132
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500133 if (status_pass != pass)
134 {
135 fprintf(stdout, "\n Pass %d: ", pass);
136 status_pass = pass;
137 status_dots = 31;
138 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 status_dots--;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500141
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 if (status_dots == 0)
143 {
144 fprintf(stdout, "\n ");
145 status_dots=30;
146 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500147
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 fprintf(stdout, "r");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600149}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600150
John Bowlere2098ba2012-08-10 17:04:56 -0500151#ifdef PNG_WRITE_SUPPORTED
John Bowler8fb6c6a2012-01-25 07:47:44 -0600152static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600153write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600154{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500155 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
156 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500157
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500158 fprintf(stdout, "w");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600159}
John Bowlere2098ba2012-08-10 17:04:56 -0500160#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600161
162
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500164/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500165 * but merely examine the row filters. We set this to 256 rather than
166 * 5 in case illegal filter values are present.)
167 */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500168static png_uint_32 filters_used[256];
John Bowler8fb6c6a2012-01-25 07:47:44 -0600169static void PNGCBAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500170count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
171{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500172 if (png_ptr != NULL && row_info != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 ++filters_used[*(data - 1)];
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500174}
175#endif
176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500178/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500179 * but merely count the zero samples)
180 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600181
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500182static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600183
John Bowler8fb6c6a2012-01-25 07:47:44 -0600184static void PNGCBAPI
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500185count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600186{
187 png_bytep dp = data;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500188 if (png_ptr == NULL)
189 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600190
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500191 /* Contents of row_info:
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600192 * png_uint_32 width width of row
193 * png_uint_32 rowbytes number of bytes in row
194 * png_byte color_type color type of pixels
195 * png_byte bit_depth bit depth of samples
196 * png_byte channels number of channels (1-4)
197 * png_byte pixel_depth bits per pixel (depth*channels)
198 */
199
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500200 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600201
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500202 if (row_info->color_type == 0 || row_info->color_type == 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600203 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500204 int pos = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500205 png_uint_32 n, nstop;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500206
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500207 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600208 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500209 if (row_info->bit_depth == 1)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500210 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500211 if (((*dp << pos++ ) & 0x80) == 0)
212 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500213
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500214 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600215 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500216 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600217 dp++;
218 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500219 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500220
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500221 if (row_info->bit_depth == 2)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500222 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500223 if (((*dp << (pos+=2)) & 0xc0) == 0)
224 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500225
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500226 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600227 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500228 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600229 dp++;
230 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500231 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500232
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500233 if (row_info->bit_depth == 4)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500234 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500235 if (((*dp << (pos+=4)) & 0xf0) == 0)
236 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500237
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500238 if (pos == 8)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600239 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500240 pos = 0;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600241 dp++;
242 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500243 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500244
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500245 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500246 if (*dp++ == 0)
247 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500248
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500249 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600250 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500251 if ((*dp | *(dp+1)) == 0)
252 zero_samples++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600253 dp+=2;
254 }
255 }
256 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500257 else /* Other color types */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600258 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500259 png_uint_32 n, nstop;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600260 int channel;
261 int color_channels = row_info->channels;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500262 if (row_info->color_type > 3)color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600263
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500264 for (n = 0, nstop=row_info->width; n<nstop; n++)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600265 {
266 for (channel = 0; channel < color_channels; channel++)
267 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500268 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500269 if (*dp++ == 0)
270 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500271
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500272 if (row_info->bit_depth == 16)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600273 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500274 if ((*dp | *(dp+1)) == 0)
275 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500276
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600277 dp+=2;
278 }
279 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500280 if (row_info->color_type > 3)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600281 {
282 dp++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500283 if (row_info->bit_depth == 16)
284 dp++;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600285 }
286 }
287 }
288}
Glenn Randers-Pehrson38d73af1998-03-07 21:30:44 -0600289#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600290
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500291#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600292/* START of code to validate stdio-free compilation */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500293/* These copies of the default read/write functions come from pngrio.c and
294 * pngwio.c. They allow "don't include stdio" testing of the library.
295 * This is the function that does the actual reading of data. If you are
296 * not reading from a standard C stream, you should create a replacement
297 * read_data function and use it at run time with png_set_read_fn(), rather
298 * than changing the library.
299 */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600300
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600301#ifdef PNG_IO_STATE_SUPPORTED
302void
303pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
304 png_uint_32 io_op);
305void
306pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
307 png_uint_32 io_op)
308{
309 png_uint_32 io_state = png_get_io_state(png_ptr);
310 int err = 0;
311
312 /* Check if the current operation (reading / writing) is as expected. */
313 if ((io_state & PNG_IO_MASK_OP) != io_op)
314 png_error(png_ptr, "Incorrect operation in I/O state");
315
316 /* Check if the buffer size specific to the current location
317 * (file signature / header / data / crc) is as expected.
318 */
319 switch (io_state & PNG_IO_MASK_LOC)
320 {
321 case PNG_IO_SIGNATURE:
322 if (data_length > 8)
323 err = 1;
324 break;
325 case PNG_IO_CHUNK_HDR:
326 if (data_length != 8)
327 err = 1;
328 break;
329 case PNG_IO_CHUNK_DATA:
330 break; /* no restrictions here */
331 case PNG_IO_CHUNK_CRC:
332 if (data_length != 4)
333 err = 1;
334 break;
335 default:
336 err = 1; /* uninitialized */
337 }
338 if (err)
339 png_error(png_ptr, "Bad I/O state or buffer size");
340}
341#endif
342
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600343static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500344pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600345{
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600346 png_size_t check = 0;
347 png_voidp io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600348
349 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
350 * instead of an int, which is what fread() actually returns.
351 */
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600352 io_ptr = png_get_io_ptr(png_ptr);
353 if (io_ptr != NULL)
354 {
355 check = fread(data, 1, length, (png_FILE_p)io_ptr);
356 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500357
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600358 if (check != length)
359 {
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600360 png_error(png_ptr, "Read Error");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600361 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600362
363#ifdef PNG_IO_STATE_SUPPORTED
364 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
365#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600366}
Guy Schalnat0d580581995-07-20 02:43:20 -0500367
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500368#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600369static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500370pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600371{
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500372 /* Do nothing; fflush() is said to be just a waste of energy. */
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600373 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600374}
375#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500376
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500377/* 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 -0500378 * not writing to a standard C stream, you should create a replacement
379 * write_data function and use it at run time with png_set_write_fn(), rather
380 * than changing the library.
381 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600382static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500383pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600384{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500385 png_size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600386
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600387 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500388
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600389 if (check != length)
390 {
391 png_error(png_ptr, "Write Error");
392 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600393
394#ifdef PNG_IO_STATE_SUPPORTED
395 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
396#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600397}
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600398
399/* This function is called when there is a warning, but the library thinks
400 * it can continue anyway. Replacement functions don't have to do anything
401 * here if you don't want to. In the default configuration, png_ptr is
402 * not used, but it is passed in case it may be useful.
403 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600404static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500405pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600406{
407 PNG_CONST char *name = "UNKNOWN (ERROR!)";
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600408 char *test;
409 test = png_get_error_ptr(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
John Bowlerad5a9932012-08-10 13:15:07 -0500411 ++warning_count;
412
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600413 if (test == NULL)
414 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600416 else
417 fprintf(STDERR, "%s: libpng warning: %s\n", test, message);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600418}
419
420/* This is the default error handling function. Note that replacements for
421 * this function MUST NOT RETURN, or the program will likely crash. This
422 * function is used by default, or if the program supplies NULL for the
423 * error function pointer in png_set_error_fn().
424 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600425static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500426pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600427{
John Bowlerad5a9932012-08-10 13:15:07 -0500428 ++error_count;
429
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500430 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500431 /* We can return because png_error calls the default handler, which is
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500432 * actually OK in this case.
433 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500435#endif /* !PNG_STDIO_SUPPORTED */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600436/* END of code to validate stdio-free compilation */
437
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600438/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500439#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600440
441/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500442 * 64K. However, zlib may allocate more then 64K if you don't tell
443 * it not to. See zconf.h and png.h for more information. zlib does
444 * need to allocate exactly 64K, so whatever you call here must
445 * have the ability to do that.
446 *
447 * This piece of code can be compiled to validate max 64K allocations
448 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
449 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600450typedef struct memory_information
451{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500452 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500453 png_voidp pointer;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600454 struct memory_information *next;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600455} memory_information;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600456typedef memory_information *memory_infop;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600457
458static memory_infop pinformation = NULL;
459static int current_allocation = 0;
460static int maximum_allocation = 0;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500461static int total_allocation = 0;
462static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600463
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600464png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
465 png_alloc_size_t size));
466void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600467
468png_voidp
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600469PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600470{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500471
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500472 /* png_malloc has already tested for NULL; png_create_struct calls
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500473 * png_debug_malloc directly, with png_ptr == NULL which is OK
474 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500475
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600476 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500477 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600478
479 /* This calls the library allocator twice, once to get the requested
480 buffer and once to get a new free list entry. */
481 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500482 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500483 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500484 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500485 pinfo = (memory_infop)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500486 (sizeof *pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600487 pinfo->size = size;
488 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500489 total_allocation += size;
490 num_allocations ++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500491
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600492 if (current_allocation > maximum_allocation)
493 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500494
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500495 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500496 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500497
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500498 png_set_mem_fn(png_ptr,
499 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500501 if (size != 0 && pinfo->pointer == NULL)
502 {
503 current_allocation -= size;
504 total_allocation -= size;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500505 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500506 "out of memory in pngtest->png_debug_malloc");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500507 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500508
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600509 pinfo->next = pinformation;
510 pinformation = pinfo;
511 /* Make sure the caller isn't assuming zeroed memory. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600512 memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500513
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500514 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600515 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500516 pinfo->pointer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500517
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600518 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600519 }
520}
521
522/* Free a pointer. It is removed from the list at the same time. */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600523void PNGCBAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500524png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600525{
526 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500527 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500528
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600529 if (ptr == 0)
530 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600531#if 0 /* This happens all the time. */
532 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
533#endif
534 return;
535 }
536
537 /* Unlink the element from the list. */
538 {
John Bowlerbaeb6d12011-11-26 18:21:02 -0600539 memory_infop *ppinfo = &pinformation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500540
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600541 for (;;)
542 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600543 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500544
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600545 if (pinfo->pointer == ptr)
546 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600547 *ppinfo = pinfo->next;
548 current_allocation -= pinfo->size;
549 if (current_allocation < 0)
550 fprintf(STDERR, "Duplicate free of memory\n");
551 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500552 the memory that is to be freed. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600553 memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500554 png_free_default(png_ptr, pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500555 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600556 break;
557 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500558
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600559 if (pinfo->next == NULL)
560 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500561 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600562 break;
563 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500564
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600565 ppinfo = &pinfo->next;
566 }
567 }
568
569 /* Finally free the data. */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500570 if (verbose)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600571 printf("Freeing %p\n", ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500572
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500573 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500574 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600575}
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500576#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600577/* END of code to test memory allocation/deallocation */
578
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500579
John Bowler3c1f6982012-08-16 20:47:34 -0500580#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500581/* Demonstration of user chunk support of the sTER and vpAg chunks */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500582
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500583/* (sTER is a public chunk not yet known by libpng. vpAg is a private
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500584chunk used in ImageMagick to store "virtual page" size). */
585
John Bowler3c1f6982012-08-16 20:47:34 -0500586static struct user_chunk_data
587{
588 png_const_infop info_ptr;
589 png_uint_32 vpAg_width, vpAg_height;
590 png_byte vpAg_units;
591 png_byte sTER_mode;
592 int location[2];
593}
594user_chunk_data;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500595
John Bowler3c1f6982012-08-16 20:47:34 -0500596/* Used for location and order; zero means nothing. */
597#define have_sTER 0x01
598#define have_vpAg 0x02
599#define before_PLTE 0x10
600#define before_IDAT 0x20
601#define after_IDAT 0x40
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500602
John Bowler3c1f6982012-08-16 20:47:34 -0500603static void
604init_callback_info(png_const_infop info_ptr)
605{
606 MEMZERO(user_chunk_data);
607 user_chunk_data.info_ptr = info_ptr;
608}
609
610static int
611set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
612{
613 int location;
614
615 if ((data->location[0] & what) || (data->location[1] & what))
616 return 0; /* already have one of these */
617
618 /* Find where we are (the code below zeros info_ptr to indicate that the
619 * chunks before the first IDAT have been read.)
620 */
621 if (data->info_ptr == NULL) /* after IDAT */
622 location = what | after_IDAT;
623
624 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE))
625 location = what | before_IDAT;
626
627 else
628 location = what | before_PLTE;
629
630 if (data->location[0] == 0)
631 data->location[0] = location;
632
633 else
634 data->location[1] = location;
635
636 return 1; /* handled */
637}
638
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -0600639static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr,
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500640 png_unknown_chunkp chunk)
641{
John Bowler3c1f6982012-08-16 20:47:34 -0500642 struct user_chunk_data *my_user_chunk_data =
643 (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
644
645 if (my_user_chunk_data == NULL)
646 png_error(png_ptr, "lost user chunk pointer");
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500647
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500648 /* Return one of the following:
649 * return (-n); chunk had an error
650 * return (0); did not recognize
651 * return (n); success
652 *
653 * The unknown chunk structure contains the chunk data:
654 * png_byte name[5];
655 * png_byte *data;
656 * png_size_t size;
657 *
658 * Note that libpng has already taken care of the CRC handling.
659 */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500660
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500661 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
662 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
663 {
664 /* Found sTER chunk */
665 if (chunk->size != 1)
666 return (-1); /* Error return */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500667
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500668 if (chunk->data[0] != 0 && chunk->data[0] != 1)
669 return (-1); /* Invalid mode */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500670
John Bowler3c1f6982012-08-16 20:47:34 -0500671 if (set_location(png_ptr, my_user_chunk_data, have_sTER))
672 {
673 my_user_chunk_data->sTER_mode=chunk->data[0];
674 return (1);
675 }
676
677 else
678 return (0); /* duplicate sTER - give it to libpng */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500679 }
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500680
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500681 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
682 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
683 return (0); /* Did not recognize */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500684
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500685 /* Found ImageMagick vpAg chunk */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500686
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500687 if (chunk->size != 9)
688 return (-1); /* Error return */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500689
John Bowler3c1f6982012-08-16 20:47:34 -0500690 if (!set_location(png_ptr, my_user_chunk_data, have_vpAg))
691 return (0); /* duplicate vpAg */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500692
John Bowler3c1f6982012-08-16 20:47:34 -0500693 my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
694 my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
695 my_user_chunk_data->vpAg_units = chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500696
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500697 return (1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500698}
John Bowler3c1f6982012-08-16 20:47:34 -0500699
700#ifdef PNG_WRITE_SUPPORTED
701static void
702write_sTER_chunk(png_structp write_ptr)
703{
704 png_byte png_sTER[5] = {115, 84, 69, 82, '\0'};
705
706 if (verbose)
707 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
708
709 png_write_chunk(write_ptr, png_sTER, &user_chunk_data.sTER_mode, 1);
710}
711
712static void
713write_vpAg_chunk(png_structp write_ptr)
714{
715 png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'};
716
717 png_byte vpag_chunk_data[9];
718
719 if (verbose)
720 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
721 (unsigned long)user_chunk_data.vpAg_width,
722 (unsigned long)user_chunk_data.vpAg_height,
723 user_chunk_data.vpAg_units);
724
725 png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
726 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
727 vpag_chunk_data[8] = user_chunk_data.vpAg_units;
728 png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
729}
730
731static void
732write_chunks(png_structp write_ptr, int location)
733{
734 int i;
735
736 /* Notice that this preserves the original chunk order, however chunks
737 * intercepted by the callback will be written *after* chunks passed to
738 * libpng. This will actually reverse a pair of sTER chunks or a pair of
739 * vpAg chunks, resulting in an error later. This is not worth worrying
740 * about - the chunks should not be duplicated!
741 */
742 for (i=0; i<2; ++i)
743 {
744 if (user_chunk_data.location[i] == (location | have_sTER))
745 write_sTER_chunk(write_ptr);
746
747 else if (user_chunk_data.location[i] == (location | have_vpAg))
748 write_vpAg_chunk(write_ptr);
749 }
750}
751#endif /* PNG_WRITE_SUPPORTED */
752#else /* !PNG_READ_USER_CHUNKS_SUPPORTED */
753# define write_chunks(pp,loc) ((void)0)
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500754#endif
755/* END of code to demonstrate user chunk support */
756
John Bowlerad5a9932012-08-10 13:15:07 -0500757/* START of code to check that libpng has the required text support; this only
758 * checks for the write support because if read support is missing the chunk
759 * will simply not be reported back to pngtest.
760 */
761#ifdef PNG_TEXT_SUPPORTED
762static void
763pngtest_check_text_support(png_const_structp png_ptr, png_textp text_ptr,
764 int num_text)
765{
766 while (num_text > 0)
767 {
768 switch (text_ptr[--num_text].compression)
769 {
770 case PNG_TEXT_COMPRESSION_NONE:
771 break;
772
773 case PNG_TEXT_COMPRESSION_zTXt:
774# ifndef PNG_WRITE_zTXt_SUPPORTED
775 ++unsupported_chunks;
776# endif
777 break;
778
779 case PNG_ITXT_COMPRESSION_NONE:
780 case PNG_ITXT_COMPRESSION_zTXt:
781# ifndef PNG_WRITE_iTXt_SUPPORTED
782 ++unsupported_chunks;
783# endif
784 break;
785
786 default:
787 /* This is an error */
788 png_error(png_ptr, "invalid text chunk compression field");
789 break;
790 }
791 }
792}
793#endif
794/* END of code to check that libpng has the required text support */
795
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600796/* Test one file */
John Bowler8fb6c6a2012-01-25 07:47:44 -0600797static int
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600798test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500800 static png_FILE_p fpin;
801 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500802 png_structp read_ptr;
803 png_infop read_info_ptr, end_info_ptr;
804#ifdef PNG_WRITE_SUPPORTED
805 png_structp write_ptr;
806 png_infop write_info_ptr;
807 png_infop write_end_info_ptr;
808#else
809 png_structp write_ptr = NULL;
810 png_infop write_info_ptr = NULL;
811 png_infop write_end_info_ptr = NULL;
812#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600813 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500814 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500815 png_uint_32 width, height;
816 int num_pass, pass;
817 int bit_depth, color_type;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600818
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500819 row_buf = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500820
Andreas Dilger47a0c421997-05-16 02:46:07 -0500821 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600822 {
823 fprintf(STDERR, "Could not find input file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600824 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600825 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500826
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600828 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500829 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600831 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600832 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500833
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600834 pngtest_debug("Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500835#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500836 read_ptr =
837 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600838 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500839#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500840 read_ptr =
841 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500842#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500843#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500844 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
845 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600846#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500847
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500848#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500849#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500850 write_ptr =
851 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
852 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500853#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500854 write_ptr =
855 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500856#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500857#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500858 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
859 pngtest_warning);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600860#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500861#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600862 pngtest_debug("Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500864 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500865#ifdef PNG_WRITE_SUPPORTED
866 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500867 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500868#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500869
John Bowler3c1f6982012-08-16 20:47:34 -0500870#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
871 init_callback_info(read_info_ptr);
872 png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
873 read_user_chunk_callback);
874#endif
875
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600876#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600877 pngtest_debug("Setting jmpbuf for read struct");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600878 if (setjmp(png_jmpbuf(read_ptr)))
Guy Schalnat0f716451995-11-28 11:22:13 -0600879 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600880 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500881 png_free(read_ptr, row_buf);
882 row_buf = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500883 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500884#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500885 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500887#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500888 FCLOSE(fpin);
889 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600890 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600891 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500893#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600894 pngtest_debug("Setting jmpbuf for write struct");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500895
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600896 if (setjmp(png_jmpbuf(write_ptr)))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600898 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500900 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500901#ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500902 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500903#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500904 FCLOSE(fpin);
905 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600906 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600907 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600908#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500909#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600910
Glenn Randers-Pehrson35f2e172012-03-10 19:42:03 -0600911 if (strict)
912 {
913 /* Treat png_benign_error() as errors on read */
914 png_set_benign_errors(read_ptr, 0);
John Bowlere2098ba2012-08-10 17:04:56 -0500915
916#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson35f2e172012-03-10 19:42:03 -0600917 /* Treat them as errors on write */
918 png_set_benign_errors(write_ptr, 0);
John Bowlere2098ba2012-08-10 17:04:56 -0500919#endif
Glenn Randers-Pehrson35f2e172012-03-10 19:42:03 -0600920
921 /* if strict is not set, then both are treated as warnings. */
922 }
923
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600924 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500925#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500926 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500927# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500929# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600930#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500931 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500932# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500933 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500934# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500935 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500936# else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600937 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500938# endif
939# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600940#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500941
John Bowlerb2bee332011-06-10 23:24:58 -0500942#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500943 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
944 * This is here just to make pngtest replicate the results from libpng
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500945 * versions prior to 1.5.4, and to test this new API.
Glenn Randers-Pehrson65a24d02011-04-01 20:41:53 -0500946 */
Glenn Randers-Pehrson6bdea982011-03-31 21:14:55 -0500947 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
948#endif
949
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500950 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600951 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500952#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600953 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500954#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600955 png_set_read_status_fn(read_ptr, read_row_callback);
956 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500957
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600958 else
959 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500960#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500961 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500962#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500963 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600964 }
965
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500966#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500967 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500968 int i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500969
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500970 for (i = 0; i<256; i++)
971 filters_used[i] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500972
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500973 png_set_read_user_transform_fn(read_ptr, count_filters);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500974 }
975#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500976#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500977 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500978 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
979#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500980
John Bowler4a6c6df2012-08-16 16:12:13 -0500981#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
John Bowler3c1f6982012-08-16 20:47:34 -0500982 /* Preserve all the unknown chunks, if possible. If this is disabled then,
983 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
984 * libpng to *save* the unknown chunks on read (because we can't switch the
985 * save option on!)
986 *
987 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
988 * unknown chunks and write will write them all.
989 */
990#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500991 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
992 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600993#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500994#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
John Bowler3c1f6982012-08-16 20:47:34 -0500995 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500996 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997#endif
John Bowler4a6c6df2012-08-16 16:12:13 -0500998#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600999
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001000 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001002
John Bowler3c1f6982012-08-16 20:47:34 -05001003#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1004 /* This is a bit of a hack; there is no obvious way in the callback function
1005 * to determine that the chunks before the first IDAT have been read, so
1006 * remove the info_ptr (which is only used to determine position relative to
1007 * PLTE) here to indicate that we are after the IDAT.
1008 */
1009 user_chunk_data.info_ptr = NULL;
1010#endif
1011
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001012 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001013 {
1014 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001015
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1017 &color_type, &interlace_type, &compression_type, &filter_type))
1018 {
1019 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001020#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001021 color_type, interlace_type, compression_type, filter_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001022#else
1023 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
1024#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001025 }
1026 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001027#ifdef PNG_FIXED_POINT_SUPPORTED
1028#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001029 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001030 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 -06001031 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001032
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001033 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1034 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001035 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001036 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001037 red_y, green_x, green_y, blue_x, blue_y);
1038 }
1039 }
1040#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001041#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001042 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001043 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001045 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001046 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001047 }
1048#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001049#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001050#ifdef PNG_FLOATING_POINT_SUPPORTED
1051#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001052 {
1053 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1054 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001055
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001056 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1057 &red_y, &green_x, &green_y, &blue_x, &blue_y))
1058 {
1059 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1060 red_y, green_x, green_y, blue_x, blue_y);
1061 }
1062 }
1063#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001064#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001065 {
1066 double gamma;
1067
1068 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001069 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001070 }
1071#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001072#endif /* Floating point */
1073#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001074#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001075 {
1076 png_charp name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001077 png_bytep profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001078 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001079 int compression_type;
1080
1081 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1082 &profile, &proflen))
1083 {
1084 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1085 profile, proflen);
1086 }
1087 }
1088#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001089#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001090 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001091 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001092
1093 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001094 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001095 }
1096#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001097 {
1098 png_colorp palette;
1099 int num_palette;
1100
1101 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001102 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001103 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001104#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001105 {
1106 png_color_16p background;
1107
1108 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
1109 {
1110 png_set_bKGD(write_ptr, write_info_ptr, background);
1111 }
1112 }
1113#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001114#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 {
1116 png_uint_16p hist;
1117
1118 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001120 }
1121#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001122#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001124 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 int unit_type;
1126
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001127 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128 &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 {
1130 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1131 }
1132 }
1133#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001134#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 {
1136 png_charp purpose, units;
1137 png_charpp params;
1138 png_int_32 X0, X1;
1139 int type, nparams;
1140
1141 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1142 &nparams, &units, &params))
1143 {
1144 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1145 nparams, units, params);
1146 }
1147 }
1148#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001149#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 {
1151 png_uint_32 res_x, res_y;
1152 int unit_type;
1153
1154 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001156 }
1157#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001158#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 {
1160 png_color_8p sig_bit;
1161
1162 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001163 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 }
1165#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -05001166#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001167#ifdef PNG_FLOATING_POINT_SUPPORTED
1168 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001169 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001170 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001171
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001172 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1173 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001174 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001175 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001176 }
1177 }
1178#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001179#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001180 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001181 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001182 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001183
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001184 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1185 &scal_height))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001186 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001187 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1188 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001189 }
1190 }
1191#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001192#endif
1193#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001194#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 {
1196 png_textp text_ptr;
1197 int num_text;
1198
1199 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1200 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001201 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001202
John Bowlerad5a9932012-08-10 13:15:07 -05001203 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1204
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001205 if (verbose)
Glenn Randers-Pehrsonf2715a52012-03-15 19:52:03 -05001206 {
1207 int i;
1208
1209 printf("\n");
1210 for (i=0; i<num_text; i++)
1211 {
1212 printf(" Text compression[%d]=%d\n",
1213 i, text_ptr[i].compression);
1214 }
1215 }
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001216
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1218 }
1219 }
1220#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001221#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 {
1223 png_timep mod_time;
1224
1225 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
1226 {
1227 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001228#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001229 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001230 tIME_string[(sizeof tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001231
John Bowler40b26032011-12-22 08:09:15 -06001232 else
John Bowler5f5977e2012-08-09 16:42:42 -05001233 {
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001234 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1235 tIME_string[(sizeof tIME_string) - 1] = '\0';
John Bowler5f5977e2012-08-09 16:42:42 -05001236 }
John Bowler40b26032011-12-22 08:09:15 -06001237
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001238 tIME_chunk_present++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001239#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001240 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 }
1242#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001243#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001245 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001247 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001249 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001250 &trans_color))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001252 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001253 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001254 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001255 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001256 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001257 ((int)trans_color->red > sample_max ||
1258 (int)trans_color->green > sample_max ||
1259 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001260 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001261 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262 }
1263 }
1264#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001265#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001266 {
1267 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001268 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001269 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001270
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001271 if (num_unknowns)
1272 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001273 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1274 num_unknowns);
John Bowler3c1f6982012-08-16 20:47:34 -05001275#if PNG_LIBPNG_VER < 10600
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001276 /* Copy the locations from the read_info_ptr. The automatically
John Bowler3c1f6982012-08-16 20:47:34 -05001277 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1278 * because they are reset from the write pointer (removed in 1.6.0).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001279 */
John Bowler3c1f6982012-08-16 20:47:34 -05001280 {
1281 int i;
1282 for (i = 0; i < num_unknowns; i++)
1283 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1284 unknowns[i].location);
1285 }
1286#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001287 }
1288 }
1289#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001291#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001292 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001293
John Bowler3c1f6982012-08-16 20:47:34 -05001294 /* Write the info in two steps so that if we write the 'unknown' chunks here
1295 * they go to the correct place.
1296 */
1297 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1298
1299 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1300
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001302
John Bowler3c1f6982012-08-16 20:47:34 -05001303 write_chunks(write_ptr, before_IDAT); /* after PLTE */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001304#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001305
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001306#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001307 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001308 row_buf = (png_bytep)png_malloc(read_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001310
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001311 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001312#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001313 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001314
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001315#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1316 defined(PNG_WRITE_INTERLACING_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001317 num_pass = png_set_interlace_handling(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001318# ifdef PNG_WRITE_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 png_set_interlace_handling(write_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001320# endif
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001321#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001322 num_pass = 1;
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001323#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001324
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001325#ifdef PNGTEST_TIMING
1326 t_stop = (float)clock();
1327 t_misc += (t_stop - t_start);
1328 t_start = t_stop;
1329#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001330 for (pass = 0; pass < num_pass; pass++)
1331 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001332 pngtest_debug1("Writing row data for pass %d", pass);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001334 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001335#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001336 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001337 row_buf = (png_bytep)png_malloc(read_ptr,
1338 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001339
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001340 pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001341 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001342
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001343#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001344 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001345
1346#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001347#ifdef PNGTEST_TIMING
1348 t_stop = (float)clock();
1349 t_decode += (t_stop - t_start);
1350 t_start = t_stop;
1351#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001352 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001353#ifdef PNGTEST_TIMING
1354 t_stop = (float)clock();
1355 t_encode += (t_stop - t_start);
1356 t_start = t_stop;
1357#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001358#endif /* PNG_WRITE_SUPPORTED */
1359
1360#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001361 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001362 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001363 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001364#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001365 }
1366 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001367
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001368#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001369 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001371#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001372 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001373#endif
1374
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001375 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001376
Andreas Dilger47a0c421997-05-16 02:46:07 -05001377 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001378#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001379 {
1380 png_textp text_ptr;
1381 int num_text;
1382
1383 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1384 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001385 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrsonf2715a52012-03-15 19:52:03 -05001386
John Bowlerad5a9932012-08-10 13:15:07 -05001387 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1388
Glenn Randers-Pehrsonf2715a52012-03-15 19:52:03 -05001389 if (verbose)
1390 {
1391 int i;
1392
1393 printf("\n");
1394 for (i=0; i<num_text; i++)
1395 {
1396 printf(" Text compression[%d]=%d\n",
1397 i, text_ptr[i].compression);
1398 }
1399 }
1400
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001401 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1402 }
1403 }
1404#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001405#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001406 {
1407 png_timep mod_time;
1408
1409 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1410 {
1411 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001412#ifdef PNG_TIME_RFC1123_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -06001413 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001414 tIME_string[(sizeof tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001415
John Bowler40b26032011-12-22 08:09:15 -06001416 else
John Bowler5f5977e2012-08-09 16:42:42 -05001417 {
1418 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1419 tIME_string[(sizeof tIME_string)-1] = '\0';
1420 }
John Bowler40b26032011-12-22 08:09:15 -06001421
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001422 tIME_chunk_present++;
1423#endif /* PNG_TIME_RFC1123_SUPPORTED */
1424 }
1425 }
1426#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001427#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001428 {
1429 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001430 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001431 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001432
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433 if (num_unknowns)
1434 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001435 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1436 num_unknowns);
John Bowler3c1f6982012-08-16 20:47:34 -05001437#if PNG_LIBPNG_VER < 10600
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001438 /* Copy the locations from the read_info_ptr. The automatically
John Bowler3c1f6982012-08-16 20:47:34 -05001439 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1440 * because they are reset from the write pointer (removed in 1.6.0).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001441 */
John Bowler3c1f6982012-08-16 20:47:34 -05001442 {
1443 int i;
1444 for (i = 0; i < num_unknowns; i++)
1445 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1446 unknowns[i].location);
1447 }
1448#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001449 }
1450 }
1451#endif
John Bowler3c1f6982012-08-16 20:47:34 -05001452
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001453#ifdef PNG_WRITE_SUPPORTED
John Bowler3c1f6982012-08-16 20:47:34 -05001454 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1455 * do it is to write them *before* calling png_write_end. When unknown
1456 * chunks are written by libpng, however, they are written just before IEND. * There seems to be no way round this, however vpAg/sTER are not expected
1457 * after IDAT.
1458 */
1459 write_chunks(write_ptr, after_IDAT);
1460
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001461 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001462#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001463
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001464#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001465 if (verbose)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001466 {
1467 png_uint_32 iwidth, iheight;
1468 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1469 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001470 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001471 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001472 }
1473#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001474
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001475 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001476#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001477 pngtest_debug("destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001478 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001479 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001480#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001481 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001482 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001483#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001484 pngtest_debug("destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001485 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001486 pngtest_debug("destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001487 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001488#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001489 pngtest_debug("Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001490
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001491 FCLOSE(fpin);
1492 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001493
John Bowlerad5a9932012-08-10 13:15:07 -05001494 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1495 * Unsupported chunks can result in warnings, in that case ignore the strict
1496 * setting, otherwise fail the test on warnings as well as errors.
1497 */
1498 if (error_count > 0)
1499 {
1500 /* We don't really expect to get here because of the setjmp handling
1501 * above, but this is safe.
1502 */
John Bowlere2098ba2012-08-10 17:04:56 -05001503 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
John Bowlerad5a9932012-08-10 13:15:07 -05001504 inname, error_count, warning_count);
1505
1506 if (strict != 0)
1507 return (1);
1508 }
1509
John Bowlere2098ba2012-08-10 17:04:56 -05001510# ifdef PNG_WRITE_SUPPORTED
1511 /* If there we no write support nothing was written! */
1512 else if (unsupported_chunks > 0)
1513 {
1514 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1515 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1516 }
1517# endif
John Bowlerad5a9932012-08-10 13:15:07 -05001518
1519 else if (warning_count > 0)
1520 {
John Bowlere2098ba2012-08-10 17:04:56 -05001521 fprintf(STDERR, "\n %s: %d libpng warnings found",
John Bowlerad5a9932012-08-10 13:15:07 -05001522 inname, warning_count);
1523
1524 if (strict != 0)
1525 return (1);
1526 }
1527
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001528 pngtest_debug("Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001530 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001531 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001532 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001533 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001534
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001536 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001537 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001538 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001539 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001540 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001541
John Bowlere2098ba2012-08-10 17:04:56 -05001542#ifdef PNG_WRITE_SUPPORTED /* else nothing was written */
Guy Schalnat0f716451995-11-28 11:22:13 -06001543 {
John Bowlere2098ba2012-08-10 17:04:56 -05001544 int wrote_question = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05001545
John Bowlere2098ba2012-08-10 17:04:56 -05001546 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001547 {
John Bowlere2098ba2012-08-10 17:04:56 -05001548 png_size_t num_in, num_out;
1549 char inbuf[256], outbuf[256];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001550
John Bowlere2098ba2012-08-10 17:04:56 -05001551
1552 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1553 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1554
1555 if (num_in != num_out)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001556 {
John Bowlere2098ba2012-08-10 17:04:56 -05001557 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1558 inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001559
John Bowlere2098ba2012-08-10 17:04:56 -05001560 if (wrote_question == 0 && unsupported_chunks == 0)
1561 {
1562 fprintf(STDERR,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001563 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001564 inname, PNG_ZBUF_SIZE);
John Bowlere2098ba2012-08-10 17:04:56 -05001565 fprintf(STDERR,
1566 "\n filtering heuristic (libpng default), compression");
1567 fprintf(STDERR,
1568 " level (zlib default),\n and zlib version (%s)?\n\n",
1569 ZLIB_VERSION);
1570 wrote_question = 1;
1571 }
1572
1573 FCLOSE(fpin);
1574 FCLOSE(fpout);
1575
1576 if (strict != 0 && unsupported_chunks == 0)
1577 return (1);
1578
1579 else
1580 return (0);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001581 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001582
John Bowlere2098ba2012-08-10 17:04:56 -05001583 if (!num_in)
1584 break;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001585
John Bowlere2098ba2012-08-10 17:04:56 -05001586 if (memcmp(inbuf, outbuf, num_in))
1587 {
1588 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1589 outname);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001590
John Bowlere2098ba2012-08-10 17:04:56 -05001591 if (wrote_question == 0 && unsupported_chunks == 0)
1592 {
1593 fprintf(STDERR,
1594 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1595 inname, PNG_ZBUF_SIZE);
1596 fprintf(STDERR,
1597 "\n filtering heuristic (libpng default), compression");
1598 fprintf(STDERR,
1599 " level (zlib default),\n and zlib version (%s)?\n\n",
1600 ZLIB_VERSION);
1601 wrote_question = 1;
1602 }
1603
1604 FCLOSE(fpin);
1605 FCLOSE(fpout);
1606
1607 /* NOTE: the unsupported_chunks escape is permitted here because
1608 * unsupported text chunk compression will result in the compression
1609 * mode being changed (to NONE) yet, in the test case, the result
1610 * can be exactly the same size!
1611 */
1612 if (strict != 0 && unsupported_chunks == 0)
1613 return (1);
1614
1615 else
1616 return (0);
1617 }
Guy Schalnat0f716451995-11-28 11:22:13 -06001618 }
1619 }
John Bowlere2098ba2012-08-10 17:04:56 -05001620#endif /* PNG_WRITE_SUPPORTED */
Guy Schalnat0f716451995-11-28 11:22:13 -06001621
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001622 FCLOSE(fpin);
1623 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001624
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001625 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001626}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001627
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001628/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001629#ifdef RISCOS
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001630static PNG_CONST char *inname = "pngtest/png";
1631static PNG_CONST char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001632#else
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001633static PNG_CONST char *inname = "pngtest.png";
1634static PNG_CONST char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001635#endif
1636
1637int
1638main(int argc, char *argv[])
1639{
1640 int multiple = 0;
1641 int ierror = 0;
1642
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001643 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001644 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001645 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001646 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001647 fprintf(STDERR, " library (%lu):%s",
1648 (unsigned long)png_access_version_number(),
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001649 png_get_header_version(NULL));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001650
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001651 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001652 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001653 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001654
1655 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001656 * not sure this matters, but it is nice to know, the first of these
1657 * tests should be impossible because of the way the macros are set
1658 * in pngconf.h
1659 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001660#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001661 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001662#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001663 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001664#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001665 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001666#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001667
1668 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1669 {
1670 fprintf(STDERR,
1671 "Warning: versions are different between png.h and png.c\n");
1672 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1673 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1674 ++ierror;
1675 }
1676
1677 if (argc > 1)
1678 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001679 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001680 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001681 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001682 status_dots_requested = 0;
1683 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001684
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001685 else if (strcmp(argv[1], "-mv") == 0 ||
1686 strcmp(argv[1], "-vm") == 0 )
1687 {
1688 multiple = 1;
1689 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001690 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001691 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001692
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001693 else if (strcmp(argv[1], "-v") == 0)
1694 {
1695 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001696 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001697 inname = argv[2];
1698 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001699
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001700 else if (strcmp(argv[1], "--strict") == 0)
1701 {
1702 status_dots_requested = 0;
1703 verbose = 1;
1704 inname = argv[2];
1705 strict++;
1706 }
1707
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001708 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001709 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001710 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001711 status_dots_requested = 0;
1712 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001713 }
1714
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001715 if (!multiple && argc == 3 + verbose)
1716 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001717
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001718 if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001719 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001720 fprintf(STDERR,
1721 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001722 argv[0], argv[0]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001723 fprintf(STDERR,
1724 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1725 fprintf(STDERR,
1726 " with -m %s is used as a temporary file\n", outname);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001727 exit(1);
1728 }
1729
1730 if (multiple)
1731 {
1732 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001733#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001734 int allocation_now = current_allocation;
1735#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001736 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001737 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001738 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001739 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001740 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001741 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001742 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001743#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1744 int k;
1745#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001746#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001747 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001748 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001749#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001750 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001751#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001752#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001753 for (k = 0; k<256; k++)
1754 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001755 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001756 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001757#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001758#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001759 if (tIME_chunk_present != 0)
1760 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001761
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001762 tIME_chunk_present = 0;
1763#endif /* PNG_TIME_RFC1123_SUPPORTED */
1764 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001765
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001766 else
1767 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001768 fprintf(STDERR, " FAIL\n");
1769 ierror += kerror;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001770 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001771#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001772 if (allocation_now != current_allocation)
1773 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001774 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001775
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001776 if (current_allocation != 0)
1777 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001778 memory_infop pinfo = pinformation;
1779
1780 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1781 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001782
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001783 while (pinfo != NULL)
1784 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001785 fprintf(STDERR, " %lu bytes at %x\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001786 (unsigned long)pinfo->size,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001787 (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001788 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001789 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001790 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001791#endif
1792 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001793#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001794 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001795 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001796 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001797 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001798 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1799 total_allocation);
1800 fprintf(STDERR, " Number of allocations: %10d\n",
1801 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001802#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001803 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001804
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001805 else
1806 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001807 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001808 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001809 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001810 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001811#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001812 int allocation_now = current_allocation;
1813#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001814 if (i == 1)
1815 status_dots_requested = 1;
1816
1817 else if (verbose == 0)
1818 status_dots_requested = 0;
1819
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001820 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001821 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001822
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001823 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001824
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001825 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001826 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001827 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001828 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001829#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001830 int k;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001831#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001832#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001833 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001834 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001835#else
1836 fprintf(STDERR, " PASS\n");
1837#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001838#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001839 for (k = 0; k<256; k++)
1840 if (filters_used[k])
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001841 fprintf(STDERR, " Filter %d was used %lu times\n",
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001842 k, (unsigned long)filters_used[k]);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001843#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001844#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001845 if (tIME_chunk_present != 0)
1846 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001847#endif /* PNG_TIME_RFC1123_SUPPORTED */
1848 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001849 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001850
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001851 else
1852 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001853 if (verbose == 0 && i != 2)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001854 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001855
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001856 fprintf(STDERR, " FAIL\n");
1857 ierror += kerror;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001858 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001859#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001860 if (allocation_now != current_allocation)
1861 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001862 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001863
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001864 if (current_allocation != 0)
1865 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001866 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001867
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001868 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1869 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001870
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001871 while (pinfo != NULL)
1872 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001873 fprintf(STDERR, " %lu bytes at %x\n",
1874 (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001875 pinfo = pinfo->next;
1876 }
1877 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001878#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001879 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001880#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001881 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001882 current_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001883 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001884 maximum_allocation);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001885 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1886 total_allocation);
1887 fprintf(STDERR, " Number of allocations: %10d\n",
1888 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001889#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001890 }
1891
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001892#ifdef PNGTEST_TIMING
1893 t_stop = (float)clock();
1894 t_misc += (t_stop - t_start);
1895 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001896 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001897 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001898 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001899 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001900 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001901 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001902 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001903 t_misc/(float)CLOCKS_PER_SEC);
1904#endif
1905
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001906 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001907 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001908
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001909 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001910 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001911
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001912 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001913}
John Bowlere2098ba2012-08-10 17:04:56 -05001914#else
1915int
1916main(void)
1917{
1918 fprintf(STDERR,
1919 " test ignored because libpng was not built with read support\n");
1920 return 0;
1921}
1922#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001923
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001924/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrsonb593b3f2012-08-11 18:15:41 -05001925typedef png_libpng_version_1_6_0beta28 Your_png_h_is_not_version_1_6_0beta28;