2 /* pngread.c - read a PNG file
4 * Last changed in libpng 1.6.26 [October 20, 2016]
5 * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
18 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
22 #ifdef PNG_READ_SUPPORTED
24 /* Create a PNG structure for reading, and allocate any memory needed. */
25 PNG_FUNCTION(png_structp
,PNGAPI
26 png_create_read_struct
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
27 png_error_ptr error_fn
, png_error_ptr warn_fn
),PNG_ALLOCATED
)
29 #ifndef PNG_USER_MEM_SUPPORTED
30 png_structp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
31 error_fn
, warn_fn
, NULL
, NULL
, NULL
);
33 return png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
34 warn_fn
, NULL
, NULL
, NULL
);
37 /* Alternate create PNG structure for reading, and allocate any memory
40 PNG_FUNCTION(png_structp
,PNGAPI
41 png_create_read_struct_2
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
42 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
43 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
),PNG_ALLOCATED
)
45 png_structp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
46 error_fn
, warn_fn
, mem_ptr
, malloc_fn
, free_fn
);
51 png_ptr
->mode
= PNG_IS_READ_STRUCT
;
53 /* Added in libpng-1.6.0; this can be used to detect a read structure if
54 * required (it will be zero in a write structure.)
56 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr
->IDAT_read_size
= PNG_IDAT_READ_SIZE
;
60 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr
->flags
|= PNG_FLAG_BENIGN_ERRORS_WARN
;
63 /* In stable builds only warn if an application error can be completely
66 # if PNG_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 png_ptr
->idat_size
= length
;
168 #ifdef PNG_READ_bKGD_SUPPORTED
169 else if (chunk_name
== png_bKGD
)
170 png_handle_bKGD(png_ptr
, info_ptr
, length
);
173 #ifdef PNG_READ_cHRM_SUPPORTED
174 else if (chunk_name
== png_cHRM
)
175 png_handle_cHRM(png_ptr
, info_ptr
, length
);
178 #ifdef PNG_READ_gAMA_SUPPORTED
179 else if (chunk_name
== png_gAMA
)
180 png_handle_gAMA(png_ptr
, info_ptr
, length
);
183 #ifdef PNG_READ_hIST_SUPPORTED
184 else if (chunk_name
== png_hIST
)
185 png_handle_hIST(png_ptr
, info_ptr
, length
);
188 #ifdef PNG_READ_oFFs_SUPPORTED
189 else if (chunk_name
== png_oFFs
)
190 png_handle_oFFs(png_ptr
, info_ptr
, length
);
193 #ifdef PNG_READ_pCAL_SUPPORTED
194 else if (chunk_name
== png_pCAL
)
195 png_handle_pCAL(png_ptr
, info_ptr
, length
);
198 #ifdef PNG_READ_sCAL_SUPPORTED
199 else if (chunk_name
== png_sCAL
)
200 png_handle_sCAL(png_ptr
, info_ptr
, length
);
203 #ifdef PNG_READ_pHYs_SUPPORTED
204 else if (chunk_name
== png_pHYs
)
205 png_handle_pHYs(png_ptr
, info_ptr
, length
);
208 #ifdef PNG_READ_sBIT_SUPPORTED
209 else if (chunk_name
== png_sBIT
)
210 png_handle_sBIT(png_ptr
, info_ptr
, length
);
213 #ifdef PNG_READ_sRGB_SUPPORTED
214 else if (chunk_name
== png_sRGB
)
215 png_handle_sRGB(png_ptr
, info_ptr
, length
);
218 #ifdef PNG_READ_iCCP_SUPPORTED
219 else if (chunk_name
== png_iCCP
)
220 png_handle_iCCP(png_ptr
, info_ptr
, length
);
223 #ifdef PNG_READ_sPLT_SUPPORTED
224 else if (chunk_name
== png_sPLT
)
225 png_handle_sPLT(png_ptr
, info_ptr
, length
);
228 #ifdef PNG_READ_tEXt_SUPPORTED
229 else if (chunk_name
== png_tEXt
)
230 png_handle_tEXt(png_ptr
, info_ptr
, length
);
233 #ifdef PNG_READ_tIME_SUPPORTED
234 else if (chunk_name
== png_tIME
)
235 png_handle_tIME(png_ptr
, info_ptr
, length
);
238 #ifdef PNG_READ_tRNS_SUPPORTED
239 else if (chunk_name
== png_tRNS
)
240 png_handle_tRNS(png_ptr
, info_ptr
, length
);
243 #ifdef PNG_READ_zTXt_SUPPORTED
244 else if (chunk_name
== png_zTXt
)
245 png_handle_zTXt(png_ptr
, info_ptr
, length
);
248 #ifdef PNG_READ_iTXt_SUPPORTED
249 else if (chunk_name
== png_iTXt
)
250 png_handle_iTXt(png_ptr
, info_ptr
, length
);
254 png_handle_unknown(png_ptr
, info_ptr
, length
,
255 PNG_HANDLE_CHUNK_AS_DEFAULT
);
258 #endif /* SEQUENTIAL_READ */
260 /* Optional call to update the users info_ptr structure */
262 png_read_update_info(png_structrp png_ptr
, png_inforp info_ptr
)
264 png_debug(1, "in png_read_update_info");
268 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
270 png_read_start_row(png_ptr
);
272 # ifdef PNG_READ_TRANSFORMS_SUPPORTED
273 png_read_transform_info(png_ptr
, info_ptr
);
279 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
281 png_app_error(png_ptr
,
282 "png_read_update_info/png_start_read_image: duplicate call");
286 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
287 /* Initialize palette, background, etc, after transformations
288 * are set, but before any reading takes place. This allows
289 * the user to obtain a gamma-corrected palette, for example.
290 * If the user doesn't call this, we will do it ourselves.
293 png_start_read_image(png_structrp png_ptr
)
295 png_debug(1, "in png_start_read_image");
299 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
300 png_read_start_row(png_ptr
);
302 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
304 png_app_error(png_ptr
,
305 "png_start_read_image/png_read_update_info: duplicate call");
308 #endif /* SEQUENTIAL_READ */
310 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
311 #ifdef PNG_MNG_FEATURES_SUPPORTED
312 /* Undoes intrapixel differencing,
313 * NOTE: this is apparently only supported in the 'sequential' reader.
316 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
318 png_debug(1, "in png_do_read_intrapixel");
321 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
324 png_uint_32 row_width
= row_info
->width
;
326 if (row_info
->bit_depth
== 8)
331 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
334 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
340 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
342 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+ 1)) & 0xff);
343 *(rp
+2) = (png_byte
)((256 + *(rp
+ 2) + *(rp
+ 1)) & 0xff);
346 else if (row_info
->bit_depth
== 16)
351 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
354 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
360 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
362 png_uint_32 s0
= (png_uint_32
)(*(rp
) << 8) | *(rp
+ 1);
363 png_uint_32 s1
= (png_uint_32
)(*(rp
+ 2) << 8) | *(rp
+ 3);
364 png_uint_32 s2
= (png_uint_32
)(*(rp
+ 4) << 8) | *(rp
+ 5);
365 png_uint_32 red
= (s0
+ s1
+ 65536) & 0xffff;
366 png_uint_32 blue
= (s2
+ s1
+ 65536) & 0xffff;
367 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
368 *(rp
+ 1) = (png_byte
)(red
& 0xff);
369 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
370 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
375 #endif /* MNG_FEATURES */
378 png_read_row(png_structrp png_ptr
, png_bytep row
, png_bytep dsp_row
)
380 png_row_info row_info
;
385 png_debug2(1, "in png_read_row (row %lu, pass %d)",
386 (unsigned long)png_ptr
->row_number
, png_ptr
->pass
);
388 /* png_read_start_row sets the information (in particular iwidth) for this
391 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
392 png_read_start_row(png_ptr
);
394 /* 1.5.6: row_info moved out of png_struct to a local here. */
395 row_info
.width
= png_ptr
->iwidth
; /* NOTE: width of current interlaced row */
396 row_info
.color_type
= png_ptr
->color_type
;
397 row_info
.bit_depth
= png_ptr
->bit_depth
;
398 row_info
.channels
= png_ptr
->channels
;
399 row_info
.pixel_depth
= png_ptr
->pixel_depth
;
400 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
402 #ifdef PNG_WARNINGS_SUPPORTED
403 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
405 /* Check for transforms that have been set but were defined out */
406 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
407 if ((png_ptr
->transformations
& PNG_INVERT_MONO
) != 0)
408 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined");
411 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
412 if ((png_ptr
->transformations
& PNG_FILLER
) != 0)
413 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined");
416 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
417 !defined(PNG_READ_PACKSWAP_SUPPORTED)
418 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
419 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
422 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
423 if ((png_ptr
->transformations
& PNG_PACK
) != 0)
424 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined");
427 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
428 if ((png_ptr
->transformations
& PNG_SHIFT
) != 0)
429 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined");
432 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
433 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
434 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined");
437 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
438 if ((png_ptr
->transformations
& PNG_SWAP_BYTES
) != 0)
439 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined");
442 #endif /* WARNINGS */
444 #ifdef PNG_READ_INTERLACING_SUPPORTED
445 /* If interlaced and we do not need a new row, combine row and return.
446 * Notice that the pixels we have from previous rows have been transformed
447 * already; we can only combine like with like (transformed or
448 * untransformed) and, because of the libpng API for interlaced images, this
449 * means we must transform before de-interlacing.
451 if (png_ptr
->interlaced
!= 0 &&
452 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
454 switch (png_ptr
->pass
)
457 if (png_ptr
->row_number
& 0x07)
460 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
461 png_read_finish_row(png_ptr
);
467 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
470 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
472 png_read_finish_row(png_ptr
);
478 if ((png_ptr
->row_number
& 0x07) != 4)
480 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
481 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
483 png_read_finish_row(png_ptr
);
489 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
492 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
494 png_read_finish_row(png_ptr
);
500 if ((png_ptr
->row_number
& 3) != 2)
502 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
503 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
505 png_read_finish_row(png_ptr
);
511 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
514 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
516 png_read_finish_row(png_ptr
);
523 if ((png_ptr
->row_number
& 1) == 0)
525 png_read_finish_row(png_ptr
);
533 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) == 0)
534 png_error(png_ptr
, "Invalid attempt to read row data");
536 /* Fill the row with IDAT data: */
537 png_read_IDAT_data(png_ptr
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
539 if (png_ptr
->row_buf
[0] > PNG_FILTER_VALUE_NONE
)
541 if (png_ptr
->row_buf
[0] < PNG_FILTER_VALUE_LAST
)
542 png_read_filter_row(png_ptr
, &row_info
, png_ptr
->row_buf
+ 1,
543 png_ptr
->prev_row
+ 1, png_ptr
->row_buf
[0]);
545 png_error(png_ptr
, "bad adaptive filter value");
548 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
549 * 1.5.6, while the buffer really is this big in current versions of libpng
550 * it may not be in the future, so this was changed just to copy the
553 memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
555 #ifdef PNG_MNG_FEATURES_SUPPORTED
556 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) != 0 &&
557 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
559 /* Intrapixel differencing */
560 png_do_read_intrapixel(&row_info
, png_ptr
->row_buf
+ 1);
564 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
565 if (png_ptr
->transformations
)
566 png_do_read_transformations(png_ptr
, &row_info
);
569 /* The transformed pixel depth should match the depth now in row_info. */
570 if (png_ptr
->transformed_pixel_depth
== 0)
572 png_ptr
->transformed_pixel_depth
= row_info
.pixel_depth
;
573 if (row_info
.pixel_depth
> png_ptr
->maximum_pixel_depth
)
574 png_error(png_ptr
, "sequential row overflow");
577 else if (png_ptr
->transformed_pixel_depth
!= row_info
.pixel_depth
)
578 png_error(png_ptr
, "internal sequential row size calculation error");
580 #ifdef PNG_READ_INTERLACING_SUPPORTED
581 /* Expand interlaced rows to full size */
582 if (png_ptr
->interlaced
!= 0 &&
583 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
585 if (png_ptr
->pass
< 6)
586 png_do_read_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
,
587 png_ptr
->transformations
);
590 png_combine_row(png_ptr
, dsp_row
, 1/*display*/);
593 png_combine_row(png_ptr
, row
, 0/*row*/);
600 png_combine_row(png_ptr
, row
, -1/*ignored*/);
603 png_combine_row(png_ptr
, dsp_row
, -1/*ignored*/);
605 png_read_finish_row(png_ptr
);
607 if (png_ptr
->read_row_fn
!= NULL
)
608 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
611 #endif /* SEQUENTIAL_READ */
613 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
614 /* Read one or more rows of image data. If the image is interlaced,
615 * and png_set_interlace_handling() has been called, the rows need to
616 * contain the contents of the rows from the previous pass. If the
617 * image has alpha or transparency, and png_handle_alpha()[*] has been
618 * called, the rows contents must be initialized to the contents of the
621 * "row" holds the actual image, and pixels are placed in it
622 * as they arrive. If the image is displayed after each pass, it will
623 * appear to "sparkle" in. "display_row" can be used to display a
624 * "chunky" progressive image, with finer detail added as it becomes
625 * available. If you do not want this "chunky" display, you may pass
626 * NULL for display_row. If you do not want the sparkle display, and
627 * you have not called png_handle_alpha(), you may pass NULL for rows.
628 * If you have called png_handle_alpha(), and the image has either an
629 * alpha channel or a transparency chunk, you must provide a buffer for
630 * rows. In this case, you do not have to provide a display_row buffer
631 * also, but you may. If the image is not interlaced, or if you have
632 * not called png_set_interlace_handling(), the display_row buffer will
633 * be ignored, so pass NULL to it.
635 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
639 png_read_rows(png_structrp png_ptr
, png_bytepp row
,
640 png_bytepp display_row
, png_uint_32 num_rows
)
646 png_debug(1, "in png_read_rows");
653 if (rp
!= NULL
&& dp
!= NULL
)
654 for (i
= 0; i
< num_rows
; i
++)
656 png_bytep rptr
= *rp
++;
657 png_bytep dptr
= *dp
++;
659 png_read_row(png_ptr
, rptr
, dptr
);
663 for (i
= 0; i
< num_rows
; i
++)
665 png_bytep rptr
= *rp
;
666 png_read_row(png_ptr
, rptr
, NULL
);
671 for (i
= 0; i
< num_rows
; i
++)
673 png_bytep dptr
= *dp
;
674 png_read_row(png_ptr
, NULL
, dptr
);
678 #endif /* SEQUENTIAL_READ */
680 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
681 /* Read the entire image. If the image has an alpha channel or a tRNS
682 * chunk, and you have called png_handle_alpha()[*], you will need to
683 * initialize the image to the current image that PNG will be overlaying.
684 * We set the num_rows again here, in case it was incorrectly set in
685 * png_read_start_row() by a call to png_read_update_info() or
686 * png_start_read_image() if png_set_interlace_handling() wasn't called
687 * prior to either of these functions like it should have been. You can
688 * only call this function once. If you desire to have an image for
689 * each pass of a interlaced image, use png_read_rows() instead.
691 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
694 png_read_image(png_structrp png_ptr
, png_bytepp image
)
696 png_uint_32 i
, image_height
;
700 png_debug(1, "in png_read_image");
705 #ifdef PNG_READ_INTERLACING_SUPPORTED
706 if ((png_ptr
->flags
& PNG_FLAG_ROW_INIT
) == 0)
708 pass
= png_set_interlace_handling(png_ptr
);
709 /* And make sure transforms are initialized. */
710 png_start_read_image(png_ptr
);
714 if (png_ptr
->interlaced
!= 0 &&
715 (png_ptr
->transformations
& PNG_INTERLACE
) == 0)
717 /* Caller called png_start_read_image or png_read_update_info without
718 * first turning on the PNG_INTERLACE transform. We can fix this here,
719 * but the caller should do it!
721 png_warning(png_ptr
, "Interlace handling should be turned on when "
722 "using png_read_image");
723 /* Make sure this is set correctly */
724 png_ptr
->num_rows
= png_ptr
->height
;
727 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
728 * the above error case.
730 pass
= png_set_interlace_handling(png_ptr
);
733 if (png_ptr
->interlaced
)
735 "Cannot read interlaced image -- interlace handler disabled");
740 image_height
=png_ptr
->height
;
742 for (j
= 0; j
< pass
; j
++)
745 for (i
= 0; i
< image_height
; i
++)
747 png_read_row(png_ptr
, *rp
, NULL
);
752 #endif /* SEQUENTIAL_READ */
754 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
755 /* Read the end of the PNG file. Will not read past the end of the
756 * file, will verify the end is accurate, and will read any comments
757 * or time information at the end of the file, if info is not NULL.
760 png_read_end(png_structrp png_ptr
, png_inforp info_ptr
)
762 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
766 png_debug(1, "in png_read_end");
771 /* If png_read_end is called in the middle of reading the rows there may
772 * still be pending IDAT data and an owned zstream. Deal with this here.
774 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
775 if (png_chunk_unknown_handling(png_ptr
, png_IDAT
) == 0)
777 png_read_finish_IDAT(png_ptr
);
779 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
780 /* Report invalid palette index; added at libng-1.5.10 */
781 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
782 png_ptr
->num_palette_max
> png_ptr
->num_palette
)
783 png_benign_error(png_ptr
, "Read palette index exceeding num_palette");
788 png_uint_32 length
= png_read_chunk_header(png_ptr
);
789 png_uint_32 chunk_name
= png_ptr
->chunk_name
;
791 if (chunk_name
!= png_IDAT
)
792 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
794 if (chunk_name
== png_IEND
)
795 png_handle_IEND(png_ptr
, info_ptr
, length
);
797 else if (chunk_name
== png_IHDR
)
798 png_handle_IHDR(png_ptr
, info_ptr
, length
);
800 else if (info_ptr
== NULL
)
801 png_crc_finish(png_ptr
, length
);
803 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
804 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
806 if (chunk_name
== png_IDAT
)
808 if ((length
> 0 && !(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
809 || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
810 png_benign_error(png_ptr
, ".Too many IDATs found");
812 png_handle_unknown(png_ptr
, info_ptr
, length
, keep
);
813 if (chunk_name
== png_PLTE
)
814 png_ptr
->mode
|= PNG_HAVE_PLTE
;
818 else if (chunk_name
== png_IDAT
)
820 /* Zero length IDATs are legal after the last IDAT has been
821 * read, but not after other chunks have been read. 1.6 does not
822 * always read all the deflate data; specifically it cannot be relied
823 * upon to read the Adler32 at the end. If it doesn't ignore IDAT
824 * chunks which are longer than zero as well:
826 if ((length
> 0 && !(png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
))
827 || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) != 0)
828 png_benign_error(png_ptr
, "..Too many IDATs found");
830 png_crc_finish(png_ptr
, length
);
832 else if (chunk_name
== png_PLTE
)
833 png_handle_PLTE(png_ptr
, info_ptr
, length
);
835 #ifdef PNG_READ_bKGD_SUPPORTED
836 else if (chunk_name
== png_bKGD
)
837 png_handle_bKGD(png_ptr
, info_ptr
, length
);
840 #ifdef PNG_READ_cHRM_SUPPORTED
841 else if (chunk_name
== png_cHRM
)
842 png_handle_cHRM(png_ptr
, info_ptr
, length
);
845 #ifdef PNG_READ_gAMA_SUPPORTED
846 else if (chunk_name
== png_gAMA
)
847 png_handle_gAMA(png_ptr
, info_ptr
, length
);
850 #ifdef PNG_READ_hIST_SUPPORTED
851 else if (chunk_name
== png_hIST
)
852 png_handle_hIST(png_ptr
, info_ptr
, length
);
855 #ifdef PNG_READ_oFFs_SUPPORTED
856 else if (chunk_name
== png_oFFs
)
857 png_handle_oFFs(png_ptr
, info_ptr
, length
);
860 #ifdef PNG_READ_pCAL_SUPPORTED
861 else if (chunk_name
== png_pCAL
)
862 png_handle_pCAL(png_ptr
, info_ptr
, length
);
865 #ifdef PNG_READ_sCAL_SUPPORTED
866 else if (chunk_name
== png_sCAL
)
867 png_handle_sCAL(png_ptr
, info_ptr
, length
);
870 #ifdef PNG_READ_pHYs_SUPPORTED
871 else if (chunk_name
== png_pHYs
)
872 png_handle_pHYs(png_ptr
, info_ptr
, length
);
875 #ifdef PNG_READ_sBIT_SUPPORTED
876 else if (chunk_name
== png_sBIT
)
877 png_handle_sBIT(png_ptr
, info_ptr
, length
);
880 #ifdef PNG_READ_sRGB_SUPPORTED
881 else if (chunk_name
== png_sRGB
)
882 png_handle_sRGB(png_ptr
, info_ptr
, length
);
885 #ifdef PNG_READ_iCCP_SUPPORTED
886 else if (chunk_name
== png_iCCP
)
887 png_handle_iCCP(png_ptr
, info_ptr
, length
);
890 #ifdef PNG_READ_sPLT_SUPPORTED
891 else if (chunk_name
== png_sPLT
)
892 png_handle_sPLT(png_ptr
, info_ptr
, length
);
895 #ifdef PNG_READ_tEXt_SUPPORTED
896 else if (chunk_name
== png_tEXt
)
897 png_handle_tEXt(png_ptr
, info_ptr
, length
);
900 #ifdef PNG_READ_tIME_SUPPORTED
901 else if (chunk_name
== png_tIME
)
902 png_handle_tIME(png_ptr
, info_ptr
, length
);
905 #ifdef PNG_READ_tRNS_SUPPORTED
906 else if (chunk_name
== png_tRNS
)
907 png_handle_tRNS(png_ptr
, info_ptr
, length
);
910 #ifdef PNG_READ_zTXt_SUPPORTED
911 else if (chunk_name
== png_zTXt
)
912 png_handle_zTXt(png_ptr
, info_ptr
, length
);
915 #ifdef PNG_READ_iTXt_SUPPORTED
916 else if (chunk_name
== png_iTXt
)
917 png_handle_iTXt(png_ptr
, info_ptr
, length
);
921 png_handle_unknown(png_ptr
, info_ptr
, length
,
922 PNG_HANDLE_CHUNK_AS_DEFAULT
);
923 } while ((png_ptr
->mode
& PNG_HAVE_IEND
) == 0);
925 #endif /* SEQUENTIAL_READ */
927 /* Free all memory used in the read struct */
929 png_read_destroy(png_structrp png_ptr
)
931 png_debug(1, "in png_read_destroy");
933 #ifdef PNG_READ_GAMMA_SUPPORTED
934 png_destroy_gamma_table(png_ptr
);
937 png_free(png_ptr
, png_ptr
->big_row_buf
);
938 png_ptr
->big_row_buf
= NULL
;
939 png_free(png_ptr
, png_ptr
->big_prev_row
);
940 png_ptr
->big_prev_row
= NULL
;
941 png_free(png_ptr
, png_ptr
->read_buffer
);
942 png_ptr
->read_buffer
= NULL
;
944 #ifdef PNG_READ_QUANTIZE_SUPPORTED
945 png_free(png_ptr
, png_ptr
->palette_lookup
);
946 png_ptr
->palette_lookup
= NULL
;
947 png_free(png_ptr
, png_ptr
->quantize_index
);
948 png_ptr
->quantize_index
= NULL
;
951 if ((png_ptr
->free_me
& PNG_FREE_PLTE
) != 0)
953 png_zfree(png_ptr
, png_ptr
->palette
);
954 png_ptr
->palette
= NULL
;
956 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
958 #if defined(PNG_tRNS_SUPPORTED) || \
959 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
960 if ((png_ptr
->free_me
& PNG_FREE_TRNS
) != 0)
962 png_free(png_ptr
, png_ptr
->trans_alpha
);
963 png_ptr
->trans_alpha
= NULL
;
965 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
968 inflateEnd(&png_ptr
->zstream
);
970 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
971 png_free(png_ptr
, png_ptr
->save_buffer
);
972 png_ptr
->save_buffer
= NULL
;
975 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
976 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
977 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
978 png_ptr
->unknown_chunk
.data
= NULL
;
981 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
982 png_free(png_ptr
, png_ptr
->chunk_list
);
983 png_ptr
->chunk_list
= NULL
;
986 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
987 * callbacks are still set at this point. They are required to complete the
988 * destruction of the png_struct itself.
992 /* Free all memory used by the read */
994 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
995 png_infopp end_info_ptr_ptr
)
997 png_structrp png_ptr
= NULL
;
999 png_debug(1, "in png_destroy_read_struct");
1001 if (png_ptr_ptr
!= NULL
)
1002 png_ptr
= *png_ptr_ptr
;
1004 if (png_ptr
== NULL
)
1007 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1008 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1009 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1011 png_destroy_info_struct(png_ptr
, end_info_ptr_ptr
);
1012 png_destroy_info_struct(png_ptr
, info_ptr_ptr
);
1014 *png_ptr_ptr
= NULL
;
1015 png_read_destroy(png_ptr
);
1016 png_destroy_png_struct(png_ptr
);
1020 png_set_read_status_fn(png_structrp png_ptr
, png_read_status_ptr read_row_fn
)
1022 if (png_ptr
== NULL
)
1025 png_ptr
->read_row_fn
= read_row_fn
;
1029 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1030 #ifdef PNG_INFO_IMAGE_SUPPORTED
1032 png_read_png(png_structrp png_ptr
, png_inforp info_ptr
,
1033 int transforms
, voidp params
)
1035 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1038 /* png_read_info() gives us all of the information from the
1039 * PNG file before the first IDAT (image data chunk).
1041 png_read_info(png_ptr
, info_ptr
);
1042 if (info_ptr
->height
> PNG_UINT_32_MAX
/(sizeof (png_bytep
)))
1043 png_error(png_ptr
, "Image is too high to process with png_read_png()");
1045 /* -------------- image transformations start here ------------------- */
1046 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1047 * is not implemented. This will only happen in de-configured (non-default)
1048 * libpng builds. The results can be unexpected - png_read_png may return
1049 * short or mal-formed rows because the transform is skipped.
1052 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1054 if ((transforms
& PNG_TRANSFORM_SCALE_16
) != 0)
1055 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1056 * did in earlier versions, while "scale_16" is now more accurate.
1058 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1059 png_set_scale_16(png_ptr
);
1061 png_app_error(png_ptr
, "PNG_TRANSFORM_SCALE_16 not supported");
1064 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1065 * latter by doing SCALE first. This is ok and allows apps not to check for
1066 * which is supported to get the right answer.
1068 if ((transforms
& PNG_TRANSFORM_STRIP_16
) != 0)
1069 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1070 png_set_strip_16(png_ptr
);
1072 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_16 not supported");
1075 /* Strip alpha bytes from the input data without combining with
1076 * the background (not recommended).
1078 if ((transforms
& PNG_TRANSFORM_STRIP_ALPHA
) != 0)
1079 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1080 png_set_strip_alpha(png_ptr
);
1082 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_ALPHA not supported");
1085 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1086 * byte into separate bytes (useful for paletted and grayscale images).
1088 if ((transforms
& PNG_TRANSFORM_PACKING
) != 0)
1089 #ifdef PNG_READ_PACK_SUPPORTED
1090 png_set_packing(png_ptr
);
1092 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKING not supported");
1095 /* Change the order of packed pixels to least significant bit first
1096 * (not useful if you are using png_set_packing).
1098 if ((transforms
& PNG_TRANSFORM_PACKSWAP
) != 0)
1099 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1100 png_set_packswap(png_ptr
);
1102 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKSWAP not supported");
1105 /* Expand paletted colors into true RGB triplets
1106 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1107 * Expand paletted or RGB images with transparency to full alpha
1108 * channels so the data will be available as RGBA quartets.
1110 if ((transforms
& PNG_TRANSFORM_EXPAND
) != 0)
1111 #ifdef PNG_READ_EXPAND_SUPPORTED
1112 png_set_expand(png_ptr
);
1114 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND not supported");
1117 /* We don't handle background color or gamma transformation or quantizing.
1120 /* Invert monochrome files to have 0 as white and 1 as black
1122 if ((transforms
& PNG_TRANSFORM_INVERT_MONO
) != 0)
1123 #ifdef PNG_READ_INVERT_SUPPORTED
1124 png_set_invert_mono(png_ptr
);
1126 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_MONO not supported");
1129 /* If you want to shift the pixel values from the range [0,255] or
1130 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1131 * colors were originally in:
1133 if ((transforms
& PNG_TRANSFORM_SHIFT
) != 0)
1134 #ifdef PNG_READ_SHIFT_SUPPORTED
1135 if ((info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
1136 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1138 png_app_error(png_ptr
, "PNG_TRANSFORM_SHIFT not supported");
1141 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1142 if ((transforms
& PNG_TRANSFORM_BGR
) != 0)
1143 #ifdef PNG_READ_BGR_SUPPORTED
1144 png_set_bgr(png_ptr
);
1146 png_app_error(png_ptr
, "PNG_TRANSFORM_BGR not supported");
1149 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1150 if ((transforms
& PNG_TRANSFORM_SWAP_ALPHA
) != 0)
1151 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1152 png_set_swap_alpha(png_ptr
);
1154 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1157 /* Swap bytes of 16-bit files to least significant byte first */
1158 if ((transforms
& PNG_TRANSFORM_SWAP_ENDIAN
) != 0)
1159 #ifdef PNG_READ_SWAP_SUPPORTED
1160 png_set_swap(png_ptr
);
1162 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1165 /* Added at libpng-1.2.41 */
1166 /* Invert the alpha channel from opacity to transparency */
1167 if ((transforms
& PNG_TRANSFORM_INVERT_ALPHA
) != 0)
1168 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1169 png_set_invert_alpha(png_ptr
);
1171 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1174 /* Added at libpng-1.2.41 */
1175 /* Expand grayscale image to RGB */
1176 if ((transforms
& PNG_TRANSFORM_GRAY_TO_RGB
) != 0)
1177 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1178 png_set_gray_to_rgb(png_ptr
);
1180 png_app_error(png_ptr
, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1183 /* Added at libpng-1.5.4 */
1184 if ((transforms
& PNG_TRANSFORM_EXPAND_16
) != 0)
1185 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1186 png_set_expand_16(png_ptr
);
1188 png_app_error(png_ptr
, "PNG_TRANSFORM_EXPAND_16 not supported");
1191 /* We don't handle adding filler bytes */
1193 /* We use png_read_image and rely on that for interlace handling, but we also
1194 * call png_read_update_info therefore must turn on interlace handling now:
1196 (void)png_set_interlace_handling(png_ptr
);
1198 /* Optional call to gamma correct and add the background to the palette
1199 * and update info structure. REQUIRED if you are expecting libpng to
1200 * update the palette for you (i.e., you selected such a transform above).
1202 png_read_update_info(png_ptr
, info_ptr
);
1204 /* -------------- image transformations end here ------------------- */
1206 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1207 if (info_ptr
->row_pointers
== NULL
)
1211 info_ptr
->row_pointers
= png_voidcast(png_bytepp
, png_malloc(png_ptr
,
1212 info_ptr
->height
* (sizeof (png_bytep
))));
1214 for (iptr
=0; iptr
<info_ptr
->height
; iptr
++)
1215 info_ptr
->row_pointers
[iptr
] = NULL
;
1217 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1219 for (iptr
= 0; iptr
< info_ptr
->height
; iptr
++)
1220 info_ptr
->row_pointers
[iptr
] = png_voidcast(png_bytep
,
1221 png_malloc(png_ptr
, info_ptr
->rowbytes
));
1224 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1225 info_ptr
->valid
|= PNG_INFO_IDAT
;
1227 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1228 png_read_end(png_ptr
, info_ptr
);
1232 #endif /* INFO_IMAGE */
1233 #endif /* SEQUENTIAL_READ */
1235 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1238 * This code currently relies on the sequential reader, though it could easily
1239 * be made to work with the progressive one.
1241 /* Arguments to png_image_finish_read: */
1243 /* Encoding of PNG data (used by the color-map code) */
1244 # define P_NOTSET 0 /* File encoding not yet known */
1245 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1246 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1247 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1248 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1250 /* Color-map processing: after libpng has run on the PNG image further
1251 * processing may be needed to convert the data to color-map indices.
1253 #define PNG_CMAP_NONE 0
1254 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1255 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1256 #define PNG_CMAP_RGB 3 /* Process RGB data */
1257 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1259 /* The following document where the background is for each processing case. */
1260 #define PNG_CMAP_NONE_BACKGROUND 256
1261 #define PNG_CMAP_GA_BACKGROUND 231
1262 #define PNG_CMAP_TRANS_BACKGROUND 254
1263 #define PNG_CMAP_RGB_BACKGROUND 256
1264 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1271 png_int_32 row_stride
;
1273 png_const_colorp background
;
1274 /* Local variables: */
1275 png_voidp local_row
;
1276 png_voidp first_row
;
1277 ptrdiff_t row_bytes
; /* step between rows */
1278 int file_encoding
; /* E_ values above */
1279 png_fixed_point gamma_to_linear
; /* For P_FILE, reciprocal of gamma */
1280 int colormap_processing
; /* PNG_CMAP_ values above */
1281 } png_image_read_control
;
1283 /* Do all the *safe* initialization - 'safe' means that png_error won't be
1284 * called, so setting up the jmp_buf is not required. This means that anything
1285 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1286 * instead so that control is returned safely back to this routine.
1289 png_image_read_init(png_imagep image
)
1291 if (image
->opaque
== NULL
)
1293 png_structp png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, image
,
1294 png_safe_error
, png_safe_warning
);
1296 /* And set the rest of the structure to NULL to ensure that the various
1297 * fields are consistent.
1299 memset(image
, 0, (sizeof *image
));
1300 image
->version
= PNG_IMAGE_VERSION
;
1302 if (png_ptr
!= NULL
)
1304 png_infop info_ptr
= png_create_info_struct(png_ptr
);
1306 if (info_ptr
!= NULL
)
1308 png_controlp control
= png_voidcast(png_controlp
,
1309 png_malloc_warn(png_ptr
, (sizeof *control
)));
1311 if (control
!= NULL
)
1313 memset(control
, 0, (sizeof *control
));
1315 control
->png_ptr
= png_ptr
;
1316 control
->info_ptr
= info_ptr
;
1317 control
->for_write
= 0;
1319 image
->opaque
= control
;
1323 /* Error clean up */
1324 png_destroy_info_struct(png_ptr
, &info_ptr
);
1327 png_destroy_read_struct(&png_ptr
, NULL
, NULL
);
1330 return png_image_error(image
, "png_image_read: out of memory");
1333 return png_image_error(image
, "png_image_read: opaque pointer not NULL");
1336 /* Utility to find the base format of a PNG file from a png_struct. */
1338 png_image_format(png_structrp png_ptr
)
1340 png_uint_32 format
= 0;
1342 if ((png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
1343 format
|= PNG_FORMAT_FLAG_COLOR
;
1345 if ((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
1346 format
|= PNG_FORMAT_FLAG_ALPHA
;
1348 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1349 * sets the png_struct fields; that's all we are interested in here. The
1350 * precise interaction with an app call to png_set_tRNS and PNG file reading
1353 else if (png_ptr
->num_trans
> 0)
1354 format
|= PNG_FORMAT_FLAG_ALPHA
;
1356 if (png_ptr
->bit_depth
== 16)
1357 format
|= PNG_FORMAT_FLAG_LINEAR
;
1359 if ((png_ptr
->color_type
& PNG_COLOR_MASK_PALETTE
) != 0)
1360 format
|= PNG_FORMAT_FLAG_COLORMAP
;
1365 /* Is the given gamma significantly different from sRGB? The test is the same
1366 * one used in pngrtran.c when deciding whether to do gamma correction. The
1367 * arithmetic optimizes the division by using the fact that the inverse of the
1368 * file sRGB gamma is 2.2
1371 png_gamma_not_sRGB(png_fixed_point g
)
1375 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1379 return png_gamma_significant((g
* 11 + 2)/5 /* i.e. *2.2, rounded */);
1385 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
1386 * header and fill in all the information. This is executed in a safe context,
1387 * unlike the init routine above.
1390 png_image_read_header(png_voidp argument
)
1392 png_imagep image
= png_voidcast(png_imagep
, argument
);
1393 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1394 png_inforp info_ptr
= image
->opaque
->info_ptr
;
1396 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
1397 png_set_benign_errors(png_ptr
, 1/*warn*/);
1399 png_read_info(png_ptr
, info_ptr
);
1401 /* Do this the fast way; just read directly out of png_struct. */
1402 image
->width
= png_ptr
->width
;
1403 image
->height
= png_ptr
->height
;
1406 png_uint_32 format
= png_image_format(png_ptr
);
1408 image
->format
= format
;
1410 #ifdef PNG_COLORSPACE_SUPPORTED
1411 /* Does the colorspace match sRGB? If there is no color endpoint
1412 * (colorant) information assume yes, otherwise require the
1413 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1414 * colorspace has been determined to be invalid ignore it.
1416 if ((format
& PNG_FORMAT_FLAG_COLOR
) != 0 && ((png_ptr
->colorspace
.flags
1417 & (PNG_COLORSPACE_HAVE_ENDPOINTS
|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB
|
1418 PNG_COLORSPACE_INVALID
)) == PNG_COLORSPACE_HAVE_ENDPOINTS
))
1419 image
->flags
|= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
;
1423 /* We need the maximum number of entries regardless of the format the
1424 * application sets here.
1427 png_uint_32 cmap_entries
;
1429 switch (png_ptr
->color_type
)
1431 case PNG_COLOR_TYPE_GRAY
:
1432 cmap_entries
= 1U << png_ptr
->bit_depth
;
1435 case PNG_COLOR_TYPE_PALETTE
:
1436 cmap_entries
= (png_uint_32
)png_ptr
->num_palette
;
1444 if (cmap_entries
> 256)
1447 image
->colormap_entries
= cmap_entries
;
1453 #ifdef PNG_STDIO_SUPPORTED
1455 png_image_begin_read_from_stdio(png_imagep image
, FILE* file
)
1457 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1461 if (png_image_read_init(image
) != 0)
1463 /* This is slightly evil, but png_init_io doesn't do anything other
1464 * than this and we haven't changed the standard IO functions so
1465 * this saves a 'safe' function.
1467 image
->opaque
->png_ptr
->io_ptr
= file
;
1468 return png_safe_execute(image
, png_image_read_header
, image
);
1473 return png_image_error(image
,
1474 "png_image_begin_read_from_stdio: invalid argument");
1477 else if (image
!= NULL
)
1478 return png_image_error(image
,
1479 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1485 png_image_begin_read_from_file(png_imagep image
, const char *file_name
)
1487 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1489 if (file_name
!= NULL
)
1491 FILE *fp
= fopen(file_name
, "rb");
1495 if (png_image_read_init(image
) != 0)
1497 image
->opaque
->png_ptr
->io_ptr
= fp
;
1498 image
->opaque
->owned_file
= 1;
1499 return png_safe_execute(image
, png_image_read_header
, image
);
1502 /* Clean up: just the opened file. */
1507 return png_image_error(image
, strerror(errno
));
1511 return png_image_error(image
,
1512 "png_image_begin_read_from_file: invalid argument");
1515 else if (image
!= NULL
)
1516 return png_image_error(image
,
1517 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1523 static void PNGCBAPI
1524 png_image_memory_read(png_structp png_ptr
, png_bytep out
, png_size_t need
)
1526 if (png_ptr
!= NULL
)
1528 png_imagep image
= png_voidcast(png_imagep
, png_ptr
->io_ptr
);
1531 png_controlp cp
= image
->opaque
;
1534 png_const_bytep memory
= cp
->memory
;
1535 png_size_t size
= cp
->size
;
1537 if (memory
!= NULL
&& size
>= need
)
1539 memcpy(out
, memory
, need
);
1540 cp
->memory
= memory
+ need
;
1541 cp
->size
= size
- need
;
1545 png_error(png_ptr
, "read beyond end of data");
1549 png_error(png_ptr
, "invalid memory read");
1553 int PNGAPI
png_image_begin_read_from_memory(png_imagep image
,
1554 png_const_voidp memory
, png_size_t size
)
1556 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
1558 if (memory
!= NULL
&& size
> 0)
1560 if (png_image_read_init(image
) != 0)
1562 /* Now set the IO functions to read from the memory buffer and
1563 * store it into io_ptr. Again do this in-place to avoid calling a
1564 * libpng function that requires error handling.
1566 image
->opaque
->memory
= png_voidcast(png_const_bytep
, memory
);
1567 image
->opaque
->size
= size
;
1568 image
->opaque
->png_ptr
->io_ptr
= image
;
1569 image
->opaque
->png_ptr
->read_data_fn
= png_image_memory_read
;
1571 return png_safe_execute(image
, png_image_read_header
, image
);
1576 return png_image_error(image
,
1577 "png_image_begin_read_from_memory: invalid argument");
1580 else if (image
!= NULL
)
1581 return png_image_error(image
,
1582 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1587 /* Utility function to skip chunks that are not used by the simplified image
1588 * read functions and an appropriate macro to call it.
1590 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1592 png_image_skip_unused_chunks(png_structrp png_ptr
)
1594 /* Prepare the reader to ignore all recognized chunks whose data will not
1595 * be used, i.e., all chunks recognized by libpng except for those
1596 * involved in basic image reading:
1598 * IHDR, PLTE, IDAT, IEND
1600 * Or image data handling:
1602 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1604 * This provides a small performance improvement and eliminates any
1605 * potential vulnerability to security problems in the unused chunks.
1607 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1608 * too. This allows the simplified API to be compiled without iCCP support,
1609 * however if the support is there the chunk is still checked to detect
1610 * errors (which are unfortunately quite common.)
1613 static PNG_CONST png_byte chunks_to_process
[] = {
1614 98, 75, 71, 68, '\0', /* bKGD */
1615 99, 72, 82, 77, '\0', /* cHRM */
1616 103, 65, 77, 65, '\0', /* gAMA */
1617 # ifdef PNG_READ_iCCP_SUPPORTED
1618 105, 67, 67, 80, '\0', /* iCCP */
1620 115, 66, 73, 84, '\0', /* sBIT */
1621 115, 82, 71, 66, '\0', /* sRGB */
1624 /* Ignore unknown chunks and all other chunks except for the
1625 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1627 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_NEVER
,
1630 /* But do not ignore image data handling chunks */
1631 png_set_keep_unknown_chunks(png_ptr
, PNG_HANDLE_CHUNK_AS_DEFAULT
,
1632 chunks_to_process
, (int)/*SAFE*/(sizeof chunks_to_process
)/5);
1636 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1638 # define PNG_SKIP_CHUNKS(p) ((void)0)
1639 #endif /* HANDLE_AS_UNKNOWN */
1641 /* The following macro gives the exact rounded answer for all values in the
1642 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1643 * the correct numbers 0..5
1645 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1647 /* Utility functions to make particular color-maps */
1649 set_file_encoding(png_image_read_control
*display
)
1651 png_fixed_point g
= display
->image
->opaque
->png_ptr
->colorspace
.gamma
;
1652 if (png_gamma_significant(g
) != 0)
1654 if (png_gamma_not_sRGB(g
) != 0)
1656 display
->file_encoding
= P_FILE
;
1657 display
->gamma_to_linear
= png_reciprocal(g
);
1661 display
->file_encoding
= P_sRGB
;
1665 display
->file_encoding
= P_LINEAR8
;
1669 decode_gamma(png_image_read_control
*display
, png_uint_32 value
, int encoding
)
1671 if (encoding
== P_FILE
) /* double check */
1672 encoding
= display
->file_encoding
;
1674 if (encoding
== P_NOTSET
) /* must be the file encoding */
1676 set_file_encoding(display
);
1677 encoding
= display
->file_encoding
;
1683 value
= png_gamma_16bit_correct(value
*257, display
->gamma_to_linear
);
1687 value
= png_sRGB_table
[value
];
1699 png_error(display
->image
->opaque
->png_ptr
,
1700 "unexpected encoding (internal error)");
1708 png_colormap_compose(png_image_read_control
*display
,
1709 png_uint_32 foreground
, int foreground_encoding
, png_uint_32 alpha
,
1710 png_uint_32 background
, int encoding
)
1712 /* The file value is composed on the background, the background has the given
1713 * encoding and so does the result, the file is encoded with P_FILE and the
1714 * file and alpha are 8-bit values. The (output) encoding will always be
1715 * P_LINEAR or P_sRGB.
1717 png_uint_32 f
= decode_gamma(display
, foreground
, foreground_encoding
);
1718 png_uint_32 b
= decode_gamma(display
, background
, encoding
);
1720 /* The alpha is always an 8-bit value (it comes from the palette), the value
1721 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1723 f
= f
* alpha
+ b
* (255-alpha
);
1725 if (encoding
== P_LINEAR
)
1727 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1728 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1730 f
*= 257; /* Now scaled by 65535 */
1732 f
= (f
+32768) >> 16;
1736 f
= PNG_sRGB_FROM_LINEAR(f
);
1741 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1745 png_create_colormap_entry(png_image_read_control
*display
,
1746 png_uint_32 ip
, png_uint_32 red
, png_uint_32 green
, png_uint_32 blue
,
1747 png_uint_32 alpha
, int encoding
)
1749 png_imagep image
= display
->image
;
1750 const int output_encoding
= (image
->format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
1752 const int convert_to_Y
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) == 0 &&
1753 (red
!= green
|| green
!= blue
);
1756 png_error(image
->opaque
->png_ptr
, "color-map index out of range");
1758 /* Update the cache with whether the file gamma is significantly different
1761 if (encoding
== P_FILE
)
1763 if (display
->file_encoding
== P_NOTSET
)
1764 set_file_encoding(display
);
1766 /* Note that the cached value may be P_FILE too, but if it is then the
1767 * gamma_to_linear member has been set.
1769 encoding
= display
->file_encoding
;
1772 if (encoding
== P_FILE
)
1774 png_fixed_point g
= display
->gamma_to_linear
;
1776 red
= png_gamma_16bit_correct(red
*257, g
);
1777 green
= png_gamma_16bit_correct(green
*257, g
);
1778 blue
= png_gamma_16bit_correct(blue
*257, g
);
1780 if (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
)
1783 encoding
= P_LINEAR
;
1788 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1789 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1790 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1795 else if (encoding
== P_LINEAR8
)
1797 /* This encoding occurs quite frequently in test cases because PngSuite
1798 * includes a gAMA 1.0 chunk with most images.
1804 encoding
= P_LINEAR
;
1807 else if (encoding
== P_sRGB
&&
1808 (convert_to_Y
!= 0 || output_encoding
== P_LINEAR
))
1810 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1813 red
= png_sRGB_table
[red
];
1814 green
= png_sRGB_table
[green
];
1815 blue
= png_sRGB_table
[blue
];
1817 encoding
= P_LINEAR
;
1820 /* This is set if the color isn't gray but the output is. */
1821 if (encoding
== P_LINEAR
)
1823 if (convert_to_Y
!= 0)
1825 /* NOTE: these values are copied from png_do_rgb_to_gray */
1826 png_uint_32 y
= (png_uint_32
)6968 * red
+ (png_uint_32
)23434 * green
+
1827 (png_uint_32
)2366 * blue
;
1829 if (output_encoding
== P_LINEAR
)
1830 y
= (y
+ 16384) >> 15;
1834 /* y is scaled by 32768, we need it scaled by 255: */
1837 y
= PNG_sRGB_FROM_LINEAR((y
+ 64) >> 7);
1838 alpha
= PNG_DIV257(alpha
);
1842 blue
= red
= green
= y
;
1845 else if (output_encoding
== P_sRGB
)
1847 red
= PNG_sRGB_FROM_LINEAR(red
* 255);
1848 green
= PNG_sRGB_FROM_LINEAR(green
* 255);
1849 blue
= PNG_sRGB_FROM_LINEAR(blue
* 255);
1850 alpha
= PNG_DIV257(alpha
);
1855 if (encoding
!= output_encoding
)
1856 png_error(image
->opaque
->png_ptr
, "bad encoding (internal error)");
1858 /* Store the value. */
1860 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1861 const int afirst
= (image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0 &&
1862 (image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
1866 # ifdef PNG_FORMAT_BGR_SUPPORTED
1867 const int bgr
= (image
->format
& PNG_FORMAT_FLAG_BGR
) != 0 ? 2 : 0;
1872 if (output_encoding
== P_LINEAR
)
1874 png_uint_16p entry
= png_voidcast(png_uint_16p
, display
->colormap
);
1876 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
1878 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1879 * value, if less than 65535 (this is, effectively, composite on black
1880 * if the alpha channel is removed.)
1882 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
1885 entry
[afirst
? 0 : 3] = (png_uint_16
)alpha
;
1893 blue
= (blue
* alpha
+ 32767U)/65535U;
1894 green
= (green
* alpha
+ 32767U)/65535U;
1895 red
= (red
* alpha
+ 32767U)/65535U;
1899 red
= green
= blue
= 0;
1901 entry
[afirst
+ (2 ^ bgr
)] = (png_uint_16
)blue
;
1902 entry
[afirst
+ 1] = (png_uint_16
)green
;
1903 entry
[afirst
+ bgr
] = (png_uint_16
)red
;
1907 entry
[1 ^ afirst
] = (png_uint_16
)alpha
;
1914 green
= (green
* alpha
+ 32767U)/65535U;
1919 entry
[afirst
] = (png_uint_16
)green
;
1927 else /* output encoding is P_sRGB */
1929 png_bytep entry
= png_voidcast(png_bytep
, display
->colormap
);
1931 entry
+= ip
* PNG_IMAGE_SAMPLE_CHANNELS(image
->format
);
1933 switch (PNG_IMAGE_SAMPLE_CHANNELS(image
->format
))
1936 entry
[afirst
? 0 : 3] = (png_byte
)alpha
;
1938 entry
[afirst
+ (2 ^ bgr
)] = (png_byte
)blue
;
1939 entry
[afirst
+ 1] = (png_byte
)green
;
1940 entry
[afirst
+ bgr
] = (png_byte
)red
;
1944 entry
[1 ^ afirst
] = (png_byte
)alpha
;
1946 entry
[afirst
] = (png_byte
)green
;
1964 make_gray_file_colormap(png_image_read_control
*display
)
1968 for (i
=0; i
<256; ++i
)
1969 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_FILE
);
1975 make_gray_colormap(png_image_read_control
*display
)
1979 for (i
=0; i
<256; ++i
)
1980 png_create_colormap_entry(display
, i
, i
, i
, i
, 255, P_sRGB
);
1984 #define PNG_GRAY_COLORMAP_ENTRIES 256
1987 make_ga_colormap(png_image_read_control
*display
)
1991 /* Alpha is retained, the output will be a color-map with entries
1992 * selected by six levels of alpha. One transparent entry, 6 gray
1993 * levels for all the intermediate alpha values, leaving 230 entries
1994 * for the opaque grays. The color-map entries are the six values
1995 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1998 * if (alpha > 229) // opaque
2000 * // The 231 entries are selected to make the math below work:
2002 * entry = (231 * gray + 128) >> 8;
2004 * else if (alpha < 26) // transparent
2009 * else // partially opaque
2011 * base = 226 + 6 * PNG_DIV51(alpha);
2012 * entry = PNG_DIV51(gray);
2018 unsigned int gray
= (i
* 256 + 115) / 231;
2019 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
, 255, P_sRGB
);
2022 /* 255 is used here for the component values for consistency with the code
2023 * that undoes premultiplication in pngwrite.c.
2025 png_create_colormap_entry(display
, i
++, 255, 255, 255, 0, P_sRGB
);
2032 png_create_colormap_entry(display
, i
++, g
*51, g
*51, g
*51, a
*51,
2039 #define PNG_GA_COLORMAP_ENTRIES 256
2042 make_rgb_colormap(png_image_read_control
*display
)
2046 /* Build a 6x6x6 opaque RGB cube */
2047 for (i
=r
=0; r
<6; ++r
)
2056 png_create_colormap_entry(display
, i
++, r
*51, g
*51, b
*51, 255,
2064 #define PNG_RGB_COLORMAP_ENTRIES 216
2066 /* Return a palette index to the above palette given three 8-bit sRGB values. */
2067 #define PNG_RGB_INDEX(r,g,b) \
2068 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2071 png_image_read_colormap(png_voidp argument
)
2073 png_image_read_control
*display
=
2074 png_voidcast(png_image_read_control
*, argument
);
2075 const png_imagep image
= display
->image
;
2077 const png_structrp png_ptr
= image
->opaque
->png_ptr
;
2078 const png_uint_32 output_format
= image
->format
;
2079 const int output_encoding
= (output_format
& PNG_FORMAT_FLAG_LINEAR
) != 0 ?
2082 unsigned int cmap_entries
;
2083 unsigned int output_processing
; /* Output processing option */
2084 unsigned int data_encoding
= P_NOTSET
; /* Encoding libpng must produce */
2086 /* Background information; the background color and the index of this color
2087 * in the color-map if it exists (else 256).
2089 unsigned int background_index
= 256;
2090 png_uint_32 back_r
, back_g
, back_b
;
2092 /* Flags to accumulate things that need to be done to the input. */
2093 int expand_tRNS
= 0;
2095 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2096 * very difficult to do, the results look awful, and it is difficult to see
2097 * what possible use it is because the application can't control the
2100 if (((png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0 ||
2101 png_ptr
->num_trans
> 0) /* alpha in input */ &&
2102 ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0) /* no alpha in output */)
2104 if (output_encoding
== P_LINEAR
) /* compose on black */
2105 back_b
= back_g
= back_r
= 0;
2107 else if (display
->background
== NULL
/* no way to remove it */)
2109 "background color must be supplied to remove alpha/transparency");
2111 /* Get a copy of the background color (this avoids repeating the checks
2112 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2117 back_g
= display
->background
->green
;
2118 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) != 0)
2120 back_r
= display
->background
->red
;
2121 back_b
= display
->background
->blue
;
2124 back_b
= back_r
= back_g
;
2128 else if (output_encoding
== P_LINEAR
)
2129 back_b
= back_r
= back_g
= 65535;
2132 back_b
= back_r
= back_g
= 255;
2134 /* Default the input file gamma if required - this is necessary because
2135 * libpng assumes that if no gamma information is present the data is in the
2136 * output format, but the simplified API deduces the gamma from the input
2139 if ((png_ptr
->colorspace
.flags
& PNG_COLORSPACE_HAVE_GAMMA
) == 0)
2141 /* Do this directly, not using the png_colorspace functions, to ensure
2142 * that it happens even if the colorspace is invalid (though probably if
2143 * it is the setting will be ignored) Note that the same thing can be
2144 * achieved at the application interface with png_set_gAMA.
2146 if (png_ptr
->bit_depth
== 16 &&
2147 (image
->flags
& PNG_IMAGE_FLAG_16BIT_sRGB
) == 0)
2148 png_ptr
->colorspace
.gamma
= PNG_GAMMA_LINEAR
;
2151 png_ptr
->colorspace
.gamma
= PNG_GAMMA_sRGB_INVERSE
;
2153 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
2156 /* Decide what to do based on the PNG color type of the input data. The
2157 * utility function png_create_colormap_entry deals with most aspects of the
2158 * output transformations; this code works out how to produce bytes of
2159 * color-map entries from the original format.
2161 switch (png_ptr
->color_type
)
2163 case PNG_COLOR_TYPE_GRAY
:
2164 if (png_ptr
->bit_depth
<= 8)
2166 /* There at most 256 colors in the output, regardless of
2169 unsigned int step
, i
, val
, trans
= 256/*ignore*/, back_alpha
= 0;
2171 cmap_entries
= 1U << png_ptr
->bit_depth
;
2172 if (cmap_entries
> image
->colormap_entries
)
2173 png_error(png_ptr
, "gray[8] color-map: too few entries");
2175 step
= 255 / (cmap_entries
- 1);
2176 output_processing
= PNG_CMAP_NONE
;
2178 /* If there is a tRNS chunk then this either selects a transparent
2179 * value or, if the output has no alpha, the background color.
2181 if (png_ptr
->num_trans
> 0)
2183 trans
= png_ptr
->trans_color
.gray
;
2185 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0)
2186 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2189 /* png_create_colormap_entry just takes an RGBA and writes the
2190 * corresponding color-map entry using the format from 'image',
2191 * including the required conversion to sRGB or linear as
2192 * appropriate. The input values are always either sRGB (if the
2193 * gamma correction flag is 0) or 0..255 scaled file encoded values
2194 * (if the function must gamma correct them).
2196 for (i
=val
=0; i
<cmap_entries
; ++i
, val
+= step
)
2198 /* 'i' is a file value. While this will result in duplicated
2199 * entries for 8-bit non-sRGB encoded files it is necessary to
2200 * have non-gamma corrected values to do tRNS handling.
2203 png_create_colormap_entry(display
, i
, val
, val
, val
, 255,
2204 P_FILE
/*8-bit with file gamma*/);
2206 /* Else this entry is transparent. The colors don't matter if
2207 * there is an alpha channel (back_alpha == 0), but it does no
2208 * harm to pass them in; the values are not set above so this
2211 * NOTE: this preserves the full precision of the application
2212 * supplied background color when it is used.
2215 png_create_colormap_entry(display
, i
, back_r
, back_g
, back_b
,
2216 back_alpha
, output_encoding
);
2219 /* We need libpng to preserve the original encoding. */
2220 data_encoding
= P_FILE
;
2222 /* The rows from libpng, while technically gray values, are now also
2223 * color-map indices; however, they may need to be expanded to 1
2224 * byte per pixel. This is what png_set_packing does (i.e., it
2225 * unpacks the bit values into bytes.)
2227 if (png_ptr
->bit_depth
< 8)
2228 png_set_packing(png_ptr
);
2231 else /* bit depth is 16 */
2233 /* The 16-bit input values can be converted directly to 8-bit gamma
2234 * encoded values; however, if a tRNS chunk is present 257 color-map
2235 * entries are required. This means that the extra entry requires
2236 * special processing; add an alpha channel, sacrifice gray level
2237 * 254 and convert transparent (alpha==0) entries to that.
2239 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2240 * same time to minimize quality loss. If a tRNS chunk is present
2241 * this means libpng must handle it too; otherwise it is impossible
2242 * to do the exact match on the 16-bit value.
2244 * If the output has no alpha channel *and* the background color is
2245 * gray then it is possible to let libpng handle the substitution by
2246 * ensuring that the corresponding gray level matches the background
2249 data_encoding
= P_sRGB
;
2251 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2252 png_error(png_ptr
, "gray[16] color-map: too few entries");
2254 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2256 if (png_ptr
->num_trans
> 0)
2258 unsigned int back_alpha
;
2260 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2265 if (back_r
== back_g
&& back_g
== back_b
)
2267 /* Background is gray; no special processing will be
2271 png_uint_32 gray
= back_g
;
2273 if (output_encoding
== P_LINEAR
)
2275 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2277 /* And make sure the corresponding palette entry
2280 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2281 back_g
, 65535, P_LINEAR
);
2284 /* The background passed to libpng, however, must be the
2287 c
.index
= 0; /*unused*/
2288 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2290 /* NOTE: does this work without expanding tRNS to alpha?
2291 * It should be the color->gray case below apparently
2294 png_set_background_fixed(png_ptr
, &c
,
2295 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2296 0/*gamma: not used*/);
2298 output_processing
= PNG_CMAP_NONE
;
2302 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2307 back_alpha
= output_encoding
== P_LINEAR
? 65535 : 255;
2311 /* output_processing means that the libpng-processed row will be
2312 * 8-bit GA and it has to be processing to single byte color-map
2313 * values. Entry 254 is replaced by either a completely
2314 * transparent entry or by the background color at full
2315 * precision (and the background color is not a simple gray
2316 * level in this case.)
2319 output_processing
= PNG_CMAP_TRANS
;
2320 background_index
= 254;
2322 /* And set (overwrite) color-map entry 254 to the actual
2323 * background color at full precision.
2325 png_create_colormap_entry(display
, 254, back_r
, back_g
, back_b
,
2326 back_alpha
, output_encoding
);
2330 output_processing
= PNG_CMAP_NONE
;
2334 case PNG_COLOR_TYPE_GRAY_ALPHA
:
2335 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2336 * of 65536 combinations. If, however, the alpha channel is to be
2337 * removed there are only 256 possibilities if the background is gray.
2338 * (Otherwise there is a subset of the 65536 possibilities defined by
2339 * the triangle between black, white and the background color.)
2341 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2342 * worry about tRNS matching - tRNS is ignored if there is an alpha
2345 data_encoding
= P_sRGB
;
2347 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2349 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2350 png_error(png_ptr
, "gray+alpha color-map: too few entries");
2352 cmap_entries
= (unsigned int)make_ga_colormap(display
);
2354 background_index
= PNG_CMAP_GA_BACKGROUND
;
2355 output_processing
= PNG_CMAP_GA
;
2358 else /* alpha is removed */
2360 /* Alpha must be removed as the PNG data is processed when the
2361 * background is a color because the G and A channels are
2362 * independent and the vector addition (non-parallel vectors) is a
2365 * This can be reduced to the same algorithm as above by making a
2366 * colormap containing gray levels (for the opaque grays), a
2367 * background entry (for a transparent pixel) and a set of four six
2368 * level color values, one set for each intermediate alpha value.
2369 * See the comments in make_ga_colormap for how this works in the
2370 * per-pixel processing.
2372 * If the background is gray, however, we only need a 256 entry gray
2373 * level color map. It is sufficient to make the entry generated
2374 * for the background color be exactly the color specified.
2376 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0 ||
2377 (back_r
== back_g
&& back_g
== back_b
))
2379 /* Background is gray; no special processing will be required. */
2381 png_uint_32 gray
= back_g
;
2383 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2384 png_error(png_ptr
, "gray-alpha color-map: too few entries");
2386 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2388 if (output_encoding
== P_LINEAR
)
2390 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2392 /* And make sure the corresponding palette entry matches. */
2393 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2394 back_g
, 65535, P_LINEAR
);
2397 /* The background passed to libpng, however, must be the sRGB
2400 c
.index
= 0; /*unused*/
2401 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2403 png_set_background_fixed(png_ptr
, &c
,
2404 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2405 0/*gamma: not used*/);
2407 output_processing
= PNG_CMAP_NONE
;
2414 /* This is the same as png_make_ga_colormap, above, except that
2415 * the entries are all opaque.
2417 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2418 png_error(png_ptr
, "ga-alpha color-map: too few entries");
2423 png_uint_32 gray
= (i
* 256 + 115) / 231;
2424 png_create_colormap_entry(display
, i
++, gray
, gray
, gray
,
2428 /* NOTE: this preserves the full precision of the application
2431 background_index
= i
;
2432 png_create_colormap_entry(display
, i
++, back_r
, back_g
, back_b
,
2434 /* Coverity claims that output_encoding
2435 * cannot be 2 (P_LINEAR) here.
2438 output_encoding
== P_LINEAR
? 65535U : 255U,
2442 /* For non-opaque input composite on the sRGB background - this
2443 * requires inverting the encoding for each component. The input
2444 * is still converted to the sRGB encoding because this is a
2445 * reasonable approximate to the logarithmic curve of human
2446 * visual sensitivity, at least over the narrow range which PNG
2447 * represents. Consequently 'G' is always sRGB encoded, while
2448 * 'A' is linear. We need the linear background colors.
2450 if (output_encoding
== P_sRGB
) /* else already linear */
2452 /* This may produce a value not exactly matching the
2453 * background, but that's ok because these numbers are only
2454 * used when alpha != 0
2456 back_r
= png_sRGB_table
[back_r
];
2457 back_g
= png_sRGB_table
[back_g
];
2458 back_b
= png_sRGB_table
[back_b
];
2465 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2466 * by an 8-bit alpha value (0..255).
2468 png_uint_32 alpha
= 51 * a
;
2469 png_uint_32 back_rx
= (255-alpha
) * back_r
;
2470 png_uint_32 back_gx
= (255-alpha
) * back_g
;
2471 png_uint_32 back_bx
= (255-alpha
) * back_b
;
2475 png_uint_32 gray
= png_sRGB_table
[g
*51] * alpha
;
2477 png_create_colormap_entry(display
, i
++,
2478 PNG_sRGB_FROM_LINEAR(gray
+ back_rx
),
2479 PNG_sRGB_FROM_LINEAR(gray
+ back_gx
),
2480 PNG_sRGB_FROM_LINEAR(gray
+ back_bx
), 255, P_sRGB
);
2485 output_processing
= PNG_CMAP_GA
;
2490 case PNG_COLOR_TYPE_RGB
:
2491 case PNG_COLOR_TYPE_RGB_ALPHA
:
2492 /* Exclude the case where the output is gray; we can always handle this
2493 * with the cases above.
2495 if ((output_format
& PNG_FORMAT_FLAG_COLOR
) == 0)
2497 /* The color-map will be grayscale, so we may as well convert the
2498 * input RGB values to a simple grayscale and use the grayscale
2501 * NOTE: calling this apparently damages the recognition of the
2502 * transparent color in background color handling; call
2503 * png_set_tRNS_to_alpha before png_set_background_fixed.
2505 png_set_rgb_to_gray_fixed(png_ptr
, PNG_ERROR_ACTION_NONE
, -1,
2507 data_encoding
= P_sRGB
;
2509 /* The output will now be one or two 8-bit gray or gray+alpha
2510 * channels. The more complex case arises when the input has alpha.
2512 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2513 png_ptr
->num_trans
> 0) &&
2514 (output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2516 /* Both input and output have an alpha channel, so no background
2517 * processing is required; just map the GA bytes to the right
2522 if (PNG_GA_COLORMAP_ENTRIES
> image
->colormap_entries
)
2523 png_error(png_ptr
, "rgb[ga] color-map: too few entries");
2525 cmap_entries
= (unsigned int)make_ga_colormap(display
);
2526 background_index
= PNG_CMAP_GA_BACKGROUND
;
2527 output_processing
= PNG_CMAP_GA
;
2532 /* Either the input or the output has no alpha channel, so there
2533 * will be no non-opaque pixels in the color-map; it will just be
2536 if (PNG_GRAY_COLORMAP_ENTRIES
> image
->colormap_entries
)
2537 png_error(png_ptr
, "rgb[gray] color-map: too few entries");
2539 /* Ideally this code would use libpng to do the gamma correction,
2540 * but if an input alpha channel is to be removed we will hit the
2541 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2542 * correction bug). Fix this by dropping the gamma correction in
2543 * this case and doing it in the palette; this will result in
2544 * duplicate palette entries, but that's better than the
2545 * alternative of double gamma correction.
2547 if ((png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2548 png_ptr
->num_trans
> 0) &&
2549 png_gamma_not_sRGB(png_ptr
->colorspace
.gamma
) != 0)
2551 cmap_entries
= (unsigned int)make_gray_file_colormap(display
);
2552 data_encoding
= P_FILE
;
2556 cmap_entries
= (unsigned int)make_gray_colormap(display
);
2558 /* But if the input has alpha or transparency it must be removed
2560 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2561 png_ptr
->num_trans
> 0)
2564 png_uint_32 gray
= back_g
;
2566 /* We need to ensure that the application background exists in
2567 * the colormap and that completely transparent pixels map to
2568 * it. Achieve this simply by ensuring that the entry
2569 * selected for the background really is the background color.
2571 if (data_encoding
== P_FILE
) /* from the fixup above */
2573 /* The app supplied a gray which is in output_encoding, we
2574 * need to convert it to a value of the input (P_FILE)
2575 * encoding then set this palette entry to the required
2578 if (output_encoding
== P_sRGB
)
2579 gray
= png_sRGB_table
[gray
]; /* now P_LINEAR */
2581 gray
= PNG_DIV257(png_gamma_16bit_correct(gray
,
2582 png_ptr
->colorspace
.gamma
)); /* now P_FILE */
2584 /* And make sure the corresponding palette entry contains
2585 * exactly the required sRGB value.
2587 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2588 back_g
, 0/*unused*/, output_encoding
);
2591 else if (output_encoding
== P_LINEAR
)
2593 gray
= PNG_sRGB_FROM_LINEAR(gray
* 255);
2595 /* And make sure the corresponding palette entry matches.
2597 png_create_colormap_entry(display
, gray
, back_g
, back_g
,
2598 back_g
, 0/*unused*/, P_LINEAR
);
2601 /* The background passed to libpng, however, must be the
2602 * output (normally sRGB) value.
2604 c
.index
= 0; /*unused*/
2605 c
.gray
= c
.red
= c
.green
= c
.blue
= (png_uint_16
)gray
;
2607 /* NOTE: the following is apparently a bug in libpng. Without
2608 * it the transparent color recognition in
2609 * png_set_background_fixed seems to go wrong.
2612 png_set_background_fixed(png_ptr
, &c
,
2613 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2614 0/*gamma: not used*/);
2617 output_processing
= PNG_CMAP_NONE
;
2621 else /* output is color */
2623 /* We could use png_quantize here so long as there is no transparent
2624 * color or alpha; png_quantize ignores alpha. Easier overall just
2625 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2626 * Consequently we always want libpng to produce sRGB data.
2628 data_encoding
= P_sRGB
;
2630 /* Is there any transparency or alpha? */
2631 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2632 png_ptr
->num_trans
> 0)
2634 /* Is there alpha in the output too? If so all four channels are
2635 * processed into a special RGB cube with alpha support.
2637 if ((output_format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2641 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2642 png_error(png_ptr
, "rgb+alpha color-map: too few entries");
2644 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2646 /* Add a transparent entry. */
2647 png_create_colormap_entry(display
, cmap_entries
, 255, 255,
2650 /* This is stored as the background index for the processing
2653 background_index
= cmap_entries
++;
2655 /* Add 27 r,g,b entries each with alpha 0.5. */
2656 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2660 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2664 /* This generates components with the values 0, 127 and
2667 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2668 png_create_colormap_entry(display
, cmap_entries
++,
2669 r
, g
, b
, 128, P_sRGB
);
2674 output_processing
= PNG_CMAP_RGB_ALPHA
;
2679 /* Alpha/transparency must be removed. The background must
2680 * exist in the color map (achieved by setting adding it after
2681 * the 666 color-map). If the standard processing code will
2682 * pick up this entry automatically that's all that is
2683 * required; libpng can be called to do the background
2686 unsigned int sample_size
=
2687 PNG_IMAGE_SAMPLE_SIZE(output_format
);
2688 png_uint_32 r
, g
, b
; /* sRGB background */
2690 if (PNG_RGB_COLORMAP_ENTRIES
+1+27 > image
->colormap_entries
)
2691 png_error(png_ptr
, "rgb-alpha color-map: too few entries");
2693 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2695 png_create_colormap_entry(display
, cmap_entries
, back_r
,
2696 back_g
, back_b
, 0/*unused*/, output_encoding
);
2698 if (output_encoding
== P_LINEAR
)
2700 r
= PNG_sRGB_FROM_LINEAR(back_r
* 255);
2701 g
= PNG_sRGB_FROM_LINEAR(back_g
* 255);
2702 b
= PNG_sRGB_FROM_LINEAR(back_b
* 255);
2712 /* Compare the newly-created color-map entry with the one the
2713 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2714 * match, add the new one and set this as the background
2717 if (memcmp((png_const_bytep
)display
->colormap
+
2718 sample_size
* cmap_entries
,
2719 (png_const_bytep
)display
->colormap
+
2720 sample_size
* PNG_RGB_INDEX(r
,g
,b
),
2723 /* The background color must be added. */
2724 background_index
= cmap_entries
++;
2726 /* Add 27 r,g,b entries each with created by composing with
2727 * the background at alpha 0.5.
2729 for (r
=0; r
<256; r
= (r
<< 1) | 0x7f)
2731 for (g
=0; g
<256; g
= (g
<< 1) | 0x7f)
2733 /* This generates components with the values 0, 127
2736 for (b
=0; b
<256; b
= (b
<< 1) | 0x7f)
2737 png_create_colormap_entry(display
, cmap_entries
++,
2738 png_colormap_compose(display
, r
, P_sRGB
, 128,
2739 back_r
, output_encoding
),
2740 png_colormap_compose(display
, g
, P_sRGB
, 128,
2741 back_g
, output_encoding
),
2742 png_colormap_compose(display
, b
, P_sRGB
, 128,
2743 back_b
, output_encoding
),
2744 0/*unused*/, output_encoding
);
2749 output_processing
= PNG_CMAP_RGB_ALPHA
;
2752 else /* background color is in the standard color-map */
2756 c
.index
= 0; /*unused*/
2757 c
.red
= (png_uint_16
)back_r
;
2758 c
.gray
= c
.green
= (png_uint_16
)back_g
;
2759 c
.blue
= (png_uint_16
)back_b
;
2761 png_set_background_fixed(png_ptr
, &c
,
2762 PNG_BACKGROUND_GAMMA_SCREEN
, 0/*need_expand*/,
2763 0/*gamma: not used*/);
2765 output_processing
= PNG_CMAP_RGB
;
2770 else /* no alpha or transparency in the input */
2772 /* Alpha in the output is irrelevant, simply map the opaque input
2773 * pixels to the 6x6x6 color-map.
2775 if (PNG_RGB_COLORMAP_ENTRIES
> image
->colormap_entries
)
2776 png_error(png_ptr
, "rgb color-map: too few entries");
2778 cmap_entries
= (unsigned int)make_rgb_colormap(display
);
2779 output_processing
= PNG_CMAP_RGB
;
2784 case PNG_COLOR_TYPE_PALETTE
:
2785 /* It's already got a color-map. It may be necessary to eliminate the
2786 * tRNS entries though.
2789 unsigned int num_trans
= png_ptr
->num_trans
;
2790 png_const_bytep trans
= num_trans
> 0 ? png_ptr
->trans_alpha
: NULL
;
2791 png_const_colorp colormap
= png_ptr
->palette
;
2792 const int do_background
= trans
!= NULL
&&
2793 (output_format
& PNG_FORMAT_FLAG_ALPHA
) == 0;
2800 output_processing
= PNG_CMAP_NONE
;
2801 data_encoding
= P_FILE
; /* Don't change from color-map indices */
2802 cmap_entries
= (unsigned int)png_ptr
->num_palette
;
2803 if (cmap_entries
> 256)
2806 if (cmap_entries
> (unsigned int)image
->colormap_entries
)
2807 png_error(png_ptr
, "palette color-map: too few entries");
2809 for (i
=0; i
< cmap_entries
; ++i
)
2811 if (do_background
!= 0 && i
< num_trans
&& trans
[i
] < 255)
2814 png_create_colormap_entry(display
, i
, back_r
, back_g
,
2815 back_b
, 0, output_encoding
);
2819 /* Must compose the PNG file color in the color-map entry
2820 * on the sRGB color in 'back'.
2822 png_create_colormap_entry(display
, i
,
2823 png_colormap_compose(display
, colormap
[i
].red
,
2824 P_FILE
, trans
[i
], back_r
, output_encoding
),
2825 png_colormap_compose(display
, colormap
[i
].green
,
2826 P_FILE
, trans
[i
], back_g
, output_encoding
),
2827 png_colormap_compose(display
, colormap
[i
].blue
,
2828 P_FILE
, trans
[i
], back_b
, output_encoding
),
2829 output_encoding
== P_LINEAR
? trans
[i
] * 257U :
2836 png_create_colormap_entry(display
, i
, colormap
[i
].red
,
2837 colormap
[i
].green
, colormap
[i
].blue
,
2838 i
< num_trans
? trans
[i
] : 255U, P_FILE
/*8-bit*/);
2841 /* The PNG data may have indices packed in fewer than 8 bits, it
2842 * must be expanded if so.
2844 if (png_ptr
->bit_depth
< 8)
2845 png_set_packing(png_ptr
);
2850 png_error(png_ptr
, "invalid PNG color type");
2854 /* Now deal with the output processing */
2855 if (expand_tRNS
!= 0 && png_ptr
->num_trans
> 0 &&
2856 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) == 0)
2857 png_set_tRNS_to_alpha(png_ptr
);
2859 switch (data_encoding
)
2862 /* Change to 8-bit sRGB */
2863 png_set_alpha_mode_fixed(png_ptr
, PNG_ALPHA_PNG
, PNG_GAMMA_sRGB
);
2867 if (png_ptr
->bit_depth
> 8)
2868 png_set_scale_16(png_ptr
);
2873 png_error(png_ptr
, "bad data option (internal error)");
2877 if (cmap_entries
> 256 || cmap_entries
> image
->colormap_entries
)
2878 png_error(png_ptr
, "color map overflow (BAD internal error)");
2880 image
->colormap_entries
= cmap_entries
;
2882 /* Double check using the recorded background index */
2883 switch (output_processing
)
2886 if (background_index
!= PNG_CMAP_NONE_BACKGROUND
)
2887 goto bad_background
;
2891 if (background_index
!= PNG_CMAP_GA_BACKGROUND
)
2892 goto bad_background
;
2895 case PNG_CMAP_TRANS
:
2896 if (background_index
>= cmap_entries
||
2897 background_index
!= PNG_CMAP_TRANS_BACKGROUND
)
2898 goto bad_background
;
2902 if (background_index
!= PNG_CMAP_RGB_BACKGROUND
)
2903 goto bad_background
;
2906 case PNG_CMAP_RGB_ALPHA
:
2907 if (background_index
!= PNG_CMAP_RGB_ALPHA_BACKGROUND
)
2908 goto bad_background
;
2912 png_error(png_ptr
, "bad processing option (internal error)");
2915 png_error(png_ptr
, "bad background index (internal error)");
2918 display
->colormap_processing
= (int)output_processing
;
2923 /* The final part of the color-map read called from png_image_finish_read. */
2925 png_image_read_and_map(png_voidp argument
)
2927 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
2929 png_imagep image
= display
->image
;
2930 png_structrp png_ptr
= image
->opaque
->png_ptr
;
2933 /* Called when the libpng data must be transformed into the color-mapped
2934 * form. There is a local row buffer in display->local and this routine must
2935 * do the interlace handling.
2937 switch (png_ptr
->interlaced
)
2939 case PNG_INTERLACE_NONE
:
2943 case PNG_INTERLACE_ADAM7
:
2944 passes
= PNG_INTERLACE_ADAM7_PASSES
;
2948 png_error(png_ptr
, "unknown interlace type");
2952 png_uint_32 height
= image
->height
;
2953 png_uint_32 width
= image
->width
;
2954 int proc
= display
->colormap_processing
;
2955 png_bytep first_row
= png_voidcast(png_bytep
, display
->first_row
);
2956 ptrdiff_t step_row
= display
->row_bytes
;
2959 for (pass
= 0; pass
< passes
; ++pass
)
2961 unsigned int startx
, stepx
, stepy
;
2964 if (png_ptr
->interlaced
== PNG_INTERLACE_ADAM7
)
2966 /* The row may be empty for a short image: */
2967 if (PNG_PASS_COLS(width
, pass
) == 0)
2970 startx
= PNG_PASS_START_COL(pass
);
2971 stepx
= PNG_PASS_COL_OFFSET(pass
);
2972 y
= PNG_PASS_START_ROW(pass
);
2973 stepy
= PNG_PASS_ROW_OFFSET(pass
);
2983 for (; y
<height
; y
+= stepy
)
2985 png_bytep inrow
= png_voidcast(png_bytep
, display
->local_row
);
2986 png_bytep outrow
= first_row
+ y
* step_row
;
2987 png_const_bytep end_row
= outrow
+ width
;
2989 /* Read read the libpng data into the temporary buffer. */
2990 png_read_row(png_ptr
, inrow
, NULL
);
2992 /* Now process the row according to the processing option, note
2993 * that the caller verifies that the format of the libpng output
2994 * data is as required.
3000 for (; outrow
< end_row
; outrow
+= stepx
)
3002 /* The data is always in the PNG order */
3003 unsigned int gray
= *inrow
++;
3004 unsigned int alpha
= *inrow
++;
3007 /* NOTE: this code is copied as a comment in
3008 * make_ga_colormap above. Please update the
3009 * comment if you change this code!
3011 if (alpha
> 229) /* opaque */
3013 entry
= (231 * gray
+ 128) >> 8;
3015 else if (alpha
< 26) /* transparent */
3019 else /* partially opaque */
3021 entry
= 226 + 6 * PNG_DIV51(alpha
) + PNG_DIV51(gray
);
3024 *outrow
= (png_byte
)entry
;
3028 case PNG_CMAP_TRANS
:
3029 for (; outrow
< end_row
; outrow
+= stepx
)
3031 png_byte gray
= *inrow
++;
3032 png_byte alpha
= *inrow
++;
3035 *outrow
= PNG_CMAP_TRANS_BACKGROUND
;
3037 else if (gray
!= PNG_CMAP_TRANS_BACKGROUND
)
3041 *outrow
= (png_byte
)(PNG_CMAP_TRANS_BACKGROUND
+1);
3046 for (; outrow
< end_row
; outrow
+= stepx
)
3048 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1], inrow
[2]);
3053 case PNG_CMAP_RGB_ALPHA
:
3054 for (; outrow
< end_row
; outrow
+= stepx
)
3056 unsigned int alpha
= inrow
[3];
3058 /* Because the alpha entries only hold alpha==0.5 values
3059 * split the processing at alpha==0.25 (64) and 0.75
3064 *outrow
= PNG_RGB_INDEX(inrow
[0], inrow
[1],
3067 else if (alpha
< 64)
3068 *outrow
= PNG_CMAP_RGB_ALPHA_BACKGROUND
;
3072 /* Likewise there are three entries for each of r, g
3073 * and b. We could select the entry by popcount on
3074 * the top two bits on those architectures that
3075 * support it, this is what the code below does,
3078 unsigned int back_i
= PNG_CMAP_RGB_ALPHA_BACKGROUND
+1;
3080 /* Here are how the values map:
3086 * So, as above with the explicit alpha checks, the
3087 * breakpoints are at 64 and 196.
3089 if (inrow
[0] & 0x80) back_i
+= 9; /* red */
3090 if (inrow
[0] & 0x40) back_i
+= 9;
3091 if (inrow
[0] & 0x80) back_i
+= 3; /* green */
3092 if (inrow
[0] & 0x40) back_i
+= 3;
3093 if (inrow
[0] & 0x80) back_i
+= 1; /* blue */
3094 if (inrow
[0] & 0x40) back_i
+= 1;
3096 *outrow
= (png_byte
)back_i
;
3114 png_image_read_colormapped(png_voidp argument
)
3116 png_image_read_control
*display
= png_voidcast(png_image_read_control
*,
3118 png_imagep image
= display
->image
;
3119 png_controlp control
= image
->opaque
;
3120 png_structrp png_ptr
= control
->png_ptr
;
3121 png_inforp info_ptr
= control
->info_ptr
;
3123 int passes
= 0; /* As a flag */
3125 PNG_SKIP_CHUNKS(png_ptr
);
3127 /* Update the 'info' structure and make sure the result is as required; first
3128 * make sure to turn on the interlace handling if it will be required
3129 * (because it can't be turned on *after* the call to png_read_update_info!)
3131 if (display
->colormap_processing
== PNG_CMAP_NONE
)
3132 passes
= png_set_interlace_handling(png_ptr
);
3134 png_read_update_info(png_ptr
, info_ptr
);
3136 /* The expected output can be deduced from the colormap_processing option. */
3137 switch (display
->colormap_processing
)
3140 /* Output must be one channel and one byte per pixel, the output
3141 * encoding can be anything.
3143 if ((info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
||
3144 info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
) &&
3145 info_ptr
->bit_depth
== 8)
3150 case PNG_CMAP_TRANS
:
3152 /* Output must be two channels and the 'G' one must be sRGB, the latter
3153 * can be checked with an exact number because it should have been set
3154 * to this number above!
3156 if (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
&&
3157 info_ptr
->bit_depth
== 8 &&
3158 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3159 image
->colormap_entries
== 256)
3165 /* Output must be 8-bit sRGB encoded RGB */
3166 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
&&
3167 info_ptr
->bit_depth
== 8 &&
3168 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3169 image
->colormap_entries
== 216)
3174 case PNG_CMAP_RGB_ALPHA
:
3175 /* Output must be 8-bit sRGB encoded RGBA */
3176 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
3177 info_ptr
->bit_depth
== 8 &&
3178 png_ptr
->screen_gamma
== PNG_GAMMA_sRGB
&&
3179 image
->colormap_entries
== 244 /* 216 + 1 + 27 */)
3182 /* goto bad_output; */
3187 png_error(png_ptr
, "bad color-map processing (internal error)");
3190 /* Now read the rows. Do this here if it is possible to read directly into
3191 * the output buffer, otherwise allocate a local row buffer of the maximum
3192 * size libpng requires and call the relevant processing routine safely.
3195 png_voidp first_row
= display
->buffer
;
3196 ptrdiff_t row_bytes
= display
->row_stride
;
3198 /* The following expression is designed to work correctly whether it gives
3199 * a signed or an unsigned result.
3203 char *ptr
= png_voidcast(char*, first_row
);
3204 ptr
+= (image
->height
-1) * (-row_bytes
);
3205 first_row
= png_voidcast(png_voidp
, ptr
);
3208 display
->first_row
= first_row
;
3209 display
->row_bytes
= row_bytes
;
3215 png_voidp row
= png_malloc(png_ptr
, png_get_rowbytes(png_ptr
, info_ptr
));
3217 display
->local_row
= row
;
3218 result
= png_safe_execute(image
, png_image_read_and_map
, display
);
3219 display
->local_row
= NULL
;
3220 png_free(png_ptr
, row
);
3227 png_alloc_size_t row_bytes
= (png_alloc_size_t
)display
->row_bytes
;
3229 while (--passes
>= 0)
3231 png_uint_32 y
= image
->height
;
3232 png_bytep row
= png_voidcast(png_bytep
, display
->first_row
);
3236 png_read_row(png_ptr
, row
, NULL
);
3245 /* Just the row reading part of png_image_read. */