blob: 5d50e66059ba37c383f16970dc4500587025b772 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngtest.c - a simple test program to test libpng
3 *
Matt Sarett96be9082016-05-03 13:29:54 -04004 * Last changed in libpng 1.5.25 [December 3, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * 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 *
19 * The program will report "FAIL" in certain legitimate cases:
20 * 1) when the compression level or filter selection method is changed.
21 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
22 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
23 * exist in the input file.
24 * 4) others not listed here...
25 * In these cases, it is best to check with another tool such as "pngcheck"
26 * to see what the differences between the two files are.
27 *
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
30 * 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 ..."
32 */
33
Chris Craikb50c2172013-07-29 15:28:30 -070034#define _POSIX_SOURCE 1
35
36#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
The Android Open Source Project893912b2009-03-03 19:30:05 -080044#include "png.h"
45
Chris Craikb50c2172013-07-29 15:28:30 -070046/* Known chunks that exist in pngtest.png must be supported or pngtest will fail
47 * simply as a result of re-ordering them. This may be fixed in 1.7
Sireesh Tripurarib478e662014-05-09 15:15:10 +053048 *
49 * pngtest allocates a single row buffer for each row and overwrites it,
50 * therefore if the write side doesn't support the writing of interlaced images
51 * nothing can be done for an interlaced image (and the code below will fail
52 * horribly trying to write extra data after writing garbage).
Chris Craikb50c2172013-07-29 15:28:30 -070053 */
54#if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
55 defined PNG_READ_bKGD_SUPPORTED &&\
56 defined PNG_READ_cHRM_SUPPORTED &&\
57 defined PNG_READ_gAMA_SUPPORTED &&\
58 defined PNG_READ_oFFs_SUPPORTED &&\
59 defined PNG_READ_pCAL_SUPPORTED &&\
60 defined PNG_READ_pHYs_SUPPORTED &&\
61 defined PNG_READ_sBIT_SUPPORTED &&\
62 defined PNG_READ_sCAL_SUPPORTED &&\
63 defined PNG_READ_sRGB_SUPPORTED &&\
Matt Sarett96be9082016-05-03 13:29:54 -040064 defined PNG_READ_sPLT_SUPPORTED &&\
Chris Craikb50c2172013-07-29 15:28:30 -070065 defined PNG_READ_tEXt_SUPPORTED &&\
66 defined PNG_READ_tIME_SUPPORTED &&\
Sireesh Tripurarib478e662014-05-09 15:15:10 +053067 defined PNG_READ_zTXt_SUPPORTED &&\
Matt Sarett96be9082016-05-03 13:29:54 -040068 (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
Chris Craikb50c2172013-07-29 15:28:30 -070069
Sireesh Tripurarib478e662014-05-09 15:15:10 +053070#ifdef PNG_ZLIB_HEADER
71# include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
72#else
73# include "zlib.h"
74#endif
75
Chris Craikb50c2172013-07-29 15:28:30 -070076/* Copied from pngpriv.h but only used in error messages below. */
77#ifndef PNG_ZBUF_SIZE
78# define PNG_ZBUF_SIZE 8192
The Android Open Source Project893912b2009-03-03 19:30:05 -080079#endif
Chris Craikb50c2172013-07-29 15:28:30 -070080#define FCLOSE(file) fclose(file)
The Android Open Source Project893912b2009-03-03 19:30:05 -080081
Patrick Scott5f6bd842010-06-28 16:55:16 -040082#ifndef PNG_STDIO_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -070083typedef FILE * png_FILE_p;
The Android Open Source Project893912b2009-03-03 19:30:05 -080084#endif
85
Chris Craikb50c2172013-07-29 15:28:30 -070086/* Makes pngtest verbose so we can find problems. */
The Android Open Source Project893912b2009-03-03 19:30:05 -080087#ifndef PNG_DEBUG
88# define PNG_DEBUG 0
89#endif
90
Chris Craikb50c2172013-07-29 15:28:30 -070091#if PNG_DEBUG > 1
92# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
93# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
94# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
95#else
96# define pngtest_debug(m) ((void)0)
97# define pngtest_debug1(m,p1) ((void)0)
98# define pngtest_debug2(m,p1,p2) ((void)0)
99#endif
100
The Android Open Source Project893912b2009-03-03 19:30:05 -0800101#if !PNG_DEBUG
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400102# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800103#endif
104
Matt Sarett96be9082016-05-03 13:29:54 -0400105#ifndef PNG_UNUSED
106# define PNG_UNUSED(param) (void)param;
107#endif
108
The Android Open Source Project893912b2009-03-03 19:30:05 -0800109/* Turn on CPU timing
110#define PNGTEST_TIMING
111*/
112
Patrick Scott5f6bd842010-06-28 16:55:16 -0400113#ifndef PNG_FLOATING_POINT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800114#undef PNGTEST_TIMING
115#endif
116
117#ifdef PNGTEST_TIMING
118static float t_start, t_stop, t_decode, t_encode, t_misc;
119#include <time.h>
120#endif
121
Patrick Scott5f6bd842010-06-28 16:55:16 -0400122#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700123#define PNG_tIME_STRING_LENGTH 29
124static int tIME_chunk_present = 0;
125static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Matt Sarett96be9082016-05-03 13:29:54 -0400126
127#if PNG_LIBPNG_VER < 10619
128#define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
129
130static int
131tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
132{
133 png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
134
135 if (str == NULL)
136 return 0;
137
138 strcpy(ts, str);
139 return 1;
140}
141#endif /* older libpng */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800142#endif
143
144static int verbose = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700145static int strict = 0;
146static int relaxed = 0;
147static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
148static int error_count = 0; /* count calls to png_error */
149static int warning_count = 0; /* count calls to png_warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800150
The Android Open Source Project893912b2009-03-03 19:30:05 -0800151/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
152#ifndef png_jmpbuf
153# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
154#endif
155
Chris Craikb50c2172013-07-29 15:28:30 -0700156/* Defines for unknown chunk handling if required. */
157#ifndef PNG_HANDLE_CHUNK_ALWAYS
158# define PNG_HANDLE_CHUNK_ALWAYS 3
159#endif
160#ifndef PNG_HANDLE_CHUNK_IF_SAFE
161# define PNG_HANDLE_CHUNK_IF_SAFE 2
162#endif
163
164/* Utility to save typing/errors, the argument must be a name */
165#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
166
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400167/* Example of using row callbacks to make a simple progress meter */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700168static int status_pass = 1;
169static int status_dots_requested = 0;
170static int status_dots = 1;
171
Chris Craikb50c2172013-07-29 15:28:30 -0700172static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800173read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
174{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400175 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
176 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700177
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400178 if (status_pass != pass)
179 {
180 fprintf(stdout, "\n Pass %d: ", pass);
181 status_pass = pass;
182 status_dots = 31;
183 }
Chris Craikb50c2172013-07-29 15:28:30 -0700184
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400185 status_dots--;
Chris Craikb50c2172013-07-29 15:28:30 -0700186
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400187 if (status_dots == 0)
188 {
189 fprintf(stdout, "\n ");
190 status_dots=30;
191 }
Chris Craikb50c2172013-07-29 15:28:30 -0700192
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400193 fprintf(stdout, "r");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800194}
195
Chris Craikb50c2172013-07-29 15:28:30 -0700196#ifdef PNG_WRITE_SUPPORTED
197static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800198write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
199{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400200 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
201 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700202
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400203 fprintf(stdout, "w");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800204}
Chris Craikb50c2172013-07-29 15:28:30 -0700205#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800206
207
Patrick Scott5f6bd842010-06-28 16:55:16 -0400208#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400209/* Example of using a user transform callback (doesn't do anything at present).
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400210 */
Chris Craikb50c2172013-07-29 15:28:30 -0700211static void PNGCBAPI
Matt Sarett96be9082016-05-03 13:29:54 -0400212read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800213{
Matt Sarett96be9082016-05-03 13:29:54 -0400214 PNG_UNUSED(png_ptr)
215 PNG_UNUSED(row_info)
216 PNG_UNUSED(data)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800217}
218#endif
219
Patrick Scott5f6bd842010-06-28 16:55:16 -0400220#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400221/* Example of using user transform callback (we don't transform anything,
222 * but merely count the zero samples)
223 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800224
225static png_uint_32 zero_samples;
226
Chris Craikb50c2172013-07-29 15:28:30 -0700227static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800228count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
229{
230 png_bytep dp = data;
Chris Craikb50c2172013-07-29 15:28:30 -0700231 if (png_ptr == NULL)
232 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800233
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400234 /* Contents of row_info:
The Android Open Source Project893912b2009-03-03 19:30:05 -0800235 * png_uint_32 width width of row
236 * png_uint_32 rowbytes number of bytes in row
237 * png_byte color_type color type of pixels
238 * png_byte bit_depth bit depth of samples
239 * png_byte channels number of channels (1-4)
240 * png_byte pixel_depth bits per pixel (depth*channels)
241 */
242
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400243 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800244
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700245 if (row_info->color_type == 0 || row_info->color_type == 3)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800246 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700247 int pos = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800248 png_uint_32 n, nstop;
Chris Craikb50c2172013-07-29 15:28:30 -0700249
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700250 for (n = 0, nstop=row_info->width; n<nstop; n++)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800251 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700252 if (row_info->bit_depth == 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800253 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400254 if (((*dp << pos++ ) & 0x80) == 0)
255 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700256
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700257 if (pos == 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800258 {
259 pos = 0;
260 dp++;
261 }
262 }
Chris Craikb50c2172013-07-29 15:28:30 -0700263
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700264 if (row_info->bit_depth == 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800265 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400266 if (((*dp << (pos+=2)) & 0xc0) == 0)
267 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700268
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700269 if (pos == 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800270 {
271 pos = 0;
272 dp++;
273 }
274 }
Chris Craikb50c2172013-07-29 15:28:30 -0700275
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700276 if (row_info->bit_depth == 4)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800277 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400278 if (((*dp << (pos+=4)) & 0xf0) == 0)
279 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700280
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700281 if (pos == 8)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800282 {
283 pos = 0;
284 dp++;
285 }
286 }
Chris Craikb50c2172013-07-29 15:28:30 -0700287
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700288 if (row_info->bit_depth == 8)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400289 if (*dp++ == 0)
290 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700291
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700292 if (row_info->bit_depth == 16)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800293 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400294 if ((*dp | *(dp+1)) == 0)
295 zero_samples++;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800296 dp+=2;
297 }
298 }
299 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400300 else /* Other color types */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800301 {
302 png_uint_32 n, nstop;
303 int channel;
304 int color_channels = row_info->channels;
Matt Sarett96be9082016-05-03 13:29:54 -0400305 if (row_info->color_type > 3)
306 color_channels--;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800307
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700308 for (n = 0, nstop=row_info->width; n<nstop; n++)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800309 {
310 for (channel = 0; channel < color_channels; channel++)
311 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700312 if (row_info->bit_depth == 8)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400313 if (*dp++ == 0)
314 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700315
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700316 if (row_info->bit_depth == 16)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800317 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400318 if ((*dp | *(dp+1)) == 0)
319 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700320
The Android Open Source Project893912b2009-03-03 19:30:05 -0800321 dp+=2;
322 }
323 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700324 if (row_info->color_type > 3)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800325 {
326 dp++;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400327 if (row_info->bit_depth == 16)
328 dp++;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800329 }
330 }
331 }
332}
Matt Sarett96be9082016-05-03 13:29:54 -0400333#endif /* WRITE_USER_TRANSFORM */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800334
Patrick Scott5f6bd842010-06-28 16:55:16 -0400335#ifndef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800336/* START of code to validate stdio-free compilation */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400337/* These copies of the default read/write functions come from pngrio.c and
338 * pngwio.c. They allow "don't include stdio" testing of the library.
339 * This is the function that does the actual reading of data. If you are
340 * not reading from a standard C stream, you should create a replacement
341 * read_data function and use it at run time with png_set_read_fn(), rather
342 * than changing the library.
343 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800344
Chris Craikb50c2172013-07-29 15:28:30 -0700345#ifdef PNG_IO_STATE_SUPPORTED
346void
347pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
348 png_uint_32 io_op);
349void
350pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
351 png_uint_32 io_op)
352{
353 png_uint_32 io_state = png_get_io_state(png_ptr);
354 int err = 0;
355
356 /* Check if the current operation (reading / writing) is as expected. */
357 if ((io_state & PNG_IO_MASK_OP) != io_op)
358 png_error(png_ptr, "Incorrect operation in I/O state");
359
360 /* Check if the buffer size specific to the current location
361 * (file signature / header / data / crc) is as expected.
362 */
363 switch (io_state & PNG_IO_MASK_LOC)
364 {
365 case PNG_IO_SIGNATURE:
366 if (data_length > 8)
367 err = 1;
368 break;
369 case PNG_IO_CHUNK_HDR:
370 if (data_length != 8)
371 err = 1;
372 break;
373 case PNG_IO_CHUNK_DATA:
374 break; /* no restrictions here */
375 case PNG_IO_CHUNK_CRC:
376 if (data_length != 4)
377 err = 1;
378 break;
379 default:
380 err = 1; /* uninitialized */
381 }
Matt Sarett96be9082016-05-03 13:29:54 -0400382 if (err != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700383 png_error(png_ptr, "Bad I/O state or buffer size");
384}
385#endif
386
387static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800388pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
389{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400390 png_size_t check = 0;
391 png_voidp io_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800392
393 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
394 * instead of an int, which is what fread() actually returns.
395 */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400396 io_ptr = png_get_io_ptr(png_ptr);
397 if (io_ptr != NULL)
398 {
Chris Craikb50c2172013-07-29 15:28:30 -0700399 check = fread(data, 1, length, (png_FILE_p)io_ptr);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400400 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800401
402 if (check != length)
403 {
Chris Craikb50c2172013-07-29 15:28:30 -0700404 png_error(png_ptr, "Read Error");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800405 }
Chris Craikb50c2172013-07-29 15:28:30 -0700406
407#ifdef PNG_IO_STATE_SUPPORTED
408 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
409#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800410}
The Android Open Source Project893912b2009-03-03 19:30:05 -0800411
Patrick Scott5f6bd842010-06-28 16:55:16 -0400412#ifdef PNG_WRITE_FLUSH_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700413static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800414pngtest_flush(png_structp png_ptr)
415{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400416 /* Do nothing; fflush() is said to be just a waste of energy. */
Chris Craikb50c2172013-07-29 15:28:30 -0700417 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800418}
419#endif
420
421/* This is the function that does the actual writing of data. If you are
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400422 * not writing to a standard C stream, you should create a replacement
423 * write_data function and use it at run time with png_set_write_fn(), rather
424 * than changing the library.
425 */
Chris Craikb50c2172013-07-29 15:28:30 -0700426static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800427pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
428{
Chris Craikb50c2172013-07-29 15:28:30 -0700429 png_size_t check;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800430
Chris Craikb50c2172013-07-29 15:28:30 -0700431 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
432
The Android Open Source Project893912b2009-03-03 19:30:05 -0800433 if (check != length)
434 {
435 png_error(png_ptr, "Write Error");
436 }
Chris Craikb50c2172013-07-29 15:28:30 -0700437
438#ifdef PNG_IO_STATE_SUPPORTED
439 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
440#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800441}
Matt Sarett96be9082016-05-03 13:29:54 -0400442#endif /* !STDIO */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800443
444/* This function is called when there is a warning, but the library thinks
445 * it can continue anyway. Replacement functions don't have to do anything
446 * here if you don't want to. In the default configuration, png_ptr is
447 * not used, but it is passed in case it may be useful.
448 */
Chris Craikb50c2172013-07-29 15:28:30 -0700449typedef struct
450{
451 PNG_CONST char *file_name;
452} pngtest_error_parameters;
453
454static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800455pngtest_warning(png_structp png_ptr, png_const_charp message)
456{
457 PNG_CONST char *name = "UNKNOWN (ERROR!)";
Chris Craikb50c2172013-07-29 15:28:30 -0700458 pngtest_error_parameters *test =
459 (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
460
461 ++warning_count;
462
463 if (test != NULL && test->file_name != NULL)
464 name = test->file_name;
465
466 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800467}
468
469/* This is the default error handling function. Note that replacements for
470 * this function MUST NOT RETURN, or the program will likely crash. This
471 * function is used by default, or if the program supplies NULL for the
472 * error function pointer in png_set_error_fn().
473 */
Chris Craikb50c2172013-07-29 15:28:30 -0700474static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800475pngtest_error(png_structp png_ptr, png_const_charp message)
476{
Chris Craikb50c2172013-07-29 15:28:30 -0700477 ++error_count;
478
The Android Open Source Project893912b2009-03-03 19:30:05 -0800479 pngtest_warning(png_ptr, message);
480 /* We can return because png_error calls the default handler, which is
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400481 * actually OK in this case.
482 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800483}
Chris Craikb50c2172013-07-29 15:28:30 -0700484
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700485/* END of code to validate stdio-free compilation */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800486
487/* START of code to validate memory allocation and deallocation */
488#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
489
490/* Allocate memory. For reasonable files, size should never exceed
Matt Sarett96be9082016-05-03 13:29:54 -0400491 * 64K. However, zlib may allocate more than 64K if you don't tell
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400492 * it not to. See zconf.h and png.h for more information. zlib does
493 * need to allocate exactly 64K, so whatever you call here must
494 * have the ability to do that.
495 *
496 * This piece of code can be compiled to validate max 64K allocations
497 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
498 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800499typedef struct memory_information
500{
Chris Craikb50c2172013-07-29 15:28:30 -0700501 png_alloc_size_t size;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800502 png_voidp pointer;
Chris Craikb50c2172013-07-29 15:28:30 -0700503 struct memory_information *next;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800504} memory_information;
Chris Craikb50c2172013-07-29 15:28:30 -0700505typedef memory_information *memory_infop;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800506
507static memory_infop pinformation = NULL;
508static int current_allocation = 0;
509static int maximum_allocation = 0;
510static int total_allocation = 0;
511static int num_allocations = 0;
512
Chris Craikb50c2172013-07-29 15:28:30 -0700513png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
514 png_alloc_size_t size));
515void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800516
517png_voidp
Chris Craikb50c2172013-07-29 15:28:30 -0700518PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800519{
520
521 /* png_malloc has already tested for NULL; png_create_struct calls
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400522 * png_debug_malloc directly, with png_ptr == NULL which is OK
523 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800524
525 if (size == 0)
526 return (NULL);
527
528 /* This calls the library allocator twice, once to get the requested
529 buffer and once to get a new free list entry. */
530 {
531 /* Disable malloc_fn and free_fn */
532 memory_infop pinfo;
533 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
534 pinfo = (memory_infop)png_malloc(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700535 (sizeof *pinfo));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800536 pinfo->size = size;
537 current_allocation += size;
538 total_allocation += size;
539 num_allocations ++;
Chris Craikb50c2172013-07-29 15:28:30 -0700540
The Android Open Source Project893912b2009-03-03 19:30:05 -0800541 if (current_allocation > maximum_allocation)
542 maximum_allocation = current_allocation;
Chris Craikb50c2172013-07-29 15:28:30 -0700543
544 pinfo->pointer = png_malloc(png_ptr, size);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800545 /* Restore malloc_fn and free_fn */
Chris Craikb50c2172013-07-29 15:28:30 -0700546
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700547 png_set_mem_fn(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700548 NULL, png_debug_malloc, png_debug_free);
549
The Android Open Source Project893912b2009-03-03 19:30:05 -0800550 if (size != 0 && pinfo->pointer == NULL)
551 {
552 current_allocation -= size;
553 total_allocation -= size;
554 png_error(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700555 "out of memory in pngtest->png_debug_malloc");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800556 }
Chris Craikb50c2172013-07-29 15:28:30 -0700557
The Android Open Source Project893912b2009-03-03 19:30:05 -0800558 pinfo->next = pinformation;
559 pinformation = pinfo;
560 /* Make sure the caller isn't assuming zeroed memory. */
Chris Craikb50c2172013-07-29 15:28:30 -0700561 memset(pinfo->pointer, 0xdd, pinfo->size);
562
Matt Sarett96be9082016-05-03 13:29:54 -0400563 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700564 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700565 pinfo->pointer);
Chris Craikb50c2172013-07-29 15:28:30 -0700566
The Android Open Source Project893912b2009-03-03 19:30:05 -0800567 return (png_voidp)(pinfo->pointer);
568 }
569}
570
571/* Free a pointer. It is removed from the list at the same time. */
Chris Craikb50c2172013-07-29 15:28:30 -0700572void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800573png_debug_free(png_structp png_ptr, png_voidp ptr)
574{
575 if (png_ptr == NULL)
576 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Chris Craikb50c2172013-07-29 15:28:30 -0700577
The Android Open Source Project893912b2009-03-03 19:30:05 -0800578 if (ptr == 0)
579 {
580#if 0 /* This happens all the time. */
581 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
582#endif
583 return;
584 }
585
586 /* Unlink the element from the list. */
Matt Sarett96be9082016-05-03 13:29:54 -0400587 if (pinformation != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800588 {
Chris Craikb50c2172013-07-29 15:28:30 -0700589 memory_infop *ppinfo = &pinformation;
590
The Android Open Source Project893912b2009-03-03 19:30:05 -0800591 for (;;)
592 {
593 memory_infop pinfo = *ppinfo;
Chris Craikb50c2172013-07-29 15:28:30 -0700594
The Android Open Source Project893912b2009-03-03 19:30:05 -0800595 if (pinfo->pointer == ptr)
596 {
597 *ppinfo = pinfo->next;
598 current_allocation -= pinfo->size;
599 if (current_allocation < 0)
600 fprintf(STDERR, "Duplicate free of memory\n");
601 /* We must free the list element too, but first kill
602 the memory that is to be freed. */
Chris Craikb50c2172013-07-29 15:28:30 -0700603 memset(ptr, 0x55, pinfo->size);
Matt Sarett96be9082016-05-03 13:29:54 -0400604 free(pinfo);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700605 pinfo = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800606 break;
607 }
Chris Craikb50c2172013-07-29 15:28:30 -0700608
The Android Open Source Project893912b2009-03-03 19:30:05 -0800609 if (pinfo->next == NULL)
610 {
Matt Sarett96be9082016-05-03 13:29:54 -0400611 fprintf(STDERR, "Pointer %p not found\n", ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800612 break;
613 }
Chris Craikb50c2172013-07-29 15:28:30 -0700614
The Android Open Source Project893912b2009-03-03 19:30:05 -0800615 ppinfo = &pinfo->next;
616 }
617 }
618
619 /* Finally free the data. */
Matt Sarett96be9082016-05-03 13:29:54 -0400620 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700621 printf("Freeing %p\n", ptr);
622
Matt Sarett96be9082016-05-03 13:29:54 -0400623 if (ptr != NULL)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530624 free(ptr);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700625 ptr = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800626}
Matt Sarett96be9082016-05-03 13:29:54 -0400627#endif /* USER_MEM && DEBUG */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800628/* END of code to test memory allocation/deallocation */
629
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700630
Chris Craikb50c2172013-07-29 15:28:30 -0700631#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700632/* Demonstration of user chunk support of the sTER and vpAg chunks */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700633
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400634/* (sTER is a public chunk not yet known by libpng. vpAg is a private
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700635chunk used in ImageMagick to store "virtual page" size). */
636
Chris Craikb50c2172013-07-29 15:28:30 -0700637static struct user_chunk_data
638{
639 png_const_infop info_ptr;
640 png_uint_32 vpAg_width, vpAg_height;
641 png_byte vpAg_units;
642 png_byte sTER_mode;
643 int location[2];
644}
645user_chunk_data;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700646
Chris Craikb50c2172013-07-29 15:28:30 -0700647/* Used for location and order; zero means nothing. */
648#define have_sTER 0x01
649#define have_vpAg 0x02
650#define before_PLTE 0x10
651#define before_IDAT 0x20
652#define after_IDAT 0x40
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700653
Chris Craikb50c2172013-07-29 15:28:30 -0700654static void
655init_callback_info(png_const_infop info_ptr)
656{
657 MEMZERO(user_chunk_data);
658 user_chunk_data.info_ptr = info_ptr;
659}
660
661static int
662set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
663{
664 int location;
665
Matt Sarett96be9082016-05-03 13:29:54 -0400666 if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700667 return 0; /* already have one of these */
668
Matt Sarett96be9082016-05-03 13:29:54 -0400669 /* Find where we are (the code below zeroes info_ptr to indicate that the
Chris Craikb50c2172013-07-29 15:28:30 -0700670 * chunks before the first IDAT have been read.)
671 */
672 if (data->info_ptr == NULL) /* after IDAT */
673 location = what | after_IDAT;
674
Matt Sarett96be9082016-05-03 13:29:54 -0400675 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700676 location = what | before_IDAT;
677
678 else
679 location = what | before_PLTE;
680
681 if (data->location[0] == 0)
682 data->location[0] = location;
683
684 else
685 data->location[1] = location;
686
687 return 1; /* handled */
688}
689
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530690static int PNGCBAPI
691read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700692{
Chris Craikb50c2172013-07-29 15:28:30 -0700693 struct user_chunk_data *my_user_chunk_data =
694 (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
695
696 if (my_user_chunk_data == NULL)
697 png_error(png_ptr, "lost user chunk pointer");
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700698
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400699 /* Return one of the following:
700 * return (-n); chunk had an error
701 * return (0); did not recognize
702 * return (n); success
703 *
704 * The unknown chunk structure contains the chunk data:
705 * png_byte name[5];
706 * png_byte *data;
707 * png_size_t size;
708 *
709 * Note that libpng has already taken care of the CRC handling.
710 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700711
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400712 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
713 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
714 {
715 /* Found sTER chunk */
716 if (chunk->size != 1)
717 return (-1); /* Error return */
Chris Craikb50c2172013-07-29 15:28:30 -0700718
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400719 if (chunk->data[0] != 0 && chunk->data[0] != 1)
720 return (-1); /* Invalid mode */
Chris Craikb50c2172013-07-29 15:28:30 -0700721
Matt Sarett96be9082016-05-03 13:29:54 -0400722 if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700723 {
724 my_user_chunk_data->sTER_mode=chunk->data[0];
725 return (1);
726 }
727
728 else
729 return (0); /* duplicate sTER - give it to libpng */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400730 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700731
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400732 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
733 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
734 return (0); /* Did not recognize */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700735
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400736 /* Found ImageMagick vpAg chunk */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700737
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400738 if (chunk->size != 9)
739 return (-1); /* Error return */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700740
Matt Sarett96be9082016-05-03 13:29:54 -0400741 if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700742 return (0); /* duplicate vpAg */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700743
Chris Craikb50c2172013-07-29 15:28:30 -0700744 my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
745 my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
746 my_user_chunk_data->vpAg_units = chunk->data[8];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700747
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400748 return (1);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700749}
Chris Craikb50c2172013-07-29 15:28:30 -0700750
751#ifdef PNG_WRITE_SUPPORTED
752static void
753write_sTER_chunk(png_structp write_ptr)
754{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530755 png_byte sTER[5] = {115, 84, 69, 82, '\0'};
Chris Craikb50c2172013-07-29 15:28:30 -0700756
Matt Sarett96be9082016-05-03 13:29:54 -0400757 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700758 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
759
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530760 png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
Chris Craikb50c2172013-07-29 15:28:30 -0700761}
762
763static void
764write_vpAg_chunk(png_structp write_ptr)
765{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530766 png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
Chris Craikb50c2172013-07-29 15:28:30 -0700767
768 png_byte vpag_chunk_data[9];
769
Matt Sarett96be9082016-05-03 13:29:54 -0400770 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700771 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
772 (unsigned long)user_chunk_data.vpAg_width,
773 (unsigned long)user_chunk_data.vpAg_height,
774 user_chunk_data.vpAg_units);
775
776 png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
777 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
778 vpag_chunk_data[8] = user_chunk_data.vpAg_units;
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530779 png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
Chris Craikb50c2172013-07-29 15:28:30 -0700780}
781
782static void
783write_chunks(png_structp write_ptr, int location)
784{
785 int i;
786
787 /* Notice that this preserves the original chunk order, however chunks
788 * intercepted by the callback will be written *after* chunks passed to
789 * libpng. This will actually reverse a pair of sTER chunks or a pair of
790 * vpAg chunks, resulting in an error later. This is not worth worrying
791 * about - the chunks should not be duplicated!
792 */
793 for (i=0; i<2; ++i)
794 {
795 if (user_chunk_data.location[i] == (location | have_sTER))
796 write_sTER_chunk(write_ptr);
797
798 else if (user_chunk_data.location[i] == (location | have_vpAg))
799 write_vpAg_chunk(write_ptr);
800 }
801}
Matt Sarett96be9082016-05-03 13:29:54 -0400802#endif /* WRITE */
803#else /* !READ_USER_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -0700804# define write_chunks(pp,loc) ((void)0)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700805#endif
806/* END of code to demonstrate user chunk support */
807
Chris Craikb50c2172013-07-29 15:28:30 -0700808/* START of code to check that libpng has the required text support; this only
809 * checks for the write support because if read support is missing the chunk
810 * will simply not be reported back to pngtest.
811 */
812#ifdef PNG_TEXT_SUPPORTED
813static void
Matt Sarett96be9082016-05-03 13:29:54 -0400814pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700815 int num_text)
816{
817 while (num_text > 0)
818 {
819 switch (text_ptr[--num_text].compression)
820 {
821 case PNG_TEXT_COMPRESSION_NONE:
822 break;
823
824 case PNG_TEXT_COMPRESSION_zTXt:
825# ifndef PNG_WRITE_zTXt_SUPPORTED
826 ++unsupported_chunks;
Matt Sarett96be9082016-05-03 13:29:54 -0400827 /* In libpng 1.7 this now does an app-error, so stop it: */
828 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Chris Craikb50c2172013-07-29 15:28:30 -0700829# endif
830 break;
831
832 case PNG_ITXT_COMPRESSION_NONE:
833 case PNG_ITXT_COMPRESSION_zTXt:
834# ifndef PNG_WRITE_iTXt_SUPPORTED
835 ++unsupported_chunks;
Matt Sarett96be9082016-05-03 13:29:54 -0400836 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Chris Craikb50c2172013-07-29 15:28:30 -0700837# endif
838 break;
839
840 default:
841 /* This is an error */
842 png_error(png_ptr, "invalid text chunk compression field");
843 break;
844 }
845 }
846}
847#endif
848/* END of code to check that libpng has the required text support */
849
The Android Open Source Project893912b2009-03-03 19:30:05 -0800850/* Test one file */
Chris Craikb50c2172013-07-29 15:28:30 -0700851static int
The Android Open Source Project893912b2009-03-03 19:30:05 -0800852test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
853{
854 static png_FILE_p fpin;
855 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Chris Craikb50c2172013-07-29 15:28:30 -0700856 pngtest_error_parameters error_parameters;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800857 png_structp read_ptr;
858 png_infop read_info_ptr, end_info_ptr;
859#ifdef PNG_WRITE_SUPPORTED
860 png_structp write_ptr;
861 png_infop write_info_ptr;
862 png_infop write_end_info_ptr;
Matt Sarett96be9082016-05-03 13:29:54 -0400863#ifdef PNG_WRITE_FILTER_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530864 int interlace_preserved = 1;
Matt Sarett96be9082016-05-03 13:29:54 -0400865#endif /* WRITE_FILTER */
866#else /* !WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800867 png_structp write_ptr = NULL;
868 png_infop write_info_ptr = NULL;
869 png_infop write_end_info_ptr = NULL;
Matt Sarett96be9082016-05-03 13:29:54 -0400870#endif /* !WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800871 png_bytep row_buf;
872 png_uint_32 y;
873 png_uint_32 width, height;
Matt Sarett96be9082016-05-03 13:29:54 -0400874 volatile int num_passes;
875 int pass;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800876 int bit_depth, color_type;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800877
878 row_buf = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -0700879 error_parameters.file_name = inname;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800880
The Android Open Source Project893912b2009-03-03 19:30:05 -0800881 if ((fpin = fopen(inname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800882 {
883 fprintf(STDERR, "Could not find input file %s\n", inname);
884 return (1);
885 }
886
The Android Open Source Project893912b2009-03-03 19:30:05 -0800887 if ((fpout = fopen(outname, "wb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800888 {
889 fprintf(STDERR, "Could not open output file %s\n", outname);
890 FCLOSE(fpin);
891 return (1);
892 }
893
Chris Craikb50c2172013-07-29 15:28:30 -0700894 pngtest_debug("Allocating read and write structures");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800895#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700896 read_ptr =
Chris Craikb50c2172013-07-29 15:28:30 -0700897 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
898 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800899#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700900 read_ptr =
Chris Craikb50c2172013-07-29 15:28:30 -0700901 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800902#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700903 png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
904 pngtest_warning);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700905
The Android Open Source Project893912b2009-03-03 19:30:05 -0800906#ifdef PNG_WRITE_SUPPORTED
907#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700908 write_ptr =
Chris Craikb50c2172013-07-29 15:28:30 -0700909 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
910 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800911#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700912 write_ptr =
Chris Craikb50c2172013-07-29 15:28:30 -0700913 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800914#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700915 png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
916 pngtest_warning);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800917#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700918 pngtest_debug("Allocating read_info, write_info and end_info structures");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800919 read_info_ptr = png_create_info_struct(read_ptr);
920 end_info_ptr = png_create_info_struct(read_ptr);
921#ifdef PNG_WRITE_SUPPORTED
922 write_info_ptr = png_create_info_struct(write_ptr);
923 write_end_info_ptr = png_create_info_struct(write_ptr);
924#endif
925
Chris Craikb50c2172013-07-29 15:28:30 -0700926#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
927 init_callback_info(read_info_ptr);
928 png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
929 read_user_chunk_callback);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800930#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700931
932#ifdef PNG_SETJMP_SUPPORTED
933 pngtest_debug("Setting jmpbuf for read struct");
934 if (setjmp(png_jmpbuf(read_ptr)))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800935 {
936 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
937 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700938 row_buf = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800939 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
940#ifdef PNG_WRITE_SUPPORTED
941 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
942 png_destroy_write_struct(&write_ptr, &write_info_ptr);
943#endif
944 FCLOSE(fpin);
945 FCLOSE(fpout);
946 return (1);
947 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800948
949#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700950 pngtest_debug("Setting jmpbuf for write struct");
951
The Android Open Source Project893912b2009-03-03 19:30:05 -0800952 if (setjmp(png_jmpbuf(write_ptr)))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800953 {
954 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
955 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
956 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
957#ifdef PNG_WRITE_SUPPORTED
958 png_destroy_write_struct(&write_ptr, &write_info_ptr);
959#endif
960 FCLOSE(fpin);
961 FCLOSE(fpout);
962 return (1);
963 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800964#endif
965#endif
966
Matt Sarett96be9082016-05-03 13:29:54 -0400967 if (strict != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700968 {
969 /* Treat png_benign_error() as errors on read */
970 png_set_benign_errors(read_ptr, 0);
971
972#ifdef PNG_WRITE_SUPPORTED
973 /* Treat them as errors on write */
974 png_set_benign_errors(write_ptr, 0);
975#endif
976
977 /* if strict is not set, then app warnings and errors are treated as
978 * warnings in release builds, but not in unstable builds; this can be
979 * changed with '--relaxed'.
980 */
981 }
982
Matt Sarett96be9082016-05-03 13:29:54 -0400983 else if (relaxed != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700984 {
985 /* Allow application (pngtest) errors and warnings to pass */
986 png_set_benign_errors(read_ptr, 1);
987
988#ifdef PNG_WRITE_SUPPORTED
989 png_set_benign_errors(write_ptr, 1);
990#endif
991 }
992
993 pngtest_debug("Initializing input and output streams");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400994#ifdef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800995 png_init_io(read_ptr, fpin);
996# ifdef PNG_WRITE_SUPPORTED
997 png_init_io(write_ptr, fpout);
998# endif
999#else
1000 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1001# ifdef PNG_WRITE_SUPPORTED
1002 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Patrick Scott5f6bd842010-06-28 16:55:16 -04001003# ifdef PNG_WRITE_FLUSH_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001004 pngtest_flush);
1005# else
1006 NULL);
1007# endif
1008# endif
1009#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001010
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001011 if (status_dots_requested == 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001012 {
1013#ifdef PNG_WRITE_SUPPORTED
1014 png_set_write_status_fn(write_ptr, write_row_callback);
1015#endif
1016 png_set_read_status_fn(read_ptr, read_row_callback);
1017 }
Chris Craikb50c2172013-07-29 15:28:30 -07001018
The Android Open Source Project893912b2009-03-03 19:30:05 -08001019 else
1020 {
1021#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001022 png_set_write_status_fn(write_ptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001023#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001024 png_set_read_status_fn(read_ptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001025 }
1026
Patrick Scott5f6bd842010-06-28 16:55:16 -04001027#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001028 png_set_read_user_transform_fn(read_ptr, read_user_callback);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001029#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001030#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001031 zero_samples = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001032 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1033#endif
1034
Chris Craikb50c2172013-07-29 15:28:30 -07001035#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1036 /* Preserve all the unknown chunks, if possible. If this is disabled then,
1037 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1038 * libpng to *save* the unknown chunks on read (because we can't switch the
1039 * save option on!)
1040 *
1041 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1042 * unknown chunks and write will write them all.
1043 */
1044#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001045 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Chris Craikb50c2172013-07-29 15:28:30 -07001046 NULL, 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001047#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001048#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001049 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1050 NULL, 0);
1051#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001052#endif
1053
Chris Craikb50c2172013-07-29 15:28:30 -07001054 pngtest_debug("Reading info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001055 png_read_info(read_ptr, read_info_ptr);
1056
Chris Craikb50c2172013-07-29 15:28:30 -07001057#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1058 /* This is a bit of a hack; there is no obvious way in the callback function
1059 * to determine that the chunks before the first IDAT have been read, so
1060 * remove the info_ptr (which is only used to determine position relative to
1061 * PLTE) here to indicate that we are after the IDAT.
1062 */
1063 user_chunk_data.info_ptr = NULL;
1064#endif
1065
1066 pngtest_debug("Transferring info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001067 {
1068 int interlace_type, compression_type, filter_type;
1069
1070 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
Matt Sarett96be9082016-05-03 13:29:54 -04001071 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001072 {
1073 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001074 color_type, interlace_type, compression_type, filter_type);
Matt Sarett96be9082016-05-03 13:29:54 -04001075 /* num_passes may not be available below if interlace support is not
1076 * provided by libpng for both read and write.
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301077 */
1078 switch (interlace_type)
1079 {
1080 case PNG_INTERLACE_NONE:
Matt Sarett96be9082016-05-03 13:29:54 -04001081 num_passes = 1;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301082 break;
1083
1084 case PNG_INTERLACE_ADAM7:
Matt Sarett96be9082016-05-03 13:29:54 -04001085 num_passes = 7;
1086 break;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301087
1088 default:
Matt Sarett96be9082016-05-03 13:29:54 -04001089 png_error(read_ptr, "invalid interlace type");
1090 /*NOT REACHED*/
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301091 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001092 }
Matt Sarett96be9082016-05-03 13:29:54 -04001093
1094 else
1095 png_error(read_ptr, "png_get_IHDR failed");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001096 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04001097#ifdef PNG_FIXED_POINT_SUPPORTED
1098#ifdef PNG_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001099 {
1100 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1101 blue_y;
Chris Craikb50c2172013-07-29 15:28:30 -07001102
Patrick Scott5f6bd842010-06-28 16:55:16 -04001103 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
Matt Sarett96be9082016-05-03 13:29:54 -04001104 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001105 {
1106 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1107 red_y, green_x, green_y, blue_x, blue_y);
1108 }
1109 }
1110#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001111#ifdef PNG_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001112 {
1113 png_fixed_point gamma;
1114
Matt Sarett96be9082016-05-03 13:29:54 -04001115 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001116 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001117 }
1118#endif
1119#else /* Use floating point versions */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001120#ifdef PNG_FLOATING_POINT_SUPPORTED
1121#ifdef PNG_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001122 {
1123 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1124 blue_y;
Chris Craikb50c2172013-07-29 15:28:30 -07001125
The Android Open Source Project893912b2009-03-03 19:30:05 -08001126 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
Matt Sarett96be9082016-05-03 13:29:54 -04001127 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001128 {
1129 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1130 red_y, green_x, green_y, blue_x, blue_y);
1131 }
1132 }
1133#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001134#ifdef PNG_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001135 {
1136 double gamma;
1137
Matt Sarett96be9082016-05-03 13:29:54 -04001138 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001139 png_set_gAMA(write_ptr, write_info_ptr, gamma);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001140 }
1141#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001142#endif /* Floating point */
1143#endif /* Fixed point */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001144#ifdef PNG_iCCP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001145 {
1146 png_charp name;
Chris Craikb50c2172013-07-29 15:28:30 -07001147 png_bytep profile;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001148 png_uint_32 proflen;
1149 int compression_type;
1150
1151 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
Matt Sarett96be9082016-05-03 13:29:54 -04001152 &profile, &proflen) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001153 {
1154 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1155 profile, proflen);
1156 }
1157 }
1158#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001159#ifdef PNG_sRGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001160 {
1161 int intent;
1162
Matt Sarett96be9082016-05-03 13:29:54 -04001163 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001164 png_set_sRGB(write_ptr, write_info_ptr, intent);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001165 }
1166#endif
1167 {
1168 png_colorp palette;
1169 int num_palette;
1170
Matt Sarett96be9082016-05-03 13:29:54 -04001171 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001172 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001173 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04001174#ifdef PNG_bKGD_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001175 {
1176 png_color_16p background;
1177
Matt Sarett96be9082016-05-03 13:29:54 -04001178 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001179 {
1180 png_set_bKGD(write_ptr, write_info_ptr, background);
1181 }
1182 }
1183#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001184#ifdef PNG_hIST_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001185 {
1186 png_uint_16p hist;
1187
Matt Sarett96be9082016-05-03 13:29:54 -04001188 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001189 png_set_hIST(write_ptr, write_info_ptr, hist);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001190 }
1191#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001192#ifdef PNG_oFFs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001193 {
1194 png_int_32 offset_x, offset_y;
1195 int unit_type;
1196
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001197 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Matt Sarett96be9082016-05-03 13:29:54 -04001198 &unit_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001199 {
1200 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1201 }
1202 }
1203#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001204#ifdef PNG_pCAL_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001205 {
1206 png_charp purpose, units;
1207 png_charpp params;
1208 png_int_32 X0, X1;
1209 int type, nparams;
1210
1211 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
Matt Sarett96be9082016-05-03 13:29:54 -04001212 &nparams, &units, &params) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001213 {
1214 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1215 nparams, units, params);
1216 }
1217 }
1218#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001219#ifdef PNG_pHYs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001220 {
1221 png_uint_32 res_x, res_y;
1222 int unit_type;
1223
Matt Sarett96be9082016-05-03 13:29:54 -04001224 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1225 &unit_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001226 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001227 }
1228#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001229#ifdef PNG_sBIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001230 {
1231 png_color_8p sig_bit;
1232
Matt Sarett96be9082016-05-03 13:29:54 -04001233 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001234 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001235 }
1236#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001237#ifdef PNG_sCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001238#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1239 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001240 {
1241 int unit;
1242 double scal_width, scal_height;
1243
1244 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
Matt Sarett96be9082016-05-03 13:29:54 -04001245 &scal_height) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001246 {
1247 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1248 }
1249 }
1250#else
1251#ifdef PNG_FIXED_POINT_SUPPORTED
1252 {
1253 int unit;
1254 png_charp scal_width, scal_height;
1255
1256 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
Matt Sarett96be9082016-05-03 13:29:54 -04001257 &scal_height) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001258 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001259 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1260 scal_height);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001261 }
1262 }
1263#endif
1264#endif
1265#endif
Matt Sarett96be9082016-05-03 13:29:54 -04001266
1267#ifdef PNG_sPLT_SUPPORTED
1268 {
1269 png_sPLT_tp entries;
1270
1271 int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1272 if (num_entries)
1273 {
1274 png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1275 }
1276 }
1277#endif
1278
Patrick Scott5f6bd842010-06-28 16:55:16 -04001279#ifdef PNG_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001280 {
1281 png_textp text_ptr;
1282 int num_text;
1283
1284 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1285 {
Chris Craikb50c2172013-07-29 15:28:30 -07001286 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1287
1288 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1289
Matt Sarett96be9082016-05-03 13:29:54 -04001290 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001291 {
1292 int i;
1293
1294 printf("\n");
1295 for (i=0; i<num_text; i++)
1296 {
1297 printf(" Text compression[%d]=%d\n",
1298 i, text_ptr[i].compression);
1299 }
1300 }
1301
The Android Open Source Project893912b2009-03-03 19:30:05 -08001302 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1303 }
1304 }
1305#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001306#ifdef PNG_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001307 {
1308 png_timep mod_time;
1309
Matt Sarett96be9082016-05-03 13:29:54 -04001310 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001311 {
1312 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001313#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001314 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001315 tIME_string[(sizeof tIME_string) - 1] = '\0';
1316
1317 else
1318 {
1319 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1320 tIME_string[(sizeof tIME_string) - 1] = '\0';
1321 }
1322
The Android Open Source Project893912b2009-03-03 19:30:05 -08001323 tIME_chunk_present++;
Matt Sarett96be9082016-05-03 13:29:54 -04001324#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001325 }
1326 }
1327#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001328#ifdef PNG_tRNS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001329 {
Chris Craikb50c2172013-07-29 15:28:30 -07001330 png_bytep trans_alpha;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001331 int num_trans;
Chris Craikb50c2172013-07-29 15:28:30 -07001332 png_color_16p trans_color;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001333
Chris Craikb50c2172013-07-29 15:28:30 -07001334 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Matt Sarett96be9082016-05-03 13:29:54 -04001335 &trans_color) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001336 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001337 int sample_max = (1 << bit_depth);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001338 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001339 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Chris Craikb50c2172013-07-29 15:28:30 -07001340 (int)trans_color->gray > sample_max) ||
Patrick Scott5f6bd842010-06-28 16:55:16 -04001341 (color_type == PNG_COLOR_TYPE_RGB &&
Chris Craikb50c2172013-07-29 15:28:30 -07001342 ((int)trans_color->red > sample_max ||
1343 (int)trans_color->green > sample_max ||
1344 (int)trans_color->blue > sample_max))))
1345 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1346 trans_color);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001347 }
1348 }
1349#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001350#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001351 {
1352 png_unknown_chunkp unknowns;
Chris Craikb50c2172013-07-29 15:28:30 -07001353 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001354 &unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001355
Matt Sarett96be9082016-05-03 13:29:54 -04001356 if (num_unknowns != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001357 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001358 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1359 num_unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001360#if PNG_LIBPNG_VER < 10600
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001361 /* Copy the locations from the read_info_ptr. The automatically
Chris Craikb50c2172013-07-29 15:28:30 -07001362 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1363 * because they are reset from the write pointer (removed in 1.6.0).
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001364 */
Chris Craikb50c2172013-07-29 15:28:30 -07001365 {
1366 int i;
1367 for (i = 0; i < num_unknowns; i++)
1368 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1369 unknowns[i].location);
1370 }
1371#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001372 }
1373 }
1374#endif
1375
1376#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001377 pngtest_debug("Writing info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001378
Chris Craikb50c2172013-07-29 15:28:30 -07001379 /* Write the info in two steps so that if we write the 'unknown' chunks here
1380 * they go to the correct place.
1381 */
1382 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1383
1384 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1385
The Android Open Source Project893912b2009-03-03 19:30:05 -08001386 png_write_info(write_ptr, write_info_ptr);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001387
Chris Craikb50c2172013-07-29 15:28:30 -07001388 write_chunks(write_ptr, before_IDAT); /* after PLTE */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001389#endif
1390
1391#ifdef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001392 pngtest_debug("Allocating row buffer...");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001393 row_buf = (png_bytep)png_malloc(read_ptr,
1394 png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001395
1396 pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001397#endif /* SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001398 pngtest_debug("Writing row data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001399
Matt Sarett96be9082016-05-03 13:29:54 -04001400#if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1401 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1402 /* Both must be defined for libpng to be able to handle the interlace,
1403 * otherwise it gets handled below by simply reading and writing the passes
1404 * directly.
1405 */
1406 if (png_set_interlace_handling(read_ptr) != num_passes)
1407 png_error(write_ptr,
1408 "png_set_interlace_handling(read): wrong pass count ");
1409 if (png_set_interlace_handling(write_ptr) != num_passes)
1410 png_error(write_ptr,
1411 "png_set_interlace_handling(write): wrong pass count ");
1412#else /* png_set_interlace_handling not called on either read or write */
1413# define calc_pass_height
1414#endif /* not using libpng interlace handling */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001415
1416#ifdef PNGTEST_TIMING
1417 t_stop = (float)clock();
1418 t_misc += (t_stop - t_start);
1419 t_start = t_stop;
1420#endif
Matt Sarett96be9082016-05-03 13:29:54 -04001421 for (pass = 0; pass < num_passes; pass++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001422 {
Matt Sarett96be9082016-05-03 13:29:54 -04001423# ifdef calc_pass_height
1424 png_uint_32 pass_height;
1425
1426 if (num_passes == 7) /* interlaced */
1427 {
1428 if (PNG_PASS_COLS(width, pass) > 0)
1429 pass_height = PNG_PASS_ROWS(height, pass);
1430
1431 else
1432 pass_height = 0;
1433 }
1434
1435 else /* not interlaced */
1436 pass_height = height;
1437# else
1438# define pass_height height
1439# endif
1440
Chris Craikb50c2172013-07-29 15:28:30 -07001441 pngtest_debug1("Writing row data for pass %d", pass);
Matt Sarett96be9082016-05-03 13:29:54 -04001442 for (y = 0; y < pass_height; y++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001443 {
1444#ifndef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001445 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Matt Sarett96be9082016-05-03 13:29:54 -04001446
The Android Open Source Project893912b2009-03-03 19:30:05 -08001447 row_buf = (png_bytep)png_malloc(read_ptr,
1448 png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001449
Matt Sarett96be9082016-05-03 13:29:54 -04001450 pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1451 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001452
The Android Open Source Project893912b2009-03-03 19:30:05 -08001453#endif /* !SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001454 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001455
1456#ifdef PNG_WRITE_SUPPORTED
1457#ifdef PNGTEST_TIMING
1458 t_stop = (float)clock();
1459 t_decode += (t_stop - t_start);
1460 t_start = t_stop;
1461#endif
1462 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1463#ifdef PNGTEST_TIMING
1464 t_stop = (float)clock();
1465 t_encode += (t_stop - t_start);
1466 t_start = t_stop;
1467#endif
Matt Sarett96be9082016-05-03 13:29:54 -04001468#endif /* WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001469
1470#ifndef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001471 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001472 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001473 row_buf = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001474#endif /* !SINGLE_ROWBUF_ALLOC */
1475 }
1476 }
1477
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301478#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1479# ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1480 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1481# endif
1482# ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1483 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1484# endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001485#endif
1486
Chris Craikb50c2172013-07-29 15:28:30 -07001487 pngtest_debug("Reading and writing end_info data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001488
1489 png_read_end(read_ptr, end_info_ptr);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001490#ifdef PNG_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001491 {
1492 png_textp text_ptr;
1493 int num_text;
1494
1495 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1496 {
Chris Craikb50c2172013-07-29 15:28:30 -07001497 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1498
1499 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1500
Matt Sarett96be9082016-05-03 13:29:54 -04001501 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001502 {
1503 int i;
1504
1505 printf("\n");
1506 for (i=0; i<num_text; i++)
1507 {
1508 printf(" Text compression[%d]=%d\n",
1509 i, text_ptr[i].compression);
1510 }
1511 }
1512
The Android Open Source Project893912b2009-03-03 19:30:05 -08001513 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1514 }
1515 }
1516#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001517#ifdef PNG_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001518 {
1519 png_timep mod_time;
1520
Matt Sarett96be9082016-05-03 13:29:54 -04001521 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001522 {
1523 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001524#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001525 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001526 tIME_string[(sizeof tIME_string) - 1] = '\0';
1527
1528 else
1529 {
1530 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1531 tIME_string[(sizeof tIME_string)-1] = '\0';
1532 }
1533
The Android Open Source Project893912b2009-03-03 19:30:05 -08001534 tIME_chunk_present++;
Matt Sarett96be9082016-05-03 13:29:54 -04001535#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001536 }
1537 }
1538#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001539#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001540 {
1541 png_unknown_chunkp unknowns;
Chris Craikb50c2172013-07-29 15:28:30 -07001542 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001543 &unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001544
Matt Sarett96be9082016-05-03 13:29:54 -04001545 if (num_unknowns != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001546 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001547 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1548 num_unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001549#if PNG_LIBPNG_VER < 10600
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001550 /* Copy the locations from the read_info_ptr. The automatically
Chris Craikb50c2172013-07-29 15:28:30 -07001551 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1552 * because they are reset from the write pointer (removed in 1.6.0).
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001553 */
Chris Craikb50c2172013-07-29 15:28:30 -07001554 {
1555 int i;
1556 for (i = 0; i < num_unknowns; i++)
1557 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1558 unknowns[i].location);
1559 }
1560#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001561 }
1562 }
1563#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001564
The Android Open Source Project893912b2009-03-03 19:30:05 -08001565#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001566#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1567 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1568 * This is here just to make pngtest replicate the results from libpng
1569 * versions prior to 1.5.4, and to test this new API.
1570 */
1571 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1572#endif
1573
1574 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1575 * do it is to write them *before* calling png_write_end. When unknown
1576 * chunks are written by libpng, however, they are written just before IEND.
1577 * There seems to be no way round this, however vpAg/sTER are not expected
1578 * after IDAT.
1579 */
1580 write_chunks(write_ptr, after_IDAT);
1581
The Android Open Source Project893912b2009-03-03 19:30:05 -08001582 png_write_end(write_ptr, write_end_info_ptr);
1583#endif
1584
1585#ifdef PNG_EASY_ACCESS_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001586 if (verbose != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001587 {
1588 png_uint_32 iwidth, iheight;
1589 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1590 iheight = png_get_image_height(write_ptr, write_info_ptr);
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001591 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
The Android Open Source Project893912b2009-03-03 19:30:05 -08001592 (unsigned long)iwidth, (unsigned long)iheight);
1593 }
1594#endif
1595
Chris Craikb50c2172013-07-29 15:28:30 -07001596 pngtest_debug("Destroying data structs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001597#ifdef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001598 pngtest_debug("destroying row_buf for read_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001599 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001600 row_buf = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001601#endif /* SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001602 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001603 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1604#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001605 pngtest_debug("destroying write_end_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001606 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07001607 pngtest_debug("destroying write_ptr, write_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001608 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1609#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001610 pngtest_debug("Destruction complete.");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001611
1612 FCLOSE(fpin);
1613 FCLOSE(fpout);
1614
Chris Craikb50c2172013-07-29 15:28:30 -07001615 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1616 * Unsupported chunks can result in warnings, in that case ignore the strict
1617 * setting, otherwise fail the test on warnings as well as errors.
1618 */
1619 if (error_count > 0)
1620 {
1621 /* We don't really expect to get here because of the setjmp handling
1622 * above, but this is safe.
1623 */
1624 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
1625 inname, error_count, warning_count);
1626
1627 if (strict != 0)
1628 return (1);
1629 }
1630
1631# ifdef PNG_WRITE_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001632 /* If there is no write support nothing was written! */
Chris Craikb50c2172013-07-29 15:28:30 -07001633 else if (unsupported_chunks > 0)
1634 {
1635 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1636 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1637 }
1638# endif
1639
1640 else if (warning_count > 0)
1641 {
1642 fprintf(STDERR, "\n %s: %d libpng warnings found",
1643 inname, warning_count);
1644
1645 if (strict != 0)
1646 return (1);
1647 }
1648
1649 pngtest_debug("Opening files for comparison");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001650 if ((fpin = fopen(inname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001651 {
1652 fprintf(STDERR, "Could not find file %s\n", inname);
1653 return (1);
1654 }
1655
The Android Open Source Project893912b2009-03-03 19:30:05 -08001656 if ((fpout = fopen(outname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001657 {
1658 fprintf(STDERR, "Could not find file %s\n", outname);
1659 FCLOSE(fpin);
1660 return (1);
1661 }
1662
Matt Sarett96be9082016-05-03 13:29:54 -04001663#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1664 defined (PNG_WRITE_FILTER_SUPPORTED)
1665 if (interlace_preserved != 0) /* else the files will be changed */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001666 {
Chris Craikb50c2172013-07-29 15:28:30 -07001667 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001668 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301669 static int wrote_question = 0;
Chris Craikb50c2172013-07-29 15:28:30 -07001670 png_size_t num_in, num_out;
1671 char inbuf[256], outbuf[256];
The Android Open Source Project893912b2009-03-03 19:30:05 -08001672
Chris Craikb50c2172013-07-29 15:28:30 -07001673 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1674 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1675
1676 if (num_in != num_out)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001677 {
Chris Craikb50c2172013-07-29 15:28:30 -07001678 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1679 inname, outname);
1680
1681 if (wrote_question == 0 && unsupported_chunks == 0)
1682 {
1683 fprintf(STDERR,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001684 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001685 inname, PNG_ZBUF_SIZE);
Chris Craikb50c2172013-07-29 15:28:30 -07001686 fprintf(STDERR,
1687 "\n filtering heuristic (libpng default), compression");
1688 fprintf(STDERR,
1689 " level (zlib default),\n and zlib version (%s)?\n\n",
1690 ZLIB_VERSION);
1691 wrote_question = 1;
1692 }
1693
1694 FCLOSE(fpin);
1695 FCLOSE(fpout);
1696
1697 if (strict != 0 && unsupported_chunks == 0)
1698 return (1);
1699
1700 else
1701 return (0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001702 }
Chris Craikb50c2172013-07-29 15:28:30 -07001703
Matt Sarett96be9082016-05-03 13:29:54 -04001704 if (num_in == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001705 break;
1706
1707 if (memcmp(inbuf, outbuf, num_in))
1708 {
1709 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1710 outname);
1711
1712 if (wrote_question == 0 && unsupported_chunks == 0)
1713 {
1714 fprintf(STDERR,
1715 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1716 inname, PNG_ZBUF_SIZE);
1717 fprintf(STDERR,
1718 "\n filtering heuristic (libpng default), compression");
1719 fprintf(STDERR,
1720 " level (zlib default),\n and zlib version (%s)?\n\n",
1721 ZLIB_VERSION);
1722 wrote_question = 1;
1723 }
1724
1725 FCLOSE(fpin);
1726 FCLOSE(fpout);
1727
1728 /* NOTE: the unsupported_chunks escape is permitted here because
1729 * unsupported text chunk compression will result in the compression
1730 * mode being changed (to NONE) yet, in the test case, the result
1731 * can be exactly the same size!
1732 */
1733 if (strict != 0 && unsupported_chunks == 0)
1734 return (1);
1735
1736 else
1737 return (0);
1738 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001739 }
1740 }
Matt Sarett96be9082016-05-03 13:29:54 -04001741#endif /* WRITE && WRITE_FILTER */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001742
1743 FCLOSE(fpin);
1744 FCLOSE(fpout);
1745
1746 return (0);
1747}
1748
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001749/* Input and output filenames */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001750#ifdef RISCOS
1751static PNG_CONST char *inname = "pngtest/png";
1752static PNG_CONST char *outname = "pngout/png";
1753#else
1754static PNG_CONST char *inname = "pngtest.png";
1755static PNG_CONST char *outname = "pngout.png";
1756#endif
1757
1758int
1759main(int argc, char *argv[])
1760{
1761 int multiple = 0;
1762 int ierror = 0;
1763
Matt Sarett96be9082016-05-03 13:29:54 -04001764 png_structp dummy_ptr;
1765
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001766 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001767 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001768 fprintf(STDERR, "%s", png_get_copyright(NULL));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001769 /* Show the version of libpng used in building the library */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001770 fprintf(STDERR, " library (%lu):%s",
The Android Open Source Project893912b2009-03-03 19:30:05 -08001771 (unsigned long)png_access_version_number(),
1772 png_get_header_version(NULL));
Chris Craikb50c2172013-07-29 15:28:30 -07001773
The Android Open Source Project893912b2009-03-03 19:30:05 -08001774 /* Show the version of libpng used in building the application */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001775 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001776 PNG_HEADER_VERSION_STRING);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001777
1778 /* Do some consistency checking on the memory allocation settings, I'm
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001779 * not sure this matters, but it is nice to know, the first of these
1780 * tests should be impossible because of the way the macros are set
1781 * in pngconf.h
1782 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001783#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1784 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1785#endif
1786 /* I think the following can happen. */
1787#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1788 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1789#endif
1790
1791 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1792 {
1793 fprintf(STDERR,
1794 "Warning: versions are different between png.h and png.c\n");
1795 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1796 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1797 ++ierror;
1798 }
1799
1800 if (argc > 1)
1801 {
1802 if (strcmp(argv[1], "-m") == 0)
1803 {
1804 multiple = 1;
1805 status_dots_requested = 0;
1806 }
Chris Craikb50c2172013-07-29 15:28:30 -07001807
The Android Open Source Project893912b2009-03-03 19:30:05 -08001808 else if (strcmp(argv[1], "-mv") == 0 ||
1809 strcmp(argv[1], "-vm") == 0 )
1810 {
1811 multiple = 1;
1812 verbose = 1;
1813 status_dots_requested = 1;
1814 }
Chris Craikb50c2172013-07-29 15:28:30 -07001815
The Android Open Source Project893912b2009-03-03 19:30:05 -08001816 else if (strcmp(argv[1], "-v") == 0)
1817 {
1818 verbose = 1;
1819 status_dots_requested = 1;
1820 inname = argv[2];
1821 }
Chris Craikb50c2172013-07-29 15:28:30 -07001822
1823 else if (strcmp(argv[1], "--strict") == 0)
1824 {
1825 status_dots_requested = 0;
1826 verbose = 1;
1827 inname = argv[2];
1828 strict++;
1829 relaxed = 0;
1830 }
1831
1832 else if (strcmp(argv[1], "--relaxed") == 0)
1833 {
1834 status_dots_requested = 0;
1835 verbose = 1;
1836 inname = argv[2];
1837 strict = 0;
1838 relaxed++;
1839 }
1840
The Android Open Source Project893912b2009-03-03 19:30:05 -08001841 else
1842 {
1843 inname = argv[1];
1844 status_dots_requested = 0;
1845 }
1846 }
1847
Matt Sarett96be9082016-05-03 13:29:54 -04001848 if (multiple == 0 && argc == 3 + verbose)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001849 outname = argv[2 + verbose];
The Android Open Source Project893912b2009-03-03 19:30:05 -08001850
Matt Sarett96be9082016-05-03 13:29:54 -04001851 if ((multiple == 0 && argc > 3 + verbose) ||
1852 (multiple != 0 && argc < 2))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001853 {
1854 fprintf(STDERR,
1855 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1856 argv[0], argv[0]);
1857 fprintf(STDERR,
1858 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1859 fprintf(STDERR,
1860 " with -m %s is used as a temporary file\n", outname);
1861 exit(1);
1862 }
1863
Matt Sarett96be9082016-05-03 13:29:54 -04001864 if (multiple != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001865 {
1866 int i;
1867#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1868 int allocation_now = current_allocation;
1869#endif
1870 for (i=2; i<argc; ++i)
1871 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001872 int kerror;
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001873 fprintf(STDERR, "\n Testing %s:", argv[i]);
Matt Sarett96be9082016-05-03 13:29:54 -04001874#if PNG_DEBUG > 0
1875 fprintf(STDERR, "\n");
1876#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001877 kerror = test_one_file(argv[i], outname);
1878 if (kerror == 0)
1879 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001880#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001881 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1882 (unsigned long)zero_samples);
1883#else
1884 fprintf(STDERR, " PASS\n");
1885#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001886#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -04001887 if (tIME_chunk_present != 0)
1888 fprintf(STDERR, " tIME = %s\n", tIME_string);
Chris Craikb50c2172013-07-29 15:28:30 -07001889
Matt Sarett96be9082016-05-03 13:29:54 -04001890 tIME_chunk_present = 0;
1891#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001892 }
Chris Craikb50c2172013-07-29 15:28:30 -07001893
The Android Open Source Project893912b2009-03-03 19:30:05 -08001894 else
1895 {
1896 fprintf(STDERR, " FAIL\n");
1897 ierror += kerror;
1898 }
1899#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1900 if (allocation_now != current_allocation)
1901 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001902 current_allocation - allocation_now);
Chris Craikb50c2172013-07-29 15:28:30 -07001903
The Android Open Source Project893912b2009-03-03 19:30:05 -08001904 if (current_allocation != 0)
1905 {
1906 memory_infop pinfo = pinformation;
1907
1908 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1909 current_allocation);
Chris Craikb50c2172013-07-29 15:28:30 -07001910
The Android Open Source Project893912b2009-03-03 19:30:05 -08001911 while (pinfo != NULL)
1912 {
Matt Sarett96be9082016-05-03 13:29:54 -04001913 fprintf(STDERR, " %lu bytes at %p\n",
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001914 (unsigned long)pinfo->size,
Matt Sarett96be9082016-05-03 13:29:54 -04001915 pinfo->pointer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001916 pinfo = pinfo->next;
1917 }
1918 }
1919#endif
1920 }
1921#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1922 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1923 current_allocation);
1924 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1925 maximum_allocation);
1926 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1927 total_allocation);
1928 fprintf(STDERR, " Number of allocations: %10d\n",
1929 num_allocations);
1930#endif
1931 }
Chris Craikb50c2172013-07-29 15:28:30 -07001932
The Android Open Source Project893912b2009-03-03 19:30:05 -08001933 else
1934 {
1935 int i;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001936 for (i = 0; i<3; ++i)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001937 {
1938 int kerror;
1939#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1940 int allocation_now = current_allocation;
1941#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001942 if (i == 1)
1943 status_dots_requested = 1;
1944
1945 else if (verbose == 0)
1946 status_dots_requested = 0;
1947
The Android Open Source Project893912b2009-03-03 19:30:05 -08001948 if (i == 0 || verbose == 1 || ierror != 0)
Matt Sarett96be9082016-05-03 13:29:54 -04001949 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001950 fprintf(STDERR, "\n Testing %s:", inname);
Matt Sarett96be9082016-05-03 13:29:54 -04001951#if PNG_DEBUG > 0
1952 fprintf(STDERR, "\n");
1953#endif
1954 }
Chris Craikb50c2172013-07-29 15:28:30 -07001955
The Android Open Source Project893912b2009-03-03 19:30:05 -08001956 kerror = test_one_file(inname, outname);
Chris Craikb50c2172013-07-29 15:28:30 -07001957
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001958 if (kerror == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001959 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001960 if (verbose == 1 || i == 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001961 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001962#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001963 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1964 (unsigned long)zero_samples);
1965#else
1966 fprintf(STDERR, " PASS\n");
1967#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001968#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001969 if (tIME_chunk_present != 0)
1970 fprintf(STDERR, " tIME = %s\n", tIME_string);
Matt Sarett96be9082016-05-03 13:29:54 -04001971#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001972 }
1973 }
Chris Craikb50c2172013-07-29 15:28:30 -07001974
The Android Open Source Project893912b2009-03-03 19:30:05 -08001975 else
1976 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001977 if (verbose == 0 && i != 2)
Matt Sarett96be9082016-05-03 13:29:54 -04001978 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001979 fprintf(STDERR, "\n Testing %s:", inname);
Matt Sarett96be9082016-05-03 13:29:54 -04001980#if PNG_DEBUG > 0
1981 fprintf(STDERR, "\n");
1982#endif
1983 }
Chris Craikb50c2172013-07-29 15:28:30 -07001984
The Android Open Source Project893912b2009-03-03 19:30:05 -08001985 fprintf(STDERR, " FAIL\n");
1986 ierror += kerror;
1987 }
1988#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1989 if (allocation_now != current_allocation)
1990 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001991 current_allocation - allocation_now);
Chris Craikb50c2172013-07-29 15:28:30 -07001992
The Android Open Source Project893912b2009-03-03 19:30:05 -08001993 if (current_allocation != 0)
1994 {
1995 memory_infop pinfo = pinformation;
1996
1997 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1998 current_allocation);
Chris Craikb50c2172013-07-29 15:28:30 -07001999
The Android Open Source Project893912b2009-03-03 19:30:05 -08002000 while (pinfo != NULL)
2001 {
Matt Sarett96be9082016-05-03 13:29:54 -04002002 fprintf(STDERR, " %lu bytes at %p\n",
2003 (unsigned long)pinfo->size, pinfo->pointer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002004 pinfo = pinfo->next;
2005 }
2006 }
2007#endif
2008 }
2009#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2010 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2011 current_allocation);
2012 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2013 maximum_allocation);
2014 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2015 total_allocation);
2016 fprintf(STDERR, " Number of allocations: %10d\n",
2017 num_allocations);
2018#endif
2019 }
2020
2021#ifdef PNGTEST_TIMING
2022 t_stop = (float)clock();
2023 t_misc += (t_stop - t_start);
2024 t_start = t_stop;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002025 fprintf(STDERR, " CPU time used = %.3f seconds",
The Android Open Source Project893912b2009-03-03 19:30:05 -08002026 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002027 fprintf(STDERR, " (decoding %.3f,\n",
The Android Open Source Project893912b2009-03-03 19:30:05 -08002028 t_decode/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002029 fprintf(STDERR, " encoding %.3f ,",
The Android Open Source Project893912b2009-03-03 19:30:05 -08002030 t_encode/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002031 fprintf(STDERR, " other %.3f seconds)\n\n",
The Android Open Source Project893912b2009-03-03 19:30:05 -08002032 t_misc/(float)CLOCKS_PER_SEC);
2033#endif
2034
2035 if (ierror == 0)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002036 fprintf(STDERR, " libpng passes test\n");
Chris Craikb50c2172013-07-29 15:28:30 -07002037
The Android Open Source Project893912b2009-03-03 19:30:05 -08002038 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002039 fprintf(STDERR, " libpng FAILS test\n");
Chris Craikb50c2172013-07-29 15:28:30 -07002040
Matt Sarett96be9082016-05-03 13:29:54 -04002041 dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2042 fprintf(STDERR, " Default limits:\n");
2043 fprintf(STDERR, " width_max = %lu\n",
2044 (unsigned long) png_get_user_width_max(dummy_ptr));
2045 fprintf(STDERR, " height_max = %lu\n",
2046 (unsigned long) png_get_user_height_max(dummy_ptr));
2047 if (png_get_chunk_cache_max(dummy_ptr) == 0)
2048 fprintf(STDERR, " cache_max = unlimited\n");
2049 else
2050 fprintf(STDERR, " cache_max = %lu\n",
2051 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
2052 if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2053 fprintf(STDERR, " malloc_max = unlimited\n");
2054 else
2055 fprintf(STDERR, " malloc_max = %lu\n",
2056 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2057 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2058
The Android Open Source Project893912b2009-03-03 19:30:05 -08002059 return (int)(ierror != 0);
2060}
Chris Craikb50c2172013-07-29 15:28:30 -07002061#else
2062int
2063main(void)
2064{
2065 fprintf(STDERR,
2066 " test ignored because libpng was not built with read support\n");
2067 /* And skip this test */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302068 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
Chris Craikb50c2172013-07-29 15:28:30 -07002069}
2070#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002071
2072/* Generate a compiler error if there is an old png.h in the search path. */
Matt Sarett96be9082016-05-03 13:29:54 -04002073typedef png_libpng_version_1_6_20 Your_png_h_is_not_version_1_6_20;