The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | |
| 2 | /* pngtest.c - a simple test program to test libpng |
| 3 | * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 4 | * Last changed in libpng 1.6.2 [April 25, 2013] |
| 5 | * Copyright (c) 1998-2013 Glenn Randers-Pehrson |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [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.) |
| 8 | * |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 9 | * This code is released under the libpng license. |
| 10 | * For conditions of distribution and use, see the disclaimer |
| 11 | * and license in png.h |
| 12 | * |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [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 | * |
| 19 | * The program will report "FAIL" in certain legitimate cases: |
| 20 | * 1) when the compression level or filter selection method is changed. |
| 21 | * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192. |
| 22 | * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks |
| 23 | * exist in the input file. |
| 24 | * 4) others not listed here... |
| 25 | * In these cases, it is best to check with another tool such as "pngcheck" |
| 26 | * to see what the differences between the two files are. |
| 27 | * |
| 28 | * If a filename is given on the command-line, then this file is used |
| 29 | * for the input, rather than the default "pngtest.png". This allows |
| 30 | * testing a wide variety of files easily. You can also test a number |
| 31 | * of files at once by typing "pngtest -m file1.png file2.png ..." |
| 32 | */ |
| 33 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 34 | #define _POSIX_SOURCE 1 |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | /* Defined so I can write to a file on gui/windowing platforms */ |
| 41 | /* #define STDERR stderr */ |
| 42 | #define STDERR stdout /* For DOS */ |
| 43 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 44 | #include "png.h" |
| 45 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 46 | /* Known chunks that exist in pngtest.png must be supported or pngtest will fail |
| 47 | * simply as a result of re-ordering them. This may be fixed in 1.7 |
| 48 | */ |
| 49 | #if defined PNG_READ_SUPPORTED && /* else nothing can be done */\ |
| 50 | defined PNG_READ_bKGD_SUPPORTED &&\ |
| 51 | defined PNG_READ_cHRM_SUPPORTED &&\ |
| 52 | defined PNG_READ_gAMA_SUPPORTED &&\ |
| 53 | defined PNG_READ_oFFs_SUPPORTED &&\ |
| 54 | defined PNG_READ_pCAL_SUPPORTED &&\ |
| 55 | defined PNG_READ_pHYs_SUPPORTED &&\ |
| 56 | defined PNG_READ_sBIT_SUPPORTED &&\ |
| 57 | defined PNG_READ_sCAL_SUPPORTED &&\ |
| 58 | defined PNG_READ_sRGB_SUPPORTED &&\ |
| 59 | defined PNG_READ_tEXt_SUPPORTED &&\ |
| 60 | defined PNG_READ_tIME_SUPPORTED &&\ |
| 61 | defined PNG_READ_zTXt_SUPPORTED |
| 62 | |
| 63 | #include "zlib.h" |
| 64 | /* Copied from pngpriv.h but only used in error messages below. */ |
| 65 | #ifndef PNG_ZBUF_SIZE |
| 66 | # define PNG_ZBUF_SIZE 8192 |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 67 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 68 | #define FCLOSE(file) fclose(file) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 69 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 70 | #ifndef PNG_STDIO_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 71 | typedef FILE * png_FILE_p; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 72 | #endif |
| 73 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 74 | /* Makes pngtest verbose so we can find problems. */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 75 | #ifndef PNG_DEBUG |
| 76 | # define PNG_DEBUG 0 |
| 77 | #endif |
| 78 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 79 | #if PNG_DEBUG > 1 |
| 80 | # define pngtest_debug(m) ((void)fprintf(stderr, m "\n")) |
| 81 | # define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1)) |
| 82 | # define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2)) |
| 83 | #else |
| 84 | # define pngtest_debug(m) ((void)0) |
| 85 | # define pngtest_debug1(m,p1) ((void)0) |
| 86 | # define pngtest_debug2(m,p1,p2) ((void)0) |
| 87 | #endif |
| 88 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 89 | #if !PNG_DEBUG |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 90 | # define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /* Turn on CPU timing |
| 94 | #define PNGTEST_TIMING |
| 95 | */ |
| 96 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 97 | #ifndef PNG_FLOATING_POINT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 98 | #undef PNGTEST_TIMING |
| 99 | #endif |
| 100 | |
| 101 | #ifdef PNGTEST_TIMING |
| 102 | static float t_start, t_stop, t_decode, t_encode, t_misc; |
| 103 | #include <time.h> |
| 104 | #endif |
| 105 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 106 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 107 | #define PNG_tIME_STRING_LENGTH 29 |
| 108 | static int tIME_chunk_present = 0; |
| 109 | static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present"; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | static int verbose = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 113 | static int strict = 0; |
| 114 | static int relaxed = 0; |
| 115 | static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */ |
| 116 | static int error_count = 0; /* count calls to png_error */ |
| 117 | static int warning_count = 0; /* count calls to png_warning */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 118 | |
| 119 | #ifdef __TURBOC__ |
| 120 | #include <mem.h> |
| 121 | #endif |
| 122 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 123 | /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */ |
| 124 | #ifndef png_jmpbuf |
| 125 | # define png_jmpbuf(png_ptr) png_ptr->jmpbuf |
| 126 | #endif |
| 127 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 128 | /* Defines for unknown chunk handling if required. */ |
| 129 | #ifndef PNG_HANDLE_CHUNK_ALWAYS |
| 130 | # define PNG_HANDLE_CHUNK_ALWAYS 3 |
| 131 | #endif |
| 132 | #ifndef PNG_HANDLE_CHUNK_IF_SAFE |
| 133 | # define PNG_HANDLE_CHUNK_IF_SAFE 2 |
| 134 | #endif |
| 135 | |
| 136 | /* Utility to save typing/errors, the argument must be a name */ |
| 137 | #define MEMZERO(var) ((void)memset(&var, 0, sizeof var)) |
| 138 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 139 | /* Example of using row callbacks to make a simple progress meter */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 140 | static int status_pass = 1; |
| 141 | static int status_dots_requested = 0; |
| 142 | static int status_dots = 1; |
| 143 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 144 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 145 | read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
| 146 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 147 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX) |
| 148 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 149 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 150 | if (status_pass != pass) |
| 151 | { |
| 152 | fprintf(stdout, "\n Pass %d: ", pass); |
| 153 | status_pass = pass; |
| 154 | status_dots = 31; |
| 155 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 156 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 157 | status_dots--; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 158 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 159 | if (status_dots == 0) |
| 160 | { |
| 161 | fprintf(stdout, "\n "); |
| 162 | status_dots=30; |
| 163 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 164 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 165 | fprintf(stdout, "r"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 168 | #ifdef PNG_WRITE_SUPPORTED |
| 169 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 170 | write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
| 171 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 172 | if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) |
| 173 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 174 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 175 | fprintf(stdout, "w"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 176 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 177 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 178 | |
| 179 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 180 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 181 | /* Example of using user transform callback (we don't transform anything, |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 182 | * but merely examine the row filters. We set this to 256 rather than |
| 183 | * 5 in case illegal filter values are present.) |
| 184 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 185 | static png_uint_32 filters_used[256]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 186 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 187 | count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
| 188 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 189 | if (png_ptr != NULL && row_info != NULL) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 190 | ++filters_used[*(data - 1)]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 191 | } |
| 192 | #endif |
| 193 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 194 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 195 | /* Example of using user transform callback (we don't transform anything, |
| 196 | * but merely count the zero samples) |
| 197 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 198 | |
| 199 | static png_uint_32 zero_samples; |
| 200 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 201 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 202 | count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
| 203 | { |
| 204 | png_bytep dp = data; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 205 | if (png_ptr == NULL) |
| 206 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 207 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 208 | /* Contents of row_info: |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 209 | * png_uint_32 width width of row |
| 210 | * png_uint_32 rowbytes number of bytes in row |
| 211 | * png_byte color_type color type of pixels |
| 212 | * png_byte bit_depth bit depth of samples |
| 213 | * png_byte channels number of channels (1-4) |
| 214 | * png_byte pixel_depth bits per pixel (depth*channels) |
| 215 | */ |
| 216 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 217 | /* Counts the number of zero samples (or zero pixels if color_type is 3 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 218 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 219 | if (row_info->color_type == 0 || row_info->color_type == 3) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 220 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 221 | int pos = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 222 | png_uint_32 n, nstop; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 223 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 224 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 225 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 226 | if (row_info->bit_depth == 1) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 227 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 228 | if (((*dp << pos++ ) & 0x80) == 0) |
| 229 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 230 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 231 | if (pos == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 232 | { |
| 233 | pos = 0; |
| 234 | dp++; |
| 235 | } |
| 236 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 237 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 238 | if (row_info->bit_depth == 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 239 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 240 | if (((*dp << (pos+=2)) & 0xc0) == 0) |
| 241 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 242 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 243 | if (pos == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 244 | { |
| 245 | pos = 0; |
| 246 | dp++; |
| 247 | } |
| 248 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 249 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 250 | if (row_info->bit_depth == 4) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 251 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 252 | if (((*dp << (pos+=4)) & 0xf0) == 0) |
| 253 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 254 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 255 | if (pos == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 256 | { |
| 257 | pos = 0; |
| 258 | dp++; |
| 259 | } |
| 260 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 261 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 262 | if (row_info->bit_depth == 8) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 263 | if (*dp++ == 0) |
| 264 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 265 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 266 | if (row_info->bit_depth == 16) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 267 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 268 | if ((*dp | *(dp+1)) == 0) |
| 269 | zero_samples++; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 270 | dp+=2; |
| 271 | } |
| 272 | } |
| 273 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 274 | else /* Other color types */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 275 | { |
| 276 | png_uint_32 n, nstop; |
| 277 | int channel; |
| 278 | int color_channels = row_info->channels; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 279 | if (row_info->color_type > 3)color_channels--; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 280 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 281 | for (n = 0, nstop=row_info->width; n<nstop; n++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 282 | { |
| 283 | for (channel = 0; channel < color_channels; channel++) |
| 284 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 285 | if (row_info->bit_depth == 8) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 286 | if (*dp++ == 0) |
| 287 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 288 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 289 | if (row_info->bit_depth == 16) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 290 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 291 | if ((*dp | *(dp+1)) == 0) |
| 292 | zero_samples++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 293 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 294 | dp+=2; |
| 295 | } |
| 296 | } |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 297 | if (row_info->color_type > 3) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 298 | { |
| 299 | dp++; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 300 | if (row_info->bit_depth == 16) |
| 301 | dp++; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */ |
| 307 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 308 | #ifndef PNG_STDIO_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 309 | /* START of code to validate stdio-free compilation */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 310 | /* These copies of the default read/write functions come from pngrio.c and |
| 311 | * pngwio.c. They allow "don't include stdio" testing of the library. |
| 312 | * This is the function that does the actual reading of data. If you are |
| 313 | * not reading from a standard C stream, you should create a replacement |
| 314 | * read_data function and use it at run time with png_set_read_fn(), rather |
| 315 | * than changing the library. |
| 316 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 317 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 318 | #ifdef PNG_IO_STATE_SUPPORTED |
| 319 | void |
| 320 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, |
| 321 | png_uint_32 io_op); |
| 322 | void |
| 323 | pngtest_check_io_state(png_structp png_ptr, png_size_t data_length, |
| 324 | png_uint_32 io_op) |
| 325 | { |
| 326 | png_uint_32 io_state = png_get_io_state(png_ptr); |
| 327 | int err = 0; |
| 328 | |
| 329 | /* Check if the current operation (reading / writing) is as expected. */ |
| 330 | if ((io_state & PNG_IO_MASK_OP) != io_op) |
| 331 | png_error(png_ptr, "Incorrect operation in I/O state"); |
| 332 | |
| 333 | /* Check if the buffer size specific to the current location |
| 334 | * (file signature / header / data / crc) is as expected. |
| 335 | */ |
| 336 | switch (io_state & PNG_IO_MASK_LOC) |
| 337 | { |
| 338 | case PNG_IO_SIGNATURE: |
| 339 | if (data_length > 8) |
| 340 | err = 1; |
| 341 | break; |
| 342 | case PNG_IO_CHUNK_HDR: |
| 343 | if (data_length != 8) |
| 344 | err = 1; |
| 345 | break; |
| 346 | case PNG_IO_CHUNK_DATA: |
| 347 | break; /* no restrictions here */ |
| 348 | case PNG_IO_CHUNK_CRC: |
| 349 | if (data_length != 4) |
| 350 | err = 1; |
| 351 | break; |
| 352 | default: |
| 353 | err = 1; /* uninitialized */ |
| 354 | } |
| 355 | if (err) |
| 356 | png_error(png_ptr, "Bad I/O state or buffer size"); |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 361 | pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 362 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 363 | png_size_t check = 0; |
| 364 | png_voidp io_ptr; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 365 | |
| 366 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t |
| 367 | * instead of an int, which is what fread() actually returns. |
| 368 | */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 369 | io_ptr = png_get_io_ptr(png_ptr); |
| 370 | if (io_ptr != NULL) |
| 371 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 372 | check = fread(data, 1, length, (png_FILE_p)io_ptr); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 373 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 374 | |
| 375 | if (check != length) |
| 376 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 377 | png_error(png_ptr, "Read Error"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 378 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 379 | |
| 380 | #ifdef PNG_IO_STATE_SUPPORTED |
| 381 | pngtest_check_io_state(png_ptr, length, PNG_IO_READING); |
| 382 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 383 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 384 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 385 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 386 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 387 | pngtest_flush(png_structp png_ptr) |
| 388 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 389 | /* Do nothing; fflush() is said to be just a waste of energy. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 390 | PNG_UNUSED(png_ptr) /* Stifle compiler warning */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 391 | } |
| 392 | #endif |
| 393 | |
| 394 | /* This is the function that does the actual writing of data. If you are |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 395 | * not writing to a standard C stream, you should create a replacement |
| 396 | * write_data function and use it at run time with png_set_write_fn(), rather |
| 397 | * than changing the library. |
| 398 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 399 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 400 | pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 401 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 402 | png_size_t check; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 403 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 404 | check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr)); |
| 405 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 406 | if (check != length) |
| 407 | { |
| 408 | png_error(png_ptr, "Write Error"); |
| 409 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 410 | |
| 411 | #ifdef PNG_IO_STATE_SUPPORTED |
| 412 | pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING); |
| 413 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 414 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 415 | #endif /* !PNG_STDIO_SUPPORTED */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 416 | |
| 417 | /* This function is called when there is a warning, but the library thinks |
| 418 | * it can continue anyway. Replacement functions don't have to do anything |
| 419 | * here if you don't want to. In the default configuration, png_ptr is |
| 420 | * not used, but it is passed in case it may be useful. |
| 421 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 422 | typedef struct |
| 423 | { |
| 424 | PNG_CONST char *file_name; |
| 425 | } pngtest_error_parameters; |
| 426 | |
| 427 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 428 | pngtest_warning(png_structp png_ptr, png_const_charp message) |
| 429 | { |
| 430 | PNG_CONST char *name = "UNKNOWN (ERROR!)"; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 431 | pngtest_error_parameters *test = |
| 432 | (pngtest_error_parameters*)png_get_error_ptr(png_ptr); |
| 433 | |
| 434 | ++warning_count; |
| 435 | |
| 436 | if (test != NULL && test->file_name != NULL) |
| 437 | name = test->file_name; |
| 438 | |
| 439 | fprintf(STDERR, "%s: libpng warning: %s\n", name, message); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* This is the default error handling function. Note that replacements for |
| 443 | * this function MUST NOT RETURN, or the program will likely crash. This |
| 444 | * function is used by default, or if the program supplies NULL for the |
| 445 | * error function pointer in png_set_error_fn(). |
| 446 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 447 | static void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 448 | pngtest_error(png_structp png_ptr, png_const_charp message) |
| 449 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 450 | ++error_count; |
| 451 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 452 | pngtest_warning(png_ptr, message); |
| 453 | /* We can return because png_error calls the default handler, which is |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 454 | * actually OK in this case. |
| 455 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 456 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 457 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 458 | /* END of code to validate stdio-free compilation */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 459 | |
| 460 | /* START of code to validate memory allocation and deallocation */ |
| 461 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 462 | |
| 463 | /* Allocate memory. For reasonable files, size should never exceed |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 464 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 465 | * it not to. See zconf.h and png.h for more information. zlib does |
| 466 | * need to allocate exactly 64K, so whatever you call here must |
| 467 | * have the ability to do that. |
| 468 | * |
| 469 | * This piece of code can be compiled to validate max 64K allocations |
| 470 | * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. |
| 471 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 472 | typedef struct memory_information |
| 473 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 474 | png_alloc_size_t size; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 475 | png_voidp pointer; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 476 | struct memory_information *next; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 477 | } memory_information; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 478 | typedef memory_information *memory_infop; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 479 | |
| 480 | static memory_infop pinformation = NULL; |
| 481 | static int current_allocation = 0; |
| 482 | static int maximum_allocation = 0; |
| 483 | static int total_allocation = 0; |
| 484 | static int num_allocations = 0; |
| 485 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 486 | png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr, |
| 487 | png_alloc_size_t size)); |
| 488 | void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 489 | |
| 490 | png_voidp |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 491 | PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 492 | { |
| 493 | |
| 494 | /* png_malloc has already tested for NULL; png_create_struct calls |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 495 | * png_debug_malloc directly, with png_ptr == NULL which is OK |
| 496 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 497 | |
| 498 | if (size == 0) |
| 499 | return (NULL); |
| 500 | |
| 501 | /* This calls the library allocator twice, once to get the requested |
| 502 | buffer and once to get a new free list entry. */ |
| 503 | { |
| 504 | /* Disable malloc_fn and free_fn */ |
| 505 | memory_infop pinfo; |
| 506 | png_set_mem_fn(png_ptr, NULL, NULL, NULL); |
| 507 | pinfo = (memory_infop)png_malloc(png_ptr, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 508 | (sizeof *pinfo)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 509 | pinfo->size = size; |
| 510 | current_allocation += size; |
| 511 | total_allocation += size; |
| 512 | num_allocations ++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 513 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 514 | if (current_allocation > maximum_allocation) |
| 515 | maximum_allocation = current_allocation; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 516 | |
| 517 | pinfo->pointer = png_malloc(png_ptr, size); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 518 | /* Restore malloc_fn and free_fn */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 519 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 520 | png_set_mem_fn(png_ptr, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 521 | NULL, png_debug_malloc, png_debug_free); |
| 522 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 523 | if (size != 0 && pinfo->pointer == NULL) |
| 524 | { |
| 525 | current_allocation -= size; |
| 526 | total_allocation -= size; |
| 527 | png_error(png_ptr, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 528 | "out of memory in pngtest->png_debug_malloc"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 529 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 530 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 531 | pinfo->next = pinformation; |
| 532 | pinformation = pinfo; |
| 533 | /* Make sure the caller isn't assuming zeroed memory. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 534 | memset(pinfo->pointer, 0xdd, pinfo->size); |
| 535 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 536 | if (verbose) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 537 | printf("png_malloc %lu bytes at %p\n", (unsigned long)size, |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 538 | pinfo->pointer); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 539 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 540 | return (png_voidp)(pinfo->pointer); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* Free a pointer. It is removed from the list at the same time. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 545 | void PNGCBAPI |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 546 | png_debug_free(png_structp png_ptr, png_voidp ptr) |
| 547 | { |
| 548 | if (png_ptr == NULL) |
| 549 | fprintf(STDERR, "NULL pointer to png_debug_free.\n"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 550 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 551 | if (ptr == 0) |
| 552 | { |
| 553 | #if 0 /* This happens all the time. */ |
| 554 | fprintf(STDERR, "WARNING: freeing NULL pointer\n"); |
| 555 | #endif |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | /* Unlink the element from the list. */ |
| 560 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 561 | memory_infop *ppinfo = &pinformation; |
| 562 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 563 | for (;;) |
| 564 | { |
| 565 | memory_infop pinfo = *ppinfo; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 566 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 567 | if (pinfo->pointer == ptr) |
| 568 | { |
| 569 | *ppinfo = pinfo->next; |
| 570 | current_allocation -= pinfo->size; |
| 571 | if (current_allocation < 0) |
| 572 | fprintf(STDERR, "Duplicate free of memory\n"); |
| 573 | /* We must free the list element too, but first kill |
| 574 | the memory that is to be freed. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 575 | memset(ptr, 0x55, pinfo->size); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 576 | png_free_default(png_ptr, pinfo); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 577 | pinfo = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 578 | break; |
| 579 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 580 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 581 | if (pinfo->next == NULL) |
| 582 | { |
| 583 | fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr); |
| 584 | break; |
| 585 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 586 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 587 | ppinfo = &pinfo->next; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* Finally free the data. */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 592 | if (verbose) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 593 | printf("Freeing %p\n", ptr); |
| 594 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 595 | png_free_default(png_ptr, ptr); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 596 | ptr = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 597 | } |
| 598 | #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */ |
| 599 | /* END of code to test memory allocation/deallocation */ |
| 600 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 601 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 602 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 603 | /* Demonstration of user chunk support of the sTER and vpAg chunks */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 604 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 605 | /* (sTER is a public chunk not yet known by libpng. vpAg is a private |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 606 | chunk used in ImageMagick to store "virtual page" size). */ |
| 607 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 608 | static struct user_chunk_data |
| 609 | { |
| 610 | png_const_infop info_ptr; |
| 611 | png_uint_32 vpAg_width, vpAg_height; |
| 612 | png_byte vpAg_units; |
| 613 | png_byte sTER_mode; |
| 614 | int location[2]; |
| 615 | } |
| 616 | user_chunk_data; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 617 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 618 | /* Used for location and order; zero means nothing. */ |
| 619 | #define have_sTER 0x01 |
| 620 | #define have_vpAg 0x02 |
| 621 | #define before_PLTE 0x10 |
| 622 | #define before_IDAT 0x20 |
| 623 | #define after_IDAT 0x40 |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 624 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 625 | static void |
| 626 | init_callback_info(png_const_infop info_ptr) |
| 627 | { |
| 628 | MEMZERO(user_chunk_data); |
| 629 | user_chunk_data.info_ptr = info_ptr; |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | set_location(png_structp png_ptr, struct user_chunk_data *data, int what) |
| 634 | { |
| 635 | int location; |
| 636 | |
| 637 | if ((data->location[0] & what) || (data->location[1] & what)) |
| 638 | return 0; /* already have one of these */ |
| 639 | |
| 640 | /* Find where we are (the code below zeros info_ptr to indicate that the |
| 641 | * chunks before the first IDAT have been read.) |
| 642 | */ |
| 643 | if (data->info_ptr == NULL) /* after IDAT */ |
| 644 | location = what | after_IDAT; |
| 645 | |
| 646 | else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE)) |
| 647 | location = what | before_IDAT; |
| 648 | |
| 649 | else |
| 650 | location = what | before_PLTE; |
| 651 | |
| 652 | if (data->location[0] == 0) |
| 653 | data->location[0] = location; |
| 654 | |
| 655 | else |
| 656 | data->location[1] = location; |
| 657 | |
| 658 | return 1; /* handled */ |
| 659 | } |
| 660 | |
| 661 | static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr, |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 662 | png_unknown_chunkp chunk) |
| 663 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 664 | struct user_chunk_data *my_user_chunk_data = |
| 665 | (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr); |
| 666 | |
| 667 | if (my_user_chunk_data == NULL) |
| 668 | png_error(png_ptr, "lost user chunk pointer"); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 669 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 670 | /* Return one of the following: |
| 671 | * return (-n); chunk had an error |
| 672 | * return (0); did not recognize |
| 673 | * return (n); success |
| 674 | * |
| 675 | * The unknown chunk structure contains the chunk data: |
| 676 | * png_byte name[5]; |
| 677 | * png_byte *data; |
| 678 | * png_size_t size; |
| 679 | * |
| 680 | * Note that libpng has already taken care of the CRC handling. |
| 681 | */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 682 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 683 | if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */ |
| 684 | chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */ |
| 685 | { |
| 686 | /* Found sTER chunk */ |
| 687 | if (chunk->size != 1) |
| 688 | return (-1); /* Error return */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 689 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 690 | if (chunk->data[0] != 0 && chunk->data[0] != 1) |
| 691 | return (-1); /* Invalid mode */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 692 | |
| 693 | if (set_location(png_ptr, my_user_chunk_data, have_sTER)) |
| 694 | { |
| 695 | my_user_chunk_data->sTER_mode=chunk->data[0]; |
| 696 | return (1); |
| 697 | } |
| 698 | |
| 699 | else |
| 700 | return (0); /* duplicate sTER - give it to libpng */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 701 | } |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 702 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 703 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */ |
| 704 | chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */ |
| 705 | return (0); /* Did not recognize */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 706 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 707 | /* Found ImageMagick vpAg chunk */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 708 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 709 | if (chunk->size != 9) |
| 710 | return (-1); /* Error return */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 711 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 712 | if (!set_location(png_ptr, my_user_chunk_data, have_vpAg)) |
| 713 | return (0); /* duplicate vpAg */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 714 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 715 | my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data); |
| 716 | my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4); |
| 717 | my_user_chunk_data->vpAg_units = chunk->data[8]; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 718 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 719 | return (1); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 720 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 721 | |
| 722 | #ifdef PNG_WRITE_SUPPORTED |
| 723 | static void |
| 724 | write_sTER_chunk(png_structp write_ptr) |
| 725 | { |
| 726 | png_byte png_sTER[5] = {115, 84, 69, 82, '\0'}; |
| 727 | |
| 728 | if (verbose) |
| 729 | fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode); |
| 730 | |
| 731 | png_write_chunk(write_ptr, png_sTER, &user_chunk_data.sTER_mode, 1); |
| 732 | } |
| 733 | |
| 734 | static void |
| 735 | write_vpAg_chunk(png_structp write_ptr) |
| 736 | { |
| 737 | png_byte png_vpAg[5] = {118, 112, 65, 103, '\0'}; |
| 738 | |
| 739 | png_byte vpag_chunk_data[9]; |
| 740 | |
| 741 | if (verbose) |
| 742 | fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n", |
| 743 | (unsigned long)user_chunk_data.vpAg_width, |
| 744 | (unsigned long)user_chunk_data.vpAg_height, |
| 745 | user_chunk_data.vpAg_units); |
| 746 | |
| 747 | png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width); |
| 748 | png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height); |
| 749 | vpag_chunk_data[8] = user_chunk_data.vpAg_units; |
| 750 | png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9); |
| 751 | } |
| 752 | |
| 753 | static void |
| 754 | write_chunks(png_structp write_ptr, int location) |
| 755 | { |
| 756 | int i; |
| 757 | |
| 758 | /* Notice that this preserves the original chunk order, however chunks |
| 759 | * intercepted by the callback will be written *after* chunks passed to |
| 760 | * libpng. This will actually reverse a pair of sTER chunks or a pair of |
| 761 | * vpAg chunks, resulting in an error later. This is not worth worrying |
| 762 | * about - the chunks should not be duplicated! |
| 763 | */ |
| 764 | for (i=0; i<2; ++i) |
| 765 | { |
| 766 | if (user_chunk_data.location[i] == (location | have_sTER)) |
| 767 | write_sTER_chunk(write_ptr); |
| 768 | |
| 769 | else if (user_chunk_data.location[i] == (location | have_vpAg)) |
| 770 | write_vpAg_chunk(write_ptr); |
| 771 | } |
| 772 | } |
| 773 | #endif /* PNG_WRITE_SUPPORTED */ |
| 774 | #else /* !PNG_READ_USER_CHUNKS_SUPPORTED */ |
| 775 | # define write_chunks(pp,loc) ((void)0) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 776 | #endif |
| 777 | /* END of code to demonstrate user chunk support */ |
| 778 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 779 | /* START of code to check that libpng has the required text support; this only |
| 780 | * checks for the write support because if read support is missing the chunk |
| 781 | * will simply not be reported back to pngtest. |
| 782 | */ |
| 783 | #ifdef PNG_TEXT_SUPPORTED |
| 784 | static void |
| 785 | pngtest_check_text_support(png_const_structp png_ptr, png_textp text_ptr, |
| 786 | int num_text) |
| 787 | { |
| 788 | while (num_text > 0) |
| 789 | { |
| 790 | switch (text_ptr[--num_text].compression) |
| 791 | { |
| 792 | case PNG_TEXT_COMPRESSION_NONE: |
| 793 | break; |
| 794 | |
| 795 | case PNG_TEXT_COMPRESSION_zTXt: |
| 796 | # ifndef PNG_WRITE_zTXt_SUPPORTED |
| 797 | ++unsupported_chunks; |
| 798 | # endif |
| 799 | break; |
| 800 | |
| 801 | case PNG_ITXT_COMPRESSION_NONE: |
| 802 | case PNG_ITXT_COMPRESSION_zTXt: |
| 803 | # ifndef PNG_WRITE_iTXt_SUPPORTED |
| 804 | ++unsupported_chunks; |
| 805 | # endif |
| 806 | break; |
| 807 | |
| 808 | default: |
| 809 | /* This is an error */ |
| 810 | png_error(png_ptr, "invalid text chunk compression field"); |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | #endif |
| 816 | /* END of code to check that libpng has the required text support */ |
| 817 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 818 | /* Test one file */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 819 | static int |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 820 | test_one_file(PNG_CONST char *inname, PNG_CONST char *outname) |
| 821 | { |
| 822 | static png_FILE_p fpin; |
| 823 | static png_FILE_p fpout; /* "static" prevents setjmp corruption */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 824 | pngtest_error_parameters error_parameters; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 825 | png_structp read_ptr; |
| 826 | png_infop read_info_ptr, end_info_ptr; |
| 827 | #ifdef PNG_WRITE_SUPPORTED |
| 828 | png_structp write_ptr; |
| 829 | png_infop write_info_ptr; |
| 830 | png_infop write_end_info_ptr; |
| 831 | #else |
| 832 | png_structp write_ptr = NULL; |
| 833 | png_infop write_info_ptr = NULL; |
| 834 | png_infop write_end_info_ptr = NULL; |
| 835 | #endif |
| 836 | png_bytep row_buf; |
| 837 | png_uint_32 y; |
| 838 | png_uint_32 width, height; |
| 839 | int num_pass, pass; |
| 840 | int bit_depth, color_type; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 841 | |
| 842 | row_buf = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 843 | error_parameters.file_name = inname; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 844 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 845 | if ((fpin = fopen(inname, "rb")) == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 846 | { |
| 847 | fprintf(STDERR, "Could not find input file %s\n", inname); |
| 848 | return (1); |
| 849 | } |
| 850 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 851 | if ((fpout = fopen(outname, "wb")) == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 852 | { |
| 853 | fprintf(STDERR, "Could not open output file %s\n", outname); |
| 854 | FCLOSE(fpin); |
| 855 | return (1); |
| 856 | } |
| 857 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 858 | pngtest_debug("Allocating read and write structures"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 859 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 860 | read_ptr = |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 861 | png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
| 862 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 863 | #else |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 864 | read_ptr = |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 865 | png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 866 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 867 | png_set_error_fn(read_ptr, &error_parameters, pngtest_error, |
| 868 | pngtest_warning); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 869 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 870 | #ifdef PNG_WRITE_SUPPORTED |
| 871 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 872 | write_ptr = |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 873 | png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, |
| 874 | NULL, NULL, NULL, png_debug_malloc, png_debug_free); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 875 | #else |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 876 | write_ptr = |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 877 | png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 878 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 879 | png_set_error_fn(write_ptr, &error_parameters, pngtest_error, |
| 880 | pngtest_warning); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 881 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 882 | pngtest_debug("Allocating read_info, write_info and end_info structures"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 883 | read_info_ptr = png_create_info_struct(read_ptr); |
| 884 | end_info_ptr = png_create_info_struct(read_ptr); |
| 885 | #ifdef PNG_WRITE_SUPPORTED |
| 886 | write_info_ptr = png_create_info_struct(write_ptr); |
| 887 | write_end_info_ptr = png_create_info_struct(write_ptr); |
| 888 | #endif |
| 889 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 890 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
| 891 | init_callback_info(read_info_ptr); |
| 892 | png_set_read_user_chunk_fn(read_ptr, &user_chunk_data, |
| 893 | read_user_chunk_callback); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 894 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 895 | |
| 896 | #ifdef PNG_SETJMP_SUPPORTED |
| 897 | pngtest_debug("Setting jmpbuf for read struct"); |
| 898 | if (setjmp(png_jmpbuf(read_ptr))) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 899 | { |
| 900 | fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); |
| 901 | png_free(read_ptr, row_buf); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 902 | row_buf = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 903 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 904 | #ifdef PNG_WRITE_SUPPORTED |
| 905 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
| 906 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
| 907 | #endif |
| 908 | FCLOSE(fpin); |
| 909 | FCLOSE(fpout); |
| 910 | return (1); |
| 911 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 912 | |
| 913 | #ifdef PNG_WRITE_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 914 | pngtest_debug("Setting jmpbuf for write struct"); |
| 915 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 916 | if (setjmp(png_jmpbuf(write_ptr))) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 917 | { |
| 918 | fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname); |
| 919 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 920 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
| 921 | #ifdef PNG_WRITE_SUPPORTED |
| 922 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
| 923 | #endif |
| 924 | FCLOSE(fpin); |
| 925 | FCLOSE(fpout); |
| 926 | return (1); |
| 927 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 928 | #endif |
| 929 | #endif |
| 930 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 931 | if (strict) |
| 932 | { |
| 933 | /* Treat png_benign_error() as errors on read */ |
| 934 | png_set_benign_errors(read_ptr, 0); |
| 935 | |
| 936 | #ifdef PNG_WRITE_SUPPORTED |
| 937 | /* Treat them as errors on write */ |
| 938 | png_set_benign_errors(write_ptr, 0); |
| 939 | #endif |
| 940 | |
| 941 | /* if strict is not set, then app warnings and errors are treated as |
| 942 | * warnings in release builds, but not in unstable builds; this can be |
| 943 | * changed with '--relaxed'. |
| 944 | */ |
| 945 | } |
| 946 | |
| 947 | else if (relaxed) |
| 948 | { |
| 949 | /* Allow application (pngtest) errors and warnings to pass */ |
| 950 | png_set_benign_errors(read_ptr, 1); |
| 951 | |
| 952 | #ifdef PNG_WRITE_SUPPORTED |
| 953 | png_set_benign_errors(write_ptr, 1); |
| 954 | #endif |
| 955 | } |
| 956 | |
| 957 | pngtest_debug("Initializing input and output streams"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 958 | #ifdef PNG_STDIO_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 959 | png_init_io(read_ptr, fpin); |
| 960 | # ifdef PNG_WRITE_SUPPORTED |
| 961 | png_init_io(write_ptr, fpout); |
| 962 | # endif |
| 963 | #else |
| 964 | png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data); |
| 965 | # ifdef PNG_WRITE_SUPPORTED |
| 966 | png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data, |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 967 | # ifdef PNG_WRITE_FLUSH_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 968 | pngtest_flush); |
| 969 | # else |
| 970 | NULL); |
| 971 | # endif |
| 972 | # endif |
| 973 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 974 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 975 | if (status_dots_requested == 1) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 976 | { |
| 977 | #ifdef PNG_WRITE_SUPPORTED |
| 978 | png_set_write_status_fn(write_ptr, write_row_callback); |
| 979 | #endif |
| 980 | png_set_read_status_fn(read_ptr, read_row_callback); |
| 981 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 982 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 983 | else |
| 984 | { |
| 985 | #ifdef PNG_WRITE_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 986 | png_set_write_status_fn(write_ptr, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 987 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 988 | png_set_read_status_fn(read_ptr, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 991 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 992 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 993 | int i; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 994 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 995 | for (i = 0; i<256; i++) |
| 996 | filters_used[i] = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 997 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 998 | png_set_read_user_transform_fn(read_ptr, count_filters); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 999 | } |
| 1000 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1001 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1002 | zero_samples = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1003 | png_set_write_user_transform_fn(write_ptr, count_zero_samples); |
| 1004 | #endif |
| 1005 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1006 | #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 1007 | /* Preserve all the unknown chunks, if possible. If this is disabled then, |
| 1008 | * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use |
| 1009 | * libpng to *save* the unknown chunks on read (because we can't switch the |
| 1010 | * save option on!) |
| 1011 | * |
| 1012 | * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all |
| 1013 | * unknown chunks and write will write them all. |
| 1014 | */ |
| 1015 | #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1016 | png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1017 | NULL, 0); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1018 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1019 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1020 | png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS, |
| 1021 | NULL, 0); |
| 1022 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1023 | #endif |
| 1024 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1025 | pngtest_debug("Reading info struct"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1026 | png_read_info(read_ptr, read_info_ptr); |
| 1027 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1028 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED |
| 1029 | /* This is a bit of a hack; there is no obvious way in the callback function |
| 1030 | * to determine that the chunks before the first IDAT have been read, so |
| 1031 | * remove the info_ptr (which is only used to determine position relative to |
| 1032 | * PLTE) here to indicate that we are after the IDAT. |
| 1033 | */ |
| 1034 | user_chunk_data.info_ptr = NULL; |
| 1035 | #endif |
| 1036 | |
| 1037 | pngtest_debug("Transferring info struct"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1038 | { |
| 1039 | int interlace_type, compression_type, filter_type; |
| 1040 | |
| 1041 | if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth, |
| 1042 | &color_type, &interlace_type, &compression_type, &filter_type)) |
| 1043 | { |
| 1044 | png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth, |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1045 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1046 | color_type, interlace_type, compression_type, filter_type); |
| 1047 | #else |
| 1048 | color_type, PNG_INTERLACE_NONE, compression_type, filter_type); |
| 1049 | #endif |
| 1050 | } |
| 1051 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1052 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1053 | #ifdef PNG_cHRM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1054 | { |
| 1055 | png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x, |
| 1056 | blue_y; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1057 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1058 | if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, |
| 1059 | &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1060 | { |
| 1061 | png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x, |
| 1062 | red_y, green_x, green_y, blue_x, blue_y); |
| 1063 | } |
| 1064 | } |
| 1065 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1066 | #ifdef PNG_gAMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1067 | { |
| 1068 | png_fixed_point gamma; |
| 1069 | |
| 1070 | if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1071 | png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1072 | } |
| 1073 | #endif |
| 1074 | #else /* Use floating point versions */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1075 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1076 | #ifdef PNG_cHRM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1077 | { |
| 1078 | double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, |
| 1079 | blue_y; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1080 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1081 | if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x, |
| 1082 | &red_y, &green_x, &green_y, &blue_x, &blue_y)) |
| 1083 | { |
| 1084 | png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x, |
| 1085 | red_y, green_x, green_y, blue_x, blue_y); |
| 1086 | } |
| 1087 | } |
| 1088 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1089 | #ifdef PNG_gAMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1090 | { |
| 1091 | double gamma; |
| 1092 | |
| 1093 | if (png_get_gAMA(read_ptr, read_info_ptr, &gamma)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1094 | png_set_gAMA(write_ptr, write_info_ptr, gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1095 | } |
| 1096 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1097 | #endif /* Floating point */ |
| 1098 | #endif /* Fixed point */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1099 | #ifdef PNG_iCCP_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1100 | { |
| 1101 | png_charp name; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1102 | png_bytep profile; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1103 | png_uint_32 proflen; |
| 1104 | int compression_type; |
| 1105 | |
| 1106 | if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type, |
| 1107 | &profile, &proflen)) |
| 1108 | { |
| 1109 | png_set_iCCP(write_ptr, write_info_ptr, name, compression_type, |
| 1110 | profile, proflen); |
| 1111 | } |
| 1112 | } |
| 1113 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1114 | #ifdef PNG_sRGB_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1115 | { |
| 1116 | int intent; |
| 1117 | |
| 1118 | if (png_get_sRGB(read_ptr, read_info_ptr, &intent)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1119 | png_set_sRGB(write_ptr, write_info_ptr, intent); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1120 | } |
| 1121 | #endif |
| 1122 | { |
| 1123 | png_colorp palette; |
| 1124 | int num_palette; |
| 1125 | |
| 1126 | if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1127 | png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1128 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1129 | #ifdef PNG_bKGD_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1130 | { |
| 1131 | png_color_16p background; |
| 1132 | |
| 1133 | if (png_get_bKGD(read_ptr, read_info_ptr, &background)) |
| 1134 | { |
| 1135 | png_set_bKGD(write_ptr, write_info_ptr, background); |
| 1136 | } |
| 1137 | } |
| 1138 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1139 | #ifdef PNG_hIST_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1140 | { |
| 1141 | png_uint_16p hist; |
| 1142 | |
| 1143 | if (png_get_hIST(read_ptr, read_info_ptr, &hist)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1144 | png_set_hIST(write_ptr, write_info_ptr, hist); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1145 | } |
| 1146 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1147 | #ifdef PNG_oFFs_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1148 | { |
| 1149 | png_int_32 offset_x, offset_y; |
| 1150 | int unit_type; |
| 1151 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1152 | if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y, |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1153 | &unit_type)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1154 | { |
| 1155 | png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type); |
| 1156 | } |
| 1157 | } |
| 1158 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1159 | #ifdef PNG_pCAL_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1160 | { |
| 1161 | png_charp purpose, units; |
| 1162 | png_charpp params; |
| 1163 | png_int_32 X0, X1; |
| 1164 | int type, nparams; |
| 1165 | |
| 1166 | if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type, |
| 1167 | &nparams, &units, ¶ms)) |
| 1168 | { |
| 1169 | png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type, |
| 1170 | nparams, units, params); |
| 1171 | } |
| 1172 | } |
| 1173 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1174 | #ifdef PNG_pHYs_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1175 | { |
| 1176 | png_uint_32 res_x, res_y; |
| 1177 | int unit_type; |
| 1178 | |
| 1179 | if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1180 | png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1181 | } |
| 1182 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1183 | #ifdef PNG_sBIT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1184 | { |
| 1185 | png_color_8p sig_bit; |
| 1186 | |
| 1187 | if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1188 | png_set_sBIT(write_ptr, write_info_ptr, sig_bit); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1189 | } |
| 1190 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1191 | #ifdef PNG_sCAL_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1192 | #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ |
| 1193 | defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1194 | { |
| 1195 | int unit; |
| 1196 | double scal_width, scal_height; |
| 1197 | |
| 1198 | if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width, |
| 1199 | &scal_height)) |
| 1200 | { |
| 1201 | png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height); |
| 1202 | } |
| 1203 | } |
| 1204 | #else |
| 1205 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1206 | { |
| 1207 | int unit; |
| 1208 | png_charp scal_width, scal_height; |
| 1209 | |
| 1210 | if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width, |
| 1211 | &scal_height)) |
| 1212 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1213 | png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, |
| 1214 | scal_height); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | #endif |
| 1218 | #endif |
| 1219 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1220 | #ifdef PNG_TEXT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1221 | { |
| 1222 | png_textp text_ptr; |
| 1223 | int num_text; |
| 1224 | |
| 1225 | if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0) |
| 1226 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1227 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
| 1228 | |
| 1229 | pngtest_check_text_support(read_ptr, text_ptr, num_text); |
| 1230 | |
| 1231 | if (verbose) |
| 1232 | { |
| 1233 | int i; |
| 1234 | |
| 1235 | printf("\n"); |
| 1236 | for (i=0; i<num_text; i++) |
| 1237 | { |
| 1238 | printf(" Text compression[%d]=%d\n", |
| 1239 | i, text_ptr[i].compression); |
| 1240 | } |
| 1241 | } |
| 1242 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1243 | png_set_text(write_ptr, write_info_ptr, text_ptr, num_text); |
| 1244 | } |
| 1245 | } |
| 1246 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1247 | #ifdef PNG_tIME_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1248 | { |
| 1249 | png_timep mod_time; |
| 1250 | |
| 1251 | if (png_get_tIME(read_ptr, read_info_ptr, &mod_time)) |
| 1252 | { |
| 1253 | png_set_tIME(write_ptr, write_info_ptr, mod_time); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1254 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1255 | if (png_convert_to_rfc1123_buffer(tIME_string, mod_time)) |
| 1256 | tIME_string[(sizeof tIME_string) - 1] = '\0'; |
| 1257 | |
| 1258 | else |
| 1259 | { |
| 1260 | strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string)); |
| 1261 | tIME_string[(sizeof tIME_string) - 1] = '\0'; |
| 1262 | } |
| 1263 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1264 | tIME_chunk_present++; |
| 1265 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1266 | } |
| 1267 | } |
| 1268 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1269 | #ifdef PNG_tRNS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1270 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1271 | png_bytep trans_alpha; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1272 | int num_trans; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1273 | png_color_16p trans_color; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1274 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1275 | if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans, |
| 1276 | &trans_color)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1277 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1278 | int sample_max = (1 << bit_depth); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1279 | /* libpng doesn't reject a tRNS chunk with out-of-range samples */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1280 | if (!((color_type == PNG_COLOR_TYPE_GRAY && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1281 | (int)trans_color->gray > sample_max) || |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1282 | (color_type == PNG_COLOR_TYPE_RGB && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1283 | ((int)trans_color->red > sample_max || |
| 1284 | (int)trans_color->green > sample_max || |
| 1285 | (int)trans_color->blue > sample_max)))) |
| 1286 | png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans, |
| 1287 | trans_color); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1291 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1292 | { |
| 1293 | png_unknown_chunkp unknowns; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1294 | int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1295 | &unknowns); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1296 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1297 | if (num_unknowns) |
| 1298 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1299 | png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns, |
| 1300 | num_unknowns); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1301 | #if PNG_LIBPNG_VER < 10600 |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1302 | /* Copy the locations from the read_info_ptr. The automatically |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1303 | * generated locations in write_end_info_ptr are wrong prior to 1.6.0 |
| 1304 | * because they are reset from the write pointer (removed in 1.6.0). |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1305 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1306 | { |
| 1307 | int i; |
| 1308 | for (i = 0; i < num_unknowns; i++) |
| 1309 | png_set_unknown_chunk_location(write_ptr, write_info_ptr, i, |
| 1310 | unknowns[i].location); |
| 1311 | } |
| 1312 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | #endif |
| 1316 | |
| 1317 | #ifdef PNG_WRITE_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1318 | pngtest_debug("Writing info struct"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1319 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1320 | /* Write the info in two steps so that if we write the 'unknown' chunks here |
| 1321 | * they go to the correct place. |
| 1322 | */ |
| 1323 | png_write_info_before_PLTE(write_ptr, write_info_ptr); |
| 1324 | |
| 1325 | write_chunks(write_ptr, before_PLTE); /* before PLTE */ |
| 1326 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1327 | png_write_info(write_ptr, write_info_ptr); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1328 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1329 | write_chunks(write_ptr, before_IDAT); /* after PLTE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1330 | #endif |
| 1331 | |
| 1332 | #ifdef SINGLE_ROWBUF_ALLOC |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1333 | pngtest_debug("Allocating row buffer..."); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1334 | row_buf = (png_bytep)png_malloc(read_ptr, |
| 1335 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1336 | |
| 1337 | pngtest_debug1("\t0x%08lx", (unsigned long)row_buf); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1338 | #endif /* SINGLE_ROWBUF_ALLOC */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1339 | pngtest_debug("Writing row data"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1340 | |
| 1341 | #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ |
| 1342 | defined(PNG_WRITE_INTERLACING_SUPPORTED) |
| 1343 | num_pass = png_set_interlace_handling(read_ptr); |
| 1344 | # ifdef PNG_WRITE_SUPPORTED |
| 1345 | png_set_interlace_handling(write_ptr); |
| 1346 | # endif |
| 1347 | #else |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1348 | num_pass = 1; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1349 | #endif |
| 1350 | |
| 1351 | #ifdef PNGTEST_TIMING |
| 1352 | t_stop = (float)clock(); |
| 1353 | t_misc += (t_stop - t_start); |
| 1354 | t_start = t_stop; |
| 1355 | #endif |
| 1356 | for (pass = 0; pass < num_pass; pass++) |
| 1357 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1358 | pngtest_debug1("Writing row data for pass %d", pass); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1359 | for (y = 0; y < height; y++) |
| 1360 | { |
| 1361 | #ifndef SINGLE_ROWBUF_ALLOC |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1362 | pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1363 | row_buf = (png_bytep)png_malloc(read_ptr, |
| 1364 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1365 | |
| 1366 | pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1367 | png_get_rowbytes(read_ptr, read_info_ptr)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1368 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1369 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1370 | png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1371 | |
| 1372 | #ifdef PNG_WRITE_SUPPORTED |
| 1373 | #ifdef PNGTEST_TIMING |
| 1374 | t_stop = (float)clock(); |
| 1375 | t_decode += (t_stop - t_start); |
| 1376 | t_start = t_stop; |
| 1377 | #endif |
| 1378 | png_write_rows(write_ptr, (png_bytepp)&row_buf, 1); |
| 1379 | #ifdef PNGTEST_TIMING |
| 1380 | t_stop = (float)clock(); |
| 1381 | t_encode += (t_stop - t_start); |
| 1382 | t_start = t_stop; |
| 1383 | #endif |
| 1384 | #endif /* PNG_WRITE_SUPPORTED */ |
| 1385 | |
| 1386 | #ifndef SINGLE_ROWBUF_ALLOC |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1387 | pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1388 | png_free(read_ptr, row_buf); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1389 | row_buf = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1390 | #endif /* !SINGLE_ROWBUF_ALLOC */ |
| 1391 | } |
| 1392 | } |
| 1393 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1394 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1395 | png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1); |
| 1396 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1397 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1398 | png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1); |
| 1399 | #endif |
| 1400 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1401 | pngtest_debug("Reading and writing end_info data"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1402 | |
| 1403 | png_read_end(read_ptr, end_info_ptr); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1404 | #ifdef PNG_TEXT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1405 | { |
| 1406 | png_textp text_ptr; |
| 1407 | int num_text; |
| 1408 | |
| 1409 | if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0) |
| 1410 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1411 | pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text); |
| 1412 | |
| 1413 | pngtest_check_text_support(read_ptr, text_ptr, num_text); |
| 1414 | |
| 1415 | if (verbose) |
| 1416 | { |
| 1417 | int i; |
| 1418 | |
| 1419 | printf("\n"); |
| 1420 | for (i=0; i<num_text; i++) |
| 1421 | { |
| 1422 | printf(" Text compression[%d]=%d\n", |
| 1423 | i, text_ptr[i].compression); |
| 1424 | } |
| 1425 | } |
| 1426 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1427 | png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text); |
| 1428 | } |
| 1429 | } |
| 1430 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1431 | #ifdef PNG_tIME_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1432 | { |
| 1433 | png_timep mod_time; |
| 1434 | |
| 1435 | if (png_get_tIME(read_ptr, end_info_ptr, &mod_time)) |
| 1436 | { |
| 1437 | png_set_tIME(write_ptr, write_end_info_ptr, mod_time); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1438 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1439 | if (png_convert_to_rfc1123_buffer(tIME_string, mod_time)) |
| 1440 | tIME_string[(sizeof tIME_string) - 1] = '\0'; |
| 1441 | |
| 1442 | else |
| 1443 | { |
| 1444 | strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string); |
| 1445 | tIME_string[(sizeof tIME_string)-1] = '\0'; |
| 1446 | } |
| 1447 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1448 | tIME_chunk_present++; |
| 1449 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1450 | } |
| 1451 | } |
| 1452 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1453 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1454 | { |
| 1455 | png_unknown_chunkp unknowns; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1456 | int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1457 | &unknowns); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1458 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1459 | if (num_unknowns) |
| 1460 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1461 | png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns, |
| 1462 | num_unknowns); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1463 | #if PNG_LIBPNG_VER < 10600 |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1464 | /* Copy the locations from the read_info_ptr. The automatically |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1465 | * generated locations in write_end_info_ptr are wrong prior to 1.6.0 |
| 1466 | * because they are reset from the write pointer (removed in 1.6.0). |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1467 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1468 | { |
| 1469 | int i; |
| 1470 | for (i = 0; i < num_unknowns; i++) |
| 1471 | png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i, |
| 1472 | unknowns[i].location); |
| 1473 | } |
| 1474 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1478 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1479 | #ifdef PNG_WRITE_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1480 | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED |
| 1481 | /* Normally one would use Z_DEFAULT_STRATEGY for text compression. |
| 1482 | * This is here just to make pngtest replicate the results from libpng |
| 1483 | * versions prior to 1.5.4, and to test this new API. |
| 1484 | */ |
| 1485 | png_set_text_compression_strategy(write_ptr, Z_FILTERED); |
| 1486 | #endif |
| 1487 | |
| 1488 | /* When the unknown vpAg/sTER chunks are written by pngtest the only way to |
| 1489 | * do it is to write them *before* calling png_write_end. When unknown |
| 1490 | * chunks are written by libpng, however, they are written just before IEND. |
| 1491 | * There seems to be no way round this, however vpAg/sTER are not expected |
| 1492 | * after IDAT. |
| 1493 | */ |
| 1494 | write_chunks(write_ptr, after_IDAT); |
| 1495 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1496 | png_write_end(write_ptr, write_end_info_ptr); |
| 1497 | #endif |
| 1498 | |
| 1499 | #ifdef PNG_EASY_ACCESS_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1500 | if (verbose) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1501 | { |
| 1502 | png_uint_32 iwidth, iheight; |
| 1503 | iwidth = png_get_image_width(write_ptr, write_info_ptr); |
| 1504 | iheight = png_get_image_height(write_ptr, write_info_ptr); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1505 | fprintf(STDERR, "\n Image width = %lu, height = %lu\n", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1506 | (unsigned long)iwidth, (unsigned long)iheight); |
| 1507 | } |
| 1508 | #endif |
| 1509 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1510 | pngtest_debug("Destroying data structs"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1511 | #ifdef SINGLE_ROWBUF_ALLOC |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1512 | pngtest_debug("destroying row_buf for read_ptr"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1513 | png_free(read_ptr, row_buf); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1514 | row_buf = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1515 | #endif /* SINGLE_ROWBUF_ALLOC */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1516 | pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1517 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 1518 | #ifdef PNG_WRITE_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1519 | pngtest_debug("destroying write_end_info_ptr"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1520 | png_destroy_info_struct(write_ptr, &write_end_info_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1521 | pngtest_debug("destroying write_ptr, write_info_ptr"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1522 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
| 1523 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1524 | pngtest_debug("Destruction complete."); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1525 | |
| 1526 | FCLOSE(fpin); |
| 1527 | FCLOSE(fpout); |
| 1528 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1529 | /* Summarize any warnings or errors and in 'strict' mode fail the test. |
| 1530 | * Unsupported chunks can result in warnings, in that case ignore the strict |
| 1531 | * setting, otherwise fail the test on warnings as well as errors. |
| 1532 | */ |
| 1533 | if (error_count > 0) |
| 1534 | { |
| 1535 | /* We don't really expect to get here because of the setjmp handling |
| 1536 | * above, but this is safe. |
| 1537 | */ |
| 1538 | fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)", |
| 1539 | inname, error_count, warning_count); |
| 1540 | |
| 1541 | if (strict != 0) |
| 1542 | return (1); |
| 1543 | } |
| 1544 | |
| 1545 | # ifdef PNG_WRITE_SUPPORTED |
| 1546 | /* If there we no write support nothing was written! */ |
| 1547 | else if (unsupported_chunks > 0) |
| 1548 | { |
| 1549 | fprintf(STDERR, "\n %s: unsupported chunks (%d)%s", |
| 1550 | inname, unsupported_chunks, strict ? ": IGNORED --strict!" : ""); |
| 1551 | } |
| 1552 | # endif |
| 1553 | |
| 1554 | else if (warning_count > 0) |
| 1555 | { |
| 1556 | fprintf(STDERR, "\n %s: %d libpng warnings found", |
| 1557 | inname, warning_count); |
| 1558 | |
| 1559 | if (strict != 0) |
| 1560 | return (1); |
| 1561 | } |
| 1562 | |
| 1563 | pngtest_debug("Opening files for comparison"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1564 | if ((fpin = fopen(inname, "rb")) == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1565 | { |
| 1566 | fprintf(STDERR, "Could not find file %s\n", inname); |
| 1567 | return (1); |
| 1568 | } |
| 1569 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1570 | if ((fpout = fopen(outname, "rb")) == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1571 | { |
| 1572 | fprintf(STDERR, "Could not find file %s\n", outname); |
| 1573 | FCLOSE(fpin); |
| 1574 | return (1); |
| 1575 | } |
| 1576 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1577 | #ifdef PNG_WRITE_SUPPORTED /* else nothing was written */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1578 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1579 | int wrote_question = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1580 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1581 | for (;;) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1582 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1583 | png_size_t num_in, num_out; |
| 1584 | char inbuf[256], outbuf[256]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1585 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1586 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1587 | num_in = fread(inbuf, 1, sizeof inbuf, fpin); |
| 1588 | num_out = fread(outbuf, 1, sizeof outbuf, fpout); |
| 1589 | |
| 1590 | if (num_in != num_out) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1591 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1592 | fprintf(STDERR, "\nFiles %s and %s are of a different size\n", |
| 1593 | inname, outname); |
| 1594 | |
| 1595 | if (wrote_question == 0 && unsupported_chunks == 0) |
| 1596 | { |
| 1597 | fprintf(STDERR, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1598 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1599 | inname, PNG_ZBUF_SIZE); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1600 | fprintf(STDERR, |
| 1601 | "\n filtering heuristic (libpng default), compression"); |
| 1602 | fprintf(STDERR, |
| 1603 | " level (zlib default),\n and zlib version (%s)?\n\n", |
| 1604 | ZLIB_VERSION); |
| 1605 | wrote_question = 1; |
| 1606 | } |
| 1607 | |
| 1608 | FCLOSE(fpin); |
| 1609 | FCLOSE(fpout); |
| 1610 | |
| 1611 | if (strict != 0 && unsupported_chunks == 0) |
| 1612 | return (1); |
| 1613 | |
| 1614 | else |
| 1615 | return (0); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1616 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1617 | |
| 1618 | if (!num_in) |
| 1619 | break; |
| 1620 | |
| 1621 | if (memcmp(inbuf, outbuf, num_in)) |
| 1622 | { |
| 1623 | fprintf(STDERR, "\nFiles %s and %s are different\n", inname, |
| 1624 | outname); |
| 1625 | |
| 1626 | if (wrote_question == 0 && unsupported_chunks == 0) |
| 1627 | { |
| 1628 | fprintf(STDERR, |
| 1629 | " Was %s written with the same maximum IDAT chunk size (%d bytes),", |
| 1630 | inname, PNG_ZBUF_SIZE); |
| 1631 | fprintf(STDERR, |
| 1632 | "\n filtering heuristic (libpng default), compression"); |
| 1633 | fprintf(STDERR, |
| 1634 | " level (zlib default),\n and zlib version (%s)?\n\n", |
| 1635 | ZLIB_VERSION); |
| 1636 | wrote_question = 1; |
| 1637 | } |
| 1638 | |
| 1639 | FCLOSE(fpin); |
| 1640 | FCLOSE(fpout); |
| 1641 | |
| 1642 | /* NOTE: the unsupported_chunks escape is permitted here because |
| 1643 | * unsupported text chunk compression will result in the compression |
| 1644 | * mode being changed (to NONE) yet, in the test case, the result |
| 1645 | * can be exactly the same size! |
| 1646 | */ |
| 1647 | if (strict != 0 && unsupported_chunks == 0) |
| 1648 | return (1); |
| 1649 | |
| 1650 | else |
| 1651 | return (0); |
| 1652 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1653 | } |
| 1654 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1655 | #endif /* PNG_WRITE_SUPPORTED */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1656 | |
| 1657 | FCLOSE(fpin); |
| 1658 | FCLOSE(fpout); |
| 1659 | |
| 1660 | return (0); |
| 1661 | } |
| 1662 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1663 | /* Input and output filenames */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1664 | #ifdef RISCOS |
| 1665 | static PNG_CONST char *inname = "pngtest/png"; |
| 1666 | static PNG_CONST char *outname = "pngout/png"; |
| 1667 | #else |
| 1668 | static PNG_CONST char *inname = "pngtest.png"; |
| 1669 | static PNG_CONST char *outname = "pngout.png"; |
| 1670 | #endif |
| 1671 | |
| 1672 | int |
| 1673 | main(int argc, char *argv[]) |
| 1674 | { |
| 1675 | int multiple = 0; |
| 1676 | int ierror = 0; |
| 1677 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1678 | fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1679 | fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1680 | fprintf(STDERR, "%s", png_get_copyright(NULL)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1681 | /* Show the version of libpng used in building the library */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1682 | fprintf(STDERR, " library (%lu):%s", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1683 | (unsigned long)png_access_version_number(), |
| 1684 | png_get_header_version(NULL)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1685 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1686 | /* Show the version of libpng used in building the application */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1687 | fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1688 | PNG_HEADER_VERSION_STRING); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1689 | |
| 1690 | /* Do some consistency checking on the memory allocation settings, I'm |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1691 | * not sure this matters, but it is nice to know, the first of these |
| 1692 | * tests should be impossible because of the way the macros are set |
| 1693 | * in pngconf.h |
| 1694 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1695 | #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) |
| 1696 | fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n"); |
| 1697 | #endif |
| 1698 | /* I think the following can happen. */ |
| 1699 | #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K) |
| 1700 | fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n"); |
| 1701 | #endif |
| 1702 | |
| 1703 | if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING)) |
| 1704 | { |
| 1705 | fprintf(STDERR, |
| 1706 | "Warning: versions are different between png.h and png.c\n"); |
| 1707 | fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING); |
| 1708 | fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver); |
| 1709 | ++ierror; |
| 1710 | } |
| 1711 | |
| 1712 | if (argc > 1) |
| 1713 | { |
| 1714 | if (strcmp(argv[1], "-m") == 0) |
| 1715 | { |
| 1716 | multiple = 1; |
| 1717 | status_dots_requested = 0; |
| 1718 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1719 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1720 | else if (strcmp(argv[1], "-mv") == 0 || |
| 1721 | strcmp(argv[1], "-vm") == 0 ) |
| 1722 | { |
| 1723 | multiple = 1; |
| 1724 | verbose = 1; |
| 1725 | status_dots_requested = 1; |
| 1726 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1727 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1728 | else if (strcmp(argv[1], "-v") == 0) |
| 1729 | { |
| 1730 | verbose = 1; |
| 1731 | status_dots_requested = 1; |
| 1732 | inname = argv[2]; |
| 1733 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1734 | |
| 1735 | else if (strcmp(argv[1], "--strict") == 0) |
| 1736 | { |
| 1737 | status_dots_requested = 0; |
| 1738 | verbose = 1; |
| 1739 | inname = argv[2]; |
| 1740 | strict++; |
| 1741 | relaxed = 0; |
| 1742 | } |
| 1743 | |
| 1744 | else if (strcmp(argv[1], "--relaxed") == 0) |
| 1745 | { |
| 1746 | status_dots_requested = 0; |
| 1747 | verbose = 1; |
| 1748 | inname = argv[2]; |
| 1749 | strict = 0; |
| 1750 | relaxed++; |
| 1751 | } |
| 1752 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1753 | else |
| 1754 | { |
| 1755 | inname = argv[1]; |
| 1756 | status_dots_requested = 0; |
| 1757 | } |
| 1758 | } |
| 1759 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1760 | if (!multiple && argc == 3 + verbose) |
| 1761 | outname = argv[2 + verbose]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1762 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1763 | if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1764 | { |
| 1765 | fprintf(STDERR, |
| 1766 | "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n", |
| 1767 | argv[0], argv[0]); |
| 1768 | fprintf(STDERR, |
| 1769 | " reads/writes one PNG file (without -m) or multiple files (-m)\n"); |
| 1770 | fprintf(STDERR, |
| 1771 | " with -m %s is used as a temporary file\n", outname); |
| 1772 | exit(1); |
| 1773 | } |
| 1774 | |
| 1775 | if (multiple) |
| 1776 | { |
| 1777 | int i; |
| 1778 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1779 | int allocation_now = current_allocation; |
| 1780 | #endif |
| 1781 | for (i=2; i<argc; ++i) |
| 1782 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1783 | int kerror; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1784 | fprintf(STDERR, "\n Testing %s:", argv[i]); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1785 | kerror = test_one_file(argv[i], outname); |
| 1786 | if (kerror == 0) |
| 1787 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1788 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
| 1789 | int k; |
| 1790 | #endif |
| 1791 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1792 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
| 1793 | (unsigned long)zero_samples); |
| 1794 | #else |
| 1795 | fprintf(STDERR, " PASS\n"); |
| 1796 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1797 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1798 | for (k = 0; k<256; k++) |
| 1799 | if (filters_used[k]) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1800 | fprintf(STDERR, " Filter %d was used %lu times\n", |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1801 | k, (unsigned long)filters_used[k]); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1802 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1803 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1804 | if (tIME_chunk_present != 0) |
| 1805 | fprintf(STDERR, " tIME = %s\n", tIME_string); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1806 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1807 | tIME_chunk_present = 0; |
| 1808 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1809 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1810 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1811 | else |
| 1812 | { |
| 1813 | fprintf(STDERR, " FAIL\n"); |
| 1814 | ierror += kerror; |
| 1815 | } |
| 1816 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1817 | if (allocation_now != current_allocation) |
| 1818 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1819 | current_allocation - allocation_now); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1820 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1821 | if (current_allocation != 0) |
| 1822 | { |
| 1823 | memory_infop pinfo = pinformation; |
| 1824 | |
| 1825 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1826 | current_allocation); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1827 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1828 | while (pinfo != NULL) |
| 1829 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1830 | fprintf(STDERR, " %lu bytes at %x\n", |
| 1831 | (unsigned long)pinfo->size, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1832 | (unsigned int)pinfo->pointer); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1833 | pinfo = pinfo->next; |
| 1834 | } |
| 1835 | } |
| 1836 | #endif |
| 1837 | } |
| 1838 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1839 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", |
| 1840 | current_allocation); |
| 1841 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", |
| 1842 | maximum_allocation); |
| 1843 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", |
| 1844 | total_allocation); |
| 1845 | fprintf(STDERR, " Number of allocations: %10d\n", |
| 1846 | num_allocations); |
| 1847 | #endif |
| 1848 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1849 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1850 | else |
| 1851 | { |
| 1852 | int i; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1853 | for (i = 0; i<3; ++i) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1854 | { |
| 1855 | int kerror; |
| 1856 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1857 | int allocation_now = current_allocation; |
| 1858 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1859 | if (i == 1) |
| 1860 | status_dots_requested = 1; |
| 1861 | |
| 1862 | else if (verbose == 0) |
| 1863 | status_dots_requested = 0; |
| 1864 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1865 | if (i == 0 || verbose == 1 || ierror != 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1866 | fprintf(STDERR, "\n Testing %s:", inname); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1867 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1868 | kerror = test_one_file(inname, outname); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1869 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1870 | if (kerror == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1871 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1872 | if (verbose == 1 || i == 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1873 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1874 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1875 | int k; |
| 1876 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1877 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1878 | fprintf(STDERR, "\n PASS (%lu zero samples)\n", |
| 1879 | (unsigned long)zero_samples); |
| 1880 | #else |
| 1881 | fprintf(STDERR, " PASS\n"); |
| 1882 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1883 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1884 | for (k = 0; k<256; k++) |
| 1885 | if (filters_used[k]) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1886 | fprintf(STDERR, " Filter %d was used %lu times\n", |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1887 | k, (unsigned long)filters_used[k]); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1888 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1889 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1890 | if (tIME_chunk_present != 0) |
| 1891 | fprintf(STDERR, " tIME = %s\n", tIME_string); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1892 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1893 | } |
| 1894 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1895 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1896 | else |
| 1897 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1898 | if (verbose == 0 && i != 2) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1899 | fprintf(STDERR, "\n Testing %s:", inname); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1900 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1901 | fprintf(STDERR, " FAIL\n"); |
| 1902 | ierror += kerror; |
| 1903 | } |
| 1904 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1905 | if (allocation_now != current_allocation) |
| 1906 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1907 | current_allocation - allocation_now); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1908 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1909 | if (current_allocation != 0) |
| 1910 | { |
| 1911 | memory_infop pinfo = pinformation; |
| 1912 | |
| 1913 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1914 | current_allocation); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1915 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1916 | while (pinfo != NULL) |
| 1917 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1918 | fprintf(STDERR, " %lu bytes at %x\n", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1919 | (unsigned long)pinfo->size, (unsigned int)pinfo->pointer); |
| 1920 | pinfo = pinfo->next; |
| 1921 | } |
| 1922 | } |
| 1923 | #endif |
| 1924 | } |
| 1925 | #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG |
| 1926 | fprintf(STDERR, " Current memory allocation: %10d bytes\n", |
| 1927 | current_allocation); |
| 1928 | fprintf(STDERR, " Maximum memory allocation: %10d bytes\n", |
| 1929 | maximum_allocation); |
| 1930 | fprintf(STDERR, " Total memory allocation: %10d bytes\n", |
| 1931 | total_allocation); |
| 1932 | fprintf(STDERR, " Number of allocations: %10d\n", |
| 1933 | num_allocations); |
| 1934 | #endif |
| 1935 | } |
| 1936 | |
| 1937 | #ifdef PNGTEST_TIMING |
| 1938 | t_stop = (float)clock(); |
| 1939 | t_misc += (t_stop - t_start); |
| 1940 | t_start = t_stop; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1941 | fprintf(STDERR, " CPU time used = %.3f seconds", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1942 | (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1943 | fprintf(STDERR, " (decoding %.3f,\n", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1944 | t_decode/(float)CLOCKS_PER_SEC); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1945 | fprintf(STDERR, " encoding %.3f ,", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1946 | t_encode/(float)CLOCKS_PER_SEC); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1947 | fprintf(STDERR, " other %.3f seconds)\n\n", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1948 | t_misc/(float)CLOCKS_PER_SEC); |
| 1949 | #endif |
| 1950 | |
| 1951 | if (ierror == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1952 | fprintf(STDERR, " libpng passes test\n"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1953 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1954 | else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1955 | fprintf(STDERR, " libpng FAILS test\n"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1956 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1957 | return (int)(ierror != 0); |
| 1958 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1959 | #else |
| 1960 | int |
| 1961 | main(void) |
| 1962 | { |
| 1963 | fprintf(STDERR, |
| 1964 | " test ignored because libpng was not built with read support\n"); |
| 1965 | /* And skip this test */ |
| 1966 | return 77; |
| 1967 | } |
| 1968 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1969 | |
| 1970 | /* Generate a compiler error if there is an old png.h in the search path. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame^] | 1971 | typedef png_libpng_version_1_6_3 Your_png_h_is_not_version_1_6_3; |