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