Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1 | |
| 2 | /* pngvalid.c - validate libpng by constructing then reading png files. |
| 3 | * |
Glenn Randers-Pehrson | f0eda4e | 2010-10-15 15:01:57 -0500 | [diff] [blame] | 4 | * Last changed in libpng 1.5.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 5 | * Copyright (c) 2010 Glenn Randers-Pehrson |
| 6 | * Written by John C. Bowler |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 7 | * |
| 8 | * This code is released under the libpng license. |
| 9 | * For conditions of distribution and use, see the disclaimer |
| 10 | * and license in png.h |
| 11 | * |
| 12 | * NOTES: |
| 13 | * This is a C program that is intended to be linked against libpng. It |
| 14 | * generates bitmaps internally, stores them as PNG files (using the |
| 15 | * sequential write code) then reads them back (using the sequential |
| 16 | * read code) and validates that the result has the correct data. |
| 17 | * |
| 18 | * The program can be modified and extended to test the correctness of |
| 19 | * transformations performed by libpng. |
| 20 | */ |
| 21 | |
| 22 | #include "png.h" |
| 23 | #include "zlib.h" /* For crc32 */ |
| 24 | |
| 25 | #include <stdlib.h> /* For malloc */ |
| 26 | #include <string.h> /* For memcpy, memset */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 27 | #include <math.h> /* For floor */ |
| 28 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 29 | /* Unused formal parameter errors are removed using the following macro which is |
| 30 | * expected to have no bad effects on performance. |
| 31 | */ |
| 32 | #ifndef UNUSED |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 33 | # define UNUSED(param) param = param; |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 34 | #endif |
| 35 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 36 | /***************************** EXCEPTION HANDLING *****************************/ |
| 37 | #include "contrib/visupng/cexcept.h" |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 38 | struct png_store; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 39 | define_exception_type(struct png_store*); |
| 40 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 41 | /* The following are macros to reduce typing everywhere where the well known |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 42 | * name 'the_exception_context' must be defined. |
| 43 | */ |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 44 | #define anon_context(ps) struct exception_context *the_exception_context = \ |
| 45 | &(ps)->exception_context |
| 46 | #define context(ps,fault) anon_context(ps); png_store *fault |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 47 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 48 | /******************************* ERROR UTILITIES ******************************/ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 49 | static size_t safecat(char *buffer, size_t bufsize, size_t pos, |
| 50 | PNG_CONST char *cat) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 51 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 52 | while (pos < bufsize && cat != NULL && *cat != 0) |
| 53 | buffer[pos++] = *cat++; |
| 54 | |
| 55 | if (pos >= bufsize) |
| 56 | pos = bufsize-1; |
| 57 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 58 | buffer[pos] = 0; |
| 59 | return pos; |
| 60 | } |
| 61 | |
| 62 | static size_t safecatn(char *buffer, size_t bufsize, size_t pos, int n) |
| 63 | { |
| 64 | char number[64]; |
| 65 | sprintf(number, "%d", n); |
| 66 | return safecat(buffer, bufsize, pos, number); |
| 67 | } |
| 68 | |
| 69 | static size_t safecatd(char *buffer, size_t bufsize, size_t pos, double d, |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 70 | int precision) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 71 | { |
| 72 | char number[64]; |
| 73 | sprintf(number, "%.*f", precision, d); |
| 74 | return safecat(buffer, bufsize, pos, number); |
| 75 | } |
| 76 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 77 | static PNG_CONST char invalid[] = "invalid"; |
| 78 | static PNG_CONST char sep[] = ": "; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 79 | |
| 80 | /* NOTE: this is indexed by ln2(bit_depth)! */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 81 | static PNG_CONST char *bit_depths[8] = |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 82 | { |
| 83 | "1", "2", "4", "8", "16", invalid, invalid, invalid |
| 84 | }; |
| 85 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 86 | static PNG_CONST char *colour_types[8] = |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 87 | { |
| 88 | "greyscale", invalid, "truecolour", "indexed-colour", |
| 89 | "greyscale with alpha", invalid, "truecolour with alpha", invalid |
| 90 | }; |
| 91 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 92 | /* To get log-bit-depth from bit depth, returns 0 to 7 (7 on error). */ |
| 93 | static unsigned int |
| 94 | log2depth(png_byte bit_depth) |
| 95 | { |
| 96 | switch (bit_depth) |
| 97 | { |
| 98 | case 1: |
| 99 | return 0; |
| 100 | |
| 101 | case 2: |
| 102 | return 1; |
| 103 | |
| 104 | case 4: |
| 105 | return 2; |
| 106 | |
| 107 | case 8: |
| 108 | return 3; |
| 109 | |
| 110 | case 16: |
| 111 | return 4; |
| 112 | |
| 113 | default: |
| 114 | return 7; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* A numeric ID based on PNG file characteristics: */ |
| 119 | #define FILEID(col, depth, interlace) \ |
| 120 | ((png_uint_32)((col) + ((depth)<<3)) + ((interlace)<<8)) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 121 | #define COL_FROM_ID(id) ((png_byte)((id)& 0x7U)) |
| 122 | #define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU)) |
| 123 | #define INTERLACE_FROM_ID(id) ((int)(((id) >> 8) & 0xff)) |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 124 | |
| 125 | /* Utility to construct a standard name for a standard image. */ |
| 126 | static size_t |
| 127 | standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type, |
| 128 | int log_bit_depth, int interlace_type) |
| 129 | { |
| 130 | pos = safecat(buffer, bufsize, pos, colour_types[colour_type]); |
| 131 | pos = safecat(buffer, bufsize, pos, " "); |
| 132 | pos = safecat(buffer, bufsize, pos, bit_depths[log_bit_depth]); |
| 133 | pos = safecat(buffer, bufsize, pos, " bit"); |
| 134 | |
| 135 | if (interlace_type != PNG_INTERLACE_NONE) |
| 136 | pos = safecat(buffer, bufsize, pos, " interlaced"); |
| 137 | |
| 138 | return pos; |
| 139 | } |
| 140 | |
| 141 | static size_t |
| 142 | standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id) |
| 143 | { |
| 144 | return standard_name(buffer, bufsize, pos, COL_FROM_ID(id), |
| 145 | log2depth(DEPTH_FROM_ID(id)), INTERLACE_FROM_ID(id)); |
| 146 | } |
| 147 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 148 | /* Convenience API and defines to list valid formats. Note that 16 bit read and |
| 149 | * write support is required to do 16 bit read tests (we must be able to make a |
| 150 | * 16 bit image to test!) |
| 151 | */ |
| 152 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
| 153 | # define WRITE_BDHI 4 |
| 154 | # ifdef PNG_READ_16BIT_SUPPORTED |
| 155 | # define READ_BDHI 4 |
| 156 | # define DO_16BIT |
| 157 | # endif |
| 158 | #else |
| 159 | # define WRITE_BDHI 3 |
| 160 | #endif |
| 161 | #ifndef DO_16BIT |
| 162 | # define READ_BDHI 3 |
| 163 | #endif |
| 164 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 165 | static int |
| 166 | next_format(png_bytep colour_type, png_bytep bit_depth) |
| 167 | { |
| 168 | if (*bit_depth == 0) |
| 169 | { |
| 170 | *colour_type = 0, *bit_depth = 1; |
| 171 | return 1; |
| 172 | } |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 173 | |
| 174 | *bit_depth = (png_byte)(*bit_depth << 1); |
| 175 | |
| 176 | /* Palette images are restricted to 8 bit depth */ |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 177 | if (*bit_depth <= 8 |
| 178 | # ifdef DO_16BIT |
| 179 | || (*colour_type != 3 && *bit_depth <= 16) |
| 180 | # endif |
| 181 | ) |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 182 | return 1; |
| 183 | |
| 184 | /* Move to the next color type, or return 0 at the end. */ |
| 185 | switch (*colour_type) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 186 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 187 | case 0: |
| 188 | *colour_type = 2; |
| 189 | *bit_depth = 8; |
| 190 | return 1; |
| 191 | |
| 192 | case 2: |
| 193 | *colour_type = 3; |
| 194 | *bit_depth = 1; |
| 195 | return 1; |
| 196 | |
| 197 | case 3: |
| 198 | *colour_type = 4; |
| 199 | *bit_depth = 8; |
| 200 | return 1; |
| 201 | |
| 202 | case 4: |
| 203 | *colour_type = 6; |
| 204 | *bit_depth = 8; |
| 205 | return 1; |
| 206 | |
| 207 | default: |
| 208 | return 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 209 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 212 | static unsigned int |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 213 | sample(png_const_bytep row, png_byte colour_type, png_byte bit_depth, |
| 214 | png_uint_32 x, unsigned int sample) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 215 | { |
| 216 | png_uint_32 index, result; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 217 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 218 | /* Find a sample index for the desired sample: */ |
| 219 | x *= bit_depth; |
| 220 | index = x; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 221 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 222 | if ((colour_type & 1) == 0) /* !palette */ |
| 223 | { |
| 224 | if (colour_type & 2) |
| 225 | index *= 3, index += sample; /* Colour channels; select one */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 226 | |
| 227 | if (colour_type & 4) |
| 228 | index += x; /* Alpha channel */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* Return the sample from the row as an integer. */ |
| 232 | row += index >> 3; |
| 233 | result = *row; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 234 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 235 | if (bit_depth == 8) |
| 236 | return result; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 237 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 238 | else if (bit_depth > 8) |
| 239 | return (result << 8) + *++row; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 240 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 241 | /* Less than 8 bits per sample. */ |
| 242 | index &= 7; |
| 243 | return (result >> (8-index-bit_depth)) & ((1U<<bit_depth)-1); |
| 244 | } |
| 245 | |
| 246 | /*************************** BASIC PNG FILE WRITING ***************************/ |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 247 | /* A png_store takes data from the sequential writer or provides data |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 248 | * to the sequential reader. It can also store the result of a PNG |
| 249 | * write for later retrieval. |
| 250 | */ |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 251 | #define STORE_BUFFER_SIZE 500 /* arbitrary */ |
| 252 | typedef struct png_store_buffer |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 253 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 254 | struct png_store_buffer* prev; /* NOTE: stored in reverse order */ |
| 255 | png_byte buffer[STORE_BUFFER_SIZE]; |
| 256 | } png_store_buffer; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 257 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 258 | #define FILE_NAME_SIZE 64 |
| 259 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 260 | typedef struct png_store_file |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 261 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 262 | struct png_store_file* next; /* as many as you like... */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 263 | char name[FILE_NAME_SIZE]; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 264 | png_uint_32 id; /* must be correct (see FILEID) */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 265 | png_size_t datacount; /* In this (the last) buffer */ |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 266 | png_store_buffer data; /* Last buffer in file */ |
| 267 | } png_store_file; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 268 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 269 | /* The following is a pool of memory allocated by a single libpng read or write |
| 270 | * operation. |
| 271 | */ |
| 272 | typedef struct store_pool |
| 273 | { |
| 274 | struct png_store *store; /* Back pointer */ |
| 275 | struct store_memory *list; /* List of allocated memory */ |
| 276 | png_byte mark[4]; /* Before and after data */ |
| 277 | |
| 278 | /* Statistics for this run. */ |
| 279 | png_alloc_size_t max; /* Maximum single allocation */ |
| 280 | png_alloc_size_t current; /* Current allocation */ |
| 281 | png_alloc_size_t limit; /* Highest current allocation */ |
| 282 | png_alloc_size_t total; /* Total allocation */ |
| 283 | |
| 284 | /* Overall statistics (retained across successive runs). */ |
| 285 | png_alloc_size_t max_max; |
| 286 | png_alloc_size_t max_limit; |
| 287 | png_alloc_size_t max_total; |
| 288 | } store_pool; |
| 289 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 290 | typedef struct png_store |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 291 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 292 | /* For cexcept.h exception handling - simply store one of these; |
| 293 | * the context is a self pointer but it may point to a different |
| 294 | * png_store (in fact it never does in this program.) |
| 295 | */ |
| 296 | struct exception_context |
| 297 | exception_context; |
| 298 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 299 | unsigned int verbose :1; |
| 300 | unsigned int treat_warnings_as_errors :1; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 301 | unsigned int expect_error :1; |
| 302 | unsigned int expect_warning :1; |
| 303 | unsigned int saw_warning :1; |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 304 | unsigned int speed :1; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 305 | unsigned int progressive :1; /* use progressive read */ |
| 306 | unsigned int validated :1; /* used as a temporary flag */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 307 | int nerrors; |
| 308 | int nwarnings; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 309 | char test[64]; /* Name of test */ |
| 310 | char error[128]; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 311 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 312 | /* Read fields */ |
| 313 | png_structp pread; /* Used to read a saved file */ |
| 314 | png_infop piread; |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 315 | png_store_file* current; /* Set when reading */ |
| 316 | png_store_buffer* next; /* Set when reading */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 317 | png_size_t readpos; /* Position in *next */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 318 | png_byte* image; /* Buffer for reading interlaced images */ |
| 319 | size_t cb_image; /* Size of this buffer */ |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 320 | store_pool read_memory_pool; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 321 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 322 | /* Write fields */ |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 323 | png_store_file* saved; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 324 | png_structp pwrite; /* Used when writing a new file */ |
| 325 | png_infop piwrite; |
| 326 | png_size_t writepos; /* Position in .new */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 327 | char wname[FILE_NAME_SIZE]; |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 328 | png_store_buffer new; /* The end of the new PNG file being written. */ |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 329 | store_pool write_memory_pool; |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 330 | } png_store; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 331 | |
| 332 | /* Initialization and cleanup */ |
| 333 | static void |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 334 | store_pool_mark(png_byte *mark) |
| 335 | { |
| 336 | /* Generate a new mark. This uses a boring repeatable algorihtm and it is |
| 337 | * implemented here so that it gives the same set of numbers on every |
| 338 | * architecture. It's a linear congruential generator (Knuth or Sedgewick |
| 339 | * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and |
| 340 | * Hill, "The Art of Electronics". |
| 341 | */ |
| 342 | static png_uint_32 u0 = 0x12345678, u1 = 1; |
| 343 | |
| 344 | /* There are thirty three bits, the next bit in the sequence is bit-33 XOR |
| 345 | * bit-20. The top 1 bit is in u1, the bottom 32 are in u0. |
| 346 | */ |
| 347 | int i; |
| 348 | for (i=0; i<4; ++i) |
| 349 | { |
| 350 | /* First generate 8 new bits then shift them in at the end. */ |
| 351 | png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff; |
| 352 | u1 <<= 8; |
| 353 | u1 |= u0 >> 24; |
| 354 | u0 <<= 8; |
| 355 | u0 |= u; |
| 356 | *mark++ = (png_byte)u; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | store_pool_init(png_store *ps, store_pool *pool) |
| 362 | { |
| 363 | memset(pool, 0, sizeof *pool); |
| 364 | |
| 365 | pool->store = ps; |
| 366 | pool->list = NULL; |
| 367 | pool->max = pool->current = pool->limit = pool->total = 0; |
| 368 | pool->max_max = pool->max_limit = pool->max_total = 0; |
| 369 | store_pool_mark(pool->mark); |
| 370 | } |
| 371 | |
| 372 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 373 | store_init(png_store* ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 374 | { |
| 375 | memset(ps, 0, sizeof *ps); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 376 | init_exception_context(&ps->exception_context); |
| 377 | store_pool_init(ps, &ps->read_memory_pool); |
| 378 | store_pool_init(ps, &ps->write_memory_pool); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 379 | ps->verbose = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 380 | ps->treat_warnings_as_errors = 0; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 381 | ps->expect_error = 0; |
| 382 | ps->expect_warning = 0; |
| 383 | ps->saw_warning = 0; |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 384 | ps->speed = 0; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 385 | ps->progressive = 0; |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 386 | ps->validated = 0; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 387 | ps->nerrors = ps->nwarnings = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 388 | ps->pread = NULL; |
| 389 | ps->piread = NULL; |
| 390 | ps->saved = ps->current = NULL; |
| 391 | ps->next = NULL; |
| 392 | ps->readpos = 0; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 393 | ps->image = NULL; |
| 394 | ps->cb_image = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 395 | ps->pwrite = NULL; |
| 396 | ps->piwrite = NULL; |
| 397 | ps->writepos = 0; |
| 398 | ps->new.prev = NULL; |
| 399 | } |
| 400 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 401 | /* This somewhat odd function is used when reading an image to ensure that the |
| 402 | * buffer is big enough - this is why a png_structp is available. |
| 403 | */ |
| 404 | static void |
| 405 | store_ensure_image(png_store *ps, png_structp pp, size_t cb) |
| 406 | { |
| 407 | if (ps->cb_image < cb) |
| 408 | { |
| 409 | if (ps->image != NULL) |
| 410 | { |
| 411 | free(ps->image-1); |
| 412 | ps->cb_image = 0; |
| 413 | } |
| 414 | |
| 415 | /* The buffer is deliberately mis-aligned. */ |
| 416 | ps->image = malloc(cb+1); |
| 417 | if (ps->image == NULL) |
| 418 | png_error(pp, "OOM allocating image buffer"); |
| 419 | |
| 420 | ++(ps->image); |
| 421 | ps->cb_image = cb; |
| 422 | } |
| 423 | } |
| 424 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 425 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 426 | store_freebuffer(png_store_buffer* psb) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 427 | { |
| 428 | if (psb->prev) |
| 429 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 430 | store_freebuffer(psb->prev); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 431 | free(psb->prev); |
| 432 | psb->prev = NULL; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 437 | store_freenew(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 438 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 439 | store_freebuffer(&ps->new); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 440 | ps->writepos = 0; |
| 441 | } |
| 442 | |
| 443 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 444 | store_storenew(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 445 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 446 | png_store_buffer *pb; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 447 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 448 | if (ps->writepos != STORE_BUFFER_SIZE) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 449 | png_error(ps->pwrite, "invalid store call"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 450 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 451 | pb = malloc(sizeof *pb); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 452 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 453 | if (pb == NULL) |
| 454 | png_error(ps->pwrite, "store new: OOM"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 455 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 456 | *pb = ps->new; |
| 457 | ps->new.prev = pb; |
| 458 | ps->writepos = 0; |
| 459 | } |
| 460 | |
| 461 | static void |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 462 | store_freefile(png_store_file **ppf) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 463 | { |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 464 | if (*ppf != NULL) |
| 465 | { |
| 466 | store_freefile(&(*ppf)->next); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 467 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 468 | store_freebuffer(&(*ppf)->data); |
| 469 | (*ppf)->datacount = 0; |
| 470 | free(*ppf); |
| 471 | *ppf = NULL; |
| 472 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* Main interface to file storeage, after writing a new PNG file (see the API |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 476 | * below) call store_storefile to store the result with the given name and id. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 477 | */ |
| 478 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 479 | store_storefile(png_store *ps, png_uint_32 id) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 480 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 481 | png_store_file *pf = malloc(sizeof *pf); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 482 | if (pf == NULL) |
| 483 | png_error(ps->pwrite, "storefile: OOM"); |
| 484 | safecat(pf->name, sizeof pf->name, 0, ps->wname); |
| 485 | pf->id = id; |
| 486 | pf->data = ps->new; |
| 487 | pf->datacount = ps->writepos; |
| 488 | ps->new.prev = NULL; |
| 489 | ps->writepos = 0; |
| 490 | |
| 491 | /* And save it. */ |
| 492 | pf->next = ps->saved; |
| 493 | ps->saved = pf; |
| 494 | } |
| 495 | |
| 496 | /* Generate an error message (in the given buffer) */ |
| 497 | static size_t |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 498 | store_message(png_store *ps, png_structp pp, char *buffer, size_t bufsize, |
| 499 | size_t pos, PNG_CONST char *msg) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 500 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 501 | if (pp != NULL && pp == ps->pread) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 502 | { |
| 503 | /* Reading a file */ |
| 504 | pos = safecat(buffer, bufsize, pos, "read: "); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 505 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 506 | if (ps->current != NULL) |
| 507 | { |
| 508 | pos = safecat(buffer, bufsize, pos, ps->current->name); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 509 | pos = safecat(buffer, bufsize, pos, sep); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 510 | } |
| 511 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 512 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 513 | else if (pp != NULL && pp == ps->pwrite) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 514 | { |
| 515 | /* Writing a file */ |
| 516 | pos = safecat(buffer, bufsize, pos, "write: "); |
| 517 | pos = safecat(buffer, bufsize, pos, ps->wname); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 518 | pos = safecat(buffer, bufsize, pos, sep); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 519 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 520 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 521 | else |
| 522 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 523 | /* Neither reading nor writing (or a memory error in struct delete) */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 524 | pos = safecat(buffer, bufsize, pos, "pngvalid: "); |
| 525 | } |
| 526 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 527 | if (ps->test[0] != 0) |
| 528 | { |
| 529 | pos = safecat(buffer, bufsize, pos, ps->test); |
| 530 | pos = safecat(buffer, bufsize, pos, sep); |
| 531 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 532 | pos = safecat(buffer, bufsize, pos, msg); |
| 533 | return pos; |
| 534 | } |
| 535 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 536 | /* Log an error or warning - the relevant count is always incremented. */ |
| 537 | static void |
| 538 | store_log(png_store* ps, png_structp pp, png_const_charp message, int is_error) |
| 539 | { |
| 540 | /* The warning is copied to the error buffer if there are no errors and it is |
| 541 | * the first warning. The error is copied to the error buffer if it is the |
| 542 | * first error (overwriting any prior warnings). |
| 543 | */ |
| 544 | if (is_error ? (ps->nerrors)++ == 0 : |
| 545 | (ps->nwarnings)++ == 0 && ps->nerrors == 0) |
| 546 | store_message(ps, pp, ps->error, sizeof ps->error, 0, message); |
| 547 | |
| 548 | if (ps->verbose) |
| 549 | { |
| 550 | char buffer[256]; |
| 551 | size_t pos; |
| 552 | |
| 553 | if (is_error) |
| 554 | pos = safecat(buffer, sizeof buffer, 0, "error: "); |
| 555 | else |
| 556 | pos = safecat(buffer, sizeof buffer, 0, "warning: "); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 557 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 558 | store_message(ps, pp, buffer, sizeof buffer, pos, message); |
| 559 | fputs(buffer, stderr); |
| 560 | fputc('\n', stderr); |
| 561 | } |
| 562 | } |
| 563 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 564 | /* Functions to use as PNG callbacks. */ |
| 565 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 566 | store_error(png_structp pp, png_const_charp message) /* PNG_NORETURN */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 567 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 568 | png_store *ps = png_get_error_ptr(pp); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 569 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 570 | if (!ps->expect_error) |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 571 | store_log(ps, pp, message, 1/*error*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 572 | |
| 573 | /* And finally throw an exception. */ |
| 574 | { |
| 575 | struct exception_context *the_exception_context = &ps->exception_context; |
| 576 | Throw ps; |
| 577 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 581 | store_warning(png_structp pp, png_const_charp message) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 582 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 583 | png_store *ps = png_get_error_ptr(pp); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 584 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 585 | if (!ps->expect_warning) |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 586 | store_log(ps, pp, message, 0/*warning*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 587 | else |
| 588 | ps->saw_warning = 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 592 | store_write(png_structp pp, png_bytep pb, png_size_t st) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 593 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 594 | png_store *ps = png_get_io_ptr(pp); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 595 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 596 | if (ps->pwrite != pp) |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 597 | png_error(pp, "store state damaged"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 598 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 599 | while (st > 0) |
| 600 | { |
| 601 | size_t cb; |
| 602 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 603 | if (ps->writepos >= STORE_BUFFER_SIZE) |
| 604 | store_storenew(ps); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 605 | |
| 606 | cb = st; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 607 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 608 | if (cb > STORE_BUFFER_SIZE - ps->writepos) |
| 609 | cb = STORE_BUFFER_SIZE - ps->writepos; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 610 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 611 | memcpy(ps->new.buffer + ps->writepos, pb, cb); |
| 612 | pb += cb; |
| 613 | st -= cb; |
| 614 | ps->writepos += cb; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 619 | store_flush(png_structp pp) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 620 | { |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 621 | pp = pp; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 622 | /*DOES NOTHING*/ |
| 623 | } |
| 624 | |
| 625 | static size_t |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 626 | store_read_buffer_size(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 627 | { |
| 628 | /* Return the bytes available for read in the current buffer. */ |
| 629 | if (ps->next != &ps->current->data) |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 630 | return STORE_BUFFER_SIZE; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 631 | |
| 632 | return ps->current->datacount; |
| 633 | } |
| 634 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 635 | /* Return total bytes available for read. */ |
| 636 | static size_t |
| 637 | store_read_buffer_avail(png_store *ps) |
| 638 | { |
| 639 | if (ps->current != NULL && ps->next != NULL) |
| 640 | { |
| 641 | png_store_buffer *next = &ps->current->data; |
| 642 | size_t cbAvail = ps->current->datacount; |
| 643 | |
| 644 | while (next != ps->next && next != NULL) |
| 645 | { |
| 646 | next = next->prev; |
| 647 | cbAvail += STORE_BUFFER_SIZE; |
| 648 | } |
| 649 | |
| 650 | if (next != ps->next) |
| 651 | png_error(ps->pread, "buffer read error"); |
| 652 | |
| 653 | if (cbAvail > ps->readpos) |
| 654 | return cbAvail - ps->readpos; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 660 | static int |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 661 | store_read_buffer_next(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 662 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 663 | png_store_buffer *pbOld = ps->next; |
| 664 | png_store_buffer *pbNew = &ps->current->data; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 665 | if (pbOld != pbNew) |
| 666 | { |
| 667 | while (pbNew != NULL && pbNew->prev != pbOld) |
| 668 | pbNew = pbNew->prev; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 669 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 670 | if (pbNew != NULL) |
| 671 | { |
| 672 | ps->next = pbNew; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 673 | ps->readpos = 0; |
| 674 | return 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | png_error(ps->pread, "buffer lost"); |
| 678 | } |
| 679 | |
| 680 | return 0; /* EOF or error */ |
| 681 | } |
| 682 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 683 | /* Need separate implementation and callback to allow use of the same code |
| 684 | * during progressive read, where the io_ptr is set internally by libpng. |
| 685 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 686 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 687 | store_read_imp(png_store *ps, png_bytep pb, png_size_t st) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 688 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 689 | if (ps->current == NULL || ps->next == NULL) |
| 690 | png_error(ps->pread, "store state damaged"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 691 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 692 | while (st > 0) |
| 693 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 694 | size_t cbAvail = store_read_buffer_size(ps) - ps->readpos; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 695 | |
| 696 | if (cbAvail > 0) |
| 697 | { |
| 698 | if (cbAvail > st) cbAvail = st; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 699 | memcpy(pb, ps->next->buffer + ps->readpos, cbAvail); |
| 700 | st -= cbAvail; |
| 701 | pb += cbAvail; |
| 702 | ps->readpos += cbAvail; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 703 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 704 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 705 | else if (!store_read_buffer_next(ps)) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 706 | png_error(ps->pread, "read beyond end of file"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 710 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 711 | store_read(png_structp pp, png_bytep pb, png_size_t st) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 712 | { |
| 713 | png_store *ps = png_get_io_ptr(pp); |
| 714 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 715 | if (ps == NULL || ps->pread != pp) |
| 716 | png_error(pp, "bad store read call"); |
| 717 | |
| 718 | store_read_imp(ps, pb, st); |
| 719 | } |
| 720 | |
| 721 | static void |
| 722 | store_progressive_read(png_store *ps, png_structp pp, png_infop pi) |
| 723 | { |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 724 | /* Notice that a call to store_read will cause this function to fail because |
| 725 | * readpos will be set. |
| 726 | */ |
| 727 | if (ps->pread != pp || ps->current == NULL || ps->next == NULL) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 728 | png_error(pp, "store state damaged (progressive)"); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 729 | |
| 730 | do |
| 731 | { |
| 732 | if (ps->readpos != 0) |
| 733 | png_error(pp, "store_read called during progressive read"); |
| 734 | |
| 735 | png_process_data(pp, pi, ps->next->buffer, store_read_buffer_size(ps)); |
| 736 | } |
| 737 | while (store_read_buffer_next(ps)); |
| 738 | } |
| 739 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 740 | /***************************** MEMORY MANAGEMENT*** ***************************/ |
| 741 | /* A store_memory is simply the header for an allocated block of memory. The |
| 742 | * pointer returned to libpng is just after the end of the header block, the |
| 743 | * allocated memory is followed by a second copy of the 'mark'. |
| 744 | */ |
| 745 | typedef struct store_memory |
| 746 | { |
| 747 | store_pool *pool; /* Originating pool */ |
| 748 | struct store_memory *next; /* Singly linked list */ |
| 749 | png_alloc_size_t size; /* Size of memory allocated */ |
| 750 | png_byte mark[4]; /* ID marker */ |
| 751 | } store_memory; |
| 752 | |
| 753 | /* Handle a fatal error in memory allocation. This calls png_error if the |
| 754 | * libpng struct is non-NULL, else it outputs a message and returns. This means |
| 755 | * that a memory problem while libpng is running will abort (png_error) the |
| 756 | * handling of particular file while one in cleanup (after the destroy of the |
| 757 | * struct has returned) will simply keep going and free (or attempt to free) |
| 758 | * all the memory. |
| 759 | */ |
| 760 | static void |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 761 | store_pool_error(png_store *ps, png_structp pp, PNG_CONST char *msg) |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 762 | { |
| 763 | if (pp != NULL) |
| 764 | png_error(pp, msg); |
| 765 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 766 | /* Else we have to do it ourselves. png_error eventually calls store_log, |
| 767 | * above. store_log accepts a NULL png_structp - it just changes what gets |
| 768 | * output by store_message. |
| 769 | */ |
| 770 | store_log(ps, pp, msg, 1/*error*/); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void |
| 774 | store_memory_free(png_structp pp, store_pool *pool, store_memory *memory) |
| 775 | { |
| 776 | /* Note that pp may be NULL (see store_pool_delete below), the caller has |
| 777 | * found 'memory' in pool->list *and* unlinked this entry, so this is a valid |
| 778 | * pointer (for sure), but the contents may have been trashed. |
| 779 | */ |
| 780 | if (memory->pool != pool) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 781 | store_pool_error(pool->store, pp, "memory corrupted (pool)"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 782 | |
| 783 | else if (memcmp(memory->mark, pool->mark, sizeof memory->mark) != 0) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 784 | store_pool_error(pool->store, pp, "memory corrupted (start)"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 785 | |
| 786 | /* It should be safe to read the size field now. */ |
| 787 | else |
| 788 | { |
| 789 | png_alloc_size_t cb = memory->size; |
| 790 | |
| 791 | if (cb > pool->max) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 792 | store_pool_error(pool->store, pp, "memory corrupted (size)"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 793 | |
| 794 | else if (memcmp((png_bytep)(memory+1)+cb, pool->mark, sizeof pool->mark) |
| 795 | != 0) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 796 | store_pool_error(pool->store, pp, "memory corrupted (end)"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 797 | |
| 798 | /* Finally give the library a chance to find problems too: */ |
| 799 | else |
| 800 | { |
| 801 | pool->current -= cb; |
| 802 | free(memory); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | static void |
| 808 | store_pool_delete(png_store *ps, store_pool *pool) |
| 809 | { |
| 810 | if (pool->list != NULL) |
| 811 | { |
| 812 | fprintf(stderr, "%s: %s %s: memory lost (list follows):\n", ps->test, |
| 813 | pool == &ps->read_memory_pool ? "read" : "write", |
| 814 | pool == &ps->read_memory_pool ? (ps->current != NULL ? |
| 815 | ps->current->name : "unknown file") : ps->wname); |
| 816 | ++ps->nerrors; |
| 817 | |
| 818 | do |
| 819 | { |
| 820 | store_memory *next = pool->list; |
| 821 | pool->list = next->next; |
| 822 | next->next = NULL; |
| 823 | |
Glenn Randers-Pehrson | 9a75d99 | 2010-10-08 16:27:14 -0500 | [diff] [blame] | 824 | fprintf(stderr, "\t%lu bytes @ %p\n", |
| 825 | (unsigned long)next->size, next+1); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 826 | /* The NULL means this will always return, even if the memory is |
| 827 | * corrupted. |
| 828 | */ |
| 829 | store_memory_free(NULL, pool, next); |
| 830 | } |
| 831 | while (pool->list != NULL); |
| 832 | } |
| 833 | |
| 834 | /* And reset the other fields too for the next time. */ |
| 835 | if (pool->max > pool->max_max) pool->max_max = pool->max; |
| 836 | pool->max = 0; |
| 837 | if (pool->current != 0) /* unexpected internal error */ |
| 838 | fprintf(stderr, "%s: %s %s: memory counter mismatch (internal error)\n", |
| 839 | ps->test, pool == &ps->read_memory_pool ? "read" : "write", |
| 840 | pool == &ps->read_memory_pool ? (ps->current != NULL ? |
| 841 | ps->current->name : "unknown file") : ps->wname); |
| 842 | pool->current = 0; |
| 843 | if (pool->limit > pool->max_limit) pool->max_limit = pool->limit; |
| 844 | pool->limit = 0; |
| 845 | if (pool->total > pool->max_total) pool->max_total = pool->total; |
| 846 | pool->total = 0; |
| 847 | |
| 848 | /* Get a new mark too. */ |
| 849 | store_pool_mark(pool->mark); |
| 850 | } |
| 851 | |
| 852 | /* The memory callbacks: */ |
| 853 | static png_voidp |
| 854 | store_malloc(png_structp pp, png_alloc_size_t cb) |
| 855 | { |
| 856 | store_pool *pool = png_get_mem_ptr(pp); |
| 857 | store_memory *new = malloc(cb + (sizeof *new) + (sizeof pool->mark)); |
| 858 | |
| 859 | if (new != NULL) |
| 860 | { |
| 861 | if (cb > pool->max) pool->max = cb; |
| 862 | pool->current += cb; |
| 863 | if (pool->current > pool->limit) pool->limit = pool->current; |
| 864 | pool->total += cb; |
| 865 | |
| 866 | new->size = cb; |
| 867 | memcpy(new->mark, pool->mark, sizeof new->mark); |
| 868 | memcpy((png_byte*)(new+1) + cb, pool->mark, sizeof pool->mark); |
| 869 | new->pool = pool; |
| 870 | new->next = pool->list; |
| 871 | pool->list = new; |
| 872 | ++new; |
| 873 | } |
| 874 | else |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 875 | store_pool_error(pool->store, pp, "out of memory"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 876 | |
| 877 | return new; |
| 878 | } |
| 879 | |
| 880 | static void |
| 881 | store_free(png_structp pp, png_voidp memory) |
| 882 | { |
| 883 | store_pool *pool = png_get_mem_ptr(pp); |
| 884 | store_memory *this = memory, **test; |
| 885 | |
| 886 | /* First check that this 'memory' really is valid memory - it must be in the |
| 887 | * pool list. If it is use the shared memory_free function to free it. |
| 888 | */ |
| 889 | --this; |
| 890 | for (test = &pool->list; *test != this; test = &(*test)->next) |
| 891 | { |
| 892 | if (*test == NULL) |
| 893 | { |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 894 | store_pool_error(pool->store, pp, "bad pointer to free"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 895 | return; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* Unlink this entry, *test == this. */ |
| 900 | *test = this->next; |
| 901 | this->next = NULL; |
| 902 | store_memory_free(pp, pool, this); |
| 903 | } |
| 904 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 905 | /* Setup functions. */ |
| 906 | /* Cleanup when aborting a write or after storing the new file. */ |
| 907 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 908 | store_write_reset(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 909 | { |
| 910 | if (ps->pwrite != NULL) |
| 911 | { |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 912 | anon_context(ps); |
| 913 | |
| 914 | Try |
| 915 | png_destroy_write_struct(&ps->pwrite, &ps->piwrite); |
| 916 | |
| 917 | Catch_anonymous |
| 918 | { |
| 919 | /* memory corruption: continue. */ |
| 920 | } |
| 921 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 922 | ps->pwrite = NULL; |
| 923 | ps->piwrite = NULL; |
| 924 | } |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 925 | |
| 926 | /* And make sure that all the memory has been freed - this will output |
| 927 | * spurious errors in the case of memory corruption above, but this is safe. |
| 928 | */ |
| 929 | store_pool_delete(ps, &ps->write_memory_pool); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 930 | |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 931 | store_freenew(ps); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* The following is the main write function, it returns a png_struct and, |
| 935 | * optionally, a png)info suitable for writiing a new PNG file. Use |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 936 | * store_storefile above to record this file after it has been written. The |
| 937 | * returned libpng structures as destroyed by store_write_reset above. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 938 | */ |
| 939 | static png_structp |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 940 | set_store_for_write(png_store *ps, png_infopp ppi, |
| 941 | PNG_CONST char * volatile name) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 942 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 943 | anon_context(ps); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 944 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 945 | Try |
| 946 | { |
| 947 | if (ps->pwrite != NULL) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 948 | png_error(ps->pwrite, "write store already in use"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 949 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 950 | store_write_reset(ps); |
| 951 | safecat(ps->wname, sizeof ps->wname, 0, name); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 952 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 953 | /* Don't do the slow memory checks if doing a speed test. */ |
| 954 | if (ps->speed) |
| 955 | ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
| 956 | ps, store_error, store_warning); |
| 957 | else |
| 958 | ps->pwrite = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, |
| 959 | ps, store_error, store_warning, &ps->write_memory_pool, |
| 960 | store_malloc, store_free); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 961 | png_set_write_fn(ps->pwrite, ps, store_write, store_flush); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 962 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 963 | if (ppi != NULL) |
| 964 | *ppi = ps->piwrite = png_create_info_struct(ps->pwrite); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 965 | } |
| 966 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 967 | Catch_anonymous |
| 968 | return NULL; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 969 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 970 | return ps->pwrite; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 971 | } |
| 972 | |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame] | 973 | /* Cleanup when finished reading (either due to error or in the success case). |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 974 | */ |
| 975 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 976 | store_read_reset(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 977 | { |
| 978 | if (ps->pread != NULL) |
| 979 | { |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 980 | anon_context(ps); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 981 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 982 | Try |
| 983 | png_destroy_read_struct(&ps->pread, &ps->piread, NULL); |
| 984 | |
| 985 | Catch_anonymous |
| 986 | { |
| 987 | /*error already output: continue*/ |
| 988 | } |
| 989 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 990 | ps->pread = NULL; |
| 991 | ps->piread = NULL; |
| 992 | } |
| 993 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 994 | /* Always do this to be safe. */ |
| 995 | store_pool_delete(ps, &ps->read_memory_pool); |
| 996 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 997 | ps->current = NULL; |
| 998 | ps->next = NULL; |
| 999 | ps->readpos = 0; |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1000 | ps->validated = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1004 | store_read_set(png_store *ps, png_uint_32 id) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1005 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1006 | png_store_file *pf = ps->saved; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1007 | |
| 1008 | while (pf != NULL) |
| 1009 | { |
| 1010 | if (pf->id == id) |
| 1011 | { |
| 1012 | ps->current = pf; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1013 | ps->next = NULL; |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1014 | store_read_buffer_next(ps); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1015 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | pf = pf->next; |
| 1019 | } |
| 1020 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1021 | { |
| 1022 | size_t pos; |
| 1023 | char msg[FILE_NAME_SIZE+64]; |
| 1024 | |
| 1025 | pos = standard_name_from_id(msg, sizeof msg, 0, id); |
| 1026 | pos = safecat(msg, sizeof msg, pos, ": file not found"); |
| 1027 | png_error(ps->pread, msg); |
| 1028 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* The main interface for reading a saved file - pass the id number of the file |
| 1032 | * to retrieve. Ids must be unique or the earlier file will be hidden. The API |
| 1033 | * returns a png_struct and, optionally, a png_info. Both of these will be |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1034 | * destroyed by store_read_reset above. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1035 | */ |
| 1036 | static png_structp |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1037 | set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1038 | PNG_CONST char *name) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1039 | { |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1040 | /* Set the name for png_error */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1041 | safecat(ps->test, sizeof ps->test, 0, name); |
| 1042 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1043 | if (ps->pread != NULL) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1044 | png_error(ps->pread, "read store already in use"); |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1045 | |
| 1046 | store_read_reset(ps); |
| 1047 | |
| 1048 | /* Both the create APIs can return NULL if used in their default mode |
| 1049 | * (because there is no other way of handling an error because the jmp_buf by |
| 1050 | * default is stored in png_struct and that has not been allocated!) |
| 1051 | * However, given that store_error works correctly in these circumstances we |
| 1052 | * don't ever expect NULL in this program. |
| 1053 | */ |
| 1054 | if (ps->speed) |
| 1055 | ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps, |
| 1056 | store_error, store_warning); |
| 1057 | else |
| 1058 | ps->pread = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, ps, |
| 1059 | store_error, store_warning, &ps->read_memory_pool, store_malloc, |
| 1060 | store_free); |
| 1061 | |
| 1062 | if (ps->pread == NULL) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1063 | { |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1064 | struct exception_context *the_exception_context = &ps->exception_context; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1065 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 1066 | store_log(ps, NULL, "png_create_read_struct returned NULL (unexpected)", |
| 1067 | 1/*error*/); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1068 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1069 | Throw ps; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1070 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1071 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1072 | store_read_set(ps, id); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1073 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1074 | if (ppi != NULL) |
| 1075 | *ppi = ps->piread = png_create_info_struct(ps->pread); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1076 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1077 | return ps->pread; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 1080 | /* The overall cleanup of a store simply calls the above then removes all the |
| 1081 | * saved files. This does not delete the store itself. |
| 1082 | */ |
| 1083 | static void |
| 1084 | store_delete(png_store *ps) |
| 1085 | { |
| 1086 | store_write_reset(ps); |
| 1087 | store_read_reset(ps); |
| 1088 | store_freefile(&ps->saved); |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1089 | if (ps->image != NULL) |
| 1090 | { |
| 1091 | free(ps->image-1); |
| 1092 | ps->image = NULL; |
| 1093 | ps->cb_image = 0; |
| 1094 | } |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 1095 | } |
| 1096 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1097 | /*********************** PNG FILE MODIFICATION ON READ ************************/ |
| 1098 | /* Files may be modified on read. The following structure contains a complete |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1099 | * png_store together with extra members to handle modification and a special |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1100 | * read callback for libpng. To use this the 'modifications' field must be set |
| 1101 | * to a list of png_modification structures that actually perform the |
| 1102 | * modification, otherwise a png_modifier is functionally equivalent to a |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1103 | * png_store. There is a special read function, set_modifier_for_read, which |
| 1104 | * replaces set_store_for_read. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1105 | */ |
| 1106 | typedef struct png_modifier |
| 1107 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1108 | png_store this; /* I am a png_store */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1109 | struct png_modification *modifications; /* Changes to make */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1110 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1111 | enum modifier_state |
| 1112 | { |
| 1113 | modifier_start, /* Initial value */ |
| 1114 | modifier_signature, /* Have a signature */ |
| 1115 | modifier_IHDR /* Have an IHDR */ |
| 1116 | } state; /* My state */ |
| 1117 | |
| 1118 | /* Information from IHDR: */ |
| 1119 | png_byte bit_depth; /* From IHDR */ |
| 1120 | png_byte colour_type; /* From IHDR */ |
| 1121 | |
| 1122 | /* While handling PLTE, IDAT and IEND these chunks may be pended to allow |
| 1123 | * other chunks to be inserted. |
| 1124 | */ |
| 1125 | png_uint_32 pending_len; |
| 1126 | png_uint_32 pending_chunk; |
| 1127 | |
| 1128 | /* Test values */ |
| 1129 | double *gammas; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1130 | unsigned int ngammas; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1131 | |
| 1132 | /* Lowest sbit to test (libpng fails for sbit < 8) */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1133 | png_byte sbitlow; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1134 | |
| 1135 | /* Error control - these are the limits on errors accepted by the gamma tests |
| 1136 | * below. |
| 1137 | */ |
| 1138 | double maxout8; /* Maximum output value error */ |
Glenn Randers-Pehrson | 21b4b33 | 2010-08-18 07:12:38 -0500 | [diff] [blame] | 1139 | double maxabs8; /* Absolute sample error 0..1 */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1140 | double maxpc8; /* Percentage sample error 0..100% */ |
| 1141 | double maxout16; /* Maximum output value error */ |
| 1142 | double maxabs16; /* Absolute sample error 0..1 */ |
| 1143 | double maxpc16; /* Percentage sample error 0..100% */ |
| 1144 | |
| 1145 | /* Logged 8 and 16 bit errors ('output' values): */ |
| 1146 | double error_gray_2; |
| 1147 | double error_gray_4; |
| 1148 | double error_gray_8; |
| 1149 | double error_gray_16; |
| 1150 | double error_color_8; |
| 1151 | double error_color_16; |
| 1152 | |
| 1153 | /* Flags: */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1154 | /* Whether or not to interlace. */ |
| 1155 | int interlace_type :9; /* int, but must store '1' */ |
| 1156 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1157 | /* When to use the use_input_precision option: */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1158 | unsigned int use_input_precision :1; |
| 1159 | unsigned int use_input_precision_sbit :1; |
| 1160 | unsigned int use_input_precision_16to8 :1; |
| 1161 | unsigned int log :1; /* Log max error */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1162 | |
| 1163 | /* Buffer information, the buffer size limits the size of the chunks that can |
| 1164 | * be modified - they must fit (including header and CRC) into the buffer! |
| 1165 | */ |
| 1166 | size_t flush; /* Count of bytes to flush */ |
| 1167 | size_t buffer_count; /* Bytes in buffer */ |
| 1168 | size_t buffer_position; /* Position in buffer */ |
| 1169 | png_byte buffer[1024]; |
| 1170 | } png_modifier; |
| 1171 | |
| 1172 | static double abserr(png_modifier *pm, png_byte bit_depth) |
| 1173 | { |
| 1174 | return bit_depth == 16 ? pm->maxabs16 : pm->maxabs8; |
| 1175 | } |
| 1176 | |
| 1177 | static double pcerr(png_modifier *pm, png_byte bit_depth) |
| 1178 | { |
| 1179 | return (bit_depth == 16 ? pm->maxpc16 : pm->maxpc8) * .01; |
| 1180 | } |
| 1181 | |
| 1182 | static double outerr(png_modifier *pm, png_byte bit_depth) |
| 1183 | { |
| 1184 | /* There is a serious error in the 2 and 4 bit grayscale transform because |
| 1185 | * the gamma table value (8 bits) is simply shifted, not rouned, so the |
| 1186 | * error in 4 bit greyscale gamma is up to the value below. This is a hack |
| 1187 | * to allow pngvalid to succeed: |
| 1188 | */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1189 | if (bit_depth == 2) |
| 1190 | return .73182-.5; |
| 1191 | |
| 1192 | if (bit_depth == 4) |
| 1193 | return .90644-.5; |
| 1194 | |
| 1195 | if (bit_depth == 16) |
| 1196 | return pm->maxout16; |
| 1197 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1198 | return pm->maxout8; |
| 1199 | } |
| 1200 | |
| 1201 | /* This returns true if the test should be stopped now because it has already |
| 1202 | * failed and it is running silently. |
| 1203 | */ |
| 1204 | static int fail(png_modifier *pm) |
| 1205 | { |
| 1206 | return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 || |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1207 | (pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | static void |
| 1211 | modifier_init(png_modifier *pm) |
| 1212 | { |
| 1213 | memset(pm, 0, sizeof *pm); |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1214 | store_init(&pm->this); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1215 | pm->modifications = NULL; |
| 1216 | pm->state = modifier_start; |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1217 | pm->sbitlow = 1U; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1218 | pm->maxout8 = pm->maxpc8 = pm->maxabs8 = 0; |
| 1219 | pm->maxout16 = pm->maxpc16 = pm->maxabs16 = 0; |
| 1220 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0; |
| 1221 | pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1222 | pm->interlace_type = PNG_INTERLACE_NONE; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1223 | pm->use_input_precision = 0; |
| 1224 | pm->use_input_precision_sbit = 0; |
| 1225 | pm->use_input_precision_16to8 = 0; |
| 1226 | pm->log = 0; |
| 1227 | |
| 1228 | /* Rely on the memset for all the other fields - there are no pointers */ |
| 1229 | } |
| 1230 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 1231 | /* One modification structure must be provided for each chunk to be modified (in |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1232 | * fact more than one can be provided if multiple separate changes are desired |
| 1233 | * for a single chunk.) Modifications include adding a new chunk when a |
| 1234 | * suitable chunk does not exist. |
| 1235 | * |
| 1236 | * The caller of modify_fn will reset the CRC of the chunk and record 'modified' |
| 1237 | * or 'added' as appropriate if the modify_fn returns 1 (true). If the |
| 1238 | * modify_fn is NULL the chunk is simply removed. |
| 1239 | */ |
| 1240 | typedef struct png_modification |
| 1241 | { |
| 1242 | struct png_modification *next; |
| 1243 | png_uint_32 chunk; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1244 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1245 | /* If the following is NULL all matching chunks will be removed: */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1246 | int (*modify_fn)(struct png_modifier *pm, |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1247 | struct png_modification *me, int add); |
| 1248 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1249 | /* If the following is set to PLTE, IDAT or IEND and the chunk has not been |
| 1250 | * found and modified (and there is a modify_fn) the modify_fn will be called |
| 1251 | * to add the chunk before the relevant chunk. |
| 1252 | */ |
| 1253 | png_uint_32 add; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1254 | unsigned int modified :1; /* Chunk was modified */ |
| 1255 | unsigned int added :1; /* Chunk was added */ |
| 1256 | unsigned int removed :1; /* Chunk was removed */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1257 | } png_modification; |
| 1258 | |
| 1259 | static void modification_reset(png_modification *pmm) |
| 1260 | { |
| 1261 | if (pmm != NULL) |
| 1262 | { |
| 1263 | pmm->modified = 0; |
| 1264 | pmm->added = 0; |
| 1265 | pmm->removed = 0; |
| 1266 | modification_reset(pmm->next); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | static void |
| 1271 | modification_init(png_modification *pmm) |
| 1272 | { |
| 1273 | memset(pmm, 0, sizeof *pmm); |
| 1274 | pmm->next = NULL; |
| 1275 | pmm->chunk = 0; |
| 1276 | pmm->modify_fn = NULL; |
| 1277 | pmm->add = 0; |
| 1278 | modification_reset(pmm); |
| 1279 | } |
| 1280 | |
| 1281 | static void |
| 1282 | modifier_reset(png_modifier *pm) |
| 1283 | { |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1284 | store_read_reset(&pm->this); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1285 | pm->modifications = NULL; |
| 1286 | pm->state = modifier_start; |
| 1287 | pm->bit_depth = pm->colour_type = 0; |
| 1288 | pm->pending_len = pm->pending_chunk = 0; |
| 1289 | pm->flush = pm->buffer_count = pm->buffer_position = 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* Convenience macros. */ |
| 1293 | #define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d)) |
| 1294 | #define CHUNK_IHDR CHUNK(73,72,68,82) |
| 1295 | #define CHUNK_PLTE CHUNK(80,76,84,69) |
| 1296 | #define CHUNK_IDAT CHUNK(73,68,65,84) |
| 1297 | #define CHUNK_IEND CHUNK(73,69,78,68) |
| 1298 | #define CHUNK_cHRM CHUNK(99,72,82,77) |
| 1299 | #define CHUNK_gAMA CHUNK(103,65,77,65) |
| 1300 | #define CHUNK_sBIT CHUNK(115,66,73,84) |
| 1301 | #define CHUNK_sRGB CHUNK(115,82,71,66) |
| 1302 | |
| 1303 | /* The guts of modification are performed during a read. */ |
| 1304 | static void |
| 1305 | modifier_crc(png_bytep buffer) |
| 1306 | { |
| 1307 | /* Recalculate the chunk CRC - a complete chunk must be in |
| 1308 | * the buffer, at the start. |
| 1309 | */ |
| 1310 | uInt datalen = png_get_uint_32(buffer); |
| 1311 | png_save_uint_32(buffer+datalen+8, crc32(0L, buffer+4, datalen+4)); |
| 1312 | } |
| 1313 | |
| 1314 | static void |
| 1315 | modifier_setbuffer(png_modifier *pm) |
| 1316 | { |
| 1317 | modifier_crc(pm->buffer); |
| 1318 | pm->buffer_count = png_get_uint_32(pm->buffer)+12; |
| 1319 | pm->buffer_position = 0; |
| 1320 | } |
| 1321 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1322 | /* Separate the callback into the actual implementation (which is passed the |
| 1323 | * png_modifier explicitly) and the callback, which gets the modifier from the |
| 1324 | * png_struct. |
| 1325 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1326 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1327 | modifier_read_imp(png_modifier *pm, png_bytep pb, png_size_t st) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1328 | { |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1329 | while (st > 0) |
| 1330 | { |
| 1331 | size_t cb; |
| 1332 | png_uint_32 len, chunk; |
| 1333 | png_modification *mod; |
| 1334 | |
| 1335 | if (pm->buffer_position >= pm->buffer_count) switch (pm->state) |
| 1336 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1337 | static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; |
| 1338 | case modifier_start: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1339 | store_read_imp(&pm->this, pm->buffer, 8); /* size of signature. */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1340 | pm->buffer_count = 8; |
| 1341 | pm->buffer_position = 0; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1342 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1343 | if (memcmp(pm->buffer, sign, 8) != 0) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1344 | png_error(pm->this.pread, "invalid PNG file signature"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1345 | pm->state = modifier_signature; |
| 1346 | break; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1347 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1348 | case modifier_signature: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1349 | store_read_imp(&pm->this, pm->buffer, 13+12); /* size of IHDR */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1350 | pm->buffer_count = 13+12; |
| 1351 | pm->buffer_position = 0; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1352 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1353 | if (png_get_uint_32(pm->buffer) != 13 || |
| 1354 | png_get_uint_32(pm->buffer+4) != CHUNK_IHDR) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1355 | png_error(pm->this.pread, "invalid IHDR"); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1356 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1357 | /* Check the list of modifiers for modifications to the IHDR. */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1358 | mod = pm->modifications; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1359 | while (mod != NULL) |
| 1360 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1361 | if (mod->chunk == CHUNK_IHDR && mod->modify_fn && |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1362 | (*mod->modify_fn)(pm, mod, 0)) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1363 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1364 | mod->modified = 1; |
| 1365 | modifier_setbuffer(pm); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1366 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1367 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1368 | /* Ignore removal or add if IHDR! */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1369 | mod = mod->next; |
| 1370 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1371 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1372 | /* Cache information from the IHDR (the modified one.) */ |
| 1373 | pm->bit_depth = pm->buffer[8+8]; |
| 1374 | pm->colour_type = pm->buffer[8+8+1]; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1375 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1376 | pm->state = modifier_IHDR; |
| 1377 | pm->flush = 0; |
| 1378 | break; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1379 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1380 | case modifier_IHDR: |
| 1381 | default: |
| 1382 | /* Read a new chunk and process it until we see PLTE, IDAT or |
| 1383 | * IEND. 'flush' indicates that there is still some data to |
| 1384 | * output from the preceding chunk. |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1385 | */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1386 | if ((cb = pm->flush) > 0) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1387 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1388 | if (cb > st) cb = st; |
| 1389 | pm->flush -= cb; |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1390 | store_read_imp(&pm->this, pb, cb); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1391 | pb += cb; |
| 1392 | st -= cb; |
| 1393 | if (st <= 0) return; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1394 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1395 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1396 | /* No more bytes to flush, read a header, or handle a pending |
| 1397 | * chunk. |
| 1398 | */ |
| 1399 | if (pm->pending_chunk != 0) |
| 1400 | { |
| 1401 | png_save_uint_32(pm->buffer, pm->pending_len); |
| 1402 | png_save_uint_32(pm->buffer+4, pm->pending_chunk); |
| 1403 | pm->pending_len = 0; |
| 1404 | pm->pending_chunk = 0; |
| 1405 | } |
| 1406 | else |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1407 | store_read_imp(&pm->this, pm->buffer, 8); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1408 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1409 | pm->buffer_count = 8; |
| 1410 | pm->buffer_position = 0; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1411 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1412 | /* Check for something to modify or a terminator chunk. */ |
| 1413 | len = png_get_uint_32(pm->buffer); |
| 1414 | chunk = png_get_uint_32(pm->buffer+4); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1415 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1416 | /* Terminators first, they may have to be delayed for added |
| 1417 | * chunks |
| 1418 | */ |
| 1419 | if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT || |
| 1420 | chunk == CHUNK_IEND) |
| 1421 | { |
| 1422 | mod = pm->modifications; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1423 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1424 | while (mod != NULL) |
| 1425 | { |
| 1426 | if ((mod->add == chunk || |
| 1427 | (mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT)) && |
| 1428 | mod->modify_fn != NULL && !mod->modified && !mod->added) |
| 1429 | { |
| 1430 | /* Regardless of what the modify function does do not run |
| 1431 | * this again. |
| 1432 | */ |
| 1433 | mod->added = 1; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1434 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1435 | if ((*mod->modify_fn)(pm, mod, 1/*add*/)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1436 | { |
| 1437 | /* Reset the CRC on a new chunk */ |
| 1438 | if (pm->buffer_count > 0) |
| 1439 | modifier_setbuffer(pm); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1440 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1441 | else |
| 1442 | { |
| 1443 | pm->buffer_position = 0; |
| 1444 | mod->removed = 1; |
| 1445 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1446 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1447 | /* The buffer has been filled with something (we assume) |
| 1448 | * so output this. Pend the current chunk. |
| 1449 | */ |
| 1450 | pm->pending_len = len; |
| 1451 | pm->pending_chunk = chunk; |
| 1452 | break; /* out of while */ |
| 1453 | } |
| 1454 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1455 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1456 | mod = mod->next; |
| 1457 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1458 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1459 | /* Don't do any further processing if the buffer was modified - |
| 1460 | * otherwise the code will end up modifying a chunk that was just |
| 1461 | * added. |
| 1462 | */ |
| 1463 | if (mod != NULL) |
| 1464 | break; /* out of switch */ |
| 1465 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1466 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1467 | /* If we get to here then this chunk may need to be modified. To do |
| 1468 | * this is must be less than 1024 bytes in total size, otherwise |
| 1469 | * it just gets flushed. |
| 1470 | */ |
| 1471 | if (len+12 <= sizeof pm->buffer) |
| 1472 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1473 | store_read_imp(&pm->this, pm->buffer+pm->buffer_count, |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1474 | len+12-pm->buffer_count); |
| 1475 | pm->buffer_count = len+12; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1476 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1477 | /* Check for a modification, else leave it be. */ |
| 1478 | mod = pm->modifications; |
| 1479 | while (mod != NULL) |
| 1480 | { |
| 1481 | if (mod->chunk == chunk) |
| 1482 | { |
| 1483 | if (mod->modify_fn == NULL) |
| 1484 | { |
| 1485 | /* Remove this chunk */ |
| 1486 | pm->buffer_count = pm->buffer_position = 0; |
| 1487 | mod->removed = 1; |
| 1488 | break; /* Terminate the while loop */ |
| 1489 | } |
| 1490 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1491 | else if ((*mod->modify_fn)(pm, mod, 0)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1492 | { |
| 1493 | mod->modified = 1; |
| 1494 | /* The chunk may have been removed: */ |
| 1495 | if (pm->buffer_count == 0) |
| 1496 | { |
| 1497 | pm->buffer_position = 0; |
| 1498 | break; |
| 1499 | } |
| 1500 | modifier_setbuffer(pm); |
| 1501 | } |
| 1502 | } |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1503 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1504 | mod = mod->next; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | else |
| 1509 | pm->flush = len+12 - pm->buffer_count; /* data + crc */ |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 1510 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1511 | /* Take the data from the buffer (if there is any). */ |
| 1512 | break; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | /* Here to read from the modifier buffer (not directly from |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1516 | * the store, as in the flush case above.) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1517 | */ |
| 1518 | cb = pm->buffer_count - pm->buffer_position; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1519 | |
| 1520 | if (cb > st) |
| 1521 | cb = st; |
| 1522 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1523 | memcpy(pb, pm->buffer + pm->buffer_position, cb); |
| 1524 | st -= cb; |
| 1525 | pb += cb; |
| 1526 | pm->buffer_position += cb; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 1530 | /* The callback: */ |
| 1531 | static void |
| 1532 | modifier_read(png_structp pp, png_bytep pb, png_size_t st) |
| 1533 | { |
| 1534 | png_modifier *pm = png_get_io_ptr(pp); |
| 1535 | |
| 1536 | if (pm == NULL || pm->this.pread != pp) |
| 1537 | png_error(pp, "bad modifier_read call"); |
| 1538 | |
| 1539 | modifier_read_imp(pm, pb, st); |
| 1540 | } |
| 1541 | |
| 1542 | /* Like store_progressive_read but the data is getting changed as we go so we |
| 1543 | * need a local buffer. |
| 1544 | */ |
| 1545 | static void |
| 1546 | modifier_progressive_read(png_modifier *pm, png_structp pp, png_infop pi) |
| 1547 | { |
| 1548 | if (pm->this.pread != pp || pm->this.current == NULL || |
| 1549 | pm->this.next == NULL) |
| 1550 | png_error(pp, "store state damaged (progressive)"); |
| 1551 | |
| 1552 | /* This is another Horowitz and Hill random noise generator. In this case |
| 1553 | * the aim is to stress the progressive reader with truely horrible variable |
| 1554 | * buffer sizes in the range 1..500, so a sequence of 9 bit random numbers is |
| 1555 | * generated. We could probably just count from 1 to 32767 and get as good |
| 1556 | * a result. |
| 1557 | */ |
| 1558 | for (;;) |
| 1559 | { |
| 1560 | static png_uint_32 noise = 1; |
| 1561 | png_size_t cb, cbAvail; |
| 1562 | png_byte buffer[512]; |
| 1563 | |
| 1564 | /* Generate 15 more bits of stuff: */ |
| 1565 | noise = (noise << 9) | ((noise ^ (noise >> (9-5))) & 0x1ff); |
| 1566 | cb = noise & 0x1ff; |
| 1567 | |
| 1568 | /* Check that this number of bytes are available (in the current buffer.) |
| 1569 | * (This doesn't quite work - the modifier might delete a chunk; unlikely |
| 1570 | * but possible, it doesn't happen at present because the modifier only |
| 1571 | * adds chunks to standard images.) |
| 1572 | */ |
| 1573 | cbAvail = store_read_buffer_avail(&pm->this); |
| 1574 | if (pm->buffer_count > pm->buffer_position) |
| 1575 | cbAvail += pm->buffer_count - pm->buffer_position; |
| 1576 | |
| 1577 | if (cb > cbAvail) |
| 1578 | { |
| 1579 | /* Check for EOF: */ |
| 1580 | if (cbAvail == 0) |
| 1581 | break; |
| 1582 | |
| 1583 | cb = cbAvail; |
| 1584 | } |
| 1585 | |
| 1586 | modifier_read_imp(pm, buffer, cb); |
| 1587 | png_process_data(pp, pi, buffer, cb); |
| 1588 | } |
| 1589 | |
| 1590 | /* Check the invariants at the end (if this fails it's a problem in this |
| 1591 | * file!) |
| 1592 | */ |
| 1593 | if (pm->buffer_count > pm->buffer_position || |
| 1594 | pm->this.next != &pm->this.current->data || |
| 1595 | pm->this.readpos < pm->this.current->datacount) |
| 1596 | png_error(pp, "progressive read implementation error"); |
| 1597 | } |
| 1598 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1599 | /* Set up a modifier. */ |
| 1600 | static png_structp |
| 1601 | set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1602 | PNG_CONST char *name) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1603 | { |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1604 | /* Do this first so that the modifier fields are cleared even if an error |
| 1605 | * happens allocating the png_struct. No allocation is done here so no |
| 1606 | * cleanup is required. |
| 1607 | */ |
| 1608 | pm->state = modifier_start; |
| 1609 | pm->bit_depth = 0; |
| 1610 | pm->colour_type = 255; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1611 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1612 | pm->pending_len = 0; |
| 1613 | pm->pending_chunk = 0; |
| 1614 | pm->flush = 0; |
| 1615 | pm->buffer_count = 0; |
| 1616 | pm->buffer_position = 0; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1617 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 1618 | return set_store_for_read(&pm->this, ppi, id, name); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /***************************** STANDARD PNG FILES *****************************/ |
| 1622 | /* Standard files - write and save standard files. */ |
| 1623 | /* The standard files are constructed with rows which fit into a 1024 byte row |
| 1624 | * buffer. This makes allocation easier below. Further regardless of the file |
| 1625 | * format every file has 128 pixels (giving 1024 bytes for 64bpp formats). |
| 1626 | * |
| 1627 | * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1628 | * and with an ID derived from the colour type, bit depth and interlace type |
| 1629 | * as above (FILEID). |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1630 | */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1631 | |
| 1632 | /* The number of passes is related to the interlace type, there's no libpng API |
| 1633 | * to determine this so we need an inquiry function: |
| 1634 | */ |
| 1635 | static int |
| 1636 | npasses_from_interlace_type(png_structp pp, int interlace_type) |
| 1637 | { |
| 1638 | switch (interlace_type) |
| 1639 | { |
| 1640 | default: |
| 1641 | png_error(pp, "invalid interlace type"); |
| 1642 | |
| 1643 | case PNG_INTERLACE_NONE: |
| 1644 | return 1; |
| 1645 | |
| 1646 | case PNG_INTERLACE_ADAM7: |
| 1647 | return 7; |
| 1648 | } |
| 1649 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1650 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1651 | #define STD_WIDTH 128U |
| 1652 | #define STD_ROWMAX (STD_WIDTH*8U) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1653 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1654 | static unsigned int |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1655 | bit_size(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1656 | { |
| 1657 | switch (colour_type) |
| 1658 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1659 | case 0: return bit_depth; |
| 1660 | |
| 1661 | case 2: return 3*bit_depth; |
| 1662 | |
| 1663 | case 3: return bit_depth; |
| 1664 | |
| 1665 | case 4: return 2*bit_depth; |
| 1666 | |
| 1667 | case 6: return 4*bit_depth; |
| 1668 | |
| 1669 | default: png_error(pp, "invalid color type"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | static size_t |
| 1674 | standard_rowsize(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1675 | { |
| 1676 | return (STD_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8; |
| 1677 | } |
| 1678 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1679 | /* standard_wdith(pp, colour_type, bit_depth) current returns the same number |
| 1680 | * every time, so just use a macro: |
| 1681 | */ |
| 1682 | #define standard_width(pp, colour_type, bit_depth) STD_WIDTH |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1683 | |
| 1684 | static png_uint_32 |
| 1685 | standard_height(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1686 | { |
| 1687 | switch (bit_size(pp, colour_type, bit_depth)) |
| 1688 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1689 | case 1: |
| 1690 | case 2: |
| 1691 | case 4: |
| 1692 | return 1; /* Total of 128 pixels */ |
| 1693 | |
| 1694 | case 8: |
| 1695 | return 2; /* Total of 256 pixels/bytes */ |
| 1696 | |
| 1697 | case 16: |
| 1698 | return 512; /* Total of 65536 pixels */ |
| 1699 | |
| 1700 | case 24: |
| 1701 | case 32: |
| 1702 | return 512; /* 65536 pixels */ |
| 1703 | |
| 1704 | case 48: |
| 1705 | case 64: |
| 1706 | return 2048;/* 4 x 65536 pixels. */ |
| 1707 | |
| 1708 | default: |
| 1709 | return 0; /* Error, will be caught later */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1713 | /* So the maximum standard image size is: */ |
| 1714 | #define STD_IMAGEMAX (STD_ROWMAX * 2048) |
| 1715 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1716 | static void |
| 1717 | standard_row(png_structp pp, png_byte buffer[STD_ROWMAX], png_byte colour_type, |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1718 | png_byte bit_depth, png_uint_32 y) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1719 | { |
| 1720 | png_uint_32 v = y << 7; |
| 1721 | png_uint_32 i = 0; |
| 1722 | |
| 1723 | switch (bit_size(pp, colour_type, bit_depth)) |
| 1724 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1725 | case 1: |
| 1726 | while (i<128/8) buffer[i] = v & 0xff, v += 17, ++i; |
| 1727 | return; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1728 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1729 | case 2: |
| 1730 | while (i<128/4) buffer[i] = v & 0xff, v += 33, ++i; |
| 1731 | return; |
| 1732 | |
| 1733 | case 4: |
| 1734 | while (i<128/2) buffer[i] = v & 0xff, v += 65, ++i; |
| 1735 | return; |
| 1736 | |
| 1737 | case 8: |
| 1738 | /* 256 bytes total, 128 bytes in each row set as follows: */ |
| 1739 | while (i<128) buffer[i] = v & 0xff, ++v, ++i; |
| 1740 | return; |
| 1741 | |
| 1742 | case 16: |
| 1743 | /* Generate all 65536 pixel values in order, this includes the 8 bit GA |
| 1744 | * case as we as the 16 bit G case. |
| 1745 | */ |
| 1746 | while (i<128) |
| 1747 | buffer[2*i] = (v>>8) & 0xff, buffer[2*i+1] = v & 0xff, ++v, ++i; |
| 1748 | |
| 1749 | return; |
| 1750 | |
| 1751 | case 24: |
| 1752 | /* 65535 pixels, but rotate the values. */ |
| 1753 | while (i<128) |
| 1754 | { |
| 1755 | /* Three bytes per pixel, r, g, b, make b by r^g */ |
| 1756 | buffer[3*i+0] = (v >> 8) & 0xff; |
| 1757 | buffer[3*i+1] = v & 0xff; |
| 1758 | buffer[3*i+2] = ((v >> 8) ^ v) & 0xff; |
| 1759 | ++v; |
| 1760 | ++i; |
| 1761 | } |
| 1762 | |
| 1763 | return; |
| 1764 | |
| 1765 | case 32: |
| 1766 | /* 65535 pixels, r, g, b, a; just replicate */ |
| 1767 | while (i<128) |
| 1768 | { |
| 1769 | buffer[4*i+0] = (v >> 8) & 0xff; |
| 1770 | buffer[4*i+1] = v & 0xff; |
| 1771 | buffer[4*i+2] = (v >> 8) & 0xff; |
| 1772 | buffer[4*i+3] = v & 0xff; |
| 1773 | ++v; |
| 1774 | ++i; |
| 1775 | } |
| 1776 | |
| 1777 | return; |
| 1778 | |
| 1779 | case 48: |
| 1780 | /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at |
| 1781 | * each pixel, g increase by 257 (0x101) and 'b' by 0x1111: |
| 1782 | */ |
| 1783 | while (i<128) |
| 1784 | { |
| 1785 | png_uint_32 t = v++; |
| 1786 | buffer[6*i+0] = (t >> 8) & 0xff; |
| 1787 | buffer[6*i+1] = t & 0xff; |
| 1788 | t *= 257; |
| 1789 | buffer[6*i+2] = (t >> 8) & 0xff; |
| 1790 | buffer[6*i+3] = t & 0xff; |
| 1791 | t *= 17; |
| 1792 | buffer[6*i+4] = (t >> 8) & 0xff; |
| 1793 | buffer[6*i+5] = t & 0xff; |
| 1794 | ++i; |
| 1795 | } |
| 1796 | |
| 1797 | return; |
| 1798 | |
| 1799 | case 64: |
| 1800 | /* As above in the 32 bit case. */ |
| 1801 | while (i<128) |
| 1802 | { |
| 1803 | png_uint_32 t = v++; |
| 1804 | buffer[8*i+0] = (t >> 8) & 0xff; |
| 1805 | buffer[8*i+1] = t & 0xff; |
| 1806 | buffer[8*i+4] = (t >> 8) & 0xff; |
| 1807 | buffer[8*i+5] = t & 0xff; |
| 1808 | t *= 257; |
| 1809 | buffer[8*i+2] = (t >> 8) & 0xff; |
| 1810 | buffer[8*i+3] = t & 0xff; |
| 1811 | buffer[8*i+6] = (t >> 8) & 0xff; |
| 1812 | buffer[8*i+7] = t & 0xff; |
| 1813 | ++i; |
| 1814 | } |
| 1815 | return; |
| 1816 | |
| 1817 | default: |
| 1818 | break; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | png_error(pp, "internal error"); |
| 1822 | } |
| 1823 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1824 | /* This is just to do the right cast - could be changed to a function to check |
| 1825 | * 'bd' but there isn't much point. |
| 1826 | */ |
| 1827 | #define DEPTH(bd) ((png_byte)(1U << (bd))) |
| 1828 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1829 | static void |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1830 | make_standard_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, |
| 1831 | png_byte PNG_CONST bit_depth, int interlace_type, png_const_charp name) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1832 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1833 | context(ps, fault); |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1834 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1835 | Try |
| 1836 | { |
| 1837 | png_infop pi; |
| 1838 | png_structp pp = set_store_for_write(ps, &pi, name); |
| 1839 | png_uint_32 h; |
| 1840 | |
| 1841 | /* In the event of a problem return control to the Catch statement below |
| 1842 | * to do the clean up - it is not possible to 'return' directly from a Try |
| 1843 | * block. |
| 1844 | */ |
| 1845 | if (pp == NULL) |
| 1846 | Throw ps; |
| 1847 | |
| 1848 | h = standard_height(pp, colour_type, bit_depth); |
| 1849 | |
| 1850 | png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth), h, |
| 1851 | bit_depth, colour_type, interlace_type, |
| 1852 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 1853 | |
| 1854 | if (colour_type == 3) /* palette */ |
| 1855 | { |
| 1856 | unsigned int i = 0; |
| 1857 | png_color pal[256]; |
| 1858 | |
| 1859 | do |
| 1860 | pal[i].red = pal[i].green = pal[i].blue = (png_byte)i; |
| 1861 | while(++i < 256U); |
| 1862 | |
| 1863 | png_set_PLTE(pp, pi, pal, 256); |
| 1864 | } |
| 1865 | |
| 1866 | png_write_info(pp, pi); |
| 1867 | |
| 1868 | if (png_get_rowbytes(pp, pi) != |
| 1869 | standard_rowsize(pp, colour_type, bit_depth)) |
| 1870 | png_error(pp, "row size incorrect"); |
| 1871 | |
| 1872 | else |
| 1873 | { |
| 1874 | /* Somewhat confusingly this must be called *after* png_write_info |
| 1875 | * because, if it is called before, the information in *pp has not been |
| 1876 | * updated to reflect the interlaced image. |
| 1877 | */ |
| 1878 | int npasses = png_set_interlace_handling(pp); |
| 1879 | int pass; |
| 1880 | |
| 1881 | if (npasses != npasses_from_interlace_type(pp, interlace_type)) |
| 1882 | png_error(pp, "write: png_set_interlace_handling failed"); |
| 1883 | |
| 1884 | for (pass=1; pass<=npasses; ++pass) |
| 1885 | { |
| 1886 | png_uint_32 y; |
| 1887 | |
| 1888 | for (y=0; y<h; ++y) |
| 1889 | { |
| 1890 | png_byte buffer[STD_ROWMAX]; |
| 1891 | |
| 1892 | standard_row(pp, buffer, colour_type, bit_depth, y); |
| 1893 | png_write_row(pp, buffer); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | png_write_end(pp, pi); |
| 1899 | |
| 1900 | /* And store this under the appropriate id, then clean up. */ |
| 1901 | store_storefile(ps, FILEID(colour_type, bit_depth, interlace_type)); |
| 1902 | |
| 1903 | store_write_reset(ps); |
| 1904 | } |
| 1905 | |
| 1906 | Catch(fault) |
| 1907 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 1908 | /* Use the png_store retuned by the exception, this may help the compiler |
| 1909 | * because 'ps' is not used in this branch of the setjmp. Note that fault |
| 1910 | * and ps will always be the same value. |
| 1911 | */ |
| 1912 | store_write_reset(fault); |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | static void |
| 1917 | make_standard(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type, int bdlo, |
| 1918 | int PNG_CONST bdhi) |
| 1919 | { |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1920 | for (; bdlo <= bdhi; ++bdlo) |
| 1921 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1922 | int interlace_type; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1923 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1924 | for (interlace_type = PNG_INTERLACE_NONE; |
| 1925 | interlace_type < PNG_INTERLACE_LAST; ++interlace_type) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1926 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1927 | char name[FILE_NAME_SIZE]; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1928 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1929 | standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type); |
| 1930 | make_standard_image(ps, colour_type, DEPTH(bdlo), interlace_type, |
| 1931 | name); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1932 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | static void |
Glenn Randers-Pehrson | b4e6997 | 2010-07-30 10:35:38 -0500 | [diff] [blame] | 1937 | make_standard_images(png_store *ps) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1938 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1939 | /* This is in case of errors. */ |
| 1940 | safecat(ps->test, sizeof ps->test, 0, "make standard images"); |
| 1941 | |
| 1942 | /* Arguments are colour_type, low bit depth, high bit depth |
| 1943 | */ |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 1944 | make_standard(ps, 0, 0, WRITE_BDHI); |
| 1945 | make_standard(ps, 2, 3, WRITE_BDHI); |
| 1946 | make_standard(ps, 3, 0, 3/*palette: max 8 bits*/); |
| 1947 | make_standard(ps, 4, 3, WRITE_BDHI); |
| 1948 | make_standard(ps, 6, 3, WRITE_BDHI); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /* Tests - individual test cases */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1952 | /* Like 'make_standard' but errors are deliberately introduced into the calls |
| 1953 | * to ensure that they get detected - it should not be possible to write an |
| 1954 | * invalid image with libpng! |
| 1955 | */ |
| 1956 | static void |
| 1957 | sBIT0_error_fn(png_structp pp, png_infop pi) |
| 1958 | { |
| 1959 | /* 0 is invalid... */ |
| 1960 | png_color_8 bad; |
| 1961 | bad.red = bad.green = bad.blue = bad.gray = bad.alpha = 0; |
| 1962 | png_set_sBIT(pp, pi, &bad); |
| 1963 | } |
| 1964 | |
| 1965 | static void |
| 1966 | sBIT_error_fn(png_structp pp, png_infop pi) |
| 1967 | { |
| 1968 | png_byte bit_depth; |
| 1969 | png_color_8 bad; |
| 1970 | |
| 1971 | if (png_get_color_type(pp, pi) == PNG_COLOR_TYPE_PALETTE) |
| 1972 | bit_depth = 8; |
| 1973 | |
| 1974 | else |
| 1975 | bit_depth = png_get_bit_depth(pp, pi); |
| 1976 | |
| 1977 | /* Now we know the bit depth we can easily generate an invalid sBIT entry */ |
| 1978 | bad.red = bad.green = bad.blue = bad.gray = bad.alpha = |
| 1979 | (png_byte)(bit_depth+1); |
| 1980 | png_set_sBIT(pp, pi, &bad); |
| 1981 | } |
| 1982 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1983 | static PNG_CONST struct |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1984 | { |
| 1985 | void (*fn)(png_structp, png_infop); |
| 1986 | PNG_CONST char *msg; |
| 1987 | unsigned int warning :1; /* the error is a warning... */ |
| 1988 | } error_test[] = |
| 1989 | { |
| 1990 | { sBIT0_error_fn, "sBIT(0): failed to detect error", 1 }, |
| 1991 | { sBIT_error_fn, "sBIT(too big): failed to detect error", 1 }, |
| 1992 | }; |
| 1993 | |
| 1994 | static void |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 1995 | make_error(png_store* ps, png_byte PNG_CONST colour_type, png_byte bit_depth, |
| 1996 | int interlace_type, int test, png_const_charp name) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1997 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 1998 | context(ps, fault); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 1999 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2000 | Try |
| 2001 | { |
| 2002 | png_structp pp; |
| 2003 | png_infop pi; |
| 2004 | |
| 2005 | pp = set_store_for_write(ps, &pi, name); |
| 2006 | |
| 2007 | if (pp == NULL) |
| 2008 | Throw ps; |
| 2009 | |
| 2010 | png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth), |
| 2011 | standard_height(pp, colour_type, bit_depth), bit_depth, colour_type, |
| 2012 | interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 2013 | |
| 2014 | if (colour_type == 3) /* palette */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2015 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2016 | unsigned int i = 0; |
| 2017 | png_color pal[256]; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2018 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2019 | do |
| 2020 | pal[i].red = pal[i].green = pal[i].blue = (png_byte)i; |
| 2021 | while(++i < 256U); |
| 2022 | |
| 2023 | png_set_PLTE(pp, pi, pal, 256); |
| 2024 | } |
| 2025 | |
| 2026 | /* Time for a few errors, these are in various optional chunks, the |
| 2027 | * standard tests test the standard chunks pretty well. |
| 2028 | */ |
| 2029 | Try |
| 2030 | { |
| 2031 | /* Expect this to throw: */ |
| 2032 | ps->expect_error = !error_test[test].warning; |
| 2033 | ps->expect_warning = error_test[test].warning; |
| 2034 | ps->saw_warning = 0; |
| 2035 | error_test[test].fn(pp, pi); |
| 2036 | |
| 2037 | /* Normally the error is only detected here: */ |
| 2038 | png_write_info(pp, pi); |
| 2039 | |
| 2040 | /* And handle the case where it was only a warning: */ |
| 2041 | if (ps->expect_warning && ps->saw_warning) |
| 2042 | Throw ps; |
| 2043 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2044 | /* If we get here there is a problem, we have success - no error or |
| 2045 | * no warning - when we shouldn't have success. Log an error. |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2046 | */ |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2047 | store_log(ps, pp, error_test[test].msg, 1/*error*/); |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | Catch (fault) |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2051 | ps = fault; /* expected exit, make sure ps is not clobbered */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2052 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2053 | /* And clear these flags */ |
| 2054 | ps->expect_error = 0; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2055 | ps->expect_warning = 0; |
| 2056 | |
| 2057 | /* Now write the whole image, just to make sure that the detected, or |
| 2058 | * undetected, errro has not created problems inside libpng. |
| 2059 | */ |
| 2060 | if (png_get_rowbytes(pp, pi) != |
| 2061 | standard_rowsize(pp, colour_type, bit_depth)) |
| 2062 | png_error(pp, "row size incorrect"); |
| 2063 | |
| 2064 | else |
| 2065 | { |
| 2066 | png_uint_32 h = standard_height(pp, colour_type, bit_depth); |
| 2067 | int npasses = png_set_interlace_handling(pp); |
| 2068 | int pass; |
| 2069 | |
| 2070 | if (npasses != npasses_from_interlace_type(pp, interlace_type)) |
| 2071 | png_error(pp, "write: png_set_interlace_handling failed"); |
| 2072 | |
| 2073 | for (pass=1; pass<=npasses; ++pass) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2074 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2075 | png_uint_32 y; |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 2076 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2077 | for (y=0; y<h; ++y) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2078 | { |
| 2079 | png_byte buffer[STD_ROWMAX]; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2080 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2081 | standard_row(pp, buffer, colour_type, bit_depth, y); |
| 2082 | png_write_row(pp, buffer); |
| 2083 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2084 | } |
| 2085 | } |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2086 | |
| 2087 | png_write_end(pp, pi); |
| 2088 | |
| 2089 | /* The following deletes the file that was just written. */ |
| 2090 | store_write_reset(ps); |
| 2091 | } |
| 2092 | |
| 2093 | Catch(fault) |
| 2094 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2095 | store_write_reset(fault); |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | static int |
| 2100 | make_errors(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type, |
| 2101 | int bdlo, int PNG_CONST bdhi) |
| 2102 | { |
| 2103 | for (; bdlo <= bdhi; ++bdlo) |
| 2104 | { |
| 2105 | int interlace_type; |
| 2106 | |
| 2107 | for (interlace_type = PNG_INTERLACE_NONE; |
| 2108 | interlace_type < PNG_INTERLACE_LAST; ++interlace_type) |
| 2109 | { |
| 2110 | unsigned int test; |
| 2111 | char name[FILE_NAME_SIZE]; |
| 2112 | |
| 2113 | standard_name(name, sizeof name, 0, colour_type, bdlo, interlace_type); |
| 2114 | |
| 2115 | for (test=0; test<(sizeof error_test)/(sizeof error_test[0]); ++test) |
| 2116 | { |
| 2117 | make_error(&pm->this, colour_type, DEPTH(bdlo), interlace_type, |
| 2118 | test, name); |
| 2119 | |
| 2120 | if (fail(pm)) |
| 2121 | return 0; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | return 1; /* keep going */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static void |
| 2130 | perform_error_test(png_modifier *pm) |
| 2131 | { |
| 2132 | /* Need to do this here because we just write in this test. */ |
| 2133 | safecat(pm->this.test, sizeof pm->this.test, 0, "error test"); |
| 2134 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2135 | if (!make_errors(pm, 0, 0, WRITE_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2136 | return; |
| 2137 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2138 | if (!make_errors(pm, 2, 3, WRITE_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2139 | return; |
| 2140 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2141 | if (!make_errors(pm, 3, 0, 3)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2142 | return; |
| 2143 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2144 | if (!make_errors(pm, 4, 3, WRITE_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2145 | return; |
| 2146 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2147 | if (!make_errors(pm, 6, 3, WRITE_BDHI)) |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2148 | return; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2149 | } |
| 2150 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2151 | /* Because we want to use the same code in both the progressive reader and the |
| 2152 | * sequential reader it is necessary to deal with the fact that the progressive |
| 2153 | * reader callbacks only have one parameter (png_get_progressive_ptr()), so this |
| 2154 | * must contain all the test parameters and all the local variables directly |
| 2155 | * accessible to the sequential reader implementation. |
| 2156 | * |
| 2157 | * The techinque adopted is to reinvent part of what Dijkstra termed a |
| 2158 | * 'display'; an array of pointers to the stack frames of enclosing functions so |
| 2159 | * that a nested function definition can access the local (C auto) variables of |
| 2160 | * the functions that contain its definition. In fact C provides the first |
| 2161 | * pointer (the local variables - the stack frame pointer) and the last (the |
| 2162 | * global variables - the BCPL global vector typically implemented as global |
| 2163 | * addresses), this code requires one more pointer to make the display - the |
| 2164 | * local variables (and function call parameters) of the function that actually |
| 2165 | * invokes either the progressive or sequential reader. |
| 2166 | * |
| 2167 | * Perhaps confusingly this technique is confounded with classes - the |
| 2168 | * 'standard_display' defined here is sub-classed as the 'gamma_display' below. |
| 2169 | * A gamma_display is a standard_display, taking advantage of the ANSI-C |
| 2170 | * requirement that the pointer to the first member of a structure must be the |
| 2171 | * same as the pointer to the structure. This allows us to reuse standard_ |
| 2172 | * functions in the gamma test code; something that could not be done with |
| 2173 | * nested funtions! |
| 2174 | */ |
| 2175 | typedef struct standard_display |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2176 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2177 | png_store* ps; /* Test parameters (passed to the function) */ |
| 2178 | png_byte colour_type; |
| 2179 | png_byte bit_depth; |
| 2180 | int interlace_type; |
| 2181 | png_uint_32 id; /* Calculated file ID */ |
| 2182 | png_uint_32 w; /* Width of image */ |
| 2183 | png_uint_32 h; /* Height of image */ |
| 2184 | int npasses; /* Number of interlaced passes */ |
| 2185 | size_t cbRow; /* Bytes in a row of the output image. */ |
| 2186 | } standard_display; |
| 2187 | |
| 2188 | static void |
| 2189 | standard_display_init(standard_display *dp, png_store* ps, png_byte colour_type, |
| 2190 | png_byte bit_depth, int interlace_type) |
| 2191 | { |
| 2192 | dp->ps = ps; |
| 2193 | dp->colour_type = colour_type; |
| 2194 | dp->bit_depth = bit_depth; |
| 2195 | dp->interlace_type = interlace_type; |
| 2196 | dp->id = FILEID(colour_type, bit_depth, interlace_type); |
| 2197 | dp->w = 0; |
| 2198 | dp->h = 0; |
| 2199 | dp->npasses = 0; |
| 2200 | dp->cbRow = 0; |
| 2201 | } |
| 2202 | |
| 2203 | /* By passing a 'standard_display' the progressive callbacks can be used |
| 2204 | * directly by the sequential code, the functions suffixed _imp are the |
| 2205 | * implementations, the functions without the suffix are the callbacks. |
| 2206 | * |
| 2207 | * The code for the info callback is split into two because this callback calls |
| 2208 | * png_read_update_info or png_start_read_image and what gets called depends on |
| 2209 | * whether the info needs updating (we want to test both calls in pngvalid.) |
| 2210 | */ |
| 2211 | static void |
| 2212 | standard_info_part1(standard_display *dp, png_structp pp, png_infop pi) |
| 2213 | { |
| 2214 | if (png_get_bit_depth(pp, pi) != dp->bit_depth) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2215 | png_error(pp, "validate: bit depth changed"); |
| 2216 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2217 | if (png_get_color_type(pp, pi) != dp->colour_type) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2218 | png_error(pp, "validate: color type changed"); |
| 2219 | |
| 2220 | if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE) |
| 2221 | png_error(pp, "validate: filter type changed"); |
| 2222 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2223 | if (png_get_interlace_type(pp, pi) != dp->interlace_type) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2224 | png_error(pp, "validate: interlacing changed"); |
| 2225 | |
| 2226 | if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE) |
| 2227 | png_error(pp, "validate: compression type changed"); |
| 2228 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2229 | dp->w = png_get_image_width(pp, pi); |
| 2230 | |
| 2231 | if (dp->w != standard_width(pp, dp->colour_type, dp->bit_depth)) |
| 2232 | png_error(pp, "validate: image width changed"); |
| 2233 | |
| 2234 | dp->h = png_get_image_height(pp, pi); |
| 2235 | |
| 2236 | if (dp->h != standard_height(pp, dp->colour_type, dp->bit_depth)) |
| 2237 | png_error(pp, "validate: image height changed"); |
| 2238 | |
| 2239 | /* Important: this is validating the value *before* any transforms have been |
| 2240 | * put in place. It doesn't matter for the standard tests, where there are |
| 2241 | * no transforms, it does for other tests where rowbytes may change after |
| 2242 | * png_read_update_info. |
| 2243 | */ |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 2244 | if (png_get_rowbytes(pp, pi) != |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2245 | standard_rowsize(pp, dp->colour_type, dp->bit_depth)) |
| 2246 | png_error(pp, "validate: row size changed"); |
| 2247 | |
| 2248 | if (dp->colour_type == 3) /* palette */ |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2249 | { |
| 2250 | png_colorp pal; |
| 2251 | int num; |
| 2252 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2253 | /* This could be passed in but isn't - the values set above when the |
| 2254 | * standard images were made are just repeated here. |
| 2255 | */ |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2256 | if (png_get_PLTE(pp, pi, &pal, &num) & PNG_INFO_PLTE) |
| 2257 | { |
| 2258 | int i; |
| 2259 | |
| 2260 | if (num != 256) |
| 2261 | png_error(pp, "validate: color type 3 PLTE chunk size changed"); |
| 2262 | |
| 2263 | for (i=0; i<num; ++i) |
| 2264 | if (pal[i].red != i || pal[i].green != i || pal[i].blue != i) |
| 2265 | png_error(pp, "validate: color type 3 PLTE chunk changed"); |
| 2266 | } |
| 2267 | |
| 2268 | else |
| 2269 | png_error(pp, "validate: missing PLTE with color type 3"); |
| 2270 | } |
| 2271 | |
| 2272 | /* Read the number of passes - expected to match the value used when |
| 2273 | * creating the image (interlaced or not). This has the side effect of |
| 2274 | * turning ono interlace handling. |
| 2275 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2276 | dp->npasses = png_set_interlace_handling(pp); |
| 2277 | |
| 2278 | if (dp->npasses != npasses_from_interlace_type(pp, dp->interlace_type)) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2279 | png_error(pp, "validate: file changed interlace type"); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2280 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2281 | /* Caller calls png_read_update_info or png_start_read_image now, then calls |
| 2282 | * part2. |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2283 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2284 | } |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2285 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2286 | /* This must be called *after* the png_read_update_info call to get the correct |
| 2287 | * 'rowbytes' value, otherwise png_get_rowbytes will refer to the untransformed |
| 2288 | * image. |
| 2289 | */ |
| 2290 | static void |
| 2291 | standard_info_part2(standard_display *dp, png_structp pp, png_infop pi, |
| 2292 | int nImages) |
| 2293 | { |
| 2294 | /* Record cbRow now that it can be found. */ |
| 2295 | dp->cbRow = png_get_rowbytes(pp, pi); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2296 | |
| 2297 | /* Then ensure there is enough space for the output image(s). */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2298 | store_ensure_image(dp->ps, pp, nImages * dp->cbRow * dp->h); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2302 | standard_info_imp(standard_display *dp, png_structp pp, png_infop pi, |
| 2303 | int nImages) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2304 | { |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2305 | /* Note that the validation routine has the side effect of turning on |
| 2306 | * interlace handling in the subsequent code. |
| 2307 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2308 | standard_info_part1(dp, pp, pi); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2309 | |
| 2310 | /* And the info callback has to call this (or png_read_update_info - see |
| 2311 | * below in the png_modifier code for that variant. |
| 2312 | */ |
| 2313 | png_start_read_image(pp); |
| 2314 | |
| 2315 | /* Validate the height, width and rowbytes plus ensure that sufficient buffer |
| 2316 | * exists for decoding the image. |
| 2317 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2318 | standard_info_part2(dp, pp, pi, nImages); |
| 2319 | } |
| 2320 | |
| 2321 | static void |
| 2322 | standard_info(png_structp pp, png_infop pi) |
| 2323 | { |
| 2324 | standard_display *dp = png_get_progressive_ptr(pp); |
| 2325 | |
| 2326 | /* Call with nImages==1 because the progressive reader can only produce one |
| 2327 | * image. |
| 2328 | */ |
| 2329 | standard_info_imp(dp, pp, pi, 1/*only one image*/); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | static void |
| 2333 | progressive_row(png_structp pp, png_bytep new_row, png_uint_32 y, int pass) |
| 2334 | { |
| 2335 | UNUSED(pass); |
| 2336 | |
| 2337 | /* When handling interlacing some rows will be absent in each pass, the |
| 2338 | * callback still gets called, but with a NULL pointer. We need our own |
| 2339 | * 'cbRow', but we can't call png_get_rowbytes because we got no info |
| 2340 | * structure. |
| 2341 | */ |
| 2342 | if (new_row != NULL) |
| 2343 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2344 | PNG_CONST standard_display *dp = png_get_progressive_ptr(pp); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2345 | |
| 2346 | /* Combine the new row into the old: */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2347 | png_progressive_combine_row(pp, dp->ps->image + y * dp->cbRow, new_row); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | static void |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2352 | sequential_row(standard_display *dp, png_structp pp, png_infop pi, |
| 2353 | PNG_CONST png_bytep pImage, PNG_CONST png_bytep pDisplay) |
| 2354 | { |
| 2355 | PNG_CONST int npasses = dp->npasses; |
| 2356 | PNG_CONST png_uint_32 h = dp->h; |
| 2357 | PNG_CONST size_t cbRow = dp->cbRow; |
| 2358 | int pass; |
| 2359 | |
| 2360 | for (pass=1; pass <= npasses; ++pass) |
| 2361 | { |
| 2362 | png_uint_32 y; |
| 2363 | png_bytep pRow1 = pImage; |
| 2364 | png_bytep pRow2 = pDisplay; |
| 2365 | |
| 2366 | for (y=0; y<h; ++y) |
| 2367 | { |
| 2368 | png_read_row(pp, pRow1, pRow2); |
| 2369 | |
| 2370 | if (pRow1 != NULL) |
| 2371 | pRow1 += cbRow; |
| 2372 | |
| 2373 | if (pRow2 != NULL) |
| 2374 | pRow2 += cbRow; |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | /* And finish the read operation (only really necessary if the caller wants |
| 2379 | * to find additional data in png_info from chunks after the last IDAT.) |
| 2380 | */ |
| 2381 | png_read_end(pp, pi); |
| 2382 | } |
| 2383 | |
| 2384 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2385 | standard_row_validate(standard_display *dp, png_structp pp, png_const_bytep row, |
| 2386 | png_const_bytep display, png_uint_32 y) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2387 | { |
| 2388 | png_byte std[STD_ROWMAX]; |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2389 | standard_row(pp, std, dp->colour_type, dp->bit_depth, y); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2390 | |
| 2391 | /* At the end both the 'read' and 'display' arrays should end up identical. |
| 2392 | * In earlier passes 'read' will be narrow, containing only the columns that |
| 2393 | * were read, and display will be full width but populated with garbage where |
| 2394 | * pixels have not been filled in. |
| 2395 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2396 | if (row != NULL && memcmp(std, row, dp->cbRow) != 0) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2397 | { |
| 2398 | char msg[64]; |
| 2399 | sprintf(msg, "PNG image row %d changed", y); |
| 2400 | png_error(pp, msg); |
| 2401 | } |
| 2402 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2403 | if (display != NULL && memcmp(std, display, dp->cbRow) != 0) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2404 | { |
| 2405 | char msg[64]; |
| 2406 | sprintf(msg, "display row %d changed", y); |
| 2407 | png_error(pp, msg); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2412 | standard_image_validate(standard_display *dp, png_structp pp, |
| 2413 | png_const_bytep pImage, png_const_bytep pDisplay) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2414 | { |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2415 | png_uint_32 y; |
| 2416 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2417 | for (y=0; y<dp->h; ++y) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2418 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2419 | standard_row_validate(dp, pp, pImage, pDisplay, y); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2420 | |
| 2421 | if (pImage != NULL) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2422 | pImage += dp->cbRow; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2423 | |
| 2424 | if (pDisplay != NULL) |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2425 | pDisplay += dp->cbRow; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | /* This avoids false positives if the validation code is never called! */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2429 | dp->ps->validated = 1; |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | static void |
| 2433 | standard_end(png_structp pp, png_infop pi) |
| 2434 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2435 | standard_display *dp = png_get_progressive_ptr(pp); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2436 | |
| 2437 | UNUSED(pi); |
| 2438 | |
| 2439 | /* Validate the image - progressive reading only produces one variant for |
| 2440 | * interlaced images. |
| 2441 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2442 | standard_image_validate(dp, pp, dp->ps->image, NULL); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2443 | } |
| 2444 | |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2445 | /* A single test run checking the standard image to ensure it is not damaged. */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2446 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2447 | standard_test(png_store* PNG_CONST psIn, png_byte PNG_CONST colour_typeIn, |
| 2448 | png_byte PNG_CONST bit_depthIn, int interlace_typeIn) |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2449 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2450 | standard_display d; |
| 2451 | context(psIn, fault); |
| 2452 | |
| 2453 | /* Set up the display (stack frame) variables from the arguments to the |
| 2454 | * function and initialize the locals that are filled in later. |
| 2455 | */ |
| 2456 | standard_display_init(&d, psIn, colour_typeIn, bit_depthIn, |
| 2457 | interlace_typeIn); |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2458 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2459 | /* Everything is protected by a Try/Catch. The functions called also |
| 2460 | * typically have local Try/Catch blocks. |
| 2461 | */ |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2462 | Try |
| 2463 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2464 | png_structp pp; |
| 2465 | png_infop pi; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2466 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 2467 | /* Get a png_struct for writing the image, this will throw an error if it |
| 2468 | * fails, so we don't need to check the result. |
| 2469 | */ |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2470 | pp = set_store_for_read(d.ps, &pi, d.id, |
| 2471 | d.ps->progressive ? "progressive reader" : "sequential reader"); |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2472 | |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2473 | /* Introduce the correct read function. */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2474 | if (d.ps->progressive) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2475 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2476 | png_set_progressive_read_fn(pp, &d, standard_info, progressive_row, |
| 2477 | standard_end); |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2478 | |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2479 | /* Now feed data into the reader until we reach the end: */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2480 | store_progressive_read(d.ps, pp, pi); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2481 | } |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2482 | else |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2483 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2484 | /* Note that this takes the store, not the display. */ |
| 2485 | png_set_read_fn(pp, d.ps, store_read); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2486 | |
| 2487 | /* Check the header values: */ |
| 2488 | png_read_info(pp, pi); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2489 | |
| 2490 | /* The code tests both versions of the images that the sequential |
| 2491 | * reader can produce. |
| 2492 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2493 | standard_info_imp(&d, pp, pi, 2/*images*/); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2494 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2495 | /* Need the total bytes in the image below; we can't get to this point |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2496 | * unless the PNG file values have been checked against the expected |
| 2497 | * values. |
| 2498 | */ |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2499 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2500 | PNG_CONST png_bytep pImage = d.ps->image; |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2501 | PNG_CONST png_bytep pDisplay = pImage + d.cbRow * d.h; |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2502 | |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2503 | sequential_row(&d, pp, pi, pImage, pDisplay); |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2504 | |
| 2505 | /* After the last pass loop over the rows again to check that the |
| 2506 | * image is correct. |
| 2507 | */ |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2508 | standard_image_validate(&d, pp, pImage, pDisplay); |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2509 | } |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2510 | } |
| 2511 | |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2512 | /* Check for validation. */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2513 | if (!d.ps->validated) |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 2514 | png_error(pp, "image read failed silently"); |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2515 | |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2516 | /* Successful completion. */ |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | Catch(fault) |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 2520 | d.ps = fault; /* make sure this hasn't been clobbered. */ |
| 2521 | |
| 2522 | /* In either case clean up the store. */ |
| 2523 | store_read_reset(d.ps); |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static int |
| 2527 | test_standard(png_modifier* PNG_CONST pm, png_byte PNG_CONST colour_type, |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2528 | int bdlo, int PNG_CONST bdhi) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2529 | { |
| 2530 | for (; bdlo <= bdhi; ++bdlo) |
| 2531 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2532 | int interlace_type; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2533 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2534 | for (interlace_type = PNG_INTERLACE_NONE; |
| 2535 | interlace_type < PNG_INTERLACE_LAST; ++interlace_type) |
| 2536 | { |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2537 | /* Test both sequential and standard readers here. */ |
| 2538 | pm->this.progressive = !pm->this.progressive; |
| 2539 | standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type); |
| 2540 | |
| 2541 | if (fail(pm)) |
| 2542 | return 0; |
| 2543 | |
| 2544 | pm->this.progressive = !pm->this.progressive; |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 2545 | standard_test(&pm->this, colour_type, DEPTH(bdlo), interlace_type); |
| 2546 | |
| 2547 | if (fail(pm)) |
| 2548 | return 0; |
| 2549 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2550 | } |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2551 | |
| 2552 | return 1; /*keep going*/ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | static void |
| 2556 | perform_standard_test(png_modifier *pm) |
| 2557 | { |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2558 | /* Test each colour type over the valid range of bit depths (expressed as |
| 2559 | * log2(bit_depth) in turn, stop as soon as any error is detected. |
| 2560 | */ |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2561 | if (!test_standard(pm, 0, 0, READ_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2562 | return; |
| 2563 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2564 | if (!test_standard(pm, 2, 3, READ_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2565 | return; |
| 2566 | |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2567 | if (!test_standard(pm, 3, 0, 3)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2568 | return; |
| 2569 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2570 | if (!test_standard(pm, 4, 3, READ_BDHI)) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2571 | return; |
| 2572 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 2573 | if (!test_standard(pm, 6, 3, READ_BDHI)) |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 2574 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | |
| 2578 | /********************************* GAMMA TESTS ********************************/ |
| 2579 | /* Gamma test images. */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2580 | typedef struct gamma_modification |
| 2581 | { |
| 2582 | png_modification this; |
| 2583 | png_fixed_point gamma; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2584 | } gamma_modification; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2585 | |
| 2586 | static int |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2587 | gamma_modify(png_modifier *pm, png_modification *me, int add) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2588 | { |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 2589 | UNUSED(add); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2590 | /* This simply dumps the given gamma value into the buffer. */ |
| 2591 | png_save_uint_32(pm->buffer, 4); |
| 2592 | png_save_uint_32(pm->buffer+4, CHUNK_gAMA); |
| 2593 | png_save_uint_32(pm->buffer+8, ((gamma_modification*)me)->gamma); |
| 2594 | return 1; |
| 2595 | } |
| 2596 | |
| 2597 | static void |
| 2598 | gamma_modification_init(gamma_modification *me, png_modifier *pm, double gamma) |
| 2599 | { |
Glenn Randers-Pehrson | 21b4b33 | 2010-08-18 07:12:38 -0500 | [diff] [blame] | 2600 | double g; |
| 2601 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2602 | modification_init(&me->this); |
| 2603 | me->this.chunk = CHUNK_gAMA; |
| 2604 | me->this.modify_fn = gamma_modify; |
| 2605 | me->this.add = CHUNK_PLTE; |
Glenn Randers-Pehrson | 21b4b33 | 2010-08-18 07:12:38 -0500 | [diff] [blame] | 2606 | g = floor(gamma * 100000 + .5); |
| 2607 | me->gamma = (png_fixed_point)g; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2608 | me->this.next = pm->modifications; |
| 2609 | pm->modifications = &me->this; |
| 2610 | } |
| 2611 | |
| 2612 | typedef struct srgb_modification |
| 2613 | { |
| 2614 | png_modification this; |
| 2615 | png_byte intent; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2616 | } srgb_modification; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2617 | |
| 2618 | static int |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2619 | srgb_modify(png_modifier *pm, png_modification *me, int add) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2620 | { |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 2621 | UNUSED(add); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2622 | /* As above, ignore add and just make a new chunk */ |
| 2623 | png_save_uint_32(pm->buffer, 1); |
| 2624 | png_save_uint_32(pm->buffer+4, CHUNK_sRGB); |
| 2625 | pm->buffer[8] = ((srgb_modification*)me)->intent; |
| 2626 | return 1; |
| 2627 | } |
| 2628 | |
| 2629 | static void |
| 2630 | srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent) |
| 2631 | { |
| 2632 | modification_init(&me->this); |
| 2633 | me->this.chunk = CHUNK_sBIT; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2634 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2635 | if (intent <= 3) /* if valid, else *delete* sRGB chunks */ |
| 2636 | { |
| 2637 | me->this.modify_fn = srgb_modify; |
| 2638 | me->this.add = CHUNK_PLTE; |
| 2639 | me->intent = intent; |
| 2640 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2641 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2642 | else |
| 2643 | { |
| 2644 | me->this.modify_fn = 0; |
| 2645 | me->this.add = 0; |
| 2646 | me->intent = 0; |
| 2647 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2648 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2649 | me->this.next = pm->modifications; |
| 2650 | pm->modifications = &me->this; |
| 2651 | } |
| 2652 | |
| 2653 | typedef struct sbit_modification |
| 2654 | { |
| 2655 | png_modification this; |
| 2656 | png_byte sbit; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2657 | } sbit_modification; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2658 | |
| 2659 | static int |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2660 | sbit_modify(png_modifier *pm, png_modification *me, int add) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2661 | { |
| 2662 | png_byte sbit = ((sbit_modification*)me)->sbit; |
| 2663 | if (pm->bit_depth > sbit) |
| 2664 | { |
| 2665 | int cb = 0; |
| 2666 | switch (pm->colour_type) |
| 2667 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2668 | case 0: |
| 2669 | cb = 1; |
| 2670 | break; |
| 2671 | |
| 2672 | case 2: |
| 2673 | case 3: |
| 2674 | cb = 3; |
| 2675 | break; |
| 2676 | |
| 2677 | case 4: |
| 2678 | cb = 2; |
| 2679 | break; |
| 2680 | |
| 2681 | case 6: |
| 2682 | cb = 4; |
| 2683 | break; |
| 2684 | |
| 2685 | default: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2686 | png_error(pm->this.pread, |
| 2687 | "unexpected colour type in sBIT modification"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | png_save_uint_32(pm->buffer, cb); |
| 2691 | png_save_uint_32(pm->buffer+4, CHUNK_sBIT); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 2692 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2693 | while (cb > 0) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2694 | (pm->buffer+8)[--cb] = sbit; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2695 | |
| 2696 | return 1; |
| 2697 | } |
| 2698 | else if (!add) |
| 2699 | { |
| 2700 | /* Remove the sBIT chunk */ |
| 2701 | pm->buffer_count = pm->buffer_position = 0; |
| 2702 | return 1; |
| 2703 | } |
| 2704 | else |
| 2705 | return 0; /* do nothing */ |
| 2706 | } |
| 2707 | |
| 2708 | static void |
| 2709 | sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit) |
| 2710 | { |
| 2711 | modification_init(&me->this); |
| 2712 | me->this.chunk = CHUNK_sBIT; |
| 2713 | me->this.modify_fn = sbit_modify; |
| 2714 | me->this.add = CHUNK_PLTE; |
| 2715 | me->sbit = sbit; |
| 2716 | me->this.next = pm->modifications; |
| 2717 | pm->modifications = &me->this; |
| 2718 | } |
| 2719 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2720 | /* Reader callbacks and implementations, where they differ from the standard |
| 2721 | * ones. |
| 2722 | */ |
| 2723 | typedef struct gamma_display |
| 2724 | { |
| 2725 | standard_display this; |
| 2726 | |
| 2727 | /* Parameters */ |
| 2728 | png_modifier* pm; |
| 2729 | double file_gamma; |
| 2730 | double screen_gamma; |
| 2731 | png_byte sbit; |
| 2732 | int threshold_test; |
| 2733 | PNG_CONST char* name; |
| 2734 | int speed; |
| 2735 | int use_input_precision; |
| 2736 | int strip16; |
| 2737 | |
| 2738 | /* Local variables */ |
| 2739 | double maxerrout; |
| 2740 | double maxerrpc; |
| 2741 | double maxerrabs; |
| 2742 | } gamma_display; |
| 2743 | |
| 2744 | static void |
| 2745 | gamma_display_init(gamma_display *dp, png_modifier *pm, png_byte colour_type, |
| 2746 | png_byte bit_depth, int interlace_type, double file_gamma, |
| 2747 | double screen_gamma, png_byte sbit, int threshold_test, int speed, |
| 2748 | int use_input_precision, int strip16) |
| 2749 | { |
| 2750 | /* Standard fields */ |
| 2751 | standard_display_init(&dp->this, &pm->this, colour_type, bit_depth, |
| 2752 | interlace_type); |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 2753 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2754 | /* Parameter fields */ |
| 2755 | dp->pm = pm; |
| 2756 | dp->file_gamma = file_gamma; |
| 2757 | dp->screen_gamma = screen_gamma; |
| 2758 | dp->sbit = sbit; |
| 2759 | dp->threshold_test = threshold_test; |
| 2760 | dp->speed = speed; |
| 2761 | dp->use_input_precision = use_input_precision; |
| 2762 | dp->strip16 = strip16; |
| 2763 | |
| 2764 | /* Local variable fields */ |
| 2765 | dp->maxerrout = dp->maxerrpc = dp->maxerrabs = 0; |
| 2766 | } |
| 2767 | |
| 2768 | static void |
| 2769 | gamma_info_imp(gamma_display *dp, png_structp pp, png_infop pi) |
| 2770 | { |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 2771 | /* Reuse the standard stuff as appropriate. */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2772 | standard_info_part1(&dp->this, pp, pi); |
| 2773 | |
| 2774 | /* If requested strip 16 to 8 bits - this is handled automagically below |
| 2775 | * because the output bit depth is read from the library. Note that there |
| 2776 | * are interactions with sBIT but, internally, libpng makes sbit at most |
| 2777 | * PNG_MAX_GAMMA_8 when doing the following. |
| 2778 | */ |
| 2779 | if (dp->strip16) |
Glenn Randers-Pehrson | fded04f | 2010-08-27 14:21:21 -0500 | [diff] [blame] | 2780 | # ifdef PNG_READ_16_TO_8 |
| 2781 | png_set_strip_16(pp); |
| 2782 | # else |
| 2783 | png_error(pp, "strip16 (16 to 8 bit conversion) not supported"); |
| 2784 | # endif |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2785 | |
| 2786 | png_read_update_info(pp, pi); |
| 2787 | |
| 2788 | /* Now we may get a different cbRow: */ |
| 2789 | standard_info_part2(&dp->this, pp, pi, 1/*images*/); |
| 2790 | } |
| 2791 | |
| 2792 | static void |
| 2793 | gamma_info(png_structp pp, png_infop pi) |
| 2794 | { |
| 2795 | gamma_info_imp(png_get_progressive_ptr(pp), pp, pi); |
| 2796 | } |
| 2797 | |
| 2798 | static void |
| 2799 | gamma_image_validate(gamma_display *dp, png_structp pp, png_infop pi, |
| 2800 | png_const_bytep pRow) |
| 2801 | { |
| 2802 | /* Get some constants derived from the input and output file formats: */ |
| 2803 | PNG_CONST png_byte sbit = dp->sbit; |
| 2804 | PNG_CONST double file_gamma = dp->file_gamma; |
| 2805 | PNG_CONST double screen_gamma = dp->screen_gamma; |
| 2806 | PNG_CONST int use_input_precision = dp->use_input_precision; |
| 2807 | PNG_CONST int speed = dp->speed; |
| 2808 | PNG_CONST png_byte in_ct = dp->this.colour_type; |
| 2809 | PNG_CONST png_byte in_bd = dp->this.bit_depth; |
| 2810 | PNG_CONST png_uint_32 w = dp->this.w; |
| 2811 | PNG_CONST png_uint_32 h = dp->this.h; |
| 2812 | PNG_CONST size_t cbRow = dp->this.cbRow; |
| 2813 | PNG_CONST png_byte out_ct = png_get_color_type(pp, pi); |
| 2814 | PNG_CONST png_byte out_bd = png_get_bit_depth(pp, pi); |
| 2815 | PNG_CONST unsigned int outmax = (1U<<out_bd)-1; |
| 2816 | PNG_CONST double maxabs = abserr(dp->pm, out_bd); |
| 2817 | PNG_CONST double maxout = outerr(dp->pm, out_bd); |
| 2818 | PNG_CONST double maxpc = pcerr(dp->pm, out_bd); |
| 2819 | |
| 2820 | /* There are three sources of error, firstly the quantization in the |
| 2821 | * file encoding, determined by sbit and/or the file depth, secondly |
| 2822 | * the output (screen) gamma and thirdly the output file encoding. |
| 2823 | * Since this API receives the screen and file gamma in double |
| 2824 | * precision it is possible to calculate an exact answer given an input |
| 2825 | * pixel value. Therefore we assume that the *input* value is exact - |
| 2826 | * sample/maxsample - calculate the corresponding gamma corrected |
| 2827 | * output to the limits of double precision arithmetic and compare with |
| 2828 | * what libpng returns. |
| 2829 | * |
| 2830 | * Since the library must quantise the output to 8 or 16 bits there is |
| 2831 | * a fundamental limit on the accuracy of the output of +/-.5 - this |
| 2832 | * quantisation limit is included in addition to the other limits |
| 2833 | * specified by the paramaters to the API. (Effectively, add .5 |
| 2834 | * everywhere.) |
| 2835 | * |
| 2836 | * The behavior of the 'sbit' paramter is defined by section 12.5 |
| 2837 | * (sample depth scaling) of the PNG spec. That section forces the |
| 2838 | * decoder to assume that the PNG values have been scaled if sBIT is |
| 2839 | * presence: |
| 2840 | * |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame] | 2841 | * png-sample = floor( input-sample * (max-out/max-in) + .5); |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 2842 | * |
| 2843 | * This means that only a subset of the possible PNG values should |
| 2844 | * appear in the input, however the spec allows the encoder to use a |
| 2845 | * variety of approximations to the above and doesn't require any |
| 2846 | * restriction of the values produced. |
| 2847 | * |
| 2848 | * Nevertheless the spec requires that the upper 'sBIT' bits of the |
| 2849 | * value stored in a PNG file be the original sample bits. |
| 2850 | * Consequently the code below simply scales the top sbit bits by |
| 2851 | * (1<<sbit)-1 to obtain an original sample value. |
| 2852 | * |
| 2853 | * Because there is limited precision in the input it is arguable that |
| 2854 | * an acceptable result is any valid result from input-.5 to input+.5. |
| 2855 | * The basic tests below do not do this, however if |
| 2856 | * 'use_input_precision' is set a subsequent test is performed below. |
| 2857 | */ |
| 2858 | PNG_CONST int processing = (fabs(screen_gamma*file_gamma-1) >= |
| 2859 | PNG_GAMMA_THRESHOLD && !dp->threshold_test && !speed && in_ct != 3) || |
| 2860 | in_bd != out_bd; |
| 2861 | |
| 2862 | PNG_CONST unsigned int samples_per_pixel = (out_ct & 2U) ? 3U : 1U; |
| 2863 | |
| 2864 | PNG_CONST double gamma = 1/(file_gamma*screen_gamma); /* Overall */ |
| 2865 | |
| 2866 | double maxerrout = 0, maxerrabs = 0, maxerrpc = 0; |
| 2867 | png_uint_32 y; |
| 2868 | |
| 2869 | for (y=0; y<h; ++y, pRow += cbRow) |
| 2870 | { |
| 2871 | unsigned int s, x; |
| 2872 | png_byte std[STD_ROWMAX]; |
| 2873 | |
| 2874 | standard_row(pp, std, in_ct, in_bd, y); |
| 2875 | |
| 2876 | if (processing) |
| 2877 | { |
| 2878 | for (x=0; x<w; ++x) for (s=0; s<samples_per_pixel; ++s) |
| 2879 | { |
| 2880 | /* Input sample values: */ |
| 2881 | PNG_CONST unsigned int |
| 2882 | id = sample(std, in_ct, in_bd, x, s); |
| 2883 | |
| 2884 | PNG_CONST unsigned int |
| 2885 | od = sample(pRow, out_ct, out_bd, x, s); |
| 2886 | |
| 2887 | PNG_CONST unsigned int |
| 2888 | isbit = id >> (in_bd-sbit); |
| 2889 | |
| 2890 | double i, sample, encoded_sample, output, encoded_error, error; |
| 2891 | double es_lo, es_hi; |
| 2892 | |
| 2893 | /* First check on the 'perfect' result obtained from the |
| 2894 | * digitized input value, id, and compare this against the |
| 2895 | * actual digitized result, 'od'. 'i' is the input result |
| 2896 | * in the range 0..1: |
| 2897 | * |
| 2898 | * NOTE: sBIT should be taken into account here but isn't, |
| 2899 | * as described above. |
| 2900 | */ |
| 2901 | i = isbit; i /= (1U<<sbit)-1; |
| 2902 | |
| 2903 | /* Then get the gamma corrected version of 'i' and compare |
| 2904 | * to 'od', any error less than .5 is insignificant - just |
| 2905 | * quantization of the output value to the nearest digital |
| 2906 | * value (nevertheless the error is still recorded - it's |
| 2907 | * interesting ;-) |
| 2908 | */ |
| 2909 | encoded_sample = pow(i, gamma) * outmax; |
| 2910 | encoded_error = fabs(od-encoded_sample); |
| 2911 | |
| 2912 | if (encoded_error > maxerrout) |
| 2913 | maxerrout = encoded_error; |
| 2914 | |
| 2915 | if (encoded_error < .5+maxout) |
| 2916 | continue; |
| 2917 | |
| 2918 | /* There may be an error, calculate the actual sample |
| 2919 | * values - unencoded light intensity values. Note that |
| 2920 | * in practice these are not unencoded because they |
| 2921 | * include a 'viewing correction' to decrease or |
| 2922 | * (normally) increase the perceptual contrast of the |
| 2923 | * image. There's nothing we can do about this - we don't |
| 2924 | * know what it is - so assume the unencoded value is |
| 2925 | * perceptually linear. |
| 2926 | */ |
| 2927 | sample = pow(i, 1/file_gamma); /* In range 0..1 */ |
| 2928 | output = od; |
| 2929 | output /= outmax; |
| 2930 | output = pow(output, screen_gamma); |
| 2931 | |
| 2932 | /* Now we have the numbers for real errors, both absolute |
| 2933 | * values as as a percentage of the correct value (output): |
| 2934 | */ |
| 2935 | error = fabs(sample-output); |
| 2936 | |
| 2937 | if (error > maxerrabs) |
| 2938 | maxerrabs = error; |
| 2939 | |
| 2940 | /* The following is an attempt to ignore the tendency of |
| 2941 | * quantization to dominate the percentage errors for low |
| 2942 | * output sample values: |
| 2943 | */ |
| 2944 | if (sample*maxpc > .5+maxabs) |
| 2945 | { |
| 2946 | double pcerr = error/sample; |
| 2947 | if (pcerr > maxerrpc) maxerrpc = pcerr; |
| 2948 | } |
| 2949 | |
| 2950 | /* Now calculate the digitization limits for |
| 2951 | * 'encoded_sample' using the 'max' values. Note that |
| 2952 | * maxout is in the encoded space but maxpc and maxabs are |
| 2953 | * in linear light space. |
| 2954 | * |
| 2955 | * First find the maximum error in linear light space, |
| 2956 | * range 0..1: |
| 2957 | */ |
| 2958 | { |
| 2959 | double tmp = sample * maxpc; |
| 2960 | if (tmp < maxabs) tmp = maxabs; |
| 2961 | |
| 2962 | /* Low bound - the minimum of the three: */ |
| 2963 | es_lo = encoded_sample - maxout; |
| 2964 | if (es_lo > 0 && sample-tmp > 0) |
| 2965 | { |
| 2966 | double l = outmax * pow(sample-tmp, 1/screen_gamma); |
| 2967 | if (l < es_lo) es_lo = l; |
| 2968 | } |
| 2969 | else |
| 2970 | es_lo = 0; |
| 2971 | |
| 2972 | es_hi = encoded_sample + maxout; |
| 2973 | if (es_hi < outmax && sample+tmp < 1) |
| 2974 | { |
| 2975 | double h = outmax * pow(sample+tmp, 1/screen_gamma); |
| 2976 | if (h > es_hi) es_hi = h; |
| 2977 | } |
| 2978 | else |
| 2979 | es_hi = outmax; |
| 2980 | } |
| 2981 | |
| 2982 | /* The primary test is that the final encoded value |
| 2983 | * returned by the library should be between the two limits |
| 2984 | * (inclusive) that were calculated above. At this point |
| 2985 | * quantization of the output must be taken into account. |
| 2986 | */ |
| 2987 | if (od+.5 < es_lo || od-.5 > es_hi) |
| 2988 | { |
| 2989 | /* There has been an error in processing. */ |
| 2990 | double is_lo, is_hi; |
| 2991 | |
| 2992 | if (use_input_precision) |
| 2993 | { |
| 2994 | /* Ok, something is wrong - this actually happens in |
| 2995 | * current libpng sbit processing. Assume that the |
| 2996 | * input value (id, adjusted for sbit) can be |
| 2997 | * anywhere between value-.5 and value+.5 - quite a |
| 2998 | * large range if sbit is low. |
| 2999 | */ |
| 3000 | double tmp = (isbit - .5)/((1U<<sbit)-1); |
| 3001 | |
| 3002 | if (tmp > 0) |
| 3003 | { |
| 3004 | is_lo = outmax * pow(tmp, gamma) - maxout; |
| 3005 | if (is_lo < 0) is_lo = 0; |
| 3006 | } |
| 3007 | |
| 3008 | else |
| 3009 | is_lo = 0; |
| 3010 | |
| 3011 | tmp = (isbit + .5)/((1U<<sbit)-1); |
| 3012 | |
| 3013 | if (tmp < 1) |
| 3014 | { |
| 3015 | is_hi = outmax * pow(tmp, gamma) + maxout; |
| 3016 | if (is_hi > outmax) is_hi = outmax; |
| 3017 | } |
| 3018 | |
| 3019 | else |
| 3020 | is_hi = outmax; |
| 3021 | |
| 3022 | if (!(od+.5 < is_lo || od-.5 > is_hi)) |
| 3023 | continue; |
| 3024 | } |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 3025 | else |
| 3026 | is_lo = es_lo, is_hi = es_hi; |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3027 | |
| 3028 | { |
| 3029 | char msg[256]; |
| 3030 | sprintf(msg, |
| 3031 | "error: %.3f; %u{%u;%u} -> %u not %.2f (%.1f-%.1f)", |
| 3032 | od-encoded_sample, id, sbit, isbit, od, |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 3033 | encoded_sample, is_lo, is_hi); |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3034 | png_warning(pp, msg); |
| 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | else if (!speed && memcmp(std, pRow, cbRow) != 0) |
| 3041 | { |
| 3042 | char msg[64]; |
| 3043 | /* No transform is expected on the threshold tests. */ |
| 3044 | sprintf(msg, "gamma: below threshold row %d changed", y); |
| 3045 | png_error(pp, msg); |
| 3046 | } |
| 3047 | } /* row (y) loop */ |
| 3048 | |
| 3049 | dp->maxerrout = maxerrout; |
| 3050 | dp->maxerrabs = maxerrabs; |
| 3051 | dp->maxerrpc = maxerrpc; |
| 3052 | dp->this.ps->validated = 1; |
| 3053 | } |
| 3054 | |
| 3055 | static void |
| 3056 | gamma_end(png_structp pp, png_infop pi) |
| 3057 | { |
| 3058 | gamma_display *dp = png_get_progressive_ptr(pp); |
| 3059 | |
| 3060 | gamma_image_validate(dp, pp, pi, dp->this.ps->image); |
| 3061 | } |
| 3062 | |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 3063 | /* A single test run checking a gamma transformation. |
| 3064 | * |
| 3065 | * maxabs: maximum absolute error as a fraction |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3066 | * maxout: maximum output error in the output units |
| 3067 | * maxpc: maximum percentage error (as a percentage) |
| 3068 | */ |
| 3069 | static void |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3070 | gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn, |
| 3071 | PNG_CONST png_byte bit_depthIn, PNG_CONST int interlace_typeIn, |
| 3072 | PNG_CONST double file_gammaIn, PNG_CONST double screen_gammaIn, |
| 3073 | PNG_CONST png_byte sbitIn, PNG_CONST int threshold_testIn, |
| 3074 | PNG_CONST char *name, PNG_CONST int speedIn, |
| 3075 | PNG_CONST int use_input_precisionIn, PNG_CONST int strip16In) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3076 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3077 | gamma_display d; |
| 3078 | context(&pmIn->this, fault); |
| 3079 | |
| 3080 | gamma_display_init(&d, pmIn, colour_typeIn, bit_depthIn, interlace_typeIn, |
| 3081 | file_gammaIn, screen_gammaIn, sbitIn, threshold_testIn, speedIn, |
| 3082 | use_input_precisionIn, strip16In); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3083 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3084 | Try |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3085 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3086 | png_structp pp; |
| 3087 | png_infop pi; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3088 | gamma_modification gamma_mod; |
| 3089 | srgb_modification srgb_mod; |
| 3090 | sbit_modification sbit_mod; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3091 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3092 | /* Make an appropriate modifier to set the PNG file gamma to the |
| 3093 | * given gamma value and the sBIT chunk to the given precision. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3094 | */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3095 | d.pm->modifications = NULL; |
| 3096 | gamma_modification_init(&gamma_mod, d.pm, d.file_gamma); |
| 3097 | srgb_modification_init(&srgb_mod, d.pm, 127/*delete*/); |
| 3098 | sbit_modification_init(&sbit_mod, d.pm, d.sbit); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3099 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3100 | modification_reset(d.pm->modifications); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3101 | |
Glenn Randers-Pehrson | f18a0ed | 2010-08-24 08:41:00 -0500 | [diff] [blame] | 3102 | /* Get a png_struct for writing the image. */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3103 | pp = set_modifier_for_read(d.pm, &pi, d.this.id, name); |
Glenn Randers-Pehrson | 949d46c | 2010-08-24 08:29:58 -0500 | [diff] [blame] | 3104 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3105 | /* Set up gamma processing. */ |
Glenn Randers-Pehrson | 15333cd | 2010-08-24 15:29:52 -0500 | [diff] [blame] | 3106 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3107 | png_set_gamma(pp, d.screen_gamma, d.file_gamma); |
Glenn Randers-Pehrson | 15333cd | 2010-08-24 15:29:52 -0500 | [diff] [blame] | 3108 | #else |
| 3109 | { |
| 3110 | png_fixed_point s = floor(d.screen_gamma*100000+.5); |
| 3111 | png_fixed_point f = floor(d.file_gamma*100000+.5); |
| 3112 | png_set_gamma_fixed(pp, s, f); |
| 3113 | } |
| 3114 | #endif |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3115 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3116 | /* Introduce the correct read function. */ |
| 3117 | if (d.pm->this.progressive) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3118 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3119 | /* Share the row function with the standard implementation. */ |
| 3120 | png_set_progressive_read_fn(pp, &d, gamma_info, progressive_row, |
| 3121 | gamma_end); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3122 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3123 | /* Now feed data into the reader until we reach the end: */ |
| 3124 | modifier_progressive_read(d.pm, pp, pi); |
| 3125 | } |
| 3126 | else |
| 3127 | { |
| 3128 | /* modifier_read expects a png_modifier* */ |
| 3129 | png_set_read_fn(pp, d.pm, modifier_read); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3130 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3131 | /* Check the header values: */ |
| 3132 | png_read_info(pp, pi); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3133 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3134 | /* Process the 'info' requirements. only one image is generated */ |
| 3135 | gamma_info_imp(&d, pp, pi); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3136 | |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 3137 | sequential_row(&d.this, pp, pi, NULL, d.this.ps->image); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3138 | |
Glenn Randers-Pehrson | 9b780b8 | 2010-08-24 08:50:01 -0500 | [diff] [blame] | 3139 | gamma_image_validate(&d, pp, pi, d.this.ps->image); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3140 | } |
| 3141 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3142 | modifier_reset(d.pm); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3143 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3144 | if (d.pm->log && !d.threshold_test && !d.speed) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3145 | fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n", |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3146 | d.this.bit_depth, colour_types[d.this.colour_type], d.name, |
| 3147 | d.maxerrout, d.maxerrabs, 100*d.maxerrpc); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3148 | |
| 3149 | /* Log the summary values too. */ |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3150 | if (d.this.colour_type == 0 || d.this.colour_type == 4) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3151 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3152 | switch (d.this.bit_depth) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3153 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3154 | case 1: |
| 3155 | break; |
| 3156 | |
| 3157 | case 2: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3158 | if (d.maxerrout > d.pm->error_gray_2) |
| 3159 | d.pm->error_gray_2 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3160 | break; |
| 3161 | |
| 3162 | case 4: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3163 | if (d.maxerrout > d.pm->error_gray_4) |
| 3164 | d.pm->error_gray_4 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3165 | break; |
| 3166 | |
| 3167 | case 8: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3168 | if (d.maxerrout > d.pm->error_gray_8) |
| 3169 | d.pm->error_gray_8 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3170 | break; |
| 3171 | |
| 3172 | case 16: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3173 | if (d.maxerrout > d.pm->error_gray_16) |
| 3174 | d.pm->error_gray_16 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3175 | break; |
| 3176 | |
| 3177 | default: |
| 3178 | png_error(pp, "bad bit depth (internal: 1)"); |
| 3179 | } |
| 3180 | } |
| 3181 | |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3182 | else if (d.this.colour_type == 2 || d.this.colour_type == 6) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3183 | { |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3184 | switch (d.this.bit_depth) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3185 | { |
| 3186 | case 8: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3187 | if (d.maxerrout > d.pm->error_color_8) |
| 3188 | d.pm->error_color_8 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3189 | |
| 3190 | break; |
| 3191 | |
| 3192 | case 16: |
Glenn Randers-Pehrson | 0f21161 | 2010-08-24 08:46:53 -0500 | [diff] [blame] | 3193 | if (d.maxerrout > d.pm->error_color_16) |
| 3194 | d.pm->error_color_16 = d.maxerrout; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3195 | |
| 3196 | break; |
| 3197 | |
| 3198 | default: |
| 3199 | png_error(pp, "bad bit depth (internal: 2)"); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3200 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3201 | } |
| 3202 | } |
| 3203 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3204 | Catch(fault) |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 3205 | modifier_reset((png_modifier*)fault); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | static void gamma_threshold_test(png_modifier *pm, png_byte colour_type, |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3209 | png_byte bit_depth, int interlace_type, double file_gamma, |
| 3210 | double screen_gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3211 | { |
| 3212 | size_t pos = 0; |
| 3213 | char name[64]; |
| 3214 | pos = safecat(name, sizeof name, pos, "threshold "); |
| 3215 | pos = safecatd(name, sizeof name, pos, file_gamma, 3); |
| 3216 | pos = safecat(name, sizeof name, pos, "/"); |
| 3217 | pos = safecatd(name, sizeof name, pos, screen_gamma, 3); |
| 3218 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3219 | (void)gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma, |
| 3220 | screen_gamma, bit_depth, 1, name, 0/*speed*/, 0/*no input precision*/, |
| 3221 | 0/*no strip16*/); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | static void |
| 3225 | perform_gamma_threshold_tests(png_modifier *pm) |
| 3226 | { |
| 3227 | png_byte colour_type = 0; |
| 3228 | png_byte bit_depth = 0; |
| 3229 | |
| 3230 | while (next_format(&colour_type, &bit_depth)) |
| 3231 | { |
| 3232 | double gamma = 1.0; |
| 3233 | while (gamma >= .4) |
| 3234 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3235 | /* There's little point testing the interlacing vs non-interlacing, |
| 3236 | * but this can be set from the command line. |
| 3237 | */ |
| 3238 | gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type, |
| 3239 | gamma, 1/gamma); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3240 | gamma *= .95; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | /* And a special test for sRGB */ |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3244 | gamma_threshold_test(pm, colour_type, bit_depth, pm->interlace_type, |
| 3245 | .45455, 2.2); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3246 | |
| 3247 | if (fail(pm)) |
| 3248 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3252 | static void gamma_transform_test(png_modifier *pm, |
| 3253 | PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth, |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3254 | PNG_CONST int interlace_type, PNG_CONST double file_gamma, |
| 3255 | PNG_CONST double screen_gamma, PNG_CONST png_byte sbit, PNG_CONST int speed, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3256 | PNG_CONST int use_input_precision, PNG_CONST int strip16) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3257 | { |
| 3258 | size_t pos = 0; |
| 3259 | char name[64]; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3260 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3261 | if (sbit != bit_depth) |
| 3262 | { |
| 3263 | pos = safecat(name, sizeof name, pos, "sbit("); |
| 3264 | pos = safecatn(name, sizeof name, pos, sbit); |
| 3265 | pos = safecat(name, sizeof name, pos, ") "); |
| 3266 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3267 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3268 | else |
| 3269 | pos = safecat(name, sizeof name, pos, "gamma "); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3270 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3271 | if (strip16) |
| 3272 | pos = safecat(name, sizeof name, pos, "16to8 "); |
| 3273 | pos = safecatd(name, sizeof name, pos, file_gamma, 3); |
| 3274 | pos = safecat(name, sizeof name, pos, "->"); |
| 3275 | pos = safecatd(name, sizeof name, pos, screen_gamma, 3); |
| 3276 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3277 | gamma_test(pm, colour_type, bit_depth, interlace_type, file_gamma, |
| 3278 | screen_gamma, sbit, 0, name, speed, use_input_precision, strip16); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | static void perform_gamma_transform_tests(png_modifier *pm, int speed) |
| 3282 | { |
| 3283 | png_byte colour_type = 0; |
| 3284 | png_byte bit_depth = 0; |
| 3285 | |
| 3286 | /* Ignore palette images - the gamma correction happens on the palette entry, |
| 3287 | * haven't got the tests for this yet. |
| 3288 | */ |
| 3289 | while (next_format(&colour_type, &bit_depth)) if (colour_type != 3) |
| 3290 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 3291 | unsigned int i, j; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3292 | |
| 3293 | for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) if (i != j) |
| 3294 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3295 | gamma_transform_test(pm, colour_type, bit_depth, pm->interlace_type, |
| 3296 | 1/pm->gammas[i], pm->gammas[j], bit_depth, speed, |
| 3297 | pm->use_input_precision, 0/*do not strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3298 | |
| 3299 | if (fail(pm)) |
| 3300 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | static void perform_gamma_sbit_tests(png_modifier *pm, int speed) |
| 3306 | { |
| 3307 | png_byte sbit; |
| 3308 | |
| 3309 | /* The only interesting cases are colour and grayscale, alpha is ignored here |
| 3310 | * for overall speed. Only bit depths 8 and 16 are tested. |
| 3311 | */ |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3312 | for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3313 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 3314 | unsigned int i, j; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3315 | |
| 3316 | for (i=0; i<pm->ngammas; ++i) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3317 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3318 | for (j=0; j<pm->ngammas; ++j) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3319 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3320 | if (i != j) |
| 3321 | { |
| 3322 | if (sbit < 8) |
| 3323 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3324 | gamma_transform_test(pm, 0, 8, pm->interlace_type, |
| 3325 | 1/pm->gammas[i], pm->gammas[j], sbit, speed, |
| 3326 | pm->use_input_precision_sbit, 0/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3327 | |
| 3328 | if (fail(pm)) |
| 3329 | return; |
| 3330 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3331 | gamma_transform_test(pm, 2, 8, pm->interlace_type, |
| 3332 | 1/pm->gammas[i], pm->gammas[j], sbit, speed, |
| 3333 | pm->use_input_precision_sbit, 0/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3334 | |
| 3335 | if (fail(pm)) |
| 3336 | return; |
| 3337 | } |
| 3338 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3339 | #ifdef DO_16BIT |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3340 | gamma_transform_test(pm, 0, 16, pm->interlace_type, |
| 3341 | 1/pm->gammas[i], pm->gammas[j], sbit, speed, |
| 3342 | pm->use_input_precision_sbit, 0/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3343 | |
| 3344 | if (fail(pm)) |
| 3345 | return; |
| 3346 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3347 | gamma_transform_test(pm, 2, 16, pm->interlace_type, |
| 3348 | 1/pm->gammas[i], pm->gammas[j], sbit, speed, |
| 3349 | pm->use_input_precision_sbit, 0/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3350 | |
| 3351 | if (fail(pm)) |
| 3352 | return; |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3353 | #endif |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3354 | } |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3355 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3360 | /* Note that this requires a 16 bit source image but produces 8 bit output, so |
| 3361 | * we only need the 16bit write support. |
| 3362 | */ |
Glenn Randers-Pehrson | fded04f | 2010-08-27 14:21:21 -0500 | [diff] [blame] | 3363 | #ifdef PNG_16_TO_8_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3364 | static void perform_gamma_strip16_tests(png_modifier *pm, int speed) |
| 3365 | { |
| 3366 | # ifndef PNG_MAX_GAMMA_8 |
| 3367 | # define PNG_MAX_GAMMA_8 11 |
| 3368 | # endif |
| 3369 | /* Include the alpha cases here, not that sbit matches the internal value |
| 3370 | * used by the library - otherwise we will get spurious errors from the |
| 3371 | * internal sbit style approximation. |
| 3372 | * |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 3373 | * The threshold test is here because otherwise the 16 to 8 conversion will |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3374 | * proceed *without* gamma correction, and the tests above will fail (but not |
| 3375 | * by much) - this could be fixed, it only appears with the -g option. |
| 3376 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 3377 | unsigned int i, j; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3378 | for (i=0; i<pm->ngammas; ++i) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3379 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3380 | for (j=0; j<pm->ngammas; ++j) |
| 3381 | { |
| 3382 | if (i != j && |
| 3383 | fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD) |
| 3384 | { |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3385 | gamma_transform_test(pm, 0, 16, pm->interlace_type, 1/pm->gammas[i], |
| 3386 | pm->gammas[j], PNG_MAX_GAMMA_8, speed, |
| 3387 | pm->use_input_precision_16to8, 1/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3388 | |
| 3389 | if (fail(pm)) |
| 3390 | return; |
| 3391 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3392 | gamma_transform_test(pm, 2, 16, pm->interlace_type, 1/pm->gammas[i], |
| 3393 | pm->gammas[j], PNG_MAX_GAMMA_8, speed, |
| 3394 | pm->use_input_precision_16to8, 1/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3395 | |
| 3396 | if (fail(pm)) |
| 3397 | return; |
| 3398 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3399 | gamma_transform_test(pm, 4, 16, pm->interlace_type, 1/pm->gammas[i], |
| 3400 | pm->gammas[j], PNG_MAX_GAMMA_8, speed, |
| 3401 | pm->use_input_precision_16to8, 1/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3402 | |
| 3403 | if (fail(pm)) |
| 3404 | return; |
| 3405 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3406 | gamma_transform_test(pm, 6, 16, pm->interlace_type, 1/pm->gammas[i], |
| 3407 | pm->gammas[j], PNG_MAX_GAMMA_8, speed, |
| 3408 | pm->use_input_precision_16to8, 1/*strip16*/); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3409 | |
| 3410 | if (fail(pm)) |
| 3411 | return; |
| 3412 | } |
| 3413 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3414 | } |
| 3415 | } |
Glenn Randers-Pehrson | fded04f | 2010-08-27 14:21:21 -0500 | [diff] [blame] | 3416 | #endif /* 16 to 8 bit conversion */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3417 | |
| 3418 | static void |
| 3419 | perform_gamma_test(png_modifier *pm, int speed, int summary) |
| 3420 | { |
| 3421 | /* First some arbitrary no-transform tests: */ |
| 3422 | if (!speed) |
| 3423 | { |
| 3424 | perform_gamma_threshold_tests(pm); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3425 | |
| 3426 | if (fail(pm)) |
| 3427 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | /* Now some real transforms. */ |
| 3431 | perform_gamma_transform_tests(pm, speed); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3432 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3433 | if (summary) |
| 3434 | { |
| 3435 | printf("Gamma correction error summary (output value error):\n"); |
| 3436 | printf(" 2 bit gray: %.5f\n", pm->error_gray_2); |
| 3437 | printf(" 4 bit gray: %.5f\n", pm->error_gray_4); |
| 3438 | printf(" 8 bit gray: %.5f\n", pm->error_gray_8); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3439 | printf(" 8 bit color: %.5f\n", pm->error_color_8); |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3440 | #ifdef DO_16BIT |
| 3441 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3442 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3443 | #endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | /* The sbit tests produce much larger errors: */ |
| 3447 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 = |
| 3448 | pm->error_color_8 = pm->error_color_16 = 0; |
| 3449 | perform_gamma_sbit_tests(pm, speed); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3450 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3451 | if (summary) |
| 3452 | { |
| 3453 | printf("Gamma correction with sBIT:\n"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3454 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3455 | if (pm->sbitlow < 8U) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3456 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3457 | printf(" 2 bit gray: %.5f\n", pm->error_gray_2); |
| 3458 | printf(" 4 bit gray: %.5f\n", pm->error_gray_4); |
| 3459 | printf(" 8 bit gray: %.5f\n", pm->error_gray_8); |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3460 | printf(" 8 bit color: %.5f\n", pm->error_color_8); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3461 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3462 | |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3463 | #ifdef DO_16BIT |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3464 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3465 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3466 | #endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3467 | } |
| 3468 | |
Glenn Randers-Pehrson | fded04f | 2010-08-27 14:21:21 -0500 | [diff] [blame] | 3469 | #ifdef PNG_16_TO_8_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3470 | /* The 16 to 8 bit strip operations: */ |
| 3471 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 = |
| 3472 | pm->error_color_8 = pm->error_color_16 = 0; |
| 3473 | perform_gamma_strip16_tests(pm, speed); |
| 3474 | if (summary) |
| 3475 | { |
| 3476 | printf("Gamma correction with 16 to 8 bit reduction:\n"); |
| 3477 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
| 3478 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
| 3479 | } |
Glenn Randers-Pehrson | 2f70282 | 2010-08-27 06:39:23 -0500 | [diff] [blame] | 3480 | #endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | /* main program */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3484 | int main(int argc, PNG_CONST char **argv) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3485 | { |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3486 | volatile int summary = 1; /* Print the error sumamry at the end */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3487 | |
| 3488 | /* Create the given output file on success: */ |
| 3489 | PNG_CONST char *volatile touch = NULL; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3490 | |
| 3491 | /* This is an array of standard gamma values (believe it or not I've seen |
| 3492 | * every one of these mentioned somewhere.) |
| 3493 | * |
| 3494 | * In the following list the most useful values are first! |
| 3495 | */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3496 | static double |
| 3497 | gammas[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9}; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3498 | |
| 3499 | png_modifier pm; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3500 | context(&pm.this, fault); |
| 3501 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3502 | modifier_init(&pm); |
| 3503 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3504 | /* Preallocate the image buffer, because we know how big it needs to be, |
| 3505 | * note that, for testing purposes, it is deliberately mis-aligned. |
| 3506 | */ |
| 3507 | pm.this.image = malloc(2*STD_IMAGEMAX+1); |
| 3508 | if (pm.this.image != NULL) |
| 3509 | { |
| 3510 | /* Ignore OOM at this point - the 'ensure' routine above will allocate the |
| 3511 | * array appropriately. |
| 3512 | */ |
| 3513 | ++(pm.this.image); |
| 3514 | pm.this.cb_image = 2*STD_IMAGEMAX; |
| 3515 | } |
| 3516 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3517 | /* Default to error on warning: */ |
| 3518 | pm.this.treat_warnings_as_errors = 1; |
| 3519 | |
| 3520 | /* Store the test gammas */ |
| 3521 | pm.gammas = gammas; |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3522 | pm.ngammas = 3U; /* for speed */ |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3523 | pm.sbitlow = 8U; /* because libpng doesn't do sBIT below 8! */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3524 | pm.use_input_precision_16to8 = 1U; /* Because of the way libpng does it */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3525 | |
| 3526 | /* Some default values (set the behavior for 'make check' here) */ |
| 3527 | pm.maxout8 = .1; /* Arithmetic error in *encoded* value */ |
| 3528 | pm.maxabs8 = .00005; /* 1/20000 */ |
| 3529 | pm.maxpc8 = .499; /* I.e. .499% fractional error */ |
| 3530 | pm.maxout16 = .499; /* Error in *encoded* value */ |
| 3531 | pm.maxabs16 = .00005;/* 1/20000 */ |
| 3532 | /* NOTE: this is a reasonable perceptual limit, we assume that humans can |
| 3533 | * perceive light level differences of 1% over a 100:1 range, so we need to |
| 3534 | * maintain 1 in 10000 accuracy (in linear light space), this is what the |
| 3535 | * following guarantees. It also allows significantly higher errors at |
| 3536 | * higher 16 bit values, which is important for performance. The actual |
| 3537 | * maximum 16 bit error is about +/-1.9 in the fixed point implementation but |
| 3538 | * this is only allowed for values >38149 by the following: |
| 3539 | */ |
| 3540 | pm.maxpc16 = .005; /* I.e. 1/200% - 1/20000 */ |
| 3541 | |
| 3542 | /* Now parse the command line options. */ |
| 3543 | while (--argc >= 1) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3544 | { |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3545 | if (strcmp(*++argv, "-v") == 0) |
| 3546 | pm.this.verbose = 1; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3547 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3548 | else if (strcmp(*argv, "-l") == 0) |
| 3549 | pm.log = 1; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3550 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3551 | else if (strcmp(*argv, "-q") == 0) |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 3552 | summary = pm.this.verbose = pm.log = 0; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3553 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3554 | else if (strcmp(*argv, "-g") == 0) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3555 | pm.ngammas = (sizeof gammas)/(sizeof gammas[0]); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3556 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3557 | else if (strcmp(*argv, "-w") == 0) |
| 3558 | pm.this.treat_warnings_as_errors = 0; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3559 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3560 | else if (strcmp(*argv, "--speed") == 0) |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3561 | pm.this.speed = 1, pm.ngammas = (sizeof gammas)/(sizeof gammas[0]); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3562 | |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 3563 | else if (strcmp(*argv, "--nogamma") == 0) |
| 3564 | pm.ngammas = 0; |
| 3565 | |
Glenn Randers-Pehrson | db712a9 | 2010-08-24 08:44:14 -0500 | [diff] [blame] | 3566 | else if (strcmp(*argv, "--progressive-read") == 0) |
| 3567 | pm.this.progressive = 1; |
| 3568 | |
Glenn Randers-Pehrson | 21af4cc | 2010-08-24 08:33:28 -0500 | [diff] [blame] | 3569 | else if (strcmp(*argv, "--interlace") == 0) |
| 3570 | pm.interlace_type = PNG_INTERLACE_ADAM7; |
| 3571 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3572 | else if (argc >= 1 && strcmp(*argv, "--sbitlow") == 0) |
| 3573 | --argc, pm.sbitlow = (png_byte)atoi(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3574 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3575 | else if (argc >= 1 && strcmp(*argv, "--touch") == 0) |
| 3576 | --argc, touch = *++argv; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3577 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3578 | else if (argc >= 1 && strncmp(*argv, "--max", 4) == 0) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3579 | { |
| 3580 | --argc; |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3581 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3582 | if (strcmp(4+*argv, "abs8") == 0) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3583 | pm.maxabs8 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3584 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3585 | else if (strcmp(4+*argv, "abs16") == 0) |
| 3586 | pm.maxabs16 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3587 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3588 | else if (strcmp(4+*argv, "out8") == 0) |
| 3589 | pm.maxout8 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3590 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3591 | else if (strcmp(4+*argv, "out16") == 0) |
| 3592 | pm.maxout16 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3593 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3594 | else if (strcmp(4+*argv, "pc8") == 0) |
| 3595 | pm.maxpc8 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3596 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3597 | else if (strcmp(4+*argv, "pc16") == 0) |
| 3598 | pm.maxpc16 = atof(*++argv); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3599 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3600 | else |
| 3601 | { |
| 3602 | fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv); |
| 3603 | exit(1); |
| 3604 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3605 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3606 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3607 | else |
| 3608 | { |
| 3609 | fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3610 | exit(1); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3611 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3612 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3613 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3614 | Try |
| 3615 | { |
| 3616 | /* Make useful base images */ |
| 3617 | make_standard_images(&pm.this); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3618 | |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3619 | /* Perform the standard and gamma tests. */ |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3620 | if (!pm.this.speed) |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3621 | { |
| 3622 | perform_standard_test(&pm); |
| 3623 | perform_error_test(&pm); |
| 3624 | } |
| 3625 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3626 | perform_gamma_test(&pm, pm.this.speed != 0, summary && !pm.this.speed); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | Catch(fault) |
| 3630 | { |
Glenn Randers-Pehrson | 438b3ca | 2010-08-24 08:55:40 -0500 | [diff] [blame] | 3631 | fprintf(stderr, "pngvalid: test aborted (probably failed in cleanup)\n"); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3632 | if (!pm.this.verbose) |
| 3633 | { |
| 3634 | if (pm.this.error[0] != 0) |
| 3635 | fprintf(stderr, "pngvalid: first error: %s\n", pm.this.error); |
| 3636 | fprintf(stderr, "pngvalid: run with -v to see what happened\n"); |
| 3637 | } |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3638 | exit(1); |
| 3639 | } |
| 3640 | |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3641 | if (summary && !pm.this.speed) |
| 3642 | { |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3643 | printf("Results using %s point arithmetic %s\n", |
| 3644 | #if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500 |
| 3645 | "floating", |
| 3646 | #else |
| 3647 | "fixed", |
| 3648 | #endif |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3649 | (pm.this.nerrors || (pm.this.treat_warnings_as_errors && |
| 3650 | pm.this.nwarnings)) ? "(errors)" : (pm.this.nwarnings ? |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3651 | "(warnings)" : "(no errors or warnings)") |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3652 | ); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3653 | printf("Allocated memory statistics (in bytes):\n" |
Glenn Randers-Pehrson | 9a75d99 | 2010-10-08 16:27:14 -0500 | [diff] [blame] | 3654 | "\tread %lu maximum single, %lu peak, %lu total\n" |
| 3655 | "\twrite %lu maximum single, %lu peak, %lu total\n", |
| 3656 | (unsigned long)pm.this.read_memory_pool.max_max, |
| 3657 | (unsigned long)pm.this.read_memory_pool.max_limit, |
| 3658 | (unsigned long)pm.this.read_memory_pool.max_total, |
| 3659 | (unsigned long)pm.this.write_memory_pool.max_max, |
| 3660 | (unsigned long)pm.this.write_memory_pool.max_limit, |
| 3661 | (unsigned long)pm.this.write_memory_pool.max_total); |
Glenn Randers-Pehrson | 921d915 | 2010-08-24 08:26:54 -0500 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /* Do this here to provoke memory corruption errors in memory not directly |
| 3665 | * allocated by libpng - not a complete test, but better than nothing. |
| 3666 | */ |
| 3667 | store_delete(&pm.this); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3668 | |
| 3669 | /* Error exit if there are any errors, and maybe if there are any |
| 3670 | * warnings. |
| 3671 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3672 | if (pm.this.nerrors || (pm.this.treat_warnings_as_errors && |
| 3673 | pm.this.nwarnings)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3674 | { |
| 3675 | if (!pm.this.verbose) |
| 3676 | fprintf(stderr, "pngvalid: %s\n", pm.this.error); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3677 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3678 | fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors, |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3679 | pm.this.nwarnings); |
| 3680 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3681 | exit(1); |
| 3682 | } |
| 3683 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3684 | /* Success case. */ |
| 3685 | if (touch != NULL) |
| 3686 | { |
| 3687 | FILE *fsuccess = fopen(touch, "wt"); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3688 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3689 | if (fsuccess != NULL) |
| 3690 | { |
| 3691 | int error = 0; |
| 3692 | fprintf(fsuccess, "PNG validation succeeded\n"); |
| 3693 | fflush(fsuccess); |
| 3694 | error = ferror(fsuccess); |
Glenn Randers-Pehrson | c08cae1 | 2010-08-20 09:55:01 -0500 | [diff] [blame] | 3695 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 3696 | if (fclose(fsuccess) || error) |
| 3697 | { |
| 3698 | fprintf(stderr, "%s: write failed\n", touch); |
| 3699 | exit(1); |
| 3700 | } |
| 3701 | } |
| 3702 | } |
| 3703 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 3704 | return 0; |
| 3705 | } |