2 /* pngread.c - read a PNG file
4 * Copyright (c) 2018-2024 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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_RELEASE_BUILD
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)
131 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
132 png_ptr
->mode
|= PNG_AFTER_IDAT
;
135 /* This should be a binary subdivision search or a hash for
136 * matching the chunk name rather than a linear search.
138 if (chunk_name
== png_IHDR
)
139 png_handle_IHDR(png_ptr
, info_ptr
, length
);
141 else if (chunk_name
== png_IEND
)
142 png_handle_IEND(png_ptr
, info_ptr
, length
);
144 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
145 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
147 png_handle_unknown(png_ptr
, info_ptr
, length
, keep
);
149 if (chunk_name
== png_PLTE
)
150 png_ptr
->mode
|= PNG_HAVE_PLTE
;
152 else if (chunk_name
== png_IDAT
)
154 png_ptr
->idat_size
= 0; /* It has been consumed */
159 else if (chunk_name
== png_PLTE
)
160 png_handle_PLTE(png_ptr
, info_ptr
, length
);
162 else if (chunk_name
== png_IDAT
)
164 #ifdef PNG_READ_APNG_SUPPORTED
165 png_have_info(png_ptr
, info_ptr
);
167 png_ptr
->idat_size
= length
;
171 #ifdef PNG_READ_bKGD_SUPPORTED
172 else if (chunk_name
== png_bKGD
)
173 png_handle_bKGD(png_ptr
, info_ptr
, length
);
176 #ifdef PNG_READ_cHRM_SUPPORTED
177 else if (chunk_name
== png_cHRM
)
178 png_handle_cHRM(png_ptr
, info_ptr
, length
);
181 #ifdef PNG_READ_eXIf_SUPPORTED
182 else if (chunk_name
== png_eXIf
)
183 png_handle_eXIf(png_ptr
, info_ptr
, length
);
186 #ifdef PNG_READ_gAMA_SUPPORTED
187 else if (chunk_name
== png_gAMA
)
188 png_handle_gAMA(png_ptr
, info_ptr
, length
);
191 #ifdef PNG_READ_hIST_SUPPORTED
192 else if (chunk_name
== png_hIST
)
193 png_handle_hIST(png_ptr
, info_ptr
, length
);
196 #ifdef PNG_READ_oFFs_SUPPORTED
197 else if (chunk_name
== png_oFFs
)
198 png_handle_oFFs(png_ptr
, info_ptr
, length
);
201 #ifdef PNG_READ_pCAL_SUPPORTED
202 else if (chunk_name
== png_pCAL
)
203 png_handle_pCAL(png_ptr
, info_ptr
, length
);
206 #ifdef PNG_READ_sCAL_SUPPORTED
207 else if (chunk_name
== png_sCAL
)
208 png_handle_sCAL(png_ptr
, info_ptr
, length
);
211 #ifdef PNG_READ_pHYs_SUPPORTED
212 else if (chunk_name
== png_pHYs
)
213 png_handle_pHYs(png_ptr
, info_ptr
, length
);
216 #ifdef PNG_READ_sBIT_SUPPORTED
217 else if (chunk_name
== png_sBIT
)
218 png_handle_sBIT(png_ptr
, info_ptr
, length
);
221 #ifdef PNG_READ_sRGB_SUPPORTED
222 else if (chunk_name
== png_sRGB
)
223 png_handle_sRGB(png_ptr
, info_ptr
, length
);
226 #ifdef PNG_READ_iCCP_SUPPORTED
227 else if (chunk_name
== png_iCCP
)
228 png_handle_iCCP(png_ptr
, info_ptr
, length
);
231 #ifdef PNG_READ_sPLT_SUPPORTED
232 else if (chunk_name
== png_sPLT
)
233 png_handle_sPLT(png_ptr
, info_ptr
, length
);
236 #ifdef PNG_READ_tEXt_SUPPORTED
237 else if (chunk_name
== png_tEXt
)
238 png_handle_tEXt(png_ptr
, info_ptr
, length
);
241 #ifdef PNG_READ_tIME_SUPPORTED
242 else if (chunk_name
== png_tIME
)
243 png_handle_tIME(png_ptr
, info_ptr
, length
);
246 #ifdef PNG_READ_tRNS_SUPPORTED
247 else if (chunk_name
== png_tRNS
)
248 png_handle_tRNS(png_ptr
, info_ptr
, length
);
251 #ifdef PNG_READ_zTXt_SUPPORTED
252 else if (chunk_name
== png_zTXt
)
253 png_handle_zTXt(png_ptr
, info_ptr
, length
);
256 #ifdef PNG_READ_iTXt_SUPPORTED
257 else if (chunk_name
== png_iTXt
)
258 png_handle_iTXt(png_ptr
, info_ptr
, length
);
261 #ifdef PNG_READ_APNG_SUPPORTED
262 else if (chunk_name
== png_acTL
)
263 png_handle_acTL(png_ptr
, info_ptr
, length
);
265 else if (chunk_name
== png_fcTL
)
266 png_handle_fcTL(png_ptr
, info_ptr
, length
);
268 else if (chunk_name
== png_fdAT
)
269 png_handle_fdAT(png_ptr
, info_ptr
, length
);
273 png_handle_unknown(png_ptr
, info_ptr
, length
,
274 PNG_HANDLE_CHUNK_AS_DEFAULT
);
277 #endif /* SEQUENTIAL_READ */
279 #ifdef PNG_READ_APNG_SUPPORTED
281 png_read_frame_head(png_structp png_ptr
, png_infop info_ptr
)
283 png_byte have_chunk_after_DAT
; /* after IDAT or after fdAT */
285 png_debug(0, "Reading frame head");
287 if ((png_ptr
->mode
& PNG_HAVE_acTL
) == 0)
288 png_error(png_ptr
, "attempt to png_read_frame_head() but "
291 /* do nothing for the main IDAT */
292 if (png_ptr
->num_frames_read
== 0)
295 png_read_reset(png_ptr
);
296 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
297 png_ptr
->mode
&= ~PNG_HAVE_fcTL
;
299 have_chunk_after_DAT
= 0;
302 png_uint_32 length
= png_read_chunk_header(png_ptr
);
304 if (png_ptr
->chunk_name
== png_IDAT
)
306 /* discard trailing IDATs for the first frame */
307 if (have_chunk_after_DAT
!= 0 || png_ptr
->num_frames_read
> 1)
308 png_error(png_ptr
, "png_read_frame_head(): out of place IDAT");
309 png_crc_finish(png_ptr
, length
);
312 else if (png_ptr
->chunk_name
== png_fcTL
)
314 png_handle_fcTL(png_ptr
, info_ptr
, length
);
315 have_chunk_after_DAT
= 1;
318 else if (png_ptr
->chunk_name
== png_fdAT
)
320 png_ensure_sequence_number(png_ptr
, length
);
322 /* discard trailing fdATs for frames other than the first */
323 if (have_chunk_after_DAT
== 0 && png_ptr
->num_frames_read
> 1)
324 png_crc_finish(png_ptr
, length
- 4);
325 else if (png_ptr
->mode
& PNG_HAVE_fcTL
)
327 png_ptr
->idat_size
= length
- 4;
328 png_ptr
->mode
|= PNG_HAVE_IDAT
;
333 png_error(png_ptr
, "png_read_frame_head(): out of place fdAT");
337 png_warning(png_ptr
, "Skipped (ignored) a chunk "
338 "between APNG chunks");
339 png_crc_finish(png_ptr
, length
);
343 #endif /* READ_APNG */
345 /* Optional call to update the users info_ptr structure */
347 png_read_update_info(png_structrp png_ptr
, png_inforp info_ptr
)
349 png_debug(1, "in png_read_update_info");
353 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
355 png_read_start_row(png_ptr
);
357 # ifdef PNG_READ_TRANSFORMS_SUPPORTED
358 png_read_transform_info(png_ptr
, info_ptr
);
364 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
366 png_app_error(png_ptr
,
367 "png_read_update_info/png_start_read_image: duplicate call");
371 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
372 /* Initialize palette, background, etc, after transformations
373 * are set, but before any reading takes place. This allows
374 * the user to obtain a gamma-corrected palette, for example.
375 * If the user doesn't call this, we will do it ourselves.
378 png_start_read_image(png_structrp png_ptr
)
380 png_debug(1, "in png_start_read_image");
384 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
385 png_read_start_row(png_ptr
);
387 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
389 png_app_error(png_ptr
,
390 "png_start_read_image/png_read_update_info: duplicate call");
393 #endif /* SEQUENTIAL_READ */
395 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
396 #ifdef PNG_MNG_FEATURES_SUPPORTED
397 /* Undoes intrapixel differencing,
398 * NOTE: this is apparently only supported in the 'sequential' reader.
401 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
403 png_debug(1, "in png_do_read_intrapixel");
406 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
409 png_uint_32 row_width
= row_info
->width
;
411 if (row_info
->bit_depth
== 8)
416 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
419 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
425 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
427 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+ 1)) & 0xff);
428 *(rp
+2) = (png_byte
)((256 + *(rp
+ 2) + *(rp
+ 1)) & 0xff);
431 else if (row_info
->bit_depth
== 16)
436 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
439 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
445 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
447 png_uint_32 s0
= (png_uint_32
)(*(rp
) << 8) | *(rp
+ 1);
448 png_uint_32 s1
= (png_uint_32
)(*(rp
+ 2) << 8) | *(rp
+ 3);
449 png_uint_32 s2
= (png_uint_32
)(*(rp
+ 4) << 8) | *(rp
+ 5);
450 png_uint_32 red
= (s0
+ s1
+ 65536) & 0xffff;
451 png_uint_32 blue
= (s2
+ s1
+ 65536) & 0xffff;
452 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
453 *(rp
+ 1) = (png_byte
)(red
& 0xff);
454 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
455 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
460 #endif /* MNG_FEATURES */
463 png_read_row(png_structrp png_ptr
, png_bytep row
, png_bytep dsp_row
)
465 png_row_info row_info
;
470 png_debug2(1, "in png_read_row (row %lu, pass %d)",
471 (unsigned long)png_ptr
->row_number
, png_ptr
->pass
);
473 /* png_read_start_row sets the information (in particular iwidth) for this
476 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
477 png_read_start_row(png_ptr
);
479 /* 1.5.6: row_info moved out of png_struct to a local here. */
480 row_info
.width
= png_ptr
->iwidth
; /* NOTE: width of current interlaced row */
481 row_info
.color_type
= png_ptr
->color_type
;
482 row_info
.bit_depth
= png_ptr
->bit_depth
;
483 row_info
.channels
= png_ptr
->channels
;
484 row_info
.pixel_depth
= png_ptr
->pixel_depth
;
485 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
487 #ifdef PNG_WARNINGS_SUPPORTED
488 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
490 /* Check for transforms that have been set but were defined out */
491 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
492 if ((png_ptr
->transformations
& PNG_INVERT_MONO
) != 0)
493 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined");
496 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
497 if ((png_ptr
->transformations
& PNG_FILLER
) != 0)
498 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined");
501 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
502 !defined(PNG_READ_PACKSWAP_SUPPORTED)
503 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
504 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
507 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
508 if ((png_ptr
->transformations
& PNG_PACK
) != 0)
509 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined");
512 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
513 if ((png_ptr
->transformations
& PNG_SHIFT
) != 0)
514 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined");
517 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
518 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
519 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined");
522 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
523 if ((png_ptr
->transformations
& PNG_SWAP_BYTES
) != 0)
524 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined");
527 #endif /* WARNINGS */
529 #ifdef PNG_READ_INTERLACING_SUPPORTED
530 /* If interlaced and we do not need a new row, combine row and return.
531 * Notice that the pixels we have from previous rows have been transformed
532 * already; we can only combine like with like (transformed or
533 * untransformed) and, because of the libpng API for interlaced images, this
534 * means we must transform before de-interlacing.
536 if (png_ptr
->interlaced
!= 0 &&
537 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
539 switch (png_ptr
->pass
)
542 if (png_ptr
->row_number
& 0x07)
545 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
546 png_read_finish_row(png_ptr
);
552 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
555 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
557 png_read_finish_row(png_ptr
);
563 if ((png_ptr
->row_number
& 0x07) != 4)
565 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
566 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
568 png_read_finish_row(png_ptr
);
574 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
577 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
579 png_read_finish_row(png_ptr
);
585 if ((png_ptr
->row_number
& 3) != 2)
587 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
588 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
590 png_read_finish_row(png_ptr
);
596 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
599 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
601 png_read_finish_row(png_ptr
);
608 if ((png_ptr
->row_number
& 1) == 0)
610 png_read_finish_row(png_ptr
);
618 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) == 0)
619 png_error(png_ptr
, "Invalid attempt to read row data");
621 /* Fill the row with IDAT data: */
622 png_ptr
->row_buf
[0]=255; /* to force error if no data was found */
623 png_read_IDAT_data(png_ptr
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
625 if (png_ptr
->row_buf
[0] > PNG_FILTER_VALUE_NONE
)
627 if (png_ptr
->row_buf
[0] < PNG_FILTER_VALUE_LAST
)
628 png_read_filter_row(png_ptr
, &row_info
, png_ptr
->row_buf
+ 1,
629 png_ptr
->prev_row
+ 1, png_ptr
->row_buf
[0]);
631 png_error(png_ptr
, "bad adaptive filter value");
634 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
635 * 1.5.6, while the buffer really is this big in current versions of libpng
636 * it may not be in the future, so this was changed just to copy the
639 memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
641 #ifdef PNG_MNG_FEATURES_SUPPORTED
642 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) != 0 &&
643 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
645 /* Intrapixel differencing */
646 png_do_read_intrapixel(&row_info
, png_ptr
->row_buf
+ 1);
650 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
651 if (png_ptr
->transformations
652 # ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
653 || png_ptr
->num_palette_max
>= 0
656 png_do_read_transformations(png_ptr
, &row_info
);
659 /* The transformed pixel depth should match the depth now in row_info. */
660 if (png_ptr
->transformed_pixel_depth
== 0)
662 png_ptr
->transformed_pixel_depth
= row_info
.pixel_depth
;
663 if (row_info
.pixel_depth
> png_ptr
->maximum_pixel_depth
)
664 png_error(png_ptr
, "sequential row overflow");
667 else if (png_ptr
->transformed_pixel_depth
!= row_info
.pixel_depth
)
668 png_error(png_ptr
, "internal sequential row size calculation error");
670 #ifdef PNG_READ_INTERLACING_SUPPORTED
671 /* Expand interlaced rows to full size */
672 if (png_ptr
->interlaced
!= 0 &&
673 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
675 if (png_ptr
->pass
< 6)
676 png_do_read_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
,
677 png_ptr
->transformations
);
680 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
683 png_combine_row(png_ptr
, row
, 0/*row*/);
690 png_combine_row(png_ptr
, row
, -1/*ignored*/);
693 png_combine_row(png_ptr
, dsp_row
, -1/*ignored*/);
695 png_read_finish_row(png_ptr
);
697 if (png_ptr
->read_row_fn
!= NULL
)
698 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
701 #endif /* SEQUENTIAL_READ */
703 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
704 /* Read one or more rows of image data. If the image is interlaced,
705 * and png_set_interlace_handling() has been called, the rows need to
706 * contain the contents of the rows from the previous pass. If the
707 * image has alpha or transparency, and png_handle_alpha()[*] has been
708 * called, the rows contents must be initialized to the contents of the
711 * "row" holds the actual image, and pixels are placed in it
712 * as they arrive. If the image is displayed after each pass, it will
713 * appear to "sparkle" in. "display_row" can be used to display a
714 * "chunky" progressive image, with finer detail added as it becomes
715 * available. If you do not want this "chunky" display, you may pass
716 * NULL for display_row. If you do not want the sparkle display, and
717 * you have not called png_handle_alpha(), you may pass NULL for rows.
718 * If you have called png_handle_alpha(), and the image has either an
719 * alpha channel or a transparency chunk, you must provide a buffer for
720 * rows. In this case, you do not have to provide a display_row buffer
721 * also, but you may. If the image is not interlaced, or if you have
722 * not called png_set_interlace_handling(), the display_row buffer will
723 * be ignored, so pass NULL to it.
725 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
729 png_read_rows(png_structrp png_ptr
, png_bytepp row
,
730 png_bytepp display_row
, png_uint_32 num_rows
)
736 png_debug(1, "in png_read_rows");
743 if (rp
!= NULL
&& dp
!= NULL
)
744 for (i
= 0; i
< num_rows
; i
++)
746 png_bytep rptr
= *rp
++;
747 png_bytep dptr
= *dp
++;
749 png_read_row(png_ptr
, rptr
, dptr
);
753 for (i
= 0; i
< num_rows
; i
++)
755 png_bytep rptr
= *rp
;
756 png_read_row(png_ptr
, rptr
, NULL
);
761 for (i
= 0; i
< num_rows
; i
++)
763 png_bytep dptr
= *dp
;
764 png_read_row(png_ptr
, NULL
, dptr
);
768 #endif /* SEQUENTIAL_READ */
770 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
771 /* Read the entire image. If the image has an alpha channel or a tRNS
772 * chunk, and you have called png_handle_alpha()[*], you will need to
773 * initialize the image to the current image that PNG will be overlaying.
774 * We set the num_rows again here, in case it was incorrectly set in
775 * png_read_start_row() by a call to png_read_update_info() or
776 * png_start_read_image() if png_set_interlace_handling() wasn't called
777 * prior to either of these functions like it should have been. You can
778 * only call this function once. If you desire to have an image for
779 * each pass of a interlaced image, use png_read_rows() instead.
781 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
784 png_read_image(png_structrp png_ptr
, png_bytepp image
)
786 png_uint_32 i
, image_height
;
790 png_debug(1, "in png_read_image");
795 #ifdef PNG_READ_INTERLACING_SUPPORTED
796 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
798 pass
= png_set_interlace_handling(png_ptr
);
799 /* And make sure transforms are initialized. */
800 png_start_read_image(png_ptr
);
804 if (png_ptr
->interlaced
!= 0 &&
805 (png_ptr
->transformations
& PNG_INTERLACE
) == 0)
807 /* Caller called png_start_read_image or png_read_update_info without
808 * first turning on the PNG_INTERLACE transform. We can fix this here,
809 * but the caller should do it!
811 png_warning(png_ptr
, "Interlace handling should be turned on when "
812 "using png_read_image");
813 /* Make sure this is set correctly */
814 png_ptr
->num_rows
= png_ptr
->height
;
817 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
818 * the above error case.
820 pass
= png_set_interlace_handling(png_ptr
);
823 if (png_ptr
->interlaced
)
825 "Cannot read interlaced image -- interlace handler disabled");
830 image_height
=png_ptr
->height
;
832 for (j
= 0; j
< pass
; j
++)
835 for (i
= 0; i
< image_height
; i
++)
837 png_read_row(png_ptr
, *rp
, NULL
);
842 #endif /* SEQUENTIAL_READ */
844 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
845 /* Read the end of the PNG file. Will not read past the end of the
846 * file, will verify the end is accurate, and will read any comments
847 * or time information at the end of the file, if info is not NULL.
850 png_read_end(png_structrp png_ptr
, png_inforp info_ptr
)
852 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
856 png_debug(1, "in png_read_end");
861 /* If png_read_end is called in the middle of reading the rows there may
862 * still be pending IDAT data and an owned zstream. Deal with this here.
864 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
865 if (png_chunk_unknown_handling(png_ptr
, png_IDAT
) == 0)
867 png_read_finish_IDAT(png_ptr
);
869 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
870 /* Report invalid palette index; added at libng-1.5.10 */
871 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
872 png_ptr
->num_palette_max
>= png_ptr
->num_palette
)
873 png_benign_error(png_ptr
, "Read palette index exceeding num_palette");
878 png_uint_32 length
= png_read_chunk_header(png_ptr
);
879 png_uint_32 chunk_name
= png_ptr
->chunk_name
;
881 if (chunk_name
!= png_IDAT
)
882 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
884 if (chunk_name
== png_IEND
)
885 png_handle_IEND(png_ptr
, info_ptr
, length
);
887 else if (chunk_name
== png_IHDR
)
888 png_handle_IHDR(png_ptr
, info_ptr
, length
);
890 else if (info_ptr
== NULL
)
891 png_crc_finish(png_ptr
, length
);
893 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
894 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
896 if (chunk_name
== png_IDAT
)
898 if ((length
> 0 && !(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
899 || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
900 png_benign_error(png_ptr
, ".Too many IDATs found");
902 png_handle_unknown(png_ptr
, info_ptr
, length
, keep
);
903 if (chunk_name
== png_PLTE
)
904 png_ptr
->mode
|= PNG_HAVE_PLTE
;
908 else if (chunk_name
== png_IDAT
)
910 /* Zero length IDATs are legal after the last IDAT has been
911 * read, but not after other chunks have been read. 1.6 does not
912 * always read all the deflate data; specifically it cannot be relied
913 * upon to read the Adler32 at the end. If it doesn't ignore IDAT
914 * chunks which are longer than zero as well:
916 if ((length
> 0 && !(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
917 || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
918 png_benign_error(png_ptr
, "..Too many IDATs found");
920 png_crc_finish(png_ptr
, length
);
922 else if (chunk_name
== png_PLTE
)
923 png_handle_PLTE(png_ptr
, info_ptr
, length
);
925 #ifdef PNG_READ_bKGD_SUPPORTED
926 else if (chunk_name
== png_bKGD
)
927 png_handle_bKGD(png_ptr
, info_ptr
, length
);
930 #ifdef PNG_READ_cHRM_SUPPORTED
931 else if (chunk_name
== png_cHRM
)
932 png_handle_cHRM(png_ptr
, info_ptr
, length
);
935 #ifdef PNG_READ_eXIf_SUPPORTED
936 else if (chunk_name
== png_eXIf
)
937 png_handle_eXIf(png_ptr
, info_ptr
, length
);
940 #ifdef PNG_READ_gAMA_SUPPORTED
941 else if (chunk_name
== png_gAMA
)
942 png_handle_gAMA(png_ptr
, info_ptr
, length
);
945 #ifdef PNG_READ_hIST_SUPPORTED
946 else if (chunk_name
== png_hIST
)
947 png_handle_hIST(png_ptr
, info_ptr
, length
);
950 #ifdef PNG_READ_oFFs_SUPPORTED
951 else if (chunk_name
== png_oFFs
)
952 png_handle_oFFs(png_ptr
, info_ptr
, length
);
955 #ifdef PNG_READ_pCAL_SUPPORTED
956 else if (chunk_name
== png_pCAL
)
957 png_handle_pCAL(png_ptr
, info_ptr
, length
);
960 #ifdef PNG_READ_sCAL_SUPPORTED
961 else if (chunk_name
== png_sCAL
)
962 png_handle_sCAL(png_ptr
, info_ptr
, length
);
965 #ifdef PNG_READ_pHYs_SUPPORTED
966 else if (chunk_name
== png_pHYs
)
967 png_handle_pHYs(png_ptr
, info_ptr
, length
);
970 #ifdef PNG_READ_sBIT_SUPPORTED
971 else if (chunk_name
== png_sBIT
)
972 png_handle_sBIT(png_ptr
, info_ptr
, length
);
975 #ifdef PNG_READ_sRGB_SUPPORTED
976 else if (chunk_name
== png_sRGB
)
977 png_handle_sRGB(png_ptr
, info_ptr
, length
);
980 #ifdef PNG_READ_iCCP_SUPPORTED
981 else if (chunk_name
== png_iCCP
)
982 png_handle_iCCP(png_ptr
, info_ptr
, length
);
985 #ifdef PNG_READ_sPLT_SUPPORTED
986 else if (chunk_name
== png_sPLT
)
987 png_handle_sPLT(png_ptr
, info_ptr
, length
);
990 #ifdef PNG_READ_tEXt_SUPPORTED
991 else if (chunk_name
== png_tEXt
)
992 png_handle_tEXt(png_ptr
, info_ptr
, length
);
995 #ifdef PNG_READ_tIME_SUPPORTED
996 else if (chunk_name
== png_tIME
)
997 png_handle_tIME(png_ptr
, info_ptr
, length
);
1000 #ifdef PNG_READ_tRNS_SUPPORTED
1001 else if (chunk_name
== png_tRNS
)
1002 png_handle_tRNS(png_ptr
, info_ptr
, length
);
1005 #ifdef PNG_READ_zTXt_SUPPORTED
1006 else if (chunk_name
== png_zTXt
)
1007 png_handle_zTXt(png_ptr
, info_ptr
, length
);
1010 #ifdef PNG_READ_iTXt_SUPPORTED
1011 else if (chunk_name
== png_iTXt
)
1012 png_handle_iTXt(png_ptr
, info_ptr
, length
);
1016 png_handle_unknown(png_ptr
, info_ptr
, length
,
1017 PNG_HANDLE_CHUNK_AS_DEFAULT
);
1018 } while ((png_ptr
->mode
& PNG_HAVE_IEND
) == 0);
1020 #endif /* SEQUENTIAL_READ */
1022 /* Free all memory used in the read struct */
1024 png_read_destroy(png_structrp png_ptr
)
1026 png_debug(1, "in png_read_destroy");
1028 #ifdef PNG_READ_GAMMA_SUPPORTED
1029 png_destroy_gamma_table(png_ptr
);
1032 png_free(png_ptr
, png_ptr
->big_row_buf
);
1033 png_ptr
->big_row_buf
= NULL
;
1034 png_free(png_ptr
, png_ptr
->big_prev_row
);
1035 png_ptr
->big_prev_row
= NULL
;
1036 png_free(png_ptr
, png_ptr
->read_buffer
);
1037 png_ptr
->read_buffer
= NULL
;
1039 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1040 png_free(png_ptr
, png_ptr
->palette_lookup
);
1041 png_ptr
->palette_lookup
= NULL
;
1042 png_free(png_ptr
, png_ptr
->quantize_index
);
1043 png_ptr
->quantize_index
= NULL
;
1046 if ((png_ptr
->free_me
& PNG_FREE_PLTE
) != 0)
1048 png_zfree(png_ptr
, png_ptr
->palette
);
1049 png_ptr
->palette
= NULL
;
1051 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1053 #if defined(PNG_tRNS_SUPPORTED) || \
1054 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1055 if ((png_ptr
->free_me
& PNG_FREE_TRNS
) != 0)
1057 png_free(png_ptr
, png_ptr
->trans_alpha
);
1058 png_ptr
->trans_alpha
= NULL
;
1060 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1063 inflateEnd(&png_ptr
->zstream
);
1065 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1066 png_free(png_ptr
, png_ptr
->save_buffer
);
1067 png_ptr
->save_buffer
= NULL
;
1070 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
1071 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1072 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
1073 png_ptr
->unknown_chunk
.data
= NULL
;
1076 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1077 png_free(png_ptr
, png_ptr
->chunk_list
);
1078 png_ptr
->chunk_list
= NULL
;
1081 #if defined(PNG_READ_EXPAND_SUPPORTED) && \
1082 defined(PNG_ARM_NEON_IMPLEMENTATION)
1083 png_free(png_ptr
, png_ptr
->riffled_palette
);
1084 png_ptr
->riffled_palette
= NULL
;
1087 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
1088 * callbacks are still set at this point. They are required to complete the
1089 * destruction of the png_struct itself.
1093 /* Free all memory used by the read */
1095 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1096 png_infopp end_info_ptr_ptr
)
1098 png_structrp png_ptr
= NULL
;
1100 png_debug(1, "in png_destroy_read_struct");
1102 if (png_ptr_ptr
!= NULL
)
1103 png_ptr
= *png_ptr_ptr
;
1105 if (png_ptr
== NULL
)
1108 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1109 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1110 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1112 png_destroy_info_struct(png_ptr
, end_info_ptr_ptr
);
1113 png_destroy_info_struct(png_ptr
, info_ptr_ptr
);
1115 *png_ptr_ptr
= NULL
;
1116 png_read_destroy(png_ptr
);
1117 png_destroy_png_struct(png_ptr
);
1121 png_set_read_status_fn(png_structrp png_ptr
, png_read_status_ptr read_row_fn
)
1123 if (png_ptr
== NULL
)
1126 png_ptr
->read_row_fn
= read_row_fn
;
1130 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1131 #ifdef PNG_INFO_IMAGE_SUPPORTED
1133 png_read_png(png_structrp png_ptr
, png_inforp info_ptr
,
1134 int transforms
, voidp params
)
1136 png_debug(1, "in png_read_png");
1138 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1141 /* png_read_info() gives us all of the information from the
1142 * PNG file before the first IDAT (image data chunk).
1144 png_read_info(png_ptr
, info_ptr
);
1145 if (info_ptr
->height
> PNG_UINT_32_MAX
/(sizeof (png_bytep
)))
1146 png_error(png_ptr
, "Image is too high to process with png_read_png()");
1148 /* -------------- image transformations start here ------------------- */
1149 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1150 * is not implemented. This will only happen in de-configured (non-default)
1151 * libpng builds. The results can be unexpected - png_read_png may return
1152 * short or mal-formed rows because the transform is skipped.
1155 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1157 if ((transforms
& PNG_TRANSFORM_SCALE_16
) != 0)
1158 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1159 * did in earlier versions, while "scale_16" is now more accurate.
1161 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1162 png_set_scale_16(png_ptr
);
1164 png_app_error(png_ptr
, "PNG_TRANSFORM_SCALE_16 not supported");
1167 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1168 * latter by doing SCALE first. This is ok and allows apps not to check for
1169 * which is supported to get the right answer.
1171 if ((transforms
& PNG_TRANSFORM_STRIP_16
) != 0)
1172 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1173 png_set_strip_16(png_ptr
);
1175 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_16 not supported");
1178 /* Strip alpha bytes from the input data without combining with
1179 * the background (not recommended).
1181 if ((transforms
& PNG_TRANSFORM_STRIP_ALPHA
) != 0)
1182 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1183 png_set_strip_alpha(png_ptr
);
1185 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_ALPHA not supported");
1188 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1189 * byte into separate bytes (useful for paletted and grayscale images).
1191 if ((transforms
& PNG_TRANSFORM_PACKING
) != 0)
1192 #ifdef PNG_READ_PACK_SUPPORTED
1193 png_set_packing(png_ptr
);
1195 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKING not supported");
1198 /* Change the order of packed pixels to least significant bit first
1199 * (not useful if you are using png_set_packing).
1201 if ((transforms
& PNG_TRANSFORM_PACKSWAP
) != 0)
1202 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1203 png_set_packswap(png_ptr
);
1205 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKSWAP not supported");
1208 /* Expand paletted colors into true RGB triplets
1209 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1210 * Expand paletted or RGB images with transparency to full alpha
1211 * channels so the data will be available as RGBA quartets.
1213 if ((transforms
& PNG_TRANSFORM_EXPAND
) != 0)
1214 #ifdef PNG_READ_EXPAND_SUPPORTED
1215 png_set_expand(png_ptr
);
1217 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND not supported");
1220 /* We don't handle background color or gamma transformation or quantizing.
1223 /* Invert monochrome files to have 0 as white and 1 as black
1225 if ((transforms
& PNG_TRANSFORM_INVERT_MONO
) != 0)
1226 #ifdef PNG_READ_INVERT_SUPPORTED
1227 png_set_invert_mono(png_ptr
);
1229 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_MONO not supported");
1232 /* If you want to shift the pixel values from the range [0,255] or
1233 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1234 * colors were originally in:
1236 if ((transforms
& PNG_TRANSFORM_SHIFT
) != 0)
1237 #ifdef PNG_READ_SHIFT_SUPPORTED
1238 if ((info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
1239 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1241 png_app_error(png_ptr
, "PNG_TRANSFORM_SHIFT not supported");
1244 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1245 if ((transforms
& PNG_TRANSFORM_BGR
) != 0)
1246 #ifdef PNG_READ_BGR_SUPPORTED
1247 png_set_bgr(png_ptr
);
1249 png_app_error(png_ptr
, "PNG_TRANSFORM_BGR not supported");
1252 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1253 if ((transforms
& PNG_TRANSFORM_SWAP_ALPHA
) != 0)
1254 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1255 png_set_swap_alpha(png_ptr
);
1257 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1260 /* Swap bytes of 16-bit files to least significant byte first */
1261 if ((transforms
& PNG_TRANSFORM_SWAP_ENDIAN
) != 0)
1262 #ifdef PNG_READ_SWAP_SUPPORTED
1263 png_set_swap(png_ptr
);
1265 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1268 /* Added at libpng-1.2.41 */
1269 /* Invert the alpha channel from opacity to transparency */
1270 if ((transforms
& PNG_TRANSFORM_INVERT_ALPHA
) != 0)
1271 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1272 png_set_invert_alpha(png_ptr
);
1274 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1277 /* Added at libpng-1.2.41 */
1278 /* Expand grayscale image to RGB */
1279 if ((transforms
& PNG_TRANSFORM_GRAY_TO_RGB
) != 0)
1280 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1281 png_set_gray_to_rgb(png_ptr
);
1283 png_app_error(png_ptr
, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1286 /* Added at libpng-1.5.4 */
1287 if ((transforms
& PNG_TRANSFORM_EXPAND_16
) != 0)
1288 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1289 png_set_expand_16(png_ptr
);
1291 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND_16 not supported");
1294 /* We don't handle adding filler bytes */
1296 /* We use png_read_image and rely on that for interlace handling, but we also
1297 * call png_read_update_info therefore must turn on interlace handling now:
1299 (void)png_set_interlace_handling(png_ptr
);
1301 /* Optional call to gamma correct and add the background to the palette
1302 * and update info structure. REQUIRED if you are expecting libpng to
1303 * update the palette for you (i.e., you selected such a transform above).
1305 png_read_update_info(png_ptr
, info_ptr
);
1307 /* -------------- image transformations end here ------------------- */
1309 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1310 if (info_ptr
->row_pointers
== NULL
)
1314 info_ptr
->row_pointers
= png_voidcast(png_bytepp
, png_malloc(png_ptr
,
1315 info_ptr
->height
* (sizeof (png_bytep
))));
1317 for (iptr
=0; iptr
<info_ptr
->height
; iptr
++)
1318 info_ptr
->row_pointers
[iptr
] = NULL
;
1320 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1322 for (iptr
= 0; iptr
< info_ptr
->height
; iptr
++)
1323 info_ptr
->row_pointers
[iptr
] = png_voidcast(png_bytep
,
1324 png_malloc(png_ptr
, info_ptr
->rowbytes
));
1327 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1328 info_ptr
->valid
|= PNG_INFO_IDAT
;
1330 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1331 png_read_end(png_ptr
, info_ptr
);
1335 #endif /* INFO_IMAGE */
1336 #endif /* SEQUENTIAL_READ */
1338 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1341 * This code currently relies on the sequential reader, though it could easily
1342 * be made to work with the progressive one.
1344 /* Arguments to png_image_finish_read: */
1346 /* Encoding of PNG data (used by the color-map code) */
1347 # define P_NOTSET 0 /* File encoding not yet known */
1348 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1349 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1350 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1351 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1353 /* Color-map processing: after libpng has run on the PNG image further
1354 * processing may be needed to convert the data to color-map indices.
1356 #define PNG_CMAP_NONE 0
1357 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1358 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1359 #define PNG_CMAP_RGB 3 /* Process RGB data */
1360 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1362 /* The following document where the background is for each processing case. */
1363 #define PNG_CMAP_NONE_BACKGROUND 256
1364 #define PNG_CMAP_GA_BACKGROUND 231
1365 #define PNG_CMAP_TRANS_BACKGROUND 254
1366 #define PNG_CMAP_RGB_BACKGROUND 256
1367 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1374 png_int_32 row_stride
;
1376 png_const_colorp background
;
1377 /* Local variables: */
1378 png_voidp local_row
;
1379 png_voidp first_row
;
1380 ptrdiff_t row_bytes
; /* step between rows */
1381 int file_encoding
; /* E_ values above */
1382 png_fixed_point gamma_to_linear
; /* For P_FILE, reciprocal of gamma */
1383 int colormap_processing
; /* PNG_CMAP_ values above */
1384 } png_image_read_control
;
1386 /* Do all the *safe* initialization - 'safe' means that png_error won't be
1387 * called, so setting up the jmp_buf is not required. This means that anything
1388 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1389 * instead so that control is returned safely back to this routine.
1392 png_image_read_init(png_imagep image
)
1394 if (image
->opaque
== NULL
)
1396 png_structp png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, image
,
1397 png_safe_error
, png_safe_warning
);
1399 /* And set the rest of the structure to NULL to ensure that the various
1400 * fields are consistent.
1402 memset(image
, 0, (sizeof *image
));
1403 image
->version
= PNG_IMAGE_VERSION
;
1405 if (png_ptr
!= NULL
)
1407 png_infop info_ptr
= png_create_info_struct(png_ptr
);
1409 if (info_ptr
!= NULL
)
1411 png_controlp control
= png_voidcast(png_controlp
,
1412 png_malloc_warn(png_ptr
, (sizeof *control
)));
1414 if (control
!= NULL
)
1416 memset(control
, 0, (sizeof *control
));
1418 control
->png_ptr
= png_ptr
;
1419 control
->info_ptr
= info_ptr
;
1420 control
->for_write
= 0;
1422 image
->opaque
= control
;
1426 /* Error clean up */
1427 png_destroy_info_struct(png_ptr
, &info_ptr
);
1430 png_destroy_read_struct(&png_ptr
, NULL
, NULL
);
1433 return png_image_error(image
, "png_image_read: out of memory");
1436 return png_image_error(image
, "png_image_read: opaque pointer not NULL");
1439 /* Utility to find the base format of a PNG file from a png_struct. */
1441 png_image_format(png_structrp png_ptr
)
1443 png_uint_32 format
= 0;
1445 if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
1446 format
|= PNG_FORMAT_FLAG_COLOR
;
1448 if ((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
1449 format
|= PNG_FORMAT_FLAG_ALPHA
;
1451 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1452 * sets the png_struct fields; that's all we are interested in here. The
1453 * precise interaction with an app call to png_set_tRNS and PNG file reading
1456 else if (png_ptr
->num_trans
> 0)
1457 format
|= PNG_FORMAT_FLAG_ALPHA
;
1459 if (png_ptr
->bit_depth
== 16)
1460 format
|= PNG_FORMAT_FLAG_LINEAR
;
1462 if ((png_ptr
->color_type
& PNG_COLOR_MASK_PALETTE
) != 0)
1463 format
|= PNG_FORMAT_FLAG_COLORMAP
;
1468 /* Is the given gamma significantly different from sRGB? The test is the same
1469 * one used in pngrtran.c when deciding whether to do gamma correction. The
1470 * arithmetic optimizes the division by using the fact that the inverse of the
1471 * file sRGB gamma is 2.2
1474 png_gamma_not_sRGB(png_fixed_point g
)
1478 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1482 return png_gamma_significant((g
* 11 + 2)/5 /* i.e. *2.2, rounded */);
1488 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
1489 * header and fill in all the information. This is executed in a safe context,
1490 * unlike the init routine above.
1493 png_image_read_header(png_voidp argument
)
1495 png_imagep image
= png_voidcast(png_imagep
, argument
);
1496 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1497 png_inforp info_ptr
= image
->opaque
->info_ptr
;
1499 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
1500 png_set_benign_errors(png_ptr
, 1/*warn*/);
1502 png_read_info(png_ptr
, info_ptr
);
1504 /* Do this the fast way; just read directly out of png_struct. */
1505 image
->width
= png_ptr
->width
;
1506 image
->height
= png_ptr
->height
;
1509 png_uint_32 format
= png_image_format(png_ptr
);
1511 image
->format
= format
;
1513 #ifdef PNG_COLORSPACE_SUPPORTED
1514 /* Does the colorspace match sRGB? If there is no color endpoint
1515 * (colorant) information assume yes, otherwise require the
1516 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1517 * colorspace has been determined to be invalid ignore it.
1519 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0 && ((png_ptr
->colorspace
.flags
1520 & (PNG_COLORSPACE_HAVE_ENDPOINTS
|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB
|
1521 PNG_COLORSPACE_INVALID
)) == PNG_COLORSPACE_HAVE_ENDPOINTS
))
1522 image
->flags
|= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
;
1526 /* We need the maximum number of entries regardless of the format the
1527 * application sets here.
1530 png_uint_32 cmap_entries
;
1532 switch (png_ptr
->color_type
)
1534 case PNG_COLOR_TYPE_GRAY
:
1535 cmap_entries
= 1U << png_ptr
->bit_depth
;
1538 case PNG_COLOR_TYPE_PALETTE
:
1539 cmap_entries
= (png_uint_32
)png_ptr
->num_palette
;
1547 if (cmap_entries
> 256)
1550 image
->colormap_entries
= cmap_entries
;
1556 #ifdef PNG_STDIO_SUPPORTED
1558 png_image_begin_read_from_stdio(png_imagep image
, FILE* file
)
1560 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1564 if (png_image_read_init(image
) != 0)
1566 /* This is slightly evil, but png_init_io doesn't do anything other
1567 * than this and we haven't changed the standard IO functions so
1568 * this saves a 'safe' function.
1570 image
->opaque
->png_ptr
->io_ptr
= file
;
1571 return png_safe_execute(image
, png_image_read_header
, image
);
1576 return png_image_error(image
,
1577 "png_image_begin_read_from_stdio: invalid argument");
1580 else if (image
!= NULL
)
1581 return png_image_error(image
,
1582 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1588 png_image_begin_read_from_file(png_imagep image
, const char *file_name
)
1590 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1592 if (file_name
!= NULL
)
1594 FILE *fp
= fopen(file_name
, "rb");
1598 if (png_image_read_init(image
) != 0)
1600 image
->opaque
->png_ptr
->io_ptr
= fp
;
1601 image
->opaque
->owned_file
= 1;
1602 return png_safe_execute(image
, png_image_read_header
, image
);
1605 /* Clean up: just the opened file. */
1610 return png_image_error(image
, strerror(errno
));
1614 return png_image_error(image
,
1615 "png_image_begin_read_from_file: invalid argument");
1618 else if (image
!= NULL
)
1619 return png_image_error(image
,
1620 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1626 static void PNGCBAPI
1627 png_image_memory_read(png_structp png_ptr
, png_bytep out
, size_t need
)
1629 if (png_ptr
!= NULL
)
1631 png_imagep image
= png_voidcast(png_imagep
, png_ptr
->io_ptr
);
1634 png_controlp cp
= image
->opaque
;
1637 png_const_bytep memory
= cp
->memory
;
1638 size_t size
= cp
->size
;
1640 if (memory
!= NULL
&& size
>= need
)
1642 memcpy(out
, memory
, need
);
1643 cp
->memory
= memory
+ need
;
1644 cp
->size
= size
- need
;
1648 png_error(png_ptr
, "read beyond end of data");
1652 png_error(png_ptr
, "invalid memory read");
1656 int PNGAPI
png_image_begin_read_from_memory(png_imagep image
,
1657 png_const_voidp memory
, size_t size
)
1659 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1661 if (memory
!= NULL
&& size
> 0)
1663 if (png_image_read_init(image
) != 0)
1665 /* Now set the IO functions to read from the memory buffer and
1666 * store it into io_ptr. Again do this in-place to avoid calling a
1667 * libpng function that requires error handling.
1669 image
->opaque
->memory
= png_voidcast(png_const_bytep
, memory
);
1670 image
->opaque
->size
= size
;
1671 image
->opaque
->png_ptr
->io_ptr
= image
;
1672 image
->opaque
->png_ptr
->read_data_fn
= png_image_memory_read
;
1674 return png_safe_execute(image
, png_image_read_header
, image
);
1679 return png_image_error(image
,
1680 "png_image_begin_read_from_memory: invalid argument");
1683 else if (image
!= NULL
)
1684 return png_image_error(image
,
1685 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1690 /* Utility function to skip chunks that are not used by the simplified image
1691 * read functions and an appropriate macro to call it.
1693 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1695 png_image_skip_unused_chunks(png_structrp png_ptr
)
1697 /* Prepare the reader to ignore all recognized chunks whose data will not
1698 * be used, i.e., all chunks recognized by libpng except for those
1699 * involved in basic image reading:
1701 * IHDR, PLTE, IDAT, IEND
1703 * Or image data handling:
1705 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1707 * This provides a small performance improvement and eliminates any
1708 * potential vulnerability to security problems in the unused chunks.
1710 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1711 * too. This allows the simplified API to be compiled without iCCP support,
1712 * however if the support is there the chunk is still checked to detect
1713 * errors (which are unfortunately quite common.)
1716 static const png_byte chunks_to_process
[] = {
1717 98, 75, 71, 68, '\0', /* bKGD */
1718 99, 72, 82, 77, '\0', /* cHRM */
1719 103, 65, 77, 65, '\0', /* gAMA */
1720 # ifdef PNG_READ_iCCP_SUPPORTED
1721 105, 67, 67, 80, '\0', /* iCCP */
1723 115, 66, 73, 84, '\0', /* sBIT */
1724 115, 82, 71, 66, '\0', /* sRGB */
1727 /* Ignore unknown chunks and all other chunks except for the
1728 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1730 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_NEVER
,
1733 /* But do not ignore image data handling chunks */
1734 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_AS_DEFAULT
,
1735 chunks_to_process
, (int)/*SAFE*/(sizeof chunks_to_process
)/5);
1739 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1741 # define PNG_SKIP_CHUNKS(p) ((void)0)
1742 #endif /* HANDLE_AS_UNKNOWN */
1744 /* The following macro gives the exact rounded answer for all values in the
1745 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1746 * the correct numbers 0..5
1748 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1750 /* Utility functions to make particular color-maps */
1752 set_file_encoding(png_image_read_control
*display
)
1754 png_fixed_point g
= display
->image
->opaque
->png_ptr
->colorspace
.gamma
;
1755 if (png_gamma_significant(g
) != 0)
1757 if (png_gamma_not_sRGB(g
) != 0)
1759 display
->file_encoding
= P_FILE
;
1760 display
->gamma_to_linear
= png_reciprocal(g
);
1764 display
->file_encoding
= P_sRGB
;
1768 display
->file_encoding
= P_LINEAR8
;
1772 decode_gamma(png_image_read_control
*display
, png_uint_32 value
, int encoding
)
1774 if (encoding
== P_FILE
) /* double check */
1775 encoding
= display
->file_encoding
;
1777 if (encoding
== P_NOTSET
) /* must be the file encoding */
1779 set_file_encoding(display
);
1780 encoding
= display
->file_encoding
;
1786 value
= png_gamma_16bit_correct(value
*257, display
->gamma_to_linear
);
1790 value
= png_sRGB_table
[value
];
1802 png_error(display
->image
->opaque
->png_ptr
,
1803 "unexpected encoding (internal error)");
1811 png_colormap_compose(png_image_read_control
*display
,
1812 png_uint_32 foreground
, int foreground_encoding
, png_uint_32 alpha
,
1813 png_uint_32 background
, int encoding
)
1815 /* The file value is composed on the background, the background has the given
1816 * encoding and so does the result, the file is encoded with P_FILE and the
1817 * file and alpha are 8-bit values. The (output) encoding will always be
1818 * P_LINEAR or P_sRGB.
1820 png_uint_32 f
= decode_gamma(display
, foreground
, foreground_encoding
);
1821 png_uint_32 b
= decode_gamma(display
, background
, encoding
);
1823 /* The alpha is always an 8-bit value (it comes from the palette), the value
1824 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1826 f
= f
* alpha
+ b
* (255-alpha
);
1828 if (encoding
== P_LINEAR
)
1830 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1831 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1833 f
*= 257; /* Now scaled by 65535 */
1835 f
= (f
+32768) >> 16;
1839 f
= PNG_sRGB_FROM_LINEAR(f
);
1844 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1848 png_create_colormap_entry(png_image_read_control
*display
,
1849 png_uint_32 ip
, png_uint_32 red
, png_uint_32 green
, png_uint_32 blue
,
1850 png_uint_32 alpha
, int encoding
)
1852 png_imagep image
= display
->image
;
1853 int output_encoding
= (image
->format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
1855 int convert_to_Y
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) == 0 &&
1856 (red
!= green
|| green
!= blue
);
1859 png_error(image
->opaque
->png_ptr
, "color-map index out of range");
1861 /* Update the cache with whether the file gamma is significantly different
1864 if (encoding
== P_FILE
)
1866 if (display
->file_encoding
== P_NOTSET
)
1867 set_file_encoding(display
);
1869 /* Note that the cached value may be P_FILE too, but if it is then the
1870 * gamma_to_linear member has been set.
1872 encoding
= display
->file_encoding
;
1875 if (encoding
== P_FILE
)
1877 png_fixed_point g
= display
->gamma_to_linear
;
1879 red
= png_gamma_16bit_correct(red
*257, g
);
1880 green
= png_gamma_16bit_correct(green
*257, g
);
1881 blue
= png_gamma_16bit_correct(blue
*257, g
);
1883 if (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
)
1886 encoding
= P_LINEAR
;
1891 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1892 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1893 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1898 else if (encoding
== P_LINEAR8
)
1900 /* This encoding occurs quite frequently in test cases because PngSuite
1901 * includes a gAMA 1.0 chunk with most images.
1907 encoding
= P_LINEAR
;
1910 else if (encoding
== P_sRGB
&&
1911 (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
))
1913 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1916 red
= png_sRGB_table
[red
];
1917 green
= png_sRGB_table
[green
];
1918 blue
= png_sRGB_table
[blue
];
1920 encoding
= P_LINEAR
;
1923 /* This is set if the color isn't gray but the output is. */
1924 if (encoding
== P_LINEAR
)
1926 if (convert_to_Y
!= 0)
1928 /* NOTE: these values are copied from png_do_rgb_to_gray */
1929 png_uint_32 y
= (png_uint_32
)6968 * red
+ (png_uint_32
)23434 * green
+
1930 (png_uint_32
)2366 * blue
;
1932 if (output_encoding
== P_LINEAR
)
1933 y
= (y
+ 16384) >> 15;
1937 /* y is scaled by 32768, we need it scaled by 255: */
1940 y
= PNG_sRGB_FROM_LINEAR((y
+ 64) >> 7);
1941 alpha
= PNG_DIV257(alpha
);
1945 blue
= red
= green
= y
;
1948 else if (output_encoding
== P_sRGB
)
1950 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1951 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1952 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1953 alpha
= PNG_DIV257(alpha
);
1958 if (encoding
!= output_encoding
)
1959 png_error(image
->opaque
->png_ptr
, "bad encoding (internal error)");
1961 /* Store the value. */
1963 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1964 int afirst
= (image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0 &&
1965 (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
1969 # ifdef PNG_FORMAT_BGR_SUPPORTED
1970 int bgr
= (image
->format
& PNG_FORMAT_FLAG_BGR
) != 0 ? 2 : 0;
1975 if (output_encoding
== P_LINEAR
)
1977 png_uint_16p entry
= png_voidcast(png_uint_16p
, display
->colormap
);
1979 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
1981 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1982 * value, if less than 65535 (this is, effectively, composite on black
1983 * if the alpha channel is removed.)
1985 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
1988 entry
[afirst
? 0 : 3] = (png_uint_16
)alpha
;
1996 blue
= (blue
* alpha
+ 32767U)/65535U;
1997 green
= (green
* alpha
+ 32767U)/65535U;
1998 red
= (red
* alpha
+ 32767U)/65535U;
2002 red
= green
= blue
= 0;
2004 entry
[afirst
+ (2 ^ bgr
)] = (png_uint_16
)blue
;
2005 entry
[afirst
+ 1] = (png_uint_16
)green
;
2006 entry
[afirst
+ bgr
] = (png_uint_16
)red
;
2010 entry
[1 ^ afirst
] = (png_uint_16
)alpha
;
2017 green
= (green
* alpha
+ 32767U)/65535U;
2022 entry
[afirst
] = (png_uint_16
)green
;
2030 else /* output encoding is P_sRGB */
2032 png_bytep entry
= png_voidcast(png_bytep
, display
->colormap
);
2034 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
2036 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
2039 entry
[afirst
? 0 : 3] = (png_byte
)alpha
;
2042 entry
[afirst
+ (2 ^ bgr
)] = (png_byte
)blue
;
2043 entry
[afirst
+ 1] = (png_byte
)green
;
2044 entry
[afirst
+ bgr
] = (png_byte
)red
;
2048 entry
[1 ^ afirst
] = (png_byte
)alpha
;
2051 entry
[afirst
] = (png_byte
)green
;
2069 make_gray_file_colormap(png_image_read_control
*display
)
2073 for (i
=0; i
<256; ++i
)
2074 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_FILE
);
2080 make_gray_colormap(png_image_read_control
*display
)
2084 for (i
=0; i
<256; ++i
)
2085 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_sRGB
);
2089 #define PNG_GRAY_COLORMAP_ENTRIES 256
2092 make_ga_colormap(png_image_read_control
*display
)
2096 /* Alpha is retained, the output will be a color-map with entries
2097 * selected by six levels of alpha. One transparent entry, 6 gray
2098 * levels for all the intermediate alpha values, leaving 230 entries
2099 * for the opaque grays. The color-map entries are the six values
2100 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2103 * if (alpha > 229) // opaque
2105 * // The 231 entries are selected to make the math below work:
2107 * entry = (231 * gray + 128) >> 8;
2109 * else if (alpha < 26) // transparent
2114 * else // partially opaque
2116 * base = 226 + 6 * PNG_DIV51(alpha);
2117 * entry = PNG_DIV51(gray);
2123 unsigned int gray
= (i
* 256 + 115) / 231;
2124 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
, 255, P_sRGB
);
2127 /* 255 is used here for the component values for consistency with the code
2128 * that undoes premultiplication in pngwrite.c.
2130 png_create_colormap_entry(display
, i
++, 255, 255, 255, 0, P_sRGB
);
2137 png_create_colormap_entry(display
, i
++, g
*51, g
*51, g
*51, a
*51,
2144 #define PNG_GA_COLORMAP_ENTRIES 256
2147 make_rgb_colormap(png_image_read_control
*display
)
2151 /* Build a 6x6x6 opaque RGB cube */
2152 for (i
=r
=0; r
<6; ++r
)
2161 png_create_colormap_entry(display
, i
++, r
*51, g
*51, b
*51, 255,
2169 #define PNG_RGB_COLORMAP_ENTRIES 216
2171 /* Return a palette index to the above palette given three 8-bit sRGB values. */
2172 #define PNG_RGB_INDEX(r,g,b) \
2173 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2176 png_image_read_colormap(png_voidp argument
)
2178 png_image_read_control
*display
=
2179 png_voidcast(png_image_read_control
*, argument
);
2180 png_imagep image
= display
->image
;
2182 png_structrp png_ptr
= image
->opaque
->png_ptr
;
2183 png_uint_32 output_format
= image
->format
;
2184 int output_encoding
= (output_format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
2187 unsigned int cmap_entries
;
2188 unsigned int output_processing
; /* Output processing option */
2189 unsigned int data_encoding
= P_NOTSET
; /* Encoding libpng must produce */
2191 /* Background information; the background color and the index of this color
2192 * in the color-map if it exists (else 256).
2194 unsigned int background_index
= 256;
2195 png_uint_32 back_r
, back_g
, back_b
;
2197 /* Flags to accumulate things that need to be done to the input. */
2198 int expand_tRNS
= 0;
2200 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2201 * very difficult to do, the results look awful, and it is difficult to see
2202 * what possible use it is because the application can't control the
2205 if (((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0 ||
2206 png_ptr
->num_trans
> 0) /* alpha in input */ &&
2207 ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0) /* no alpha in output */)
2209 if (output_encoding
== P_LINEAR
) /* compose on black */
2210 back_b
= back_g
= back_r
= 0;
2212 else if (display
->background
== NULL
/* no way to remove it */)
2214 "background color must be supplied to remove alpha/transparency");
2216 /* Get a copy of the background color (this avoids repeating the checks
2217 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2222 back_g
= display
->background
->green
;
2223 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) != 0)
2225 back_r
= display
->background
->red
;
2226 back_b
= display
->background
->blue
;
2229 back_b
= back_r
= back_g
;
2233 else if (output_encoding
== P_LINEAR
)
2234 back_b
= back_r
= back_g
= 65535;
2237 back_b
= back_r
= back_g
= 255;
2239 /* Default the input file gamma if required - this is necessary because
2240 * libpng assumes that if no gamma information is present the data is in the
2241 * output format, but the simplified API deduces the gamma from the input
2244 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_GAMMA
) == 0)
2246 /* Do this directly, not using the png_colorspace functions, to ensure
2247 * that it happens even if the colorspace is invalid (though probably if
2248 * it is the setting will be ignored) Note that the same thing can be
2249 * achieved at the application interface with png_set_gAMA.
2251 if (png_ptr
->bit_depth
== 16 &&
2252 (image
->flags
& PNG_IMAGE_FLAG_16BIT_sRGB
) == 0)
2253 png_ptr
->colorspace
.gamma
= PNG_GAMMA_LINEAR
;
2256 png_ptr
->colorspace
.gamma
= PNG_GAMMA_sRGB_INVERSE
;
2258 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
2261 /* Decide what to do based on the PNG color type of the input data. The
2262 * utility function png_create_colormap_entry deals with most aspects of the
2263 * output transformations; this code works out how to produce bytes of
2264 * color-map entries from the original format.
2266 switch (png_ptr
->color_type
)
2268 case PNG_COLOR_TYPE_GRAY
:
2269 if (png_ptr
->bit_depth
<= 8)
2271 /* There at most 256 colors in the output, regardless of
2274 unsigned int step
, i
, val
, trans
= 256/*ignore*/, back_alpha
= 0;
2276 cmap_entries
= 1U << png_ptr
->bit_depth
;
2277 if (cmap_entries
> image
->colormap_entries
)
2278 png_error(png_ptr
, "gray[8] color-map: too few entries");
2280 step
= 255 / (cmap_entries
- 1);
2281 output_processing
= PNG_CMAP_NONE
;
2283 /* If there is a tRNS chunk then this either selects a transparent
2284 * value or, if the output has no alpha, the background color.
2286 if (png_ptr
->num_trans
> 0)
2288 trans
= png_ptr
->trans_color
.gray
;
2290 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0)
2291 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2294 /* png_create_colormap_entry just takes an RGBA and writes the
2295 * corresponding color-map entry using the format from 'image',
2296 * including the required conversion to sRGB or linear as
2297 * appropriate. The input values are always either sRGB (if the
2298 * gamma correction flag is 0) or 0..255 scaled file encoded values
2299 * (if the function must gamma correct them).
2301 for (i
=val
=0; i
<cmap_entries
; ++i
, val
+= step
)
2303 /* 'i' is a file value. While this will result in duplicated
2304 * entries for 8-bit non-sRGB encoded files it is necessary to
2305 * have non-gamma corrected values to do tRNS handling.
2308 png_create_colormap_entry(display
, i
, val
, val
, val
, 255,
2309 P_FILE
/*8-bit with file gamma*/);
2311 /* Else this entry is transparent. The colors don't matter if
2312 * there is an alpha channel (back_alpha == 0), but it does no
2313 * harm to pass them in; the values are not set above so this
2316 * NOTE: this preserves the full precision of the application
2317 * supplied background color when it is used.
2320 png_create_colormap_entry(display
, i
, back_r
, back_g
, back_b
,
2321 back_alpha
, output_encoding
);
2324 /* We need libpng to preserve the original encoding. */
2325 data_encoding
= P_FILE
;
2327 /* The rows from libpng, while technically gray values, are now also
2328 * color-map indices; however, they may need to be expanded to 1
2329 * byte per pixel. This is what png_set_packing does (i.e., it
2330 * unpacks the bit values into bytes.)
2332 if (png_ptr
->bit_depth
< 8)
2333 png_set_packing(png_ptr
);
2336 else /* bit depth is 16 */
2338 /* The 16-bit input values can be converted directly to 8-bit gamma
2339 * encoded values; however, if a tRNS chunk is present 257 color-map
2340 * entries are required. This means that the extra entry requires
2341 * special processing; add an alpha channel, sacrifice gray level
2342 * 254 and convert transparent (alpha==0) entries to that.
2344 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2345 * same time to minimize quality loss. If a tRNS chunk is present
2346 * this means libpng must handle it too; otherwise it is impossible
2347 * to do the exact match on the 16-bit value.
2349 * If the output has no alpha channel *and* the background color is
2350 * gray then it is possible to let libpng handle the substitution by
2351 * ensuring that the corresponding gray level matches the background
2354 data_encoding
= P_sRGB
;
2356 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2357 png_error(png_ptr
, "gray[16] color-map: too few entries");
2359 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2361 if (png_ptr
->num_trans
> 0)
2363 unsigned int back_alpha
;
2365 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2370 if (back_r
== back_g
&& back_g
== back_b
)
2372 /* Background is gray; no special processing will be
2376 png_uint_32 gray
= back_g
;
2378 if (output_encoding
== P_LINEAR
)
2380 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2382 /* And make sure the corresponding palette entry
2385 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2386 back_g
, 65535, P_LINEAR
);
2389 /* The background passed to libpng, however, must be the
2392 c
.index
= 0; /*unused*/
2393 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2395 /* NOTE: does this work without expanding tRNS to alpha?
2396 * It should be the color->gray case below apparently
2399 png_set_background_fixed(png_ptr
, &c
,
2400 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2401 0/*gamma: not used*/);
2403 output_processing
= PNG_CMAP_NONE
;
2407 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2412 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2416 /* output_processing means that the libpng-processed row will be
2417 * 8-bit GA and it has to be processing to single byte color-map
2418 * values. Entry 254 is replaced by either a completely
2419 * transparent entry or by the background color at full
2420 * precision (and the background color is not a simple gray
2421 * level in this case.)
2424 output_processing
= PNG_CMAP_TRANS
;
2425 background_index
= 254;
2427 /* And set (overwrite) color-map entry 254 to the actual
2428 * background color at full precision.
2430 png_create_colormap_entry(display
, 254, back_r
, back_g
, back_b
,
2431 back_alpha
, output_encoding
);
2435 output_processing
= PNG_CMAP_NONE
;
2439 case PNG_COLOR_TYPE_GRAY_ALPHA
:
2440 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2441 * of 65536 combinations. If, however, the alpha channel is to be
2442 * removed there are only 256 possibilities if the background is gray.
2443 * (Otherwise there is a subset of the 65536 possibilities defined by
2444 * the triangle between black, white and the background color.)
2446 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2447 * worry about tRNS matching - tRNS is ignored if there is an alpha
2450 data_encoding
= P_sRGB
;
2452 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2454 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2455 png_error(png_ptr
, "gray+alpha color-map: too few entries");
2457 cmap_entries
= (unsigned int)make_ga_colormap(display
);
2459 background_index
= PNG_CMAP_GA_BACKGROUND
;
2460 output_processing
= PNG_CMAP_GA
;
2463 else /* alpha is removed */
2465 /* Alpha must be removed as the PNG data is processed when the
2466 * background is a color because the G and A channels are
2467 * independent and the vector addition (non-parallel vectors) is a
2470 * This can be reduced to the same algorithm as above by making a
2471 * colormap containing gray levels (for the opaque grays), a
2472 * background entry (for a transparent pixel) and a set of four six
2473 * level color values, one set for each intermediate alpha value.
2474 * See the comments in make_ga_colormap for how this works in the
2475 * per-pixel processing.
2477 * If the background is gray, however, we only need a 256 entry gray
2478 * level color map. It is sufficient to make the entry generated
2479 * for the background color be exactly the color specified.
2481 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0 ||
2482 (back_r
== back_g
&& back_g
== back_b
))
2484 /* Background is gray; no special processing will be required. */
2486 png_uint_32 gray
= back_g
;
2488 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2489 png_error(png_ptr
, "gray-alpha color-map: too few entries");
2491 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2493 if (output_encoding
== P_LINEAR
)
2495 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2497 /* And make sure the corresponding palette entry matches. */
2498 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2499 back_g
, 65535, P_LINEAR
);
2502 /* The background passed to libpng, however, must be the sRGB
2505 c
.index
= 0; /*unused*/
2506 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2508 png_set_background_fixed(png_ptr
, &c
,
2509 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2510 0/*gamma: not used*/);
2512 output_processing
= PNG_CMAP_NONE
;
2519 /* This is the same as png_make_ga_colormap, above, except that
2520 * the entries are all opaque.
2522 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2523 png_error(png_ptr
, "ga-alpha color-map: too few entries");
2528 png_uint_32 gray
= (i
* 256 + 115) / 231;
2529 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
,
2533 /* NOTE: this preserves the full precision of the application
2536 background_index
= i
;
2537 png_create_colormap_entry(display
, i
++, back_r
, back_g
, back_b
,
2539 /* Coverity claims that output_encoding
2540 * cannot be 2 (P_LINEAR) here.
2543 output_encoding
== P_LINEAR
? 65535U : 255U,
2547 /* For non-opaque input composite on the sRGB background - this
2548 * requires inverting the encoding for each component. The input
2549 * is still converted to the sRGB encoding because this is a
2550 * reasonable approximate to the logarithmic curve of human
2551 * visual sensitivity, at least over the narrow range which PNG
2552 * represents. Consequently 'G' is always sRGB encoded, while
2553 * 'A' is linear. We need the linear background colors.
2555 if (output_encoding
== P_sRGB
) /* else already linear */
2557 /* This may produce a value not exactly matching the
2558 * background, but that's ok because these numbers are only
2559 * used when alpha != 0
2561 back_r
= png_sRGB_table
[back_r
];
2562 back_g
= png_sRGB_table
[back_g
];
2563 back_b
= png_sRGB_table
[back_b
];
2570 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2571 * by an 8-bit alpha value (0..255).
2573 png_uint_32 alpha
= 51 * a
;
2574 png_uint_32 back_rx
= (255-alpha
) * back_r
;
2575 png_uint_32 back_gx
= (255-alpha
) * back_g
;
2576 png_uint_32 back_bx
= (255-alpha
) * back_b
;
2580 png_uint_32 gray
= png_sRGB_table
[g
*51] * alpha
;
2582 png_create_colormap_entry(display
, i
++,
2583 PNG_sRGB_FROM_LINEAR(gray
+ back_rx
),
2584 PNG_sRGB_FROM_LINEAR(gray
+ back_gx
),
2585 PNG_sRGB_FROM_LINEAR(gray
+ back_bx
), 255, P_sRGB
);
2590 output_processing
= PNG_CMAP_GA
;
2595 case PNG_COLOR_TYPE_RGB
:
2596 case PNG_COLOR_TYPE_RGB_ALPHA
:
2597 /* Exclude the case where the output is gray; we can always handle this
2598 * with the cases above.
2600 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0)
2602 /* The color-map will be grayscale, so we may as well convert the
2603 * input RGB values to a simple grayscale and use the grayscale
2606 * NOTE: calling this apparently damages the recognition of the
2607 * transparent color in background color handling; call
2608 * png_set_tRNS_to_alpha before png_set_background_fixed.
2610 png_set_rgb_to_gray_fixed(png_ptr
, PNG_ERROR_ACTION_NONE
, -1,
2612 data_encoding
= P_sRGB
;
2614 /* The output will now be one or two 8-bit gray or gray+alpha
2615 * channels. The more complex case arises when the input has alpha.
2617 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2618 png_ptr
->num_trans
> 0) &&
2619 (output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2621 /* Both input and output have an alpha channel, so no background
2622 * processing is required; just map the GA bytes to the right
2627 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2628 png_error(png_ptr
, "rgb[ga] color-map: too few entries");
2630 cmap_entries
= (unsigned int)make_ga_colormap(display
);
2631 background_index
= PNG_CMAP_GA_BACKGROUND
;
2632 output_processing
= PNG_CMAP_GA
;
2637 /* Either the input or the output has no alpha channel, so there
2638 * will be no non-opaque pixels in the color-map; it will just be
2641 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2642 png_error(png_ptr
, "rgb[gray] color-map: too few entries");
2644 /* Ideally this code would use libpng to do the gamma correction,
2645 * but if an input alpha channel is to be removed we will hit the
2646 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2647 * correction bug). Fix this by dropping the gamma correction in
2648 * this case and doing it in the palette; this will result in
2649 * duplicate palette entries, but that's better than the
2650 * alternative of double gamma correction.
2652 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2653 png_ptr
->num_trans
> 0) &&
2654 png_gamma_not_sRGB(png_ptr
->colorspace
.gamma
) != 0)
2656 cmap_entries
= (unsigned int)make_gray_file_colormap(display
);
2657 data_encoding
= P_FILE
;
2661 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2663 /* But if the input has alpha or transparency it must be removed
2665 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2666 png_ptr
->num_trans
> 0)
2669 png_uint_32 gray
= back_g
;
2671 /* We need to ensure that the application background exists in
2672 * the colormap and that completely transparent pixels map to
2673 * it. Achieve this simply by ensuring that the entry
2674 * selected for the background really is the background color.
2676 if (data_encoding
== P_FILE
) /* from the fixup above */
2678 /* The app supplied a gray which is in output_encoding, we
2679 * need to convert it to a value of the input (P_FILE)
2680 * encoding then set this palette entry to the required
2683 if (output_encoding
== P_sRGB
)
2684 gray
= png_sRGB_table
[gray
]; /* now P_LINEAR */
2686 gray
= PNG_DIV257(png_gamma_16bit_correct(gray
,
2687 png_ptr
->colorspace
.gamma
)); /* now P_FILE */
2689 /* And make sure the corresponding palette entry contains
2690 * exactly the required sRGB value.
2692 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2693 back_g
, 0/*unused*/, output_encoding
);
2696 else if (output_encoding
== P_LINEAR
)
2698 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2700 /* And make sure the corresponding palette entry matches.
2702 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2703 back_g
, 0/*unused*/, P_LINEAR
);
2706 /* The background passed to libpng, however, must be the
2707 * output (normally sRGB) value.
2709 c
.index
= 0; /*unused*/
2710 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2712 /* NOTE: the following is apparently a bug in libpng. Without
2713 * it the transparent color recognition in
2714 * png_set_background_fixed seems to go wrong.
2717 png_set_background_fixed(png_ptr
, &c
,
2718 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2719 0/*gamma: not used*/);
2722 output_processing
= PNG_CMAP_NONE
;
2726 else /* output is color */
2728 /* We could use png_quantize here so long as there is no transparent
2729 * color or alpha; png_quantize ignores alpha. Easier overall just
2730 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2731 * Consequently we always want libpng to produce sRGB data.
2733 data_encoding
= P_sRGB
;
2735 /* Is there any transparency or alpha? */
2736 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2737 png_ptr
->num_trans
> 0)
2739 /* Is there alpha in the output too? If so all four channels are
2740 * processed into a special RGB cube with alpha support.
2742 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2746 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2747 png_error(png_ptr
, "rgb+alpha color-map: too few entries");
2749 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2751 /* Add a transparent entry. */
2752 png_create_colormap_entry(display
, cmap_entries
, 255, 255,
2755 /* This is stored as the background index for the processing
2758 background_index
= cmap_entries
++;
2760 /* Add 27 r,g,b entries each with alpha 0.5. */
2761 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2765 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2769 /* This generates components with the values 0, 127 and
2772 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2773 png_create_colormap_entry(display
, cmap_entries
++,
2774 r
, g
, b
, 128, P_sRGB
);
2779 output_processing
= PNG_CMAP_RGB_ALPHA
;
2784 /* Alpha/transparency must be removed. The background must
2785 * exist in the color map (achieved by setting adding it after
2786 * the 666 color-map). If the standard processing code will
2787 * pick up this entry automatically that's all that is
2788 * required; libpng can be called to do the background
2791 unsigned int sample_size
=
2792 PNG_IMAGE_SAMPLE_SIZE(output_format
);
2793 png_uint_32 r
, g
, b
; /* sRGB background */
2795 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2796 png_error(png_ptr
, "rgb-alpha color-map: too few entries");
2798 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2800 png_create_colormap_entry(display
, cmap_entries
, back_r
,
2801 back_g
, back_b
, 0/*unused*/, output_encoding
);
2803 if (output_encoding
== P_LINEAR
)
2805 r
= PNG_sRGB_FROM_LINEAR(back_r
* 255);
2806 g
= PNG_sRGB_FROM_LINEAR(back_g
* 255);
2807 b
= PNG_sRGB_FROM_LINEAR(back_b
* 255);
2817 /* Compare the newly-created color-map entry with the one the
2818 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2819 * match, add the new one and set this as the background
2822 if (memcmp((png_const_bytep
)display
->colormap
+
2823 sample_size
* cmap_entries
,
2824 (png_const_bytep
)display
->colormap
+
2825 sample_size
* PNG_RGB_INDEX(r
,g
,b
),
2828 /* The background color must be added. */
2829 background_index
= cmap_entries
++;
2831 /* Add 27 r,g,b entries each with created by composing with
2832 * the background at alpha 0.5.
2834 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2836 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2838 /* This generates components with the values 0, 127
2841 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2842 png_create_colormap_entry(display
, cmap_entries
++,
2843 png_colormap_compose(display
, r
, P_sRGB
, 128,
2844 back_r
, output_encoding
),
2845 png_colormap_compose(display
, g
, P_sRGB
, 128,
2846 back_g
, output_encoding
),
2847 png_colormap_compose(display
, b
, P_sRGB
, 128,
2848 back_b
, output_encoding
),
2849 0/*unused*/, output_encoding
);
2854 output_processing
= PNG_CMAP_RGB_ALPHA
;
2857 else /* background color is in the standard color-map */
2861 c
.index
= 0; /*unused*/
2862 c
.red
= (png_uint_16
)back_r
;
2863 c
.gray
= c
.green
= (png_uint_16
)back_g
;
2864 c
.blue
= (png_uint_16
)back_b
;
2866 png_set_background_fixed(png_ptr
, &c
,
2867 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2868 0/*gamma: not used*/);
2870 output_processing
= PNG_CMAP_RGB
;
2875 else /* no alpha or transparency in the input */
2877 /* Alpha in the output is irrelevant, simply map the opaque input
2878 * pixels to the 6x6x6 color-map.
2880 if (PNG_RGB_COLORMAP_ENTRIES
> image
->colormap_entries
)
2881 png_error(png_ptr
, "rgb color-map: too few entries");
2883 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2884 output_processing
= PNG_CMAP_RGB
;
2889 case PNG_COLOR_TYPE_PALETTE
:
2890 /* It's already got a color-map. It may be necessary to eliminate the
2891 * tRNS entries though.
2894 unsigned int num_trans
= png_ptr
->num_trans
;
2895 png_const_bytep trans
= num_trans
> 0 ? png_ptr
->trans_alpha
: NULL
;
2896 png_const_colorp colormap
= png_ptr
->palette
;
2897 int do_background
= trans
!= NULL
&&
2898 (output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0;
2905 output_processing
= PNG_CMAP_NONE
;
2906 data_encoding
= P_FILE
; /* Don't change from color-map indices */
2907 cmap_entries
= (unsigned int)png_ptr
->num_palette
;
2908 if (cmap_entries
> 256)
2911 if (cmap_entries
> (unsigned int)image
->colormap_entries
)
2912 png_error(png_ptr
, "palette color-map: too few entries");
2914 for (i
=0; i
< cmap_entries
; ++i
)
2916 if (do_background
!= 0 && i
< num_trans
&& trans
[i
] < 255)
2919 png_create_colormap_entry(display
, i
, back_r
, back_g
,
2920 back_b
, 0, output_encoding
);
2924 /* Must compose the PNG file color in the color-map entry
2925 * on the sRGB color in 'back'.
2927 png_create_colormap_entry(display
, i
,
2928 png_colormap_compose(display
, colormap
[i
].red
,
2929 P_FILE
, trans
[i
], back_r
, output_encoding
),
2930 png_colormap_compose(display
, colormap
[i
].green
,
2931 P_FILE
, trans
[i
], back_g
, output_encoding
),
2932 png_colormap_compose(display
, colormap
[i
].blue
,
2933 P_FILE
, trans
[i
], back_b
, output_encoding
),
2934 output_encoding
== P_LINEAR
? trans
[i
] * 257U :
2941 png_create_colormap_entry(display
, i
, colormap
[i
].red
,
2942 colormap
[i
].green
, colormap
[i
].blue
,
2943 i
< num_trans
? trans
[i
] : 255U, P_FILE
/*8-bit*/);
2946 /* The PNG data may have indices packed in fewer than 8 bits, it
2947 * must be expanded if so.
2949 if (png_ptr
->bit_depth
< 8)
2950 png_set_packing(png_ptr
);
2955 png_error(png_ptr
, "invalid PNG color type");
2959 /* Now deal with the output processing */
2960 if (expand_tRNS
!= 0 && png_ptr
->num_trans
> 0 &&
2961 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) == 0)
2962 png_set_tRNS_to_alpha(png_ptr
);
2964 switch (data_encoding
)
2967 /* Change to 8-bit sRGB */
2968 png_set_alpha_mode_fixed(png_ptr
, PNG_ALPHA_PNG
, PNG_GAMMA_sRGB
);
2972 if (png_ptr
->bit_depth
> 8)
2973 png_set_scale_16(png_ptr
);
2978 png_error(png_ptr
, "bad data option (internal error)");
2982 if (cmap_entries
> 256 || cmap_entries
> image
->colormap_entries
)
2983 png_error(png_ptr
, "color map overflow (BAD internal error)");
2985 image
->colormap_entries
= cmap_entries
;
2987 /* Double check using the recorded background index */
2988 switch (output_processing
)
2991 if (background_index
!= PNG_CMAP_NONE_BACKGROUND
)
2992 goto bad_background
;
2996 if (background_index
!= PNG_CMAP_GA_BACKGROUND
)
2997 goto bad_background
;
3000 case PNG_CMAP_TRANS
:
3001 if (background_index
>= cmap_entries
||
3002 background_index
!= PNG_CMAP_TRANS_BACKGROUND
)
3003 goto bad_background
;
3007 if (background_index
!= PNG_CMAP_RGB_BACKGROUND
)
3008 goto bad_background
;
3011 case PNG_CMAP_RGB_ALPHA
:
3012 if (background_index
!= PNG_CMAP_RGB_ALPHA_BACKGROUND
)
3013 goto bad_background
;
3017 png_error(png_ptr
, "bad processing option (internal error)");
3020 png_error(png_ptr
, "bad background index (internal error)");
3023 display
->colormap_processing
= (int)output_processing
;
3028 /* The final part of the color-map read called from png_image_finish_read. */
3030 png_image_read_and_map(png_voidp argument
)
3032 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3034 png_imagep image
= display
->image
;
3035 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3038 /* Called when the libpng data must be transformed into the color-mapped
3039 * form. There is a local row buffer in display->local and this routine must
3040 * do the interlace handling.
3042 switch (png_ptr
->interlaced
)
3044 case PNG_INTERLACE_NONE
:
3048 case PNG_INTERLACE_ADAM7
:
3049 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3053 png_error(png_ptr
, "unknown interlace type");
3057 png_uint_32 height
= image
->height
;
3058 png_uint_32 width
= image
->width
;
3059 int proc
= display
->colormap_processing
;
3060 png_bytep first_row
= png_voidcast(png_bytep
, display
->first_row
);
3061 ptrdiff_t step_row
= display
->row_bytes
;
3064 for (pass
= 0; pass
< passes
; ++pass
)
3066 unsigned int startx
, stepx
, stepy
;
3069 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3071 /* The row may be empty for a short image: */
3072 if (PNG_PASS_COLS(width
, pass
) == 0)
3075 startx
= PNG_PASS_START_COL(pass
);
3076 stepx
= PNG_PASS_COL_OFFSET(pass
);
3077 y
= PNG_PASS_START_ROW(pass
);
3078 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3088 for (; y
<height
; y
+= stepy
)
3090 png_bytep inrow
= png_voidcast(png_bytep
, display
->local_row
);
3091 png_bytep outrow
= first_row
+ y
* step_row
;
3092 png_const_bytep end_row
= outrow
+ width
;
3094 /* Read read the libpng data into the temporary buffer. */
3095 png_read_row(png_ptr
, inrow
, NULL
);
3097 /* Now process the row according to the processing option, note
3098 * that the caller verifies that the format of the libpng output
3099 * data is as required.
3105 for (; outrow
< end_row
; outrow
+= stepx
)
3107 /* The data is always in the PNG order */
3108 unsigned int gray
= *inrow
++;
3109 unsigned int alpha
= *inrow
++;
3112 /* NOTE: this code is copied as a comment in
3113 * make_ga_colormap above. Please update the
3114 * comment if you change this code!
3116 if (alpha
> 229) /* opaque */
3118 entry
= (231 * gray
+ 128) >> 8;
3120 else if (alpha
< 26) /* transparent */
3124 else /* partially opaque */
3126 entry
= 226 + 6 * PNG_DIV51(alpha
) + PNG_DIV51(gray
);
3129 *outrow
= (png_byte
)entry
;
3133 case PNG_CMAP_TRANS
:
3134 for (; outrow
< end_row
; outrow
+= stepx
)
3136 png_byte gray
= *inrow
++;
3137 png_byte alpha
= *inrow
++;
3140 *outrow
= PNG_CMAP_TRANS_BACKGROUND
;
3142 else if (gray
!= PNG_CMAP_TRANS_BACKGROUND
)
3146 *outrow
= (png_byte
)(PNG_CMAP_TRANS_BACKGROUND
+1);
3151 for (; outrow
< end_row
; outrow
+= stepx
)
3153 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1], inrow
[2]);
3158 case PNG_CMAP_RGB_ALPHA
:
3159 for (; outrow
< end_row
; outrow
+= stepx
)
3161 unsigned int alpha
= inrow
[3];
3163 /* Because the alpha entries only hold alpha==0.5 values
3164 * split the processing at alpha==0.25 (64) and 0.75
3169 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1],
3172 else if (alpha
< 64)
3173 *outrow
= PNG_CMAP_RGB_ALPHA_BACKGROUND
;
3177 /* Likewise there are three entries for each of r, g
3178 * and b. We could select the entry by popcount on
3179 * the top two bits on those architectures that
3180 * support it, this is what the code below does,
3183 unsigned int back_i
= PNG_CMAP_RGB_ALPHA_BACKGROUND
+1;
3185 /* Here are how the values map:
3191 * So, as above with the explicit alpha checks, the
3192 * breakpoints are at 64 and 196.
3194 if (inrow
[0] & 0x80) back_i
+= 9; /* red */
3195 if (inrow
[0] & 0x40) back_i
+= 9;
3196 if (inrow
[0] & 0x80) back_i
+= 3; /* green */
3197 if (inrow
[0] & 0x40) back_i
+= 3;
3198 if (inrow
[0] & 0x80) back_i
+= 1; /* blue */
3199 if (inrow
[0] & 0x40) back_i
+= 1;
3201 *outrow
= (png_byte
)back_i
;
3219 png_image_read_colormapped(png_voidp argument
)
3221 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3223 png_imagep image
= display
->image
;
3224 png_controlp control
= image
->opaque
;
3225 png_structrp png_ptr
= control
->png_ptr
;
3226 png_inforp info_ptr
= control
->info_ptr
;
3228 int passes
= 0; /* As a flag */
3230 PNG_SKIP_CHUNKS(png_ptr
);
3232 /* Update the 'info' structure and make sure the result is as required; first
3233 * make sure to turn on the interlace handling if it will be required
3234 * (because it can't be turned on *after* the call to png_read_update_info!)
3236 if (display
->colormap_processing
== PNG_CMAP_NONE
)
3237 passes
= png_set_interlace_handling(png_ptr
);
3239 png_read_update_info(png_ptr
, info_ptr
);
3241 /* The expected output can be deduced from the colormap_processing option. */
3242 switch (display
->colormap_processing
)
3245 /* Output must be one channel and one byte per pixel, the output
3246 * encoding can be anything.
3248 if ((info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
||
3249 info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
) &&
3250 info_ptr
->bit_depth
== 8)
3255 case PNG_CMAP_TRANS
:
3257 /* Output must be two channels and the 'G' one must be sRGB, the latter
3258 * can be checked with an exact number because it should have been set
3259 * to this number above!
3261 if (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
&&
3262 info_ptr
->bit_depth
== 8 &&
3263 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3264 image
->colormap_entries
== 256)
3270 /* Output must be 8-bit sRGB encoded RGB */
3271 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
&&
3272 info_ptr
->bit_depth
== 8 &&
3273 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3274 image
->colormap_entries
== 216)
3279 case PNG_CMAP_RGB_ALPHA
:
3280 /* Output must be 8-bit sRGB encoded RGBA */
3281 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
3282 info_ptr
->bit_depth
== 8 &&
3283 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3284 image
->colormap_entries
== 244 /* 216 + 1 + 27 */)
3291 png_error(png_ptr
, "bad color-map processing (internal error)");
3294 /* Now read the rows. Do this here if it is possible to read directly into
3295 * the output buffer, otherwise allocate a local row buffer of the maximum
3296 * size libpng requires and call the relevant processing routine safely.
3299 png_voidp first_row
= display
->buffer
;
3300 ptrdiff_t row_bytes
= display
->row_stride
;
3302 /* The following expression is designed to work correctly whether it gives
3303 * a signed or an unsigned result.
3307 char *ptr
= png_voidcast(char*, first_row
);
3308 ptr
+= (image
->height
-1) * (-row_bytes
);
3309 first_row
= png_voidcast(png_voidp
, ptr
);
3312 display
->first_row
= first_row
;
3313 display
->row_bytes
= row_bytes
;
3319 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
3321 display
->local_row
= row
;
3322 result
= png_safe_execute(image
, png_image_read_and_map
, display
);
3323 display
->local_row
= NULL
;
3324 png_free(png_ptr
, row
);
3331 png_alloc_size_t row_bytes
= (png_alloc_size_t
)display
->row_bytes
;
3333 while (--passes
>= 0)
3335 png_uint_32 y
= image
->height
;
3336 png_bytep row
= png_voidcast(png_bytep
, display
->first_row
);
3340 png_read_row(png_ptr
, row
, NULL
);
3349 /* Just the row reading part of png_image_read. */
3351 png_image_read_composite(png_voidp argument
)
3353 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3355 png_imagep image
= display
->image
;
3356 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3359 switch (png_ptr
->interlaced
)
3361 case PNG_INTERLACE_NONE
:
3365 case PNG_INTERLACE_ADAM7
:
3366 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3370 png_error(png_ptr
, "unknown interlace type");
3374 png_uint_32 height
= image
->height
;
3375 png_uint_32 width
= image
->width
;
3376 ptrdiff_t step_row
= display
->row_bytes
;
3377 unsigned int channels
=
3378 (image
->format
& PNG_FORMAT_FLAG_COLOR
) != 0 ? 3 : 1;
3381 for (pass
= 0; pass
< passes
; ++pass
)
3383 unsigned int startx
, stepx
, stepy
;
3386 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3388 /* The row may be empty for a short image: */
3389 if (PNG_PASS_COLS(width
, pass
) == 0)
3392 startx
= PNG_PASS_START_COL(pass
) * channels
;
3393 stepx
= PNG_PASS_COL_OFFSET(pass
) * channels
;
3394 y
= PNG_PASS_START_ROW(pass
);
3395 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3406 for (; y
<height
; y
+= stepy
)
3408 png_bytep inrow
= png_voidcast(png_bytep
, display
->local_row
);
3410 png_const_bytep end_row
;
3412 /* Read the row, which is packed: */
3413 png_read_row(png_ptr
, inrow
, NULL
);
3415 outrow
= png_voidcast(png_bytep
, display
->first_row
);
3416 outrow
+= y
* step_row
;
3417 end_row
= outrow
+ width
* channels
;
3419 /* Now do the composition on each pixel in this row. */
3421 for (; outrow
< end_row
; outrow
+= stepx
)
3423 png_byte alpha
= inrow
[channels
];
3425 if (alpha
> 0) /* else no change to the output */
3429 for (c
=0; c
<channels
; ++c
)
3431 png_uint_32 component
= inrow
[c
];
3433 if (alpha
< 255) /* else just use component */
3435 /* This is PNG_OPTIMIZED_ALPHA, the component value
3436 * is a linear 8-bit value. Combine this with the
3437 * current outrow[c] value which is sRGB encoded.
3438 * Arithmetic here is 16-bits to preserve the output
3441 component
*= 257*255; /* =65535 */
3442 component
+= (255-alpha
)*png_sRGB_table
[outrow
[c
]];
3444 /* So 'component' is scaled by 255*65535 and is
3445 * therefore appropriate for the sRGB to linear
3448 component
= PNG_sRGB_FROM_LINEAR(component
);
3451 outrow
[c
] = (png_byte
)component
;
3455 inrow
+= channels
+1; /* components and alpha channel */
3464 /* The do_local_background case; called when all the following transforms are to
3471 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3472 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3473 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3474 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3475 * row and handles the removal or pre-multiplication of the alpha channel.
3478 png_image_read_background(png_voidp argument
)
3480 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3482 png_imagep image
= display
->image
;
3483 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3484 png_inforp info_ptr
= image
->opaque
->info_ptr
;
3485 png_uint_32 height
= image
->height
;
3486 png_uint_32 width
= image
->width
;
3489 /* Double check the convoluted logic below. We expect to get here with
3490 * libpng doing rgb to gray and gamma correction but background processing
3491 * left to the png_image_read_background function. The rows libpng produce
3492 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3494 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) == 0)
3495 png_error(png_ptr
, "lost rgb to gray");
3497 if ((png_ptr
->transformations
& PNG_COMPOSE
) != 0)
3498 png_error(png_ptr
, "unexpected compose");
3500 if (png_get_channels(png_ptr
, info_ptr
) != 2)
3501 png_error(png_ptr
, "lost/gained channels");
3503 /* Expect the 8-bit case to always remove the alpha channel */
3504 if ((image
->format
& PNG_FORMAT_FLAG_LINEAR
) == 0 &&
3505 (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3506 png_error(png_ptr
, "unexpected 8-bit transformation");
3508 switch (png_ptr
->interlaced
)
3510 case PNG_INTERLACE_NONE
:
3514 case PNG_INTERLACE_ADAM7
:
3515 passes
= PNG_INTERLACE_ADAM7_PASSES
;
3519 png_error(png_ptr
, "unknown interlace type");
3522 /* Use direct access to info_ptr here because otherwise the simplified API
3523 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3524 * checking the value after libpng expansions, not the original value in the
3527 switch (info_ptr
->bit_depth
)
3530 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3531 * to be removed by composing on a background: either the row if
3532 * display->background is NULL or display->background->green if not.
3533 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3536 png_bytep first_row
= png_voidcast(png_bytep
, display
->first_row
);
3537 ptrdiff_t step_row
= display
->row_bytes
;
3539 for (pass
= 0; pass
< passes
; ++pass
)
3541 unsigned int startx
, stepx
, stepy
;
3544 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3546 /* The row may be empty for a short image: */
3547 if (PNG_PASS_COLS(width
, pass
) == 0)
3550 startx
= PNG_PASS_START_COL(pass
);
3551 stepx
= PNG_PASS_COL_OFFSET(pass
);
3552 y
= PNG_PASS_START_ROW(pass
);
3553 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3563 if (display
->background
== NULL
)
3565 for (; y
<height
; y
+= stepy
)
3567 png_bytep inrow
= png_voidcast(png_bytep
,
3568 display
->local_row
);
3569 png_bytep outrow
= first_row
+ y
* step_row
;
3570 png_const_bytep end_row
= outrow
+ width
;
3572 /* Read the row, which is packed: */
3573 png_read_row(png_ptr
, inrow
, NULL
);
3575 /* Now do the composition on each pixel in this row. */
3577 for (; outrow
< end_row
; outrow
+= stepx
)
3579 png_byte alpha
= inrow
[1];
3581 if (alpha
> 0) /* else no change to the output */
3583 png_uint_32 component
= inrow
[0];
3585 if (alpha
< 255) /* else just use component */
3587 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3588 * necessary to invert the sRGB transfer
3589 * function and multiply the alpha out.
3591 component
= png_sRGB_table
[component
] * alpha
;
3592 component
+= png_sRGB_table
[outrow
[0]] *
3594 component
= PNG_sRGB_FROM_LINEAR(component
);
3597 outrow
[0] = (png_byte
)component
;
3600 inrow
+= 2; /* gray and alpha channel */
3605 else /* constant background value */
3607 png_byte background8
= display
->background
->green
;
3608 png_uint_16 background
= png_sRGB_table
[background8
];
3610 for (; y
<height
; y
+= stepy
)
3612 png_bytep inrow
= png_voidcast(png_bytep
,
3613 display
->local_row
);
3614 png_bytep outrow
= first_row
+ y
* step_row
;
3615 png_const_bytep end_row
= outrow
+ width
;
3617 /* Read the row, which is packed: */
3618 png_read_row(png_ptr
, inrow
, NULL
);
3620 /* Now do the composition on each pixel in this row. */
3622 for (; outrow
< end_row
; outrow
+= stepx
)
3624 png_byte alpha
= inrow
[1];
3626 if (alpha
> 0) /* else use background */
3628 png_uint_32 component
= inrow
[0];
3630 if (alpha
< 255) /* else just use component */
3632 component
= png_sRGB_table
[component
] * alpha
;
3633 component
+= background
* (255-alpha
);
3634 component
= PNG_sRGB_FROM_LINEAR(component
);
3637 outrow
[0] = (png_byte
)component
;
3641 outrow
[0] = background8
;
3643 inrow
+= 2; /* gray and alpha channel */
3652 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3653 * still be done and, maybe, the alpha channel removed. This code also
3654 * handles the alpha-first option.
3657 png_uint_16p first_row
= png_voidcast(png_uint_16p
,
3658 display
->first_row
);
3659 /* The division by two is safe because the caller passed in a
3660 * stride which was multiplied by 2 (below) to get row_bytes.
3662 ptrdiff_t step_row
= display
->row_bytes
/ 2;
3663 unsigned int preserve_alpha
= (image
->format
&
3664 PNG_FORMAT_FLAG_ALPHA
) != 0;
3665 unsigned int outchannels
= 1U+preserve_alpha
;
3668 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3669 if (preserve_alpha
!= 0 &&
3670 (image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
3674 for (pass
= 0; pass
< passes
; ++pass
)
3676 unsigned int startx
, stepx
, stepy
;
3679 /* The 'x' start and step are adjusted to output components here.
3681 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
3683 /* The row may be empty for a short image: */
3684 if (PNG_PASS_COLS(width
, pass
) == 0)
3687 startx
= PNG_PASS_START_COL(pass
) * outchannels
;
3688 stepx
= PNG_PASS_COL_OFFSET(pass
) * outchannels
;
3689 y
= PNG_PASS_START_ROW(pass
);
3690 stepy
= PNG_PASS_ROW_OFFSET(pass
);
3697 stepx
= outchannels
;
3701 for (; y
<height
; y
+= stepy
)
3703 png_const_uint_16p inrow
;
3704 png_uint_16p outrow
= first_row
+ y
*step_row
;
3705 png_uint_16p end_row
= outrow
+ width
* outchannels
;
3707 /* Read the row, which is packed: */
3708 png_read_row(png_ptr
, png_voidcast(png_bytep
,
3709 display
->local_row
), NULL
);
3710 inrow
= png_voidcast(png_const_uint_16p
, display
->local_row
);
3712 /* Now do the pre-multiplication on each pixel in this row.
3715 for (; outrow
< end_row
; outrow
+= stepx
)
3717 png_uint_32 component
= inrow
[0];
3718 png_uint_16 alpha
= inrow
[1];
3720 if (alpha
> 0) /* else 0 */
3722 if (alpha
< 65535) /* else just use component */
3733 outrow
[swap_alpha
] = (png_uint_16
)component
;
3734 if (preserve_alpha
!= 0)
3735 outrow
[1 ^ swap_alpha
] = alpha
;
3737 inrow
+= 2; /* components and alpha channel */
3746 png_error(png_ptr
, "unexpected bit depth");
3753 /* The guts of png_image_finish_read as a png_safe_execute callback. */
3755 png_image_read_direct(png_voidp argument
)
3757 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3759 png_imagep image
= display
->image
;
3760 png_structrp png_ptr
= image
->opaque
->png_ptr
;
3761 png_inforp info_ptr
= image
->opaque
->info_ptr
;
3763 png_uint_32 format
= image
->format
;
3764 int linear
= (format
& PNG_FORMAT_FLAG_LINEAR
) != 0;
3765 int do_local_compose
= 0;
3766 int do_local_background
= 0; /* to avoid double gamma correction bug */
3769 /* Add transforms to ensure the correct output format is produced then check
3770 * that the required implementation support is there. Always expand; always
3771 * need 8 bits minimum, no palette and expanded tRNS.
3773 png_set_expand(png_ptr
);
3775 /* Now check the format to see if it was modified. */
3777 png_uint_32 base_format
= png_image_format(png_ptr
) &
3778 ~PNG_FORMAT_FLAG_COLORMAP
/* removed by png_set_expand */;
3779 png_uint_32 change
= format
^ base_format
;
3780 png_fixed_point output_gamma
;
3781 int mode
; /* alpha mode */
3783 /* Do this first so that we have a record if rgb to gray is happening. */
3784 if ((change
& PNG_FORMAT_FLAG_COLOR
) != 0)
3786 /* gray<->color transformation required. */
3787 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0)
3788 png_set_gray_to_rgb(png_ptr
);
3792 /* libpng can't do both rgb to gray and
3793 * background/pre-multiplication if there is also significant gamma
3794 * correction, because both operations require linear colors and
3795 * the code only supports one transform doing the gamma correction.
3796 * Handle this by doing the pre-multiplication or background
3797 * operation in this code, if necessary.
3799 * TODO: fix this by rewriting pngrtran.c (!)
3801 * For the moment (given that fixing this in pngrtran.c is an
3802 * enormous change) 'do_local_background' is used to indicate that
3803 * the problem exists.
3805 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3806 do_local_background
= 1/*maybe*/;
3808 png_set_rgb_to_gray_fixed(png_ptr
, PNG_ERROR_ACTION_NONE
,
3809 PNG_RGB_TO_GRAY_DEFAULT
, PNG_RGB_TO_GRAY_DEFAULT
);
3812 change
&= ~PNG_FORMAT_FLAG_COLOR
;
3815 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3818 png_fixed_point input_gamma_default
;
3820 if ((base_format
& PNG_FORMAT_FLAG_LINEAR
) != 0 &&
3821 (image
->flags
& PNG_IMAGE_FLAG_16BIT_sRGB
) == 0)
3822 input_gamma_default
= PNG_GAMMA_LINEAR
;
3824 input_gamma_default
= PNG_DEFAULT_sRGB
;
3826 /* Call png_set_alpha_mode to set the default for the input gamma; the
3827 * output gamma is set by a second call below.
3829 png_set_alpha_mode_fixed(png_ptr
, PNG_ALPHA_PNG
, input_gamma_default
);
3834 /* If there *is* an alpha channel in the input it must be multiplied
3835 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3837 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3838 mode
= PNG_ALPHA_STANDARD
; /* associated alpha */
3841 mode
= PNG_ALPHA_PNG
;
3843 output_gamma
= PNG_GAMMA_LINEAR
;
3848 mode
= PNG_ALPHA_PNG
;
3849 output_gamma
= PNG_DEFAULT_sRGB
;
3852 if ((change
& PNG_FORMAT_FLAG_ASSOCIATED_ALPHA
) != 0)
3854 mode
= PNG_ALPHA_OPTIMIZED
;
3855 change
&= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA
;
3858 /* If 'do_local_background' is set check for the presence of gamma
3859 * correction; this is part of the work-round for the libpng bug
3862 * TODO: fix libpng and remove this.
3864 if (do_local_background
!= 0)
3866 png_fixed_point gtest
;
3868 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3869 * gamma correction, the screen gamma hasn't been set on png_struct
3870 * yet; it's set below. png_struct::gamma, however, is set to the
3873 if (png_muldiv(>est
, output_gamma
, png_ptr
->colorspace
.gamma
,
3874 PNG_FP_1
) != 0 && png_gamma_significant(gtest
) == 0)
3875 do_local_background
= 0;
3877 else if (mode
== PNG_ALPHA_STANDARD
)
3879 do_local_background
= 2/*required*/;
3880 mode
= PNG_ALPHA_PNG
; /* prevent libpng doing it */
3883 /* else leave as 1 for the checks below */
3886 /* If the bit-depth changes then handle that here. */
3887 if ((change
& PNG_FORMAT_FLAG_LINEAR
) != 0)
3889 if (linear
!= 0 /*16-bit output*/)
3890 png_set_expand_16(png_ptr
);
3892 else /* 8-bit output */
3893 png_set_scale_16(png_ptr
);
3895 change
&= ~PNG_FORMAT_FLAG_LINEAR
;
3898 /* Now the background/alpha channel changes. */
3899 if ((change
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3901 /* Removing an alpha channel requires composition for the 8-bit
3902 * formats; for the 16-bit it is already done, above, by the
3903 * pre-multiplication and the channel just needs to be stripped.
3905 if ((base_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3907 /* If RGB->gray is happening the alpha channel must be left and the
3908 * operation completed locally.
3910 * TODO: fix libpng and remove this.
3912 if (do_local_background
!= 0)
3913 do_local_background
= 2/*required*/;
3915 /* 16-bit output: just remove the channel */
3916 else if (linear
!= 0) /* compose on black (well, pre-multiply) */
3917 png_set_strip_alpha(png_ptr
);
3919 /* 8-bit output: do an appropriate compose */
3920 else if (display
->background
!= NULL
)
3924 c
.index
= 0; /*unused*/
3925 c
.red
= display
->background
->red
;
3926 c
.green
= display
->background
->green
;
3927 c
.blue
= display
->background
->blue
;
3928 c
.gray
= display
->background
->green
;
3930 /* This is always an 8-bit sRGB value, using the 'green' channel
3931 * for gray is much better than calculating the luminance here;
3932 * we can get off-by-one errors in that calculation relative to
3933 * the app expectations and that will show up in transparent
3936 png_set_background_fixed(png_ptr
, &c
,
3937 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
3938 0/*gamma: not used*/);
3941 else /* compose on row: implemented below. */
3943 do_local_compose
= 1;
3944 /* This leaves the alpha channel in the output, so it has to be
3945 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3946 * one so the code only has to hack on the pixels that require
3949 mode
= PNG_ALPHA_OPTIMIZED
;
3953 else /* output needs an alpha channel */
3955 /* This is tricky because it happens before the swap operation has
3956 * been accomplished; however, the swap does *not* swap the added
3957 * alpha channel (weird API), so it must be added in the correct
3960 png_uint_32 filler
; /* opaque filler */
3969 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
3970 if ((format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
3972 where
= PNG_FILLER_BEFORE
;
3973 change
&= ~PNG_FORMAT_FLAG_AFIRST
;
3978 where
= PNG_FILLER_AFTER
;
3980 png_set_add_alpha(png_ptr
, filler
, where
);
3983 /* This stops the (irrelevant) call to swap_alpha below. */
3984 change
&= ~PNG_FORMAT_FLAG_ALPHA
;
3987 /* Now set the alpha mode correctly; this is always done, even if there is
3988 * no alpha channel in either the input or the output because it correctly
3989 * sets the output gamma.
3991 png_set_alpha_mode_fixed(png_ptr
, mode
, output_gamma
);
3993 # ifdef PNG_FORMAT_BGR_SUPPORTED
3994 if ((change
& PNG_FORMAT_FLAG_BGR
) != 0)
3996 /* Check only the output format; PNG is never BGR; don't do this if
3997 * the output is gray, but fix up the 'format' value in that case.
3999 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0)
4000 png_set_bgr(png_ptr
);
4003 format
&= ~PNG_FORMAT_FLAG_BGR
;
4005 change
&= ~PNG_FORMAT_FLAG_BGR
;
4009 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
4010 if ((change
& PNG_FORMAT_FLAG_AFIRST
) != 0)
4012 /* Only relevant if there is an alpha channel - it's particularly
4013 * important to handle this correctly because do_local_compose may
4014 * be set above and then libpng will keep the alpha channel for this
4017 if ((format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
4019 /* Disable this if doing a local background,
4020 * TODO: remove this when local background is no longer required.
4022 if (do_local_background
!= 2)
4023 png_set_swap_alpha(png_ptr
);
4027 format
&= ~PNG_FORMAT_FLAG_AFIRST
;
4029 change
&= ~PNG_FORMAT_FLAG_AFIRST
;
4033 /* If the *output* is 16-bit then we need to check for a byte-swap on this
4038 png_uint_16 le
= 0x0001;
4040 if ((*(png_const_bytep
) & le
) != 0)
4041 png_set_swap(png_ptr
);
4044 /* If change is not now 0 some transformation is missing - error out. */
4046 png_error(png_ptr
, "png_read_image: unsupported transformation");
4049 PNG_SKIP_CHUNKS(png_ptr
);
4051 /* Update the 'info' structure and make sure the result is as required; first
4052 * make sure to turn on the interlace handling if it will be required
4053 * (because it can't be turned on *after* the call to png_read_update_info!)
4055 * TODO: remove the do_local_background fixup below.
4057 if (do_local_compose
== 0 && do_local_background
!= 2)
4058 passes
= png_set_interlace_handling(png_ptr
);
4060 png_read_update_info(png_ptr
, info_ptr
);
4063 png_uint_32 info_format
= 0;
4065 if ((info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
4066 info_format
|= PNG_FORMAT_FLAG_COLOR
;
4068 if ((info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
4070 /* do_local_compose removes this channel below. */
4071 if (do_local_compose
== 0)
4073 /* do_local_background does the same if required. */
4074 if (do_local_background
!= 2 ||
4075 (format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
4076 info_format
|= PNG_FORMAT_FLAG_ALPHA
;
4080 else if (do_local_compose
!= 0) /* internal error */
4081 png_error(png_ptr
, "png_image_read: alpha channel lost");
4083 if ((format
& PNG_FORMAT_FLAG_ASSOCIATED_ALPHA
) != 0) {
4084 info_format
|= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA
;
4087 if (info_ptr
->bit_depth
== 16)
4088 info_format
|= PNG_FORMAT_FLAG_LINEAR
;
4090 #ifdef PNG_FORMAT_BGR_SUPPORTED
4091 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
4092 info_format
|= PNG_FORMAT_FLAG_BGR
;
4095 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
4096 if (do_local_background
== 2)
4098 if ((format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
4099 info_format
|= PNG_FORMAT_FLAG_AFIRST
;
4102 if ((png_ptr
->transformations
& PNG_SWAP_ALPHA
) != 0 ||
4103 ((png_ptr
->transformations
& PNG_ADD_ALPHA
) != 0 &&
4104 (png_ptr
->flags
& PNG_FLAG_FILLER_AFTER
) == 0))
4106 if (do_local_background
== 2)
4107 png_error(png_ptr
, "unexpected alpha swap transformation");
4109 info_format
|= PNG_FORMAT_FLAG_AFIRST
;
4113 /* This is actually an internal error. */
4114 if (info_format
!= format
)
4115 png_error(png_ptr
, "png_read_image: invalid transformations");
4118 /* Now read the rows. If do_local_compose is set then it is necessary to use
4119 * a local row buffer. The output will be GA, RGBA or BGRA and must be
4120 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
4121 * display acts as a flag.
4124 png_voidp first_row
= display
->buffer
;
4125 ptrdiff_t row_bytes
= display
->row_stride
;
4130 /* The following expression is designed to work correctly whether it gives
4131 * a signed or an unsigned result.
4135 char *ptr
= png_voidcast(char*, first_row
);
4136 ptr
+= (image
->height
-1) * (-row_bytes
);
4137 first_row
= png_voidcast(png_voidp
, ptr
);
4140 display
->first_row
= first_row
;
4141 display
->row_bytes
= row_bytes
;
4144 if (do_local_compose
!= 0)
4147 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
4149 display
->local_row
= row
;
4150 result
= png_safe_execute(image
, png_image_read_composite
, display
);
4151 display
->local_row
= NULL
;
4152 png_free(png_ptr
, row
);
4157 else if (do_local_background
== 2)
4160 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
4162 display
->local_row
= row
;
4163 result
= png_safe_execute(image
, png_image_read_background
, display
);
4164 display
->local_row
= NULL
;
4165 png_free(png_ptr
, row
);
4172 png_alloc_size_t row_bytes
= (png_alloc_size_t
)display
->row_bytes
;
4174 while (--passes
>= 0)
4176 png_uint_32 y
= image
->height
;
4177 png_bytep row
= png_voidcast(png_bytep
, display
->first_row
);
4181 png_read_row(png_ptr
, row
, NULL
);
4191 png_image_finish_read(png_imagep image
, png_const_colorp background
,
4192 void *buffer
, png_int_32 row_stride
, void *colormap
)
4194 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
4196 /* Check for row_stride overflow. This check is not performed on the
4197 * original PNG format because it may not occur in the output PNG format
4198 * and libpng deals with the issues of reading the original.
4200 unsigned int channels
= PNG_IMAGE_PIXEL_CHANNELS(image
->format
);
4202 /* The following checks just the 'row_stride' calculation to ensure it
4203 * fits in a signed 32-bit value. Because channels/components can be
4204 * either 1 or 2 bytes in size the length of a row can still overflow 32
4205 * bits; this is just to verify that the 'row_stride' argument can be
4208 if (image
->width
<= 0x7fffffffU
/channels
) /* no overflow */
4211 png_uint_32 png_row_stride
= image
->width
* channels
;
4213 if (row_stride
== 0)
4214 row_stride
= (png_int_32
)/*SAFE*/png_row_stride
;
4217 check
= (png_uint_32
)(-row_stride
);
4220 check
= (png_uint_32
)row_stride
;
4222 /* This verifies 'check', the absolute value of the actual stride
4223 * passed in and detects overflow in the application calculation (i.e.
4224 * if the app did actually pass in a non-zero 'row_stride'.
4226 if (image
->opaque
!= NULL
&& buffer
!= NULL
&& check
>= png_row_stride
)
4228 /* Now check for overflow of the image buffer calculation; this
4229 * limits the whole image size to 32 bits for API compatibility with
4230 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4232 * The PNG_IMAGE_BUFFER_SIZE macro is:
4234 * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
4236 * And the component size is always 1 or 2, so make sure that the
4237 * number of *bytes* that the application is saying are available
4238 * does actually fit into a 32-bit number.
4240 * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
4241 * will be changed to use png_alloc_size_t; bigger images can be
4242 * accommodated on 64-bit systems.
4244 if (image
->height
<=
4245 0xffffffffU
/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image
->format
)/check
)
4247 if ((image
->format
& PNG_FORMAT_FLAG_COLORMAP
) == 0 ||
4248 (image
->colormap_entries
> 0 && colormap
!= NULL
))
4251 png_image_read_control display
;
4253 memset(&display
, 0, (sizeof display
));
4254 display
.image
= image
;
4255 display
.buffer
= buffer
;
4256 display
.row_stride
= row_stride
;
4257 display
.colormap
= colormap
;
4258 display
.background
= background
;
4259 display
.local_row
= NULL
;
4261 /* Choose the correct 'end' routine; for the color-map case
4262 * all the setup has already been done.
4264 if ((image
->format
& PNG_FORMAT_FLAG_COLORMAP
) != 0)
4266 png_safe_execute(image
,
4267 png_image_read_colormap
, &display
) &&
4268 png_safe_execute(image
,
4269 png_image_read_colormapped
, &display
);
4273 png_safe_execute(image
,
4274 png_image_read_direct
, &display
);
4276 png_image_free(image
);
4281 return png_image_error(image
,
4282 "png_image_finish_read[color-map]: no color-map");
4286 return png_image_error(image
,
4287 "png_image_finish_read: image too large");
4291 return png_image_error(image
,
4292 "png_image_finish_read: invalid argument");
4296 return png_image_error(image
,
4297 "png_image_finish_read: row_stride too large");
4300 else if (image
!= NULL
)
4301 return png_image_error(image
,
4302 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4307 #endif /* SIMPLIFIED_READ */