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