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