Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2 | /* pngtest.c - a simple test program to test libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 67ee8ce | 2011-12-22 08:21:00 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 1531bd6 | 2012-01-01 14:45:04 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2012 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 13 | * This program reads in a PNG image, writes it out again, and then |
| 14 | * compares the two files. If the files are identical, this shows that |
| 15 | * the basic chunk handling, filtering, and (de)compression code is working |
| 16 | * properly. It does not currently test all of the transforms, although |
| 17 | * it probably should. |
| 18 | * |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 19 | * The program will report "FAIL" in certain legitimate cases: |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 20 | * 1) when the compression level or filter selection method is changed. |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 21 | * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192. |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 22 | * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks |
| 23 | * exist in the input file. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 24 | * 4) others not listed here... |
| 25 | * In these cases, it is best to check with another tool such as "pngcheck" |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 26 | * to see what the differences between the two files are. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 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 |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 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 ..." |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 32 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 33 | |
Glenn Randers-Pehrson | b3b7168 | 2011-05-03 22:30:19 -0500 | [diff] [blame] | 34 | #define _POSIX_SOURCE 1 |
| 35 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 36 | #include "zlib.h" |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 37 | #include "png.h" |
Glenn Randers-Pehrson | 7253144 | 2010-04-17 08:17:51 -0500 | [diff] [blame] | 38 | /* Copied from pngpriv.h but only used in error messages below. */ |
| 39 | #ifndef PNG_ZBUF_SIZE |
| 40 | # define PNG_ZBUF_SIZE 8192 |
| 41 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 42 | # include <stdio.h> |
| 43 | # include <stdlib.h> |
Glenn Randers-Pehrson | 7253144 | 2010-04-17 08:17:51 -0500 | [diff] [blame] | 44 | # include <string.h> |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 45 | # define FCLOSE(file) fclose(file) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 46 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 47 | #ifndef PNG_STDIO_SUPPORTED |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 48 | typedef FILE * png_FILE_p; |
Glenn Randers-Pehrson | be9de0f | 2001-01-22 08:52:16 -0600 | [diff] [blame] | 49 | #endif |
| 50 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 51 | /* Makes pngtest verbose so we can find problems. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 52 | #ifndef PNG_DEBUG |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 53 | # define PNG_DEBUG 0 |
| 54 | #endif |
| 55 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 56 | #if PNG_DEBUG > 1 |
| 57 | # define pngtest_debug(m) ((void)fprintf(stderr, m "\n")) |
| 58 | # define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1)) |
| 59 | # define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2)) |
| 60 | #else |
| 61 | # define pngtest_debug(m) ((void)0) |
| 62 | # define pngtest_debug1(m,p1) ((void)0) |
| 63 | # define pngtest_debug2(m,p1,p2) ((void)0) |
| 64 | #endif |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 65 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 66 | #if !PNG_DEBUG |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 67 | # define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */ |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 68 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 69 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 70 | /* Turn on CPU timing |
| 71 | #define PNGTEST_TIMING |
| 72 | */ |
| 73 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 74 | #ifndef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 75 | #undef PNGTEST_TIMING |
| 76 | #endif |
| 77 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 78 | #ifdef PNGTEST_TIMING |
| 79 | static float t_start, t_stop, t_decode, t_encode, t_misc; |
| 80 | #include <time.h> |
| 81 | #endif |
| 82 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 83 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | a5fa5c9 | 2008-09-06 07:06:22 -0500 | [diff] [blame] | 84 | #define PNG_tIME_STRING_LENGTH 29 |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 85 | static int tIME_chunk_present = 0; |
Glenn Randers-Pehrson | a5fa5c9 | 2008-09-06 07:06:22 -0500 | [diff] [blame] | 86 | static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present"; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 87 | #endif |
| 88 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 89 | static int verbose = 0; |
Glenn Randers-Pehrson | 8e25a61 | 2011-09-26 20:57:33 -0500 | [diff] [blame] | 90 | static int strict = 0; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 91 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 92 | #ifdef __TURBOC__ |
| 93 | #include <mem.h> |
| 94 | #endif |
| 95 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 96 | /* Defined so I can write to a file on gui/windowing platforms */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 97 | /* #define STDERR stderr */ |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 98 | #define STDERR stdout /* For DOS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 99 | |
Glenn Randers-Pehrson | a7dbcba | 2007-05-15 16:16:34 -0500 | [diff] [blame] | 100 | /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */ |
| 101 | #ifndef png_jmpbuf |
| 102 | # define png_jmpbuf(png_ptr) png_ptr->jmpbuf |
| 103 | #endif |
| 104 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 105 | /* Example of using row callbacks to make a simple progress meter */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 106 | static int status_pass = 1; |
| 107 | static int status_dots_requested = 0; |
| 108 | static int status_dots = 1; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 109 | |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame^] | 110 | static void PNGCBAPI |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 111 | read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 112 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 113 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX) |
| 114 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 115 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 116 | if (status_pass != pass) |
| 117 | { |
| 118 | fprintf(stdout, "\n Pass %d: ", pass); |
| 119 | status_pass = pass; |
| 120 | status_dots = 31; |
| 121 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 122 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 123 | status_dots--; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 124 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 125 | if (status_dots == 0) |
| 126 | { |
| 127 | fprintf(stdout, "\n "); |
| 128 | status_dots=30; |
| 129 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 130 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 131 | fprintf(stdout, "r"); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 132 | } |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 133 | |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame^] | 134 | static void PNGCBAPI |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 135 | write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 136 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 137 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) |
| 138 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 139 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 140 | fprintf(stdout, "w"); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 144 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 145 | /* Example of using user transform callback (we don't transform anything, |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 146 | * but merely examine the row filters. We set this to 256 rather than |
| 147 | * 5 in case illegal filter values are present.) |
| 148 | */ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 149 | static png_uint_32 filters_used[256]; |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame^] | 150 | static void PNGCBAPI |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 151 | count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
| 152 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 153 | if (png_ptr != NULL && row_info != NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 154 | ++filters_used[*(data - 1)]; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 155 | } |
| 156 | #endif |
| 157 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 158 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 159 | /* Example of using user transform callback (we don't transform anything, |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 160 | * but merely count the zero samples) |
| 161 | */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 162 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 163 | static png_uint_32 zero_samples; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 164 | |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame^] | 165 | static void PNGCBAPI |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 166 | count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 167 | { |
| 168 | png_bytep dp = data; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 169 | if (png_ptr == NULL) |
| 170 | return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 171 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 172 | /* Contents of row_info: |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 173 | * png_uint_32 width width of row |
| 174 | * png_uint_32 rowbytes number of bytes in row |
| 175 | * png_byte color_type color type of pixels |
| 176 | * png_byte bit_depth bit depth of samples |
| 177 | * png_byte channels number of channels (1-4) |
| 178 | * png_byte pixel_depth bits per pixel (depth*channels) |
| 179 | */ |
| 180 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 181 | /* Counts the number of zero samples (or zero pixels if color_type is 3 */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 182 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 183 | if (row_info->color_type == 0 || row_info->color_type == 3) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 184 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 185 | int pos = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 186 | png_uint_32 n, nstop; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 187 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 188 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 189 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 190 | if (row_info->bit_depth == 1) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 191 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 192 | if (((*dp << pos++ ) & 0x80) == 0) |
| 193 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 194 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 195 | if (pos == 8) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 196 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 197 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 198 | dp++; |
| 199 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 200 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 201 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 202 | if (row_info->bit_depth == 2) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 203 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 204 | if (((*dp << (pos+=2)) & 0xc0) == 0) |
| 205 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 206 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 207 | if (pos == 8) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 208 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 209 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 210 | dp++; |
| 211 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 212 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 213 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 214 | if (row_info->bit_depth == 4) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 215 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 216 | if (((*dp << (pos+=4)) & 0xf0) == 0) |
| 217 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 218 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 219 | if (pos == 8) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 220 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 221 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 222 | dp++; |
| 223 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 224 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 225 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 226 | if (row_info->bit_depth == 8) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 227 | if (*dp++ == 0) |
| 228 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 229 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 230 | if (row_info->bit_depth == 16) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 231 | { |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 232 | if ((*dp | *(dp+1)) == 0) |
| 233 | zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 234 | dp+=2; |
| 235 | } |
| 236 | } |
| 237 | } |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 238 | else /* Other color types */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 239 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 240 | png_uint_32 n, nstop; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 241 | int channel; |
| 242 | int color_channels = row_info->channels; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 243 | if (row_info->color_type > 3)color_channels--; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 244 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 245 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 246 | { |
| 247 | for (channel = 0; channel < color_channels; channel++) |
| 248 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 249 | if (row_info->bit_depth == 8) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 250 | if (*dp++ == 0) |
| 251 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 252 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 253 | if (row_info->bit_depth == 16) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 254 | { |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 255 | if ((*dp | *(dp+1)) == 0) |
| 256 | zero_samples++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 257 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 258 | dp+=2; |
| 259 | } |
| 260 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 261 | if (row_info->color_type > 3) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 262 | { |
| 263 | dp++; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 264 | if (row_info->bit_depth == 16) |
| 265 | dp++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
Glenn Randers-Pehrson | 38d73af | 1998-03-07 21:30:44 -0600 | [diff] [blame] | 270 | #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 271 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 272 | static int wrote_question = 0; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 273 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 274 | #ifndef PNG_STDIO_SUPPORTED |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 275 | /* START of code to validate stdio-free compilation */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 276 | /* These copies of the default read/write functions come from pngrio.c and |
| 277 | * pngwio.c. They allow "don't include stdio" testing of the library. |
| 278 | * This is the function that does the actual reading of data. If you are |
| 279 | * not reading from a standard C stream, you should create a replacement |
| 280 | * read_data function and use it at run time with png_set_read_fn(), rather |
| 281 | * than changing the library. |
| 282 | */ |
Glenn Randers-Pehrson | be9de0f | 2001-01-22 08:52:16 -0600 | [diff] [blame] | 283 | |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 284 | #ifdef PNG_IO_STATE_SUPPORTED |
| 285 | void |
| 286 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, |
| 287 | png_uint_32 io_op); |
| 288 | void |
| 289 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, |
| 290 | png_uint_32 io_op) |
| 291 | { |
| 292 | png_uint_32 io_state = png_get_io_state(png_ptr); |
| 293 | int err = 0; |
| 294 | |
| 295 | /* Check if the current operation (reading / writing) is as expected. */ |
| 296 | if ((io_state & PNG_IO_MASK_OP) != io_op) |
| 297 | png_error(png_ptr, "Incorrect operation in I/O state"); |
| 298 | |
| 299 | /* Check if the buffer size specific to the current location |
| 300 | * (file signature / header / data / crc) is as expected. |
| 301 | */ |
| 302 | switch (io_state & PNG_IO_MASK_LOC) |
| 303 | { |
| 304 | case PNG_IO_SIGNATURE: |
| 305 | if (data_length > 8) |
| 306 | err = 1; |
| 307 | break; |
| 308 | case PNG_IO_CHUNK_HDR: |
| 309 | if (data_length != 8) |
| 310 | err = 1; |
| 311 | break; |
| 312 | case PNG_IO_CHUNK_DATA: |
| 313 | break; /* no restrictions here */ |
| 314 | case PNG_IO_CHUNK_CRC: |
| 315 | if (data_length != 4) |
| 316 | err = 1; |
| 317 | break; |
| 318 | default: |
| 319 | err = 1; /* uninitialized */ |
| 320 | } |
| 321 | if (err) |
| 322 | png_error(png_ptr, "Bad I/O state or buffer size"); |
| 323 | } |
| 324 | #endif |
| 325 | |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 326 | static void PNGCBAPI |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 327 | pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 328 | { |
Glenn Randers-Pehrson | a0e0c6c | 2010-01-01 18:31:26 -0600 | [diff] [blame] | 329 | png_size_t check = 0; |
| 330 | png_voidp io_ptr; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 331 | |
| 332 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t |
| 333 | * instead of an int, which is what fread() actually returns. |
| 334 | */ |
Glenn Randers-Pehrson | a0e0c6c | 2010-01-01 18:31:26 -0600 | [diff] [blame] | 335 | io_ptr = png_get_io_ptr(png_ptr); |
| 336 | if (io_ptr != NULL) |
| 337 | { |
| 338 | check = fread(data, 1, length, (png_FILE_p)io_ptr); |
| 339 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 340 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 341 | if (check != length) |
| 342 | { |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 343 | png_error(png_ptr, "Read Error"); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 344 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 345 | |
| 346 | #ifdef PNG_IO_STATE_SUPPORTED |
| 347 | pngtest_check_io_state(png_ptr, length, PNG_IO_READING); |
| 348 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 349 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 350 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 351 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 352 | static void PNGCBAPI |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 353 | pngtest_flush(png_structp png_ptr) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 354 | { |
Glenn Randers-Pehrson | 8fb550c | 2009-03-21 08:15:32 -0500 | [diff] [blame] | 355 | /* Do nothing; fflush() is said to be just a waste of energy. */ |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 356 | PNG_UNUSED(png_ptr) /* Stifle compiler warning */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 357 | } |
| 358 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 359 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 360 | /* This is the function that does the actual writing of data. If you are |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 361 | * not writing to a standard C stream, you should create a replacement |
| 362 | * write_data function and use it at run time with png_set_write_fn(), rather |
| 363 | * than changing the library. |
| 364 | */ |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 365 | static void PNGCBAPI |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 366 | pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 367 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 368 | png_size_t check; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 369 | |
Glenn Randers-Pehrson | 526a6ad | 2010-03-11 05:42:20 -0600 | [diff] [blame] | 370 | check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 371 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 372 | if (check != length) |
| 373 | { |
| 374 | png_error(png_ptr, "Write Error"); |
| 375 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 376 | |
| 377 | #ifdef PNG_IO_STATE_SUPPORTED |
| 378 | pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING); |
| 379 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 380 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 381 | |
| 382 | /* This function is called when there is a warning, but the library thinks |
| 383 | * it can continue anyway. Replacement functions don't have to do anything |
| 384 | * here if you don't want to. In the default configuration, png_ptr is |
| 385 | * not used, but it is passed in case it may be useful. |
| 386 | */ |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 387 | static void PNGCBAPI |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 388 | pngtest_warning(png_structp png_ptr, png_const_charp message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 389 | { |
| 390 | PNG_CONST char *name = "UNKNOWN (ERROR!)"; |
Glenn Randers-Pehrson | a0e0c6c | 2010-01-01 18:31:26 -0600 | [diff] [blame] | 391 | char *test; |
| 392 | test = png_get_error_ptr(png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 393 | |
Glenn Randers-Pehrson | a0e0c6c | 2010-01-01 18:31:26 -0600 | [diff] [blame] | 394 | if (test == NULL) |
| 395 | fprintf(STDERR, "%s: libpng warning: %s\n", name, message); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 396 | |
Glenn Randers-Pehrson | a0e0c6c | 2010-01-01 18:31:26 -0600 | [diff] [blame] | 397 | else |
| 398 | fprintf(STDERR, "%s: libpng warning: %s\n", test, message); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* This is the default error handling function. Note that replacements for |
| 402 | * this function MUST NOT RETURN, or the program will likely crash. This |
| 403 | * function is used by default, or if the program supplies NULL for the |
| 404 | * error function pointer in png_set_error_fn(). |
| 405 | */ |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 406 | static void PNGCBAPI |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 407 | pngtest_error(png_structp png_ptr, png_const_charp message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 408 | { |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 409 | pngtest_warning(png_ptr, message); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 410 | /* We can return because png_error calls the default handler, which is |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 411 | * actually OK in this case. |
| 412 | */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 413 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 414 | #endif /* !PNG_STDIO_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 415 | /* END of code to validate stdio-free compilation */ |
| 416 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 417 | /* START of code to validate memory allocation and deallocation */ |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 418 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 419 | |
| 420 | /* Allocate memory. For reasonable files, size should never exceed |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 421 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 422 | * it not to. See zconf.h and png.h for more information. zlib does |
| 423 | * need to allocate exactly 64K, so whatever you call here must |
| 424 | * have the ability to do that. |
| 425 | * |
| 426 | * This piece of code can be compiled to validate max 64K allocations |
| 427 | * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. |
| 428 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 429 | typedef struct memory_information |
| 430 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 431 | png_alloc_size_t size; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 432 | png_voidp pointer; |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 433 | struct memory_information *next; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 434 | } memory_information; |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 435 | typedef memory_information *memory_infop; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 436 | |
| 437 | static memory_infop pinformation = NULL; |
| 438 | static int current_allocation = 0; |
| 439 | static int maximum_allocation = 0; |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 440 | static int total_allocation = 0; |
| 441 | static int num_allocations = 0; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 442 | |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 443 | png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr, |
| 444 | png_alloc_size_t size)); |
| 445 | void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr)); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 446 | |
| 447 | png_voidp |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 448 | PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 449 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 450 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 451 | /* png_malloc has already tested for NULL; png_create_struct calls |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 452 | * png_debug_malloc directly, with png_ptr == NULL which is OK |
| 453 | */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 454 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 455 | if (size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 456 | return (NULL); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 457 | |
| 458 | /* This calls the library allocator twice, once to get the requested |
| 459 | buffer and once to get a new free list entry. */ |
| 460 | { |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 461 | /* Disable malloc_fn and free_fn */ |
Glenn Randers-Pehrson | 36d7bc7 | 2004-08-10 06:55:02 -0500 | [diff] [blame] | 462 | memory_infop pinfo; |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 463 | png_set_mem_fn(png_ptr, NULL, NULL, NULL); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 464 | pinfo = (memory_infop)png_malloc(png_ptr, |
| 465 | png_sizeof(*pinfo)); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 466 | pinfo->size = size; |
| 467 | current_allocation += size; |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 468 | total_allocation += size; |
| 469 | num_allocations ++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 470 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 471 | if (current_allocation > maximum_allocation) |
| 472 | maximum_allocation = current_allocation; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 473 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 474 | pinfo->pointer = png_malloc(png_ptr, size); |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 475 | /* Restore malloc_fn and free_fn */ |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 476 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 477 | png_set_mem_fn(png_ptr, |
| 478 | NULL, png_debug_malloc, png_debug_free); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 479 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 480 | if (size != 0 && pinfo->pointer == NULL) |
| 481 | { |
| 482 | current_allocation -= size; |
| 483 | total_allocation -= size; |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 484 | png_error(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 485 | "out of memory in pngtest->png_debug_malloc"); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 486 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 487 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 488 | pinfo->next = pinformation; |
| 489 | pinformation = pinfo; |
| 490 | /* Make sure the caller isn't assuming zeroed memory. */ |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 491 | memset(pinfo->pointer, 0xdd, pinfo->size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 492 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 493 | if (verbose) |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 494 | printf("png_malloc %lu bytes at %p\n", (unsigned long)size, |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 495 | pinfo->pointer); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 496 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 497 | return (png_voidp)(pinfo->pointer); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | /* Free a pointer. It is removed from the list at the same time. */ |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 502 | void PNGCBAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 503 | png_debug_free(png_structp png_ptr, png_voidp ptr) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 504 | { |
| 505 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 506 | fprintf(STDERR, "NULL pointer to png_debug_free.\n"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 507 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 508 | if (ptr == 0) |
| 509 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 510 | #if 0 /* This happens all the time. */ |
| 511 | fprintf(STDERR, "WARNING: freeing NULL pointer\n"); |
| 512 | #endif |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | /* Unlink the element from the list. */ |
| 517 | { |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 518 | memory_infop *ppinfo = &pinformation; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 519 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 520 | for (;;) |
| 521 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 522 | memory_infop pinfo = *ppinfo; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 523 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 524 | if (pinfo->pointer == ptr) |
| 525 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 526 | *ppinfo = pinfo->next; |
| 527 | current_allocation -= pinfo->size; |
| 528 | if (current_allocation < 0) |
| 529 | fprintf(STDERR, "Duplicate free of memory\n"); |
| 530 | /* We must free the list element too, but first kill |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 531 | the memory that is to be freed. */ |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 532 | memset(ptr, 0x55, pinfo->size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 533 | png_free_default(png_ptr, pinfo); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 534 | pinfo = NULL; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 535 | break; |
| 536 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 537 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 538 | if (pinfo->next == NULL) |
| 539 | { |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 540 | fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 541 | break; |
| 542 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 543 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 544 | ppinfo = &pinfo->next; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* Finally free the data. */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 549 | if (verbose) |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 550 | printf("Freeing %p\n", ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 551 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 552 | png_free_default(png_ptr, ptr); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 553 | ptr = NULL; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 554 | } |
Glenn Randers-Pehrson | d029a75 | 2004-08-09 21:50:32 -0500 | [diff] [blame] | 555 | #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 556 | /* END of code to test memory allocation/deallocation */ |
| 557 | |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 558 | |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 559 | /* Demonstration of user chunk support of the sTER and vpAg chunks */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 560 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 561 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 562 | /* (sTER is a public chunk not yet known by libpng. vpAg is a private |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 563 | chunk used in ImageMagick to store "virtual page" size). */ |
| 564 | |
| 565 | static png_uint_32 user_chunk_data[4]; |
| 566 | |
Glenn Randers-Pehrson | e826d7e | 2006-07-03 00:21:58 -0500 | [diff] [blame] | 567 | /* 0: sTER mode + 1 |
| 568 | * 1: vpAg width |
| 569 | * 2: vpAg height |
| 570 | * 3: vpAg units |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 571 | */ |
| 572 | |
Glenn Randers-Pehrson | eae8e36 | 2010-03-12 17:36:53 -0600 | [diff] [blame] | 573 | static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr, |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 574 | png_unknown_chunkp chunk) |
| 575 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 576 | png_uint_32 |
| 577 | *my_user_chunk_data; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 578 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 579 | /* Return one of the following: |
| 580 | * return (-n); chunk had an error |
| 581 | * return (0); did not recognize |
| 582 | * return (n); success |
| 583 | * |
| 584 | * The unknown chunk structure contains the chunk data: |
| 585 | * png_byte name[5]; |
| 586 | * png_byte *data; |
| 587 | * png_size_t size; |
| 588 | * |
| 589 | * Note that libpng has already taken care of the CRC handling. |
| 590 | */ |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 591 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 592 | if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */ |
| 593 | chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */ |
| 594 | { |
| 595 | /* Found sTER chunk */ |
| 596 | if (chunk->size != 1) |
| 597 | return (-1); /* Error return */ |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 598 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 599 | if (chunk->data[0] != 0 && chunk->data[0] != 1) |
| 600 | return (-1); /* Invalid mode */ |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 601 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 602 | my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr); |
| 603 | my_user_chunk_data[0]=chunk->data[0]+1; |
| 604 | return (1); |
| 605 | } |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 606 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 607 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */ |
| 608 | chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */ |
| 609 | return (0); /* Did not recognize */ |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 610 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 611 | /* Found ImageMagick vpAg chunk */ |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 612 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 613 | if (chunk->size != 9) |
| 614 | return (-1); /* Error return */ |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 615 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 616 | my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 617 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 618 | my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data); |
| 619 | my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4); |
| 620 | my_user_chunk_data[3]=(png_uint_32)chunk->data[8]; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 621 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 622 | return (1); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 623 | |
| 624 | } |
| 625 | #endif |
| 626 | /* END of code to demonstrate user chunk support */ |
| 627 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 628 | /* Test one file */ |
John Bowler | 8fb6c6a | 2012-01-25 07:47:44 -0600 | [diff] [blame^] | 629 | static int |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 630 | test_one_file(PNG_CONST char *inname, PNG_CONST char *outname) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 631 | { |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 632 | static png_FILE_p fpin; |
| 633 | static png_FILE_p fpout; /* "static" prevents setjmp corruption */ |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 634 | png_structp read_ptr; |
| 635 | png_infop read_info_ptr, end_info_ptr; |
| 636 | #ifdef PNG_WRITE_SUPPORTED |
| 637 | png_structp write_ptr; |
| 638 | png_infop write_info_ptr; |
| 639 | png_infop write_end_info_ptr; |
| 640 | #else |
| 641 | png_structp write_ptr = NULL; |
| 642 | png_infop write_info_ptr = NULL; |
| 643 | png_infop write_end_info_ptr = NULL; |
| 644 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 645 | png_bytep row_buf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 646 | png_uint_32 y; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 647 | png_uint_32 width, height; |
| 648 | int num_pass, pass; |
| 649 | int bit_depth, color_type; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 650 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 651 | char inbuf[256], outbuf[256]; |
| 652 | |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 653 | row_buf = NULL; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 654 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 655 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 656 | { |
| 657 | fprintf(STDERR, "Could not find input file %s\n", inname); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 658 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 659 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 660 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 661 | if ((fpout = fopen(outname, "wb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 662 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 663 | fprintf(STDERR, "Could not open output file %s\n", outname); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 664 | FCLOSE(fpin); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 665 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 666 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 667 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 668 | pngtest_debug("Allocating read and write structures"); |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 669 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 670 | read_ptr = |
| 671 | png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
Glenn Randers-Pehrson | d7da8bb | 2010-03-13 20:30:10 -0600 | [diff] [blame] | 672 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 673 | #else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 674 | read_ptr = |
| 675 | png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 676 | #endif |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 677 | #ifndef PNG_STDIO_SUPPORTED |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 678 | png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error, |
| 679 | pngtest_warning); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 680 | #endif |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 681 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 682 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 683 | user_chunk_data[0] = 0; |
| 684 | user_chunk_data[1] = 0; |
| 685 | user_chunk_data[2] = 0; |
| 686 | user_chunk_data[3] = 0; |
| 687 | png_set_read_user_chunk_fn(read_ptr, user_chunk_data, |
| 688 | read_user_chunk_callback); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 689 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 690 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 691 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 692 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 693 | write_ptr = |
| 694 | png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
| 695 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 696 | #else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 697 | write_ptr = |
| 698 | png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 699 | #endif |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 700 | #ifndef PNG_STDIO_SUPPORTED |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 701 | png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error, |
| 702 | pngtest_warning); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 703 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 704 | #endif |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 705 | pngtest_debug("Allocating read_info, write_info and end_info structures"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 706 | read_info_ptr = png_create_info_struct(read_ptr); |
Glenn Randers-Pehrson | 1fd5fb3 | 2001-05-06 05:34:26 -0500 | [diff] [blame] | 707 | end_info_ptr = png_create_info_struct(read_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 708 | #ifdef PNG_WRITE_SUPPORTED |
| 709 | write_info_ptr = png_create_info_struct(write_ptr); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 710 | write_end_info_ptr = png_create_info_struct(write_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 711 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 712 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 713 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 714 | pngtest_debug("Setting jmpbuf for read struct"); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 715 | if (setjmp(png_jmpbuf(read_ptr))) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 716 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 717 | fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 718 | png_free(read_ptr, row_buf); |
| 719 | row_buf = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 720 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 721 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 722 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 723 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 724 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 725 | FCLOSE(fpin); |
| 726 | FCLOSE(fpout); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 727 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 728 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 729 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 730 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 731 | pngtest_debug("Setting jmpbuf for write struct"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 732 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 733 | if (setjmp(png_jmpbuf(write_ptr))) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 734 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 735 | fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 736 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 737 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 738 | #ifdef PNG_WRITE_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 739 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 740 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 741 | FCLOSE(fpin); |
| 742 | FCLOSE(fpout); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 743 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 744 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 745 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 746 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 747 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 748 | pngtest_debug("Initializing input and output streams"); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 749 | #ifdef PNG_STDIO_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 750 | png_init_io(read_ptr, fpin); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 751 | # ifdef PNG_WRITE_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 752 | png_init_io(write_ptr, fpout); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 753 | # endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 754 | #else |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 755 | png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 756 | # ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 757 | png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data, |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 758 | # ifdef PNG_WRITE_FLUSH_SUPPORTED |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 759 | pngtest_flush); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 760 | # else |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 761 | NULL); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 762 | # endif |
| 763 | # endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 764 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 765 | |
John Bowler | b2bee33 | 2011-06-10 23:24:58 -0500 | [diff] [blame] | 766 | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED |
Glenn Randers-Pehrson | 65a24d0 | 2011-04-01 20:41:53 -0500 | [diff] [blame] | 767 | /* Normally one would use Z_DEFAULT_STRATEGY for text compression. |
| 768 | * This is here just to make pngtest replicate the results from libpng |
Glenn Randers-Pehrson | ef217b7 | 2011-06-15 12:58:27 -0500 | [diff] [blame] | 769 | * versions prior to 1.5.4, and to test this new API. |
Glenn Randers-Pehrson | 65a24d0 | 2011-04-01 20:41:53 -0500 | [diff] [blame] | 770 | */ |
Glenn Randers-Pehrson | 6bdea98 | 2011-03-31 21:14:55 -0500 | [diff] [blame] | 771 | png_set_text_compression_strategy(write_ptr, Z_FILTERED); |
| 772 | #endif |
| 773 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 774 | if (status_dots_requested == 1) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 775 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 776 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 777 | png_set_write_status_fn(write_ptr, write_row_callback); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 778 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 779 | png_set_read_status_fn(read_ptr, read_row_callback); |
| 780 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 781 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 782 | else |
| 783 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 784 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 785 | png_set_write_status_fn(write_ptr, NULL); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 786 | #endif |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 787 | png_set_read_status_fn(read_ptr, NULL); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 788 | } |
| 789 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 790 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 791 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 792 | int i; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 793 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 794 | for (i = 0; i<256; i++) |
| 795 | filters_used[i] = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 796 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 797 | png_set_read_user_transform_fn(read_ptr, count_filters); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 798 | } |
| 799 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 800 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 801 | zero_samples = 0; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 802 | png_set_write_user_transform_fn(write_ptr, count_zero_samples); |
| 803 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 804 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 805 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 806 | # ifndef PNG_HANDLE_CHUNK_ALWAYS |
| 807 | # define PNG_HANDLE_CHUNK_ALWAYS 3 |
| 808 | # endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 809 | png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS, |
| 810 | NULL, 0); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 811 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 812 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 813 | # ifndef PNG_HANDLE_CHUNK_IF_SAFE |
| 814 | # define PNG_HANDLE_CHUNK_IF_SAFE 2 |
| 815 | # endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 816 | png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE, |
| 817 | NULL, 0); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 818 | #endif |
| 819 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 820 | pngtest_debug("Reading info struct"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 821 | png_read_info(read_ptr, read_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 822 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 823 | pngtest_debug("Transferring info struct"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 824 | { |
| 825 | int interlace_type, compression_type, filter_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 826 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 827 | if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth, |
| 828 | &color_type, &interlace_type, &compression_type, &filter_type)) |
| 829 | { |
| 830 | png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth, |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 831 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 832 | color_type, interlace_type, compression_type, filter_type); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 833 | #else |
| 834 | color_type, PNG_INTERLACE_NONE, compression_type, filter_type); |
| 835 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 836 | } |
| 837 | } |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 838 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 839 | #ifdef PNG_cHRM_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 840 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 841 | png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 842 | blue_y; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 843 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 844 | if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, |
| 845 | &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 846 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 847 | png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 848 | red_y, green_x, green_y, blue_x, blue_y); |
| 849 | } |
| 850 | } |
| 851 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 852 | #ifdef PNG_gAMA_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 853 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 854 | png_fixed_point gamma; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 855 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 856 | if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 857 | png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 858 | } |
| 859 | #endif |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 860 | #else /* Use floating point versions */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 861 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 862 | #ifdef PNG_cHRM_SUPPORTED |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 863 | { |
| 864 | double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, |
| 865 | blue_y; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 866 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 867 | if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x, |
| 868 | &red_y, &green_x, &green_y, &blue_x, &blue_y)) |
| 869 | { |
| 870 | png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x, |
| 871 | red_y, green_x, green_y, blue_x, blue_y); |
| 872 | } |
| 873 | } |
| 874 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 875 | #ifdef PNG_gAMA_SUPPORTED |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 876 | { |
| 877 | double gamma; |
| 878 | |
| 879 | if (png_get_gAMA(read_ptr, read_info_ptr, &gamma)) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 880 | png_set_gAMA(write_ptr, write_info_ptr, gamma); |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 881 | } |
| 882 | #endif |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 883 | #endif /* Floating point */ |
| 884 | #endif /* Fixed point */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 885 | #ifdef PNG_iCCP_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 886 | { |
| 887 | png_charp name; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 888 | png_bytep profile; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 889 | png_uint_32 proflen; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 890 | int compression_type; |
| 891 | |
| 892 | if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type, |
| 893 | &profile, &proflen)) |
| 894 | { |
| 895 | png_set_iCCP(write_ptr, write_info_ptr, name, compression_type, |
| 896 | profile, proflen); |
| 897 | } |
| 898 | } |
| 899 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 900 | #ifdef PNG_sRGB_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 901 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 902 | int intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 903 | |
| 904 | if (png_get_sRGB(read_ptr, read_info_ptr, &intent)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 905 | png_set_sRGB(write_ptr, write_info_ptr, intent); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 906 | } |
| 907 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 908 | { |
| 909 | png_colorp palette; |
| 910 | int num_palette; |
| 911 | |
| 912 | if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 913 | png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 914 | } |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 915 | #ifdef PNG_bKGD_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 916 | { |
| 917 | png_color_16p background; |
| 918 | |
| 919 | if (png_get_bKGD(read_ptr, read_info_ptr, &background)) |
| 920 | { |
| 921 | png_set_bKGD(write_ptr, write_info_ptr, background); |
| 922 | } |
| 923 | } |
| 924 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 925 | #ifdef PNG_hIST_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 926 | { |
| 927 | png_uint_16p hist; |
| 928 | |
| 929 | if (png_get_hIST(read_ptr, read_info_ptr, &hist)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 930 | png_set_hIST(write_ptr, write_info_ptr, hist); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 931 | } |
| 932 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 933 | #ifdef PNG_oFFs_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 934 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 935 | png_int_32 offset_x, offset_y; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 936 | int unit_type; |
| 937 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 938 | if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y, |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 939 | &unit_type)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 940 | { |
| 941 | png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type); |
| 942 | } |
| 943 | } |
| 944 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 945 | #ifdef PNG_pCAL_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 946 | { |
| 947 | png_charp purpose, units; |
| 948 | png_charpp params; |
| 949 | png_int_32 X0, X1; |
| 950 | int type, nparams; |
| 951 | |
| 952 | if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type, |
| 953 | &nparams, &units, ¶ms)) |
| 954 | { |
| 955 | png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type, |
| 956 | nparams, units, params); |
| 957 | } |
| 958 | } |
| 959 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 960 | #ifdef PNG_pHYs_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 961 | { |
| 962 | png_uint_32 res_x, res_y; |
| 963 | int unit_type; |
| 964 | |
| 965 | if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 966 | png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 967 | } |
| 968 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 969 | #ifdef PNG_sBIT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 970 | { |
| 971 | png_color_8p sig_bit; |
| 972 | |
| 973 | if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 974 | png_set_sBIT(write_ptr, write_info_ptr, sig_bit); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 975 | } |
| 976 | #endif |
Glenn Randers-Pehrson | 9c1bb20 | 2009-09-17 10:55:49 -0500 | [diff] [blame] | 977 | #ifdef PNG_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 978 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 979 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 980 | int unit; |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame] | 981 | double scal_width, scal_height; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 982 | |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame] | 983 | if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width, |
| 984 | &scal_height)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 985 | { |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame] | 986 | png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | #else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 990 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 991 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 992 | int unit; |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame] | 993 | png_charp scal_width, scal_height; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 994 | |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame] | 995 | if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width, |
| 996 | &scal_height)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 997 | { |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 998 | png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, |
| 999 | scal_height); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1003 | #endif |
| 1004 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1005 | #ifdef PNG_TEXT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1006 | { |
| 1007 | png_textp text_ptr; |
| 1008 | int num_text; |
| 1009 | |
| 1010 | if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0) |
| 1011 | { |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1012 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
Glenn Randers-Pehrson | 15ea1fa | 2011-11-23 15:28:01 -0600 | [diff] [blame] | 1013 | |
| 1014 | if (verbose) |
| 1015 | printf("\nText compression=%d\n", text_ptr->compression); |
| 1016 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1017 | png_set_text(write_ptr, write_info_ptr, text_ptr, num_text); |
| 1018 | } |
| 1019 | } |
| 1020 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1021 | #ifdef PNG_tIME_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1022 | { |
| 1023 | png_timep mod_time; |
| 1024 | |
| 1025 | if (png_get_tIME(read_ptr, read_info_ptr, &mod_time)) |
| 1026 | { |
| 1027 | png_set_tIME(write_ptr, write_info_ptr, mod_time); |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1028 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 1029 | if (png_convert_to_rfc1123_buffer(tIME_string, mod_time)) |
| 1030 | tIME_string[png_sizeof(tIME_string) - 1] = '\0'; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1031 | |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 1032 | else |
| 1033 | strcpy(tIME_string, "*** invalid time ***"); |
| 1034 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1035 | tIME_chunk_present++; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1036 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 1037 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1038 | } |
| 1039 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1040 | #ifdef PNG_tRNS_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1041 | { |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 1042 | png_bytep trans_alpha; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1043 | int num_trans; |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1044 | png_color_16p trans_color; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1045 | |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 1046 | if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans, |
Glenn Randers-Pehrson | 56f6396 | 2008-10-06 10:16:17 -0500 | [diff] [blame] | 1047 | &trans_color)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1048 | { |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 1049 | int sample_max = (1 << bit_depth); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1050 | /* libpng doesn't reject a tRNS chunk with out-of-range samples */ |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 1051 | if (!((color_type == PNG_COLOR_TYPE_GRAY && |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1052 | (int)trans_color->gray > sample_max) || |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 1053 | (color_type == PNG_COLOR_TYPE_RGB && |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1054 | ((int)trans_color->red > sample_max || |
| 1055 | (int)trans_color->green > sample_max || |
| 1056 | (int)trans_color->blue > sample_max)))) |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 1057 | png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans, |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1058 | trans_color); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1062 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1063 | { |
| 1064 | png_unknown_chunkp unknowns; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1065 | int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1066 | &unknowns); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1067 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1068 | if (num_unknowns) |
| 1069 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1070 | int i; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1071 | png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns, |
| 1072 | num_unknowns); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1073 | /* Copy the locations from the read_info_ptr. The automatically |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1074 | * generated locations in write_info_ptr are wrong because we |
| 1075 | * haven't written anything yet. |
| 1076 | */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1077 | for (i = 0; i < num_unknowns; i++) |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 1078 | png_set_unknown_chunk_location(write_ptr, write_info_ptr, i, |
| 1079 | unknowns[i].location); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1083 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1084 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1085 | pngtest_debug("Writing info struct"); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1086 | |
| 1087 | /* If we wanted, we could write info in two steps: |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1088 | * png_write_info_before_PLTE(write_ptr, write_info_ptr); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1089 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1090 | png_write_info(write_ptr, write_info_ptr); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1091 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1092 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1093 | if (user_chunk_data[0] != 0) |
| 1094 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1095 | png_byte png_sTER[5] = {115, 84, 69, 82, '\0'}; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1096 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1097 | unsigned char |
| 1098 | ster_chunk_data[1]; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1099 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1100 | if (verbose) |
| 1101 | fprintf(STDERR, "\n stereo mode = %lu\n", |
| 1102 | (unsigned long)(user_chunk_data[0] - 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1103 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1104 | ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1); |
| 1105 | png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1106 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1107 | |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1108 | if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0) |
| 1109 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1110 | png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'}; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1111 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1112 | unsigned char |
| 1113 | vpag_chunk_data[9]; |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1114 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1115 | if (verbose) |
| 1116 | fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n", |
| 1117 | (unsigned long)user_chunk_data[1], |
| 1118 | (unsigned long)user_chunk_data[2], |
| 1119 | (unsigned long)user_chunk_data[3]); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1120 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1121 | png_save_uint_32(vpag_chunk_data, user_chunk_data[1]); |
| 1122 | png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]); |
| 1123 | vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff); |
| 1124 | png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9); |
Glenn Randers-Pehrson | d120996 | 2006-06-21 19:40:52 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1128 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1129 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1130 | #ifdef SINGLE_ROWBUF_ALLOC |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1131 | pngtest_debug("Allocating row buffer..."); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1132 | row_buf = (png_bytep)png_malloc(read_ptr, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1133 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1134 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1135 | pngtest_debug1("\t0x%08lx", (unsigned long)row_buf); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1136 | #endif /* SINGLE_ROWBUF_ALLOC */ |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1137 | pngtest_debug("Writing row data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1138 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1139 | #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ |
| 1140 | defined(PNG_WRITE_INTERLACING_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1141 | num_pass = png_set_interlace_handling(read_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1142 | # ifdef PNG_WRITE_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1143 | png_set_interlace_handling(write_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1144 | # endif |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1145 | #else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1146 | num_pass = 1; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1147 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1148 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1149 | #ifdef PNGTEST_TIMING |
| 1150 | t_stop = (float)clock(); |
| 1151 | t_misc += (t_stop - t_start); |
| 1152 | t_start = t_stop; |
| 1153 | #endif |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1154 | for (pass = 0; pass < num_pass; pass++) |
| 1155 | { |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1156 | pngtest_debug1("Writing row data for pass %d", pass); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1157 | for (y = 0; y < height; y++) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1158 | { |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1159 | #ifndef SINGLE_ROWBUF_ALLOC |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1160 | pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1161 | row_buf = (png_bytep)png_malloc(read_ptr, |
| 1162 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1163 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1164 | pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf, |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1165 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1166 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1167 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1168 | png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1169 | |
| 1170 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1171 | #ifdef PNGTEST_TIMING |
| 1172 | t_stop = (float)clock(); |
| 1173 | t_decode += (t_stop - t_start); |
| 1174 | t_start = t_stop; |
| 1175 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1176 | png_write_rows(write_ptr, (png_bytepp)&row_buf, 1); |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1177 | #ifdef PNGTEST_TIMING |
| 1178 | t_stop = (float)clock(); |
| 1179 | t_encode += (t_stop - t_start); |
| 1180 | t_start = t_stop; |
| 1181 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1182 | #endif /* PNG_WRITE_SUPPORTED */ |
| 1183 | |
| 1184 | #ifndef SINGLE_ROWBUF_ALLOC |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1185 | pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1186 | png_free(read_ptr, row_buf); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 1187 | row_buf = NULL; |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1188 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1189 | } |
| 1190 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1191 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1192 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1193 | png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1194 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1195 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1196 | png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1197 | #endif |
| 1198 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1199 | pngtest_debug("Reading and writing end_info data"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1200 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1201 | png_read_end(read_ptr, end_info_ptr); |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1202 | #ifdef PNG_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1203 | { |
| 1204 | png_textp text_ptr; |
| 1205 | int num_text; |
| 1206 | |
| 1207 | if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0) |
| 1208 | { |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1209 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1210 | png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text); |
| 1211 | } |
| 1212 | } |
| 1213 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1214 | #ifdef PNG_tIME_SUPPORTED |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1215 | { |
| 1216 | png_timep mod_time; |
| 1217 | |
| 1218 | if (png_get_tIME(read_ptr, end_info_ptr, &mod_time)) |
| 1219 | { |
| 1220 | png_set_tIME(write_ptr, write_end_info_ptr, mod_time); |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1221 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 1222 | if (png_convert_to_rfc1123_buffer(tIME_string, mod_time)) |
| 1223 | tIME_string[png_sizeof(tIME_string) - 1] = '\0'; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1224 | |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 1225 | else |
| 1226 | strcpy(tIME_string, "*** invalid time ***"); |
| 1227 | |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1228 | tIME_chunk_present++; |
| 1229 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1230 | } |
| 1231 | } |
| 1232 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1233 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1234 | { |
| 1235 | png_unknown_chunkp unknowns; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1236 | int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1237 | &unknowns); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1238 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1239 | if (num_unknowns) |
| 1240 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1241 | int i; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1242 | png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns, |
| 1243 | num_unknowns); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1244 | /* Copy the locations from the read_info_ptr. The automatically |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1245 | * generated locations in write_end_info_ptr are wrong because we |
| 1246 | * haven't written the end_info yet. |
| 1247 | */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1248 | for (i = 0; i < num_unknowns; i++) |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 1249 | png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i, |
| 1250 | unknowns[i].location); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | #endif |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1254 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1255 | png_write_end(write_ptr, write_end_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1256 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1257 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1258 | #ifdef PNG_EASY_ACCESS_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1259 | if (verbose) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1260 | { |
| 1261 | png_uint_32 iwidth, iheight; |
| 1262 | iwidth = png_get_image_width(write_ptr, write_info_ptr); |
| 1263 | iheight = png_get_image_height(write_ptr, write_info_ptr); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1264 | fprintf(STDERR, "\n Image width = %lu, height = %lu\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1265 | (unsigned long)iwidth, (unsigned long)iheight); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1266 | } |
| 1267 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1268 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1269 | pngtest_debug("Destroying data structs"); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1270 | #ifdef SINGLE_ROWBUF_ALLOC |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1271 | pngtest_debug("destroying row_buf for read_ptr"); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1272 | png_free(read_ptr, row_buf); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 1273 | row_buf = NULL; |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1274 | #endif /* SINGLE_ROWBUF_ALLOC */ |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1275 | pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1276 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1277 | #ifdef PNG_WRITE_SUPPORTED |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1278 | pngtest_debug("destroying write_end_info_ptr"); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1279 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1280 | pngtest_debug("destroying write_ptr, write_info_ptr"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1281 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1282 | #endif |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1283 | pngtest_debug("Destruction complete."); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1284 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1285 | FCLOSE(fpin); |
| 1286 | FCLOSE(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1287 | |
Glenn Randers-Pehrson | 31f92b0 | 2010-03-09 16:47:59 -0600 | [diff] [blame] | 1288 | pngtest_debug("Opening files for comparison"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1289 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1290 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1291 | fprintf(STDERR, "Could not find file %s\n", inname); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 1292 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1293 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1294 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1295 | if ((fpout = fopen(outname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1296 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1297 | fprintf(STDERR, "Could not find file %s\n", outname); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1298 | FCLOSE(fpin); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 1299 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1300 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1301 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1302 | for (;;) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1303 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1304 | png_size_t num_in, num_out; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1305 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1306 | num_in = fread(inbuf, 1, 1, fpin); |
| 1307 | num_out = fread(outbuf, 1, 1, fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1308 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1309 | if (num_in != num_out) |
| 1310 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1311 | fprintf(STDERR, "\nFiles %s and %s are of a different size\n", |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1312 | inname, outname); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1313 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1314 | if (wrote_question == 0) |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1315 | { |
| 1316 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1317 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1318 | inname, PNG_ZBUF_SIZE); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1319 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1320 | "\n filtering heuristic (libpng default), compression"); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1321 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1322 | " level (zlib default),\n and zlib version (%s)?\n\n", |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1323 | ZLIB_VERSION); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1324 | wrote_question = 1; |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1325 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1326 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1327 | FCLOSE(fpin); |
| 1328 | FCLOSE(fpout); |
Glenn Randers-Pehrson | 8e25a61 | 2011-09-26 20:57:33 -0500 | [diff] [blame] | 1329 | |
| 1330 | if (strict != 0) |
| 1331 | return (1); |
| 1332 | |
| 1333 | else |
| 1334 | return (0); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1335 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1336 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1337 | if (!num_in) |
| 1338 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1339 | |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 1340 | if (memcmp(inbuf, outbuf, num_in)) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1341 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1342 | fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1343 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1344 | if (wrote_question == 0) |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1345 | { |
| 1346 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1347 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1348 | inname, PNG_ZBUF_SIZE); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1349 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1350 | "\n filtering heuristic (libpng default), compression"); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1351 | fprintf(STDERR, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1352 | " level (zlib default),\n and zlib version (%s)?\n\n", |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1353 | ZLIB_VERSION); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1354 | wrote_question = 1; |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1355 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1356 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1357 | FCLOSE(fpin); |
| 1358 | FCLOSE(fpout); |
Glenn Randers-Pehrson | 8e25a61 | 2011-09-26 20:57:33 -0500 | [diff] [blame] | 1359 | |
| 1360 | if (strict != 0) |
| 1361 | return (1); |
| 1362 | |
| 1363 | else |
| 1364 | return (0); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1368 | FCLOSE(fpin); |
| 1369 | FCLOSE(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1370 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 1371 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1372 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1373 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1374 | /* Input and output filenames */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1375 | #ifdef RISCOS |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1376 | static PNG_CONST char *inname = "pngtest/png"; |
| 1377 | static PNG_CONST char *outname = "pngout/png"; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1378 | #else |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1379 | static PNG_CONST char *inname = "pngtest.png"; |
| 1380 | static PNG_CONST char *outname = "pngout.png"; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1381 | #endif |
| 1382 | |
| 1383 | int |
| 1384 | main(int argc, char *argv[]) |
| 1385 | { |
| 1386 | int multiple = 0; |
| 1387 | int ierror = 0; |
| 1388 | |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1389 | fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1390 | fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1391 | fprintf(STDERR, "%s", png_get_copyright(NULL)); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1392 | /* Show the version of libpng used in building the library */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1393 | fprintf(STDERR, " library (%lu):%s", |
| 1394 | (unsigned long)png_access_version_number(), |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1395 | png_get_header_version(NULL)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1396 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1397 | /* Show the version of libpng used in building the application */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1398 | fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER, |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1399 | PNG_HEADER_VERSION_STRING); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1400 | |
| 1401 | /* Do some consistency checking on the memory allocation settings, I'm |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1402 | * not sure this matters, but it is nice to know, the first of these |
| 1403 | * tests should be impossible because of the way the macros are set |
| 1404 | * in pngconf.h |
| 1405 | */ |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 1406 | #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1407 | fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n"); |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 1408 | #endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1409 | /* I think the following can happen. */ |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 1410 | #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1411 | fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n"); |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 1412 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1413 | |
| 1414 | if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING)) |
| 1415 | { |
| 1416 | fprintf(STDERR, |
| 1417 | "Warning: versions are different between png.h and png.c\n"); |
| 1418 | fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING); |
| 1419 | fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver); |
| 1420 | ++ierror; |
| 1421 | } |
| 1422 | |
| 1423 | if (argc > 1) |
| 1424 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1425 | if (strcmp(argv[1], "-m") == 0) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1426 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1427 | multiple = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1428 | status_dots_requested = 0; |
| 1429 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1430 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1431 | else if (strcmp(argv[1], "-mv") == 0 || |
| 1432 | strcmp(argv[1], "-vm") == 0 ) |
| 1433 | { |
| 1434 | multiple = 1; |
| 1435 | verbose = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1436 | status_dots_requested = 1; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1437 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1438 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1439 | else if (strcmp(argv[1], "-v") == 0) |
| 1440 | { |
| 1441 | verbose = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1442 | status_dots_requested = 1; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1443 | inname = argv[2]; |
| 1444 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1445 | |
Glenn Randers-Pehrson | 8e25a61 | 2011-09-26 20:57:33 -0500 | [diff] [blame] | 1446 | else if (strcmp(argv[1], "--strict") == 0) |
| 1447 | { |
| 1448 | status_dots_requested = 0; |
| 1449 | verbose = 1; |
| 1450 | inname = argv[2]; |
| 1451 | strict++; |
| 1452 | } |
| 1453 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1454 | else |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1455 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1456 | inname = argv[1]; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1457 | status_dots_requested = 0; |
| 1458 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1459 | } |
| 1460 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1461 | if (!multiple && argc == 3 + verbose) |
| 1462 | outname = argv[2 + verbose]; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1463 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1464 | if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2)) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1465 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1466 | fprintf(STDERR, |
| 1467 | "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n", |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1468 | argv[0], argv[0]); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1469 | fprintf(STDERR, |
| 1470 | " reads/writes one PNG file (without -m) or multiple files (-m)\n"); |
| 1471 | fprintf(STDERR, |
| 1472 | " with -m %s is used as a temporary file\n", outname); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1473 | exit(1); |
| 1474 | } |
| 1475 | |
| 1476 | if (multiple) |
| 1477 | { |
| 1478 | int i; |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1479 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1480 | int allocation_now = current_allocation; |
| 1481 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1482 | for (i=2; i<argc; ++i) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1483 | { |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1484 | int kerror; |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1485 | fprintf(STDERR, "\n Testing %s:", argv[i]); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1486 | kerror = test_one_file(argv[i], outname); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1487 | if (kerror == 0) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1488 | { |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 1489 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
| 1490 | int k; |
| 1491 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1492 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1493 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1494 | (unsigned long)zero_samples); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1495 | #else |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1496 | fprintf(STDERR, " PASS\n"); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1497 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1498 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1499 | for (k = 0; k<256; k++) |
| 1500 | if (filters_used[k]) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1501 | fprintf(STDERR, " Filter %d was used %lu times\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1502 | k, (unsigned long)filters_used[k]); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1503 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1504 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1505 | if (tIME_chunk_present != 0) |
| 1506 | fprintf(STDERR, " tIME = %s\n", tIME_string); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1507 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1508 | tIME_chunk_present = 0; |
| 1509 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1510 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1511 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1512 | else |
| 1513 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1514 | fprintf(STDERR, " FAIL\n"); |
| 1515 | ierror += kerror; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1516 | } |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1517 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1518 | if (allocation_now != current_allocation) |
| 1519 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1520 | current_allocation - allocation_now); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1521 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1522 | if (current_allocation != 0) |
| 1523 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1524 | memory_infop pinfo = pinformation; |
| 1525 | |
| 1526 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1527 | current_allocation); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1528 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1529 | while (pinfo != NULL) |
| 1530 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1531 | fprintf(STDERR, " %lu bytes at %x\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1532 | (unsigned long)pinfo->size, |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 1533 | (unsigned int)pinfo->pointer); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1534 | pinfo = pinfo->next; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1535 | } |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 1536 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1537 | #endif |
| 1538 | } |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1539 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1540 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1541 | current_allocation); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1542 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1543 | maximum_allocation); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1544 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", |
| 1545 | total_allocation); |
| 1546 | fprintf(STDERR, " Number of allocations: %10d\n", |
| 1547 | num_allocations); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1548 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1549 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1550 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1551 | else |
| 1552 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1553 | int i; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1554 | for (i = 0; i<3; ++i) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1555 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1556 | int kerror; |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1557 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1558 | int allocation_now = current_allocation; |
| 1559 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1560 | if (i == 1) |
| 1561 | status_dots_requested = 1; |
| 1562 | |
| 1563 | else if (verbose == 0) |
| 1564 | status_dots_requested = 0; |
| 1565 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1566 | if (i == 0 || verbose == 1 || ierror != 0) |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1567 | fprintf(STDERR, "\n Testing %s:", inname); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1568 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1569 | kerror = test_one_file(inname, outname); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1570 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1571 | if (kerror == 0) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1572 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1573 | if (verbose == 1 || i == 2) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1574 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1575 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1576 | int k; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1577 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1578 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1579 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1580 | (unsigned long)zero_samples); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1581 | #else |
| 1582 | fprintf(STDERR, " PASS\n"); |
| 1583 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1584 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1585 | for (k = 0; k<256; k++) |
| 1586 | if (filters_used[k]) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1587 | fprintf(STDERR, " Filter %d was used %lu times\n", |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 1588 | k, (unsigned long)filters_used[k]); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1589 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1590 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1591 | if (tIME_chunk_present != 0) |
| 1592 | fprintf(STDERR, " tIME = %s\n", tIME_string); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1593 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1594 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1595 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1596 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1597 | else |
| 1598 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1599 | if (verbose == 0 && i != 2) |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1600 | fprintf(STDERR, "\n Testing %s:", inname); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1601 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1602 | fprintf(STDERR, " FAIL\n"); |
| 1603 | ierror += kerror; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1604 | } |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1605 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1606 | if (allocation_now != current_allocation) |
| 1607 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1608 | current_allocation - allocation_now); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1609 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1610 | if (current_allocation != 0) |
| 1611 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1612 | memory_infop pinfo = pinformation; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1613 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1614 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1615 | current_allocation); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1616 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1617 | while (pinfo != NULL) |
| 1618 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1619 | fprintf(STDERR, " %lu bytes at %x\n", |
| 1620 | (unsigned long)pinfo->size, (unsigned int)pinfo->pointer); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1621 | pinfo = pinfo->next; |
| 1622 | } |
| 1623 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1624 | #endif |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1625 | } |
Glenn Randers-Pehrson | 37f116a | 2004-08-15 07:15:39 -0500 | [diff] [blame] | 1626 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1627 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1628 | current_allocation); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1629 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1630 | maximum_allocation); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1631 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", |
| 1632 | total_allocation); |
| 1633 | fprintf(STDERR, " Number of allocations: %10d\n", |
| 1634 | num_allocations); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1635 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1636 | } |
| 1637 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1638 | #ifdef PNGTEST_TIMING |
| 1639 | t_stop = (float)clock(); |
| 1640 | t_misc += (t_stop - t_start); |
| 1641 | t_start = t_stop; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1642 | fprintf(STDERR, " CPU time used = %.3f seconds", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1643 | (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1644 | fprintf(STDERR, " (decoding %.3f,\n", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1645 | t_decode/(float)CLOCKS_PER_SEC); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1646 | fprintf(STDERR, " encoding %.3f ,", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1647 | t_encode/(float)CLOCKS_PER_SEC); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1648 | fprintf(STDERR, " other %.3f seconds)\n\n", |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1649 | t_misc/(float)CLOCKS_PER_SEC); |
| 1650 | #endif |
| 1651 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1652 | if (ierror == 0) |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1653 | fprintf(STDERR, " libpng passes test\n"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1654 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1655 | else |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 1656 | fprintf(STDERR, " libpng FAILS test\n"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1657 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1658 | return (int)(ierror != 0); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1659 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1660 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1661 | /* Generate a compiler error if there is an old png.h in the search path. */ |
Glenn Randers-Pehrson | 99c7ad2 | 2012-01-24 07:58:30 -0600 | [diff] [blame] | 1662 | typedef png_libpng_version_1_6_0beta07 Your_png_h_is_not_version_1_6_0beta07; |