2 /* pngread.c - read a PNG file
4 * Last changed in libpng 1.6.15 [November 20, 2014]
5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
18 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
22 #ifdef PNG_READ_SUPPORTED
24 /* Create a PNG structure for reading, and allocate any memory needed. */
25 PNG_FUNCTION(png_structp
,PNGAPI
26 png_create_read_struct
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
27 png_error_ptr error_fn
, png_error_ptr warn_fn
),PNG_ALLOCATED
)
29 #ifndef PNG_USER_MEM_SUPPORTED
30 png_structp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
31 error_fn
, warn_fn
, NULL
, NULL
, NULL
);
33 return png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
34 warn_fn
, NULL
, NULL
, NULL
);
37 /* Alternate create PNG structure for reading, and allocate any memory
40 PNG_FUNCTION(png_structp
,PNGAPI
41 png_create_read_struct_2
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
42 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
43 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
),PNG_ALLOCATED
)
45 png_structp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
46 error_fn
, warn_fn
, mem_ptr
, malloc_fn
, free_fn
);
51 png_ptr
->mode
= PNG_IS_READ_STRUCT
;
53 /* Added in libpng-1.6.0; this can be used to detect a read structure if
54 * required (it will be zero in a write structure.)
56 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr
->IDAT_read_size
= PNG_IDAT_READ_SIZE
;
60 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr
->flags
|= PNG_FLAG_BENIGN_ERRORS_WARN
;
63 /* In stable builds only warn if an application error can be completely
66 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
67 png_ptr
->flags
|= PNG_FLAG_APP_WARNINGS_WARN
;
71 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
72 * do it itself) avoiding setting the default function if it is not
75 png_set_read_fn(png_ptr
, NULL
, NULL
);
82 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
83 /* Read the information before the actual image data. This has been
84 * changed in v0.90 to allow reading a file that already has the magic
85 * bytes read from the stream. You can tell libpng how many bytes have
86 * been read from the beginning of the stream (up to the maximum of 8)
87 * via png_set_sig_bytes(), and we will only check the remaining bytes
88 * here. The application can then have access to the signature bytes we
89 * read if it is determined that this isn't a valid PNG file.
92 png_read_info(png_structrp png_ptr
, png_inforp info_ptr
)
94 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
98 png_debug(1, "in png_read_info");
100 if (png_ptr
== NULL
|| info_ptr
== NULL
)
103 /* Read and check the PNG file signature. */
104 png_read_sig(png_ptr
, info_ptr
);
108 png_uint_32 length
= png_read_chunk_header(png_ptr
);
109 png_uint_32 chunk_name
= png_ptr
->chunk_name
;
111 /* IDAT logic needs to happen here to simplify getting the two flags
114 if (chunk_name
== png_IDAT
)
116 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
117 png_chunk_error(png_ptr
, "Missing IHDR before IDAT");
119 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
120 (png_ptr
->mode
& PNG_HAVE_PLTE
) == 0)
121 png_chunk_error(png_ptr
, "Missing PLTE before IDAT");
123 else if ((png_ptr
->mode
& PNG_AFTER_IDAT
) != 0)
124 png_chunk_benign_error(png_ptr
, "Too many IDATs found");
126 png_ptr
->mode
|= PNG_HAVE_IDAT
;
129 else if ((png_ptr
->mode
& PNG_HAVE_IDAT
) != 0)
130 png_ptr
->mode
|= PNG_AFTER_IDAT
;
132 /* This should be a binary subdivision search or a hash for
133 * matching the chunk name rather than a linear search.
135 if (chunk_name
== png_IHDR
)
136 png_handle_IHDR(png_ptr
, info_ptr
, length
);
138 else if (chunk_name
== png_IEND
)
139 png_handle_IEND(png_ptr
, info_ptr
, length
);
141 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
142 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
144 png_handle_unknown(png_ptr
, info_ptr
, length
, keep
);
146 if (chunk_name
== png_PLTE
)
147 png_ptr
->mode
|= PNG_HAVE_PLTE
;
149 else if (chunk_name
== png_IDAT
)
151 png_ptr
->idat_size
= 0; /* It has been consumed */
156 else if (chunk_name
== png_PLTE
)
157 png_handle_PLTE(png_ptr
, info_ptr
, length
);
159 else if (chunk_name
== png_IDAT
)
161 #ifdef PNG_READ_APNG_SUPPORTED
162 png_have_info(png_ptr
, info_ptr
);
164 png_ptr
->idat_size
= length
;
168 #ifdef PNG_READ_bKGD_SUPPORTED
169 else if (chunk_name
== png_bKGD
)
170 png_handle_bKGD(png_ptr
, info_ptr
, length
);
173 #ifdef PNG_READ_cHRM_SUPPORTED
174 else if (chunk_name
== png_cHRM
)
175 png_handle_cHRM(png_ptr
, info_ptr
, length
);
178 #ifdef PNG_READ_gAMA_SUPPORTED
179 else if (chunk_name
== png_gAMA
)
180 png_handle_gAMA(png_ptr
, info_ptr
, length
);
183 #ifdef PNG_READ_hIST_SUPPORTED
184 else if (chunk_name
== png_hIST
)
185 png_handle_hIST(png_ptr
, info_ptr
, length
);
188 #ifdef PNG_READ_oFFs_SUPPORTED
189 else if (chunk_name
== png_oFFs
)
190 png_handle_oFFs(png_ptr
, info_ptr
, length
);
193 #ifdef PNG_READ_pCAL_SUPPORTED
194 else if (chunk_name
== png_pCAL
)
195 png_handle_pCAL(png_ptr
, info_ptr
, length
);
198 #ifdef PNG_READ_sCAL_SUPPORTED
199 else if (chunk_name
== png_sCAL
)
200 png_handle_sCAL(png_ptr
, info_ptr
, length
);
203 #ifdef PNG_READ_pHYs_SUPPORTED
204 else if (chunk_name
== png_pHYs
)
205 png_handle_pHYs(png_ptr
, info_ptr
, length
);
208 #ifdef PNG_READ_sBIT_SUPPORTED
209 else if (chunk_name
== png_sBIT
)
210 png_handle_sBIT(png_ptr
, info_ptr
, length
);
213 #ifdef PNG_READ_sRGB_SUPPORTED
214 else if (chunk_name
== png_sRGB
)
215 png_handle_sRGB(png_ptr
, info_ptr
, length
);
218 #ifdef PNG_READ_iCCP_SUPPORTED
219 else if (chunk_name
== png_iCCP
)
220 png_handle_iCCP(png_ptr
, info_ptr
, length
);
223 #ifdef PNG_READ_sPLT_SUPPORTED
224 else if (chunk_name
== png_sPLT
)
225 png_handle_sPLT(png_ptr
, info_ptr
, length
);
228 #ifdef PNG_READ_tEXt_SUPPORTED
229 else if (chunk_name
== png_tEXt
)
230 png_handle_tEXt(png_ptr
, info_ptr
, length
);
233 #ifdef PNG_READ_tIME_SUPPORTED
234 else if (chunk_name
== png_tIME
)
235 png_handle_tIME(png_ptr
, info_ptr
, length
);
238 #ifdef PNG_READ_tRNS_SUPPORTED
239 else if (chunk_name
== png_tRNS
)
240 png_handle_tRNS(png_ptr
, info_ptr
, length
);
243 #ifdef PNG_READ_zTXt_SUPPORTED
244 else if (chunk_name
== png_zTXt
)
245 png_handle_zTXt(png_ptr
, info_ptr
, length
);
248 #ifdef PNG_READ_iTXt_SUPPORTED
249 else if (chunk_name
== png_iTXt
)
250 png_handle_iTXt(png_ptr
, info_ptr
, length
);
253 #ifdef PNG_READ_APNG_SUPPORTED
254 else if (chunk_name
== png_acTL
)
255 png_handle_acTL(png_ptr
, info_ptr
, length
);
257 else if (chunk_name
== png_fcTL
)
258 png_handle_fcTL(png_ptr
, info_ptr
, length
);
260 else if (chunk_name
== png_fdAT
)
261 png_handle_fdAT(png_ptr
, info_ptr
, length
);
265 png_handle_unknown(png_ptr
, info_ptr
, length
,
266 PNG_HANDLE_CHUNK_AS_DEFAULT
);
269 #endif /* SEQUENTIAL_READ */
271 #ifdef PNG_READ_APNG_SUPPORTED
273 png_read_frame_head(png_structp png_ptr
, png_infop info_ptr
)
275 png_byte have_chunk_after_DAT
; /* after IDAT or after fdAT */
277 png_debug(0, "Reading frame head");
279 if ((png_ptr
->mode
& PNG_HAVE_acTL
) == 0)
280 png_error(png_ptr
, "attempt to png_read_frame_head() but "
283 /* do nothing for the main IDAT */
284 if (png_ptr
->num_frames_read
== 0)
287 png_read_reset(png_ptr
);
288 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
289 png_ptr
->mode
&= ~PNG_HAVE_fcTL
;
291 have_chunk_after_DAT
= 0;
294 png_uint_32 length
= png_read_chunk_header(png_ptr
);
296 if (png_ptr
->chunk_name
== png_IDAT
)
298 /* discard trailing IDATs for the first frame */
299 if (have_chunk_after_DAT
!= 0 || png_ptr
->num_frames_read
> 1)
300 png_error(png_ptr
, "png_read_frame_head(): out of place IDAT");
301 png_crc_finish(png_ptr
, length
);
304 else if (png_ptr
->chunk_name
== png_fcTL
)
306 png_handle_fcTL(png_ptr
, info_ptr
, length
);
307 have_chunk_after_DAT
= 1;
310 else if (png_ptr
->chunk_name
== png_fdAT
)
312 png_ensure_sequence_number(png_ptr
, length
);
314 /* discard trailing fdATs for frames other than the first */
315 if (have_chunk_after_DAT
== 0 && png_ptr
->num_frames_read
> 1)
316 png_crc_finish(png_ptr
, length
- 4);
317 else if(png_ptr
->mode
& PNG_HAVE_fcTL
)
319 png_ptr
->idat_size
= length
- 4;
320 png_ptr
->mode
|= PNG_HAVE_IDAT
;
325 png_error(png_ptr
, "png_read_frame_head(): out of place fdAT");
329 png_warning(png_ptr
, "Skipped (ignored) a chunk "
330 "between APNG chunks");
331 png_crc_finish(png_ptr
, length
);
335 #endif /* READ_APNG */
337 /* Optional call to update the users info_ptr structure */
339 png_read_update_info(png_structrp png_ptr
, png_inforp info_ptr
)
341 png_debug(1, "in png_read_update_info");
345 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
347 png_read_start_row(png_ptr
);
349 # ifdef PNG_READ_TRANSFORMS_SUPPORTED
350 png_read_transform_info(png_ptr
, info_ptr
);
356 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
358 png_app_error(png_ptr
,
359 "png_read_update_info/png_start_read_image: duplicate call");
363 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
364 /* Initialize palette, background, etc, after transformations
365 * are set, but before any reading takes place. This allows
366 * the user to obtain a gamma-corrected palette, for example.
367 * If the user doesn't call this, we will do it ourselves.
370 png_start_read_image(png_structrp png_ptr
)
372 png_debug(1, "in png_start_read_image");
376 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
377 png_read_start_row(png_ptr
);
379 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
381 png_app_error(png_ptr
,
382 "png_start_read_image/png_read_update_info: duplicate call");
385 #endif /* SEQUENTIAL_READ */
387 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
388 #ifdef PNG_MNG_FEATURES_SUPPORTED
389 /* Undoes intrapixel differencing,
390 * NOTE: this is apparently only supported in the 'sequential' reader.
393 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
395 png_debug(1, "in png_do_read_intrapixel");
398 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
401 png_uint_32 row_width
= row_info
->width
;
403 if (row_info
->bit_depth
== 8)
408 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
411 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
417 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
419 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+ 1)) & 0xff);
420 *(rp
+2) = (png_byte
)((256 + *(rp
+ 2) + *(rp
+ 1)) & 0xff);
423 else if (row_info
->bit_depth
== 16)
428 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
431 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
437 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
439 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
440 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
441 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
442 png_uint_32 red
= (s0
+ s1
+ 65536) & 0xffff;
443 png_uint_32 blue
= (s2
+ s1
+ 65536) & 0xffff;
444 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
445 *(rp
+ 1) = (png_byte
)(red
& 0xff);
446 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
447 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
452 #endif /* MNG_FEATURES */
455 png_read_row(png_structrp png_ptr
, png_bytep row
, png_bytep dsp_row
)
457 png_row_info row_info
;
462 png_debug2(1, "in png_read_row (row %lu, pass %d)",
463 (unsigned long)png_ptr
->row_number
, png_ptr
->pass
);
465 /* png_read_start_row sets the information (in particular iwidth) for this
468 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
469 png_read_start_row(png_ptr
);
471 /* 1.5.6: row_info moved out of png_struct to a local here. */
472 row_info
.width
= png_ptr
->iwidth
; /* NOTE: width of current interlaced row */
473 row_info
.color_type
= png_ptr
->color_type
;
474 row_info
.bit_depth
= png_ptr
->bit_depth
;
475 row_info
.channels
= png_ptr
->channels
;
476 row_info
.pixel_depth
= png_ptr
->pixel_depth
;
477 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
479 #ifdef PNG_WARNINGS_SUPPORTED
480 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
482 /* Check for transforms that have been set but were defined out */
483 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
484 if ((png_ptr
->transformations
& PNG_INVERT_MONO
) != 0)
485 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined");
488 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
489 if ((png_ptr
->transformations
& PNG_FILLER
) != 0)
490 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined");
493 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
494 !defined(PNG_READ_PACKSWAP_SUPPORTED)
495 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
496 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
499 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
500 if ((png_ptr
->transformations
& PNG_PACK
) != 0)
501 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined");
504 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
505 if ((png_ptr
->transformations
& PNG_SHIFT
) != 0)
506 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined");
509 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
510 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
511 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined");
514 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
515 if ((png_ptr
->transformations
& PNG_SWAP_BYTES
) != 0)
516 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined");
519 #endif /* WARNINGS */
521 #ifdef PNG_READ_INTERLACING_SUPPORTED
522 /* If interlaced and we do not need a new row, combine row and return.
523 * Notice that the pixels we have from previous rows have been transformed
524 * already; we can only combine like with like (transformed or
525 * untransformed) and, because of the libpng API for interlaced images, this
526 * means we must transform before de-interlacing.
528 if (png_ptr
->interlaced
!= 0 &&
529 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
531 switch (png_ptr
->pass
)
534 if (png_ptr
->row_number
& 0x07)
537 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
538 png_read_finish_row(png_ptr
);
544 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
547 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
549 png_read_finish_row(png_ptr
);
555 if ((png_ptr
->row_number
& 0x07) != 4)
557 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
558 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
560 png_read_finish_row(png_ptr
);
566 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
569 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
571 png_read_finish_row(png_ptr
);
577 if ((png_ptr
->row_number
& 3) != 2)
579 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
580 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
582 png_read_finish_row(png_ptr
);
588 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
591 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
593 png_read_finish_row(png_ptr
);
600 if ((png_ptr
->row_number
& 1) == 0)
602 png_read_finish_row(png_ptr
);
610 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) == 0)
611 png_error(png_ptr
, "Invalid attempt to read row data");
613 /* Fill the row with IDAT data: */
614 png_read_IDAT_data(png_ptr
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
616 if (png_ptr
->row_buf
[0] > PNG_FILTER_VALUE_NONE
)
618 if (png_ptr
->row_buf
[0] < PNG_FILTER_VALUE_LAST
)
619 png_read_filter_row(png_ptr
, &row_info
, png_ptr
->row_buf
+ 1,
620 png_ptr
->prev_row
+ 1, png_ptr
->row_buf
[0]);
622 png_error(png_ptr
, "bad adaptive filter value");
625 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
626 * 1.5.6, while the buffer really is this big in current versions of libpng
627 * it may not be in the future, so this was changed just to copy the
630 memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
632 #ifdef PNG_MNG_FEATURES_SUPPORTED
633 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) != 0 &&
634 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
636 /* Intrapixel differencing */
637 png_do_read_intrapixel(&row_info
, png_ptr
->row_buf
+ 1);
641 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
642 if (png_ptr
->transformations
)
643 png_do_read_transformations(png_ptr
, &row_info
);
646 /* The transformed pixel depth should match the depth now in row_info. */
647 if (png_ptr
->transformed_pixel_depth
== 0)
649 png_ptr
->transformed_pixel_depth
= row_info
.pixel_depth
;
650 if (row_info
.pixel_depth
> png_ptr
->maximum_pixel_depth
)
651 png_error(png_ptr
, "sequential row overflow");
654 else if (png_ptr
->transformed_pixel_depth
!= row_info
.pixel_depth
)
655 png_error(png_ptr
, "internal sequential row size calculation error");
657 #ifdef PNG_READ_INTERLACING_SUPPORTED
658 /* Expand interlaced rows to full size */
659 if (png_ptr
->interlaced
!= 0 &&
660 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
662 if (png_ptr
->pass
< 6)
663 png_do_read_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
,
664 png_ptr
->transformations
);
667 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
670 png_combine_row(png_ptr
, row
, 0/*row*/);
677 png_combine_row(png_ptr
, row
, -1/*ignored*/);
680 png_combine_row(png_ptr
, dsp_row
, -1/*ignored*/);
682 png_read_finish_row(png_ptr
);
684 if (png_ptr
->read_row_fn
!= NULL
)
685 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
688 #endif /* SEQUENTIAL_READ */
690 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
691 /* Read one or more rows of image data. If the image is interlaced,
692 * and png_set_interlace_handling() has been called, the rows need to
693 * contain the contents of the rows from the previous pass. If the
694 * image has alpha or transparency, and png_handle_alpha()[*] has been
695 * called, the rows contents must be initialized to the contents of the
698 * "row" holds the actual image, and pixels are placed in it
699 * as they arrive. If the image is displayed after each pass, it will
700 * appear to "sparkle" in. "display_row" can be used to display a
701 * "chunky" progressive image, with finer detail added as it becomes
702 * available. If you do not want this "chunky" display, you may pass
703 * NULL for display_row. If you do not want the sparkle display, and
704 * you have not called png_handle_alpha(), you may pass NULL for rows.
705 * If you have called png_handle_alpha(), and the image has either an
706 * alpha channel or a transparency chunk, you must provide a buffer for
707 * rows. In this case, you do not have to provide a display_row buffer
708 * also, but you may. If the image is not interlaced, or if you have
709 * not called png_set_interlace_handling(), the display_row buffer will
710 * be ignored, so pass NULL to it.
712 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
716 png_read_rows(png_structrp png_ptr
, png_bytepp row
,
717 png_bytepp display_row
, png_uint_32 num_rows
)
723 png_debug(1, "in png_read_rows");
730 if (rp
!= NULL
&& dp
!= NULL
)
731 for (i
= 0; i
< num_rows
; i
++)
733 png_bytep rptr
= *rp
++;
734 png_bytep dptr
= *dp
++;
736 png_read_row(png_ptr
, rptr
, dptr
);
740 for (i
= 0; i
< num_rows
; i
++)
742 png_bytep rptr
= *rp
;
743 png_read_row(png_ptr
, rptr
, NULL
);
748 for (i
= 0; i
< num_rows
; i
++)
750 png_bytep dptr
= *dp
;
751 png_read_row(png_ptr
, NULL
, dptr
);
755 #endif /* SEQUENTIAL_READ */
757 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
758 /* Read the entire image. If the image has an alpha channel or a tRNS
759 * chunk, and you have called png_handle_alpha()[*], you will need to
760 * initialize the image to the current image that PNG will be overlaying.
761 * We set the num_rows again here, in case it was incorrectly set in
762 * png_read_start_row() by a call to png_read_update_info() or
763 * png_start_read_image() if png_set_interlace_handling() wasn't called
764 * prior to either of these functions like it should have been. You can
765 * only call this function once. If you desire to have an image for
766 * each pass of a interlaced image, use png_read_rows() instead.
768 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
771 png_read_image(png_structrp png_ptr
, png_bytepp image
)
773 png_uint_32 i
, image_height
;
777 png_debug(1, "in png_read_image");
782 #ifdef PNG_READ_INTERLACING_SUPPORTED
783 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
785 pass
= png_set_interlace_handling(png_ptr
);
786 /* And make sure transforms are initialized. */
787 png_start_read_image(png_ptr
);
791 if (png_ptr
->interlaced
!= 0 &&
792 (png_ptr
->transformations
& PNG_INTERLACE
) == 0)
794 /* Caller called png_start_read_image or png_read_update_info without
795 * first turning on the PNG_INTERLACE transform. We can fix this here,
796 * but the caller should do it!
798 png_warning(png_ptr
, "Interlace handling should be turned on when "
799 "using png_read_image");
800 /* Make sure this is set correctly */
801 png_ptr
->num_rows
= png_ptr
->height
;
804 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
805 * the above error case.
807 pass
= png_set_interlace_handling(png_ptr
);
810 if (png_ptr
->interlaced
)
812 "Cannot read interlaced image -- interlace handler disabled");
817 image_height
=png_ptr
->height
;
819 for (j
= 0; j
< pass
; j
++)
822 for (i
= 0; i
< image_height
; i
++)
824 png_read_row(png_ptr
, *rp
, NULL
);
829 #endif /* SEQUENTIAL_READ */
831 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
832 /* Read the end of the PNG file. Will not read past the end of the
833 * file, will verify the end is accurate, and will read any comments
834 * or time information at the end of the file, if info is not NULL.
837 png_read_end(png_structrp png_ptr
, png_inforp info_ptr
)
839 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
843 png_debug(1, "in png_read_end");
848 /* If png_read_end is called in the middle of reading the rows there may
849 * still be pending IDAT data and an owned zstream. Deal with this here.
851 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
852 if (png_chunk_unknown_handling(png_ptr
, png_IDAT
) == 0)
854 png_read_finish_IDAT(png_ptr
);
856 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
857 /* Report invalid palette index; added at libng-1.5.10 */
858 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
859 png_ptr
->num_palette_max
> png_ptr
->num_palette
)
860 png_benign_error(png_ptr
, "Read palette index exceeding num_palette");
865 png_uint_32 length
= png_read_chunk_header(png_ptr
);
866 png_uint_32 chunk_name
= png_ptr
->chunk_name
;
868 if (chunk_name
== png_IEND
)
869 png_handle_IEND(png_ptr
, info_ptr
, length
);
871 else if (chunk_name
== png_IHDR
)
872 png_handle_IHDR(png_ptr
, info_ptr
, length
);
874 else if (info_ptr
== NULL
)
875 png_crc_finish(png_ptr
, length
);
877 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
878 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
880 if (chunk_name
== png_IDAT
)
883 (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
884 png_benign_error(png_ptr
, "Too many IDATs found");
886 png_handle_unknown(png_ptr
, info_ptr
, length
, keep
);
887 if (chunk_name
== png_PLTE
)
888 png_ptr
->mode
|= PNG_HAVE_PLTE
;
892 else if (chunk_name
== png_IDAT
)
894 /* Zero length IDATs are legal after the last IDAT has been
895 * read, but not after other chunks have been read.
898 (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
899 png_benign_error(png_ptr
, "Too many IDATs found");
901 png_crc_finish(png_ptr
, length
);
903 else if (chunk_name
== png_PLTE
)
904 png_handle_PLTE(png_ptr
, info_ptr
, length
);
906 #ifdef PNG_READ_bKGD_SUPPORTED
907 else if (chunk_name
== png_bKGD
)
908 png_handle_bKGD(png_ptr
, info_ptr
, length
);
911 #ifdef PNG_READ_cHRM_SUPPORTED
912 else if (chunk_name
== png_cHRM
)
913 png_handle_cHRM(png_ptr
, info_ptr
, length
);
916 #ifdef PNG_READ_gAMA_SUPPORTED
917 else if (chunk_name
== png_gAMA
)
918 png_handle_gAMA(png_ptr
, info_ptr
, length
);
921 #ifdef PNG_READ_hIST_SUPPORTED
922 else if (chunk_name
== png_hIST
)
923 png_handle_hIST(png_ptr
, info_ptr
, length
);
926 #ifdef PNG_READ_oFFs_SUPPORTED
927 else if (chunk_name
== png_oFFs
)
928 png_handle_oFFs(png_ptr
, info_ptr
, length
);
931 #ifdef PNG_READ_pCAL_SUPPORTED
932 else if (chunk_name
== png_pCAL
)
933 png_handle_pCAL(png_ptr
, info_ptr
, length
);
936 #ifdef PNG_READ_sCAL_SUPPORTED
937 else if (chunk_name
== png_sCAL
)
938 png_handle_sCAL(png_ptr
, info_ptr
, length
);
941 #ifdef PNG_READ_pHYs_SUPPORTED
942 else if (chunk_name
== png_pHYs
)
943 png_handle_pHYs(png_ptr
, info_ptr
, length
);
946 #ifdef PNG_READ_sBIT_SUPPORTED
947 else if (chunk_name
== png_sBIT
)
948 png_handle_sBIT(png_ptr
, info_ptr
, length
);
951 #ifdef PNG_READ_sRGB_SUPPORTED
952 else if (chunk_name
== png_sRGB
)
953 png_handle_sRGB(png_ptr
, info_ptr
, length
);
956 #ifdef PNG_READ_iCCP_SUPPORTED
957 else if (chunk_name
== png_iCCP
)
958 png_handle_iCCP(png_ptr
, info_ptr
, length
);
961 #ifdef PNG_READ_sPLT_SUPPORTED
962 else if (chunk_name
== png_sPLT
)
963 png_handle_sPLT(png_ptr
, info_ptr
, length
);
966 #ifdef PNG_READ_tEXt_SUPPORTED
967 else if (chunk_name
== png_tEXt
)
968 png_handle_tEXt(png_ptr
, info_ptr
, length
);
971 #ifdef PNG_READ_tIME_SUPPORTED
972 else if (chunk_name
== png_tIME
)
973 png_handle_tIME(png_ptr
, info_ptr
, length
);
976 #ifdef PNG_READ_tRNS_SUPPORTED
977 else if (chunk_name
== png_tRNS
)
978 png_handle_tRNS(png_ptr
, info_ptr
, length
);
981 #ifdef PNG_READ_zTXt_SUPPORTED
982 else if (chunk_name
== png_zTXt
)
983 png_handle_zTXt(png_ptr
, info_ptr
, length
);
986 #ifdef PNG_READ_iTXt_SUPPORTED
987 else if (chunk_name
== png_iTXt
)
988 png_handle_iTXt(png_ptr
, info_ptr
, length
);
992 png_handle_unknown(png_ptr
, info_ptr
, length
,
993 PNG_HANDLE_CHUNK_AS_DEFAULT
);
994 } while ((png_ptr
->mode
& PNG_HAVE_IEND
) == 0);
996 #endif /* SEQUENTIAL_READ */
998 /* Free all memory used in the read struct */
1000 png_read_destroy(png_structrp png_ptr
)
1002 png_debug(1, "in png_read_destroy");
1004 #ifdef PNG_READ_GAMMA_SUPPORTED
1005 png_destroy_gamma_table(png_ptr
);
1008 png_free(png_ptr
, png_ptr
->big_row_buf
);
1009 png_ptr
->big_row_buf
= NULL
;
1010 png_free(png_ptr
, png_ptr
->big_prev_row
);
1011 png_ptr
->big_prev_row
= NULL
;
1012 png_free(png_ptr
, png_ptr
->read_buffer
);
1013 png_ptr
->read_buffer
= NULL
;
1015 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1016 png_free(png_ptr
, png_ptr
->palette_lookup
);
1017 png_ptr
->palette_lookup
= NULL
;
1018 png_free(png_ptr
, png_ptr
->quantize_index
);
1019 png_ptr
->quantize_index
= NULL
;
1022 if ((png_ptr
->free_me
& PNG_FREE_PLTE
) != 0)
1024 png_zfree(png_ptr
, png_ptr
->palette
);
1025 png_ptr
->palette
= NULL
;
1027 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1029 #if defined(PNG_tRNS_SUPPORTED) || \
1030 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1031 if ((png_ptr
->free_me
& PNG_FREE_TRNS
) != 0)
1033 png_free(png_ptr
, png_ptr
->trans_alpha
);
1034 png_ptr
->trans_alpha
= NULL
;
1036 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1039 inflateEnd(&png_ptr
->zstream
);
1041 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1042 png_free(png_ptr
, png_ptr
->save_buffer
);
1043 png_ptr
->save_buffer
= NULL
;
1046 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
1047 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1048 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1049 png_ptr
->unknown_chunk
.data
= NULL
;
1052 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1053 png_free(png_ptr
, png_ptr
->chunk_list
);
1054 png_ptr
->chunk_list
= NULL
;
1057 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
1058 * callbacks are still set at this point. They are required to complete the
1059 * destruction of the png_struct itself.
1063 /* Free all memory used by the read */
1065 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1066 png_infopp end_info_ptr_ptr
)
1068 png_structrp png_ptr
= NULL
;
1070 png_debug(1, "in png_destroy_read_struct");
1072 if (png_ptr_ptr
!= NULL
)
1073 png_ptr
= *png_ptr_ptr
;
1075 if (png_ptr
== NULL
)
1078 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1079 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1080 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1082 png_destroy_info_struct(png_ptr
, end_info_ptr_ptr
);
1083 png_destroy_info_struct(png_ptr
, info_ptr_ptr
);
1085 *png_ptr_ptr
= NULL
;
1086 png_read_destroy(png_ptr
);
1087 png_destroy_png_struct(png_ptr
);
1091 png_set_read_status_fn(png_structrp png_ptr
, png_read_status_ptr read_row_fn
)
1093 if (png_ptr
== NULL
)
1096 png_ptr
->read_row_fn
= read_row_fn
;
1100 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1101 #ifdef PNG_INFO_IMAGE_SUPPORTED
1103 png_read_png(png_structrp png_ptr
, png_inforp info_ptr
,
1107 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1110 /* png_read_info() gives us all of the information from the
1111 * PNG file before the first IDAT (image data chunk).
1113 png_read_info(png_ptr
, info_ptr
);
1114 if (info_ptr
->height
> PNG_UINT_32_MAX
/(sizeof (png_bytep
)))
1115 png_error(png_ptr
, "Image is too high to process with png_read_png()");
1117 /* -------------- image transformations start here ------------------- */
1118 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1119 * is not implemented. This will only happen in de-configured (non-default)
1120 * libpng builds. The results can be unexpected - png_read_png may return
1121 * short or mal-formed rows because the transform is skipped.
1124 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1126 if ((transforms
& PNG_TRANSFORM_SCALE_16
) != 0)
1127 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1128 * did in earlier versions, while "scale_16" is now more accurate.
1130 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1131 png_set_scale_16(png_ptr
);
1133 png_app_error(png_ptr
, "PNG_TRANSFORM_SCALE_16 not supported");
1136 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1137 * latter by doing SCALE first. This is ok and allows apps not to check for
1138 * which is supported to get the right answer.
1140 if ((transforms
& PNG_TRANSFORM_STRIP_16
) != 0)
1141 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1142 png_set_strip_16(png_ptr
);
1144 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_16 not supported");
1147 /* Strip alpha bytes from the input data without combining with
1148 * the background (not recommended).
1150 if ((transforms
& PNG_TRANSFORM_STRIP_ALPHA
) != 0)
1151 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1152 png_set_strip_alpha(png_ptr
);
1154 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_ALPHA not supported");
1157 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1158 * byte into separate bytes (useful for paletted and grayscale images).
1160 if ((transforms
& PNG_TRANSFORM_PACKING
) != 0)
1161 #ifdef PNG_READ_PACK_SUPPORTED
1162 png_set_packing(png_ptr
);
1164 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKING not supported");
1167 /* Change the order of packed pixels to least significant bit first
1168 * (not useful if you are using png_set_packing).
1170 if ((transforms
& PNG_TRANSFORM_PACKSWAP
) != 0)
1171 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1172 png_set_packswap(png_ptr
);
1174 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKSWAP not supported");
1177 /* Expand paletted colors into true RGB triplets
1178 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1179 * Expand paletted or RGB images with transparency to full alpha
1180 * channels so the data will be available as RGBA quartets.
1182 if ((transforms
& PNG_TRANSFORM_EXPAND
) != 0)
1183 #ifdef PNG_READ_EXPAND_SUPPORTED
1184 png_set_expand(png_ptr
);
1186 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND not supported");
1189 /* We don't handle background color or gamma transformation or quantizing.
1192 /* Invert monochrome files to have 0 as white and 1 as black
1194 if ((transforms
& PNG_TRANSFORM_INVERT_MONO
) != 0)
1195 #ifdef PNG_READ_INVERT_SUPPORTED
1196 png_set_invert_mono(png_ptr
);
1198 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_MONO not supported");
1201 /* If you want to shift the pixel values from the range [0,255] or
1202 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1203 * colors were originally in:
1205 if ((transforms
& PNG_TRANSFORM_SHIFT
) != 0)
1206 #ifdef PNG_READ_SHIFT_SUPPORTED
1207 if ((info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
1208 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1210 png_app_error(png_ptr
, "PNG_TRANSFORM_SHIFT not supported");
1213 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1214 if ((transforms
& PNG_TRANSFORM_BGR
) != 0)
1215 #ifdef PNG_READ_BGR_SUPPORTED
1216 png_set_bgr(png_ptr
);
1218 png_app_error(png_ptr
, "PNG_TRANSFORM_BGR not supported");
1221 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1222 if ((transforms
& PNG_TRANSFORM_SWAP_ALPHA
) != 0)
1223 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1224 png_set_swap_alpha(png_ptr
);
1226 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1229 /* Swap bytes of 16-bit files to least significant byte first */
1230 if ((transforms
& PNG_TRANSFORM_SWAP_ENDIAN
) != 0)
1231 #ifdef PNG_READ_SWAP_SUPPORTED
1232 png_set_swap(png_ptr
);
1234 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1237 /* Added at libpng-1.2.41 */
1238 /* Invert the alpha channel from opacity to transparency */
1239 if ((transforms
& PNG_TRANSFORM_INVERT_ALPHA
) != 0)
1240 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1241 png_set_invert_alpha(png_ptr
);
1243 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1246 /* Added at libpng-1.2.41 */
1247 /* Expand grayscale image to RGB */
1248 if ((transforms
& PNG_TRANSFORM_GRAY_TO_RGB
) != 0)
1249 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1250 png_set_gray_to_rgb(png_ptr
);
1252 png_app_error(png_ptr
, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1255 /* Added at libpng-1.5.4 */
1256 if ((transforms
& PNG_TRANSFORM_EXPAND_16
) != 0)
1257 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1258 png_set_expand_16(png_ptr
);
1260 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND_16 not supported");
1263 /* We don't handle adding filler bytes */
1265 /* We use png_read_image and rely on that for interlace handling, but we also
1266 * call png_read_update_info therefore must turn on interlace handling now:
1268 (void)png_set_interlace_handling(png_ptr
);
1270 /* Optional call to gamma correct and add the background to the palette
1271 * and update info structure. REQUIRED if you are expecting libpng to
1272 * update the palette for you (i.e., you selected such a transform above).
1274 png_read_update_info(png_ptr
, info_ptr
);
1276 /* -------------- image transformations end here ------------------- */
1278 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1279 if (info_ptr
->row_pointers
== NULL
)
1283 info_ptr
->row_pointers
= png_voidcast(png_bytepp
, png_malloc(png_ptr
,
1284 info_ptr
->height
* (sizeof (png_bytep
))));
1286 for (iptr
=0; iptr
<info_ptr
->height
; iptr
++)
1287 info_ptr
->row_pointers
[iptr
] = NULL
;
1289 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1291 for (iptr
= 0; iptr
< info_ptr
->height
; iptr
++)
1292 info_ptr
->row_pointers
[iptr
] = png_voidcast(png_bytep
,
1293 png_malloc(png_ptr
, info_ptr
->rowbytes
));
1296 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1297 info_ptr
->valid
|= PNG_INFO_IDAT
;
1299 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1300 png_read_end(png_ptr
, info_ptr
);
1304 #endif /* INFO_IMAGE */
1305 #endif /* SEQUENTIAL_READ */
1307 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1310 * This code currently relies on the sequential reader, though it could easily
1311 * be made to work with the progressive one.
1313 /* Arguments to png_image_finish_read: */
1315 /* Encoding of PNG data (used by the color-map code) */
1316 # define P_NOTSET 0 /* File encoding not yet known */
1317 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1318 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1319 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1320 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1322 /* Color-map processing: after libpng has run on the PNG image further
1323 * processing may be needed to convert the data to color-map indices.
1325 #define PNG_CMAP_NONE 0
1326 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1327 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1328 #define PNG_CMAP_RGB 3 /* Process RGB data */
1329 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1331 /* The following document where the background is for each processing case. */
1332 #define PNG_CMAP_NONE_BACKGROUND 256
1333 #define PNG_CMAP_GA_BACKGROUND 231
1334 #define PNG_CMAP_TRANS_BACKGROUND 254
1335 #define PNG_CMAP_RGB_BACKGROUND 256
1336 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1343 png_int_32 row_stride
;
1345 png_const_colorp background
;
1346 /* Local variables: */
1347 png_voidp local_row
;
1348 png_voidp first_row
;
1349 ptrdiff_t row_bytes
; /* step between rows */
1350 int file_encoding
; /* E_ values above */
1351 png_fixed_point gamma_to_linear
; /* For P_FILE, reciprocal of gamma */
1352 int colormap_processing
; /* PNG_CMAP_ values above */
1353 } png_image_read_control
;
1355 /* Do all the *safe* initialization - 'safe' means that png_error won't be
1356 * called, so setting up the jmp_buf is not required. This means that anything
1357 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1358 * instead so that control is returned safely back to this routine.
1361 png_image_read_init(png_imagep image
)
1363 if (image
->opaque
== NULL
)
1365 png_structp png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, image
,
1366 png_safe_error
, png_safe_warning
);
1368 /* And set the rest of the structure to NULL to ensure that the various
1369 * fields are consistent.
1371 memset(image
, 0, (sizeof *image
));
1372 image
->version
= PNG_IMAGE_VERSION
;
1374 if (png_ptr
!= NULL
)
1376 png_infop info_ptr
= png_create_info_struct(png_ptr
);
1378 if (info_ptr
!= NULL
)
1380 png_controlp control
= png_voidcast(png_controlp
,
1381 png_malloc_warn(png_ptr
, (sizeof *control
)));
1383 if (control
!= NULL
)
1385 memset(control
, 0, (sizeof *control
));
1387 control
->png_ptr
= png_ptr
;
1388 control
->info_ptr
= info_ptr
;
1389 control
->for_write
= 0;
1391 image
->opaque
= control
;
1395 /* Error clean up */
1396 png_destroy_info_struct(png_ptr
, &info_ptr
);
1399 png_destroy_read_struct(&png_ptr
, NULL
, NULL
);
1402 return png_image_error(image
, "png_image_read: out of memory");
1405 return png_image_error(image
, "png_image_read: opaque pointer not NULL");
1408 /* Utility to find the base format of a PNG file from a png_struct. */
1410 png_image_format(png_structrp png_ptr
)
1412 png_uint_32 format
= 0;
1414 if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
1415 format
|= PNG_FORMAT_FLAG_COLOR
;
1417 if ((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
1418 format
|= PNG_FORMAT_FLAG_ALPHA
;
1420 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1421 * sets the png_struct fields; that's all we are interested in here. The
1422 * precise interaction with an app call to png_set_tRNS and PNG file reading
1425 else if (png_ptr
->num_trans
> 0)
1426 format
|= PNG_FORMAT_FLAG_ALPHA
;
1428 if (png_ptr
->bit_depth
== 16)
1429 format
|= PNG_FORMAT_FLAG_LINEAR
;
1431 if ((png_ptr
->color_type
& PNG_COLOR_MASK_PALETTE
) != 0)
1432 format
|= PNG_FORMAT_FLAG_COLORMAP
;
1437 /* Is the given gamma significantly different from sRGB? The test is the same
1438 * one used in pngrtran.c when deciding whether to do gamma correction. The
1439 * arithmetic optimizes the division by using the fact that the inverse of the
1440 * file sRGB gamma is 2.2
1443 png_gamma_not_sRGB(png_fixed_point g
)
1447 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1451 return png_gamma_significant((g
* 11 + 2)/5 /* i.e. *2.2, rounded */);
1457 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
1458 * header and fill in all the information. This is executed in a safe context,
1459 * unlike the init routine above.
1462 png_image_read_header(png_voidp argument
)
1464 png_imagep image
= png_voidcast(png_imagep
, argument
);
1465 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1466 png_inforp info_ptr
= image
->opaque
->info_ptr
;
1468 png_set_benign_errors(png_ptr
, 1/*warn*/);
1469 png_read_info(png_ptr
, info_ptr
);
1471 /* Do this the fast way; just read directly out of png_struct. */
1472 image
->width
= png_ptr
->width
;
1473 image
->height
= png_ptr
->height
;
1476 png_uint_32 format
= png_image_format(png_ptr
);
1478 image
->format
= format
;
1480 #ifdef PNG_COLORSPACE_SUPPORTED
1481 /* Does the colorspace match sRGB? If there is no color endpoint
1482 * (colorant) information assume yes, otherwise require the
1483 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1484 * colorspace has been determined to be invalid ignore it.
1486 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0 && ((png_ptr
->colorspace
.flags
1487 & (PNG_COLORSPACE_HAVE_ENDPOINTS
|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB
|
1488 PNG_COLORSPACE_INVALID
)) == PNG_COLORSPACE_HAVE_ENDPOINTS
))
1489 image
->flags
|= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
;
1493 /* We need the maximum number of entries regardless of the format the
1494 * application sets here.
1497 png_uint_32 cmap_entries
;
1499 switch (png_ptr
->color_type
)
1501 case PNG_COLOR_TYPE_GRAY
:
1502 cmap_entries
= 1U << png_ptr
->bit_depth
;
1505 case PNG_COLOR_TYPE_PALETTE
:
1506 cmap_entries
= png_ptr
->num_palette
;
1514 if (cmap_entries
> 256)
1517 image
->colormap_entries
= cmap_entries
;
1523 #ifdef PNG_STDIO_SUPPORTED
1525 png_image_begin_read_from_stdio(png_imagep image
, FILE* file
)
1527 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1531 if (png_image_read_init(image
) != 0)
1533 /* This is slightly evil, but png_init_io doesn't do anything other
1534 * than this and we haven't changed the standard IO functions so
1535 * this saves a 'safe' function.
1537 image
->opaque
->png_ptr
->io_ptr
= file
;
1538 return png_safe_execute(image
, png_image_read_header
, image
);
1543 return png_image_error(image
,
1544 "png_image_begin_read_from_stdio: invalid argument");
1547 else if (image
!= NULL
)
1548 return png_image_error(image
,
1549 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1555 png_image_begin_read_from_file(png_imagep image
, const char *file_name
)
1557 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1559 if (file_name
!= NULL
)
1561 FILE *fp
= fopen(file_name
, "rb");
1565 if (png_image_read_init(image
) != 0)
1567 image
->opaque
->png_ptr
->io_ptr
= fp
;
1568 image
->opaque
->owned_file
= 1;
1569 return png_safe_execute(image
, png_image_read_header
, image
);
1572 /* Clean up: just the opened file. */
1577 return png_image_error(image
, strerror(errno
));
1581 return png_image_error(image
,
1582 "png_image_begin_read_from_file: invalid argument");
1585 else if (image
!= NULL
)
1586 return png_image_error(image
,
1587 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1593 static void PNGCBAPI
1594 png_image_memory_read(png_structp png_ptr
, png_bytep out
, png_size_t need
)
1596 if (png_ptr
!= NULL
)
1598 png_imagep image
= png_voidcast(png_imagep
, png_ptr
->io_ptr
);
1601 png_controlp cp
= image
->opaque
;
1604 png_const_bytep memory
= cp
->memory
;
1605 png_size_t size
= cp
->size
;
1607 if (memory
!= NULL
&& size
>= need
)
1609 memcpy(out
, memory
, need
);
1610 cp
->memory
= memory
+ need
;
1611 cp
->size
= size
- need
;
1615 png_error(png_ptr
, "read beyond end of data");
1619 png_error(png_ptr
, "invalid memory read");
1623 int PNGAPI
png_image_begin_read_from_memory(png_imagep image
,
1624 png_const_voidp memory
, png_size_t size
)
1626 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1628 if (memory
!= NULL
&& size
> 0)
1630 if (png_image_read_init(image
) != 0)
1632 /* Now set the IO functions to read from the memory buffer and
1633 * store it into io_ptr. Again do this in-place to avoid calling a
1634 * libpng function that requires error handling.
1636 image
->opaque
->memory
= png_voidcast(png_const_bytep
, memory
);
1637 image
->opaque
->size
= size
;
1638 image
->opaque
->png_ptr
->io_ptr
= image
;
1639 image
->opaque
->png_ptr
->read_data_fn
= png_image_memory_read
;
1641 return png_safe_execute(image
, png_image_read_header
, image
);
1646 return png_image_error(image
,
1647 "png_image_begin_read_from_memory: invalid argument");
1650 else if (image
!= NULL
)
1651 return png_image_error(image
,
1652 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1657 /* Utility function to skip chunks that are not used by the simplified image
1658 * read functions and an appropriate macro to call it.
1660 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1662 png_image_skip_unused_chunks(png_structrp png_ptr
)
1664 /* Prepare the reader to ignore all recognized chunks whose data will not
1665 * be used, i.e., all chunks recognized by libpng except for those
1666 * involved in basic image reading:
1668 * IHDR, PLTE, IDAT, IEND
1670 * Or image data handling:
1672 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1674 * This provides a small performance improvement and eliminates any
1675 * potential vulnerability to security problems in the unused chunks.
1677 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1678 * too. This allows the simplified API to be compiled without iCCP support,
1679 * however if the support is there the chunk is still checked to detect
1680 * errors (which are unfortunately quite common.)
1683 static PNG_CONST png_byte chunks_to_process
[] = {
1684 98, 75, 71, 68, '\0', /* bKGD */
1685 99, 72, 82, 77, '\0', /* cHRM */
1686 103, 65, 77, 65, '\0', /* gAMA */
1687 # ifdef PNG_READ_iCCP_SUPPORTED
1688 105, 67, 67, 80, '\0', /* iCCP */
1690 115, 66, 73, 84, '\0', /* sBIT */
1691 115, 82, 71, 66, '\0', /* sRGB */
1694 /* Ignore unknown chunks and all other chunks except for the
1695 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1697 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_NEVER
,
1700 /* But do not ignore image data handling chunks */
1701 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_AS_DEFAULT
,
1702 chunks_to_process
, (int)/*SAFE*/(sizeof chunks_to_process
)/5);
1706 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1708 # define PNG_SKIP_CHUNKS(p) ((void)0)
1709 #endif /* HANDLE_AS_UNKNOWN */
1711 /* The following macro gives the exact rounded answer for all values in the
1712 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1713 * the correct numbers 0..5
1715 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1717 /* Utility functions to make particular color-maps */
1719 set_file_encoding(png_image_read_control
*display
)
1721 png_fixed_point g
= display
->image
->opaque
->png_ptr
->colorspace
.gamma
;
1722 if (png_gamma_significant(g
) != 0)
1724 if (png_gamma_not_sRGB(g
) != 0)
1726 display
->file_encoding
= P_FILE
;
1727 display
->gamma_to_linear
= png_reciprocal(g
);
1731 display
->file_encoding
= P_sRGB
;
1735 display
->file_encoding
= P_LINEAR8
;
1739 decode_gamma(png_image_read_control
*display
, png_uint_32 value
, int encoding
)
1741 if (encoding
== P_FILE
) /* double check */
1742 encoding
= display
->file_encoding
;
1744 if (encoding
== P_NOTSET
) /* must be the file encoding */
1746 set_file_encoding(display
);
1747 encoding
= display
->file_encoding
;
1753 value
= png_gamma_16bit_correct(value
*257, display
->gamma_to_linear
);
1757 value
= png_sRGB_table
[value
];
1768 png_error(display
->image
->opaque
->png_ptr
,
1769 "unexpected encoding (internal error)");
1777 png_colormap_compose(png_image_read_control
*display
,
1778 png_uint_32 foreground
, int foreground_encoding
, png_uint_32 alpha
,
1779 png_uint_32 background
, int encoding
)
1781 /* The file value is composed on the background, the background has the given
1782 * encoding and so does the result, the file is encoded with P_FILE and the
1783 * file and alpha are 8-bit values. The (output) encoding will always be
1784 * P_LINEAR or P_sRGB.
1786 png_uint_32 f
= decode_gamma(display
, foreground
, foreground_encoding
);
1787 png_uint_32 b
= decode_gamma(display
, background
, encoding
);
1789 /* The alpha is always an 8-bit value (it comes from the palette), the value
1790 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1792 f
= f
* alpha
+ b
* (255-alpha
);
1794 if (encoding
== P_LINEAR
)
1796 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1797 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1799 f
*= 257; /* Now scaled by 65535 */
1801 f
= (f
+32768) >> 16;
1805 f
= PNG_sRGB_FROM_LINEAR(f
);
1810 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1814 png_create_colormap_entry(png_image_read_control
*display
,
1815 png_uint_32 ip
, png_uint_32 red
, png_uint_32 green
, png_uint_32 blue
,
1816 png_uint_32 alpha
, int encoding
)
1818 png_imagep image
= display
->image
;
1819 const int output_encoding
= (image
->format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
1821 const int convert_to_Y
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) == 0 &&
1822 (red
!= green
|| green
!= blue
);
1825 png_error(image
->opaque
->png_ptr
, "color-map index out of range");
1827 /* Update the cache with whether the file gamma is significantly different
1830 if (encoding
== P_FILE
)
1832 if (display
->file_encoding
== P_NOTSET
)
1833 set_file_encoding(display
);
1835 /* Note that the cached value may be P_FILE too, but if it is then the
1836 * gamma_to_linear member has been set.
1838 encoding
= display
->file_encoding
;
1841 if (encoding
== P_FILE
)
1843 png_fixed_point g
= display
->gamma_to_linear
;
1845 red
= png_gamma_16bit_correct(red
*257, g
);
1846 green
= png_gamma_16bit_correct(green
*257, g
);
1847 blue
= png_gamma_16bit_correct(blue
*257, g
);
1849 if (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
)
1852 encoding
= P_LINEAR
;
1857 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1858 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1859 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1864 else if (encoding
== P_LINEAR8
)
1866 /* This encoding occurs quite frequently in test cases because PngSuite
1867 * includes a gAMA 1.0 chunk with most images.
1873 encoding
= P_LINEAR
;
1876 else if (encoding
== P_sRGB
&&
1877 (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
))
1879 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1882 red
= png_sRGB_table
[red
];
1883 green
= png_sRGB_table
[green
];
1884 blue
= png_sRGB_table
[blue
];
1886 encoding
= P_LINEAR
;
1889 /* This is set if the color isn't gray but the output is. */
1890 if (encoding
== P_LINEAR
)
1892 if (convert_to_Y
!= 0)
1894 /* NOTE: these values are copied from png_do_rgb_to_gray */
1895 png_uint_32 y
= (png_uint_32
)6968 * red
+ (png_uint_32
)23434 * green
+
1896 (png_uint_32
)2366 * blue
;
1898 if (output_encoding
== P_LINEAR
)
1899 y
= (y
+ 16384) >> 15;
1903 /* y is scaled by 32768, we need it scaled by 255: */
1906 y
= PNG_sRGB_FROM_LINEAR((y
+ 64) >> 7);
1910 blue
= red
= green
= y
;
1913 else if (output_encoding
== P_sRGB
)
1915 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1916 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1917 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1918 alpha
= PNG_DIV257(alpha
);
1923 if (encoding
!= output_encoding
)
1924 png_error(image
->opaque
->png_ptr
, "bad encoding (internal error)");
1926 /* Store the value. */
1928 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1929 const int afirst
= (image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0 &&
1930 (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
1934 # ifdef PNG_FORMAT_BGR_SUPPORTED
1935 const int bgr
= (image
->format
& PNG_FORMAT_FLAG_BGR
) != 0 ? 2 : 0;
1940 if (output_encoding
== P_LINEAR
)
1942 png_uint_16p entry
= png_voidcast(png_uint_16p
, display
->colormap
);
1944 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
1946 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1947 * value, if less than 65535 (this is, effectively, composite on black
1948 * if the alpha channel is removed.)
1950 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
1953 entry
[afirst
? 0 : 3] = (png_uint_16
)alpha
;
1961 blue
= (blue
* alpha
+ 32767U)/65535U;
1962 green
= (green
* alpha
+ 32767U)/65535U;
1963 red
= (red
* alpha
+ 32767U)/65535U;
1967 red
= green
= blue
= 0;
1969 entry
[afirst
+ (2 ^ bgr
)] = (png_uint_16
)blue
;
1970 entry
[afirst
+ 1] = (png_uint_16
)green
;
1971 entry
[afirst
+ bgr
] = (png_uint_16
)red
;
1975 entry
[1 ^ afirst
] = (png_uint_16
)alpha
;
1982 green
= (green
* alpha
+ 32767U)/65535U;
1987 entry
[afirst
] = (png_uint_16
)green
;
1995 else /* output encoding is P_sRGB */
1997 png_bytep entry
= png_voidcast(png_bytep
, display
->colormap
);
1999 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
2001 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
2004 entry
[afirst
? 0 : 3] = (png_byte
)alpha
;
2006 entry
[afirst
+ (2 ^ bgr
)] = (png_byte
)blue
;
2007 entry
[afirst
+ 1] = (png_byte
)green
;
2008 entry
[afirst
+ bgr
] = (png_byte
)red
;
2012 entry
[1 ^ afirst
] = (png_byte
)alpha
;
2014 entry
[afirst
] = (png_byte
)green
;
2032 make_gray_file_colormap(png_image_read_control
*display
)
2036 for (i
=0; i
<256; ++i
)
2037 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_FILE
);
2043 make_gray_colormap(png_image_read_control
*display
)
2047 for (i
=0; i
<256; ++i
)
2048 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_sRGB
);
2052 #define PNG_GRAY_COLORMAP_ENTRIES 256
2055 make_ga_colormap(png_image_read_control
*display
)
2059 /* Alpha is retained, the output will be a color-map with entries
2060 * selected by six levels of alpha. One transparent entry, 6 gray
2061 * levels for all the intermediate alpha values, leaving 230 entries
2062 * for the opaque grays. The color-map entries are the six values
2063 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2066 * if (alpha > 229) // opaque
2068 * // The 231 entries are selected to make the math below work:
2070 * entry = (231 * gray + 128) >> 8;
2072 * else if (alpha < 26) // transparent
2077 * else // partially opaque
2079 * base = 226 + 6 * PNG_DIV51(alpha);
2080 * entry = PNG_DIV51(gray);
2086 unsigned int gray
= (i
* 256 + 115) / 231;
2087 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
, 255, P_sRGB
);
2090 /* 255 is used here for the component values for consistency with the code
2091 * that undoes premultiplication in pngwrite.c.
2093 png_create_colormap_entry(display
, i
++, 255, 255, 255, 0, P_sRGB
);
2100 png_create_colormap_entry(display
, i
++, g
*51, g
*51, g
*51, a
*51,
2107 #define PNG_GA_COLORMAP_ENTRIES 256
2110 make_rgb_colormap(png_image_read_control
*display
)
2114 /* Build a 6x6x6 opaque RGB cube */
2115 for (i
=r
=0; r
<6; ++r
)
2124 png_create_colormap_entry(display
, i
++, r
*51, g
*51, b
*51, 255,
2132 #define PNG_RGB_COLORMAP_ENTRIES 216
2134 /* Return a palette index to the above palette given three 8-bit sRGB values. */
2135 #define PNG_RGB_INDEX(r,g,b) \
2136 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2139 png_image_read_colormap(png_voidp argument
)
2141 png_image_read_control
*display
=
2142 png_voidcast(png_image_read_control
*, argument
);
2143 const png_imagep image
= display
->image
;
2145 const png_structrp png_ptr
= image
->opaque
->png_ptr
;
2146 const png_uint_32 output_format
= image
->format
;
2147 const int output_encoding
= (output_format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
2150 unsigned int cmap_entries
;
2151 unsigned int output_processing
; /* Output processing option */
2152 unsigned int data_encoding
= P_NOTSET
; /* Encoding libpng must produce */
2154 /* Background information; the background color and the index of this color
2155 * in the color-map if it exists (else 256).
2157 unsigned int background_index
= 256;
2158 png_uint_32 back_r
, back_g
, back_b
;
2160 /* Flags to accumulate things that need to be done to the input. */
2161 int expand_tRNS
= 0;
2163 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2164 * very difficult to do, the results look awful, and it is difficult to see
2165 * what possible use it is because the application can't control the
2168 if (((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0 ||
2169 png_ptr
->num_trans
> 0) /* alpha in input */ &&
2170 ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0) /* no alpha in output */)
2172 if (output_encoding
== P_LINEAR
) /* compose on black */
2173 back_b
= back_g
= back_r
= 0;
2175 else if (display
->background
== NULL
/* no way to remove it */)
2177 "a background color must be supplied to remove alpha/transparency");
2179 /* Get a copy of the background color (this avoids repeating the checks
2180 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2185 back_g
= display
->background
->green
;
2186 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) != 0)
2188 back_r
= display
->background
->red
;
2189 back_b
= display
->background
->blue
;
2192 back_b
= back_r
= back_g
;
2196 else if (output_encoding
== P_LINEAR
)
2197 back_b
= back_r
= back_g
= 65535;
2200 back_b
= back_r
= back_g
= 255;
2202 /* Default the input file gamma if required - this is necessary because
2203 * libpng assumes that if no gamma information is present the data is in the
2204 * output format, but the simplified API deduces the gamma from the input
2207 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_GAMMA
) == 0)
2209 /* Do this directly, not using the png_colorspace functions, to ensure
2210 * that it happens even if the colorspace is invalid (though probably if
2211 * it is the setting will be ignored) Note that the same thing can be
2212 * achieved at the application interface with png_set_gAMA.
2214 if (png_ptr
->bit_depth
== 16 &&
2215 (image
->flags
& PNG_IMAGE_FLAG_16BIT_sRGB
) == 0)
2216 png_ptr
->colorspace
.gamma
= PNG_GAMMA_LINEAR
;
2219 png_ptr
->colorspace
.gamma
= PNG_GAMMA_sRGB_INVERSE
;
2221 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
2224 /* Decide what to do based on the PNG color type of the input data. The
2225 * utility function png_create_colormap_entry deals with most aspects of the
2226 * output transformations; this code works out how to produce bytes of
2227 * color-map entries from the original format.
2229 switch (png_ptr
->color_type
)
2231 case PNG_COLOR_TYPE_GRAY
:
2232 if (png_ptr
->bit_depth
<= 8)
2234 /* There at most 256 colors in the output, regardless of
2237 unsigned int step
, i
, val
, trans
= 256/*ignore*/, back_alpha
= 0;
2239 cmap_entries
= 1U << png_ptr
->bit_depth
;
2240 if (cmap_entries
> image
->colormap_entries
)
2241 png_error(png_ptr
, "gray[8] color-map: too few entries");
2243 step
= 255 / (cmap_entries
- 1);
2244 output_processing
= PNG_CMAP_NONE
;
2246 /* If there is a tRNS chunk then this either selects a transparent
2247 * value or, if the output has no alpha, the background color.
2249 if (png_ptr
->num_trans
> 0)
2251 trans
= png_ptr
->trans_color
.gray
;
2253 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0)
2254 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2257 /* png_create_colormap_entry just takes an RGBA and writes the
2258 * corresponding color-map entry using the format from 'image',
2259 * including the required conversion to sRGB or linear as
2260 * appropriate. The input values are always either sRGB (if the
2261 * gamma correction flag is 0) or 0..255 scaled file encoded values
2262 * (if the function must gamma correct them).
2264 for (i
=val
=0; i
<cmap_entries
; ++i
, val
+= step
)
2266 /* 'i' is a file value. While this will result in duplicated
2267 * entries for 8-bit non-sRGB encoded files it is necessary to
2268 * have non-gamma corrected values to do tRNS handling.
2271 png_create_colormap_entry(display
, i
, val
, val
, val
, 255,
2272 P_FILE
/*8-bit with file gamma*/);
2274 /* Else this entry is transparent. The colors don't matter if
2275 * there is an alpha channel (back_alpha == 0), but it does no
2276 * harm to pass them in; the values are not set above so this
2279 * NOTE: this preserves the full precision of the application
2280 * supplied background color when it is used.
2283 png_create_colormap_entry(display
, i
, back_r
, back_g
, back_b
,
2284 back_alpha
, output_encoding
);
2287 /* We need libpng to preserve the original encoding. */
2288 data_encoding
= P_FILE
;
2290 /* The rows from libpng, while technically gray values, are now also
2291 * color-map indices; however, they may need to be expanded to 1
2292 * byte per pixel. This is what png_set_packing does (i.e., it
2293 * unpacks the bit values into bytes.)
2295 if (png_ptr
->bit_depth
< 8)
2296 png_set_packing(png_ptr
);
2299 else /* bit depth is 16 */
2301 /* The 16-bit input values can be converted directly to 8-bit gamma
2302 * encoded values; however, if a tRNS chunk is present 257 color-map
2303 * entries are required. This means that the extra entry requires
2304 * special processing; add an alpha channel, sacrifice gray level
2305 * 254 and convert transparent (alpha==0) entries to that.
2307 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2308 * same time to minimize quality loss. If a tRNS chunk is present
2309 * this means libpng must handle it too; otherwise it is impossible
2310 * to do the exact match on the 16-bit value.
2312 * If the output has no alpha channel *and* the background color is
2313 * gray then it is possible to let libpng handle the substitution by
2314 * ensuring that the corresponding gray level matches the background
2317 data_encoding
= P_sRGB
;
2319 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2320 png_error(png_ptr
, "gray[16] color-map: too few entries");
2322 cmap_entries
= make_gray_colormap(display
);
2324 if (png_ptr
->num_trans
> 0)
2326 unsigned int back_alpha
;
2328 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2333 if (back_r
== back_g
&& back_g
== back_b
)
2335 /* Background is gray; no special processing will be
2339 png_uint_32 gray
= back_g
;
2341 if (output_encoding
== P_LINEAR
)
2343 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2345 /* And make sure the corresponding palette entry
2348 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2349 back_g
, 65535, P_LINEAR
);
2352 /* The background passed to libpng, however, must be the
2355 c
.index
= 0; /*unused*/
2356 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2358 /* NOTE: does this work without expanding tRNS to alpha?
2359 * It should be the color->gray case below apparently
2362 png_set_background_fixed(png_ptr
, &c
,
2363 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2364 0/*gamma: not used*/);
2366 output_processing
= PNG_CMAP_NONE
;
2370 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2373 /* output_processing means that the libpng-processed row will be
2374 * 8-bit GA and it has to be processing to single byte color-map
2375 * values. Entry 254 is replaced by either a completely
2376 * transparent entry or by the background color at full
2377 * precision (and the background color is not a simple gray
2378 * level in this case.)
2381 output_processing
= PNG_CMAP_TRANS
;
2382 background_index
= 254;
2384 /* And set (overwrite) color-map entry 254 to the actual
2385 * background color at full precision.
2387 png_create_colormap_entry(display
, 254, back_r
, back_g
, back_b
,
2388 back_alpha
, output_encoding
);
2392 output_processing
= PNG_CMAP_NONE
;
2396 case PNG_COLOR_TYPE_GRAY_ALPHA
:
2397 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2398 * of 65536 combinations. If, however, the alpha channel is to be
2399 * removed there are only 256 possibilities if the background is gray.
2400 * (Otherwise there is a subset of the 65536 possibilities defined by
2401 * the triangle between black, white and the background color.)
2403 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2404 * worry about tRNS matching - tRNS is ignored if there is an alpha
2407 data_encoding
= P_sRGB
;
2409 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2411 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2412 png_error(png_ptr
, "gray+alpha color-map: too few entries");
2414 cmap_entries
= make_ga_colormap(display
);
2416 background_index
= PNG_CMAP_GA_BACKGROUND
;
2417 output_processing
= PNG_CMAP_GA
;
2420 else /* alpha is removed */
2422 /* Alpha must be removed as the PNG data is processed when the
2423 * background is a color because the G and A channels are
2424 * independent and the vector addition (non-parallel vectors) is a
2427 * This can be reduced to the same algorithm as above by making a
2428 * colormap containing gray levels (for the opaque grays), a
2429 * background entry (for a transparent pixel) and a set of four six
2430 * level color values, one set for each intermediate alpha value.
2431 * See the comments in make_ga_colormap for how this works in the
2432 * per-pixel processing.
2434 * If the background is gray, however, we only need a 256 entry gray
2435 * level color map. It is sufficient to make the entry generated
2436 * for the background color be exactly the color specified.
2438 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0 ||
2439 (back_r
== back_g
&& back_g
== back_b
))
2441 /* Background is gray; no special processing will be required. */
2443 png_uint_32 gray
= back_g
;
2445 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2446 png_error(png_ptr
, "gray-alpha color-map: too few entries");
2448 cmap_entries
= make_gray_colormap(display
);
2450 if (output_encoding
== P_LINEAR
)
2452 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2454 /* And make sure the corresponding palette entry matches. */
2455 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2456 back_g
, 65535, P_LINEAR
);
2459 /* The background passed to libpng, however, must be the sRGB
2462 c
.index
= 0; /*unused*/
2463 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2465 png_set_background_fixed(png_ptr
, &c
,
2466 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2467 0/*gamma: not used*/);
2469 output_processing
= PNG_CMAP_NONE
;
2476 /* This is the same as png_make_ga_colormap, above, except that
2477 * the entries are all opaque.
2479 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2480 png_error(png_ptr
, "ga-alpha color-map: too few entries");
2485 png_uint_32 gray
= (i
* 256 + 115) / 231;
2486 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
,
2490 /* NOTE: this preserves the full precision of the application
2493 background_index
= i
;
2494 png_create_colormap_entry(display
, i
++, back_r
, back_g
, back_b
,
2495 output_encoding
== P_LINEAR
? 65535U : 255U, output_encoding
);
2497 /* For non-opaque input composite on the sRGB background - this
2498 * requires inverting the encoding for each component. The input
2499 * is still converted to the sRGB encoding because this is a
2500 * reasonable approximate to the logarithmic curve of human
2501 * visual sensitivity, at least over the narrow range which PNG
2502 * represents. Consequently 'G' is always sRGB encoded, while
2503 * 'A' is linear. We need the linear background colors.
2505 if (output_encoding
== P_sRGB
) /* else already linear */
2507 /* This may produce a value not exactly matching the
2508 * background, but that's ok because these numbers are only
2509 * used when alpha != 0
2511 back_r
= png_sRGB_table
[back_r
];
2512 back_g
= png_sRGB_table
[back_g
];
2513 back_b
= png_sRGB_table
[back_b
];
2520 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2521 * by an 8-bit alpha value (0..255).
2523 png_uint_32 alpha
= 51 * a
;
2524 png_uint_32 back_rx
= (255-alpha
) * back_r
;
2525 png_uint_32 back_gx
= (255-alpha
) * back_g
;
2526 png_uint_32 back_bx
= (255-alpha
) * back_b
;
2530 png_uint_32 gray
= png_sRGB_table
[g
*51] * alpha
;
2532 png_create_colormap_entry(display
, i
++,
2533 PNG_sRGB_FROM_LINEAR(gray
+ back_rx
),
2534 PNG_sRGB_FROM_LINEAR(gray
+ back_gx
),
2535 PNG_sRGB_FROM_LINEAR(gray
+ back_bx
), 255, P_sRGB
);
2540 output_processing
= PNG_CMAP_GA
;
2545 case PNG_COLOR_TYPE_RGB
:
2546 case PNG_COLOR_TYPE_RGB_ALPHA
:
2547 /* Exclude the case where the output is gray; we can always handle this
2548 * with the cases above.
2550 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0)
2552 /* The color-map will be grayscale, so we may as well convert the
2553 * input RGB values to a simple grayscale and use the grayscale
2556 * NOTE: calling this apparently damages the recognition of the
2557 * transparent color in background color handling; call
2558 * png_set_tRNS_to_alpha before png_set_background_fixed.
2560 png_set_rgb_to_gray_fixed(png_ptr
, PNG_ERROR_ACTION_NONE
, -1,
2562 data_encoding
= P_sRGB
;
2564 /* The output will now be one or two 8-bit gray or gray+alpha
2565 * channels. The more complex case arises when the input has alpha.
2567 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2568 png_ptr
->num_trans
> 0) &&
2569 (output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2571 /* Both input and output have an alpha channel, so no background
2572 * processing is required; just map the GA bytes to the right
2577 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2578 png_error(png_ptr
, "rgb[ga] color-map: too few entries");
2580 cmap_entries
= make_ga_colormap(display
);
2581 background_index
= PNG_CMAP_GA_BACKGROUND
;
2582 output_processing
= PNG_CMAP_GA
;
2587 /* Either the input or the output has no alpha channel, so there
2588 * will be no non-opaque pixels in the color-map; it will just be
2591 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2592 png_error(png_ptr
, "rgb[gray] color-map: too few entries");
2594 /* Ideally this code would use libpng to do the gamma correction,
2595 * but if an input alpha channel is to be removed we will hit the
2596 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2597 * correction bug). Fix this by dropping the gamma correction in
2598 * this case and doing it in the palette; this will result in
2599 * duplicate palette entries, but that's better than the
2600 * alternative of double gamma correction.
2602 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2603 png_ptr
->num_trans
> 0) &&
2604 png_gamma_not_sRGB(png_ptr
->colorspace
.gamma
) != 0)
2606 cmap_entries
= make_gray_file_colormap(display
);
2607 data_encoding
= P_FILE
;
2611 cmap_entries
= make_gray_colormap(display
);
2613 /* But if the input has alpha or transparency it must be removed
2615 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2616 png_ptr
->num_trans
> 0)
2619 png_uint_32 gray
= back_g
;
2621 /* We need to ensure that the application background exists in
2622 * the colormap and that completely transparent pixels map to
2623 * it. Achieve this simply by ensuring that the entry
2624 * selected for the background really is the background color.
2626 if (data_encoding
== P_FILE
) /* from the fixup above */
2628 /* The app supplied a gray which is in output_encoding, we
2629 * need to convert it to a value of the input (P_FILE)
2630 * encoding then set this palette entry to the required
2633 if (output_encoding
== P_sRGB
)
2634 gray
= png_sRGB_table
[gray
]; /* now P_LINEAR */
2636 gray
= PNG_DIV257(png_gamma_16bit_correct(gray
,
2637 png_ptr
->colorspace
.gamma
)); /* now P_FILE */
2639 /* And make sure the corresponding palette entry contains
2640 * exactly the required sRGB value.
2642 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2643 back_g
, 0/*unused*/, output_encoding
);
2646 else if (output_encoding
== P_LINEAR
)
2648 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2650 /* And make sure the corresponding palette entry matches.
2652 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2653 back_g
, 0/*unused*/, P_LINEAR
);
2656 /* The background passed to libpng, however, must be the
2657 * output (normally sRGB) value.
2659 c
.index
= 0; /*unused*/
2660 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2662 /* NOTE: the following is apparently a bug in libpng. Without
2663 * it the transparent color recognition in
2664 * png_set_background_fixed seems to go wrong.
2667 png_set_background_fixed(png_ptr
, &c
,
2668 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2669 0/*gamma: not used*/);
2672 output_processing
= PNG_CMAP_NONE
;
2676 else /* output is color */
2678 /* We could use png_quantize here so long as there is no transparent
2679 * color or alpha; png_quantize ignores alpha. Easier overall just
2680 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2681 * Consequently we always want libpng to produce sRGB data.
2683 data_encoding
= P_sRGB
;
2685 /* Is there any transparency or alpha? */
2686 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2687 png_ptr
->num_trans
> 0)
2689 /* Is there alpha in the output too? If so all four channels are
2690 * processed into a special RGB cube with alpha support.
2692 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2696 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2697 png_error(png_ptr
, "rgb+alpha color-map: too few entries");
2699 cmap_entries
= make_rgb_colormap(display
);
2701 /* Add a transparent entry. */
2702 png_create_colormap_entry(display
, cmap_entries
, 255, 255,
2705 /* This is stored as the background index for the processing
2708 background_index
= cmap_entries
++;
2710 /* Add 27 r,g,b entries each with alpha 0.5. */
2711 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2715 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2719 /* This generates components with the values 0, 127 and
2722 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2723 png_create_colormap_entry(display
, cmap_entries
++,
2724 r
, g
, b
, 128, P_sRGB
);
2729 output_processing
= PNG_CMAP_RGB_ALPHA
;
2734 /* Alpha/transparency must be removed. The background must
2735 * exist in the color map (achieved by setting adding it after
2736 * the 666 color-map). If the standard processing code will
2737 * pick up this entry automatically that's all that is
2738 * required; libpng can be called to do the background
2741 unsigned int sample_size
=
2742 PNG_IMAGE_SAMPLE_SIZE(output_format
);
2743 png_uint_32 r
, g
, b
; /* sRGB background */
2745 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2746 png_error(png_ptr
, "rgb-alpha color-map: too few entries");
2748 cmap_entries
= make_rgb_colormap(display
);
2750 png_create_colormap_entry(display
, cmap_entries
, back_r
,
2751 back_g
, back_b
, 0/*unused*/, output_encoding
);
2753 if (output_encoding
== P_LINEAR
)
2755 r
= PNG_sRGB_FROM_LINEAR(back_r
* 255);
2756 g
= PNG_sRGB_FROM_LINEAR(back_g
* 255);
2757 b
= PNG_sRGB_FROM_LINEAR(back_b
* 255);
2767 /* Compare the newly-created color-map entry with the one the
2768 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2769 * match, add the new one and set this as the background
2772 if (memcmp((png_const_bytep
)display
->colormap
+
2773 sample_size
* cmap_entries
,
2774 (png_const_bytep
)display
->colormap
+
2775 sample_size
* PNG_RGB_INDEX(r
,g
,b
),
2778 /* The background color must be added. */
2779 background_index
= cmap_entries
++;
2781 /* Add 27 r,g,b entries each with created by composing with
2782 * the background at alpha 0.5.
2784 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2786 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2788 /* This generates components with the values 0, 127
2791 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2792 png_create_colormap_entry(display
, cmap_entries
++,
2793 png_colormap_compose(display
, r
, P_sRGB
, 128,
2794 back_r
, output_encoding
),
2795 png_colormap_compose(display
, g
, P_sRGB
, 128,
2796 back_g
, output_encoding
),
2797 png_colormap_compose(display
, b
, P_sRGB
, 128,
2798 back_b
, output_encoding
),
2799 0/*unused*/, output_encoding
);
2804 output_processing
= PNG_CMAP_RGB_ALPHA
;
2807 else /* background color is in the standard color-map */
2811 c
.index
= 0; /*unused*/
2812 c
.red
= (png_uint_16
)back_r
;
2813 c
.gray
= c
.green
= (png_uint_16
)back_g
;
2814 c
.blue
= (png_uint_16
)back_b
;
2816 png_set_background_fixed(png_ptr
, &c
,
2817 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2818 0/*gamma: not used*/);
2820 output_processing
= PNG_CMAP_RGB
;
2825 else /* no alpha or transparency in the input */
2827 /* Alpha in the output is irrelevant, simply map the opaque input
2828 * pixels to the 6x6x6 color-map.
2830 if (PNG_RGB_COLORMAP_ENTRIES
> image
->colormap_entries
)
2831 png_error(png_ptr
, "rgb color-map: too few entries");
2833 cmap_entries
= make_rgb_colormap(display
);
2834 output_processing
= PNG_CMAP_RGB
;
2839 case PNG_COLOR_TYPE_PALETTE
:
2840 /* It's already got a color-map. It may be necessary to eliminate the
2841 * tRNS entries though.
2844 unsigned int num_trans
= png_ptr
->num_trans
;
2845 png_const_bytep trans
= num_trans
> 0 ? png_ptr
->trans_alpha
: NULL
;
2846 png_const_colorp colormap
= png_ptr
->palette
;
2847 const int do_background
= trans
!= NULL
&&
2848 (output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0;
2855 output_processing
= PNG_CMAP_NONE
;
2856 data_encoding
= P_FILE
; /* Don't change from color-map indices */
2857 cmap_entries
= png_ptr
->num_palette
;
2858 if (cmap_entries
> 256)
2861 if (cmap_entries
> image
->colormap_entries
)
2862 png_error(png_ptr
, "palette color-map: too few entries");
2864 for (i
=0; i
< cmap_entries
; ++i
)
2866 if (do_background
!= 0 && i
< num_trans
&& trans
[i
] < 255)
2869 png_create_colormap_entry(display
, i
, back_r
, back_g
,
2870 back_b
, 0, output_encoding
);
2874 /* Must compose the PNG file color in the color-map entry
2875 * on the sRGB color in 'back'.
2877 png_create_colormap_entry(display
, i
,
2878 png_colormap_compose(display
, colormap
[i
].red
, P_FILE
,
2879 trans
[i
], back_r
, output_encoding
),
2880 png_colormap_compose(display
, colormap
[i
].green
, P_FILE
,
2881 trans
[i
], back_g
, output_encoding
),
2882 png_colormap_compose(display
, colormap
[i
].blue
, P_FILE
,
2883 trans
[i
], back_b
, output_encoding
),
2884 output_encoding
== P_LINEAR
? trans
[i
] * 257U :
2891 png_create_colormap_entry(display
, i
, colormap
[i
].red
,
2892 colormap
[i
].green
, colormap
[i
].blue
,
2893 i
< num_trans
? trans
[i
] : 255U, P_FILE
/*8-bit*/);
2896 /* The PNG data may have indices packed in fewer than 8 bits, it
2897 * must be expanded if so.
2899 if (png_ptr
->bit_depth
< 8)
2900 png_set_packing(png_ptr
);
2905 png_error(png_ptr
, "invalid PNG color type");
2910 /* Now deal with the output processing */
2911 if (expand_tRNS
!= 0 && png_ptr
->num_trans
> 0 &&
2912 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) == 0)
2913 png_set_tRNS_to_alpha(png_ptr
);
2915 switch (data_encoding
)
2918 png_error(png_ptr
, "bad data option (internal error)");
2922 /* Change to 8-bit sRGB */
2923 png_set_alpha_mode_fixed(png_ptr
, PNG_ALPHA_PNG
, PNG_GAMMA_sRGB
);
2927 if (png_ptr
->bit_depth
> 8)
2928 png_set_scale_16(png_ptr
);
2932 if (cmap_entries
> 256 || cmap_entries
> image
->colormap_entries
)
2933 png_error(png_ptr
, "color map overflow (BAD internal error)");
2935 image
->colormap_entries
= cmap_entries
;
2937 /* Double check using the recorded background index */
2938 switch (output_processing
)
2941 if (background_index
!= PNG_CMAP_NONE_BACKGROUND
)
2942 goto bad_background
;
2946 if (background_index
!= PNG_CMAP_GA_BACKGROUND
)
2947 goto bad_background
;
2950 case PNG_CMAP_TRANS
:
2951 if (background_index
>= cmap_entries
||
2952 background_index
!= PNG_CMAP_TRANS_BACKGROUND
)
2953 goto bad_background
;
2957 if (background_index
!= PNG_CMAP_RGB_BACKGROUND
)
2958 goto bad_background
;
2961 case PNG_CMAP_RGB_ALPHA
:
2962 if (background_index
!= PNG_CMAP_RGB_ALPHA_BACKGROUND
)
2963 goto bad_background
;
2967 png_error(png_ptr
, "bad processing option (internal error)");
2970 png_error(png_ptr
, "bad background index (internal error)");
2973 display
->colormap_processing
= output_processing
;
2978 /* The final part of the color-map read called from png_image_finish_read. */
2980 png_image_read_and_map(png_voidp argument
)
2982 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
2984 png_imagep image
= display
->image
;
2985 png_structrp png_ptr
= image
->opaque
->png_ptr
;
2988 /* Called when the libpng data must be transformed into the color-mapped
2989 * form. There is a local row buffer in display->local and this routine must
2990 * do the interlace handling.
2992 switch (png_ptr
->interlaced
)
2994 case PNG_INTERLACE_NONE
:
2998 case PNG_INTERLACE_ADAM7
:
2999 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3003 png_error(png_ptr
, "unknown interlace type");
3007 png_uint_32 height
= image
->height
;
3008 png_uint_32 width
= image
->width
;
3009 int proc
= display
->colormap_processing
;
3010 png_bytep first_row
= png_voidcast(png_bytep
, display
->first_row
);
3011 ptrdiff_t step_row
= display
->row_bytes
;
3014 for (pass
= 0; pass
< passes
; ++pass
)
3016 unsigned int startx
, stepx
, stepy
;
3019 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3021 /* The row may be empty for a short image: */
3022 if (PNG_PASS_COLS(width
, pass
) == 0)
3025 startx
= PNG_PASS_START_COL(pass
);
3026 stepx
= PNG_PASS_COL_OFFSET(pass
);
3027 y
= PNG_PASS_START_ROW(pass
);
3028 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3038 for (; y
<height
; y
+= stepy
)
3040 png_bytep inrow
= png_voidcast(png_bytep
, display
->local_row
);
3041 png_bytep outrow
= first_row
+ y
* step_row
;
3042 png_const_bytep end_row
= outrow
+ width
;
3044 /* Read read the libpng data into the temporary buffer. */
3045 png_read_row(png_ptr
, inrow
, NULL
);
3047 /* Now process the row according to the processing option, note
3048 * that the caller verifies that the format of the libpng output
3049 * data is as required.
3055 for (; outrow
< end_row
; outrow
+= stepx
)
3057 /* The data is always in the PNG order */
3058 unsigned int gray
= *inrow
++;
3059 unsigned int alpha
= *inrow
++;
3062 /* NOTE: this code is copied as a comment in
3063 * make_ga_colormap above. Please update the
3064 * comment if you change this code!
3066 if (alpha
> 229) /* opaque */
3068 entry
= (231 * gray
+ 128) >> 8;
3070 else if (alpha
< 26) /* transparent */
3074 else /* partially opaque */
3076 entry
= 226 + 6 * PNG_DIV51(alpha
) + PNG_DIV51(gray
);
3079 *outrow
= (png_byte
)entry
;
3083 case PNG_CMAP_TRANS
:
3084 for (; outrow
< end_row
; outrow
+= stepx
)
3086 png_byte gray
= *inrow
++;
3087 png_byte alpha
= *inrow
++;
3090 *outrow
= PNG_CMAP_TRANS_BACKGROUND
;
3092 else if (gray
!= PNG_CMAP_TRANS_BACKGROUND
)
3096 *outrow
= (png_byte
)(PNG_CMAP_TRANS_BACKGROUND
+1);
3101 for (; outrow
< end_row
; outrow
+= stepx
)
3103 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1], inrow
[2]);
3108 case PNG_CMAP_RGB_ALPHA
:
3109 for (; outrow
< end_row
; outrow
+= stepx
)
3111 unsigned int alpha
= inrow
[3];
3113 /* Because the alpha entries only hold alpha==0.5 values
3114 * split the processing at alpha==0.25 (64) and 0.75
3119 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1],
3122 else if (alpha
< 64)
3123 *outrow
= PNG_CMAP_RGB_ALPHA_BACKGROUND
;
3127 /* Likewise there are three entries for each of r, g
3128 * and b. We could select the entry by popcount on
3129 * the top two bits on those architectures that
3130 * support it, this is what the code below does,
3133 unsigned int back_i
= PNG_CMAP_RGB_ALPHA_BACKGROUND
+1;
3135 /* Here are how the values map:
3141 * So, as above with the explicit alpha checks, the
3142 * breakpoints are at 64 and 196.
3144 if (inrow
[0] & 0x80) back_i
+= 9; /* red */
3145 if (inrow
[0] & 0x40) back_i
+= 9;
3146 if (inrow
[0] & 0x80) back_i
+= 3; /* green */
3147 if (inrow
[0] & 0x40) back_i
+= 3;
3148 if (inrow
[0] & 0x80) back_i
+= 1; /* blue */
3149 if (inrow
[0] & 0x40) back_i
+= 1;
3151 *outrow
= (png_byte
)back_i
;
3169 png_image_read_colormapped(png_voidp argument
)
3171 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3173 png_imagep image
= display
->image
;
3174 png_controlp control
= image
->opaque
;
3175 png_structrp png_ptr
= control
->png_ptr
;
3176 png_inforp info_ptr
= control
->info_ptr
;
3178 int passes
= 0; /* As a flag */
3180 PNG_SKIP_CHUNKS(png_ptr
);
3182 /* Update the 'info' structure and make sure the result is as required; first
3183 * make sure to turn on the interlace handling if it will be required
3184 * (because it can't be turned on *after* the call to png_read_update_info!)
3186 if (display
->colormap_processing
== PNG_CMAP_NONE
)
3187 passes
= png_set_interlace_handling(png_ptr
);
3189 png_read_update_info(png_ptr
, info_ptr
);
3191 /* The expected output can be deduced from the colormap_processing option. */
3192 switch (display
->colormap_processing
)
3195 /* Output must be one channel and one byte per pixel, the output
3196 * encoding can be anything.
3198 if ((info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
||
3199 info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
) &&
3200 info_ptr
->bit_depth
== 8)
3205 case PNG_CMAP_TRANS
:
3207 /* Output must be two channels and the 'G' one must be sRGB, the latter
3208 * can be checked with an exact number because it should have been set
3209 * to this number above!
3211 if (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
&&
3212 info_ptr
->bit_depth
== 8 &&
3213 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3214 image
->colormap_entries
== 256)
3220 /* Output must be 8-bit sRGB encoded RGB */
3221 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
&&
3222 info_ptr
->bit_depth
== 8 &&
3223 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3224 image
->colormap_entries
== 216)
3229 case PNG_CMAP_RGB_ALPHA
:
3230 /* Output must be 8-bit sRGB encoded RGBA */
3231 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
3232 info_ptr
->bit_depth
== 8 &&
3233 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3234 image
->colormap_entries
== 244 /* 216 + 1 + 27 */)
3237 /* goto bad_output; */
3242 png_error(png_ptr
, "bad color-map processing (internal error)");
3245 /* Now read the rows. Do this here if it is possible to read directly into
3246 * the output buffer, otherwise allocate a local row buffer of the maximum
3247 * size libpng requires and call the relevant processing routine safely.
3250 png_voidp first_row
= display
->buffer
;
3251 ptrdiff_t row_bytes
= display
->row_stride
;
3253 /* The following expression is designed to work correctly whether it gives
3254 * a signed or an unsigned result.
3258 char *ptr
= png_voidcast(char*, first_row
);
3259 ptr
+= (image
->height
-1) * (-row_bytes
);
3260 first_row
= png_voidcast(png_voidp
, ptr
);
3263 display
->first_row
= first_row
;
3264 display
->row_bytes
= row_bytes
;
3270 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
3272 display
->local_row
= row
;
3273 result
= png_safe_execute(image
, png_image_read_and_map
, display
);
3274 display
->local_row
= NULL
;
3275 png_free(png_ptr
, row
);
3282 png_alloc_size_t row_bytes
= display
->row_bytes
;
3284 while (--passes
>= 0)
3286 png_uint_32 y
= image
->height
;
3287 png_bytep row
= png_voidcast(png_bytep
, display
->first_row
);
3291 png_read_row(png_ptr
, row
, NULL
);
3300 /* Just the row reading part of png_image_read. */
3302 png_image_read_composite(png_voidp argument
)
3304 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3306 png_imagep image
= display
->image
;
3307 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3310 switch (png_ptr
->interlaced
)
3312 case PNG_INTERLACE_NONE
:
3316 case PNG_INTERLACE_ADAM7
:
3317 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3321 png_error(png_ptr
, "unknown interlace type");
3325 png_uint_32 height
= image
->height
;
3326 png_uint_32 width
= image
->width
;
3327 ptrdiff_t step_row
= display
->row_bytes
;
3328 unsigned int channels
=
3329 (image
->format
& PNG_FORMAT_FLAG_COLOR
) != 0 ? 3 : 1;
3332 for (pass
= 0; pass
< passes
; ++pass
)
3334 unsigned int startx
, stepx
, stepy
;
3337 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3339 /* The row may be empty for a short image: */
3340 if (PNG_PASS_COLS(width
, pass
) == 0)
3343 startx
= PNG_PASS_START_COL(pass
) * channels
;
3344 stepx
= PNG_PASS_COL_OFFSET(pass
) * channels
;
3345 y
= PNG_PASS_START_ROW(pass
);
3346 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3357 for (; y
<height
; y
+= stepy
)
3359 png_bytep inrow
= png_voidcast(png_bytep
, display
->local_row
);
3361 png_const_bytep end_row
;
3363 /* Read the row, which is packed: */
3364 png_read_row(png_ptr
, inrow
, NULL
);
3366 outrow
= png_voidcast(png_bytep
, display
->first_row
);
3367 outrow
+= y
* step_row
;
3368 end_row
= outrow
+ width
* channels
;
3370 /* Now do the composition on each pixel in this row. */
3372 for (; outrow
< end_row
; outrow
+= stepx
)
3374 png_byte alpha
= inrow
[channels
];
3376 if (alpha
> 0) /* else no change to the output */
3380 for (c
=0; c
<channels
; ++c
)
3382 png_uint_32 component
= inrow
[c
];
3384 if (alpha
< 255) /* else just use component */
3386 /* This is PNG_OPTIMIZED_ALPHA, the component value
3387 * is a linear 8-bit value. Combine this with the
3388 * current outrow[c] value which is sRGB encoded.
3389 * Arithmetic here is 16-bits to preserve the output
3392 component
*= 257*255; /* =65535 */
3393 component
+= (255-alpha
)*png_sRGB_table
[outrow
[c
]];
3395 /* So 'component' is scaled by 255*65535 and is
3396 * therefore appropriate for the sRGB to linear
3399 component
= PNG_sRGB_FROM_LINEAR(component
);
3402 outrow
[c
] = (png_byte
)component
;
3406 inrow
+= channels
+1; /* components and alpha channel */
3415 /* The do_local_background case; called when all the following transforms are to
3422 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3423 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3424 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3425 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3426 * row and handles the removal or pre-multiplication of the alpha channel.
3429 png_image_read_background(png_voidp argument
)
3431 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3433 png_imagep image
= display
->image
;
3434 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3435 png_inforp info_ptr
= image
->opaque
->info_ptr
;
3436 png_uint_32 height
= image
->height
;
3437 png_uint_32 width
= image
->width
;
3440 /* Double check the convoluted logic below. We expect to get here with
3441 * libpng doing rgb to gray and gamma correction but background processing
3442 * left to the png_image_read_background function. The rows libpng produce
3443 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3445 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) == 0)
3446 png_error(png_ptr
, "lost rgb to gray");
3448 if ((png_ptr
->transformations
& PNG_COMPOSE
) != 0)
3449 png_error(png_ptr
, "unexpected compose");
3451 if (png_get_channels(png_ptr
, info_ptr
) != 2)
3452 png_error(png_ptr
, "lost/gained channels");
3454 /* Expect the 8-bit case to always remove the alpha channel */
3455 if ((image
->format
& PNG_FORMAT_FLAG_LINEAR
) == 0 &&
3456 (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3457 png_error(png_ptr
, "unexpected 8-bit transformation");
3459 switch (png_ptr
->interlaced
)
3461 case PNG_INTERLACE_NONE
:
3465 case PNG_INTERLACE_ADAM7
:
3466 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3470 png_error(png_ptr
, "unknown interlace type");
3473 /* Use direct access to info_ptr here because otherwise the simplified API
3474 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3475 * checking the value after libpng expansions, not the original value in the
3478 switch (info_ptr
->bit_depth
)
3481 png_error(png_ptr
, "unexpected bit depth");
3485 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3486 * to be removed by composing on a background: either the row if
3487 * display->background is NULL or display->background->green if not.
3488 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3491 png_bytep first_row
= png_voidcast(png_bytep
, display
->first_row
);
3492 ptrdiff_t step_row
= display
->row_bytes
;
3494 for (pass
= 0; pass
< passes
; ++pass
)
3496 png_bytep row
= png_voidcast(png_bytep
,
3497 display
->first_row
);
3498 unsigned int startx
, stepx
, stepy
;
3501 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3503 /* The row may be empty for a short image: */
3504 if (PNG_PASS_COLS(width
, pass
) == 0)
3507 startx
= PNG_PASS_START_COL(pass
);
3508 stepx
= PNG_PASS_COL_OFFSET(pass
);
3509 y
= PNG_PASS_START_ROW(pass
);
3510 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3520 if (display
->background
== NULL
)
3522 for (; y
<height
; y
+= stepy
)
3524 png_bytep inrow
= png_voidcast(png_bytep
,
3525 display
->local_row
);
3526 png_bytep outrow
= first_row
+ y
* step_row
;
3527 png_const_bytep end_row
= outrow
+ width
;
3529 /* Read the row, which is packed: */
3530 png_read_row(png_ptr
, inrow
, NULL
);
3532 /* Now do the composition on each pixel in this row. */
3534 for (; outrow
< end_row
; outrow
+= stepx
)
3536 png_byte alpha
= inrow
[1];
3538 if (alpha
> 0) /* else no change to the output */
3540 png_uint_32 component
= inrow
[0];
3542 if (alpha
< 255) /* else just use component */
3544 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3545 * necessary to invert the sRGB transfer
3546 * function and multiply the alpha out.
3548 component
= png_sRGB_table
[component
] * alpha
;
3549 component
+= png_sRGB_table
[outrow
[0]] *
3551 component
= PNG_sRGB_FROM_LINEAR(component
);
3554 outrow
[0] = (png_byte
)component
;
3557 inrow
+= 2; /* gray and alpha channel */
3562 else /* constant background value */
3564 png_byte background8
= display
->background
->green
;
3565 png_uint_16 background
= png_sRGB_table
[background8
];
3567 for (; y
<height
; y
+= stepy
)
3569 png_bytep inrow
= png_voidcast(png_bytep
,
3570 display
->local_row
);
3571 png_bytep outrow
= first_row
+ y
* step_row
;
3572 png_const_bytep end_row
= outrow
+ width
;
3574 /* Read the row, which is packed: */
3575 png_read_row(png_ptr
, inrow
, NULL
);
3577 /* Now do the composition on each pixel in this row. */
3579 for (; outrow
< end_row
; outrow
+= stepx
)
3581 png_byte alpha
= inrow
[1];
3583 if (alpha
> 0) /* else use background */
3585 png_uint_32 component
= inrow
[0];
3587 if (alpha
< 255) /* else just use component */
3589 component
= png_sRGB_table
[component
] * alpha
;
3590 component
+= background
* (255-alpha
);
3591 component
= PNG_sRGB_FROM_LINEAR(component
);
3594 outrow
[0] = (png_byte
)component
;
3598 outrow
[0] = background8
;
3600 inrow
+= 2; /* gray and alpha channel */
3603 row
+= display
->row_bytes
;
3611 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3612 * still be done and, maybe, the alpha channel removed. This code also
3613 * handles the alpha-first option.
3616 png_uint_16p first_row
= png_voidcast(png_uint_16p
,
3617 display
->first_row
);
3618 /* The division by two is safe because the caller passed in a
3619 * stride which was multiplied by 2 (below) to get row_bytes.
3621 ptrdiff_t step_row
= display
->row_bytes
/ 2;
3622 int preserve_alpha
= (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
3623 unsigned int outchannels
= 1+preserve_alpha
;
3626 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3627 if (preserve_alpha
!= 0 &&
3628 (image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
3632 for (pass
= 0; pass
< passes
; ++pass
)
3634 unsigned int startx
, stepx
, stepy
;
3637 /* The 'x' start and step are adjusted to output components here.
3639 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3641 /* The row may be empty for a short image: */
3642 if (PNG_PASS_COLS(width
, pass
) == 0)
3645 startx
= PNG_PASS_START_COL(pass
) * outchannels
;
3646 stepx
= PNG_PASS_COL_OFFSET(pass
) * outchannels
;
3647 y
= PNG_PASS_START_ROW(pass
);
3648 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3655 stepx
= outchannels
;
3659 for (; y
<height
; y
+= stepy
)
3661 png_const_uint_16p inrow
;
3662 png_uint_16p outrow
= first_row
+ y
*step_row
;
3663 png_uint_16p end_row
= outrow
+ width
* outchannels
;
3665 /* Read the row, which is packed: */
3666 png_read_row(png_ptr
, png_voidcast(png_bytep
,
3667 display
->local_row
), NULL
);
3668 inrow
= png_voidcast(png_const_uint_16p
, display
->local_row
);
3670 /* Now do the pre-multiplication on each pixel in this row.
3673 for (; outrow
< end_row
; outrow
+= stepx
)
3675 png_uint_32 component
= inrow
[0];
3676 png_uint_16 alpha
= inrow
[1];
3678 if (alpha
> 0) /* else 0 */
3680 if (alpha
< 65535) /* else just use component */
3691 outrow
[swap_alpha
] = (png_uint_16
)component
;
3692 if (preserve_alpha
!= 0)
3693 outrow
[1 ^ swap_alpha
] = alpha
;
3695 inrow
+= 2; /* components and alpha channel */
3706 /* The guts of png_image_finish_read as a png_safe_execute callback. */
3708 png_image_read_direct(png_voidp argument
)
3710 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3712 png_imagep image
= display
->image
;
3713 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3714 png_inforp info_ptr
= image
->opaque
->info_ptr
;
3716 png_uint_32 format
= image
->format
;
3717 int linear
= (format
& PNG_FORMAT_FLAG_LINEAR
) != 0;
3718 int do_local_compose
= 0;
3719 int do_local_background
= 0; /* to avoid double gamma correction bug */
3722 /* Add transforms to ensure the correct output format is produced then check
3723 * that the required implementation support is there. Always expand; always
3724 * need 8 bits minimum, no palette and expanded tRNS.
3726 png_set_expand(png_ptr
);
3728 /* Now check the format to see if it was modified. */
3730 png_uint_32 base_format
= png_image_format(png_ptr
) &
3731 ~PNG_FORMAT_FLAG_COLORMAP
/* removed by png_set_expand */;
3732 png_uint_32 change
= format
^ base_format
;
3733 png_fixed_point output_gamma
;
3734 int mode
; /* alpha mode */
3736 /* Do this first so that we have a record if rgb to gray is happening. */
3737 if ((change
& PNG_FORMAT_FLAG_COLOR
) != 0)
3739 /* gray<->color transformation required. */
3740 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0)
3741 png_set_gray_to_rgb(png_ptr
);
3745 /* libpng can't do both rgb to gray and
3746 * background/pre-multiplication if there is also significant gamma
3747 * correction, because both operations require linear colors and
3748 * the code only supports one transform doing the gamma correction.
3749 * Handle this by doing the pre-multiplication or background
3750 * operation in this code, if necessary.
3752 * TODO: fix this by rewriting pngrtran.c (!)
3754 * For the moment (given that fixing this in pngrtran.c is an
3755 * enormous change) 'do_local_background' is used to indicate that
3756 * the problem exists.
3758 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3759 do_local_background
= 1/*maybe*/;
3761 png_set_rgb_to_gray_fixed(png_ptr
, PNG_ERROR_ACTION_NONE
,
3762 PNG_RGB_TO_GRAY_DEFAULT
, PNG_RGB_TO_GRAY_DEFAULT
);
3765 change
&= ~PNG_FORMAT_FLAG_COLOR
;
3768 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3771 png_fixed_point input_gamma_default
;
3773 if ((base_format
& PNG_FORMAT_FLAG_LINEAR
) != 0 &&
3774 (image
->flags
& PNG_IMAGE_FLAG_16BIT_sRGB
) == 0)
3775 input_gamma_default
= PNG_GAMMA_LINEAR
;
3777 input_gamma_default
= PNG_DEFAULT_sRGB
;
3779 /* Call png_set_alpha_mode to set the default for the input gamma; the
3780 * output gamma is set by a second call below.
3782 png_set_alpha_mode_fixed(png_ptr
, PNG_ALPHA_PNG
, input_gamma_default
);
3787 /* If there *is* an alpha channel in the input it must be multiplied
3788 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3790 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3791 mode
= PNG_ALPHA_STANDARD
; /* associated alpha */
3794 mode
= PNG_ALPHA_PNG
;
3796 output_gamma
= PNG_GAMMA_LINEAR
;
3801 mode
= PNG_ALPHA_PNG
;
3802 output_gamma
= PNG_DEFAULT_sRGB
;
3805 /* If 'do_local_background' is set check for the presence of gamma
3806 * correction; this is part of the work-round for the libpng bug
3809 * TODO: fix libpng and remove this.
3811 if (do_local_background
!= 0)
3813 png_fixed_point gtest
;
3815 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3816 * gamma correction, the screen gamma hasn't been set on png_struct
3817 * yet; it's set below. png_struct::gamma, however, is set to the
3820 if (png_muldiv(>est
, output_gamma
, png_ptr
->colorspace
.gamma
,
3821 PNG_FP_1
) != 0 && png_gamma_significant(gtest
) == 0)
3822 do_local_background
= 0;
3824 else if (mode
== PNG_ALPHA_STANDARD
)
3826 do_local_background
= 2/*required*/;
3827 mode
= PNG_ALPHA_PNG
; /* prevent libpng doing it */
3830 /* else leave as 1 for the checks below */
3833 /* If the bit-depth changes then handle that here. */
3834 if ((change
& PNG_FORMAT_FLAG_LINEAR
) != 0)
3836 if (linear
!= 0 /*16-bit output*/)
3837 png_set_expand_16(png_ptr
);
3839 else /* 8-bit output */
3840 png_set_scale_16(png_ptr
);
3842 change
&= ~PNG_FORMAT_FLAG_LINEAR
;
3845 /* Now the background/alpha channel changes. */
3846 if ((change
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3848 /* Removing an alpha channel requires composition for the 8-bit
3849 * formats; for the 16-bit it is already done, above, by the
3850 * pre-multiplication and the channel just needs to be stripped.
3852 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3854 /* If RGB->gray is happening the alpha channel must be left and the
3855 * operation completed locally.
3857 * TODO: fix libpng and remove this.
3859 if (do_local_background
!= 0)
3860 do_local_background
= 2/*required*/;
3862 /* 16-bit output: just remove the channel */
3863 else if (linear
!= 0) /* compose on black (well, pre-multiply) */
3864 png_set_strip_alpha(png_ptr
);
3866 /* 8-bit output: do an appropriate compose */
3867 else if (display
->background
!= NULL
)
3871 c
.index
= 0; /*unused*/
3872 c
.red
= display
->background
->red
;
3873 c
.green
= display
->background
->green
;
3874 c
.blue
= display
->background
->blue
;
3875 c
.gray
= display
->background
->green
;
3877 /* This is always an 8-bit sRGB value, using the 'green' channel
3878 * for gray is much better than calculating the luminance here;
3879 * we can get off-by-one errors in that calculation relative to
3880 * the app expectations and that will show up in transparent
3883 png_set_background_fixed(png_ptr
, &c
,
3884 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
3885 0/*gamma: not used*/);
3888 else /* compose on row: implemented below. */
3890 do_local_compose
= 1;
3891 /* This leaves the alpha channel in the output, so it has to be
3892 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3893 * one so the code only has to hack on the pixels that require
3896 mode
= PNG_ALPHA_OPTIMIZED
;
3900 else /* output needs an alpha channel */
3902 /* This is tricky because it happens before the swap operation has
3903 * been accomplished; however, the swap does *not* swap the added
3904 * alpha channel (weird API), so it must be added in the correct
3907 png_uint_32 filler
; /* opaque filler */
3916 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
3917 if ((format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
3919 where
= PNG_FILLER_BEFORE
;
3920 change
&= ~PNG_FORMAT_FLAG_AFIRST
;
3925 where
= PNG_FILLER_AFTER
;
3927 png_set_add_alpha(png_ptr
, filler
, where
);
3930 /* This stops the (irrelevant) call to swap_alpha below. */
3931 change
&= ~PNG_FORMAT_FLAG_ALPHA
;
3934 /* Now set the alpha mode correctly; this is always done, even if there is
3935 * no alpha channel in either the input or the output because it correctly
3936 * sets the output gamma.
3938 png_set_alpha_mode_fixed(png_ptr
, mode
, output_gamma
);
3940 # ifdef PNG_FORMAT_BGR_SUPPORTED
3941 if ((change
& PNG_FORMAT_FLAG_BGR
) != 0)
3943 /* Check only the output format; PNG is never BGR; don't do this if
3944 * the output is gray, but fix up the 'format' value in that case.
3946 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0)
3947 png_set_bgr(png_ptr
);
3950 format
&= ~PNG_FORMAT_FLAG_BGR
;
3952 change
&= ~PNG_FORMAT_FLAG_BGR
;
3956 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
3957 if ((change
& PNG_FORMAT_FLAG_AFIRST
) != 0)
3959 /* Only relevant if there is an alpha channel - it's particularly
3960 * important to handle this correctly because do_local_compose may
3961 * be set above and then libpng will keep the alpha channel for this
3964 if ((format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3966 /* Disable this if doing a local background,
3967 * TODO: remove this when local background is no longer required.
3969 if (do_local_background
!= 2)
3970 png_set_swap_alpha(png_ptr
);
3974 format
&= ~PNG_FORMAT_FLAG_AFIRST
;
3976 change
&= ~PNG_FORMAT_FLAG_AFIRST
;
3980 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3985 PNG_CONST png_uint_16 le
= 0x0001;
3987 if ((*(png_const_bytep
) & le
) != 0)
3988 png_set_swap(png_ptr
);
3991 /* If change is not now 0 some transformation is missing - error out. */
3993 png_error(png_ptr
, "png_read_image: unsupported transformation");
3996 PNG_SKIP_CHUNKS(png_ptr
);
3998 /* Update the 'info' structure and make sure the result is as required; first
3999 * make sure to turn on the interlace handling if it will be required
4000 * (because it can't be turned on *after* the call to png_read_update_info!)
4002 * TODO: remove the do_local_background fixup below.
4004 if (do_local_compose
== 0 && do_local_background
!= 2)
4005 passes
= png_set_interlace_handling(png_ptr
);
4007 png_read_update_info(png_ptr
, info_ptr
);
4010 png_uint_32 info_format
= 0;
4012 if ((info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
4013 info_format
|= PNG_FORMAT_FLAG_COLOR
;
4015 if ((info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
4017 /* do_local_compose removes this channel below. */
4018 if (do_local_compose
== 0)
4020 /* do_local_background does the same if required. */
4021 if (do_local_background
!= 2 ||
4022 (format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
4023 info_format
|= PNG_FORMAT_FLAG_ALPHA
;
4027 else if (do_local_compose
!= 0) /* internal error */
4028 png_error(png_ptr
, "png_image_read: alpha channel lost");
4030 if (info_ptr
->bit_depth
== 16)
4031 info_format
|= PNG_FORMAT_FLAG_LINEAR
;
4033 # ifdef PNG_FORMAT_BGR_SUPPORTED
4034 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
4035 info_format
|= PNG_FORMAT_FLAG_BGR
;
4038 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
4039 if (do_local_background
== 2)
4041 if ((format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
4042 info_format
|= PNG_FORMAT_FLAG_AFIRST
;
4045 if ((png_ptr
->transformations
& PNG_SWAP_ALPHA
) != 0 ||
4046 ((png_ptr
->transformations
& PNG_ADD_ALPHA
) != 0 &&
4047 (png_ptr
->flags
& PNG_FLAG_FILLER_AFTER
) == 0))
4049 if (do_local_background
== 2)
4050 png_error(png_ptr
, "unexpected alpha swap transformation");
4052 info_format
|= PNG_FORMAT_FLAG_AFIRST
;
4056 /* This is actually an internal error. */
4057 if (info_format
!= format
)
4058 png_error(png_ptr
, "png_read_image: invalid transformations");
4061 /* Now read the rows. If do_local_compose is set then it is necessary to use
4062 * a local row buffer. The output will be GA, RGBA or BGRA and must be
4063 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
4064 * display acts as a flag.
4067 png_voidp first_row
= display
->buffer
;
4068 ptrdiff_t row_bytes
= display
->row_stride
;
4073 /* The following expression is designed to work correctly whether it gives
4074 * a signed or an unsigned result.
4078 char *ptr
= png_voidcast(char*, first_row
);
4079 ptr
+= (image
->height
-1) * (-row_bytes
);
4080 first_row
= png_voidcast(png_voidp
, ptr
);
4083 display
->first_row
= first_row
;
4084 display
->row_bytes
= row_bytes
;
4087 if (do_local_compose
!= 0)
4090 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
4092 display
->local_row
= row
;
4093 result
= png_safe_execute(image
, png_image_read_composite
, display
);
4094 display
->local_row
= NULL
;
4095 png_free(png_ptr
, row
);
4100 else if (do_local_background
== 2)
4103 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
4105 display
->local_row
= row
;
4106 result
= png_safe_execute(image
, png_image_read_background
, display
);
4107 display
->local_row
= NULL
;
4108 png_free(png_ptr
, row
);
4115 png_alloc_size_t row_bytes
= display
->row_bytes
;
4117 while (--passes
>= 0)
4119 png_uint_32 y
= image
->height
;
4120 png_bytep row
= png_voidcast(png_bytep
, display
->first_row
);
4124 png_read_row(png_ptr
, row
, NULL
);
4134 png_image_finish_read(png_imagep image
, png_const_colorp background
,
4135 void *buffer
, png_int_32 row_stride
, void *colormap
)
4137 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
4141 if (row_stride
== 0)
4142 row_stride
= PNG_IMAGE_ROW_STRIDE(*image
);
4145 check
= -row_stride
;
4150 if (image
->opaque
!= NULL
&& buffer
!= NULL
&&
4151 check
>= PNG_IMAGE_ROW_STRIDE(*image
))
4153 if ((image
->format
& PNG_FORMAT_FLAG_COLORMAP
) == 0 ||
4154 (image
->colormap_entries
> 0 && colormap
!= NULL
))
4157 png_image_read_control display
;
4159 memset(&display
, 0, (sizeof display
));
4160 display
.image
= image
;
4161 display
.buffer
= buffer
;
4162 display
.row_stride
= row_stride
;
4163 display
.colormap
= colormap
;
4164 display
.background
= background
;
4165 display
.local_row
= NULL
;
4167 /* Choose the correct 'end' routine; for the color-map case all the
4168 * setup has already been done.
4170 if ((image
->format
& PNG_FORMAT_FLAG_COLORMAP
) != 0)
4172 png_safe_execute(image
, png_image_read_colormap
, &display
) &&
4173 png_safe_execute(image
, png_image_read_colormapped
, &display
);
4177 png_safe_execute(image
, png_image_read_direct
, &display
);
4179 png_image_free(image
);
4184 return png_image_error(image
,
4185 "png_image_finish_read[color-map]: no color-map");
4189 return png_image_error(image
,
4190 "png_image_finish_read: invalid argument");
4193 else if (image
!= NULL
)
4194 return png_image_error(image
,
4195 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4200 #endif /* SIMPLIFIED_READ */