Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: third_party/libpng/pngread.c

Issue 1591483003: XFA: Upgrade libpng to 1.6.20. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libpng/pngpriv.h ('k') | third_party/libpng/pngrio.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
1 /* pngread.c - read a PNG file 2 /* pngread.c - read a PNG file
2 * 3 *
3 * Last changed in libpng 1.6.1 [March 28, 2013] 4 * Last changed in libpng 1.6.17 [March 26, 2015]
4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7 * 8 *
8 * This code is released under the libpng license. 9 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h 11 * and license in png.h
11 * 12 *
12 * This file contains routines that an application calls directly to 13 * This file contains routines that an application calls directly to
13 * read a PNG file or stream. 14 * read a PNG file or stream.
14 */ 15 */
(...skipping 21 matching lines...) Expand all
36 /* Alternate create PNG structure for reading, and allocate any memory 37 /* Alternate create PNG structure for reading, and allocate any memory
37 * needed. 38 * needed.
38 */ 39 */
39 PNG_FUNCTION(png_structp,PNGAPI 40 PNG_FUNCTION(png_structp,PNGAPI
40 png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, 41 png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
41 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, 42 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
42 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) 43 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
43 { 44 {
44 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, 45 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
45 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); 46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
46 #endif /* PNG_USER_MEM_SUPPORTED */ 47 #endif /* USER_MEM */
47 48
48 if (png_ptr != NULL) 49 if (png_ptr != NULL)
49 { 50 {
50 png_ptr->mode = PNG_IS_READ_STRUCT; 51 png_ptr->mode = PNG_IS_READ_STRUCT;
51 52
52 /* Added in libpng-1.6.0; this can be used to detect a read structure if 53 /* Added in libpng-1.6.0; this can be used to detect a read structure if
53 * required (it will be zero in a write structure.) 54 * required (it will be zero in a write structure.)
54 */ 55 */
55 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED 56 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
56 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; 57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
57 # endif 58 # endif
58 59
59 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED 60 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
60 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; 61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
61 62
62 /* In stable builds only warn if an application error can be completely 63 /* In stable builds only warn if an application error can be completely
63 * handled. 64 * handled.
64 */ 65 */
65 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC 66 # if PNG_RELEASE_BUILD
66 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; 67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
67 # endif 68 # endif
68 # endif 69 # endif
69 70
70 /* TODO: delay this, it can be done in png_init_io (if the app doesn't 71 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
71 * do it itself) avoiding setting the default function if it is not 72 * do it itself) avoiding setting the default function if it is not
72 * required. 73 * required.
73 */ 74 */
74 png_set_read_fn(png_ptr, NULL, NULL); 75 png_set_read_fn(png_ptr, NULL, NULL);
75 } 76 }
(...skipping 29 matching lines...) Expand all
105 for (;;) 106 for (;;)
106 { 107 {
107 png_uint_32 length = png_read_chunk_header(png_ptr); 108 png_uint_32 length = png_read_chunk_header(png_ptr);
108 png_uint_32 chunk_name = png_ptr->chunk_name; 109 png_uint_32 chunk_name = png_ptr->chunk_name;
109 110
110 /* IDAT logic needs to happen here to simplify getting the two flags 111 /* IDAT logic needs to happen here to simplify getting the two flags
111 * right. 112 * right.
112 */ 113 */
113 if (chunk_name == png_IDAT) 114 if (chunk_name == png_IDAT)
114 { 115 {
115 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 116 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
116 png_chunk_error(png_ptr, "Missing IHDR before IDAT"); 117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
117 118
118 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && 119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
119 !(png_ptr->mode & PNG_HAVE_PLTE)) 120 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
120 png_chunk_error(png_ptr, "Missing PLTE before IDAT"); 121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
121 122
122 else if (png_ptr->mode & PNG_AFTER_IDAT) 123 else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
123 png_chunk_benign_error(png_ptr, "Too many IDATs found"); 124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
124 125
125 png_ptr->mode |= PNG_HAVE_IDAT; 126 png_ptr->mode |= PNG_HAVE_IDAT;
126 } 127 }
127 128
128 else if (png_ptr->mode & PNG_HAVE_IDAT) 129 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
129 png_ptr->mode |= PNG_AFTER_IDAT; 130 png_ptr->mode |= PNG_AFTER_IDAT;
130 131
131 /* This should be a binary subdivision search or a hash for 132 /* This should be a binary subdivision search or a hash for
132 * matching the chunk name rather than a linear search. 133 * matching the chunk name rather than a linear search.
133 */ 134 */
134 if (chunk_name == png_IHDR) 135 if (chunk_name == png_IHDR)
135 png_handle_IHDR(png_ptr, info_ptr, length); 136 png_handle_IHDR(png_ptr, info_ptr, length);
136 137
137 else if (chunk_name == png_IEND) 138 else if (chunk_name == png_IEND)
138 png_handle_IEND(png_ptr, info_ptr, length); 139 png_handle_IEND(png_ptr, info_ptr, length);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 #ifdef PNG_READ_iTXt_SUPPORTED 245 #ifdef PNG_READ_iTXt_SUPPORTED
245 else if (chunk_name == png_iTXt) 246 else if (chunk_name == png_iTXt)
246 png_handle_iTXt(png_ptr, info_ptr, length); 247 png_handle_iTXt(png_ptr, info_ptr, length);
247 #endif 248 #endif
248 249
249 else 250 else
250 png_handle_unknown(png_ptr, info_ptr, length, 251 png_handle_unknown(png_ptr, info_ptr, length,
251 PNG_HANDLE_CHUNK_AS_DEFAULT); 252 PNG_HANDLE_CHUNK_AS_DEFAULT);
252 } 253 }
253 } 254 }
254 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 255 #endif /* SEQUENTIAL_READ */
255 256
256 /* Optional call to update the users info_ptr structure */ 257 /* Optional call to update the users info_ptr structure */
257 void PNGAPI 258 void PNGAPI
258 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) 259 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
259 { 260 {
260 png_debug(1, "in png_read_update_info"); 261 png_debug(1, "in png_read_update_info");
261 262
262 if (png_ptr != NULL) 263 if (png_ptr != NULL)
263 { 264 {
264 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) 265 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
(...skipping 29 matching lines...) Expand all
294 { 295 {
295 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) 296 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
296 png_read_start_row(png_ptr); 297 png_read_start_row(png_ptr);
297 298
298 /* New in 1.6.0 this avoids the bug of doing the initializations twice */ 299 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
299 else 300 else
300 png_app_error(png_ptr, 301 png_app_error(png_ptr,
301 "png_start_read_image/png_read_update_info: duplicate call"); 302 "png_start_read_image/png_read_update_info: duplicate call");
302 } 303 }
303 } 304 }
304 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 305 #endif /* SEQUENTIAL_READ */
305 306
306 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 307 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
308 #ifdef PNG_MNG_FEATURES_SUPPORTED
309 /* Undoes intrapixel differencing,
310 * NOTE: this is apparently only supported in the 'sequential' reader.
311 */
312 static void
313 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
314 {
315 png_debug(1, "in png_do_read_intrapixel");
316
317 if (
318 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
319 {
320 int bytes_per_pixel;
321 png_uint_32 row_width = row_info->width;
322
323 if (row_info->bit_depth == 8)
324 {
325 png_bytep rp;
326 png_uint_32 i;
327
328 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
329 bytes_per_pixel = 3;
330
331 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
332 bytes_per_pixel = 4;
333
334 else
335 return;
336
337 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
338 {
339 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
340 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
341 }
342 }
343 else if (row_info->bit_depth == 16)
344 {
345 png_bytep rp;
346 png_uint_32 i;
347
348 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
349 bytes_per_pixel = 6;
350
351 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
352 bytes_per_pixel = 8;
353
354 else
355 return;
356
357 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
358 {
359 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
360 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
361 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
362 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
363 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
364 *(rp ) = (png_byte)((red >> 8) & 0xff);
365 *(rp + 1) = (png_byte)(red & 0xff);
366 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
367 *(rp + 5) = (png_byte)(blue & 0xff);
368 }
369 }
370 }
371 }
372 #endif /* MNG_FEATURES */
373
307 void PNGAPI 374 void PNGAPI
308 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) 375 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
309 { 376 {
310 png_row_info row_info; 377 png_row_info row_info;
311 378
312 if (png_ptr == NULL) 379 if (png_ptr == NULL)
313 return; 380 return;
314 381
315 png_debug2(1, "in png_read_row (row %lu, pass %d)", 382 png_debug2(1, "in png_read_row (row %lu, pass %d)",
316 (unsigned long)png_ptr->row_number, png_ptr->pass); 383 (unsigned long)png_ptr->row_number, png_ptr->pass);
317 384
318 /* png_read_start_row sets the information (in particular iwidth) for this 385 /* png_read_start_row sets the information (in particular iwidth) for this
319 * interlace pass. 386 * interlace pass.
320 */ 387 */
321 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) 388 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
322 png_read_start_row(png_ptr); 389 png_read_start_row(png_ptr);
323 390
324 /* 1.5.6: row_info moved out of png_struct to a local here. */ 391 /* 1.5.6: row_info moved out of png_struct to a local here. */
325 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ 392 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
326 row_info.color_type = png_ptr->color_type; 393 row_info.color_type = png_ptr->color_type;
327 row_info.bit_depth = png_ptr->bit_depth; 394 row_info.bit_depth = png_ptr->bit_depth;
328 row_info.channels = png_ptr->channels; 395 row_info.channels = png_ptr->channels;
329 row_info.pixel_depth = png_ptr->pixel_depth; 396 row_info.pixel_depth = png_ptr->pixel_depth;
330 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); 397 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
331 398
399 #ifdef PNG_WARNINGS_SUPPORTED
332 if (png_ptr->row_number == 0 && png_ptr->pass == 0) 400 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
333 { 401 {
334 /* Check for transforms that have been set but were defined out */ 402 /* Check for transforms that have been set but were defined out */
335 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) 403 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
336 if (png_ptr->transformations & PNG_INVERT_MONO) 404 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
337 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); 405 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
338 #endif 406 #endif
339 407
340 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) 408 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
341 if (png_ptr->transformations & PNG_FILLER) 409 if ((png_ptr->transformations & PNG_FILLER) != 0)
342 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); 410 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
343 #endif 411 #endif
344 412
345 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ 413 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
346 !defined(PNG_READ_PACKSWAP_SUPPORTED) 414 !defined(PNG_READ_PACKSWAP_SUPPORTED)
347 if (png_ptr->transformations & PNG_PACKSWAP) 415 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
348 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); 416 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
349 #endif 417 #endif
350 418
351 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) 419 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
352 if (png_ptr->transformations & PNG_PACK) 420 if ((png_ptr->transformations & PNG_PACK) != 0)
353 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); 421 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
354 #endif 422 #endif
355 423
356 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) 424 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
357 if (png_ptr->transformations & PNG_SHIFT) 425 if ((png_ptr->transformations & PNG_SHIFT) != 0)
358 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); 426 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
359 #endif 427 #endif
360 428
361 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) 429 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
362 if (png_ptr->transformations & PNG_BGR) 430 if ((png_ptr->transformations & PNG_BGR) != 0)
363 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); 431 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
364 #endif 432 #endif
365 433
366 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) 434 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
367 if (png_ptr->transformations & PNG_SWAP_BYTES) 435 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
368 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); 436 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
369 #endif 437 #endif
370 } 438 }
439 #endif /* WARNINGS */
371 440
372 #ifdef PNG_READ_INTERLACING_SUPPORTED 441 #ifdef PNG_READ_INTERLACING_SUPPORTED
373 /* If interlaced and we do not need a new row, combine row and return. 442 /* If interlaced and we do not need a new row, combine row and return.
374 * Notice that the pixels we have from previous rows have been transformed 443 * Notice that the pixels we have from previous rows have been transformed
375 * already; we can only combine like with like (transformed or 444 * already; we can only combine like with like (transformed or
376 * untransformed) and, because of the libpng API for interlaced images, this 445 * untransformed) and, because of the libpng API for interlaced images, this
377 * means we must transform before de-interlacing. 446 * means we must transform before de-interlacing.
378 */ 447 */
379 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) 448 if (png_ptr->interlaced != 0 &&
449 (png_ptr->transformations & PNG_INTERLACE) != 0)
380 { 450 {
381 switch (png_ptr->pass) 451 switch (png_ptr->pass)
382 { 452 {
383 case 0: 453 case 0:
384 if (png_ptr->row_number & 0x07) 454 if (png_ptr->row_number & 0x07)
385 { 455 {
386 if (dsp_row != NULL) 456 if (dsp_row != NULL)
387 png_combine_row(png_ptr, dsp_row, 1/*display*/); 457 png_combine_row(png_ptr, dsp_row, 1/*display*/);
388 png_read_finish_row(png_ptr); 458 png_read_finish_row(png_ptr);
389 return; 459 return;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 if (dsp_row != NULL) 510 if (dsp_row != NULL)
441 png_combine_row(png_ptr, dsp_row, 1/*display*/); 511 png_combine_row(png_ptr, dsp_row, 1/*display*/);
442 512
443 png_read_finish_row(png_ptr); 513 png_read_finish_row(png_ptr);
444 return; 514 return;
445 } 515 }
446 break; 516 break;
447 517
448 default: 518 default:
449 case 6: 519 case 6:
450 if (!(png_ptr->row_number & 1)) 520 if ((png_ptr->row_number & 1) == 0)
451 { 521 {
452 png_read_finish_row(png_ptr); 522 png_read_finish_row(png_ptr);
453 return; 523 return;
454 } 524 }
455 break; 525 break;
456 } 526 }
457 } 527 }
458 #endif 528 #endif
459 529
460 if (!(png_ptr->mode & PNG_HAVE_IDAT)) 530 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
461 png_error(png_ptr, "Invalid attempt to read row data"); 531 png_error(png_ptr, "Invalid attempt to read row data");
462 532
463 /* Fill the row with IDAT data: */ 533 /* Fill the row with IDAT data: */
464 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); 534 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
465 535
466 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) 536 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
467 { 537 {
468 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) 538 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
469 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, 539 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
470 png_ptr->prev_row + 1, png_ptr->row_buf[0]); 540 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
471 else 541 else
472 png_error(png_ptr, "bad adaptive filter value"); 542 png_error(png_ptr, "bad adaptive filter value");
473 } 543 }
474 544
475 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before 545 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
476 * 1.5.6, while the buffer really is this big in current versions of libpng 546 * 1.5.6, while the buffer really is this big in current versions of libpng
477 * it may not be in the future, so this was changed just to copy the 547 * it may not be in the future, so this was changed just to copy the
478 * interlaced count: 548 * interlaced count:
479 */ 549 */
480 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); 550 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
481 551
482 #ifdef PNG_MNG_FEATURES_SUPPORTED 552 #ifdef PNG_MNG_FEATURES_SUPPORTED
483 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && 553 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
484 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) 554 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
485 { 555 {
486 /* Intrapixel differencing */ 556 /* Intrapixel differencing */
487 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); 557 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
488 } 558 }
489 #endif 559 #endif
490 560
491
492 #ifdef PNG_READ_TRANSFORMS_SUPPORTED 561 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
493 if (png_ptr->transformations) 562 if (png_ptr->transformations)
494 png_do_read_transformations(png_ptr, &row_info); 563 png_do_read_transformations(png_ptr, &row_info);
495 #endif 564 #endif
496 565
497 /* The transformed pixel depth should match the depth now in row_info. */ 566 /* The transformed pixel depth should match the depth now in row_info. */
498 if (png_ptr->transformed_pixel_depth == 0) 567 if (png_ptr->transformed_pixel_depth == 0)
499 { 568 {
500 png_ptr->transformed_pixel_depth = row_info.pixel_depth; 569 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
501 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) 570 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
502 png_error(png_ptr, "sequential row overflow"); 571 png_error(png_ptr, "sequential row overflow");
503 } 572 }
504 573
505 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) 574 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
506 png_error(png_ptr, "internal sequential row size calculation error"); 575 png_error(png_ptr, "internal sequential row size calculation error");
507 576
508 #ifdef PNG_READ_INTERLACING_SUPPORTED 577 #ifdef PNG_READ_INTERLACING_SUPPORTED
509 /* Blow up interlaced rows to full size */ 578 /* Expand interlaced rows to full size */
510 if (png_ptr->interlaced && 579 if (png_ptr->interlaced != 0 &&
511 (png_ptr->transformations & PNG_INTERLACE)) 580 (png_ptr->transformations & PNG_INTERLACE) != 0)
512 { 581 {
513 if (png_ptr->pass < 6) 582 if (png_ptr->pass < 6)
514 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, 583 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
515 png_ptr->transformations); 584 png_ptr->transformations);
516 585
517 if (dsp_row != NULL) 586 if (dsp_row != NULL)
518 png_combine_row(png_ptr, dsp_row, 1/*display*/); 587 png_combine_row(png_ptr, dsp_row, 1/*display*/);
519 588
520 if (row != NULL) 589 if (row != NULL)
521 png_combine_row(png_ptr, row, 0/*row*/); 590 png_combine_row(png_ptr, row, 0/*row*/);
522 } 591 }
523 592
524 else 593 else
525 #endif 594 #endif
526 { 595 {
527 if (row != NULL) 596 if (row != NULL)
528 png_combine_row(png_ptr, row, -1/*ignored*/); 597 png_combine_row(png_ptr, row, -1/*ignored*/);
529 598
530 if (dsp_row != NULL) 599 if (dsp_row != NULL)
531 png_combine_row(png_ptr, dsp_row, -1/*ignored*/); 600 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
532 } 601 }
533 png_read_finish_row(png_ptr); 602 png_read_finish_row(png_ptr);
534 603
535 if (png_ptr->read_row_fn != NULL) 604 if (png_ptr->read_row_fn != NULL)
536 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); 605 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
537 606
538 } 607 }
539 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 608 #endif /* SEQUENTIAL_READ */
540 609
541 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 610 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
542 /* Read one or more rows of image data. If the image is interlaced, 611 /* Read one or more rows of image data. If the image is interlaced,
543 * and png_set_interlace_handling() has been called, the rows need to 612 * and png_set_interlace_handling() has been called, the rows need to
544 * contain the contents of the rows from the previous pass. If the 613 * contain the contents of the rows from the previous pass. If the
545 * image has alpha or transparency, and png_handle_alpha()[*] has been 614 * image has alpha or transparency, and png_handle_alpha()[*] has been
546 * called, the rows contents must be initialized to the contents of the 615 * called, the rows contents must be initialized to the contents of the
547 * screen. 616 * screen.
548 * 617 *
549 * "row" holds the actual image, and pixels are placed in it 618 * "row" holds the actual image, and pixels are placed in it
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 } 665 }
597 666
598 else if (dp != NULL) 667 else if (dp != NULL)
599 for (i = 0; i < num_rows; i++) 668 for (i = 0; i < num_rows; i++)
600 { 669 {
601 png_bytep dptr = *dp; 670 png_bytep dptr = *dp;
602 png_read_row(png_ptr, NULL, dptr); 671 png_read_row(png_ptr, NULL, dptr);
603 dp++; 672 dp++;
604 } 673 }
605 } 674 }
606 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 675 #endif /* SEQUENTIAL_READ */
607 676
608 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 677 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
609 /* Read the entire image. If the image has an alpha channel or a tRNS 678 /* Read the entire image. If the image has an alpha channel or a tRNS
610 * chunk, and you have called png_handle_alpha()[*], you will need to 679 * chunk, and you have called png_handle_alpha()[*], you will need to
611 * initialize the image to the current image that PNG will be overlaying. 680 * initialize the image to the current image that PNG will be overlaying.
612 * We set the num_rows again here, in case it was incorrectly set in 681 * We set the num_rows again here, in case it was incorrectly set in
613 * png_read_start_row() by a call to png_read_update_info() or 682 * png_read_start_row() by a call to png_read_update_info() or
614 * png_start_read_image() if png_set_interlace_handling() wasn't called 683 * png_start_read_image() if png_set_interlace_handling() wasn't called
615 * prior to either of these functions like it should have been. You can 684 * prior to either of these functions like it should have been. You can
616 * only call this function once. If you desire to have an image for 685 * only call this function once. If you desire to have an image for
617 * each pass of a interlaced image, use png_read_rows() instead. 686 * each pass of a interlaced image, use png_read_rows() instead.
618 * 687 *
619 * [*] png_handle_alpha() does not exist yet, as of this version of libpng 688 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
620 */ 689 */
621 void PNGAPI 690 void PNGAPI
622 png_read_image(png_structrp png_ptr, png_bytepp image) 691 png_read_image(png_structrp png_ptr, png_bytepp image)
623 { 692 {
624 png_uint_32 i, image_height; 693 png_uint_32 i, image_height;
625 int pass, j; 694 int pass, j;
626 png_bytepp rp; 695 png_bytepp rp;
627 696
628 png_debug(1, "in png_read_image"); 697 png_debug(1, "in png_read_image");
629 698
630 if (png_ptr == NULL) 699 if (png_ptr == NULL)
631 return; 700 return;
632 701
633 #ifdef PNG_READ_INTERLACING_SUPPORTED 702 #ifdef PNG_READ_INTERLACING_SUPPORTED
634 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) 703 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
635 { 704 {
636 pass = png_set_interlace_handling(png_ptr); 705 pass = png_set_interlace_handling(png_ptr);
637 /* And make sure transforms are initialized. */ 706 /* And make sure transforms are initialized. */
638 png_start_read_image(png_ptr); 707 png_start_read_image(png_ptr);
639 } 708 }
640 else 709 else
641 { 710 {
642 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE)) 711 if (png_ptr->interlaced != 0 &&
712 (png_ptr->transformations & PNG_INTERLACE) == 0)
643 { 713 {
644 /* Caller called png_start_read_image or png_read_update_info without 714 /* Caller called png_start_read_image or png_read_update_info without
645 * first turning on the PNG_INTERLACE transform. We can fix this here, 715 * first turning on the PNG_INTERLACE transform. We can fix this here,
646 * but the caller should do it! 716 * but the caller should do it!
647 */ 717 */
648 png_warning(png_ptr, "Interlace handling should be turned on when " 718 png_warning(png_ptr, "Interlace handling should be turned on when "
649 "using png_read_image"); 719 "using png_read_image");
650 /* Make sure this is set correctly */ 720 /* Make sure this is set correctly */
651 png_ptr->num_rows = png_ptr->height; 721 png_ptr->num_rows = png_ptr->height;
652 } 722 }
(...skipping 16 matching lines...) Expand all
669 for (j = 0; j < pass; j++) 739 for (j = 0; j < pass; j++)
670 { 740 {
671 rp = image; 741 rp = image;
672 for (i = 0; i < image_height; i++) 742 for (i = 0; i < image_height; i++)
673 { 743 {
674 png_read_row(png_ptr, *rp, NULL); 744 png_read_row(png_ptr, *rp, NULL);
675 rp++; 745 rp++;
676 } 746 }
677 } 747 }
678 } 748 }
679 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 749 #endif /* SEQUENTIAL_READ */
680 750
681 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 751 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
682 /* Read the end of the PNG file. Will not read past the end of the 752 /* Read the end of the PNG file. Will not read past the end of the
683 * file, will verify the end is accurate, and will read any comments 753 * file, will verify the end is accurate, and will read any comments
684 * or time information at the end of the file, if info is not NULL. 754 * or time information at the end of the file, if info is not NULL.
685 */ 755 */
686 void PNGAPI 756 void PNGAPI
687 png_read_end(png_structrp png_ptr, png_inforp info_ptr) 757 png_read_end(png_structrp png_ptr, png_inforp info_ptr)
688 { 758 {
689 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 759 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
690 int keep; 760 int keep;
691 #endif 761 #endif
692 762
693 png_debug(1, "in png_read_end"); 763 png_debug(1, "in png_read_end");
694 764
695 if (png_ptr == NULL) 765 if (png_ptr == NULL)
696 return; 766 return;
697 767
698 /* If png_read_end is called in the middle of reading the rows there may 768 /* If png_read_end is called in the middle of reading the rows there may
699 * still be pending IDAT data and an owned zstream. Deal with this here. 769 * still be pending IDAT data and an owned zstream. Deal with this here.
700 */ 770 */
701 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 771 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
702 if (!png_chunk_unknown_handling(png_ptr, png_IDAT)) 772 if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
703 #endif 773 #endif
704 png_read_finish_IDAT(png_ptr); 774 png_read_finish_IDAT(png_ptr);
705 775
706 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED 776 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
707 /* Report invalid palette index; added at libng-1.5.10 */ 777 /* Report invalid palette index; added at libng-1.5.10 */
708 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && 778 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
709 png_ptr->num_palette_max > png_ptr->num_palette) 779 png_ptr->num_palette_max > png_ptr->num_palette)
710 png_benign_error(png_ptr, "Read palette index exceeding num_palette"); 780 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
711 #endif 781 #endif
712 782
713 do 783 do
714 { 784 {
715 png_uint_32 length = png_read_chunk_header(png_ptr); 785 png_uint_32 length = png_read_chunk_header(png_ptr);
716 png_uint_32 chunk_name = png_ptr->chunk_name; 786 png_uint_32 chunk_name = png_ptr->chunk_name;
717 787
718 if (chunk_name == png_IHDR) 788 if (chunk_name == png_IEND)
789 png_handle_IEND(png_ptr, info_ptr, length);
790
791 else if (chunk_name == png_IHDR)
719 png_handle_IHDR(png_ptr, info_ptr, length); 792 png_handle_IHDR(png_ptr, info_ptr, length);
720 793
721 else if (chunk_name == png_IEND) 794 else if (info_ptr == NULL)
722 png_handle_IEND(png_ptr, info_ptr, length); 795 png_crc_finish(png_ptr, length);
723 796
724 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED 797 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
725 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) 798 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
726 { 799 {
727 if (chunk_name == png_IDAT) 800 if (chunk_name == png_IDAT)
728 { 801 {
729 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) 802 if ((length > 0) ||
803 (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
730 png_benign_error(png_ptr, "Too many IDATs found"); 804 png_benign_error(png_ptr, "Too many IDATs found");
731 } 805 }
732 png_handle_unknown(png_ptr, info_ptr, length, keep); 806 png_handle_unknown(png_ptr, info_ptr, length, keep);
733 if (chunk_name == png_PLTE) 807 if (chunk_name == png_PLTE)
734 png_ptr->mode |= PNG_HAVE_PLTE; 808 png_ptr->mode |= PNG_HAVE_PLTE;
735 } 809 }
736 #endif 810 #endif
737 811
738 else if (chunk_name == png_IDAT) 812 else if (chunk_name == png_IDAT)
739 { 813 {
740 /* Zero length IDATs are legal after the last IDAT has been 814 /* Zero length IDATs are legal after the last IDAT has been
741 * read, but not after other chunks have been read. 815 * read, but not after other chunks have been read.
742 */ 816 */
743 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) 817 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
744 png_benign_error(png_ptr, "Too many IDATs found"); 818 png_benign_error(png_ptr, "Too many IDATs found");
745 819
746 png_crc_finish(png_ptr, length); 820 png_crc_finish(png_ptr, length);
747 } 821 }
748 else if (chunk_name == png_PLTE) 822 else if (chunk_name == png_PLTE)
749 png_handle_PLTE(png_ptr, info_ptr, length); 823 png_handle_PLTE(png_ptr, info_ptr, length);
750 824
751 #ifdef PNG_READ_bKGD_SUPPORTED 825 #ifdef PNG_READ_bKGD_SUPPORTED
752 else if (chunk_name == png_bKGD) 826 else if (chunk_name == png_bKGD)
753 png_handle_bKGD(png_ptr, info_ptr, length); 827 png_handle_bKGD(png_ptr, info_ptr, length);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 #endif 903 #endif
830 904
831 #ifdef PNG_READ_iTXt_SUPPORTED 905 #ifdef PNG_READ_iTXt_SUPPORTED
832 else if (chunk_name == png_iTXt) 906 else if (chunk_name == png_iTXt)
833 png_handle_iTXt(png_ptr, info_ptr, length); 907 png_handle_iTXt(png_ptr, info_ptr, length);
834 #endif 908 #endif
835 909
836 else 910 else
837 png_handle_unknown(png_ptr, info_ptr, length, 911 png_handle_unknown(png_ptr, info_ptr, length,
838 PNG_HANDLE_CHUNK_AS_DEFAULT); 912 PNG_HANDLE_CHUNK_AS_DEFAULT);
839 } while (!(png_ptr->mode & PNG_HAVE_IEND)); 913 } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
840 } 914 }
841 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 915 #endif /* SEQUENTIAL_READ */
842 916
843 /* Free all memory used in the read struct */ 917 /* Free all memory used in the read struct */
844 static void 918 static void
845 png_read_destroy(png_structrp png_ptr) 919 png_read_destroy(png_structrp png_ptr)
846 { 920 {
847 png_debug(1, "in png_read_destroy"); 921 png_debug(1, "in png_read_destroy");
848 922
849 #ifdef PNG_READ_GAMMA_SUPPORTED 923 #ifdef PNG_READ_GAMMA_SUPPORTED
850 png_destroy_gamma_table(png_ptr); 924 png_destroy_gamma_table(png_ptr);
851 #endif 925 #endif
852 926
853 png_free(png_ptr, png_ptr->big_row_buf); 927 png_free(png_ptr, png_ptr->big_row_buf);
928 png_ptr->big_row_buf = NULL;
854 png_free(png_ptr, png_ptr->big_prev_row); 929 png_free(png_ptr, png_ptr->big_prev_row);
930 png_ptr->big_prev_row = NULL;
855 png_free(png_ptr, png_ptr->read_buffer); 931 png_free(png_ptr, png_ptr->read_buffer);
932 png_ptr->read_buffer = NULL;
856 933
857 #ifdef PNG_READ_QUANTIZE_SUPPORTED 934 #ifdef PNG_READ_QUANTIZE_SUPPORTED
858 png_free(png_ptr, png_ptr->palette_lookup); 935 png_free(png_ptr, png_ptr->palette_lookup);
936 png_ptr->palette_lookup = NULL;
859 png_free(png_ptr, png_ptr->quantize_index); 937 png_free(png_ptr, png_ptr->quantize_index);
860 #endif 938 png_ptr->quantize_index = NULL;
861 939 #endif
862 if (png_ptr->free_me & PNG_FREE_PLTE) 940
941 if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
942 {
863 png_zfree(png_ptr, png_ptr->palette); 943 png_zfree(png_ptr, png_ptr->palette);
944 png_ptr->palette = NULL;
945 }
864 png_ptr->free_me &= ~PNG_FREE_PLTE; 946 png_ptr->free_me &= ~PNG_FREE_PLTE;
865 947
866 #if defined(PNG_tRNS_SUPPORTED) || \ 948 #if defined(PNG_tRNS_SUPPORTED) || \
867 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) 949 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
868 if (png_ptr->free_me & PNG_FREE_TRNS) 950 if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
951 {
869 png_free(png_ptr, png_ptr->trans_alpha); 952 png_free(png_ptr, png_ptr->trans_alpha);
953 png_ptr->trans_alpha = NULL;
954 }
870 png_ptr->free_me &= ~PNG_FREE_TRNS; 955 png_ptr->free_me &= ~PNG_FREE_TRNS;
871 #endif 956 #endif
872 957
873 inflateEnd(&png_ptr->zstream); 958 inflateEnd(&png_ptr->zstream);
874 959
875 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED 960 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
876 png_free(png_ptr, png_ptr->save_buffer); 961 png_free(png_ptr, png_ptr->save_buffer);
877 #endif 962 png_ptr->save_buffer = NULL;
878 963 #endif
879 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\ 964
965 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
880 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) 966 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
881 png_free(png_ptr, png_ptr->unknown_chunk.data); 967 png_free(png_ptr, png_ptr->unknown_chunk.data);
968 png_ptr->unknown_chunk.data = NULL;
882 #endif 969 #endif
883 970
884 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED 971 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
885 png_free(png_ptr, png_ptr->chunk_list); 972 png_free(png_ptr, png_ptr->chunk_list);
973 png_ptr->chunk_list = NULL;
886 #endif 974 #endif
887 975
888 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error 976 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
889 * callbacks are still set at this point. They are required to complete the 977 * callbacks are still set at this point. They are required to complete the
890 * destruction of the png_struct itself. 978 * destruction of the png_struct itself.
891 */ 979 */
892 } 980 }
893 981
894 /* Free all memory used by the read */ 982 /* Free all memory used by the read */
895 void PNGAPI 983 void PNGAPI
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 } 1016 }
929 1017
930 1018
931 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED 1019 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
932 #ifdef PNG_INFO_IMAGE_SUPPORTED 1020 #ifdef PNG_INFO_IMAGE_SUPPORTED
933 void PNGAPI 1021 void PNGAPI
934 png_read_png(png_structrp png_ptr, png_inforp info_ptr, 1022 png_read_png(png_structrp png_ptr, png_inforp info_ptr,
935 int transforms, 1023 int transforms,
936 voidp params) 1024 voidp params)
937 { 1025 {
938 int row;
939
940 if (png_ptr == NULL || info_ptr == NULL) 1026 if (png_ptr == NULL || info_ptr == NULL)
941 return; 1027 return;
942 1028
943 /* png_read_info() gives us all of the information from the 1029 /* png_read_info() gives us all of the information from the
944 * PNG file before the first IDAT (image data chunk). 1030 * PNG file before the first IDAT (image data chunk).
945 */ 1031 */
946 png_read_info(png_ptr, info_ptr); 1032 png_read_info(png_ptr, info_ptr);
947 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) 1033 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
948 png_error(png_ptr, "Image is too high to process with png_read_png()"); 1034 png_error(png_ptr, "Image is too high to process with png_read_png()");
949 1035
950 /* -------------- image transformations start here ------------------- */ 1036 /* -------------- image transformations start here ------------------- */
951 1037 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
952 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 1038 * is not implemented. This will only happen in de-configured (non-default)
1039 * libpng builds. The results can be unexpected - png_read_png may return
1040 * short or mal-formed rows because the transform is skipped.
1041 */
1042
953 /* Tell libpng to strip 16-bit/color files down to 8 bits per color. 1043 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
954 */ 1044 */
955 if (transforms & PNG_TRANSFORM_SCALE_16) 1045 if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
956 { 1046 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
957 /* Added at libpng-1.5.4. "strip_16" produces the same result that it 1047 * did in earlier versions, while "scale_16" is now more accurate.
958 * did in earlier versions, while "scale_16" is now more accurate. 1048 */
959 */ 1049 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
960 png_set_scale_16(png_ptr); 1050 png_set_scale_16(png_ptr);
961 } 1051 #else
962 #endif 1052 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
963 1053 #endif
964 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED 1054
965 /* If both SCALE and STRIP are required pngrtran will effectively cancel the 1055 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
966 * latter by doing SCALE first. This is ok and allows apps not to check for 1056 * latter by doing SCALE first. This is ok and allows apps not to check for
967 * which is supported to get the right answer. 1057 * which is supported to get the right answer.
968 */ 1058 */
969 if (transforms & PNG_TRANSFORM_STRIP_16) 1059 if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
1060 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
970 png_set_strip_16(png_ptr); 1061 png_set_strip_16(png_ptr);
971 #endif 1062 #else
972 1063 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
973 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED 1064 #endif
1065
974 /* Strip alpha bytes from the input data without combining with 1066 /* Strip alpha bytes from the input data without combining with
975 * the background (not recommended). 1067 * the background (not recommended).
976 */ 1068 */
977 if (transforms & PNG_TRANSFORM_STRIP_ALPHA) 1069 if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
1070 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
978 png_set_strip_alpha(png_ptr); 1071 png_set_strip_alpha(png_ptr);
979 #endif 1072 #else
980 1073 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
981 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) 1074 #endif
1075
982 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single 1076 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
983 * byte into separate bytes (useful for paletted and grayscale images). 1077 * byte into separate bytes (useful for paletted and grayscale images).
984 */ 1078 */
985 if (transforms & PNG_TRANSFORM_PACKING) 1079 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
1080 #ifdef PNG_READ_PACK_SUPPORTED
986 png_set_packing(png_ptr); 1081 png_set_packing(png_ptr);
987 #endif 1082 #else
988 1083 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
989 #ifdef PNG_READ_PACKSWAP_SUPPORTED 1084 #endif
1085
990 /* Change the order of packed pixels to least significant bit first 1086 /* Change the order of packed pixels to least significant bit first
991 * (not useful if you are using png_set_packing). 1087 * (not useful if you are using png_set_packing).
992 */ 1088 */
993 if (transforms & PNG_TRANSFORM_PACKSWAP) 1089 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
1090 #ifdef PNG_READ_PACKSWAP_SUPPORTED
994 png_set_packswap(png_ptr); 1091 png_set_packswap(png_ptr);
995 #endif 1092 #else
996 1093 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
997 #ifdef PNG_READ_EXPAND_SUPPORTED 1094 #endif
1095
998 /* Expand paletted colors into true RGB triplets 1096 /* Expand paletted colors into true RGB triplets
999 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel 1097 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1000 * Expand paletted or RGB images with transparency to full alpha 1098 * Expand paletted or RGB images with transparency to full alpha
1001 * channels so the data will be available as RGBA quartets. 1099 * channels so the data will be available as RGBA quartets.
1002 */ 1100 */
1003 if (transforms & PNG_TRANSFORM_EXPAND) 1101 if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
1004 if ((png_ptr->bit_depth < 8) || 1102 #ifdef PNG_READ_EXPAND_SUPPORTED
1005 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || 1103 png_set_expand(png_ptr);
1006 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) 1104 #else
1007 png_set_expand(png_ptr); 1105 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
1008 #endif 1106 #endif
1009 1107
1010 /* We don't handle background color or gamma transformation or quantizing. 1108 /* We don't handle background color or gamma transformation or quantizing.
1011 */ 1109 */
1012 1110
1013 #ifdef PNG_READ_INVERT_SUPPORTED
1014 /* Invert monochrome files to have 0 as white and 1 as black 1111 /* Invert monochrome files to have 0 as white and 1 as black
1015 */ 1112 */
1016 if (transforms & PNG_TRANSFORM_INVERT_MONO) 1113 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
1114 #ifdef PNG_READ_INVERT_SUPPORTED
1017 png_set_invert_mono(png_ptr); 1115 png_set_invert_mono(png_ptr);
1018 #endif 1116 #else
1019 1117 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
1020 #ifdef PNG_READ_SHIFT_SUPPORTED 1118 #endif
1119
1021 /* If you want to shift the pixel values from the range [0,255] or 1120 /* If you want to shift the pixel values from the range [0,255] or
1022 * [0,65535] to the original [0,7] or [0,31], or whatever range the 1121 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1023 * colors were originally in: 1122 * colors were originally in:
1024 */ 1123 */
1025 if ((transforms & PNG_TRANSFORM_SHIFT) 1124 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
1026 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) 1125 #ifdef PNG_READ_SHIFT_SUPPORTED
1027 { 1126 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
1028 png_color_8p sig_bit; 1127 png_set_shift(png_ptr, &info_ptr->sig_bit);
1029 1128 #else
1030 png_get_sBIT(png_ptr, info_ptr, &sig_bit); 1129 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
1031 png_set_shift(png_ptr, sig_bit); 1130 #endif
1032 } 1131
1033 #endif 1132 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1034 1133 if ((transforms & PNG_TRANSFORM_BGR) != 0)
1035 #ifdef PNG_READ_BGR_SUPPORTED 1134 #ifdef PNG_READ_BGR_SUPPORTED
1036 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1037 if (transforms & PNG_TRANSFORM_BGR)
1038 png_set_bgr(png_ptr); 1135 png_set_bgr(png_ptr);
1039 #endif 1136 #else
1040 1137 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
1138 #endif
1139
1140 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1141 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
1041 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED 1142 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1042 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1043 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1044 png_set_swap_alpha(png_ptr); 1143 png_set_swap_alpha(png_ptr);
1045 #endif 1144 #else
1046 1145 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1146 #endif
1147
1148 /* Swap bytes of 16-bit files to least significant byte first */
1149 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
1047 #ifdef PNG_READ_SWAP_SUPPORTED 1150 #ifdef PNG_READ_SWAP_SUPPORTED
1048 /* Swap bytes of 16-bit files to least significant byte first */
1049 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1050 png_set_swap(png_ptr); 1151 png_set_swap(png_ptr);
1152 #else
1153 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1051 #endif 1154 #endif
1052 1155
1053 /* Added at libpng-1.2.41 */ 1156 /* Added at libpng-1.2.41 */
1157 /* Invert the alpha channel from opacity to transparency */
1158 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1054 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED 1159 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1055 /* Invert the alpha channel from opacity to transparency */
1056 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1057 png_set_invert_alpha(png_ptr); 1160 png_set_invert_alpha(png_ptr);
1161 #else
1162 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1058 #endif 1163 #endif
1059 1164
1060 /* Added at libpng-1.2.41 */ 1165 /* Added at libpng-1.2.41 */
1166 /* Expand grayscale image to RGB */
1167 if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
1061 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 1168 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1062 /* Expand grayscale image to RGB */
1063 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1064 png_set_gray_to_rgb(png_ptr); 1169 png_set_gray_to_rgb(png_ptr);
1170 #else
1171 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1065 #endif 1172 #endif
1066 1173
1067 /* Added at libpng-1.5.4 */ 1174 /* Added at libpng-1.5.4 */
1175 if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
1068 #ifdef PNG_READ_EXPAND_16_SUPPORTED 1176 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1069 if (transforms & PNG_TRANSFORM_EXPAND_16)
1070 png_set_expand_16(png_ptr); 1177 png_set_expand_16(png_ptr);
1178 #else
1179 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
1071 #endif 1180 #endif
1072 1181
1073 /* We don't handle adding filler bytes */ 1182 /* We don't handle adding filler bytes */
1074 1183
1075 /* We use png_read_image and rely on that for interlace handling, but we also 1184 /* We use png_read_image and rely on that for interlace handling, but we also
1076 * call png_read_update_info therefore must turn on interlace handling now: 1185 * call png_read_update_info therefore must turn on interlace handling now:
1077 */ 1186 */
1078 (void)png_set_interlace_handling(png_ptr); 1187 (void)png_set_interlace_handling(png_ptr);
1079 1188
1080 /* Optional call to gamma correct and add the background to the palette 1189 /* Optional call to gamma correct and add the background to the palette
1081 * and update info structure. REQUIRED if you are expecting libpng to 1190 * and update info structure. REQUIRED if you are expecting libpng to
1082 * update the palette for you (i.e., you selected such a transform above). 1191 * update the palette for you (i.e., you selected such a transform above).
1083 */ 1192 */
1084 png_read_update_info(png_ptr, info_ptr); 1193 png_read_update_info(png_ptr, info_ptr);
1085 1194
1086 /* -------------- image transformations end here ------------------- */ 1195 /* -------------- image transformations end here ------------------- */
1087 1196
1088 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); 1197 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1089 if (info_ptr->row_pointers == NULL) 1198 if (info_ptr->row_pointers == NULL)
1090 { 1199 {
1091 png_uint_32 iptr; 1200 png_uint_32 iptr;
1092 1201
1093 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, 1202 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1094 info_ptr->height * (sizeof (png_bytep))); 1203 info_ptr->height * (sizeof (png_bytep))));
1204
1095 for (iptr=0; iptr<info_ptr->height; iptr++) 1205 for (iptr=0; iptr<info_ptr->height; iptr++)
1096 info_ptr->row_pointers[iptr] = NULL; 1206 info_ptr->row_pointers[iptr] = NULL;
1097 1207
1098 info_ptr->free_me |= PNG_FREE_ROWS; 1208 info_ptr->free_me |= PNG_FREE_ROWS;
1099 1209
1100 for (row = 0; row < (int)info_ptr->height; row++) 1210 for (iptr = 0; iptr < info_ptr->height; iptr++)
1101 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, 1211 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1102 png_get_rowbytes(png_ptr, info_ptr)); 1212 png_malloc(png_ptr, info_ptr->rowbytes));
1103 } 1213 }
1104 1214
1105 png_read_image(png_ptr, info_ptr->row_pointers); 1215 png_read_image(png_ptr, info_ptr->row_pointers);
1106 info_ptr->valid |= PNG_INFO_IDAT; 1216 info_ptr->valid |= PNG_INFO_IDAT;
1107 1217
1108 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ 1218 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1109 png_read_end(png_ptr, info_ptr); 1219 png_read_end(png_ptr, info_ptr);
1110 1220
1111 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1112 PNG_UNUSED(params) 1221 PNG_UNUSED(params)
1113
1114 } 1222 }
1115 #endif /* PNG_INFO_IMAGE_SUPPORTED */ 1223 #endif /* INFO_IMAGE */
1116 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ 1224 #endif /* SEQUENTIAL_READ */
1117 1225
1118 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED 1226 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1119 /* SIMPLIFIED READ 1227 /* SIMPLIFIED READ
1120 * 1228 *
1121 * This code currently relies on the sequential reader, though it could easily 1229 * This code currently relies on the sequential reader, though it could easily
1122 * be made to work with the progressive one. 1230 * be made to work with the progressive one.
1123 */ 1231 */
1124 /* Arguments to png_image_finish_read: */ 1232 /* Arguments to png_image_finish_read: */
1125 1233
1126 /* Encoding of PNG data (used by the color-map code) */ 1234 /* Encoding of PNG data (used by the color-map code) */
1127 /* TODO: change these, dang, ANSI-C reserves the 'E' namespace. */ 1235 # define P_NOTSET 0 /* File encoding not yet known */
1128 # define E_NOTSET 0 /* File encoding not yet known */ 1236 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1129 # define E_sRGB 1 /* 8-bit encoded to sRGB gamma */ 1237 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1130 # define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ 1238 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1131 # define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ 1239 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1132 # define E_LINEAR8 4 /* 8-bit linear: only from a file value */
1133 1240
1134 /* Color-map processing: after libpng has run on the PNG image further 1241 /* Color-map processing: after libpng has run on the PNG image further
1135 * processing may be needed to conver the data to color-map indicies. 1242 * processing may be needed to convert the data to color-map indices.
1136 */ 1243 */
1137 #define PNG_CMAP_NONE 0 1244 #define PNG_CMAP_NONE 0
1138 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ 1245 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1139 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ 1246 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1140 #define PNG_CMAP_RGB 3 /* Process RGB data */ 1247 #define PNG_CMAP_RGB 3 /* Process RGB data */
1141 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ 1248 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1142 1249
1143 /* The following document where the background is for each processing case. */ 1250 /* The following document where the background is for each processing case. */
1144 #define PNG_CMAP_NONE_BACKGROUND 256 1251 #define PNG_CMAP_NONE_BACKGROUND 256
1145 #define PNG_CMAP_GA_BACKGROUND 231 1252 #define PNG_CMAP_GA_BACKGROUND 231
1146 #define PNG_CMAP_TRANS_BACKGROUND 254 1253 #define PNG_CMAP_TRANS_BACKGROUND 254
1147 #define PNG_CMAP_RGB_BACKGROUND 256 1254 #define PNG_CMAP_RGB_BACKGROUND 256
1148 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 1255 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1149 1256
1150 typedef struct 1257 typedef struct
1151 { 1258 {
1152 /* Arguments: */ 1259 /* Arguments: */
1153 png_imagep image; 1260 png_imagep image;
1154 png_voidp buffer; 1261 png_voidp buffer;
1155 png_int_32 row_stride; 1262 png_int_32 row_stride;
1156 png_voidp colormap; 1263 png_voidp colormap;
1157 png_const_colorp background; 1264 png_const_colorp background;
1158 /* Local variables: */ 1265 /* Local variables: */
1159 png_voidp local_row; 1266 png_voidp local_row;
1160 png_voidp first_row; 1267 png_voidp first_row;
1161 ptrdiff_t row_bytes; /* step between rows */ 1268 ptrdiff_t row_bytes; /* step between rows */
1162 int file_encoding; /* E_ values above */ 1269 int file_encoding; /* E_ values above */
1163 png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */ 1270 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
1164 int colormap_processing; /* PNG_CMAP_ values above */ 1271 int colormap_processing; /* PNG_CMAP_ values above */
1165 } png_image_read_control; 1272 } png_image_read_control;
1166 1273
1167 /* Do all the *safe* initialization - 'safe' means that png_error won't be 1274 /* Do all the *safe* initialization - 'safe' means that png_error won't be
1168 * called, so setting up the jmp_buf is not required. This means that anything 1275 * called, so setting up the jmp_buf is not required. This means that anything
1169 * called from here must *not* call png_malloc - it has to call png_malloc_warn 1276 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1170 * instead so that control is returned safely back to this routine. 1277 * instead so that control is returned safely back to this routine.
1171 */ 1278 */
1172 static int 1279 static int
1173 png_image_read_init(png_imagep image) 1280 png_image_read_init(png_imagep image)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 1323
1217 return png_image_error(image, "png_image_read: opaque pointer not NULL"); 1324 return png_image_error(image, "png_image_read: opaque pointer not NULL");
1218 } 1325 }
1219 1326
1220 /* Utility to find the base format of a PNG file from a png_struct. */ 1327 /* Utility to find the base format of a PNG file from a png_struct. */
1221 static png_uint_32 1328 static png_uint_32
1222 png_image_format(png_structrp png_ptr) 1329 png_image_format(png_structrp png_ptr)
1223 { 1330 {
1224 png_uint_32 format = 0; 1331 png_uint_32 format = 0;
1225 1332
1226 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) 1333 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1227 format |= PNG_FORMAT_FLAG_COLOR; 1334 format |= PNG_FORMAT_FLAG_COLOR;
1228 1335
1229 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) 1336 if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
1230 format |= PNG_FORMAT_FLAG_ALPHA; 1337 format |= PNG_FORMAT_FLAG_ALPHA;
1231 1338
1232 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS 1339 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1233 * sets the png_struct fields; that's all we are interested in here. The 1340 * sets the png_struct fields; that's all we are interested in here. The
1234 * precise interaction with an app call to png_set_tRNS and PNG file reading 1341 * precise interaction with an app call to png_set_tRNS and PNG file reading
1235 * is unclear. 1342 * is unclear.
1236 */ 1343 */
1237 else if (png_ptr->num_trans > 0) 1344 else if (png_ptr->num_trans > 0)
1238 format |= PNG_FORMAT_FLAG_ALPHA; 1345 format |= PNG_FORMAT_FLAG_ALPHA;
1239 1346
1240 if (png_ptr->bit_depth == 16) 1347 if (png_ptr->bit_depth == 16)
1241 format |= PNG_FORMAT_FLAG_LINEAR; 1348 format |= PNG_FORMAT_FLAG_LINEAR;
1242 1349
1243 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE) 1350 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
1244 format |= PNG_FORMAT_FLAG_COLORMAP; 1351 format |= PNG_FORMAT_FLAG_COLORMAP;
1245 1352
1246 return format; 1353 return format;
1247 } 1354 }
1248 1355
1249 /* Is the given gamma significantly different from sRGB? The test is the same 1356 /* Is the given gamma significantly different from sRGB? The test is the same
1250 * one used in pngrtran.c when deciding whether to do gamma correction. The 1357 * one used in pngrtran.c when deciding whether to do gamma correction. The
1251 * arithmetic optimizes the division by using the fact that the inverse of the 1358 * arithmetic optimizes the division by using the fact that the inverse of the
1252 * file sRGB gamma is 2.2 1359 * file sRGB gamma is 2.2
1253 */ 1360 */
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 image->height = png_ptr->height; 1392 image->height = png_ptr->height;
1286 1393
1287 { 1394 {
1288 png_uint_32 format = png_image_format(png_ptr); 1395 png_uint_32 format = png_image_format(png_ptr);
1289 1396
1290 image->format = format; 1397 image->format = format;
1291 1398
1292 #ifdef PNG_COLORSPACE_SUPPORTED 1399 #ifdef PNG_COLORSPACE_SUPPORTED
1293 /* Does the colorspace match sRGB? If there is no color endpoint 1400 /* Does the colorspace match sRGB? If there is no color endpoint
1294 * (colorant) information assume yes, otherwise require the 1401 * (colorant) information assume yes, otherwise require the
1295 * 'ENDPOINTS_MATCHE_sRGB' colorspace flag to have been set. If the 1402 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1296 * colorspace has been determined to be invalid ignore it. 1403 * colorspace has been determined to be invalid ignore it.
1297 */ 1404 */
1298 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags 1405 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1299 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| 1406 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1300 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) 1407 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1301 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; 1408 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1302 #endif 1409 #endif
1303 } 1410 }
1304 1411
1305 /* We need the maximum number of entries regardless of the format the 1412 /* We need the maximum number of entries regardless of the format the
(...skipping 27 matching lines...) Expand all
1333 } 1440 }
1334 1441
1335 #ifdef PNG_STDIO_SUPPORTED 1442 #ifdef PNG_STDIO_SUPPORTED
1336 int PNGAPI 1443 int PNGAPI
1337 png_image_begin_read_from_stdio(png_imagep image, FILE* file) 1444 png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1338 { 1445 {
1339 if (image != NULL && image->version == PNG_IMAGE_VERSION) 1446 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1340 { 1447 {
1341 if (file != NULL) 1448 if (file != NULL)
1342 { 1449 {
1343 if (png_image_read_init(image)) 1450 if (png_image_read_init(image) != 0)
1344 { 1451 {
1345 /* This is slightly evil, but png_init_io doesn't do anything other 1452 /* This is slightly evil, but png_init_io doesn't do anything other
1346 * than this and we haven't changed the standard IO functions so 1453 * than this and we haven't changed the standard IO functions so
1347 * this saves a 'safe' function. 1454 * this saves a 'safe' function.
1348 */ 1455 */
1349 image->opaque->png_ptr->io_ptr = file; 1456 image->opaque->png_ptr->io_ptr = file;
1350 return png_safe_execute(image, png_image_read_header, image); 1457 return png_safe_execute(image, png_image_read_header, image);
1351 } 1458 }
1352 } 1459 }
1353 1460
(...skipping 13 matching lines...) Expand all
1367 png_image_begin_read_from_file(png_imagep image, const char *file_name) 1474 png_image_begin_read_from_file(png_imagep image, const char *file_name)
1368 { 1475 {
1369 if (image != NULL && image->version == PNG_IMAGE_VERSION) 1476 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1370 { 1477 {
1371 if (file_name != NULL) 1478 if (file_name != NULL)
1372 { 1479 {
1373 FILE *fp = fopen(file_name, "rb"); 1480 FILE *fp = fopen(file_name, "rb");
1374 1481
1375 if (fp != NULL) 1482 if (fp != NULL)
1376 { 1483 {
1377 if (png_image_read_init(image)) 1484 if (png_image_read_init(image) != 0)
1378 { 1485 {
1379 image->opaque->png_ptr->io_ptr = fp; 1486 image->opaque->png_ptr->io_ptr = fp;
1380 image->opaque->owned_file = 1; 1487 image->opaque->owned_file = 1;
1381 return png_safe_execute(image, png_image_read_header, image); 1488 return png_safe_execute(image, png_image_read_header, image);
1382 } 1489 }
1383 1490
1384 /* Clean up: just the opened file. */ 1491 /* Clean up: just the opened file. */
1385 (void)fclose(fp); 1492 (void)fclose(fp);
1386 } 1493 }
1387 1494
1388 else 1495 else
1389 return png_image_error(image, strerror(errno)); 1496 return png_image_error(image, strerror(errno));
1390 } 1497 }
1391 1498
1392 else 1499 else
1393 return png_image_error(image, 1500 return png_image_error(image,
1394 "png_image_begin_read_from_file: invalid argument"); 1501 "png_image_begin_read_from_file: invalid argument");
1395 } 1502 }
1396 1503
1397 else if (image != NULL) 1504 else if (image != NULL)
1398 return png_image_error(image, 1505 return png_image_error(image,
1399 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); 1506 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1400 1507
1401 return 0; 1508 return 0;
1402 } 1509 }
1403 #endif /* PNG_STDIO_SUPPORTED */ 1510 #endif /* STDIO */
1404 1511
1405 static void PNGCBAPI 1512 static void PNGCBAPI
1406 png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) 1513 png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1407 { 1514 {
1408 if (png_ptr != NULL) 1515 if (png_ptr != NULL)
1409 { 1516 {
1410 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); 1517 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1411 if (image != NULL) 1518 if (image != NULL)
1412 { 1519 {
1413 png_controlp cp = image->opaque; 1520 png_controlp cp = image->opaque;
(...skipping 18 matching lines...) Expand all
1432 } 1539 }
1433 } 1540 }
1434 1541
1435 int PNGAPI png_image_begin_read_from_memory(png_imagep image, 1542 int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1436 png_const_voidp memory, png_size_t size) 1543 png_const_voidp memory, png_size_t size)
1437 { 1544 {
1438 if (image != NULL && image->version == PNG_IMAGE_VERSION) 1545 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1439 { 1546 {
1440 if (memory != NULL && size > 0) 1547 if (memory != NULL && size > 0)
1441 { 1548 {
1442 if (png_image_read_init(image)) 1549 if (png_image_read_init(image) != 0)
1443 { 1550 {
1444 /* Now set the IO functions to read from the memory buffer and 1551 /* Now set the IO functions to read from the memory buffer and
1445 * store it into io_ptr. Again do this in-place to avoid calling a 1552 * store it into io_ptr. Again do this in-place to avoid calling a
1446 * libpng function that requires error handling. 1553 * libpng function that requires error handling.
1447 */ 1554 */
1448 image->opaque->memory = png_voidcast(png_const_bytep, memory); 1555 image->opaque->memory = png_voidcast(png_const_bytep, memory);
1449 image->opaque->size = size; 1556 image->opaque->size = size;
1450 image->opaque->png_ptr->io_ptr = image; 1557 image->opaque->png_ptr->io_ptr = image;
1451 image->opaque->png_ptr->read_data_fn = png_image_memory_read; 1558 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1452 1559
(...skipping 21 matching lines...) Expand all
1474 png_image_skip_unused_chunks(png_structrp png_ptr) 1581 png_image_skip_unused_chunks(png_structrp png_ptr)
1475 { 1582 {
1476 /* Prepare the reader to ignore all recognized chunks whose data will not 1583 /* Prepare the reader to ignore all recognized chunks whose data will not
1477 * be used, i.e., all chunks recognized by libpng except for those 1584 * be used, i.e., all chunks recognized by libpng except for those
1478 * involved in basic image reading: 1585 * involved in basic image reading:
1479 * 1586 *
1480 * IHDR, PLTE, IDAT, IEND 1587 * IHDR, PLTE, IDAT, IEND
1481 * 1588 *
1482 * Or image data handling: 1589 * Or image data handling:
1483 * 1590 *
1484 * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT. 1591 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1485 * 1592 *
1486 * This provides a small performance improvement and eliminates any 1593 * This provides a small performance improvement and eliminates any
1487 * potential vulnerability to security problems in the unused chunks. 1594 * potential vulnerability to security problems in the unused chunks.
1595 *
1596 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1597 * too. This allows the simplified API to be compiled without iCCP support,
1598 * however if the support is there the chunk is still checked to detect
1599 * errors (which are unfortunately quite common.)
1488 */ 1600 */
1489 { 1601 {
1490 static PNG_CONST png_byte chunks_to_process[] = { 1602 static PNG_CONST png_byte chunks_to_process[] = {
1491 98, 75, 71, 68, '\0', /* bKGD */ 1603 98, 75, 71, 68, '\0', /* bKGD */
1492 99, 72, 82, 77, '\0', /* cHRM */ 1604 99, 72, 82, 77, '\0', /* cHRM */
1493 103, 65, 77, 65, '\0', /* gAMA */ 1605 103, 65, 77, 65, '\0', /* gAMA */
1606 # ifdef PNG_READ_iCCP_SUPPORTED
1494 105, 67, 67, 80, '\0', /* iCCP */ 1607 105, 67, 67, 80, '\0', /* iCCP */
1608 # endif
1495 115, 66, 73, 84, '\0', /* sBIT */ 1609 115, 66, 73, 84, '\0', /* sBIT */
1496 115, 82, 71, 66, '\0', /* sRGB */ 1610 115, 82, 71, 66, '\0', /* sRGB */
1497 }; 1611 };
1498 1612
1499 /* Ignore unknown chunks and all other chunks except for the 1613 /* Ignore unknown chunks and all other chunks except for the
1500 * IHDR, PLTE, tRNS, IDAT, and IEND chunks. 1614 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1501 */ 1615 */
1502 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, 1616 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1503 NULL, -1); 1617 NULL, -1);
1504 1618
1505 /* But do not ignore image data handling chunks */ 1619 /* But do not ignore image data handling chunks */
1506 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, 1620 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1507 chunks_to_process, (sizeof chunks_to_process)/5); 1621 chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
1508 } 1622 }
1509 } 1623 }
1510 1624
1511 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) 1625 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1512 #else 1626 #else
1513 # define PNG_SKIP_CHUNKS(p) ((void)0) 1627 # define PNG_SKIP_CHUNKS(p) ((void)0)
1514 #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */ 1628 #endif /* HANDLE_AS_UNKNOWN */
1515 1629
1516 /* The following macro gives the exact rounded answer for all values in the 1630 /* The following macro gives the exact rounded answer for all values in the
1517 * range 0..255 (it actually divides by 51.2, but the rounding still generates 1631 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1518 * the correct numbers 0..5 1632 * the correct numbers 0..5
1519 */ 1633 */
1520 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) 1634 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1521 1635
1522 /* Utility functions to make particular color-maps */ 1636 /* Utility functions to make particular color-maps */
1523 static void 1637 static void
1524 set_file_encoding(png_image_read_control *display) 1638 set_file_encoding(png_image_read_control *display)
1525 { 1639 {
1526 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; 1640 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
1527 if (png_gamma_significant(g)) 1641 if (png_gamma_significant(g) != 0)
1528 { 1642 {
1529 if (png_gamma_not_sRGB(g)) 1643 if (png_gamma_not_sRGB(g) != 0)
1530 { 1644 {
1531 display->file_encoding = E_FILE; 1645 display->file_encoding = P_FILE;
1532 display->gamma_to_linear = png_reciprocal(g); 1646 display->gamma_to_linear = png_reciprocal(g);
1533 } 1647 }
1534 1648
1535 else 1649 else
1536 display->file_encoding = E_sRGB; 1650 display->file_encoding = P_sRGB;
1537 } 1651 }
1538 1652
1539 else 1653 else
1540 display->file_encoding = E_LINEAR8; 1654 display->file_encoding = P_LINEAR8;
1541 } 1655 }
1542 1656
1543 static unsigned int 1657 static unsigned int
1544 decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) 1658 decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1545 { 1659 {
1546 if (encoding == E_FILE) /* double check */ 1660 if (encoding == P_FILE) /* double check */
1547 encoding = display->file_encoding; 1661 encoding = display->file_encoding;
1548 1662
1549 if (encoding == E_NOTSET) /* must be the file encoding */ 1663 if (encoding == P_NOTSET) /* must be the file encoding */
1550 { 1664 {
1551 set_file_encoding(display); 1665 set_file_encoding(display);
1552 encoding = display->file_encoding; 1666 encoding = display->file_encoding;
1553 } 1667 }
1554 1668
1555 switch (encoding) 1669 switch (encoding)
1556 { 1670 {
1557 case E_FILE: 1671 case P_FILE:
1558 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); 1672 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1559 break; 1673 break;
1560 1674
1561 case E_sRGB: 1675 case P_sRGB:
1562 value = png_sRGB_table[value]; 1676 value = png_sRGB_table[value];
1563 break; 1677 break;
1564 1678
1565 case E_LINEAR: 1679 case P_LINEAR:
1566 break; 1680 break;
1567 1681
1568 case E_LINEAR8: 1682 case P_LINEAR8:
1569 value *= 257; 1683 value *= 257;
1570 break; 1684 break;
1571 1685
1686 #ifdef __GNUC__
1572 default: 1687 default:
1573 png_error(display->image->opaque->png_ptr, 1688 png_error(display->image->opaque->png_ptr,
1574 "unexpected encoding (internal error)"); 1689 "unexpected encoding (internal error)");
1575 break; 1690 #endif
1576 } 1691 }
1577 1692
1578 return value; 1693 return value;
1579 } 1694 }
1580 1695
1581 static png_uint_32 1696 static png_uint_32
1582 png_colormap_compose(png_image_read_control *display, 1697 png_colormap_compose(png_image_read_control *display,
1583 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, 1698 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1584 png_uint_32 background, int encoding) 1699 png_uint_32 background, int encoding)
1585 { 1700 {
1586 /* The file value is composed on the background, the background has the given 1701 /* The file value is composed on the background, the background has the given
1587 * encoding and so does the result, the file is encoded with E_FILE and the 1702 * encoding and so does the result, the file is encoded with P_FILE and the
1588 * file and alpha are 8-bit values. The (output) encoding will always be 1703 * file and alpha are 8-bit values. The (output) encoding will always be
1589 * E_LINEAR or E_sRGB. 1704 * P_LINEAR or P_sRGB.
1590 */ 1705 */
1591 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); 1706 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1592 png_uint_32 b = decode_gamma(display, background, encoding); 1707 png_uint_32 b = decode_gamma(display, background, encoding);
1593 1708
1594 /* The alpha is always an 8-bit value (it comes from the palette), the value 1709 /* The alpha is always an 8-bit value (it comes from the palette), the value
1595 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. 1710 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1596 */ 1711 */
1597 f = f * alpha + b * (255-alpha); 1712 f = f * alpha + b * (255-alpha);
1598 1713
1599 if (encoding == E_LINEAR) 1714 if (encoding == P_LINEAR)
1600 { 1715 {
1601 /* Scale to 65535; divide by 255, approximately (in fact this is extremely 1716 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1602 * accurate, it divides by 255.00000005937181414556, with no overflow.) 1717 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1603 */ 1718 */
1604 f *= 257; /* Now scaled by 65535 */ 1719 f *= 257; /* Now scaled by 65535 */
1605 f += f >> 16; 1720 f += f >> 16;
1606 f = (f+32768) >> 16; 1721 f = (f+32768) >> 16;
1607 } 1722 }
1608 1723
1609 else /* E_sRGB */ 1724 else /* P_sRGB */
1610 f = PNG_sRGB_FROM_LINEAR(f); 1725 f = PNG_sRGB_FROM_LINEAR(f);
1611 1726
1612 return f; 1727 return f;
1613 } 1728 }
1614 1729
1615 /* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must 1730 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1616 * be 8-bit. 1731 * be 8-bit.
1617 */ 1732 */
1618 static void 1733 static void
1619 png_create_colormap_entry(png_image_read_control *display, 1734 png_create_colormap_entry(png_image_read_control *display,
1620 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, 1735 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1621 png_uint_32 alpha, int encoding) 1736 png_uint_32 alpha, int encoding)
1622 { 1737 {
1623 png_imagep image = display->image; 1738 png_imagep image = display->image;
1624 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ? 1739 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1625 E_LINEAR : E_sRGB; 1740 P_LINEAR : P_sRGB;
1626 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && 1741 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1627 (red != green || green != blue); 1742 (red != green || green != blue);
1628 1743
1629 if (ip > 255) 1744 if (ip > 255)
1630 png_error(image->opaque->png_ptr, "color-map index out of range"); 1745 png_error(image->opaque->png_ptr, "color-map index out of range");
1631 1746
1632 /* Update the cache with whether the file gamma is significantly different 1747 /* Update the cache with whether the file gamma is significantly different
1633 * from sRGB. 1748 * from sRGB.
1634 */ 1749 */
1635 if (encoding == E_FILE) 1750 if (encoding == P_FILE)
1636 { 1751 {
1637 if (display->file_encoding == E_NOTSET) 1752 if (display->file_encoding == P_NOTSET)
1638 set_file_encoding(display); 1753 set_file_encoding(display);
1639 1754
1640 /* Note that the cached value may be E_FILE too, but if it is then the 1755 /* Note that the cached value may be P_FILE too, but if it is then the
1641 * gamma_to_linear member has been set. 1756 * gamma_to_linear member has been set.
1642 */ 1757 */
1643 encoding = display->file_encoding; 1758 encoding = display->file_encoding;
1644 } 1759 }
1645 1760
1646 if (encoding == E_FILE) 1761 if (encoding == P_FILE)
1647 { 1762 {
1648 png_fixed_point g = display->gamma_to_linear; 1763 png_fixed_point g = display->gamma_to_linear;
1649 1764
1650 red = png_gamma_16bit_correct(red*257, g); 1765 red = png_gamma_16bit_correct(red*257, g);
1651 green = png_gamma_16bit_correct(green*257, g); 1766 green = png_gamma_16bit_correct(green*257, g);
1652 blue = png_gamma_16bit_correct(blue*257, g); 1767 blue = png_gamma_16bit_correct(blue*257, g);
1653 1768
1654 if (convert_to_Y || output_encoding == E_LINEAR) 1769 if (convert_to_Y != 0 || output_encoding == P_LINEAR)
1655 { 1770 {
1656 alpha *= 257; 1771 alpha *= 257;
1657 encoding = E_LINEAR; 1772 encoding = P_LINEAR;
1658 } 1773 }
1659 1774
1660 else 1775 else
1661 { 1776 {
1662 red = PNG_sRGB_FROM_LINEAR(red * 255); 1777 red = PNG_sRGB_FROM_LINEAR(red * 255);
1663 green = PNG_sRGB_FROM_LINEAR(green * 255); 1778 green = PNG_sRGB_FROM_LINEAR(green * 255);
1664 blue = PNG_sRGB_FROM_LINEAR(blue * 255); 1779 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1665 encoding = E_sRGB; 1780 encoding = P_sRGB;
1666 } 1781 }
1667 } 1782 }
1668 1783
1669 else if (encoding == E_LINEAR8) 1784 else if (encoding == P_LINEAR8)
1670 { 1785 {
1671 /* This encoding occurs quite frequently in test cases because PngSuite 1786 /* This encoding occurs quite frequently in test cases because PngSuite
1672 * includes a gAMA 1.0 chunk with most images. 1787 * includes a gAMA 1.0 chunk with most images.
1673 */ 1788 */
1674 red *= 257; 1789 red *= 257;
1675 green *= 257; 1790 green *= 257;
1676 blue *= 257; 1791 blue *= 257;
1677 alpha *= 257; 1792 alpha *= 257;
1678 encoding = E_LINEAR; 1793 encoding = P_LINEAR;
1679 } 1794 }
1680 1795
1681 else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR)) 1796 else if (encoding == P_sRGB &&
1797 (convert_to_Y != 0 || output_encoding == P_LINEAR))
1682 { 1798 {
1683 /* The values are 8-bit sRGB values, but must be converted to 16-bit 1799 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1684 * linear. 1800 * linear.
1685 */ 1801 */
1686 red = png_sRGB_table[red]; 1802 red = png_sRGB_table[red];
1687 green = png_sRGB_table[green]; 1803 green = png_sRGB_table[green];
1688 blue = png_sRGB_table[blue]; 1804 blue = png_sRGB_table[blue];
1689 alpha *= 257; 1805 alpha *= 257;
1690 encoding = E_LINEAR; 1806 encoding = P_LINEAR;
1691 } 1807 }
1692 1808
1693 /* This is set if the color isn't gray but the output is. */ 1809 /* This is set if the color isn't gray but the output is. */
1694 if (encoding == E_LINEAR) 1810 if (encoding == P_LINEAR)
1695 { 1811 {
1696 if (convert_to_Y) 1812 if (convert_to_Y != 0)
1697 { 1813 {
1698 /* NOTE: these values are copied from png_do_rgb_to_gray */ 1814 /* NOTE: these values are copied from png_do_rgb_to_gray */
1699 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + 1815 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1700 (png_uint_32)2366 * blue; 1816 (png_uint_32)2366 * blue;
1701 1817
1702 if (output_encoding == E_LINEAR) 1818 if (output_encoding == P_LINEAR)
1703 y = (y + 16384) >> 15; 1819 y = (y + 16384) >> 15;
1704 1820
1705 else 1821 else
1706 { 1822 {
1707 /* y is scaled by 32768, we need it scaled by 255: */ 1823 /* y is scaled by 32768, we need it scaled by 255: */
1708 y = (y + 128) >> 8; 1824 y = (y + 128) >> 8;
1709 y *= 255; 1825 y *= 255;
1710 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); 1826 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1711 encoding = E_sRGB; 1827 alpha = PNG_DIV257(alpha);
1828 encoding = P_sRGB;
1712 } 1829 }
1713 1830
1714 blue = red = green = y; 1831 blue = red = green = y;
1715 } 1832 }
1716 1833
1717 else if (output_encoding == E_sRGB) 1834 else if (output_encoding == P_sRGB)
1718 { 1835 {
1719 red = PNG_sRGB_FROM_LINEAR(red * 255); 1836 red = PNG_sRGB_FROM_LINEAR(red * 255);
1720 green = PNG_sRGB_FROM_LINEAR(green * 255); 1837 green = PNG_sRGB_FROM_LINEAR(green * 255);
1721 blue = PNG_sRGB_FROM_LINEAR(blue * 255); 1838 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1722 alpha = PNG_DIV257(alpha); 1839 alpha = PNG_DIV257(alpha);
1723 encoding = E_sRGB; 1840 encoding = P_sRGB;
1724 } 1841 }
1725 } 1842 }
1726 1843
1727 if (encoding != output_encoding) 1844 if (encoding != output_encoding)
1728 png_error(image->opaque->png_ptr, "bad encoding (internal error)"); 1845 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1729 1846
1730 /* Store the value. */ 1847 /* Store the value. */
1731 { 1848 {
1732 # ifdef PNG_FORMAT_BGR_SUPPORTED 1849 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1733 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && 1850 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1734 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; 1851 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1735 # else 1852 # else
1736 # define afirst 0 1853 # define afirst 0
1737 # endif 1854 # endif
1738 # ifdef PNG_FORMAT_BGR_SUPPORTED 1855 # ifdef PNG_FORMAT_BGR_SUPPORTED
1739 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0; 1856 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1740 # else 1857 # else
1741 # define bgr 0 1858 # define bgr 0
1742 # endif 1859 # endif
1743 1860
1744 if (output_encoding == E_LINEAR) 1861 if (output_encoding == P_LINEAR)
1745 { 1862 {
1746 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); 1863 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1747 1864
1748 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); 1865 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1749 1866
1750 /* The linear 16-bit values must be pre-multiplied by the alpha channel 1867 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1751 * value, if less than 65535 (this is, effectively, composite on black 1868 * value, if less than 65535 (this is, effectively, composite on black
1752 * if the alpha channel is removed.) 1869 * if the alpha channel is removed.)
1753 */ 1870 */
1754 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) 1871 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1789 green = 0; 1906 green = 0;
1790 } 1907 }
1791 entry[afirst] = (png_uint_16)green; 1908 entry[afirst] = (png_uint_16)green;
1792 break; 1909 break;
1793 1910
1794 default: 1911 default:
1795 break; 1912 break;
1796 } 1913 }
1797 } 1914 }
1798 1915
1799 else /* output encoding is E_sRGB */ 1916 else /* output encoding is P_sRGB */
1800 { 1917 {
1801 png_bytep entry = png_voidcast(png_bytep, display->colormap); 1918 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1802 1919
1803 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); 1920 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1804 1921
1805 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) 1922 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1806 { 1923 {
1807 case 4: 1924 case 4:
1808 entry[afirst ? 0 : 3] = (png_byte)alpha; 1925 entry[afirst ? 0 : 3] = (png_byte)alpha;
1809 case 3: 1926 case 3:
(...skipping 21 matching lines...) Expand all
1831 # endif 1948 # endif
1832 } 1949 }
1833 } 1950 }
1834 1951
1835 static int 1952 static int
1836 make_gray_file_colormap(png_image_read_control *display) 1953 make_gray_file_colormap(png_image_read_control *display)
1837 { 1954 {
1838 unsigned int i; 1955 unsigned int i;
1839 1956
1840 for (i=0; i<256; ++i) 1957 for (i=0; i<256; ++i)
1841 png_create_colormap_entry(display, i, i, i, i, 255, E_FILE); 1958 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1842 1959
1843 return i; 1960 return i;
1844 } 1961 }
1845 1962
1846 static int 1963 static int
1847 make_gray_colormap(png_image_read_control *display) 1964 make_gray_colormap(png_image_read_control *display)
1848 { 1965 {
1849 unsigned int i; 1966 unsigned int i;
1850 1967
1851 for (i=0; i<256; ++i) 1968 for (i=0; i<256; ++i)
1852 png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB); 1969 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
1853 1970
1854 return i; 1971 return i;
1855 } 1972 }
1856 #define PNG_GRAY_COLORMAP_ENTRIES 256 1973 #define PNG_GRAY_COLORMAP_ENTRIES 256
1857 1974
1858 static int 1975 static int
1859 make_ga_colormap(png_image_read_control *display) 1976 make_ga_colormap(png_image_read_control *display)
1860 { 1977 {
1861 unsigned int i, a; 1978 unsigned int i, a;
1862 1979
(...skipping 18 matching lines...) Expand all
1881 * else // partially opaque 1998 * else // partially opaque
1882 * { 1999 * {
1883 * base = 226 + 6 * PNG_DIV51(alpha); 2000 * base = 226 + 6 * PNG_DIV51(alpha);
1884 * entry = PNG_DIV51(gray); 2001 * entry = PNG_DIV51(gray);
1885 * } 2002 * }
1886 */ 2003 */
1887 i = 0; 2004 i = 0;
1888 while (i < 231) 2005 while (i < 231)
1889 { 2006 {
1890 unsigned int gray = (i * 256 + 115) / 231; 2007 unsigned int gray = (i * 256 + 115) / 231;
1891 png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB); 2008 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
1892 } 2009 }
1893 2010
1894 /* 255 is used here for the component values for consistency with the code 2011 /* 255 is used here for the component values for consistency with the code
1895 * that undoes premultiplication in pngwrite.c. 2012 * that undoes premultiplication in pngwrite.c.
1896 */ 2013 */
1897 png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB); 2014 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
1898 2015
1899 for (a=1; a<5; ++a) 2016 for (a=1; a<5; ++a)
1900 { 2017 {
1901 unsigned int g; 2018 unsigned int g;
1902 2019
1903 for (g=0; g<6; ++g) 2020 for (g=0; g<6; ++g)
1904 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, 2021 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1905 E_sRGB); 2022 P_sRGB);
1906 } 2023 }
1907 2024
1908 return i; 2025 return i;
1909 } 2026 }
1910 2027
1911 #define PNG_GA_COLORMAP_ENTRIES 256 2028 #define PNG_GA_COLORMAP_ENTRIES 256
1912 2029
1913 static int 2030 static int
1914 make_rgb_colormap(png_image_read_control *display) 2031 make_rgb_colormap(png_image_read_control *display)
1915 { 2032 {
1916 unsigned int i, r; 2033 unsigned int i, r;
1917 2034
1918 /* Build a 6x6x6 opaque RGB cube */ 2035 /* Build a 6x6x6 opaque RGB cube */
1919 for (i=r=0; r<6; ++r) 2036 for (i=r=0; r<6; ++r)
1920 { 2037 {
1921 unsigned int g; 2038 unsigned int g;
1922 2039
1923 for (g=0; g<6; ++g) 2040 for (g=0; g<6; ++g)
1924 { 2041 {
1925 unsigned int b; 2042 unsigned int b;
1926 2043
1927 for (b=0; b<6; ++b) 2044 for (b=0; b<6; ++b)
1928 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, 2045 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1929 E_sRGB); 2046 P_sRGB);
1930 } 2047 }
1931 } 2048 }
1932 2049
1933 return i; 2050 return i;
1934 } 2051 }
1935 2052
1936 #define PNG_RGB_COLORMAP_ENTRIES 216 2053 #define PNG_RGB_COLORMAP_ENTRIES 216
1937 2054
1938 /* Return a palette index to the above palette given three 8-bit sRGB values. */ 2055 /* Return a palette index to the above palette given three 8-bit sRGB values. */
1939 #define PNG_RGB_INDEX(r,g,b) \ 2056 #define PNG_RGB_INDEX(r,g,b) \
1940 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) 2057 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1941 2058
1942 static int 2059 static int
1943 png_image_read_colormap(png_voidp argument) 2060 png_image_read_colormap(png_voidp argument)
1944 { 2061 {
1945 png_image_read_control *display = 2062 png_image_read_control *display =
1946 png_voidcast(png_image_read_control*, argument); 2063 png_voidcast(png_image_read_control*, argument);
1947 const png_imagep image = display->image; 2064 const png_imagep image = display->image;
1948 2065
1949 const png_structrp png_ptr = image->opaque->png_ptr; 2066 const png_structrp png_ptr = image->opaque->png_ptr;
1950 const png_uint_32 output_format = image->format; 2067 const png_uint_32 output_format = image->format;
1951 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ? 2068 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1952 E_LINEAR : E_sRGB; 2069 P_LINEAR : P_sRGB;
1953 2070
1954 unsigned int cmap_entries; 2071 unsigned int cmap_entries;
1955 unsigned int output_processing; /* Output processing option */ 2072 unsigned int output_processing; /* Output processing option */
1956 unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */ 2073 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
1957 2074
1958 /* Background information; the background color and the index of this color 2075 /* Background information; the background color and the index of this color
1959 * in the color-map if it exists (else 256). 2076 * in the color-map if it exists (else 256).
1960 */ 2077 */
1961 unsigned int background_index = 256; 2078 unsigned int background_index = 256;
1962 png_uint_32 back_r, back_g, back_b; 2079 png_uint_32 back_r, back_g, back_b;
1963 2080
1964 /* Flags to accumulate things that need to be done to the input. */ 2081 /* Flags to accumulate things that need to be done to the input. */
1965 int expand_tRNS = 0; 2082 int expand_tRNS = 0;
1966 2083
1967 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is 2084 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1968 * very difficult to do, the results look awful, and it is difficult to see 2085 * very difficult to do, the results look awful, and it is difficult to see
1969 * what possible use it is because the application can't control the 2086 * what possible use it is because the application can't control the
1970 * color-map. 2087 * color-map.
1971 */ 2088 */
1972 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || 2089 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1973 png_ptr->num_trans > 0) /* alpha in input */ && 2090 png_ptr->num_trans > 0) /* alpha in input */ &&
1974 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) 2091 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1975 { 2092 {
1976 if (output_encoding == E_LINEAR) /* compose on black */ 2093 if (output_encoding == P_LINEAR) /* compose on black */
1977 back_b = back_g = back_r = 0; 2094 back_b = back_g = back_r = 0;
1978 2095
1979 else if (display->background == NULL /* no way to remove it */) 2096 else if (display->background == NULL /* no way to remove it */)
1980 png_error(png_ptr, 2097 png_error(png_ptr,
1981 "a background color must be supplied to remove alpha/transparency"); 2098 "a background color must be supplied to remove alpha/transparency");
1982 2099
1983 /* Get a copy of the background color (this avoids repeating the checks 2100 /* Get a copy of the background color (this avoids repeating the checks
1984 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the 2101 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
1985 * output format. 2102 * output format.
1986 */ 2103 */
1987 else 2104 else
1988 { 2105 {
1989 back_g = display->background->green; 2106 back_g = display->background->green;
1990 if (output_format & PNG_FORMAT_FLAG_COLOR) 2107 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
1991 { 2108 {
1992 back_r = display->background->red; 2109 back_r = display->background->red;
1993 back_b = display->background->blue; 2110 back_b = display->background->blue;
1994 } 2111 }
1995 else 2112 else
1996 back_b = back_r = back_g; 2113 back_b = back_r = back_g;
1997 } 2114 }
1998 } 2115 }
1999 2116
2000 else if (output_encoding == E_LINEAR) 2117 else if (output_encoding == P_LINEAR)
2001 back_b = back_r = back_g = 65535; 2118 back_b = back_r = back_g = 65535;
2002 2119
2003 else 2120 else
2004 back_b = back_r = back_g = 255; 2121 back_b = back_r = back_g = 255;
2005 2122
2006 /* Default the input file gamma if required - this is necessary because 2123 /* Default the input file gamma if required - this is necessary because
2007 * libpng assumes that if no gamma information is present the data is in the 2124 * libpng assumes that if no gamma information is present the data is in the
2008 * output format, but the simplified API deduces the gamma from the input 2125 * output format, but the simplified API deduces the gamma from the input
2009 * format. 2126 * format.
2010 */ 2127 */
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2048 output_processing = PNG_CMAP_NONE; 2165 output_processing = PNG_CMAP_NONE;
2049 2166
2050 /* If there is a tRNS chunk then this either selects a transparent 2167 /* If there is a tRNS chunk then this either selects a transparent
2051 * value or, if the output has no alpha, the background color. 2168 * value or, if the output has no alpha, the background color.
2052 */ 2169 */
2053 if (png_ptr->num_trans > 0) 2170 if (png_ptr->num_trans > 0)
2054 { 2171 {
2055 trans = png_ptr->trans_color.gray; 2172 trans = png_ptr->trans_color.gray;
2056 2173
2057 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) 2174 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2058 back_alpha = output_encoding == E_LINEAR ? 65535 : 255; 2175 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2059 } 2176 }
2060 2177
2061 /* png_create_colormap_entry just takes an RGBA and writes the 2178 /* png_create_colormap_entry just takes an RGBA and writes the
2062 * corresponding color-map entry using the format from 'image', 2179 * corresponding color-map entry using the format from 'image',
2063 * including the required conversion to sRGB or linear as 2180 * including the required conversion to sRGB or linear as
2064 * appropriate. The input values are always either sRGB (if the 2181 * appropriate. The input values are always either sRGB (if the
2065 * gamma correction flag is 0) or 0..255 scaled file encoded values 2182 * gamma correction flag is 0) or 0..255 scaled file encoded values
2066 * (if the function must gamma correct them). 2183 * (if the function must gamma correct them).
2067 */ 2184 */
2068 for (i=val=0; i<cmap_entries; ++i, val += step) 2185 for (i=val=0; i<cmap_entries; ++i, val += step)
2069 { 2186 {
2070 /* 'i' is a file value. While this will result in duplicated 2187 /* 'i' is a file value. While this will result in duplicated
2071 * entries for 8-bit non-sRGB encoded files it is necessary to 2188 * entries for 8-bit non-sRGB encoded files it is necessary to
2072 * have non-gamma corrected values to do tRNS handling. 2189 * have non-gamma corrected values to do tRNS handling.
2073 */ 2190 */
2074 if (i != trans) 2191 if (i != trans)
2075 png_create_colormap_entry(display, i, val, val, val, 255, 2192 png_create_colormap_entry(display, i, val, val, val, 255,
2076 E_FILE/*8-bit with file gamma*/); 2193 P_FILE/*8-bit with file gamma*/);
2077 2194
2078 /* Else this entry is transparent. The colors don't matter if 2195 /* Else this entry is transparent. The colors don't matter if
2079 * there is an alpha channel (back_alpha == 0), but it does no 2196 * there is an alpha channel (back_alpha == 0), but it does no
2080 * harm to pass them in; the values are not set above so this 2197 * harm to pass them in; the values are not set above so this
2081 * passes in white. 2198 * passes in white.
2082 * 2199 *
2083 * NOTE: this preserves the full precision of the application 2200 * NOTE: this preserves the full precision of the application
2084 * supplied background color when it is used. 2201 * supplied background color when it is used.
2085 */ 2202 */
2086 else 2203 else
2087 png_create_colormap_entry(display, i, back_r, back_g, back_b, 2204 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2088 back_alpha, output_encoding); 2205 back_alpha, output_encoding);
2089 } 2206 }
2090 2207
2091 /* We need libpng to preserve the original encoding. */ 2208 /* We need libpng to preserve the original encoding. */
2092 data_encoding = E_FILE; 2209 data_encoding = P_FILE;
2093 2210
2094 /* The rows from libpng, while technically gray values, are now also 2211 /* The rows from libpng, while technically gray values, are now also
2095 * color-map indicies; however, they may need to be expanded to 1 2212 * color-map indices; however, they may need to be expanded to 1
2096 * byte per pixel. This is what png_set_packing does (i.e., it 2213 * byte per pixel. This is what png_set_packing does (i.e., it
2097 * unpacks the bit values into bytes.) 2214 * unpacks the bit values into bytes.)
2098 */ 2215 */
2099 if (png_ptr->bit_depth < 8) 2216 if (png_ptr->bit_depth < 8)
2100 png_set_packing(png_ptr); 2217 png_set_packing(png_ptr);
2101 } 2218 }
2102 2219
2103 else /* bit depth is 16 */ 2220 else /* bit depth is 16 */
2104 { 2221 {
2105 /* The 16-bit input values can be converted directly to 8-bit gamma 2222 /* The 16-bit input values can be converted directly to 8-bit gamma
2106 * encoded values; however, if a tRNS chunk is present 257 color-map 2223 * encoded values; however, if a tRNS chunk is present 257 color-map
2107 * entries are required. This means that the extra entry requires 2224 * entries are required. This means that the extra entry requires
2108 * special processing; add an alpha channel, sacrifice gray level 2225 * special processing; add an alpha channel, sacrifice gray level
2109 * 254 and convert transparent (alpha==0) entries to that. 2226 * 254 and convert transparent (alpha==0) entries to that.
2110 * 2227 *
2111 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the 2228 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2112 * same time to minimize quality loss. If a tRNS chunk is present 2229 * same time to minimize quality loss. If a tRNS chunk is present
2113 * this means libpng must handle it too; otherwise it is impossible 2230 * this means libpng must handle it too; otherwise it is impossible
2114 * to do the exact match on the 16-bit value. 2231 * to do the exact match on the 16-bit value.
2115 * 2232 *
2116 * If the output has no alpha channel *and* the background color is 2233 * If the output has no alpha channel *and* the background color is
2117 * gray then it is possible to let libpng handle the substitution by 2234 * gray then it is possible to let libpng handle the substitution by
2118 * ensuring that the corresponding gray level matches the background 2235 * ensuring that the corresponding gray level matches the background
2119 * color exactly. 2236 * color exactly.
2120 */ 2237 */
2121 data_encoding = E_sRGB; 2238 data_encoding = P_sRGB;
2122 2239
2123 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) 2240 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2124 png_error(png_ptr, "gray[16] color-map: too few entries"); 2241 png_error(png_ptr, "gray[16] color-map: too few entries");
2125 2242
2126 cmap_entries = make_gray_colormap(display); 2243 cmap_entries = make_gray_colormap(display);
2127 2244
2128 if (png_ptr->num_trans > 0) 2245 if (png_ptr->num_trans > 0)
2129 { 2246 {
2130 unsigned int back_alpha; 2247 unsigned int back_alpha;
2131 2248
2132 if (output_format & PNG_FORMAT_FLAG_ALPHA) 2249 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2133 back_alpha = 0; 2250 back_alpha = 0;
2134 2251
2135 else 2252 else
2136 { 2253 {
2137 if (back_r == back_g && back_g == back_b) 2254 if (back_r == back_g && back_g == back_b)
2138 { 2255 {
2139 /* Background is gray; no special processing will be 2256 /* Background is gray; no special processing will be
2140 * required. 2257 * required.
2141 */ 2258 */
2142 png_color_16 c; 2259 png_color_16 c;
2143 png_uint_32 gray = back_g; 2260 png_uint_32 gray = back_g;
2144 2261
2145 if (output_encoding == E_LINEAR) 2262 if (output_encoding == P_LINEAR)
2146 { 2263 {
2147 gray = PNG_sRGB_FROM_LINEAR(gray * 255); 2264 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2148 2265
2149 /* And make sure the corresponding palette entry 2266 /* And make sure the corresponding palette entry
2150 * matches. 2267 * matches.
2151 */ 2268 */
2152 png_create_colormap_entry(display, gray, back_g, back_g, 2269 png_create_colormap_entry(display, gray, back_g, back_g,
2153 back_g, 65535, E_LINEAR); 2270 back_g, 65535, P_LINEAR);
2154 } 2271 }
2155 2272
2156 /* The background passed to libpng, however, must be the 2273 /* The background passed to libpng, however, must be the
2157 * sRGB value. 2274 * sRGB value.
2158 */ 2275 */
2159 c.index = 0; /*unused*/ 2276 c.index = 0; /*unused*/
2160 c.gray = c.red = c.green = c.blue = (png_uint_16)gray; 2277 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2161 2278
2162 /* NOTE: does this work without expanding tRNS to alpha? 2279 /* NOTE: does this work without expanding tRNS to alpha?
2163 * It should be the color->gray case below apparently 2280 * It should be the color->gray case below apparently
2164 * doesn't. 2281 * doesn't.
2165 */ 2282 */
2166 png_set_background_fixed(png_ptr, &c, 2283 png_set_background_fixed(png_ptr, &c,
2167 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, 2284 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2168 0/*gamma: not used*/); 2285 0/*gamma: not used*/);
2169 2286
2170 output_processing = PNG_CMAP_NONE; 2287 output_processing = PNG_CMAP_NONE;
2171 break; 2288 break;
2172 } 2289 }
2173 2290 #ifdef __COVERITY__
2174 back_alpha = output_encoding == E_LINEAR ? 65535 : 255; 2291 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2292 * here.
2293 */
2294 back_alpha = 255;
2295 #else
2296 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2297 #endif
2175 } 2298 }
2176 2299
2177 /* output_processing means that the libpng-processed row will be 2300 /* output_processing means that the libpng-processed row will be
2178 * 8-bit GA and it has to be processing to single byte color-map 2301 * 8-bit GA and it has to be processing to single byte color-map
2179 * values. Entry 254 is replaced by either a completely 2302 * values. Entry 254 is replaced by either a completely
2180 * transparent entry or by the background color at full 2303 * transparent entry or by the background color at full
2181 * precision (and the background color is not a simple gray leve 2304 * precision (and the background color is not a simple gray
2182 * in this case.) 2305 * level in this case.)
2183 */ 2306 */
2184 expand_tRNS = 1; 2307 expand_tRNS = 1;
2185 output_processing = PNG_CMAP_TRANS; 2308 output_processing = PNG_CMAP_TRANS;
2186 background_index = 254; 2309 background_index = 254;
2187 2310
2188 /* And set (overwrite) color-map entry 254 to the actual 2311 /* And set (overwrite) color-map entry 254 to the actual
2189 * background color at full precision. 2312 * background color at full precision.
2190 */ 2313 */
2191 png_create_colormap_entry(display, 254, back_r, back_g, back_b, 2314 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2192 back_alpha, output_encoding); 2315 back_alpha, output_encoding);
2193 } 2316 }
2194 2317
2195 else 2318 else
2196 output_processing = PNG_CMAP_NONE; 2319 output_processing = PNG_CMAP_NONE;
2197 } 2320 }
2198 break; 2321 break;
2199 2322
2200 case PNG_COLOR_TYPE_GRAY_ALPHA: 2323 case PNG_COLOR_TYPE_GRAY_ALPHA:
2201 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum 2324 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2202 * of 65536 combinations. If, however, the alpha channel is to be 2325 * of 65536 combinations. If, however, the alpha channel is to be
2203 * removed there are only 256 possibilities if the background is gray. 2326 * removed there are only 256 possibilities if the background is gray.
2204 * (Otherwise there is a subset of the 65536 possibilities defined by 2327 * (Otherwise there is a subset of the 65536 possibilities defined by
2205 * the triangle between black, white and the background color.) 2328 * the triangle between black, white and the background color.)
2206 * 2329 *
2207 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to 2330 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2208 * worry about tRNS matching - tRNS is ignored if there is an alpha 2331 * worry about tRNS matching - tRNS is ignored if there is an alpha
2209 * channel. 2332 * channel.
2210 */ 2333 */
2211 data_encoding = E_sRGB; 2334 data_encoding = P_sRGB;
2212 2335
2213 if (output_format & PNG_FORMAT_FLAG_ALPHA) 2336 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2214 { 2337 {
2215 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) 2338 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2216 png_error(png_ptr, "gray+alpha color-map: too few entries"); 2339 png_error(png_ptr, "gray+alpha color-map: too few entries");
2217 2340
2218 cmap_entries = make_ga_colormap(display); 2341 cmap_entries = make_ga_colormap(display);
2219 2342
2220 background_index = PNG_CMAP_GA_BACKGROUND; 2343 background_index = PNG_CMAP_GA_BACKGROUND;
2221 output_processing = PNG_CMAP_GA; 2344 output_processing = PNG_CMAP_GA;
2222 } 2345 }
2223 2346
(...skipping 20 matching lines...) Expand all
2244 { 2367 {
2245 /* Background is gray; no special processing will be required. */ 2368 /* Background is gray; no special processing will be required. */
2246 png_color_16 c; 2369 png_color_16 c;
2247 png_uint_32 gray = back_g; 2370 png_uint_32 gray = back_g;
2248 2371
2249 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) 2372 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2250 png_error(png_ptr, "gray-alpha color-map: too few entries"); 2373 png_error(png_ptr, "gray-alpha color-map: too few entries");
2251 2374
2252 cmap_entries = make_gray_colormap(display); 2375 cmap_entries = make_gray_colormap(display);
2253 2376
2254 if (output_encoding == E_LINEAR) 2377 if (output_encoding == P_LINEAR)
2255 { 2378 {
2256 gray = PNG_sRGB_FROM_LINEAR(gray * 255); 2379 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2257 2380
2258 /* And make sure the corresponding palette entry matches. */ 2381 /* And make sure the corresponding palette entry matches. */
2259 png_create_colormap_entry(display, gray, back_g, back_g, 2382 png_create_colormap_entry(display, gray, back_g, back_g,
2260 back_g, 65535, E_LINEAR); 2383 back_g, 65535, P_LINEAR);
2261 } 2384 }
2262 2385
2263 /* The background passed to libpng, however, must be the sRGB 2386 /* The background passed to libpng, however, must be the sRGB
2264 * value. 2387 * value.
2265 */ 2388 */
2266 c.index = 0; /*unused*/ 2389 c.index = 0; /*unused*/
2267 c.gray = c.red = c.green = c.blue = (png_uint_16)gray; 2390 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2268 2391
2269 png_set_background_fixed(png_ptr, &c, 2392 png_set_background_fixed(png_ptr, &c,
2270 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, 2393 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
(...skipping 10 matching lines...) Expand all
2281 * the entries are all opaque. 2404 * the entries are all opaque.
2282 */ 2405 */
2283 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) 2406 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2284 png_error(png_ptr, "ga-alpha color-map: too few entries"); 2407 png_error(png_ptr, "ga-alpha color-map: too few entries");
2285 2408
2286 i = 0; 2409 i = 0;
2287 while (i < 231) 2410 while (i < 231)
2288 { 2411 {
2289 png_uint_32 gray = (i * 256 + 115) / 231; 2412 png_uint_32 gray = (i * 256 + 115) / 231;
2290 png_create_colormap_entry(display, i++, gray, gray, gray, 2413 png_create_colormap_entry(display, i++, gray, gray, gray,
2291 255, E_sRGB); 2414 255, P_sRGB);
2292 } 2415 }
2293 2416
2294 /* NOTE: this preserves the full precision of the application 2417 /* NOTE: this preserves the full precision of the application
2295 * background color. 2418 * background color.
2296 */ 2419 */
2297 background_index = i; 2420 background_index = i;
2298 png_create_colormap_entry(display, i++, back_r, back_g, back_b, 2421 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2299 output_encoding == E_LINEAR ? 65535U : 255U, output_encoding); 2422 #ifdef __COVERITY__
2423 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2424 * here.
2425 */ 255U,
2426 #else
2427 output_encoding == P_LINEAR ? 65535U : 255U,
2428 #endif
2429 output_encoding);
2300 2430
2301 /* For non-opaque input composite on the sRGB background - this 2431 /* For non-opaque input composite on the sRGB background - this
2302 * requires inverting the encoding for each component. The input 2432 * requires inverting the encoding for each component. The input
2303 * is still converted to the sRGB encoding because this is a 2433 * is still converted to the sRGB encoding because this is a
2304 * reasonable approximate to the logarithmic curve of human 2434 * reasonable approximate to the logarithmic curve of human
2305 * visual sensitivity, at least over the narrow range which PNG 2435 * visual sensitivity, at least over the narrow range which PNG
2306 * represents. Consequently 'G' is always sRGB encoded, while 2436 * represents. Consequently 'G' is always sRGB encoded, while
2307 * 'A' is linear. We need the linear background colors. 2437 * 'A' is linear. We need the linear background colors.
2308 */ 2438 */
2309 if (output_encoding == E_sRGB) /* else already linear */ 2439 if (output_encoding == P_sRGB) /* else already linear */
2310 { 2440 {
2311 /* This may produce a value not exactly matching the 2441 /* This may produce a value not exactly matching the
2312 * background, but that's ok because these numbers are only 2442 * background, but that's ok because these numbers are only
2313 * used when alpha != 0 2443 * used when alpha != 0
2314 */ 2444 */
2315 back_r = png_sRGB_table[back_r]; 2445 back_r = png_sRGB_table[back_r];
2316 back_g = png_sRGB_table[back_g]; 2446 back_g = png_sRGB_table[back_g];
2317 back_b = png_sRGB_table[back_b]; 2447 back_b = png_sRGB_table[back_b];
2318 } 2448 }
2319 2449
2320 for (a=1; a<5; ++a) 2450 for (a=1; a<5; ++a)
2321 { 2451 {
2322 unsigned int g; 2452 unsigned int g;
2323 2453
2324 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled 2454 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2325 * by an 8-bit alpha value (0..255). 2455 * by an 8-bit alpha value (0..255).
2326 */ 2456 */
2327 png_uint_32 alpha = 51 * a; 2457 png_uint_32 alpha = 51 * a;
2328 png_uint_32 back_rx = (255-alpha) * back_r; 2458 png_uint_32 back_rx = (255-alpha) * back_r;
2329 png_uint_32 back_gx = (255-alpha) * back_g; 2459 png_uint_32 back_gx = (255-alpha) * back_g;
2330 png_uint_32 back_bx = (255-alpha) * back_b; 2460 png_uint_32 back_bx = (255-alpha) * back_b;
2331 2461
2332 for (g=0; g<6; ++g) 2462 for (g=0; g<6; ++g)
2333 { 2463 {
2334 png_uint_32 gray = png_sRGB_table[g*51] * alpha; 2464 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2335 2465
2336 png_create_colormap_entry(display, i++, 2466 png_create_colormap_entry(display, i++,
2337 PNG_sRGB_FROM_LINEAR(gray + back_rx), 2467 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2338 PNG_sRGB_FROM_LINEAR(gray + back_gx), 2468 PNG_sRGB_FROM_LINEAR(gray + back_gx),
2339 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB); 2469 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
2340 } 2470 }
2341 } 2471 }
2342 2472
2343 cmap_entries = i; 2473 cmap_entries = i;
2344 output_processing = PNG_CMAP_GA; 2474 output_processing = PNG_CMAP_GA;
2345 } 2475 }
2346 } 2476 }
2347 break; 2477 break;
2348 2478
2349 case PNG_COLOR_TYPE_RGB: 2479 case PNG_COLOR_TYPE_RGB:
2350 case PNG_COLOR_TYPE_RGB_ALPHA: 2480 case PNG_COLOR_TYPE_RGB_ALPHA:
2351 /* Exclude the case where the output is gray; we can always handle this 2481 /* Exclude the case where the output is gray; we can always handle this
2352 * with the cases above. 2482 * with the cases above.
2353 */ 2483 */
2354 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) 2484 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2355 { 2485 {
2356 /* The color-map will be grayscale, so we may as well convert the 2486 /* The color-map will be grayscale, so we may as well convert the
2357 * input RGB values to a simple grayscale and use the grayscale 2487 * input RGB values to a simple grayscale and use the grayscale
2358 * code above. 2488 * code above.
2359 * 2489 *
2360 * NOTE: calling this apparently damages the recognition of the 2490 * NOTE: calling this apparently damages the recognition of the
2361 * transparent color in background color handling; call 2491 * transparent color in background color handling; call
2362 * png_set_tRNS_to_alpha before png_set_background_fixed. 2492 * png_set_tRNS_to_alpha before png_set_background_fixed.
2363 */ 2493 */
2364 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, 2494 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2365 -1); 2495 -1);
2366 data_encoding = E_sRGB; 2496 data_encoding = P_sRGB;
2367 2497
2368 /* The output will now be one or two 8-bit gray or gray+alpha 2498 /* The output will now be one or two 8-bit gray or gray+alpha
2369 * channels. The more complex case arises when the input has alpha. 2499 * channels. The more complex case arises when the input has alpha.
2370 */ 2500 */
2371 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || 2501 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2372 png_ptr->num_trans > 0) && 2502 png_ptr->num_trans > 0) &&
2373 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) 2503 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2374 { 2504 {
2375 /* Both input and output have an alpha channel, so no background 2505 /* Both input and output have an alpha channel, so no background
2376 * processing is required; just map the GA bytes to the right 2506 * processing is required; just map the GA bytes to the right
(...skipping 21 matching lines...) Expand all
2398 /* Ideally this code would use libpng to do the gamma correction, 2528 /* Ideally this code would use libpng to do the gamma correction,
2399 * but if an input alpha channel is to be removed we will hit the 2529 * but if an input alpha channel is to be removed we will hit the
2400 * libpng bug in gamma+compose+rgb-to-gray (the double gamma 2530 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2401 * correction bug). Fix this by dropping the gamma correction in 2531 * correction bug). Fix this by dropping the gamma correction in
2402 * this case and doing it in the palette; this will result in 2532 * this case and doing it in the palette; this will result in
2403 * duplicate palette entries, but that's better than the 2533 * duplicate palette entries, but that's better than the
2404 * alternative of double gamma correction. 2534 * alternative of double gamma correction.
2405 */ 2535 */
2406 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || 2536 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2407 png_ptr->num_trans > 0) && 2537 png_ptr->num_trans > 0) &&
2408 png_gamma_not_sRGB(png_ptr->colorspace.gamma)) 2538 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
2409 { 2539 {
2410 cmap_entries = make_gray_file_colormap(display); 2540 cmap_entries = make_gray_file_colormap(display);
2411 data_encoding = E_FILE; 2541 data_encoding = P_FILE;
2412 } 2542 }
2413 2543
2414 else 2544 else
2415 cmap_entries = make_gray_colormap(display); 2545 cmap_entries = make_gray_colormap(display);
2416 2546
2417 /* But if the input has alpha or transparency it must be removed 2547 /* But if the input has alpha or transparency it must be removed
2418 */ 2548 */
2419 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || 2549 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2420 png_ptr->num_trans > 0) 2550 png_ptr->num_trans > 0)
2421 { 2551 {
2422 png_color_16 c; 2552 png_color_16 c;
2423 png_uint_32 gray = back_g; 2553 png_uint_32 gray = back_g;
2424 2554
2425 /* We need to ensure that the application background exists in 2555 /* We need to ensure that the application background exists in
2426 * the colormap and that completely transparent pixels map to 2556 * the colormap and that completely transparent pixels map to
2427 * it. Achieve this simply by ensuring that the entry 2557 * it. Achieve this simply by ensuring that the entry
2428 * selected for the background really is the background color. 2558 * selected for the background really is the background color.
2429 */ 2559 */
2430 if (data_encoding == E_FILE) /* from the fixup above */ 2560 if (data_encoding == P_FILE) /* from the fixup above */
2431 { 2561 {
2432 /* The app supplied a gray which is in output_encoding, we 2562 /* The app supplied a gray which is in output_encoding, we
2433 * need to convert it to a value of the input (E_FILE) 2563 * need to convert it to a value of the input (P_FILE)
2434 * encoding then set this palette entry to the required 2564 * encoding then set this palette entry to the required
2435 * output encoding. 2565 * output encoding.
2436 */ 2566 */
2437 if (output_encoding == E_sRGB) 2567 if (output_encoding == P_sRGB)
2438 gray = png_sRGB_table[gray]; /* now E_LINEAR */ 2568 gray = png_sRGB_table[gray]; /* now P_LINEAR */
2439 2569
2440 gray = PNG_DIV257(png_gamma_16bit_correct(gray, 2570 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
2441 png_ptr->colorspace.gamma)); /* now E_FILE */ 2571 png_ptr->colorspace.gamma)); /* now P_FILE */
2442 2572
2443 /* And make sure the corresponding palette entry contains 2573 /* And make sure the corresponding palette entry contains
2444 * exactly the required sRGB value. 2574 * exactly the required sRGB value.
2445 */ 2575 */
2446 png_create_colormap_entry(display, gray, back_g, back_g, 2576 png_create_colormap_entry(display, gray, back_g, back_g,
2447 back_g, 0/*unused*/, output_encoding); 2577 back_g, 0/*unused*/, output_encoding);
2448 } 2578 }
2449 2579
2450 else if (output_encoding == E_LINEAR) 2580 else if (output_encoding == P_LINEAR)
2451 { 2581 {
2452 gray = PNG_sRGB_FROM_LINEAR(gray * 255); 2582 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2453 2583
2454 /* And make sure the corresponding palette entry matches. 2584 /* And make sure the corresponding palette entry matches.
2455 */ 2585 */
2456 png_create_colormap_entry(display, gray, back_g, back_g, 2586 png_create_colormap_entry(display, gray, back_g, back_g,
2457 back_g, 0/*unused*/, E_LINEAR); 2587 back_g, 0/*unused*/, P_LINEAR);
2458 } 2588 }
2459 2589
2460 /* The background passed to libpng, however, must be the 2590 /* The background passed to libpng, however, must be the
2461 * output (normally sRGB) value. 2591 * output (normally sRGB) value.
2462 */ 2592 */
2463 c.index = 0; /*unused*/ 2593 c.index = 0; /*unused*/
2464 c.gray = c.red = c.green = c.blue = (png_uint_16)gray; 2594 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2465 2595
2466 /* NOTE: the following is apparently a bug in libpng. Without 2596 /* NOTE: the following is apparently a bug in libpng. Without
2467 * it the transparent color recognition in 2597 * it the transparent color recognition in
2468 * png_set_background_fixed seems to go wrong. 2598 * png_set_background_fixed seems to go wrong.
2469 */ 2599 */
2470 expand_tRNS = 1; 2600 expand_tRNS = 1;
2471 png_set_background_fixed(png_ptr, &c, 2601 png_set_background_fixed(png_ptr, &c,
2472 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, 2602 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2473 0/*gamma: not used*/); 2603 0/*gamma: not used*/);
2474 } 2604 }
2475 2605
2476 output_processing = PNG_CMAP_NONE; 2606 output_processing = PNG_CMAP_NONE;
2477 } 2607 }
2478 } 2608 }
2479 2609
2480 else /* output is color */ 2610 else /* output is color */
2481 { 2611 {
2482 /* We could use png_quantize here so long as there is no transparent 2612 /* We could use png_quantize here so long as there is no transparent
2483 * color or alpha; png_quantize ignores alpha. Easier overall just 2613 * color or alpha; png_quantize ignores alpha. Easier overall just
2484 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. 2614 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2485 * Consequently we always want libpng to produce sRGB data. 2615 * Consequently we always want libpng to produce sRGB data.
2486 */ 2616 */
2487 data_encoding = E_sRGB; 2617 data_encoding = P_sRGB;
2488 2618
2489 /* Is there any transparency or alpha? */ 2619 /* Is there any transparency or alpha? */
2490 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || 2620 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2491 png_ptr->num_trans > 0) 2621 png_ptr->num_trans > 0)
2492 { 2622 {
2493 /* Is there alpha in the output too? If so all four channels are 2623 /* Is there alpha in the output too? If so all four channels are
2494 * processed into a special RGB cube with alpha support. 2624 * processed into a special RGB cube with alpha support.
2495 */ 2625 */
2496 if (output_format & PNG_FORMAT_FLAG_ALPHA) 2626 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2497 { 2627 {
2498 png_uint_32 r; 2628 png_uint_32 r;
2499 2629
2500 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) 2630 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2501 png_error(png_ptr, "rgb+alpha color-map: too few entries"); 2631 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2502 2632
2503 cmap_entries = make_rgb_colormap(display); 2633 cmap_entries = make_rgb_colormap(display);
2504 2634
2505 /* Add a transparent entry. */ 2635 /* Add a transparent entry. */
2506 png_create_colormap_entry(display, cmap_entries, 255, 255, 2636 png_create_colormap_entry(display, cmap_entries, 255, 255,
2507 255, 0, E_sRGB); 2637 255, 0, P_sRGB);
2508 2638
2509 /* This is stored as the background index for the processing 2639 /* This is stored as the background index for the processing
2510 * algorithm. 2640 * algorithm.
2511 */ 2641 */
2512 background_index = cmap_entries++; 2642 background_index = cmap_entries++;
2513 2643
2514 /* Add 27 r,g,b entries each with alpha 0.5. */ 2644 /* Add 27 r,g,b entries each with alpha 0.5. */
2515 for (r=0; r<256; r = (r << 1) | 0x7f) 2645 for (r=0; r<256; r = (r << 1) | 0x7f)
2516 { 2646 {
2517 png_uint_32 g; 2647 png_uint_32 g;
2518 2648
2519 for (g=0; g<256; g = (g << 1) | 0x7f) 2649 for (g=0; g<256; g = (g << 1) | 0x7f)
2520 { 2650 {
2521 png_uint_32 b; 2651 png_uint_32 b;
2522 2652
2523 /* This generates components with the values 0, 127 and 2653 /* This generates components with the values 0, 127 and
2524 * 255 2654 * 255
2525 */ 2655 */
2526 for (b=0; b<256; b = (b << 1) | 0x7f) 2656 for (b=0; b<256; b = (b << 1) | 0x7f)
2527 png_create_colormap_entry(display, cmap_entries++, 2657 png_create_colormap_entry(display, cmap_entries++,
2528 r, g, b, 128, E_sRGB); 2658 r, g, b, 128, P_sRGB);
2529 } 2659 }
2530 } 2660 }
2531 2661
2532 expand_tRNS = 1; 2662 expand_tRNS = 1;
2533 output_processing = PNG_CMAP_RGB_ALPHA; 2663 output_processing = PNG_CMAP_RGB_ALPHA;
2534 } 2664 }
2535 2665
2536 else 2666 else
2537 { 2667 {
2538 /* Alpha/transparency must be removed. The background must 2668 /* Alpha/transparency must be removed. The background must
2539 * exist in the color map (achieved by setting adding it after 2669 * exist in the color map (achieved by setting adding it after
2540 * the 666 color-map). If the standard processing code will 2670 * the 666 color-map). If the standard processing code will
2541 * pick up this entry automatically that's all that is 2671 * pick up this entry automatically that's all that is
2542 * required; libpng can be called to do the background 2672 * required; libpng can be called to do the background
2543 * processing. 2673 * processing.
2544 */ 2674 */
2545 unsigned int sample_size = 2675 unsigned int sample_size =
2546 PNG_IMAGE_SAMPLE_SIZE(output_format); 2676 PNG_IMAGE_SAMPLE_SIZE(output_format);
2547 png_uint_32 r, g, b; /* sRGB background */ 2677 png_uint_32 r, g, b; /* sRGB background */
2548 2678
2549 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) 2679 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2550 png_error(png_ptr, "rgb-alpha color-map: too few entries"); 2680 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2551 2681
2552 cmap_entries = make_rgb_colormap(display); 2682 cmap_entries = make_rgb_colormap(display);
2553 2683
2554 png_create_colormap_entry(display, cmap_entries, back_r, 2684 png_create_colormap_entry(display, cmap_entries, back_r,
2555 back_g, back_b, 0/*unused*/, output_encoding); 2685 back_g, back_b, 0/*unused*/, output_encoding);
2556 2686
2557 if (output_encoding == E_LINEAR) 2687 if (output_encoding == P_LINEAR)
2558 { 2688 {
2559 r = PNG_sRGB_FROM_LINEAR(back_r * 255); 2689 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2560 g = PNG_sRGB_FROM_LINEAR(back_g * 255); 2690 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2561 b = PNG_sRGB_FROM_LINEAR(back_b * 255); 2691 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2562 } 2692 }
2563 2693
2564 else 2694 else
2565 { 2695 {
2566 r = back_r; 2696 r = back_r;
2567 g = back_g; 2697 g = back_g;
(...skipping 19 matching lines...) Expand all
2587 */ 2717 */
2588 for (r=0; r<256; r = (r << 1) | 0x7f) 2718 for (r=0; r<256; r = (r << 1) | 0x7f)
2589 { 2719 {
2590 for (g=0; g<256; g = (g << 1) | 0x7f) 2720 for (g=0; g<256; g = (g << 1) | 0x7f)
2591 { 2721 {
2592 /* This generates components with the values 0, 127 2722 /* This generates components with the values 0, 127
2593 * and 255 2723 * and 255
2594 */ 2724 */
2595 for (b=0; b<256; b = (b << 1) | 0x7f) 2725 for (b=0; b<256; b = (b << 1) | 0x7f)
2596 png_create_colormap_entry(display, cmap_entries++, 2726 png_create_colormap_entry(display, cmap_entries++,
2597 png_colormap_compose(display, r, E_sRGB, 128, 2727 png_colormap_compose(display, r, P_sRGB, 128,
2598 back_r, output_encoding), 2728 back_r, output_encoding),
2599 png_colormap_compose(display, g, E_sRGB, 128, 2729 png_colormap_compose(display, g, P_sRGB, 128,
2600 back_g, output_encoding), 2730 back_g, output_encoding),
2601 png_colormap_compose(display, b, E_sRGB, 128, 2731 png_colormap_compose(display, b, P_sRGB, 128,
2602 back_b, output_encoding), 2732 back_b, output_encoding),
2603 0/*unused*/, output_encoding); 2733 0/*unused*/, output_encoding);
2604 } 2734 }
2605 } 2735 }
2606 2736
2607 expand_tRNS = 1; 2737 expand_tRNS = 1;
2608 output_processing = PNG_CMAP_RGB_ALPHA; 2738 output_processing = PNG_CMAP_RGB_ALPHA;
2609 } 2739 }
2610 2740
2611 else /* background color is in the standard color-map */ 2741 else /* background color is in the standard color-map */
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2650 png_const_colorp colormap = png_ptr->palette; 2780 png_const_colorp colormap = png_ptr->palette;
2651 const int do_background = trans != NULL && 2781 const int do_background = trans != NULL &&
2652 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; 2782 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2653 unsigned int i; 2783 unsigned int i;
2654 2784
2655 /* Just in case: */ 2785 /* Just in case: */
2656 if (trans == NULL) 2786 if (trans == NULL)
2657 num_trans = 0; 2787 num_trans = 0;
2658 2788
2659 output_processing = PNG_CMAP_NONE; 2789 output_processing = PNG_CMAP_NONE;
2660 data_encoding = E_FILE; /* Don't change from color-map indicies */ 2790 data_encoding = P_FILE; /* Don't change from color-map indices */
2661 cmap_entries = png_ptr->num_palette; 2791 cmap_entries = png_ptr->num_palette;
2662 if (cmap_entries > 256) 2792 if (cmap_entries > 256)
2663 cmap_entries = 256; 2793 cmap_entries = 256;
2664 2794
2665 if (cmap_entries > image->colormap_entries) 2795 if (cmap_entries > image->colormap_entries)
2666 png_error(png_ptr, "palette color-map: too few entries"); 2796 png_error(png_ptr, "palette color-map: too few entries");
2667 2797
2668 for (i=0; i < cmap_entries; ++i) 2798 for (i=0; i < cmap_entries; ++i)
2669 { 2799 {
2670 if (do_background && i < num_trans && trans[i] < 255) 2800 if (do_background != 0 && i < num_trans && trans[i] < 255)
2671 { 2801 {
2672 if (trans[i] == 0) 2802 if (trans[i] == 0)
2673 png_create_colormap_entry(display, i, back_r, back_g, 2803 png_create_colormap_entry(display, i, back_r, back_g,
2674 back_b, 0, output_encoding); 2804 back_b, 0, output_encoding);
2675 2805
2676 else 2806 else
2677 { 2807 {
2678 /* Must compose the PNG file color in the color-map entry 2808 /* Must compose the PNG file color in the color-map entry
2679 * on the sRGB color in 'back'. 2809 * on the sRGB color in 'back'.
2680 */ 2810 */
2681 png_create_colormap_entry(display, i, 2811 png_create_colormap_entry(display, i,
2682 png_colormap_compose(display, colormap[i].red, E_FILE, 2812 png_colormap_compose(display, colormap[i].red, P_FILE,
2683 trans[i], back_r, output_encoding), 2813 trans[i], back_r, output_encoding),
2684 png_colormap_compose(display, colormap[i].green, E_FILE, 2814 png_colormap_compose(display, colormap[i].green, P_FILE,
2685 trans[i], back_g, output_encoding), 2815 trans[i], back_g, output_encoding),
2686 png_colormap_compose(display, colormap[i].blue, E_FILE, 2816 png_colormap_compose(display, colormap[i].blue, P_FILE,
2687 trans[i], back_b, output_encoding), 2817 trans[i], back_b, output_encoding),
2688 output_encoding == E_LINEAR ? trans[i] * 257U : 2818 output_encoding == P_LINEAR ? trans[i] * 257U :
2689 trans[i], 2819 trans[i],
2690 output_encoding); 2820 output_encoding);
2691 } 2821 }
2692 } 2822 }
2693 2823
2694 else 2824 else
2695 png_create_colormap_entry(display, i, colormap[i].red, 2825 png_create_colormap_entry(display, i, colormap[i].red,
2696 colormap[i].green, colormap[i].blue, 2826 colormap[i].green, colormap[i].blue,
2697 i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/); 2827 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2698 } 2828 }
2699 2829
2700 /* The PNG data may have indicies packed in fewer than 8 bits, it 2830 /* The PNG data may have indices packed in fewer than 8 bits, it
2701 * must be expanded if so. 2831 * must be expanded if so.
2702 */ 2832 */
2703 if (png_ptr->bit_depth < 8) 2833 if (png_ptr->bit_depth < 8)
2704 png_set_packing(png_ptr); 2834 png_set_packing(png_ptr);
2705 } 2835 }
2706 break; 2836 break;
2707 2837
2708 default: 2838 default:
2709 png_error(png_ptr, "invalid PNG color type"); 2839 png_error(png_ptr, "invalid PNG color type");
2710 /*NOT REACHED*/ 2840 /*NOT REACHED*/
2711 break;
2712 } 2841 }
2713 2842
2714 /* Now deal with the output processing */ 2843 /* Now deal with the output processing */
2715 if (expand_tRNS && png_ptr->num_trans > 0 && 2844 if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2716 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) 2845 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2717 png_set_tRNS_to_alpha(png_ptr); 2846 png_set_tRNS_to_alpha(png_ptr);
2718 2847
2719 switch (data_encoding) 2848 switch (data_encoding)
2720 { 2849 {
2721 default: 2850 case P_sRGB:
2722 png_error(png_ptr, "bad data option (internal error)");
2723 break;
2724
2725 case E_sRGB:
2726 /* Change to 8-bit sRGB */ 2851 /* Change to 8-bit sRGB */
2727 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); 2852 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2728 /* FALL THROUGH */ 2853 /* FALL THROUGH */
2729 2854
2730 case E_FILE: 2855 case P_FILE:
2731 if (png_ptr->bit_depth > 8) 2856 if (png_ptr->bit_depth > 8)
2732 png_set_scale_16(png_ptr); 2857 png_set_scale_16(png_ptr);
2733 break; 2858 break;
2859
2860 #ifdef __GNUC__
2861 default:
2862 png_error(png_ptr, "bad data option (internal error)");
2863 #endif
2734 } 2864 }
2735 2865
2736 if (cmap_entries > 256 || cmap_entries > image->colormap_entries) 2866 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2737 png_error(png_ptr, "color map overflow (BAD internal error)"); 2867 png_error(png_ptr, "color map overflow (BAD internal error)");
2738 2868
2739 image->colormap_entries = cmap_entries; 2869 image->colormap_entries = cmap_entries;
2740 2870
2741 /* Double check using the recorded background index */ 2871 /* Double check using the recorded background index */
2742 switch (output_processing) 2872 switch (output_processing)
2743 { 2873 {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2797 { 2927 {
2798 case PNG_INTERLACE_NONE: 2928 case PNG_INTERLACE_NONE:
2799 passes = 1; 2929 passes = 1;
2800 break; 2930 break;
2801 2931
2802 case PNG_INTERLACE_ADAM7: 2932 case PNG_INTERLACE_ADAM7:
2803 passes = PNG_INTERLACE_ADAM7_PASSES; 2933 passes = PNG_INTERLACE_ADAM7_PASSES;
2804 break; 2934 break;
2805 2935
2806 default: 2936 default:
2807 passes = 0;
2808 png_error(png_ptr, "unknown interlace type"); 2937 png_error(png_ptr, "unknown interlace type");
2809 } 2938 }
2810 2939
2811 { 2940 {
2812 png_uint_32 height = image->height; 2941 png_uint_32 height = image->height;
2813 png_uint_32 width = image->width; 2942 png_uint_32 width = image->width;
2814 int proc = display->colormap_processing; 2943 int proc = display->colormap_processing;
2815 png_bytep first_row = png_voidcast(png_bytep, display->first_row); 2944 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2816 ptrdiff_t step_row = display->row_bytes; 2945 ptrdiff_t step_row = display->row_bytes;
2817 int pass; 2946 int pass;
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
3116 { 3245 {
3117 case PNG_INTERLACE_NONE: 3246 case PNG_INTERLACE_NONE:
3118 passes = 1; 3247 passes = 1;
3119 break; 3248 break;
3120 3249
3121 case PNG_INTERLACE_ADAM7: 3250 case PNG_INTERLACE_ADAM7:
3122 passes = PNG_INTERLACE_ADAM7_PASSES; 3251 passes = PNG_INTERLACE_ADAM7_PASSES;
3123 break; 3252 break;
3124 3253
3125 default: 3254 default:
3126 passes = 0;
3127 png_error(png_ptr, "unknown interlace type"); 3255 png_error(png_ptr, "unknown interlace type");
3128 } 3256 }
3129 3257
3130 { 3258 {
3131 png_uint_32 height = image->height; 3259 png_uint_32 height = image->height;
3132 png_uint_32 width = image->width; 3260 png_uint_32 width = image->width;
3133 ptrdiff_t step_row = display->row_bytes; 3261 ptrdiff_t step_row = display->row_bytes;
3134 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; 3262 unsigned int channels =
3263 (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
3135 int pass; 3264 int pass;
3136 3265
3137 for (pass = 0; pass < passes; ++pass) 3266 for (pass = 0; pass < passes; ++pass)
3138 { 3267 {
3139 unsigned int startx, stepx, stepy; 3268 unsigned int startx, stepx, stepy;
3140 png_uint_32 y; 3269 png_uint_32 y;
3141 3270
3142 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) 3271 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3143 { 3272 {
3144 /* The row may be empty for a short image: */ 3273 /* The row may be empty for a short image: */
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
3217 return 1; 3346 return 1;
3218 } 3347 }
3219 3348
3220 /* The do_local_background case; called when all the following transforms are to 3349 /* The do_local_background case; called when all the following transforms are to
3221 * be done: 3350 * be done:
3222 * 3351 *
3223 * PNG_RGB_TO_GRAY 3352 * PNG_RGB_TO_GRAY
3224 * PNG_COMPOSITE 3353 * PNG_COMPOSITE
3225 * PNG_GAMMA 3354 * PNG_GAMMA
3226 * 3355 *
3227 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and 3356 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3228 * PNG_COMPOSITE code performs gamma correction, so we get double gamma 3357 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3229 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening 3358 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3230 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles 3359 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3231 * the removal or pre-multiplication of the alpha channel. 3360 * row and handles the removal or pre-multiplication of the alpha channel.
3232 */ 3361 */
3233 static int 3362 static int
3234 png_image_read_background(png_voidp argument) 3363 png_image_read_background(png_voidp argument)
3235 { 3364 {
3236 png_image_read_control *display = png_voidcast(png_image_read_control*, 3365 png_image_read_control *display = png_voidcast(png_image_read_control*,
3237 argument); 3366 argument);
3238 png_imagep image = display->image; 3367 png_imagep image = display->image;
3239 png_structrp png_ptr = image->opaque->png_ptr; 3368 png_structrp png_ptr = image->opaque->png_ptr;
3240 png_inforp info_ptr = image->opaque->info_ptr; 3369 png_inforp info_ptr = image->opaque->info_ptr;
3241 png_uint_32 height = image->height; 3370 png_uint_32 height = image->height;
(...skipping 23 matching lines...) Expand all
3265 { 3394 {
3266 case PNG_INTERLACE_NONE: 3395 case PNG_INTERLACE_NONE:
3267 passes = 1; 3396 passes = 1;
3268 break; 3397 break;
3269 3398
3270 case PNG_INTERLACE_ADAM7: 3399 case PNG_INTERLACE_ADAM7:
3271 passes = PNG_INTERLACE_ADAM7_PASSES; 3400 passes = PNG_INTERLACE_ADAM7_PASSES;
3272 break; 3401 break;
3273 3402
3274 default: 3403 default:
3275 passes = 0;
3276 png_error(png_ptr, "unknown interlace type"); 3404 png_error(png_ptr, "unknown interlace type");
3277 } 3405 }
3278 3406
3279 switch (png_get_bit_depth(png_ptr, info_ptr)) 3407 /* Use direct access to info_ptr here because otherwise the simplified API
3280 { 3408 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3281 default: 3409 * checking the value after libpng expansions, not the original value in the
3282 png_error(png_ptr, "unexpected bit depth"); 3410 * PNG.
3283 break; 3411 */
3284 3412 switch (info_ptr->bit_depth)
3413 {
3285 case 8: 3414 case 8:
3286 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is 3415 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3287 * to be removed by composing on a backgroundi: either the row if 3416 * to be removed by composing on a background: either the row if
3288 * display->background is NULL or display->background->green if not. 3417 * display->background is NULL or display->background->green if not.
3289 * Unlike the code above ALPHA_OPTIMIZED has *not* been done. 3418 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3290 */ 3419 */
3291 { 3420 {
3292 png_bytep first_row = png_voidcast(png_bytep, display->first_row); 3421 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3293 ptrdiff_t step_row = display->row_bytes; 3422 ptrdiff_t step_row = display->row_bytes;
3294 3423
3295 for (pass = 0; pass < passes; ++pass) 3424 for (pass = 0; pass < passes; ++pass)
3296 { 3425 {
3297 png_bytep row = png_voidcast(png_bytep, 3426 png_bytep row = png_voidcast(png_bytep,
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
3417 png_uint_16p first_row = png_voidcast(png_uint_16p, 3546 png_uint_16p first_row = png_voidcast(png_uint_16p,
3418 display->first_row); 3547 display->first_row);
3419 /* The division by two is safe because the caller passed in a 3548 /* The division by two is safe because the caller passed in a
3420 * stride which was multiplied by 2 (below) to get row_bytes. 3549 * stride which was multiplied by 2 (below) to get row_bytes.
3421 */ 3550 */
3422 ptrdiff_t step_row = display->row_bytes / 2; 3551 ptrdiff_t step_row = display->row_bytes / 2;
3423 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; 3552 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
3424 unsigned int outchannels = 1+preserve_alpha; 3553 unsigned int outchannels = 1+preserve_alpha;
3425 int swap_alpha = 0; 3554 int swap_alpha = 0;
3426 3555
3427 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST)) 3556 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3428 swap_alpha = 1; 3557 if (preserve_alpha != 0 &&
3558 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3559 swap_alpha = 1;
3560 # endif
3429 3561
3430 for (pass = 0; pass < passes; ++pass) 3562 for (pass = 0; pass < passes; ++pass)
3431 { 3563 {
3432 unsigned int startx, stepx, stepy; 3564 unsigned int startx, stepx, stepy;
3433 png_uint_32 y; 3565 png_uint_32 y;
3434 3566
3435 /* The 'x' start and step are adjusted to output components here. 3567 /* The 'x' start and step are adjusted to output components here.
3436 */ 3568 */
3437 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) 3569 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3438 { 3570 {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3480 component *= alpha; 3612 component *= alpha;
3481 component += 32767; 3613 component += 32767;
3482 component /= 65535; 3614 component /= 65535;
3483 } 3615 }
3484 } 3616 }
3485 3617
3486 else 3618 else
3487 component = 0; 3619 component = 0;
3488 3620
3489 outrow[swap_alpha] = (png_uint_16)component; 3621 outrow[swap_alpha] = (png_uint_16)component;
3490 if (preserve_alpha) 3622 if (preserve_alpha != 0)
3491 outrow[1 ^ swap_alpha] = alpha; 3623 outrow[1 ^ swap_alpha] = alpha;
3492 3624
3493 inrow += 2; /* components and alpha channel */ 3625 inrow += 2; /* components and alpha channel */
3494 } 3626 }
3495 } 3627 }
3496 } 3628 }
3497 } 3629 }
3498 break; 3630 break;
3631
3632 #ifdef __GNUC__
3633 default:
3634 png_error(png_ptr, "unexpected bit depth");
3635 #endif
3499 } 3636 }
3500 3637
3501 return 1; 3638 return 1;
3502 } 3639 }
3503 3640
3504 /* The guts of png_image_finish_read as a png_safe_execute callback. */ 3641 /* The guts of png_image_finish_read as a png_safe_execute callback. */
3505 static int 3642 static int
3506 png_image_read_direct(png_voidp argument) 3643 png_image_read_direct(png_voidp argument)
3507 { 3644 {
3508 png_image_read_control *display = png_voidcast(png_image_read_control*, 3645 png_image_read_control *display = png_voidcast(png_image_read_control*,
(...skipping 16 matching lines...) Expand all
3525 3662
3526 /* Now check the format to see if it was modified. */ 3663 /* Now check the format to see if it was modified. */
3527 { 3664 {
3528 png_uint_32 base_format = png_image_format(png_ptr) & 3665 png_uint_32 base_format = png_image_format(png_ptr) &
3529 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; 3666 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3530 png_uint_32 change = format ^ base_format; 3667 png_uint_32 change = format ^ base_format;
3531 png_fixed_point output_gamma; 3668 png_fixed_point output_gamma;
3532 int mode; /* alpha mode */ 3669 int mode; /* alpha mode */
3533 3670
3534 /* Do this first so that we have a record if rgb to gray is happening. */ 3671 /* Do this first so that we have a record if rgb to gray is happening. */
3535 if (change & PNG_FORMAT_FLAG_COLOR) 3672 if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
3536 { 3673 {
3537 /* gray<->color transformation required. */ 3674 /* gray<->color transformation required. */
3538 if (format & PNG_FORMAT_FLAG_COLOR) 3675 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3539 png_set_gray_to_rgb(png_ptr); 3676 png_set_gray_to_rgb(png_ptr);
3540 3677
3541 else 3678 else
3542 { 3679 {
3543 /* libpng can't do both rgb to gray and 3680 /* libpng can't do both rgb to gray and
3544 * background/pre-multiplication if there is also significant gamma 3681 * background/pre-multiplication if there is also significant gamma
3545 * correction, because both operations require linear colors and 3682 * correction, because both operations require linear colors and
3546 * the code only supports one transform doing the gamma correction. 3683 * the code only supports one transform doing the gamma correction.
3547 * Handle this by doing the pre-multiplication or background 3684 * Handle this by doing the pre-multiplication or background
3548 * operation in this code, if necessary. 3685 * operation in this code, if necessary.
3549 * 3686 *
3550 * TODO: fix this by rewriting pngrtran.c (!) 3687 * TODO: fix this by rewriting pngrtran.c (!)
3551 * 3688 *
3552 * For the moment (given that fixing this in pngrtran.c is an 3689 * For the moment (given that fixing this in pngrtran.c is an
3553 * enormous change) 'do_local_background' is used to indicate that 3690 * enormous change) 'do_local_background' is used to indicate that
3554 * the problem exists. 3691 * the problem exists.
3555 */ 3692 */
3556 if (base_format & PNG_FORMAT_FLAG_ALPHA) 3693 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3557 do_local_background = 1/*maybe*/; 3694 do_local_background = 1/*maybe*/;
3558 3695
3559 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, 3696 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3560 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); 3697 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3561 } 3698 }
3562 3699
3563 change &= ~PNG_FORMAT_FLAG_COLOR; 3700 change &= ~PNG_FORMAT_FLAG_COLOR;
3564 } 3701 }
3565 3702
3566 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. 3703 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3567 */ 3704 */
3568 { 3705 {
3569 png_fixed_point input_gamma_default; 3706 png_fixed_point input_gamma_default;
3570 3707
3571 if ((base_format & PNG_FORMAT_FLAG_LINEAR) && 3708 if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3572 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) 3709 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
3573 input_gamma_default = PNG_GAMMA_LINEAR; 3710 input_gamma_default = PNG_GAMMA_LINEAR;
3574 else 3711 else
3575 input_gamma_default = PNG_DEFAULT_sRGB; 3712 input_gamma_default = PNG_DEFAULT_sRGB;
3576 3713
3577 /* Call png_set_alpha_mode to set the default for the input gamma; the 3714 /* Call png_set_alpha_mode to set the default for the input gamma; the
3578 * output gamma is set by a second call below. 3715 * output gamma is set by a second call below.
3579 */ 3716 */
3580 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); 3717 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3581 } 3718 }
3582 3719
3583 if (linear) 3720 if (linear != 0)
3584 { 3721 {
3585 /* If there *is* an alpha channel in the input it must be multiplied 3722 /* If there *is* an alpha channel in the input it must be multiplied
3586 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. 3723 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3587 */ 3724 */
3588 if (base_format & PNG_FORMAT_FLAG_ALPHA) 3725 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3589 mode = PNG_ALPHA_STANDARD; /* associated alpha */ 3726 mode = PNG_ALPHA_STANDARD; /* associated alpha */
3590 3727
3591 else 3728 else
3592 mode = PNG_ALPHA_PNG; 3729 mode = PNG_ALPHA_PNG;
3593 3730
3594 output_gamma = PNG_GAMMA_LINEAR; 3731 output_gamma = PNG_GAMMA_LINEAR;
3595 } 3732 }
3596 3733
3597 else 3734 else
3598 { 3735 {
3599 mode = PNG_ALPHA_PNG; 3736 mode = PNG_ALPHA_PNG;
3600 output_gamma = PNG_DEFAULT_sRGB; 3737 output_gamma = PNG_DEFAULT_sRGB;
3601 } 3738 }
3602 3739
3603 /* If 'do_local_background' is set check for the presence of gamma 3740 /* If 'do_local_background' is set check for the presence of gamma
3604 * correction; this is part of the work-round for the libpng bug 3741 * correction; this is part of the work-round for the libpng bug
3605 * described above. 3742 * described above.
3606 * 3743 *
3607 * TODO: fix libpng and remove this. 3744 * TODO: fix libpng and remove this.
3608 */ 3745 */
3609 if (do_local_background) 3746 if (do_local_background != 0)
3610 { 3747 {
3611 png_fixed_point gtest; 3748 png_fixed_point gtest;
3612 3749
3613 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for 3750 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3614 * gamma correction, the screen gamma hasn't been set on png_struct 3751 * gamma correction, the screen gamma hasn't been set on png_struct
3615 * yet; it's set below. png_struct::gamma, however, is set to the 3752 * yet; it's set below. png_struct::gamma, however, is set to the
3616 * final value. 3753 * final value.
3617 */ 3754 */
3618 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma, 3755 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3619 PNG_FP_1) && !png_gamma_significant(gtest)) 3756 PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
3620 do_local_background = 0; 3757 do_local_background = 0;
3621 3758
3622 else if (mode == PNG_ALPHA_STANDARD) 3759 else if (mode == PNG_ALPHA_STANDARD)
3623 { 3760 {
3624 do_local_background = 2/*required*/; 3761 do_local_background = 2/*required*/;
3625 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ 3762 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3626 } 3763 }
3627 3764
3628 /* else leave as 1 for the checks below */ 3765 /* else leave as 1 for the checks below */
3629 } 3766 }
3630 3767
3631 /* If the bit-depth changes then handle that here. */ 3768 /* If the bit-depth changes then handle that here. */
3632 if (change & PNG_FORMAT_FLAG_LINEAR) 3769 if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
3633 { 3770 {
3634 if (linear /*16-bit output*/) 3771 if (linear != 0 /*16-bit output*/)
3635 png_set_expand_16(png_ptr); 3772 png_set_expand_16(png_ptr);
3636 3773
3637 else /* 8-bit output */ 3774 else /* 8-bit output */
3638 png_set_scale_16(png_ptr); 3775 png_set_scale_16(png_ptr);
3639 3776
3640 change &= ~PNG_FORMAT_FLAG_LINEAR; 3777 change &= ~PNG_FORMAT_FLAG_LINEAR;
3641 } 3778 }
3642 3779
3643 /* Now the background/alpha channel changes. */ 3780 /* Now the background/alpha channel changes. */
3644 if (change & PNG_FORMAT_FLAG_ALPHA) 3781 if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
3645 { 3782 {
3646 /* Removing an alpha channel requires composition for the 8-bit 3783 /* Removing an alpha channel requires composition for the 8-bit
3647 * formats; for the 16-bit it is already done, above, by the 3784 * formats; for the 16-bit it is already done, above, by the
3648 * pre-multiplication and the channel just needs to be stripped. 3785 * pre-multiplication and the channel just needs to be stripped.
3649 */ 3786 */
3650 if (base_format & PNG_FORMAT_FLAG_ALPHA) 3787 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3651 { 3788 {
3652 /* If RGB->gray is happening the alpha channel must be left and the 3789 /* If RGB->gray is happening the alpha channel must be left and the
3653 * operation completed locally. 3790 * operation completed locally.
3654 * 3791 *
3655 * TODO: fix libpng and remove this. 3792 * TODO: fix libpng and remove this.
3656 */ 3793 */
3657 if (do_local_background) 3794 if (do_local_background != 0)
3658 do_local_background = 2/*required*/; 3795 do_local_background = 2/*required*/;
3659 3796
3660 /* 16-bit output: just remove the channel */ 3797 /* 16-bit output: just remove the channel */
3661 else if (linear) /* compose on black (well, pre-multiply) */ 3798 else if (linear != 0) /* compose on black (well, pre-multiply) */
3662 png_set_strip_alpha(png_ptr); 3799 png_set_strip_alpha(png_ptr);
3663 3800
3664 /* 8-bit output: do an appropriate compose */ 3801 /* 8-bit output: do an appropriate compose */
3665 else if (display->background != NULL) 3802 else if (display->background != NULL)
3666 { 3803 {
3667 png_color_16 c; 3804 png_color_16 c;
3668 3805
3669 c.index = 0; /*unused*/ 3806 c.index = 0; /*unused*/
3670 c.red = display->background->red; 3807 c.red = display->background->red;
3671 c.green = display->background->green; 3808 c.green = display->background->green;
(...skipping 26 matching lines...) Expand all
3698 else /* output needs an alpha channel */ 3835 else /* output needs an alpha channel */
3699 { 3836 {
3700 /* This is tricky because it happens before the swap operation has 3837 /* This is tricky because it happens before the swap operation has
3701 * been accomplished; however, the swap does *not* swap the added 3838 * been accomplished; however, the swap does *not* swap the added
3702 * alpha channel (weird API), so it must be added in the correct 3839 * alpha channel (weird API), so it must be added in the correct
3703 * place. 3840 * place.
3704 */ 3841 */
3705 png_uint_32 filler; /* opaque filler */ 3842 png_uint_32 filler; /* opaque filler */
3706 int where; 3843 int where;
3707 3844
3708 if (linear) 3845 if (linear != 0)
3709 filler = 65535; 3846 filler = 65535;
3710 3847
3711 else 3848 else
3712 filler = 255; 3849 filler = 255;
3713 3850
3714 # ifdef PNG_FORMAT_AFIRST_SUPPORTED 3851 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
3715 if (format & PNG_FORMAT_FLAG_AFIRST) 3852 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3716 { 3853 {
3717 where = PNG_FILLER_BEFORE; 3854 where = PNG_FILLER_BEFORE;
3718 change &= ~PNG_FORMAT_FLAG_AFIRST; 3855 change &= ~PNG_FORMAT_FLAG_AFIRST;
3719 } 3856 }
3720 3857
3721 else 3858 else
3722 # endif 3859 # endif
3723 where = PNG_FILLER_AFTER; 3860 where = PNG_FILLER_AFTER;
3724 3861
3725 png_set_add_alpha(png_ptr, filler, where); 3862 png_set_add_alpha(png_ptr, filler, where);
3726 } 3863 }
3727 3864
3728 /* This stops the (irrelevant) call to swap_alpha below. */ 3865 /* This stops the (irrelevant) call to swap_alpha below. */
3729 change &= ~PNG_FORMAT_FLAG_ALPHA; 3866 change &= ~PNG_FORMAT_FLAG_ALPHA;
3730 } 3867 }
3731 3868
3732 /* Now set the alpha mode correctly; this is always done, even if there is 3869 /* Now set the alpha mode correctly; this is always done, even if there is
3733 * no alpha channel in either the input or the output because it correctly 3870 * no alpha channel in either the input or the output because it correctly
3734 * sets the output gamma. 3871 * sets the output gamma.
3735 */ 3872 */
3736 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); 3873 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3737 3874
3738 # ifdef PNG_FORMAT_BGR_SUPPORTED 3875 # ifdef PNG_FORMAT_BGR_SUPPORTED
3739 if (change & PNG_FORMAT_FLAG_BGR) 3876 if ((change & PNG_FORMAT_FLAG_BGR) != 0)
3740 { 3877 {
3741 /* Check only the output format; PNG is never BGR; don't do this if 3878 /* Check only the output format; PNG is never BGR; don't do this if
3742 * the output is gray, but fix up the 'format' value in that case. 3879 * the output is gray, but fix up the 'format' value in that case.
3743 */ 3880 */
3744 if (format & PNG_FORMAT_FLAG_COLOR) 3881 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3745 png_set_bgr(png_ptr); 3882 png_set_bgr(png_ptr);
3746 3883
3747 else 3884 else
3748 format &= ~PNG_FORMAT_FLAG_BGR; 3885 format &= ~PNG_FORMAT_FLAG_BGR;
3749 3886
3750 change &= ~PNG_FORMAT_FLAG_BGR; 3887 change &= ~PNG_FORMAT_FLAG_BGR;
3751 } 3888 }
3752 # endif 3889 # endif
3753 3890
3754 # ifdef PNG_FORMAT_AFIRST_SUPPORTED 3891 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
3755 if (change & PNG_FORMAT_FLAG_AFIRST) 3892 if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
3756 { 3893 {
3757 /* Only relevant if there is an alpha channel - it's particularly 3894 /* Only relevant if there is an alpha channel - it's particularly
3758 * important to handle this correctly because do_local_compose may 3895 * important to handle this correctly because do_local_compose may
3759 * be set above and then libpng will keep the alpha channel for this 3896 * be set above and then libpng will keep the alpha channel for this
3760 * code to remove. 3897 * code to remove.
3761 */ 3898 */
3762 if (format & PNG_FORMAT_FLAG_ALPHA) 3899 if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
3763 { 3900 {
3764 /* Disable this if doing a local background, 3901 /* Disable this if doing a local background,
3765 * TODO: remove this when local background is no longer required. 3902 * TODO: remove this when local background is no longer required.
3766 */ 3903 */
3767 if (do_local_background != 2) 3904 if (do_local_background != 2)
3768 png_set_swap_alpha(png_ptr); 3905 png_set_swap_alpha(png_ptr);
3769 } 3906 }
3770 3907
3771 else 3908 else
3772 format &= ~PNG_FORMAT_FLAG_AFIRST; 3909 format &= ~PNG_FORMAT_FLAG_AFIRST;
3773 3910
3774 change &= ~PNG_FORMAT_FLAG_AFIRST; 3911 change &= ~PNG_FORMAT_FLAG_AFIRST;
3775 } 3912 }
3776 # endif 3913 # endif
3777 3914
3778 /* If the *output* is 16-bit then we need to check for a byte-swap on this 3915 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3779 * architecture. 3916 * architecture.
3780 */ 3917 */
3781 if (linear) 3918 if (linear != 0)
3782 { 3919 {
3783 PNG_CONST png_uint_16 le = 0x0001; 3920 PNG_CONST png_uint_16 le = 0x0001;
3784 3921
3785 if (*(png_const_bytep)&le) 3922 if ((*(png_const_bytep) & le) != 0)
3786 png_set_swap(png_ptr); 3923 png_set_swap(png_ptr);
3787 } 3924 }
3788 3925
3789 /* If change is not now 0 some transformation is missing - error out. */ 3926 /* If change is not now 0 some transformation is missing - error out. */
3790 if (change) 3927 if (change != 0)
3791 png_error(png_ptr, "png_read_image: unsupported transformation"); 3928 png_error(png_ptr, "png_read_image: unsupported transformation");
3792 } 3929 }
3793 3930
3794 PNG_SKIP_CHUNKS(png_ptr); 3931 PNG_SKIP_CHUNKS(png_ptr);
3795 3932
3796 /* Update the 'info' structure and make sure the result is as required; first 3933 /* Update the 'info' structure and make sure the result is as required; first
3797 * make sure to turn on the interlace handling if it will be required 3934 * make sure to turn on the interlace handling if it will be required
3798 * (because it can't be turned on *after* the call to png_read_update_info!) 3935 * (because it can't be turned on *after* the call to png_read_update_info!)
3799 * 3936 *
3800 * TODO: remove the do_local_background fixup below. 3937 * TODO: remove the do_local_background fixup below.
3801 */ 3938 */
3802 if (!do_local_compose && do_local_background != 2) 3939 if (do_local_compose == 0 && do_local_background != 2)
3803 passes = png_set_interlace_handling(png_ptr); 3940 passes = png_set_interlace_handling(png_ptr);
3804 3941
3805 png_read_update_info(png_ptr, info_ptr); 3942 png_read_update_info(png_ptr, info_ptr);
3806 3943
3807 { 3944 {
3808 png_uint_32 info_format = 0; 3945 png_uint_32 info_format = 0;
3809 3946
3810 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) 3947 if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3811 info_format |= PNG_FORMAT_FLAG_COLOR; 3948 info_format |= PNG_FORMAT_FLAG_COLOR;
3812 3949
3813 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) 3950 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
3814 { 3951 {
3815 /* do_local_compose removes this channel below. */ 3952 /* do_local_compose removes this channel below. */
3816 if (!do_local_compose) 3953 if (do_local_compose == 0)
3817 { 3954 {
3818 /* do_local_background does the same if required. */ 3955 /* do_local_background does the same if required. */
3819 if (do_local_background != 2 || 3956 if (do_local_background != 2 ||
3820 (format & PNG_FORMAT_FLAG_ALPHA) != 0) 3957 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3821 info_format |= PNG_FORMAT_FLAG_ALPHA; 3958 info_format |= PNG_FORMAT_FLAG_ALPHA;
3822 } 3959 }
3823 } 3960 }
3824 3961
3825 else if (do_local_compose) /* internal error */ 3962 else if (do_local_compose != 0) /* internal error */
3826 png_error(png_ptr, "png_image_read: alpha channel lost"); 3963 png_error(png_ptr, "png_image_read: alpha channel lost");
3827 3964
3828 if (info_ptr->bit_depth == 16) 3965 if (info_ptr->bit_depth == 16)
3829 info_format |= PNG_FORMAT_FLAG_LINEAR; 3966 info_format |= PNG_FORMAT_FLAG_LINEAR;
3830 3967
3831 # ifdef PNG_FORMAT_BGR_SUPPORTED 3968 # ifdef PNG_FORMAT_BGR_SUPPORTED
3832 if (png_ptr->transformations & PNG_BGR) 3969 if ((png_ptr->transformations & PNG_BGR) != 0)
3833 info_format |= PNG_FORMAT_FLAG_BGR; 3970 info_format |= PNG_FORMAT_FLAG_BGR;
3834 # endif 3971 # endif
3835 3972
3836 # ifdef PNG_FORMAT_AFIRST_SUPPORTED 3973 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
3837 if (do_local_background == 2) 3974 if (do_local_background == 2)
3838 { 3975 {
3839 if (format & PNG_FORMAT_FLAG_AFIRST) 3976 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3840 info_format |= PNG_FORMAT_FLAG_AFIRST; 3977 info_format |= PNG_FORMAT_FLAG_AFIRST;
3841 } 3978 }
3842 3979
3843 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || 3980 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
3844 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && 3981 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3845 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) 3982 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
3846 { 3983 {
3847 if (do_local_background == 2) 3984 if (do_local_background == 2)
3848 png_error(png_ptr, "unexpected alpha swap transformation"); 3985 png_error(png_ptr, "unexpected alpha swap transformation");
3849 3986
3850 info_format |= PNG_FORMAT_FLAG_AFIRST; 3987 info_format |= PNG_FORMAT_FLAG_AFIRST;
3851 } 3988 }
3852 # endif 3989 # endif
3853 3990
3854 /* This is actually an internal error. */ 3991 /* This is actually an internal error. */
3855 if (info_format != format) 3992 if (info_format != format)
3856 png_error(png_ptr, "png_read_image: invalid transformations"); 3993 png_error(png_ptr, "png_read_image: invalid transformations");
3857 } 3994 }
3858 3995
3859 /* Now read the rows. If do_local_compose is set then it is necessary to use 3996 /* Now read the rows. If do_local_compose is set then it is necessary to use
3860 * a local row buffer. The output will be GA, RGBA or BGRA and must be 3997 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3861 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the 3998 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3862 * display acts as a flag. 3999 * display acts as a flag.
3863 */ 4000 */
3864 { 4001 {
3865 png_voidp first_row = display->buffer; 4002 png_voidp first_row = display->buffer;
3866 ptrdiff_t row_bytes = display->row_stride; 4003 ptrdiff_t row_bytes = display->row_stride;
3867 4004
3868 if (linear) 4005 if (linear != 0)
3869 row_bytes *= 2; 4006 row_bytes *= 2;
3870 4007
3871 /* The following expression is designed to work correctly whether it gives 4008 /* The following expression is designed to work correctly whether it gives
3872 * a signed or an unsigned result. 4009 * a signed or an unsigned result.
3873 */ 4010 */
3874 if (row_bytes < 0) 4011 if (row_bytes < 0)
3875 { 4012 {
3876 char *ptr = png_voidcast(char*, first_row); 4013 char *ptr = png_voidcast(char*, first_row);
3877 ptr += (image->height-1) * (-row_bytes); 4014 ptr += (image->height-1) * (-row_bytes);
3878 first_row = png_voidcast(png_voidp, ptr); 4015 first_row = png_voidcast(png_voidp, ptr);
3879 } 4016 }
3880 4017
3881 display->first_row = first_row; 4018 display->first_row = first_row;
3882 display->row_bytes = row_bytes; 4019 display->row_bytes = row_bytes;
3883 } 4020 }
3884 4021
3885 if (do_local_compose) 4022 if (do_local_compose != 0)
3886 { 4023 {
3887 int result; 4024 int result;
3888 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); 4025 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3889 4026
3890 display->local_row = row; 4027 display->local_row = row;
3891 result = png_safe_execute(image, png_image_read_composite, display); 4028 result = png_safe_execute(image, png_image_read_composite, display);
3892 display->local_row = NULL; 4029 display->local_row = NULL;
3893 png_free(png_ptr, row); 4030 png_free(png_ptr, row);
3894 4031
3895 return result; 4032 return result;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
3958 display.image = image; 4095 display.image = image;
3959 display.buffer = buffer; 4096 display.buffer = buffer;
3960 display.row_stride = row_stride; 4097 display.row_stride = row_stride;
3961 display.colormap = colormap; 4098 display.colormap = colormap;
3962 display.background = background; 4099 display.background = background;
3963 display.local_row = NULL; 4100 display.local_row = NULL;
3964 4101
3965 /* Choose the correct 'end' routine; for the color-map case all the 4102 /* Choose the correct 'end' routine; for the color-map case all the
3966 * setup has already been done. 4103 * setup has already been done.
3967 */ 4104 */
3968 if (image->format & PNG_FORMAT_FLAG_COLORMAP) 4105 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
3969 result = 4106 result =
3970 png_safe_execute(image, png_image_read_colormap, &display) && 4107 png_safe_execute(image, png_image_read_colormap, &display) &&
3971 png_safe_execute(image, png_image_read_colormapped, &display); 4108 png_safe_execute(image, png_image_read_colormapped, &display);
3972 4109
3973 else 4110 else
3974 result = 4111 result =
3975 png_safe_execute(image, png_image_read_direct, &display); 4112 png_safe_execute(image, png_image_read_direct, &display);
3976 4113
3977 png_image_free(image); 4114 png_image_free(image);
3978 return result; 4115 return result;
3979 } 4116 }
3980 4117
3981 else 4118 else
3982 return png_image_error(image, 4119 return png_image_error(image,
3983 "png_image_finish_read[color-map]: no color-map"); 4120 "png_image_finish_read[color-map]: no color-map");
3984 } 4121 }
3985 4122
3986 else 4123 else
3987 return png_image_error(image, 4124 return png_image_error(image,
3988 "png_image_finish_read: invalid argument"); 4125 "png_image_finish_read: invalid argument");
3989 } 4126 }
3990 4127
3991 else if (image != NULL) 4128 else if (image != NULL)
3992 return png_image_error(image, 4129 return png_image_error(image,
3993 "png_image_finish_read: damaged PNG_IMAGE_VERSION"); 4130 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
3994 4131
3995 return 0; 4132 return 0;
3996 } 4133 }
3997 4134
3998 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */ 4135 #endif /* SIMPLIFIED_READ */
3999 #endif /* PNG_READ_SUPPORTED */ 4136 #endif /* READ */
OLDNEW
« no previous file with comments | « third_party/libpng/pngpriv.h ('k') | third_party/libpng/pngrio.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698