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 | * |
| 4 | * Last changed in libpng 1.5.0 [July 5, 2010] |
| 5 | * Copyright (c) 2010-2010 Glenn Randers-Pehrson |
| 6 | * |
| 7 | * This code is released under the libpng license. |
| 8 | * For conditions of distribution and use, see the disclaimer |
| 9 | * and license in png.h |
| 10 | * |
| 11 | * NOTES: |
| 12 | * This is a C program that is intended to be linked against libpng. It |
| 13 | * generates bitmaps internally, stores them as PNG files (using the |
| 14 | * sequential write code) then reads them back (using the sequential |
| 15 | * read code) and validates that the result has the correct data. |
| 16 | * |
| 17 | * The program can be modified and extended to test the correctness of |
| 18 | * transformations performed by libpng. |
| 19 | */ |
| 20 | |
| 21 | #include "png.h" |
| 22 | #include "zlib.h" /* For crc32 */ |
| 23 | |
| 24 | #include <stdlib.h> /* For malloc */ |
| 25 | #include <string.h> /* For memcpy, memset */ |
| 26 | #include <setjmp.h> /* For jmp_buf, setjmp, longjmp */ |
| 27 | #include <math.h> /* For floor */ |
| 28 | |
| 29 | /******************************* ERROR UTILITIES ******************************/ |
| 30 | static size_t safecat(char *buffer, size_t bufsize, size_t pos, const char *cat) |
| 31 | { |
| 32 | while (pos < bufsize && cat != NULL && *cat != 0) buffer[pos++] = *cat++; |
| 33 | if (pos >= bufsize) pos = bufsize-1; |
| 34 | buffer[pos] = 0; |
| 35 | return pos; |
| 36 | } |
| 37 | |
| 38 | static size_t safecatn(char *buffer, size_t bufsize, size_t pos, int n) |
| 39 | { |
| 40 | char number[64]; |
| 41 | sprintf(number, "%d", n); |
| 42 | return safecat(buffer, bufsize, pos, number); |
| 43 | } |
| 44 | |
| 45 | static size_t safecatd(char *buffer, size_t bufsize, size_t pos, double d, |
| 46 | int precision) |
| 47 | { |
| 48 | char number[64]; |
| 49 | sprintf(number, "%.*f", precision, d); |
| 50 | return safecat(buffer, bufsize, pos, number); |
| 51 | } |
| 52 | |
| 53 | static const char invalid[] = "invalid"; |
| 54 | static const char sep[] = ": "; |
| 55 | |
| 56 | /* NOTE: this is indexed by ln2(bit_depth)! */ |
| 57 | static const char *bit_depths[8] = |
| 58 | { |
| 59 | "1", "2", "4", "8", "16", invalid, invalid, invalid |
| 60 | }; |
| 61 | |
| 62 | static const char *colour_types[8] = |
| 63 | { |
| 64 | "greyscale", invalid, "truecolour", "indexed-colour", |
| 65 | "greyscale with alpha", invalid, "truecolour with alpha", invalid |
| 66 | }; |
| 67 | |
| 68 | /* Convenience API to list valid formats: */ |
| 69 | static int |
| 70 | next_format(png_bytep colour_type, png_bytep bit_depth) |
| 71 | { |
| 72 | if (*bit_depth == 0) |
| 73 | { |
| 74 | *colour_type = 0, *bit_depth = 1; |
| 75 | return 1; |
| 76 | } |
| 77 | else switch (*colour_type) |
| 78 | { |
| 79 | case 0: |
| 80 | *bit_depth <<= 1; |
| 81 | if (*bit_depth <= 16) return 1; |
| 82 | *colour_type = 2; |
| 83 | *bit_depth = 8; |
| 84 | return 1; |
| 85 | case 2: |
| 86 | *bit_depth <<= 1; |
| 87 | if (*bit_depth <= 16) return 1; |
| 88 | *colour_type = 3; |
| 89 | *bit_depth = 1; |
| 90 | return 1; |
| 91 | case 3: |
| 92 | *bit_depth <<= 1; |
| 93 | if (*bit_depth <= 8) return 1; |
| 94 | *colour_type = 4; |
| 95 | *bit_depth = 8; |
| 96 | return 1; |
| 97 | case 4: |
| 98 | *bit_depth <<= 1; |
| 99 | if (*bit_depth <= 16) return 1; |
| 100 | *colour_type = 6; |
| 101 | *bit_depth = 8; |
| 102 | return 1; |
| 103 | case 6: |
| 104 | *bit_depth <<= 1; |
| 105 | if (*bit_depth <= 16) return 1; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | /* Here at the end. */ |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static inline unsigned |
| 114 | sample(png_byte *row, png_byte colour_type, png_byte bit_depth, png_uint_32 x, |
| 115 | unsigned sample) |
| 116 | { |
| 117 | png_uint_32 index, result; |
| 118 | |
| 119 | /* Find a sample index for the desired sample: */ |
| 120 | x *= bit_depth; |
| 121 | index = x; |
| 122 | if ((colour_type & 1) == 0) /* !palette */ |
| 123 | { |
| 124 | if (colour_type & 2) |
| 125 | index *= 3, index += sample; /* Colour channels; select one */ |
| 126 | if (colour_type & 4) index += x; /* Alpha channel */ |
| 127 | } |
| 128 | |
| 129 | /* Return the sample from the row as an integer. */ |
| 130 | row += index >> 3; |
| 131 | result = *row; |
| 132 | if (bit_depth == 8) |
| 133 | return result; |
| 134 | else if (bit_depth > 8) |
| 135 | return (result << 8) + *++row; |
| 136 | /* Less than 8 bits per sample. */ |
| 137 | index &= 7; |
| 138 | return (result >> (8-index-bit_depth)) & ((1U<<bit_depth)-1); |
| 139 | } |
| 140 | |
| 141 | /*************************** BASIC PNG FILE WRITING ***************************/ |
| 142 | /* A png_sucker takes data from the sequential writer or provides data |
| 143 | * to the sequential reader. It can also store the result of a PNG |
| 144 | * write for later retrieval. |
| 145 | */ |
| 146 | #define SUCKER_BUFFER_SIZE 500 /* arbitrary */ |
| 147 | typedef struct png_sucker_buffer |
| 148 | { |
| 149 | struct png_sucker_buffer* prev; /* NOTE: stored in reverse order */ |
| 150 | png_byte buffer[SUCKER_BUFFER_SIZE]; |
| 151 | } png_sucker_buffer; |
| 152 | |
| 153 | typedef struct png_sucker_file |
| 154 | { |
| 155 | struct png_sucker_file* next; /* as many as you like... */ |
| 156 | char name[64]; /* textual name */ |
| 157 | png_uint_32 id; /* as a convenience to users */ |
| 158 | png_size_t datacount; /* In this (the last) buffer */ |
| 159 | png_sucker_buffer data; /* Last buffer in file */ |
| 160 | } png_sucker_file; |
| 161 | |
| 162 | #define SUCKER_ERROR 0x345 |
| 163 | typedef struct png_sucker |
| 164 | { |
| 165 | jmp_buf jmpbuf; |
| 166 | int verbose; |
| 167 | int nerrors; |
| 168 | int nwarnings; |
| 169 | int treat_warnings_as_errors; |
| 170 | char test[64]; /* Name of test */ |
| 171 | char error[128]; |
| 172 | /* Read fields */ |
| 173 | png_structp pread; /* Used to read a saved file */ |
| 174 | png_infop piread; |
| 175 | png_sucker_file* current; /* Set when reading */ |
| 176 | png_sucker_buffer* next; /* Set when reading */ |
| 177 | png_size_t readpos; /* Position in *next */ |
| 178 | /* Write fields */ |
| 179 | png_sucker_file* saved; |
| 180 | png_structp pwrite; /* Used when writing a new file */ |
| 181 | png_infop piwrite; |
| 182 | png_size_t writepos; /* Position in .new */ |
| 183 | char wname[64];/* Name of file being written */ |
| 184 | png_sucker_buffer new; /* The end of the new PNG file being written. */ |
| 185 | } png_sucker; |
| 186 | |
| 187 | /* Initialization and cleanup */ |
| 188 | static void |
| 189 | sucker_init(png_sucker* ps) |
| 190 | { |
| 191 | memset(ps, 0, sizeof *ps); |
| 192 | ps->verbose = 0; |
| 193 | ps->nerrors = ps->nwarnings = 0; |
| 194 | ps->treat_warnings_as_errors = 0; |
| 195 | ps->pread = NULL; |
| 196 | ps->piread = NULL; |
| 197 | ps->saved = ps->current = NULL; |
| 198 | ps->next = NULL; |
| 199 | ps->readpos = 0; |
| 200 | ps->pwrite = NULL; |
| 201 | ps->piwrite = NULL; |
| 202 | ps->writepos = 0; |
| 203 | ps->new.prev = NULL; |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | sucker_freebuffer(png_sucker_buffer* psb) |
| 208 | { |
| 209 | if (psb->prev) |
| 210 | { |
| 211 | sucker_freebuffer(psb->prev); |
| 212 | free(psb->prev); |
| 213 | psb->prev = NULL; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | sucker_freenew(png_sucker *ps) |
| 219 | { |
| 220 | sucker_freebuffer(&ps->new); |
| 221 | ps->writepos = 0; |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | sucker_storenew(png_sucker *ps) |
| 226 | { |
| 227 | png_sucker_buffer *pb; |
| 228 | if (ps->writepos != SUCKER_BUFFER_SIZE) |
| 229 | png_error(ps->pwrite, "invalid store call"); |
| 230 | pb = malloc(sizeof *pb); |
| 231 | if (pb == NULL) |
| 232 | png_error(ps->pwrite, "store new: OOM"); |
| 233 | *pb = ps->new; |
| 234 | ps->new.prev = pb; |
| 235 | ps->writepos = 0; |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | sucker_freefile(png_sucker_file *pf) |
| 240 | { |
| 241 | if (pf->next) |
| 242 | sucker_freefile(pf->next); |
| 243 | pf->next = NULL; |
| 244 | sucker_freebuffer(&pf->data); |
| 245 | pf->datacount = 0; |
| 246 | free(pf); |
| 247 | } |
| 248 | |
| 249 | /* Main interface to file storeage, after writing a new PNG file (see the API |
| 250 | * below) call sucker_storefile to store the result with the given name and id. |
| 251 | */ |
| 252 | static void |
| 253 | sucker_storefile(png_sucker *ps, png_uint_32 id) |
| 254 | { |
| 255 | png_sucker_file *pf = malloc(sizeof *pf); |
| 256 | if (pf == NULL) |
| 257 | png_error(ps->pwrite, "storefile: OOM"); |
| 258 | safecat(pf->name, sizeof pf->name, 0, ps->wname); |
| 259 | pf->id = id; |
| 260 | pf->data = ps->new; |
| 261 | pf->datacount = ps->writepos; |
| 262 | ps->new.prev = NULL; |
| 263 | ps->writepos = 0; |
| 264 | |
| 265 | /* And save it. */ |
| 266 | pf->next = ps->saved; |
| 267 | ps->saved = pf; |
| 268 | } |
| 269 | |
| 270 | /* Generate an error message (in the given buffer) */ |
| 271 | static size_t |
| 272 | sucker_message(png_structp pp, char *buffer, size_t bufsize, const char *msg) |
| 273 | { |
| 274 | size_t pos = 0; |
| 275 | png_sucker *ps = png_get_error_ptr(pp); |
| 276 | |
| 277 | if (pp == ps->pread) |
| 278 | { |
| 279 | /* Reading a file */ |
| 280 | pos = safecat(buffer, bufsize, pos, "read: "); |
| 281 | if (ps->current != NULL) |
| 282 | { |
| 283 | pos = safecat(buffer, bufsize, pos, ps->current->name); |
| 284 | pos = safecat(buffer, bufsize, pos, sep); |
| 285 | } |
| 286 | } |
| 287 | else if (pp == ps->pwrite) |
| 288 | { |
| 289 | /* Writing a file */ |
| 290 | pos = safecat(buffer, bufsize, pos, "write: "); |
| 291 | pos = safecat(buffer, bufsize, pos, ps->wname); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | /* Neither reading nor writing */ |
| 296 | pos = safecat(buffer, bufsize, pos, "pngvalid: "); |
| 297 | } |
| 298 | |
| 299 | pos = safecat(buffer, bufsize, pos, ps->test); |
| 300 | pos = safecat(buffer, bufsize, pos, " "); |
| 301 | pos = safecat(buffer, bufsize, pos, msg); |
| 302 | return pos; |
| 303 | } |
| 304 | |
| 305 | /* Functions to use as PNG callbacks. */ |
| 306 | static void |
| 307 | sucker_error(png_structp pp, png_const_charp message) /* PNG_NORETURN */ |
| 308 | { |
| 309 | png_sucker *ps = png_get_error_ptr(pp); |
| 310 | char buffer[256]; |
| 311 | |
| 312 | sucker_message(pp, buffer, sizeof buffer, message); |
| 313 | |
| 314 | if (ps->nerrors++ == 0) |
| 315 | safecat(ps->error, sizeof ps->error, 0, buffer); |
| 316 | |
| 317 | if (ps->verbose) |
| 318 | fprintf(stderr, "error: %s\n", buffer); |
| 319 | |
| 320 | /* The longjmp argument is because, by UTSL, libpng calls longjmp with 1, and |
| 321 | * libpng is *not* expected to ever call longjmp, so this is a sanity |
| 322 | * check. The code below ensures that libpng gets a copy of our jmp_buf. |
| 323 | */ |
| 324 | longjmp(ps->jmpbuf, SUCKER_ERROR); |
| 325 | } |
| 326 | |
| 327 | static void |
| 328 | sucker_warning(png_structp pp, png_const_charp message) |
| 329 | { |
| 330 | png_sucker *ps = png_get_error_ptr(pp); |
| 331 | char buffer[256]; |
| 332 | |
| 333 | sucker_message(pp, buffer, sizeof buffer, message); |
| 334 | |
| 335 | if (ps->nwarnings++ == 0 && ps->nerrors == 0) |
| 336 | safecat(ps->error, sizeof ps->error, 0, buffer); |
| 337 | |
| 338 | if (ps->verbose) |
| 339 | fprintf(stderr, "warning: %s\n", buffer); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | sucker_write(png_structp pp, png_bytep pb, png_size_t st) |
| 344 | { |
| 345 | png_sucker *ps = png_get_io_ptr(pp); |
| 346 | if (ps->pwrite != pp) |
| 347 | png_error(pp, "sucker state damaged"); |
| 348 | while (st > 0) |
| 349 | { |
| 350 | size_t cb; |
| 351 | |
| 352 | if (ps->writepos >= SUCKER_BUFFER_SIZE) |
| 353 | sucker_storenew(ps); |
| 354 | |
| 355 | cb = st; |
| 356 | if (cb > SUCKER_BUFFER_SIZE - ps->writepos) |
| 357 | cb = SUCKER_BUFFER_SIZE - ps->writepos; |
| 358 | memcpy(ps->new.buffer + ps->writepos, pb, cb); |
| 359 | pb += cb; |
| 360 | st -= cb; |
| 361 | ps->writepos += cb; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | sucker_flush(png_structp pp) |
| 367 | { |
| 368 | /*DOES NOTHING*/ |
| 369 | } |
| 370 | |
| 371 | static size_t |
| 372 | sucker_read_buffer_size(png_sucker *ps) |
| 373 | { |
| 374 | /* Return the bytes available for read in the current buffer. */ |
| 375 | if (ps->next != &ps->current->data) |
| 376 | return SUCKER_BUFFER_SIZE; |
| 377 | |
| 378 | return ps->current->datacount; |
| 379 | } |
| 380 | |
| 381 | static int |
| 382 | sucker_read_buffer_next(png_sucker *ps) |
| 383 | { |
| 384 | png_sucker_buffer *pbOld = ps->next; |
| 385 | png_sucker_buffer *pbNew = &ps->current->data; |
| 386 | if (pbOld != pbNew) |
| 387 | { |
| 388 | while (pbNew != NULL && pbNew->prev != pbOld) |
| 389 | pbNew = pbNew->prev; |
| 390 | if (pbNew != NULL) |
| 391 | { |
| 392 | ps->next = pbNew; |
| 393 | ps->readpos = 0; |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | png_error(ps->pread, "buffer lost"); |
| 398 | } |
| 399 | |
| 400 | return 0; /* EOF or error */ |
| 401 | } |
| 402 | |
| 403 | static void |
| 404 | sucker_read(png_structp pp, png_bytep pb, png_size_t st) |
| 405 | { |
| 406 | png_sucker *ps = png_get_io_ptr(pp); |
| 407 | if (ps->pread != pp || ps->current == NULL || ps->next == NULL) |
| 408 | png_error(pp, "sucker state damaged"); |
| 409 | while (st > 0) |
| 410 | { |
| 411 | size_t cbAvail = sucker_read_buffer_size(ps) - ps->readpos; |
| 412 | |
| 413 | if (cbAvail > 0) |
| 414 | { |
| 415 | if (cbAvail > st) cbAvail = st; |
| 416 | memcpy(pb, ps->next->buffer + ps->readpos, cbAvail); |
| 417 | st -= cbAvail; |
| 418 | pb += cbAvail; |
| 419 | ps->readpos += cbAvail; |
| 420 | } |
| 421 | else if (!sucker_read_buffer_next(ps)) |
| 422 | png_error(pp, "read beyond end of file"); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* Setup functions. */ |
| 427 | /* Cleanup when aborting a write or after storing the new file. */ |
| 428 | static void |
| 429 | sucker_write_reset(png_sucker *ps) |
| 430 | { |
| 431 | if (ps->pwrite != NULL) |
| 432 | { |
| 433 | png_destroy_write_struct(&ps->pwrite, &ps->piwrite); |
| 434 | ps->pwrite = NULL; |
| 435 | ps->piwrite = NULL; |
| 436 | } |
| 437 | |
| 438 | sucker_freenew(ps); |
| 439 | } |
| 440 | |
| 441 | /* The following is the main write function, it returns a png_struct and, |
| 442 | * optionally, a png)info suitable for writiing a new PNG file. Use |
| 443 | * sucker_storefile above to record this file after it has been written. The |
| 444 | * returned libpng structures as destroyed by sucker_write_reset above. |
| 445 | */ |
| 446 | static png_structp |
| 447 | set_sucker_for_write(png_sucker *ps, png_infopp ppi, const char name[64]) |
| 448 | { |
| 449 | if (setjmp(ps->jmpbuf) != 0) |
| 450 | return NULL; |
| 451 | |
| 452 | if (ps->pwrite != NULL) |
| 453 | png_error(ps->pwrite, "sucker already in use"); |
| 454 | |
| 455 | sucker_write_reset(ps); |
| 456 | safecat(ps->wname, sizeof ps->wname, 0, name); |
| 457 | |
| 458 | ps->pwrite = png_create_write_struct(PNG_LIBPNG_VER_STRING, ps, sucker_error, |
| 459 | sucker_warning); |
| 460 | png_set_write_fn(ps->pwrite, ps, sucker_write, sucker_flush); |
| 461 | |
| 462 | if (ppi != NULL) |
| 463 | *ppi = ps->piwrite = png_create_info_struct(ps->pwrite); |
| 464 | |
| 465 | return ps->pwrite; |
| 466 | } |
| 467 | |
| 468 | /* Cleanup when finished reading (either due to error or in the success case. ) |
| 469 | */ |
| 470 | static void |
| 471 | sucker_read_reset(png_sucker *ps) |
| 472 | { |
| 473 | if (ps->pread != NULL) |
| 474 | { |
| 475 | png_destroy_read_struct(&ps->pread, &ps->piread, NULL); |
| 476 | ps->pread = NULL; |
| 477 | ps->piread = NULL; |
| 478 | } |
| 479 | |
| 480 | ps->current = NULL; |
| 481 | ps->next = NULL; |
| 482 | ps->readpos = 0; |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | sucker_read_set(png_sucker *ps, png_uint_32 id) |
| 487 | { |
| 488 | png_sucker_file *pf = ps->saved; |
| 489 | |
| 490 | while (pf != NULL) |
| 491 | { |
| 492 | if (pf->id == id) |
| 493 | { |
| 494 | ps->current = pf; |
| 495 | ps->next = NULL; |
| 496 | sucker_read_buffer_next(ps); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | pf = pf->next; |
| 501 | } |
| 502 | |
| 503 | png_error(ps->pread, "unable to find file to read"); |
| 504 | } |
| 505 | |
| 506 | /* The main interface for reading a saved file - pass the id number of the file |
| 507 | * to retrieve. Ids must be unique or the earlier file will be hidden. The API |
| 508 | * returns a png_struct and, optionally, a png_info. Both of these will be |
| 509 | * destroyed by sucker_read_reset above. |
| 510 | */ |
| 511 | static png_structp |
| 512 | set_sucker_for_read(png_sucker *ps, png_infopp ppi, png_uint_32 id, |
| 513 | const char *name) |
| 514 | { |
| 515 | safecat(ps->test, sizeof ps->test, 0, name); |
| 516 | |
| 517 | if (setjmp(ps->jmpbuf) != 0) |
| 518 | return NULL; |
| 519 | |
| 520 | if (ps->pread != NULL) |
| 521 | png_error(ps->pread, "sucker already in use"); |
| 522 | |
| 523 | sucker_read_reset(ps); |
| 524 | |
| 525 | ps->pread = png_create_read_struct(PNG_LIBPNG_VER_STRING, ps, sucker_error, |
| 526 | sucker_warning); |
| 527 | sucker_read_set(ps, id); |
| 528 | png_set_read_fn(ps->pread, ps, sucker_read); |
| 529 | |
| 530 | if (ppi != NULL) |
| 531 | *ppi = ps->piread = png_create_info_struct(ps->pread); |
| 532 | |
| 533 | return ps->pread; |
| 534 | } |
| 535 | |
| 536 | /*********************** PNG FILE MODIFICATION ON READ ************************/ |
| 537 | /* Files may be modified on read. The following structure contains a complete |
| 538 | * png_sucker together with extra members to handle modification and a special |
| 539 | * read callback for libpng. To use this the 'modifications' field must be set |
| 540 | * to a list of png_modification structures that actually perform the |
| 541 | * modification, otherwise a png_modifier is functionally equivalent to a |
| 542 | * png_sucker. There is a special read function, set_modifier_for_read, which |
| 543 | * replaces set_sucker_for_read. |
| 544 | */ |
| 545 | typedef struct png_modifier |
| 546 | { |
| 547 | png_sucker this; /* I am a png_sucker */ |
| 548 | struct png_modification *modifications; /* Changes to make */ |
| 549 | enum modifier_state |
| 550 | { |
| 551 | modifier_start, /* Initial value */ |
| 552 | modifier_signature, /* Have a signature */ |
| 553 | modifier_IHDR /* Have an IHDR */ |
| 554 | } state; /* My state */ |
| 555 | |
| 556 | /* Information from IHDR: */ |
| 557 | png_byte bit_depth; /* From IHDR */ |
| 558 | png_byte colour_type; /* From IHDR */ |
| 559 | |
| 560 | /* While handling PLTE, IDAT and IEND these chunks may be pended to allow |
| 561 | * other chunks to be inserted. |
| 562 | */ |
| 563 | png_uint_32 pending_len; |
| 564 | png_uint_32 pending_chunk; |
| 565 | |
| 566 | /* Test values */ |
| 567 | double *gammas; |
| 568 | unsigned ngammas; |
| 569 | |
| 570 | /* Lowest sbit to test (libpng fails for sbit < 8) */ |
| 571 | unsigned sbitlow; |
| 572 | |
| 573 | /* Error control - these are the limits on errors accepted by the gamma tests |
| 574 | * below. |
| 575 | */ |
| 576 | double maxout8; /* Maximum output value error */ |
| 577 | double maxabs8; /* Abosulte sample error 0..1 */ |
| 578 | double maxpc8; /* Percentage sample error 0..100% */ |
| 579 | double maxout16; /* Maximum output value error */ |
| 580 | double maxabs16; /* Absolute sample error 0..1 */ |
| 581 | double maxpc16; /* Percentage sample error 0..100% */ |
| 582 | |
| 583 | /* Logged 8 and 16 bit errors ('output' values): */ |
| 584 | double error_gray_2; |
| 585 | double error_gray_4; |
| 586 | double error_gray_8; |
| 587 | double error_gray_16; |
| 588 | double error_color_8; |
| 589 | double error_color_16; |
| 590 | |
| 591 | /* Flags: */ |
| 592 | /* When to use the use_input_precision option: */ |
| 593 | int use_input_precision :1; |
| 594 | int use_input_precision_sbit :1; |
| 595 | int use_input_precision_16to8 :1; |
| 596 | int log :1; /* Log max error */ |
| 597 | |
| 598 | /* Buffer information, the buffer size limits the size of the chunks that can |
| 599 | * be modified - they must fit (including header and CRC) into the buffer! |
| 600 | */ |
| 601 | size_t flush; /* Count of bytes to flush */ |
| 602 | size_t buffer_count; /* Bytes in buffer */ |
| 603 | size_t buffer_position; /* Position in buffer */ |
| 604 | png_byte buffer[1024]; |
| 605 | } png_modifier; |
| 606 | |
| 607 | static double abserr(png_modifier *pm, png_byte bit_depth) |
| 608 | { |
| 609 | return bit_depth == 16 ? pm->maxabs16 : pm->maxabs8; |
| 610 | } |
| 611 | |
| 612 | static double pcerr(png_modifier *pm, png_byte bit_depth) |
| 613 | { |
| 614 | return (bit_depth == 16 ? pm->maxpc16 : pm->maxpc8) * .01; |
| 615 | } |
| 616 | |
| 617 | static double outerr(png_modifier *pm, png_byte bit_depth) |
| 618 | { |
| 619 | /* There is a serious error in the 2 and 4 bit grayscale transform because |
| 620 | * the gamma table value (8 bits) is simply shifted, not rouned, so the |
| 621 | * error in 4 bit greyscale gamma is up to the value below. This is a hack |
| 622 | * to allow pngvalid to succeed: |
| 623 | */ |
| 624 | if (bit_depth == 2) return .73182-.5; |
| 625 | if (bit_depth == 4) return .90644-.5; |
| 626 | if (bit_depth == 16) return pm->maxout16; |
| 627 | return pm->maxout8; |
| 628 | } |
| 629 | |
| 630 | /* This returns true if the test should be stopped now because it has already |
| 631 | * failed and it is running silently. |
| 632 | */ |
| 633 | static int fail(png_modifier *pm) |
| 634 | { |
| 635 | return !pm->log && !pm->this.verbose && (pm->this.nerrors > 0 || |
| 636 | pm->this.treat_warnings_as_errors && pm->this.nwarnings > 0); |
| 637 | } |
| 638 | |
| 639 | static void |
| 640 | modifier_init(png_modifier *pm) |
| 641 | { |
| 642 | memset(pm, 0, sizeof *pm); |
| 643 | sucker_init(&pm->this); |
| 644 | pm->modifications = NULL; |
| 645 | pm->state = modifier_start; |
| 646 | pm->sbitlow = 1; |
| 647 | pm->maxout8 = pm->maxpc8 = pm->maxabs8 = 0; |
| 648 | pm->maxout16 = pm->maxpc16 = pm->maxabs16 = 0; |
| 649 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = 0; |
| 650 | pm->error_gray_16 = pm->error_color_8 = pm->error_color_16 = 0; |
| 651 | pm->use_input_precision = 0; |
| 652 | pm->use_input_precision_sbit = 0; |
| 653 | pm->use_input_precision_16to8 = 0; |
| 654 | pm->log = 0; |
| 655 | |
| 656 | /* Rely on the memset for all the other fields - there are no pointers */ |
| 657 | } |
| 658 | |
| 659 | /* One modification strucutre must be provided for each chunk to be modified (in |
| 660 | * fact more than one can be provided if multiple separate changes are desired |
| 661 | * for a single chunk.) Modifications include adding a new chunk when a |
| 662 | * suitable chunk does not exist. |
| 663 | * |
| 664 | * The caller of modify_fn will reset the CRC of the chunk and record 'modified' |
| 665 | * or 'added' as appropriate if the modify_fn returns 1 (true). If the |
| 666 | * modify_fn is NULL the chunk is simply removed. |
| 667 | */ |
| 668 | typedef struct png_modification |
| 669 | { |
| 670 | struct png_modification *next; |
| 671 | png_uint_32 chunk; |
| 672 | /* If the following is NULL all matching chunks will be removed: */ |
| 673 | int (*modify_fn)(png_structp pp, struct png_modifier *pm, |
| 674 | struct png_modification *me, int add); |
| 675 | /* If the following is set to PLTE, IDAT or IEND and the chunk has not been |
| 676 | * found and modified (and there is a modify_fn) the modify_fn will be called |
| 677 | * to add the chunk before the relevant chunk. |
| 678 | */ |
| 679 | png_uint_32 add; |
| 680 | int modified :1; /* Chunk was modified */ |
| 681 | int added :1; /* Chunk was added */ |
| 682 | int removed :1; /* Chunk was removed */ |
| 683 | } png_modification; |
| 684 | |
| 685 | static void modification_reset(png_modification *pmm) |
| 686 | { |
| 687 | if (pmm != NULL) |
| 688 | { |
| 689 | pmm->modified = 0; |
| 690 | pmm->added = 0; |
| 691 | pmm->removed = 0; |
| 692 | modification_reset(pmm->next); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static void |
| 697 | modification_init(png_modification *pmm) |
| 698 | { |
| 699 | memset(pmm, 0, sizeof *pmm); |
| 700 | pmm->next = NULL; |
| 701 | pmm->chunk = 0; |
| 702 | pmm->modify_fn = NULL; |
| 703 | pmm->add = 0; |
| 704 | modification_reset(pmm); |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | modifier_reset(png_modifier *pm) |
| 709 | { |
| 710 | sucker_read_reset(&pm->this); |
| 711 | pm->modifications = NULL; |
| 712 | pm->state = modifier_start; |
| 713 | pm->bit_depth = pm->colour_type = 0; |
| 714 | pm->pending_len = pm->pending_chunk = 0; |
| 715 | pm->flush = pm->buffer_count = pm->buffer_position = 0; |
| 716 | } |
| 717 | |
| 718 | /* Convenience macros. */ |
| 719 | #define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d)) |
| 720 | #define CHUNK_IHDR CHUNK(73,72,68,82) |
| 721 | #define CHUNK_PLTE CHUNK(80,76,84,69) |
| 722 | #define CHUNK_IDAT CHUNK(73,68,65,84) |
| 723 | #define CHUNK_IEND CHUNK(73,69,78,68) |
| 724 | #define CHUNK_cHRM CHUNK(99,72,82,77) |
| 725 | #define CHUNK_gAMA CHUNK(103,65,77,65) |
| 726 | #define CHUNK_sBIT CHUNK(115,66,73,84) |
| 727 | #define CHUNK_sRGB CHUNK(115,82,71,66) |
| 728 | |
| 729 | /* The guts of modification are performed during a read. */ |
| 730 | static void |
| 731 | modifier_crc(png_bytep buffer) |
| 732 | { |
| 733 | /* Recalculate the chunk CRC - a complete chunk must be in |
| 734 | * the buffer, at the start. |
| 735 | */ |
| 736 | uInt datalen = png_get_uint_32(buffer); |
| 737 | png_save_uint_32(buffer+datalen+8, crc32(0L, buffer+4, datalen+4)); |
| 738 | } |
| 739 | |
| 740 | static void |
| 741 | modifier_setbuffer(png_modifier *pm) |
| 742 | { |
| 743 | modifier_crc(pm->buffer); |
| 744 | pm->buffer_count = png_get_uint_32(pm->buffer)+12; |
| 745 | pm->buffer_position = 0; |
| 746 | } |
| 747 | |
| 748 | static void |
| 749 | modifier_read(png_structp pp, png_bytep pb, png_size_t st) |
| 750 | { |
| 751 | png_modifier *pm = png_get_io_ptr(pp); |
| 752 | |
| 753 | while (st > 0) |
| 754 | { |
| 755 | size_t cb; |
| 756 | png_uint_32 len, chunk; |
| 757 | png_modification *mod; |
| 758 | |
| 759 | if (pm->buffer_position >= pm->buffer_count) switch (pm->state) |
| 760 | { |
| 761 | static png_byte sign[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; |
| 762 | case modifier_start: |
| 763 | sucker_read(pp, pm->buffer, 8); /* size of signature. */ |
| 764 | pm->buffer_count = 8; |
| 765 | pm->buffer_position = 0; |
| 766 | |
| 767 | if (memcmp(pm->buffer, sign, 8) != 0) |
| 768 | png_error(pp, "invalid PNG file signature"); |
| 769 | pm->state = modifier_signature; |
| 770 | break; |
| 771 | |
| 772 | case modifier_signature: |
| 773 | sucker_read(pp, pm->buffer, 13+12); /* size of IHDR */ |
| 774 | pm->buffer_count = 13+12; |
| 775 | pm->buffer_position = 0; |
| 776 | |
| 777 | if (png_get_uint_32(pm->buffer) != 13 || |
| 778 | png_get_uint_32(pm->buffer+4) != CHUNK_IHDR) |
| 779 | png_error(pp, "invalid IHDR"); |
| 780 | |
| 781 | /* Check the list of modifiers for modifications to the IHDR. */ |
| 782 | mod = pm->modifications; |
| 783 | while (mod != NULL) |
| 784 | { |
| 785 | if (mod->chunk == CHUNK_IHDR && mod->modify_fn && |
| 786 | (*mod->modify_fn)(pp, pm, mod, 0)) |
| 787 | { |
| 788 | mod->modified = 1; |
| 789 | modifier_setbuffer(pm); |
| 790 | } |
| 791 | |
| 792 | /* Ignore removal or add if IHDR! */ |
| 793 | mod = mod->next; |
| 794 | } |
| 795 | |
| 796 | /* Cache information from the IHDR (the modified one.) */ |
| 797 | pm->bit_depth = pm->buffer[8+8]; |
| 798 | pm->colour_type = pm->buffer[8+8+1]; |
| 799 | |
| 800 | pm->state = modifier_IHDR; |
| 801 | pm->flush = 0; |
| 802 | break; |
| 803 | |
| 804 | default: |
| 805 | /* Read a new chunk and process it until we see PLTE, IDAT or |
| 806 | * IEND. 'flush' indicates that there is still some data to |
| 807 | * output from the preceding chunk. |
| 808 | */ |
| 809 | if ((cb = pm->flush) > 0) |
| 810 | { |
| 811 | if (cb > st) cb = st; |
| 812 | pm->flush -= cb; |
| 813 | sucker_read(pp, pb, cb); |
| 814 | pb += cb; |
| 815 | st -= cb; |
| 816 | if (st <= 0) return; |
| 817 | } |
| 818 | |
| 819 | /* No more bytes to flush, read a header, or handle a pending |
| 820 | * chunk. |
| 821 | */ |
| 822 | if (pm->pending_chunk != 0) |
| 823 | { |
| 824 | png_save_uint_32(pm->buffer, pm->pending_len); |
| 825 | png_save_uint_32(pm->buffer+4, pm->pending_chunk); |
| 826 | pm->pending_len = 0; |
| 827 | pm->pending_chunk = 0; |
| 828 | } |
| 829 | else |
| 830 | sucker_read(pp, pm->buffer, 8); |
| 831 | |
| 832 | pm->buffer_count = 8; |
| 833 | pm->buffer_position = 0; |
| 834 | |
| 835 | /* Check for something to modify or a terminator chunk. */ |
| 836 | len = png_get_uint_32(pm->buffer); |
| 837 | chunk = png_get_uint_32(pm->buffer+4); |
| 838 | |
| 839 | /* Terminators first, they may have to be delayed for added |
| 840 | * chunks |
| 841 | */ |
| 842 | if (chunk == CHUNK_PLTE || chunk == CHUNK_IDAT || chunk == CHUNK_IEND) |
| 843 | { |
| 844 | mod = pm->modifications; |
| 845 | |
| 846 | while (mod != NULL) |
| 847 | { |
| 848 | if ((mod->add == chunk || |
| 849 | mod->add == CHUNK_PLTE && chunk == CHUNK_IDAT) && |
| 850 | mod->modify_fn != NULL && !mod->modified && !mod->added) |
| 851 | { |
| 852 | /* Regardless of what the modify function does do not run this |
| 853 | * again. |
| 854 | */ |
| 855 | mod->added = 1; |
| 856 | |
| 857 | if ((*mod->modify_fn)(pp, pm, mod, 1/*add*/)) |
| 858 | { |
| 859 | /* Reset the CRC on a new chunk */ |
| 860 | if (pm->buffer_count > 0) |
| 861 | modifier_setbuffer(pm); |
| 862 | else |
| 863 | { |
| 864 | pm->buffer_position = 0; |
| 865 | mod->removed = 1; |
| 866 | } |
| 867 | |
| 868 | /* The buffer has been filled with something (we assume) so |
| 869 | * output this. Pend the current chunk. |
| 870 | */ |
| 871 | pm->pending_len = len; |
| 872 | pm->pending_chunk = chunk; |
| 873 | break; /* out of while */ |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | mod = mod->next; |
| 878 | } |
| 879 | |
| 880 | /* Don't do any further processing if the buffer was modified - |
| 881 | * otherwise the code will end up modifying a chunk that was just |
| 882 | * added. |
| 883 | */ |
| 884 | if (mod != NULL) |
| 885 | break; /* out of switch */ |
| 886 | } |
| 887 | |
| 888 | /* If we get to here then this chunk may need to be modified. To do |
| 889 | * this is must be less than 1024 bytes in total size, otherwise |
| 890 | * it just gets flushed. |
| 891 | */ |
| 892 | if (len+12 <= sizeof pm->buffer) |
| 893 | { |
| 894 | sucker_read(pp, pm->buffer+pm->buffer_count, |
| 895 | len+12-pm->buffer_count); |
| 896 | pm->buffer_count = len+12; |
| 897 | |
| 898 | /* Check for a modification, else leave it be. */ |
| 899 | mod = pm->modifications; |
| 900 | while (mod != NULL) |
| 901 | { |
| 902 | if (mod->chunk == chunk) |
| 903 | { |
| 904 | if (mod->modify_fn == NULL) |
| 905 | { |
| 906 | /* Remove this chunk */ |
| 907 | pm->buffer_count = pm->buffer_position = 0; |
| 908 | mod->removed = 1; |
| 909 | break; /* Terminate the while loop */ |
| 910 | } |
| 911 | else if ((*mod->modify_fn)(pp, pm, mod, 0)) |
| 912 | { |
| 913 | mod->modified = 1; |
| 914 | /* The chunk may have been removed: */ |
| 915 | if (pm->buffer_count == 0) |
| 916 | { |
| 917 | pm->buffer_position = 0; |
| 918 | break; |
| 919 | } |
| 920 | modifier_setbuffer(pm); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | mod = mod->next; |
| 925 | } |
| 926 | } |
| 927 | else |
| 928 | pm->flush = len+12 - pm->buffer_count; /* data + crc */ |
| 929 | |
| 930 | /* Take the data from the buffer (if there is any). */ |
| 931 | break; |
| 932 | } |
| 933 | |
| 934 | /* Here to read from the modifier buffer (not directly from |
| 935 | * the sucker, as in the flush case above.) |
| 936 | */ |
| 937 | cb = pm->buffer_count - pm->buffer_position; |
| 938 | if (cb > st) cb = st; |
| 939 | memcpy(pb, pm->buffer + pm->buffer_position, cb); |
| 940 | st -= cb; |
| 941 | pb += cb; |
| 942 | pm->buffer_position += cb; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /* Set up a modifier. */ |
| 947 | static png_structp |
| 948 | set_modifier_for_read(png_modifier *pm, png_infopp ppi, png_uint_32 id, |
| 949 | const char *name) |
| 950 | { |
| 951 | png_structp pp = set_sucker_for_read(&pm->this, ppi, id, name); |
| 952 | |
| 953 | if (pp != NULL) |
| 954 | { |
| 955 | if (setjmp(pm->this.jmpbuf) == 0) |
| 956 | { |
| 957 | png_set_read_fn(pp, pm, modifier_read); |
| 958 | |
| 959 | pm->state = modifier_start; |
| 960 | pm->bit_depth = 0; |
| 961 | pm->colour_type = 255; |
| 962 | |
| 963 | pm->pending_len = 0; |
| 964 | pm->pending_chunk = 0; |
| 965 | pm->flush = 0; |
| 966 | pm->buffer_count = 0; |
| 967 | pm->buffer_position = 0; |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | sucker_read_reset(&pm->this); |
| 972 | pp = NULL; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | return pp; |
| 977 | } |
| 978 | |
| 979 | /***************************** STANDARD PNG FILES *****************************/ |
| 980 | /* Standard files - write and save standard files. */ |
| 981 | /* The standard files are constructed with rows which fit into a 1024 byte row |
| 982 | * buffer. This makes allocation easier below. Further regardless of the file |
| 983 | * format every file has 128 pixels (giving 1024 bytes for 64bpp formats). |
| 984 | * |
| 985 | * Files are stored with no gAMA or sBIT chunks, with a PLTE only when needed |
| 986 | * and with an ID derived from the colour type and bit depth as follows: |
| 987 | */ |
| 988 | #define FILEID(col, depth) ((png_uint_32)((col) + ((depth)<<3))) |
| 989 | #define COL_FROM_ID(id) ((id)& 0x7) |
| 990 | #define DEPTH_FROM_ID(id) (((id) >> 3) & 0x1f) |
| 991 | |
| 992 | #define STD_WIDTH 128 |
| 993 | #define STD_ROWMAX (STD_WIDTH*8) |
| 994 | |
| 995 | static unsigned |
| 996 | bit_size(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 997 | { |
| 998 | switch (colour_type) |
| 999 | { |
| 1000 | case 0: return bit_depth; |
| 1001 | case 2: return 3*bit_depth; |
| 1002 | case 3: return bit_depth; |
| 1003 | case 4: return 2*bit_depth; |
| 1004 | case 6: return 4*bit_depth; |
| 1005 | default: png_error(pp, "invalid color type"); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | static size_t |
| 1010 | standard_rowsize(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1011 | { |
| 1012 | return (STD_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8; |
| 1013 | } |
| 1014 | |
| 1015 | static png_uint_32 |
| 1016 | standard_width(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1017 | { |
| 1018 | return STD_WIDTH; |
| 1019 | } |
| 1020 | |
| 1021 | static png_uint_32 |
| 1022 | standard_height(png_structp pp, png_byte colour_type, png_byte bit_depth) |
| 1023 | { |
| 1024 | switch (bit_size(pp, colour_type, bit_depth)) |
| 1025 | { |
| 1026 | case 1: |
| 1027 | case 2: |
| 1028 | case 4: |
| 1029 | return 1; /* Total of 128 pixels */ |
| 1030 | case 8: |
| 1031 | return 2; /* Total of 256 pixels/bytes */ |
| 1032 | case 16: |
| 1033 | return 512; /* Total of 65536 pixels */ |
| 1034 | case 24: |
| 1035 | case 32: |
| 1036 | return 512; /* 65536 pixels */ |
| 1037 | case 48: |
| 1038 | case 64: |
| 1039 | return 2048;/* 4 x 65536 pixels. */ |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | static void |
| 1044 | standard_row(png_structp pp, png_byte buffer[STD_ROWMAX], png_byte colour_type, |
| 1045 | png_byte bit_depth, png_uint_32 y) |
| 1046 | { |
| 1047 | png_uint_32 v = y << 7; |
| 1048 | png_uint_32 i = 0; |
| 1049 | |
| 1050 | switch (bit_size(pp, colour_type, bit_depth)) |
| 1051 | { |
| 1052 | case 1: |
| 1053 | while (i<128/8) buffer[i] = v & 0xff, v += 17, ++i; |
| 1054 | return; |
| 1055 | case 2: |
| 1056 | while (i<128/4) buffer[i] = v & 0xff, v += 33, ++i; |
| 1057 | return; |
| 1058 | case 4: |
| 1059 | while (i<128/2) buffer[i] = v & 0xff, v += 65, ++i; |
| 1060 | return; |
| 1061 | case 8: |
| 1062 | /* 256 bytes total, 128 bytes in each row set as follows: */ |
| 1063 | while (i<128) buffer[i] = v & 0xff, ++v, ++i; |
| 1064 | return; |
| 1065 | case 16: |
| 1066 | /* Generate all 65536 pixel values in order, this includes the 8 bit GA |
| 1067 | * case as we as the 16 bit G case. |
| 1068 | */ |
| 1069 | while (i<128) |
| 1070 | buffer[2*i] = (v>>8) & 0xff, buffer[2*i+1] = v & 0xff, ++v, ++i; |
| 1071 | return; |
| 1072 | case 24: |
| 1073 | /* 65535 pixels, but rotate the values. */ |
| 1074 | while (i<128) |
| 1075 | { |
| 1076 | /* Three bytes per pixel, r, g, b, make b by r^g */ |
| 1077 | buffer[3*i+0] = (v >> 8) & 0xff; |
| 1078 | buffer[3*i+1] = v & 0xff; |
| 1079 | buffer[3*i+2] = ((v >> 8) ^ v) & 0xff; |
| 1080 | ++v; |
| 1081 | ++i; |
| 1082 | } |
| 1083 | return; |
| 1084 | case 32: |
| 1085 | /* 65535 pixels, r, g, b, a; just replicate */ |
| 1086 | while (i<128) |
| 1087 | { |
| 1088 | buffer[4*i+0] = (v >> 8) & 0xff; |
| 1089 | buffer[4*i+1] = v & 0xff; |
| 1090 | buffer[4*i+2] = (v >> 8) & 0xff; |
| 1091 | buffer[4*i+3] = v & 0xff; |
| 1092 | ++v; |
| 1093 | ++i; |
| 1094 | } |
| 1095 | return; |
| 1096 | case 48: |
| 1097 | /* y is maximum 2047, giving 4x65536 pixels, make 'r' increase by 1 at |
| 1098 | * each pixel, g increase by 257 (0x101) and 'b' by 0x1111: |
| 1099 | */ |
| 1100 | while (i<128) |
| 1101 | { |
| 1102 | png_uint_32 t = v++; |
| 1103 | buffer[6*i+0] = (t >> 8) & 0xff; |
| 1104 | buffer[6*i+1] = t & 0xff; |
| 1105 | t *= 257; |
| 1106 | buffer[6*i+2] = (t >> 8) & 0xff; |
| 1107 | buffer[6*i+3] = t & 0xff; |
| 1108 | t *= 17; |
| 1109 | buffer[6*i+4] = (t >> 8) & 0xff; |
| 1110 | buffer[6*i+5] = t & 0xff; |
| 1111 | ++i; |
| 1112 | } |
| 1113 | return; |
| 1114 | case 64: |
| 1115 | /* As above in the 32 bit case. */ |
| 1116 | while (i<128) |
| 1117 | { |
| 1118 | png_uint_32 t = v++; |
| 1119 | buffer[8*i+0] = (t >> 8) & 0xff; |
| 1120 | buffer[8*i+1] = t & 0xff; |
| 1121 | buffer[8*i+4] = (t >> 8) & 0xff; |
| 1122 | buffer[8*i+5] = t & 0xff; |
| 1123 | t *= 257; |
| 1124 | buffer[8*i+2] = (t >> 8) & 0xff; |
| 1125 | buffer[8*i+3] = t & 0xff; |
| 1126 | buffer[8*i+6] = (t >> 8) & 0xff; |
| 1127 | buffer[8*i+7] = t & 0xff; |
| 1128 | ++i; |
| 1129 | } |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | png_error(pp, "internal error"); |
| 1134 | } |
| 1135 | |
| 1136 | static void |
| 1137 | make_standard(png_sucker* ps, png_byte colour_type, int bdlo, int bdhi) |
| 1138 | { |
| 1139 | for (; bdlo <= bdhi; ++bdlo) |
| 1140 | { |
| 1141 | png_byte bit_depth = 1U << bdlo; |
| 1142 | png_uint_32 h, y; |
| 1143 | png_structp pp; |
| 1144 | png_infop pi; |
| 1145 | |
| 1146 | { |
| 1147 | size_t pos; |
| 1148 | char name[64]; /* Same size as the buffer in a file. */ |
| 1149 | |
| 1150 | /* Build a name */ |
| 1151 | pos = safecat(name, sizeof name, 0, bit_depths[bdlo]); |
| 1152 | pos = safecat(name, sizeof name, pos, "bit "); |
| 1153 | pos = safecat(name, sizeof name, pos, colour_types[colour_type]); |
| 1154 | |
| 1155 | /* Get a png_struct for writing the image. */ |
| 1156 | pp = set_sucker_for_write(ps, &pi, name); |
| 1157 | } |
| 1158 | if (pp == NULL) return; |
| 1159 | |
| 1160 | /* Do the honourable write stuff, protected by a local setjmp */ |
| 1161 | if (setjmp(ps->jmpbuf) != 0) |
| 1162 | { |
| 1163 | sucker_write_reset(ps); |
| 1164 | continue; |
| 1165 | } |
| 1166 | |
| 1167 | h = standard_height(pp, colour_type, bit_depth), |
| 1168 | png_set_IHDR(pp, pi, standard_width(pp, colour_type, bit_depth), h, |
| 1169 | bit_depth, colour_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, |
| 1170 | PNG_FILTER_TYPE_BASE); |
| 1171 | |
| 1172 | if (colour_type == 3) /* palette */ |
| 1173 | { |
| 1174 | int i; |
| 1175 | png_color pal[256]; |
| 1176 | for (i=0; i<256; ++i) pal[i].red = pal[i].green = pal[i].blue = i; |
| 1177 | png_set_PLTE(pp, pi, pal, 256); |
| 1178 | } |
| 1179 | |
| 1180 | png_write_info(pp, pi); |
| 1181 | |
| 1182 | if (png_get_rowbytes(pp, pi) != |
| 1183 | standard_rowsize(pp, colour_type, bit_depth)) |
| 1184 | png_error(pp, "row size incorrect"); |
| 1185 | |
| 1186 | else for (y=0; y<h; ++y) |
| 1187 | { |
| 1188 | png_byte buffer[STD_ROWMAX]; |
| 1189 | standard_row(pp, buffer, colour_type, bit_depth, y); |
| 1190 | png_write_row(pp, buffer); |
| 1191 | } |
| 1192 | |
| 1193 | png_write_end(pp, pi); |
| 1194 | |
| 1195 | /* And store this under the appropriate id, then clean up. */ |
| 1196 | sucker_storefile(ps, FILEID(colour_type, bit_depth)); |
| 1197 | |
| 1198 | sucker_write_reset(ps); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | static void |
| 1203 | make_standard_images(png_sucker *ps) |
| 1204 | { |
| 1205 | /* Arguments are colour_type, low bit depth, high bit depth */ |
| 1206 | make_standard(ps, 0, 0, 4); |
| 1207 | make_standard(ps, 2, 3, 4); |
| 1208 | make_standard(ps, 3, 0, 3); |
| 1209 | make_standard(ps, 4, 3, 4); |
| 1210 | make_standard(ps, 6, 3, 4); |
| 1211 | } |
| 1212 | |
| 1213 | /* Tests - individual test cases */ |
| 1214 | static void |
| 1215 | test_standard(png_sucker* ps, png_byte colour_type, int bdlo, int bdhi) |
| 1216 | { |
| 1217 | for (; bdlo <= bdhi; ++bdlo) |
| 1218 | { |
| 1219 | png_byte bit_depth = 1U << bdlo; |
| 1220 | png_uint_32 h, y; |
| 1221 | size_t cb; |
| 1222 | png_structp pp; |
| 1223 | png_infop pi; |
| 1224 | |
| 1225 | /* Get a png_struct for writing the image. */ |
| 1226 | pp = set_sucker_for_read(ps, &pi, FILEID(colour_type, bit_depth), |
| 1227 | "standard"); |
| 1228 | if (pp == NULL) return; |
| 1229 | |
| 1230 | /* Do the honourable write stuff, protected by a local setjmp */ |
| 1231 | if (setjmp(ps->jmpbuf) != 0) |
| 1232 | { |
| 1233 | sucker_read_reset(ps); |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | h = standard_height(pp, colour_type, bit_depth); |
| 1238 | |
| 1239 | /* Check the header values: */ |
| 1240 | png_read_info(pp, pi); |
| 1241 | |
| 1242 | if (png_get_image_width(pp, pi) != |
| 1243 | standard_width(pp, colour_type, bit_depth)) |
| 1244 | png_error(pp, "validate: image width changed"); |
| 1245 | if (png_get_image_height(pp, pi) != h) |
| 1246 | png_error(pp, "validate: image height changed"); |
| 1247 | if (png_get_bit_depth(pp, pi) != bit_depth) |
| 1248 | png_error(pp, "validate: bit depth changed"); |
| 1249 | if (png_get_color_type(pp, pi) != colour_type) |
| 1250 | png_error(pp, "validate: color type changed"); |
| 1251 | if (png_get_filter_type(pp, pi) != PNG_FILTER_TYPE_BASE) |
| 1252 | png_error(pp, "validate: filter type changed"); |
| 1253 | if (png_get_interlace_type(pp, pi) != PNG_INTERLACE_NONE) |
| 1254 | png_error(pp, "validate: interlacing changed"); |
| 1255 | if (png_get_compression_type(pp, pi) != PNG_COMPRESSION_TYPE_BASE) |
| 1256 | png_error(pp, "validate: compression type changed"); |
| 1257 | if (png_set_interlace_handling(pp) != 1) |
| 1258 | png_error(pp, "validate: interlacing unexpected"); |
| 1259 | |
| 1260 | if (colour_type == 3) /* palette */ |
| 1261 | { |
| 1262 | png_colorp pal; |
| 1263 | int num; |
| 1264 | if (png_get_PLTE(pp, pi, &pal, &num) & PNG_INFO_PLTE) |
| 1265 | { |
| 1266 | int i; |
| 1267 | if (num != 256) |
| 1268 | png_error(pp, "validate: color type 3 PLTE chunk size changed"); |
| 1269 | for (i=0; i<num; ++i) |
| 1270 | if (pal[i].red != i || pal[i].green != i || pal[i].blue != i) |
| 1271 | png_error(pp, "validate: color type 3 PLTE chunk changed"); |
| 1272 | } |
| 1273 | else |
| 1274 | png_error(pp, "validate: missing PLTE with color type 3"); |
| 1275 | } |
| 1276 | |
| 1277 | cb = standard_rowsize(pp, colour_type, bit_depth); |
| 1278 | png_start_read_image(pp); |
| 1279 | |
| 1280 | if (png_get_rowbytes(pp, pi) != cb) |
| 1281 | png_error(pp, "validate: row size changed"); |
| 1282 | |
| 1283 | else for (y=0; y<h; ++y) |
| 1284 | { |
| 1285 | png_byte std[STD_ROWMAX]; |
| 1286 | png_byte read[STD_ROWMAX]; |
| 1287 | png_byte display[STD_ROWMAX]; |
| 1288 | |
| 1289 | standard_row(pp, std, colour_type, bit_depth, y); |
| 1290 | png_read_row(pp, read, display); |
| 1291 | |
| 1292 | if (memcmp(std, read, cb) != 0) |
| 1293 | { |
| 1294 | char msg[64]; |
| 1295 | sprintf(msg, "validate: PNG image row %d (of %d) changed", y, |
| 1296 | h); |
| 1297 | png_error(pp, msg); |
| 1298 | } |
| 1299 | if (memcmp(std, display, cb) != 0) |
| 1300 | { |
| 1301 | char msg[64]; |
| 1302 | sprintf(msg, "validate: transformed row %d (of %d) changed", y, h); |
| 1303 | png_error(pp, msg); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | png_read_end(pp, pi); |
| 1308 | |
| 1309 | sucker_read_reset(ps); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | static void |
| 1314 | perform_standard_test(png_modifier *pm) |
| 1315 | { |
| 1316 | test_standard(&pm->this, 0, 0, 4); |
| 1317 | if (fail(pm)) return; |
| 1318 | test_standard(&pm->this, 2, 3, 4); |
| 1319 | if (fail(pm)) return; |
| 1320 | test_standard(&pm->this, 3, 0, 3); |
| 1321 | if (fail(pm)) return; |
| 1322 | test_standard(&pm->this, 4, 3, 4); |
| 1323 | if (fail(pm)) return; |
| 1324 | test_standard(&pm->this, 6, 3, 4); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | /********************************* GAMMA TESTS ********************************/ |
| 1329 | /* Gamma test images. */ |
| 1330 | static void |
| 1331 | make_gamma_images(png_sucker *ps) |
| 1332 | { |
| 1333 | /* Do nothing - the standard greyscale images are used. */ |
| 1334 | } |
| 1335 | |
| 1336 | typedef struct gamma_modification |
| 1337 | { |
| 1338 | png_modification this; |
| 1339 | png_fixed_point gamma; |
| 1340 | } |
| 1341 | gamma_modification; |
| 1342 | |
| 1343 | static int |
| 1344 | gamma_modify(png_structp pp, png_modifier *pm, png_modification *me, int add) |
| 1345 | { |
| 1346 | /* This simply dumps the given gamma value into the buffer. */ |
| 1347 | png_save_uint_32(pm->buffer, 4); |
| 1348 | png_save_uint_32(pm->buffer+4, CHUNK_gAMA); |
| 1349 | png_save_uint_32(pm->buffer+8, ((gamma_modification*)me)->gamma); |
| 1350 | return 1; |
| 1351 | } |
| 1352 | |
| 1353 | static void |
| 1354 | gamma_modification_init(gamma_modification *me, png_modifier *pm, double gamma) |
| 1355 | { |
| 1356 | modification_init(&me->this); |
| 1357 | me->this.chunk = CHUNK_gAMA; |
| 1358 | me->this.modify_fn = gamma_modify; |
| 1359 | me->this.add = CHUNK_PLTE; |
| 1360 | me->gamma = floor(gamma * 100000 + .5); |
| 1361 | me->this.next = pm->modifications; |
| 1362 | pm->modifications = &me->this; |
| 1363 | } |
| 1364 | |
| 1365 | typedef struct srgb_modification |
| 1366 | { |
| 1367 | png_modification this; |
| 1368 | png_byte intent; |
| 1369 | } |
| 1370 | srgb_modification; |
| 1371 | |
| 1372 | static int |
| 1373 | srgb_modify(png_structp pp, png_modifier *pm, png_modification *me, int add) |
| 1374 | { |
| 1375 | /* As above, ignore add and just make a new chunk */ |
| 1376 | png_save_uint_32(pm->buffer, 1); |
| 1377 | png_save_uint_32(pm->buffer+4, CHUNK_sRGB); |
| 1378 | pm->buffer[8] = ((srgb_modification*)me)->intent; |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
| 1382 | static void |
| 1383 | srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent) |
| 1384 | { |
| 1385 | modification_init(&me->this); |
| 1386 | me->this.chunk = CHUNK_sBIT; |
| 1387 | if (intent <= 3) /* if valid, else *delete* sRGB chunks */ |
| 1388 | { |
| 1389 | me->this.modify_fn = srgb_modify; |
| 1390 | me->this.add = CHUNK_PLTE; |
| 1391 | me->intent = intent; |
| 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | me->this.modify_fn = 0; |
| 1396 | me->this.add = 0; |
| 1397 | me->intent = 0; |
| 1398 | } |
| 1399 | me->this.next = pm->modifications; |
| 1400 | pm->modifications = &me->this; |
| 1401 | } |
| 1402 | |
| 1403 | typedef struct sbit_modification |
| 1404 | { |
| 1405 | png_modification this; |
| 1406 | png_byte sbit; |
| 1407 | } |
| 1408 | sbit_modification; |
| 1409 | |
| 1410 | static int |
| 1411 | sbit_modify(png_structp pp, png_modifier *pm, png_modification *me, int add) |
| 1412 | { |
| 1413 | png_byte sbit = ((sbit_modification*)me)->sbit; |
| 1414 | if (pm->bit_depth > sbit) |
| 1415 | { |
| 1416 | int cb = 0; |
| 1417 | switch (pm->colour_type) |
| 1418 | { |
| 1419 | case 0: cb = 1; break; |
| 1420 | case 2: |
| 1421 | case 3: cb = 3; break; |
| 1422 | case 4: cb = 2; break; |
| 1423 | case 6: cb = 4; break; |
| 1424 | default: |
| 1425 | png_error(pp, "unexpected colour type in sBIT modification"); |
| 1426 | } |
| 1427 | |
| 1428 | png_save_uint_32(pm->buffer, cb); |
| 1429 | png_save_uint_32(pm->buffer+4, CHUNK_sBIT); |
| 1430 | while (cb > 0) |
| 1431 | (pm->buffer+8)[--cb] = sbit; |
| 1432 | |
| 1433 | return 1; |
| 1434 | } |
| 1435 | else if (!add) |
| 1436 | { |
| 1437 | /* Remove the sBIT chunk */ |
| 1438 | pm->buffer_count = pm->buffer_position = 0; |
| 1439 | return 1; |
| 1440 | } |
| 1441 | else |
| 1442 | return 0; /* do nothing */ |
| 1443 | } |
| 1444 | |
| 1445 | static void |
| 1446 | sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit) |
| 1447 | { |
| 1448 | modification_init(&me->this); |
| 1449 | me->this.chunk = CHUNK_sBIT; |
| 1450 | me->this.modify_fn = sbit_modify; |
| 1451 | me->this.add = CHUNK_PLTE; |
| 1452 | me->sbit = sbit; |
| 1453 | me->this.next = pm->modifications; |
| 1454 | pm->modifications = &me->this; |
| 1455 | } |
| 1456 | |
| 1457 | /* maxabs: maximum absolute error as a fraction |
| 1458 | * maxout: maximum output error in the output units |
| 1459 | * maxpc: maximum percentage error (as a percentage) |
| 1460 | */ |
| 1461 | static void |
| 1462 | gamma_test(png_modifier *pm, const png_byte colour_type, |
| 1463 | const png_byte bit_depth, const double file_gamma, const double screen_gamma, |
| 1464 | const png_byte sbit, const int threshold_test, const char *name, |
| 1465 | const int speed, const int use_input_precision, const int strip16) |
| 1466 | { |
| 1467 | png_structp pp; |
| 1468 | png_infop pi; |
| 1469 | double maxerrout = 0, maxerrpc = 0, maxerrabs = 0; |
| 1470 | |
| 1471 | gamma_modification gamma_mod; |
| 1472 | srgb_modification srgb_mod; |
| 1473 | sbit_modification sbit_mod; |
| 1474 | |
| 1475 | /* Make an appropriate modifier to set the PNG file gamma to the |
| 1476 | * given gamma value and the sBIT chunk to the given precision. |
| 1477 | */ |
| 1478 | pm->modifications = NULL; |
| 1479 | gamma_modification_init(&gamma_mod, pm, file_gamma); |
| 1480 | srgb_modification_init(&srgb_mod, pm, 127/*delete*/); |
| 1481 | sbit_modification_init(&sbit_mod, pm, sbit); |
| 1482 | |
| 1483 | modification_reset(pm->modifications); |
| 1484 | |
| 1485 | /* Get a png_struct for writing the image. */ |
| 1486 | pp = set_modifier_for_read(pm, &pi, FILEID(colour_type, bit_depth), name); |
| 1487 | if (pp == NULL) return; |
| 1488 | |
| 1489 | /* Do the honourable write stuff, protected by a local setjmp */ |
| 1490 | if (setjmp(pm->this.jmpbuf) != 0) |
| 1491 | { |
| 1492 | modifier_reset(pm); |
| 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | /* Set up gamma processing. */ |
| 1497 | png_set_gamma(pp, screen_gamma, file_gamma); |
| 1498 | |
| 1499 | /* Check the header values: */ |
| 1500 | png_read_info(pp, pi); |
| 1501 | |
| 1502 | /* If requested strip 16 to 8 bits - this is handled automagically below |
| 1503 | * because the output bit depth is read from the library. Note that there |
| 1504 | * are interactions with sBIT but, internally, libpng makes sbit at most |
| 1505 | * PNG_MAX_GAMMA_8 when doing the following. |
| 1506 | */ |
| 1507 | if (strip16) |
| 1508 | png_set_strip_16(pp); |
| 1509 | |
| 1510 | if (png_set_interlace_handling(pp) != 1) |
| 1511 | png_error(pp, "gamma: interlaced images not supported"); |
| 1512 | |
| 1513 | png_read_update_info(pp, pi); |
| 1514 | |
| 1515 | { |
| 1516 | const png_byte out_ct = png_get_color_type(pp, pi); |
| 1517 | const png_byte out_bd = png_get_bit_depth(pp, pi); |
| 1518 | const unsigned outmax = (1U<<out_bd)-1; |
| 1519 | const png_uint_32 w = png_get_image_width(pp, pi); |
| 1520 | const png_uint_32 h = png_get_image_height(pp, pi); |
| 1521 | const size_t cb = png_get_rowbytes(pp, pi); /* For the memcmp below. */ |
| 1522 | const double maxabs = abserr(pm, out_bd); |
| 1523 | const double maxout = outerr(pm, out_bd); |
| 1524 | const double maxpc = pcerr(pm, out_bd); |
| 1525 | png_uint_32 y; |
| 1526 | |
| 1527 | /* There are three sources of error, firstly the quantization in the file |
| 1528 | * encoding, determined by sbit and/or the file depth, secondly the output |
| 1529 | * (screen) gamma and thirdly the output file encoding. Since this API |
| 1530 | * receives the screen and file gamma in double precision it is possible |
| 1531 | * to calculate an exact answer given an input pixel value. Therefore we |
| 1532 | * assume that the *input* value is exact - sample/maxsample - calculate |
| 1533 | * the corresponding gamma corrected output to the limits of double |
| 1534 | * precision arithmetic and compare with what libpng returns. |
| 1535 | * |
| 1536 | * Since the library must quantise the output to 8 or 16 bits there is a |
| 1537 | * fundamental limit on the accuracy of the output of +/-.5 - this |
| 1538 | * quantisation limit is included in addition to the other limits |
| 1539 | * specified by the paramaters to the API. (Effectively, add .5 |
| 1540 | * everywhere.) |
| 1541 | * |
| 1542 | * The behavior of the 'sbit' paramter is defined by section 12.5 (sample |
| 1543 | * depth scaling) of the PNG spec. That section forces the decoder to |
| 1544 | * assume that the PNG values have been scaled if sBIT is presence: |
| 1545 | * |
| 1546 | * png-sample = floor( input-sample * (max-out/max-in) + .5 ); |
| 1547 | * |
| 1548 | * This means that only a subset of the possible PNG values should appear |
| 1549 | * in the input, however the spec allows the encoder to use a variety of |
| 1550 | * approximations to the above and doesn't require any restriction of the |
| 1551 | * values produced. |
| 1552 | * |
| 1553 | * Nevertheless the spec requires that the upper 'sBIT' bits of the value |
| 1554 | * stored in a PNG file be the original sample bits. Consequently the |
| 1555 | * code below simply scales the top sbit bits by (1<<sbit)-1 to obtain an |
| 1556 | * original sample value. |
| 1557 | * |
| 1558 | * Because there is limited precision in the input it is arguable that an |
| 1559 | * acceptable result is any valid result from input-.5 to input+.5. The |
| 1560 | * basic tests below do not do this, however if 'use_input_precision' is |
| 1561 | * set a subsequent test is performed below. |
| 1562 | */ |
| 1563 | const int processing = (fabs(screen_gamma*file_gamma-1) >= |
| 1564 | PNG_GAMMA_THRESHOLD && !threshold_test && !speed && colour_type != 3) |
| 1565 | || bit_depth != out_bd; |
| 1566 | const int samples_per_pixel = (out_ct & 2) ? 3 : 1; |
| 1567 | const double gamma = 1/(file_gamma*screen_gamma); /* Overall correction */ |
| 1568 | |
| 1569 | for (y=0; y<h; ++y) /* just one pass - no interlacing */ |
| 1570 | { |
| 1571 | unsigned s, x; |
| 1572 | png_byte std[STD_ROWMAX]; |
| 1573 | png_byte display[STD_ROWMAX]; |
| 1574 | |
| 1575 | standard_row(pp, std, colour_type, bit_depth, y); |
| 1576 | png_read_row(pp, NULL, display); |
| 1577 | |
| 1578 | if (processing) for (x=0; x<w; ++x) for (s=0; s<samples_per_pixel; ++s) |
| 1579 | { |
| 1580 | /* Input sample values: */ |
| 1581 | const unsigned id = sample(std, colour_type, bit_depth, x, s); |
| 1582 | const unsigned od = sample(display, out_ct, out_bd, x, s); |
| 1583 | const unsigned isbit = id >> (bit_depth-sbit); |
| 1584 | double i, sample, encoded_sample, output, encoded_error, error; |
| 1585 | double es_lo, es_hi; |
| 1586 | |
| 1587 | /* First check on the 'perfect' result obtained from the digitized |
| 1588 | * input value, id, and compare this against the actual digitized |
| 1589 | * result, 'od'. 'i' is the input result in the range 0..1: |
| 1590 | * |
| 1591 | * NOTE: sbit should be taken into account here but isn't, as |
| 1592 | * described above. |
| 1593 | */ |
| 1594 | i = isbit; i /= (1U<<sbit)-1; |
| 1595 | |
| 1596 | /* Then get the gamma corrected version of 'i' and compare to 'od', |
| 1597 | * any error less than .5 is insignificant - just quantization of |
| 1598 | * the output value to the nearest digital value (neverthelss the |
| 1599 | * error is still recorded - it's interesting ;-) |
| 1600 | */ |
| 1601 | encoded_sample = pow(i, gamma) * outmax; |
| 1602 | encoded_error = fabs(od-encoded_sample); |
| 1603 | |
| 1604 | if (encoded_error > maxerrout) |
| 1605 | maxerrout = encoded_error; |
| 1606 | |
| 1607 | if (encoded_error < .5+maxout) |
| 1608 | continue; |
| 1609 | |
| 1610 | /* There may be an error, calculate the actual sample values - |
| 1611 | * unencoded light intensity values. Note that in practice these |
| 1612 | * are not unencoded because they include a 'viewing correction' to |
| 1613 | * decrease or (normally) increase the perceptual contrast of the |
| 1614 | * image. There's nothing we can do about this - we don't know what |
| 1615 | * it is - so assume the unencoded value is perceptually linear. |
| 1616 | */ |
| 1617 | sample = pow(i, 1/file_gamma); /* In range 0..1 */ |
| 1618 | output = od; |
| 1619 | output /= outmax; |
| 1620 | output = pow(output, screen_gamma); |
| 1621 | |
| 1622 | /* Now we have the numbers for real errors, both absolute values as |
| 1623 | * as a percentage of the correct value (output): |
| 1624 | */ |
| 1625 | error = fabs(sample-output); |
| 1626 | if (error > maxerrabs) |
| 1627 | maxerrabs = error; |
| 1628 | /* The following is an attempt to ignore the tendency of |
| 1629 | * quantization to dominate the percentage errors for low output |
| 1630 | * sample values: |
| 1631 | */ |
| 1632 | if (sample*maxpc > .5+maxabs) |
| 1633 | { |
| 1634 | double pcerr = error/sample; |
| 1635 | if (pcerr > maxerrpc) maxerrpc = pcerr; |
| 1636 | } |
| 1637 | |
| 1638 | /* Now calculate the digitization limits for 'encoded_sample' using |
| 1639 | * the 'max' values. Note that maxout is in the encoded space but |
| 1640 | * maxpc and maxabs are in linear light space. |
| 1641 | * |
| 1642 | * First find the maximum error in linear light space, range 0..1: |
| 1643 | */ |
| 1644 | { |
| 1645 | double tmp = sample * maxpc; |
| 1646 | if (tmp < maxabs) tmp = maxabs; |
| 1647 | |
| 1648 | /* Low bound - the minimum of the three: */ |
| 1649 | es_lo = encoded_sample - maxout; |
| 1650 | if (es_lo > 0 && sample-tmp > 0) |
| 1651 | { |
| 1652 | double l = outmax * pow(sample-tmp, 1/screen_gamma); |
| 1653 | if (l < es_lo) es_lo = l; |
| 1654 | } |
| 1655 | else |
| 1656 | es_lo = 0; |
| 1657 | |
| 1658 | es_hi = encoded_sample + maxout; |
| 1659 | if (es_hi < outmax && sample+tmp < 1) |
| 1660 | { |
| 1661 | double h = outmax * pow(sample+tmp, 1/screen_gamma); |
| 1662 | if (h > es_hi) es_hi = h; |
| 1663 | } |
| 1664 | else |
| 1665 | es_hi = outmax; |
| 1666 | } |
| 1667 | |
| 1668 | /* The primary test is that the final encoded value returned by the |
| 1669 | * library should be between the two limits (inclusive) that were |
| 1670 | * calculated above. At this point quantization of the output must |
| 1671 | * be taken into account. |
| 1672 | */ |
| 1673 | if (od+.5 < es_lo || od-.5 > es_hi) |
| 1674 | { |
| 1675 | /* Thee has been an error in processing. */ |
| 1676 | double is_lo, is_hi; |
| 1677 | |
| 1678 | if (use_input_precision) |
| 1679 | { |
| 1680 | /* Ok, something is wrong - this actually happens in current |
| 1681 | * libpng sbit processing. Assume that the input value (id, |
| 1682 | * adjusted for sbit) can be anywhere between value-.5 and |
| 1683 | * value+.5 - quite a large range if sbit is low. |
| 1684 | */ |
| 1685 | double tmp = (isbit - .5)/((1U<<sbit)-1); |
| 1686 | if (tmp > 0) |
| 1687 | { |
| 1688 | is_lo = outmax * pow(tmp, gamma) - maxout; |
| 1689 | if (is_lo < 0) is_lo = 0; |
| 1690 | } |
| 1691 | else |
| 1692 | is_lo = 0; |
| 1693 | |
| 1694 | tmp = (isbit + .5)/((1U<<sbit)-1); |
| 1695 | if (tmp < 1) |
| 1696 | { |
| 1697 | is_hi = outmax * pow(tmp, gamma) + maxout; |
| 1698 | if (is_hi > outmax) is_hi = outmax; |
| 1699 | } |
| 1700 | else |
| 1701 | is_hi = outmax; |
| 1702 | |
| 1703 | if (!(od+.5 < is_lo || od-.5 > is_hi)) |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | { |
| 1708 | char msg[256]; |
| 1709 | sprintf(msg, |
| 1710 | "error: %.3f; %u{%u;%u} -> %u not %.2f (%.1f-%.1f)", |
| 1711 | od-encoded_sample, id, sbit, isbit, od, encoded_sample, |
| 1712 | use_input_precision ? is_lo : es_lo, |
| 1713 | use_input_precision ? is_hi : es_hi); |
| 1714 | png_warning(pp, msg); |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | else if (!speed && memcmp(std, display, cb) != 0) |
| 1719 | { |
| 1720 | char msg[64]; |
| 1721 | /* No transform is expected on the threshold tests. */ |
| 1722 | sprintf(msg, "gamma: below threshold row %d (of %d) changed", y, h); |
| 1723 | png_error(pp, msg); |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | png_read_end(pp, pi); |
| 1729 | modifier_reset(pm); |
| 1730 | |
| 1731 | if (pm->log && !threshold_test && !speed) |
| 1732 | fprintf(stderr, "%d bit %s %s: max error %f (%.2g, %2g%%)\n", bit_depth, |
| 1733 | colour_types[colour_type], name, maxerrout, maxerrabs, 100*maxerrpc); |
| 1734 | |
| 1735 | /* Log the summary values too. */ |
| 1736 | if (colour_type == 0 || colour_type == 4) switch (bit_depth) |
| 1737 | { |
| 1738 | case 2: |
| 1739 | if (maxerrout > pm->error_gray_2) pm->error_gray_2 = maxerrout; break; |
| 1740 | case 4: |
| 1741 | if (maxerrout > pm->error_gray_4) pm->error_gray_4 = maxerrout; break; |
| 1742 | case 8: |
| 1743 | if (maxerrout > pm->error_gray_8) pm->error_gray_8 = maxerrout; break; |
| 1744 | case 16: |
| 1745 | if (maxerrout > pm->error_gray_16) pm->error_gray_16 = maxerrout; break; |
| 1746 | } |
| 1747 | else if (colour_type == 2 || colour_type == 6) switch (bit_depth) |
| 1748 | { |
| 1749 | case 8: |
| 1750 | if (maxerrout > pm->error_color_8) pm->error_color_8 = maxerrout; break; |
| 1751 | case 16: |
| 1752 | if (maxerrout > pm->error_color_16) pm->error_color_16 = maxerrout; break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | static void gamma_threshold_test(png_modifier *pm, png_byte colour_type, |
| 1757 | png_byte bit_depth, double file_gamma, double screen_gamma) |
| 1758 | { |
| 1759 | size_t pos = 0; |
| 1760 | char name[64]; |
| 1761 | pos = safecat(name, sizeof name, pos, "threshold "); |
| 1762 | pos = safecatd(name, sizeof name, pos, file_gamma, 3); |
| 1763 | pos = safecat(name, sizeof name, pos, "/"); |
| 1764 | pos = safecatd(name, sizeof name, pos, screen_gamma, 3); |
| 1765 | |
| 1766 | (void)gamma_test(pm, colour_type, bit_depth, file_gamma, screen_gamma, |
| 1767 | bit_depth, 1, name, 0/*speed*/, 0/*no input precision*/, 0/*no strip16*/); |
| 1768 | } |
| 1769 | |
| 1770 | static void |
| 1771 | perform_gamma_threshold_tests(png_modifier *pm) |
| 1772 | { |
| 1773 | png_byte colour_type = 0; |
| 1774 | png_byte bit_depth = 0; |
| 1775 | |
| 1776 | while (next_format(&colour_type, &bit_depth)) |
| 1777 | { |
| 1778 | double gamma = 1.0; |
| 1779 | while (gamma >= .4) |
| 1780 | { |
| 1781 | gamma_threshold_test(pm, colour_type, bit_depth, gamma, 1/gamma); |
| 1782 | gamma *= .95; |
| 1783 | } |
| 1784 | |
| 1785 | /* And a special test for sRGB */ |
| 1786 | gamma_threshold_test(pm, colour_type, bit_depth, .45455, 2.2); |
| 1787 | if (fail(pm)) return; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | static void gamma_transform_test(png_modifier *pm, const png_byte colour_type, |
| 1792 | const png_byte bit_depth, const double file_gamma, const double screen_gamma, |
| 1793 | const png_byte sbit, const int speed, const int use_input_precision, |
| 1794 | const int strip16) |
| 1795 | { |
| 1796 | size_t pos = 0; |
| 1797 | char name[64]; |
| 1798 | if (sbit != bit_depth) |
| 1799 | { |
| 1800 | pos = safecat(name, sizeof name, pos, "sbit("); |
| 1801 | pos = safecatn(name, sizeof name, pos, sbit); |
| 1802 | pos = safecat(name, sizeof name, pos, ") "); |
| 1803 | } |
| 1804 | else |
| 1805 | pos = safecat(name, sizeof name, pos, "gamma "); |
| 1806 | if (strip16) |
| 1807 | pos = safecat(name, sizeof name, pos, "16to8 "); |
| 1808 | pos = safecatd(name, sizeof name, pos, file_gamma, 3); |
| 1809 | pos = safecat(name, sizeof name, pos, "->"); |
| 1810 | pos = safecatd(name, sizeof name, pos, screen_gamma, 3); |
| 1811 | |
| 1812 | gamma_test(pm, colour_type, bit_depth, file_gamma, screen_gamma, sbit, 0, |
| 1813 | name, speed, use_input_precision, strip16); |
| 1814 | } |
| 1815 | |
| 1816 | static void perform_gamma_transform_tests(png_modifier *pm, int speed) |
| 1817 | { |
| 1818 | png_byte colour_type = 0; |
| 1819 | png_byte bit_depth = 0; |
| 1820 | |
| 1821 | /* Ignore palette images - the gamma correction happens on the palette entry, |
| 1822 | * haven't got the tests for this yet. |
| 1823 | */ |
| 1824 | while (next_format(&colour_type, &bit_depth)) if (colour_type != 3) |
| 1825 | { |
| 1826 | int i, j; |
| 1827 | |
| 1828 | for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) if (i != j) |
| 1829 | { |
| 1830 | gamma_transform_test(pm, colour_type, bit_depth, 1/pm->gammas[i], |
| 1831 | pm->gammas[j], bit_depth, speed, pm->use_input_precision, |
| 1832 | 0/*do not strip16*/); |
| 1833 | if (fail(pm)) return; |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | static void perform_gamma_sbit_tests(png_modifier *pm, int speed) |
| 1839 | { |
| 1840 | png_byte sbit; |
| 1841 | |
| 1842 | /* The only interesting cases are colour and grayscale, alpha is ignored here |
| 1843 | * for overall speed. Only bit depths 8 and 16 are tested. |
| 1844 | */ |
| 1845 | for (sbit=pm->sbitlow; sbit<16; ++sbit) |
| 1846 | { |
| 1847 | int i, j; |
| 1848 | for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) |
| 1849 | if (i != j) |
| 1850 | { |
| 1851 | if (sbit < 8) |
| 1852 | { |
| 1853 | gamma_transform_test(pm, 0, 8, 1/pm->gammas[i], pm->gammas[j], sbit, |
| 1854 | speed, pm->use_input_precision_sbit, 0/*strip16*/); |
| 1855 | if (fail(pm)) return; |
| 1856 | gamma_transform_test(pm, 2, 8, 1/pm->gammas[i], pm->gammas[j], sbit, |
| 1857 | speed, pm->use_input_precision_sbit, 0/*strip16*/); |
| 1858 | if (fail(pm)) return; |
| 1859 | } |
| 1860 | gamma_transform_test(pm, 0, 16, 1/pm->gammas[i], pm->gammas[j], sbit, |
| 1861 | speed, pm->use_input_precision_sbit, 0/*strip16*/); |
| 1862 | if (fail(pm)) return; |
| 1863 | gamma_transform_test(pm, 2, 16, 1/pm->gammas[i], pm->gammas[j], sbit, |
| 1864 | speed, pm->use_input_precision_sbit, 0/*strip16*/); |
| 1865 | if (fail(pm)) return; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | static void perform_gamma_strip16_tests(png_modifier *pm, int speed) |
| 1871 | { |
| 1872 | # ifndef PNG_MAX_GAMMA_8 |
| 1873 | # define PNG_MAX_GAMMA_8 11 |
| 1874 | # endif |
| 1875 | /* Include the alpha cases here, not that sbit matches the internal value |
| 1876 | * used by the library - otherwise we will get spurious errors from the |
| 1877 | * internal sbit style approximation. |
| 1878 | * |
| 1879 | * The threshold test is here because otherwise the 16 to 8 convertion will |
| 1880 | * proceed *without* gamma correction, and the tests above will fail (but not |
| 1881 | * by much) - this could be fixed, it only appears with the -g option. |
| 1882 | */ |
| 1883 | int i, j; |
| 1884 | for (i=0; i<pm->ngammas; ++i) for (j=0; j<pm->ngammas; ++j) |
| 1885 | if (i != j && fabs(pm->gammas[j]/pm->gammas[i]-1) >= PNG_GAMMA_THRESHOLD) |
| 1886 | { |
| 1887 | gamma_transform_test(pm, 0, 16, 1/pm->gammas[i], pm->gammas[j], |
| 1888 | PNG_MAX_GAMMA_8, speed, pm->use_input_precision_16to8, 1/*strip16*/); |
| 1889 | if (fail(pm)) return; |
| 1890 | gamma_transform_test(pm, 2, 16, 1/pm->gammas[i], pm->gammas[j], |
| 1891 | PNG_MAX_GAMMA_8, speed, pm->use_input_precision_16to8, 1/*strip16*/); |
| 1892 | if (fail(pm)) return; |
| 1893 | gamma_transform_test(pm, 4, 16, 1/pm->gammas[i], pm->gammas[j], |
| 1894 | PNG_MAX_GAMMA_8, speed, pm->use_input_precision_16to8, 1/*strip16*/); |
| 1895 | if (fail(pm)) return; |
| 1896 | gamma_transform_test(pm, 6, 16, 1/pm->gammas[i], pm->gammas[j], |
| 1897 | PNG_MAX_GAMMA_8, speed, pm->use_input_precision_16to8, 1/*strip16*/); |
| 1898 | if (fail(pm)) return; |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | static void |
| 1903 | perform_gamma_test(png_modifier *pm, int speed, int summary) |
| 1904 | { |
| 1905 | /* First some arbitrary no-transform tests: */ |
| 1906 | if (!speed) |
| 1907 | { |
| 1908 | perform_gamma_threshold_tests(pm); |
| 1909 | if (fail(pm)) return; |
| 1910 | } |
| 1911 | |
| 1912 | /* Now some real transforms. */ |
| 1913 | perform_gamma_transform_tests(pm, speed); |
| 1914 | if (summary) |
| 1915 | { |
| 1916 | printf("Gamma correction error summary (output value error):\n"); |
| 1917 | printf(" 2 bit gray: %.5f\n", pm->error_gray_2); |
| 1918 | printf(" 4 bit gray: %.5f\n", pm->error_gray_4); |
| 1919 | printf(" 8 bit gray: %.5f\n", pm->error_gray_8); |
| 1920 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
| 1921 | printf(" 8 bit color: %.5f\n", pm->error_color_8); |
| 1922 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
| 1923 | } |
| 1924 | |
| 1925 | /* The sbit tests produce much larger errors: */ |
| 1926 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 = |
| 1927 | pm->error_color_8 = pm->error_color_16 = 0; |
| 1928 | perform_gamma_sbit_tests(pm, speed); |
| 1929 | if (summary) |
| 1930 | { |
| 1931 | printf("Gamma correction with sBIT:\n"); |
| 1932 | if (pm->sbitlow < 8) |
| 1933 | { |
| 1934 | printf(" 2 bit gray: %.5f\n", pm->error_gray_2); |
| 1935 | printf(" 4 bit gray: %.5f\n", pm->error_gray_4); |
| 1936 | printf(" 8 bit gray: %.5f\n", pm->error_gray_8); |
| 1937 | } |
| 1938 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
| 1939 | if (pm->sbitlow < 8) |
| 1940 | printf(" 8 bit color: %.5f\n", pm->error_color_8); |
| 1941 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
| 1942 | } |
| 1943 | |
| 1944 | /* The 16 to 8 bit strip operations: */ |
| 1945 | pm->error_gray_2 = pm->error_gray_4 = pm->error_gray_8 = pm->error_gray_16 = |
| 1946 | pm->error_color_8 = pm->error_color_16 = 0; |
| 1947 | perform_gamma_strip16_tests(pm, speed); |
| 1948 | if (summary) |
| 1949 | { |
| 1950 | printf("Gamma correction with 16 to 8 bit reduction:\n"); |
| 1951 | printf(" 16 bit gray: %.5f\n", pm->error_gray_16); |
| 1952 | printf(" 16 bit color: %.5f\n", pm->error_color_16); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | /* main program */ |
| 1957 | int main(int argc, const char **argv) |
| 1958 | { |
| 1959 | int summary = 1; /* Print the error sumamry at the end */ |
| 1960 | int speed = 0; /* Speed test only (for gamma stuff) */ |
| 1961 | |
| 1962 | /* This is an array of standard gamma values (believe it or not I've seen |
| 1963 | * every one of these mentioned somewhere.) |
| 1964 | * |
| 1965 | * In the following list the most useful values are first! |
| 1966 | */ |
| 1967 | static double gammas[]={2.2, 1.0, 2.2/1.45, 1.8, 1.5, 2.4, 2.5, 2.62, 2.9}; |
| 1968 | |
| 1969 | png_modifier pm; |
| 1970 | modifier_init(&pm); |
| 1971 | |
| 1972 | /* Default to error on warning: */ |
| 1973 | pm.this.treat_warnings_as_errors = 1; |
| 1974 | |
| 1975 | /* Store the test gammas */ |
| 1976 | pm.gammas = gammas; |
| 1977 | pm.ngammas = 3; /* for speed */ |
| 1978 | pm.sbitlow = 8; /* because libpng doesn't do sbit below 8! */ |
| 1979 | pm.use_input_precision_16to8 = 1; /* Because of the way libpng does it */ |
| 1980 | |
| 1981 | /* Some default values (set the behavior for 'make check' here) */ |
| 1982 | pm.maxout8 = .1; /* Arithmetic error in *encoded* value */ |
| 1983 | pm.maxabs8 = .00005; /* 1/20000 */ |
| 1984 | pm.maxpc8 = .499; /* I.e. .499% fractional error */ |
| 1985 | pm.maxout16 = .499; /* Error in *encoded* value */ |
| 1986 | pm.maxabs16 = .00005;/* 1/20000 */ |
| 1987 | /* NOTE: this is a reasonable perceptual limit, we assume that humans can |
| 1988 | * perceive light level differences of 1% over a 100:1 range, so we need to |
| 1989 | * maintain 1 in 10000 accuracy (in linear light space), this is what the |
| 1990 | * following guarantees. It also allows significantly higher errors at |
| 1991 | * higher 16 bit values, which is important for performance. The actual |
| 1992 | * maximum 16 bit error is about +/-1.9 in the fixed point implementation but |
| 1993 | * this is only allowed for values >38149 by the following: |
| 1994 | */ |
| 1995 | pm.maxpc16 = .005; /* I.e. 1/200% - 1/20000 */ |
| 1996 | |
| 1997 | /* Now parse the command line options. */ |
| 1998 | while (--argc >= 1) |
| 1999 | if (strcmp(*++argv, "-v") == 0) |
| 2000 | pm.this.verbose = 1; |
| 2001 | else if (strcmp(*argv, "-l") == 0) |
| 2002 | pm.log = 1; |
| 2003 | else if (strcmp(*argv, "-q") == 0) |
| 2004 | pm.this.verbose = pm.log = summary = 0; |
| 2005 | else if (strcmp(*argv, "-g") == 0) |
| 2006 | pm.ngammas = (sizeof gammas)/(sizeof gammas[0]); |
| 2007 | else if (strcmp(*argv, "-w") == 0) |
| 2008 | pm.this.treat_warnings_as_errors = 0; |
| 2009 | else if (strcmp(*argv, "-speed") == 0) |
| 2010 | speed = 1, pm.ngammas = (sizeof gammas)/(sizeof gammas[0]); |
| 2011 | else if (argc >= 1 && strcmp(*argv, "-sbitlow") == 0) |
| 2012 | --argc, pm.sbitlow = atol(*++argv); |
| 2013 | else if (argc >= 1 && strncmp(*argv, "-max", 4) == 0) |
| 2014 | { |
| 2015 | --argc; |
| 2016 | if (strcmp(4+*argv, "abs8") == 0) |
| 2017 | pm.maxabs8 = atof(*++argv); |
| 2018 | else if (strcmp(4+*argv, "abs16") == 0) |
| 2019 | pm.maxabs16 = atof(*++argv); |
| 2020 | else if (strcmp(4+*argv, "out8") == 0) |
| 2021 | pm.maxout8 = atof(*++argv); |
| 2022 | else if (strcmp(4+*argv, "out16") == 0) |
| 2023 | pm.maxout16 = atof(*++argv); |
| 2024 | else if (strcmp(4+*argv, "pc8") == 0) |
| 2025 | pm.maxpc8 = atof(*++argv); |
| 2026 | else if (strcmp(4+*argv, "pc16") == 0) |
| 2027 | pm.maxpc16 = atof(*++argv); |
| 2028 | else |
| 2029 | { |
| 2030 | fprintf(stderr, "pngvalid: %s: unknown 'max' option\n", *argv); |
| 2031 | exit(1); |
| 2032 | } |
| 2033 | } |
| 2034 | else |
| 2035 | { |
| 2036 | fprintf(stderr, "pngvalid: %s: unknown argument\n", *argv); |
| 2037 | exit(1); |
| 2038 | } |
| 2039 | |
| 2040 | /* Make useful base images */ |
| 2041 | make_standard_images(&pm.this); |
| 2042 | make_gamma_images(&pm.this); |
| 2043 | |
| 2044 | /* Perform the standard and gamma tests. */ |
| 2045 | if (!speed) |
| 2046 | perform_standard_test(&pm); |
| 2047 | perform_gamma_test(&pm, speed, summary && !speed); |
| 2048 | if (summary && !speed) |
| 2049 | printf("Results using %s point arithmetic %s\n", |
| 2050 | #if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || PNG_LIBPNG_VER < 10500 |
| 2051 | "floating", |
| 2052 | #else |
| 2053 | "fixed", |
| 2054 | #endif |
| 2055 | (pm.this.nerrors || pm.this.treat_warnings_as_errors && |
| 2056 | pm.this.nwarnings) ? "(errors)" : (pm.this.nwarnings ? |
| 2057 | "(warnings)" : "(no errors or warnings)") |
| 2058 | ); |
| 2059 | |
| 2060 | /* Error exit if there are any errors, and maybe if there are any |
| 2061 | * warnings. |
| 2062 | */ |
| 2063 | if (pm.this.nerrors || pm.this.treat_warnings_as_errors && pm.this.nwarnings) |
| 2064 | { |
| 2065 | if (!pm.this.verbose) |
| 2066 | fprintf(stderr, "pngvalid: %s\n", pm.this.error); |
| 2067 | fprintf(stderr, "pngvalid: %d errors, %d warnings\n", pm.this.nerrors, |
| 2068 | pm.this.nwarnings); |
| 2069 | exit(1); |
| 2070 | } |
| 2071 | |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | /* vim: set sw=3 ts=8 tw=80: */ |