VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.43/pngread.c@ 105468

Last change on this file since 105468 was 103316, checked in by vboxsync, 10 months ago

libpng-1.6.42: Applied and adjusted our libpng changes to 1.6.42. bugref:8515

  • Property svn:eol-style set to native
File size: 138.9 KB
Line 
1
2/* pngread.c - read a PNG file
3 *
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.
8 *
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
12 *
13 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
16
17#include "pngpriv.h"
18#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
19# include <errno.h>
20#endif
21
22#ifdef PNG_READ_SUPPORTED
23
24/* Create a PNG structure for reading, and allocate any memory needed. */
25PNG_FUNCTION(png_structp,PNGAPI
26png_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)
28{
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);
32#else
33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
34 warn_fn, NULL, NULL, NULL);
35}
36
37/* Alternate create PNG structure for reading, and allocate any memory
38 * needed.
39 */
40PNG_FUNCTION(png_structp,PNGAPI
41png_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)
44{
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);
47#endif /* USER_MEM */
48
49 if (png_ptr != NULL)
50 {
51 png_ptr->mode = PNG_IS_READ_STRUCT;
52
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.)
55 */
56# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
58# endif
59
60# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
62
63 /* In stable builds only warn if an application error can be completely
64 * handled.
65 */
66# if PNG_RELEASE_BUILD
67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
68# endif
69# endif
70
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
73 * required.
74 */
75 png_set_read_fn(png_ptr, NULL, NULL);
76 }
77
78 return png_ptr;
79}
80
81
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.
90 */
91void PNGAPI
92png_read_info(png_structrp png_ptr, png_inforp info_ptr)
93{
94#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
95 int keep;
96#endif
97
98 png_debug(1, "in png_read_info");
99
100 if (png_ptr == NULL || info_ptr == NULL)
101 return;
102
103 /* Read and check the PNG file signature. */
104 png_read_sig(png_ptr, info_ptr);
105
106 for (;;)
107 {
108 png_uint_32 length = png_read_chunk_header(png_ptr);
109 png_uint_32 chunk_name = png_ptr->chunk_name;
110
111 /* IDAT logic needs to happen here to simplify getting the two flags
112 * right.
113 */
114 if (chunk_name == png_IDAT)
115 {
116 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
118
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");
122
123 else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
125
126 png_ptr->mode |= PNG_HAVE_IDAT;
127 }
128
129 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
130 {
131 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
132 png_ptr->mode |= PNG_AFTER_IDAT;
133 }
134
135 /* This should be a binary subdivision search or a hash for
136 * matching the chunk name rather than a linear search.
137 */
138 if (chunk_name == png_IHDR)
139 png_handle_IHDR(png_ptr, info_ptr, length);
140
141 else if (chunk_name == png_IEND)
142 png_handle_IEND(png_ptr, info_ptr, length);
143
144#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
145 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
146 {
147 png_handle_unknown(png_ptr, info_ptr, length, keep);
148
149 if (chunk_name == png_PLTE)
150 png_ptr->mode |= PNG_HAVE_PLTE;
151
152 else if (chunk_name == png_IDAT)
153 {
154 png_ptr->idat_size = 0; /* It has been consumed */
155 break;
156 }
157 }
158#endif
159 else if (chunk_name == png_PLTE)
160 png_handle_PLTE(png_ptr, info_ptr, length);
161
162 else if (chunk_name == png_IDAT)
163 {
164 png_ptr->idat_size = length;
165 break;
166 }
167
168#ifdef PNG_READ_bKGD_SUPPORTED
169 else if (chunk_name == png_bKGD)
170 png_handle_bKGD(png_ptr, info_ptr, length);
171#endif
172
173#ifdef PNG_READ_cHRM_SUPPORTED
174 else if (chunk_name == png_cHRM)
175 png_handle_cHRM(png_ptr, info_ptr, length);
176#endif
177
178#ifdef PNG_READ_eXIf_SUPPORTED
179 else if (chunk_name == png_eXIf)
180 png_handle_eXIf(png_ptr, info_ptr, length);
181#endif
182
183#ifdef PNG_READ_gAMA_SUPPORTED
184 else if (chunk_name == png_gAMA)
185 png_handle_gAMA(png_ptr, info_ptr, length);
186#endif
187
188#ifdef PNG_READ_hIST_SUPPORTED
189 else if (chunk_name == png_hIST)
190 png_handle_hIST(png_ptr, info_ptr, length);
191#endif
192
193#ifdef PNG_READ_oFFs_SUPPORTED
194 else if (chunk_name == png_oFFs)
195 png_handle_oFFs(png_ptr, info_ptr, length);
196#endif
197
198#ifdef PNG_READ_pCAL_SUPPORTED
199 else if (chunk_name == png_pCAL)
200 png_handle_pCAL(png_ptr, info_ptr, length);
201#endif
202
203#ifdef PNG_READ_sCAL_SUPPORTED
204 else if (chunk_name == png_sCAL)
205 png_handle_sCAL(png_ptr, info_ptr, length);
206#endif
207
208#ifdef PNG_READ_pHYs_SUPPORTED
209 else if (chunk_name == png_pHYs)
210 png_handle_pHYs(png_ptr, info_ptr, length);
211#endif
212
213#ifdef PNG_READ_sBIT_SUPPORTED
214 else if (chunk_name == png_sBIT)
215 png_handle_sBIT(png_ptr, info_ptr, length);
216#endif
217
218#ifdef PNG_READ_sRGB_SUPPORTED
219 else if (chunk_name == png_sRGB)
220 png_handle_sRGB(png_ptr, info_ptr, length);
221#endif
222
223#ifdef PNG_READ_iCCP_SUPPORTED
224 else if (chunk_name == png_iCCP)
225 png_handle_iCCP(png_ptr, info_ptr, length);
226#endif
227
228#ifdef PNG_READ_sPLT_SUPPORTED
229 else if (chunk_name == png_sPLT)
230 png_handle_sPLT(png_ptr, info_ptr, length);
231#endif
232
233#ifdef PNG_READ_tEXt_SUPPORTED
234 else if (chunk_name == png_tEXt)
235 png_handle_tEXt(png_ptr, info_ptr, length);
236#endif
237
238#ifdef PNG_READ_tIME_SUPPORTED
239 else if (chunk_name == png_tIME)
240 png_handle_tIME(png_ptr, info_ptr, length);
241#endif
242
243#ifdef PNG_READ_tRNS_SUPPORTED
244 else if (chunk_name == png_tRNS)
245 png_handle_tRNS(png_ptr, info_ptr, length);
246#endif
247
248#ifdef PNG_READ_zTXt_SUPPORTED
249 else if (chunk_name == png_zTXt)
250 png_handle_zTXt(png_ptr, info_ptr, length);
251#endif
252
253#ifdef PNG_READ_iTXt_SUPPORTED
254 else if (chunk_name == png_iTXt)
255 png_handle_iTXt(png_ptr, info_ptr, length);
256#endif
257
258 else
259 png_handle_unknown(png_ptr, info_ptr, length,
260 PNG_HANDLE_CHUNK_AS_DEFAULT);
261 }
262}
263#endif /* SEQUENTIAL_READ */
264
265/* Optional call to update the users info_ptr structure */
266void PNGAPI
267png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
268{
269 png_debug(1, "in png_read_update_info");
270
271 if (png_ptr != NULL)
272 {
273 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
274 {
275 png_read_start_row(png_ptr);
276
277# ifdef PNG_READ_TRANSFORMS_SUPPORTED
278 png_read_transform_info(png_ptr, info_ptr);
279# else
280 PNG_UNUSED(info_ptr)
281# endif
282 }
283
284 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
285 else
286 png_app_error(png_ptr,
287 "png_read_update_info/png_start_read_image: duplicate call");
288 }
289}
290
291#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
292/* Initialize palette, background, etc, after transformations
293 * are set, but before any reading takes place. This allows
294 * the user to obtain a gamma-corrected palette, for example.
295 * If the user doesn't call this, we will do it ourselves.
296 */
297void PNGAPI
298png_start_read_image(png_structrp png_ptr)
299{
300 png_debug(1, "in png_start_read_image");
301
302 if (png_ptr != NULL)
303 {
304 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
305 png_read_start_row(png_ptr);
306
307 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
308 else
309 png_app_error(png_ptr,
310 "png_start_read_image/png_read_update_info: duplicate call");
311 }
312}
313#endif /* SEQUENTIAL_READ */
314
315#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
316#ifdef PNG_MNG_FEATURES_SUPPORTED
317/* Undoes intrapixel differencing,
318 * NOTE: this is apparently only supported in the 'sequential' reader.
319 */
320static void
321png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
322{
323 png_debug(1, "in png_do_read_intrapixel");
324
325 if (
326 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
327 {
328 int bytes_per_pixel;
329 png_uint_32 row_width = row_info->width;
330
331 if (row_info->bit_depth == 8)
332 {
333 png_bytep rp;
334 png_uint_32 i;
335
336 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
337 bytes_per_pixel = 3;
338
339 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
340 bytes_per_pixel = 4;
341
342 else
343 return;
344
345 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
346 {
347 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
348 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
349 }
350 }
351 else if (row_info->bit_depth == 16)
352 {
353 png_bytep rp;
354 png_uint_32 i;
355
356 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
357 bytes_per_pixel = 6;
358
359 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
360 bytes_per_pixel = 8;
361
362 else
363 return;
364
365 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
366 {
367 png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
368 png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
369 png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
370 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
371 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
372 *(rp ) = (png_byte)((red >> 8) & 0xff);
373 *(rp + 1) = (png_byte)(red & 0xff);
374 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
375 *(rp + 5) = (png_byte)(blue & 0xff);
376 }
377 }
378 }
379}
380#endif /* MNG_FEATURES */
381
382void PNGAPI
383png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
384{
385 png_row_info row_info;
386
387 if (png_ptr == NULL)
388 return;
389
390 png_debug2(1, "in png_read_row (row %lu, pass %d)",
391 (unsigned long)png_ptr->row_number, png_ptr->pass);
392
393 /* png_read_start_row sets the information (in particular iwidth) for this
394 * interlace pass.
395 */
396 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
397 png_read_start_row(png_ptr);
398
399 /* 1.5.6: row_info moved out of png_struct to a local here. */
400 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
401 row_info.color_type = png_ptr->color_type;
402 row_info.bit_depth = png_ptr->bit_depth;
403 row_info.channels = png_ptr->channels;
404 row_info.pixel_depth = png_ptr->pixel_depth;
405 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
406
407#ifdef PNG_WARNINGS_SUPPORTED
408 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
409 {
410 /* Check for transforms that have been set but were defined out */
411#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
412 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
413 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
414#endif
415
416#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
417 if ((png_ptr->transformations & PNG_FILLER) != 0)
418 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
419#endif
420
421#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
422 !defined(PNG_READ_PACKSWAP_SUPPORTED)
423 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
424 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
425#endif
426
427#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
428 if ((png_ptr->transformations & PNG_PACK) != 0)
429 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
430#endif
431
432#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
433 if ((png_ptr->transformations & PNG_SHIFT) != 0)
434 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
435#endif
436
437#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
438 if ((png_ptr->transformations & PNG_BGR) != 0)
439 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
440#endif
441
442#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
443 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
444 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
445#endif
446 }
447#endif /* WARNINGS */
448
449#ifdef PNG_READ_INTERLACING_SUPPORTED
450 /* If interlaced and we do not need a new row, combine row and return.
451 * Notice that the pixels we have from previous rows have been transformed
452 * already; we can only combine like with like (transformed or
453 * untransformed) and, because of the libpng API for interlaced images, this
454 * means we must transform before de-interlacing.
455 */
456 if (png_ptr->interlaced != 0 &&
457 (png_ptr->transformations & PNG_INTERLACE) != 0)
458 {
459 switch (png_ptr->pass)
460 {
461 case 0:
462 if (png_ptr->row_number & 0x07)
463 {
464 if (dsp_row != NULL)
465 png_combine_row(png_ptr, dsp_row, 1/*display*/);
466 png_read_finish_row(png_ptr);
467 return;
468 }
469 break;
470
471 case 1:
472 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
473 {
474 if (dsp_row != NULL)
475 png_combine_row(png_ptr, dsp_row, 1/*display*/);
476
477 png_read_finish_row(png_ptr);
478 return;
479 }
480 break;
481
482 case 2:
483 if ((png_ptr->row_number & 0x07) != 4)
484 {
485 if (dsp_row != NULL && (png_ptr->row_number & 4))
486 png_combine_row(png_ptr, dsp_row, 1/*display*/);
487
488 png_read_finish_row(png_ptr);
489 return;
490 }
491 break;
492
493 case 3:
494 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
495 {
496 if (dsp_row != NULL)
497 png_combine_row(png_ptr, dsp_row, 1/*display*/);
498
499 png_read_finish_row(png_ptr);
500 return;
501 }
502 break;
503
504 case 4:
505 if ((png_ptr->row_number & 3) != 2)
506 {
507 if (dsp_row != NULL && (png_ptr->row_number & 2))
508 png_combine_row(png_ptr, dsp_row, 1/*display*/);
509
510 png_read_finish_row(png_ptr);
511 return;
512 }
513 break;
514
515 case 5:
516 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
517 {
518 if (dsp_row != NULL)
519 png_combine_row(png_ptr, dsp_row, 1/*display*/);
520
521 png_read_finish_row(png_ptr);
522 return;
523 }
524 break;
525
526 default:
527 case 6:
528 if ((png_ptr->row_number & 1) == 0)
529 {
530 png_read_finish_row(png_ptr);
531 return;
532 }
533 break;
534 }
535 }
536#endif
537
538 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
539 png_error(png_ptr, "Invalid attempt to read row data");
540
541 /* Fill the row with IDAT data: */
542 png_ptr->row_buf[0]=255; /* to force error if no data was found */
543 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
544
545 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
546 {
547 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
548 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
549 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
550 else
551 png_error(png_ptr, "bad adaptive filter value");
552 }
553
554 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
555 * 1.5.6, while the buffer really is this big in current versions of libpng
556 * it may not be in the future, so this was changed just to copy the
557 * interlaced count:
558 */
559 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
560
561#ifdef PNG_MNG_FEATURES_SUPPORTED
562 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
563 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
564 {
565 /* Intrapixel differencing */
566 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
567 }
568#endif
569
570#ifdef PNG_READ_TRANSFORMS_SUPPORTED
571 if (png_ptr->transformations || png_ptr->num_palette_max >= 0)
572 png_do_read_transformations(png_ptr, &row_info);
573#endif
574
575 /* The transformed pixel depth should match the depth now in row_info. */
576 if (png_ptr->transformed_pixel_depth == 0)
577 {
578 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
579 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
580 png_error(png_ptr, "sequential row overflow");
581 }
582
583 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
584 png_error(png_ptr, "internal sequential row size calculation error");
585
586#ifdef PNG_READ_INTERLACING_SUPPORTED
587 /* Expand interlaced rows to full size */
588 if (png_ptr->interlaced != 0 &&
589 (png_ptr->transformations & PNG_INTERLACE) != 0)
590 {
591 if (png_ptr->pass < 6)
592 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
593 png_ptr->transformations);
594
595 if (dsp_row != NULL)
596 png_combine_row(png_ptr, dsp_row, 1/*display*/);
597
598 if (row != NULL)
599 png_combine_row(png_ptr, row, 0/*row*/);
600 }
601
602 else
603#endif
604 {
605 if (row != NULL)
606 png_combine_row(png_ptr, row, -1/*ignored*/);
607
608 if (dsp_row != NULL)
609 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
610 }
611 png_read_finish_row(png_ptr);
612
613 if (png_ptr->read_row_fn != NULL)
614 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
615
616}
617#endif /* SEQUENTIAL_READ */
618
619#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
620/* Read one or more rows of image data. If the image is interlaced,
621 * and png_set_interlace_handling() has been called, the rows need to
622 * contain the contents of the rows from the previous pass. If the
623 * image has alpha or transparency, and png_handle_alpha()[*] has been
624 * called, the rows contents must be initialized to the contents of the
625 * screen.
626 *
627 * "row" holds the actual image, and pixels are placed in it
628 * as they arrive. If the image is displayed after each pass, it will
629 * appear to "sparkle" in. "display_row" can be used to display a
630 * "chunky" progressive image, with finer detail added as it becomes
631 * available. If you do not want this "chunky" display, you may pass
632 * NULL for display_row. If you do not want the sparkle display, and
633 * you have not called png_handle_alpha(), you may pass NULL for rows.
634 * If you have called png_handle_alpha(), and the image has either an
635 * alpha channel or a transparency chunk, you must provide a buffer for
636 * rows. In this case, you do not have to provide a display_row buffer
637 * also, but you may. If the image is not interlaced, or if you have
638 * not called png_set_interlace_handling(), the display_row buffer will
639 * be ignored, so pass NULL to it.
640 *
641 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
642 */
643
644void PNGAPI
645png_read_rows(png_structrp png_ptr, png_bytepp row,
646 png_bytepp display_row, png_uint_32 num_rows)
647{
648 png_uint_32 i;
649 png_bytepp rp;
650 png_bytepp dp;
651
652 png_debug(1, "in png_read_rows");
653
654 if (png_ptr == NULL)
655 return;
656
657 rp = row;
658 dp = display_row;
659 if (rp != NULL && dp != NULL)
660 for (i = 0; i < num_rows; i++)
661 {
662 png_bytep rptr = *rp++;
663 png_bytep dptr = *dp++;
664
665 png_read_row(png_ptr, rptr, dptr);
666 }
667
668 else if (rp != NULL)
669 for (i = 0; i < num_rows; i++)
670 {
671 png_bytep rptr = *rp;
672 png_read_row(png_ptr, rptr, NULL);
673 rp++;
674 }
675
676 else if (dp != NULL)
677 for (i = 0; i < num_rows; i++)
678 {
679 png_bytep dptr = *dp;
680 png_read_row(png_ptr, NULL, dptr);
681 dp++;
682 }
683}
684#endif /* SEQUENTIAL_READ */
685
686#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
687/* Read the entire image. If the image has an alpha channel or a tRNS
688 * chunk, and you have called png_handle_alpha()[*], you will need to
689 * initialize the image to the current image that PNG will be overlaying.
690 * We set the num_rows again here, in case it was incorrectly set in
691 * png_read_start_row() by a call to png_read_update_info() or
692 * png_start_read_image() if png_set_interlace_handling() wasn't called
693 * prior to either of these functions like it should have been. You can
694 * only call this function once. If you desire to have an image for
695 * each pass of a interlaced image, use png_read_rows() instead.
696 *
697 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
698 */
699void PNGAPI
700png_read_image(png_structrp png_ptr, png_bytepp image)
701{
702 png_uint_32 i, image_height;
703 int pass, j;
704 png_bytepp rp;
705
706 png_debug(1, "in png_read_image");
707
708 if (png_ptr == NULL)
709 return;
710
711#ifdef PNG_READ_INTERLACING_SUPPORTED
712 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
713 {
714 pass = png_set_interlace_handling(png_ptr);
715 /* And make sure transforms are initialized. */
716 png_start_read_image(png_ptr);
717 }
718 else
719 {
720 if (png_ptr->interlaced != 0 &&
721 (png_ptr->transformations & PNG_INTERLACE) == 0)
722 {
723 /* Caller called png_start_read_image or png_read_update_info without
724 * first turning on the PNG_INTERLACE transform. We can fix this here,
725 * but the caller should do it!
726 */
727 png_warning(png_ptr, "Interlace handling should be turned on when "
728 "using png_read_image");
729 /* Make sure this is set correctly */
730 png_ptr->num_rows = png_ptr->height;
731 }
732
733 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
734 * the above error case.
735 */
736 pass = png_set_interlace_handling(png_ptr);
737 }
738#else
739 if (png_ptr->interlaced)
740 png_error(png_ptr,
741 "Cannot read interlaced image -- interlace handler disabled");
742
743 pass = 1;
744#endif
745
746 image_height=png_ptr->height;
747
748 for (j = 0; j < pass; j++)
749 {
750 rp = image;
751 for (i = 0; i < image_height; i++)
752 {
753 png_read_row(png_ptr, *rp, NULL);
754 rp++;
755 }
756 }
757}
758#endif /* SEQUENTIAL_READ */
759
760#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
761/* Read the end of the PNG file. Will not read past the end of the
762 * file, will verify the end is accurate, and will read any comments
763 * or time information at the end of the file, if info is not NULL.
764 */
765void PNGAPI
766png_read_end(png_structrp png_ptr, png_inforp info_ptr)
767{
768#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
769 int keep;
770#endif
771
772 png_debug(1, "in png_read_end");
773
774 if (png_ptr == NULL)
775 return;
776
777 /* If png_read_end is called in the middle of reading the rows there may
778 * still be pending IDAT data and an owned zstream. Deal with this here.
779 */
780#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
781 if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
782#endif
783 png_read_finish_IDAT(png_ptr);
784
785#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
786 /* Report invalid palette index; added at libng-1.5.10 */
787 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
788 png_ptr->num_palette_max >= png_ptr->num_palette)
789 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
790#endif
791
792 do
793 {
794 png_uint_32 length = png_read_chunk_header(png_ptr);
795 png_uint_32 chunk_name = png_ptr->chunk_name;
796
797 if (chunk_name != png_IDAT)
798 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
799
800 if (chunk_name == png_IEND)
801 png_handle_IEND(png_ptr, info_ptr, length);
802
803 else if (chunk_name == png_IHDR)
804 png_handle_IHDR(png_ptr, info_ptr, length);
805
806 else if (info_ptr == NULL)
807 png_crc_finish(png_ptr, length);
808
809#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
810 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
811 {
812 if (chunk_name == png_IDAT)
813 {
814 if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
815 || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
816 png_benign_error(png_ptr, ".Too many IDATs found");
817 }
818 png_handle_unknown(png_ptr, info_ptr, length, keep);
819 if (chunk_name == png_PLTE)
820 png_ptr->mode |= PNG_HAVE_PLTE;
821 }
822#endif
823
824 else if (chunk_name == png_IDAT)
825 {
826 /* Zero length IDATs are legal after the last IDAT has been
827 * read, but not after other chunks have been read. 1.6 does not
828 * always read all the deflate data; specifically it cannot be relied
829 * upon to read the Adler32 at the end. If it doesn't ignore IDAT
830 * chunks which are longer than zero as well:
831 */
832 if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
833 || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
834 png_benign_error(png_ptr, "..Too many IDATs found");
835
836 png_crc_finish(png_ptr, length);
837 }
838 else if (chunk_name == png_PLTE)
839 png_handle_PLTE(png_ptr, info_ptr, length);
840
841#ifdef PNG_READ_bKGD_SUPPORTED
842 else if (chunk_name == png_bKGD)
843 png_handle_bKGD(png_ptr, info_ptr, length);
844#endif
845
846#ifdef PNG_READ_cHRM_SUPPORTED
847 else if (chunk_name == png_cHRM)
848 png_handle_cHRM(png_ptr, info_ptr, length);
849#endif
850
851#ifdef PNG_READ_eXIf_SUPPORTED
852 else if (chunk_name == png_eXIf)
853 png_handle_eXIf(png_ptr, info_ptr, length);
854#endif
855
856#ifdef PNG_READ_gAMA_SUPPORTED
857 else if (chunk_name == png_gAMA)
858 png_handle_gAMA(png_ptr, info_ptr, length);
859#endif
860
861#ifdef PNG_READ_hIST_SUPPORTED
862 else if (chunk_name == png_hIST)
863 png_handle_hIST(png_ptr, info_ptr, length);
864#endif
865
866#ifdef PNG_READ_oFFs_SUPPORTED
867 else if (chunk_name == png_oFFs)
868 png_handle_oFFs(png_ptr, info_ptr, length);
869#endif
870
871#ifdef PNG_READ_pCAL_SUPPORTED
872 else if (chunk_name == png_pCAL)
873 png_handle_pCAL(png_ptr, info_ptr, length);
874#endif
875
876#ifdef PNG_READ_sCAL_SUPPORTED
877 else if (chunk_name == png_sCAL)
878 png_handle_sCAL(png_ptr, info_ptr, length);
879#endif
880
881#ifdef PNG_READ_pHYs_SUPPORTED
882 else if (chunk_name == png_pHYs)
883 png_handle_pHYs(png_ptr, info_ptr, length);
884#endif
885
886#ifdef PNG_READ_sBIT_SUPPORTED
887 else if (chunk_name == png_sBIT)
888 png_handle_sBIT(png_ptr, info_ptr, length);
889#endif
890
891#ifdef PNG_READ_sRGB_SUPPORTED
892 else if (chunk_name == png_sRGB)
893 png_handle_sRGB(png_ptr, info_ptr, length);
894#endif
895
896#ifdef PNG_READ_iCCP_SUPPORTED
897 else if (chunk_name == png_iCCP)
898 png_handle_iCCP(png_ptr, info_ptr, length);
899#endif
900
901#ifdef PNG_READ_sPLT_SUPPORTED
902 else if (chunk_name == png_sPLT)
903 png_handle_sPLT(png_ptr, info_ptr, length);
904#endif
905
906#ifdef PNG_READ_tEXt_SUPPORTED
907 else if (chunk_name == png_tEXt)
908 png_handle_tEXt(png_ptr, info_ptr, length);
909#endif
910
911#ifdef PNG_READ_tIME_SUPPORTED
912 else if (chunk_name == png_tIME)
913 png_handle_tIME(png_ptr, info_ptr, length);
914#endif
915
916#ifdef PNG_READ_tRNS_SUPPORTED
917 else if (chunk_name == png_tRNS)
918 png_handle_tRNS(png_ptr, info_ptr, length);
919#endif
920
921#ifdef PNG_READ_zTXt_SUPPORTED
922 else if (chunk_name == png_zTXt)
923 png_handle_zTXt(png_ptr, info_ptr, length);
924#endif
925
926#ifdef PNG_READ_iTXt_SUPPORTED
927 else if (chunk_name == png_iTXt)
928 png_handle_iTXt(png_ptr, info_ptr, length);
929#endif
930
931 else
932 png_handle_unknown(png_ptr, info_ptr, length,
933 PNG_HANDLE_CHUNK_AS_DEFAULT);
934 } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
935}
936#endif /* SEQUENTIAL_READ */
937
938/* Free all memory used in the read struct */
939static void
940png_read_destroy(png_structrp png_ptr)
941{
942 png_debug(1, "in png_read_destroy");
943
944#ifdef PNG_READ_GAMMA_SUPPORTED
945 png_destroy_gamma_table(png_ptr);
946#endif
947
948 png_free(png_ptr, png_ptr->big_row_buf);
949 png_ptr->big_row_buf = NULL;
950 png_free(png_ptr, png_ptr->big_prev_row);
951 png_ptr->big_prev_row = NULL;
952 png_free(png_ptr, png_ptr->read_buffer);
953 png_ptr->read_buffer = NULL;
954
955#ifdef PNG_READ_QUANTIZE_SUPPORTED
956 png_free(png_ptr, png_ptr->palette_lookup);
957 png_ptr->palette_lookup = NULL;
958 png_free(png_ptr, png_ptr->quantize_index);
959 png_ptr->quantize_index = NULL;
960#endif
961
962 if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
963 {
964 png_zfree(png_ptr, png_ptr->palette);
965 png_ptr->palette = NULL;
966 }
967 png_ptr->free_me &= ~PNG_FREE_PLTE;
968
969#if defined(PNG_tRNS_SUPPORTED) || \
970 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
971 if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
972 {
973 png_free(png_ptr, png_ptr->trans_alpha);
974 png_ptr->trans_alpha = NULL;
975 }
976 png_ptr->free_me &= ~PNG_FREE_TRNS;
977#endif
978
979 inflateEnd(&png_ptr->zstream);
980
981#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
982 png_free(png_ptr, png_ptr->save_buffer);
983 png_ptr->save_buffer = NULL;
984#endif
985
986#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
987 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
988 png_free(png_ptr, png_ptr->unknown_chunk.data);
989 png_ptr->unknown_chunk.data = NULL;
990#endif
991
992#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
993 png_free(png_ptr, png_ptr->chunk_list);
994 png_ptr->chunk_list = NULL;
995#endif
996
997#if defined(PNG_READ_EXPAND_SUPPORTED) && \
998 defined(PNG_ARM_NEON_IMPLEMENTATION)
999 png_free(png_ptr, png_ptr->riffled_palette);
1000 png_ptr->riffled_palette = NULL;
1001#endif
1002
1003 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
1004 * callbacks are still set at this point. They are required to complete the
1005 * destruction of the png_struct itself.
1006 */
1007}
1008
1009/* Free all memory used by the read */
1010void PNGAPI
1011png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1012 png_infopp end_info_ptr_ptr)
1013{
1014 png_structrp png_ptr = NULL;
1015
1016 png_debug(1, "in png_destroy_read_struct");
1017
1018 if (png_ptr_ptr != NULL)
1019 png_ptr = *png_ptr_ptr;
1020
1021 if (png_ptr == NULL)
1022 return;
1023
1024 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1025 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1026 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1027 */
1028 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
1029 png_destroy_info_struct(png_ptr, info_ptr_ptr);
1030
1031 *png_ptr_ptr = NULL;
1032 png_read_destroy(png_ptr);
1033 png_destroy_png_struct(png_ptr);
1034}
1035
1036void PNGAPI
1037png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
1038{
1039 if (png_ptr == NULL)
1040 return;
1041
1042 png_ptr->read_row_fn = read_row_fn;
1043}
1044
1045
1046#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1047#ifdef PNG_INFO_IMAGE_SUPPORTED
1048void PNGAPI
1049png_read_png(png_structrp png_ptr, png_inforp info_ptr,
1050 int transforms, voidp params)
1051{
1052 png_debug(1, "in png_read_png");
1053
1054 if (png_ptr == NULL || info_ptr == NULL)
1055 return;
1056
1057 /* png_read_info() gives us all of the information from the
1058 * PNG file before the first IDAT (image data chunk).
1059 */
1060 png_read_info(png_ptr, info_ptr);
1061 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
1062 png_error(png_ptr, "Image is too high to process with png_read_png()");
1063
1064 /* -------------- image transformations start here ------------------- */
1065 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1066 * is not implemented. This will only happen in de-configured (non-default)
1067 * libpng builds. The results can be unexpected - png_read_png may return
1068 * short or mal-formed rows because the transform is skipped.
1069 */
1070
1071 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1072 */
1073 if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
1074 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1075 * did in earlier versions, while "scale_16" is now more accurate.
1076 */
1077#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1078 png_set_scale_16(png_ptr);
1079#else
1080 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
1081#endif
1082
1083 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1084 * latter by doing SCALE first. This is ok and allows apps not to check for
1085 * which is supported to get the right answer.
1086 */
1087 if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
1088#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1089 png_set_strip_16(png_ptr);
1090#else
1091 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
1092#endif
1093
1094 /* Strip alpha bytes from the input data without combining with
1095 * the background (not recommended).
1096 */
1097 if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
1098#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1099 png_set_strip_alpha(png_ptr);
1100#else
1101 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
1102#endif
1103
1104 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1105 * byte into separate bytes (useful for paletted and grayscale images).
1106 */
1107 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
1108#ifdef PNG_READ_PACK_SUPPORTED
1109 png_set_packing(png_ptr);
1110#else
1111 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
1112#endif
1113
1114 /* Change the order of packed pixels to least significant bit first
1115 * (not useful if you are using png_set_packing).
1116 */
1117 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
1118#ifdef PNG_READ_PACKSWAP_SUPPORTED
1119 png_set_packswap(png_ptr);
1120#else
1121 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
1122#endif
1123
1124 /* Expand paletted colors into true RGB triplets
1125 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1126 * Expand paletted or RGB images with transparency to full alpha
1127 * channels so the data will be available as RGBA quartets.
1128 */
1129 if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
1130#ifdef PNG_READ_EXPAND_SUPPORTED
1131 png_set_expand(png_ptr);
1132#else
1133 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
1134#endif
1135
1136 /* We don't handle background color or gamma transformation or quantizing.
1137 */
1138
1139 /* Invert monochrome files to have 0 as white and 1 as black
1140 */
1141 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
1142#ifdef PNG_READ_INVERT_SUPPORTED
1143 png_set_invert_mono(png_ptr);
1144#else
1145 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
1146#endif
1147
1148 /* If you want to shift the pixel values from the range [0,255] or
1149 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1150 * colors were originally in:
1151 */
1152 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
1153#ifdef PNG_READ_SHIFT_SUPPORTED
1154 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
1155 png_set_shift(png_ptr, &info_ptr->sig_bit);
1156#else
1157 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
1158#endif
1159
1160 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1161 if ((transforms & PNG_TRANSFORM_BGR) != 0)
1162#ifdef PNG_READ_BGR_SUPPORTED
1163 png_set_bgr(png_ptr);
1164#else
1165 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
1166#endif
1167
1168 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1169 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
1170#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1171 png_set_swap_alpha(png_ptr);
1172#else
1173 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1174#endif
1175
1176 /* Swap bytes of 16-bit files to least significant byte first */
1177 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
1178#ifdef PNG_READ_SWAP_SUPPORTED
1179 png_set_swap(png_ptr);
1180#else
1181 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1182#endif
1183
1184/* Added at libpng-1.2.41 */
1185 /* Invert the alpha channel from opacity to transparency */
1186 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1187#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1188 png_set_invert_alpha(png_ptr);
1189#else
1190 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1191#endif
1192
1193/* Added at libpng-1.2.41 */
1194 /* Expand grayscale image to RGB */
1195 if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
1196#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1197 png_set_gray_to_rgb(png_ptr);
1198#else
1199 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1200#endif
1201
1202/* Added at libpng-1.5.4 */
1203 if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
1204#ifdef PNG_READ_EXPAND_16_SUPPORTED
1205 png_set_expand_16(png_ptr);
1206#else
1207 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
1208#endif
1209
1210 /* We don't handle adding filler bytes */
1211
1212 /* We use png_read_image and rely on that for interlace handling, but we also
1213 * call png_read_update_info therefore must turn on interlace handling now:
1214 */
1215 (void)png_set_interlace_handling(png_ptr);
1216
1217 /* Optional call to gamma correct and add the background to the palette
1218 * and update info structure. REQUIRED if you are expecting libpng to
1219 * update the palette for you (i.e., you selected such a transform above).
1220 */
1221 png_read_update_info(png_ptr, info_ptr);
1222
1223 /* -------------- image transformations end here ------------------- */
1224
1225 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1226 if (info_ptr->row_pointers == NULL)
1227 {
1228 png_uint_32 iptr;
1229
1230 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1231 info_ptr->height * (sizeof (png_bytep))));
1232
1233 for (iptr=0; iptr<info_ptr->height; iptr++)
1234 info_ptr->row_pointers[iptr] = NULL;
1235
1236 info_ptr->free_me |= PNG_FREE_ROWS;
1237
1238 for (iptr = 0; iptr < info_ptr->height; iptr++)
1239 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1240 png_malloc(png_ptr, info_ptr->rowbytes));
1241 }
1242
1243 png_read_image(png_ptr, info_ptr->row_pointers);
1244 info_ptr->valid |= PNG_INFO_IDAT;
1245
1246 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1247 png_read_end(png_ptr, info_ptr);
1248
1249 PNG_UNUSED(params)
1250}
1251#endif /* INFO_IMAGE */
1252#endif /* SEQUENTIAL_READ */
1253
1254#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1255/* SIMPLIFIED READ
1256 *
1257 * This code currently relies on the sequential reader, though it could easily
1258 * be made to work with the progressive one.
1259 */
1260/* Arguments to png_image_finish_read: */
1261
1262/* Encoding of PNG data (used by the color-map code) */
1263# define P_NOTSET 0 /* File encoding not yet known */
1264# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1265# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1266# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1267# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1268
1269/* Color-map processing: after libpng has run on the PNG image further
1270 * processing may be needed to convert the data to color-map indices.
1271 */
1272#define PNG_CMAP_NONE 0
1273#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1274#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1275#define PNG_CMAP_RGB 3 /* Process RGB data */
1276#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1277
1278/* The following document where the background is for each processing case. */
1279#define PNG_CMAP_NONE_BACKGROUND 256
1280#define PNG_CMAP_GA_BACKGROUND 231
1281#define PNG_CMAP_TRANS_BACKGROUND 254
1282#define PNG_CMAP_RGB_BACKGROUND 256
1283#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1284
1285typedef struct
1286{
1287 /* Arguments: */
1288 png_imagep image;
1289 png_voidp buffer;
1290 png_int_32 row_stride;
1291 png_voidp colormap;
1292 png_const_colorp background;
1293 /* Local variables: */
1294 png_voidp local_row;
1295 png_voidp first_row;
1296 ptrdiff_t row_bytes; /* step between rows */
1297 int file_encoding; /* E_ values above */
1298 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
1299 int colormap_processing; /* PNG_CMAP_ values above */
1300} png_image_read_control;
1301
1302/* Do all the *safe* initialization - 'safe' means that png_error won't be
1303 * called, so setting up the jmp_buf is not required. This means that anything
1304 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1305 * instead so that control is returned safely back to this routine.
1306 */
1307static int
1308png_image_read_init(png_imagep image)
1309{
1310 if (image->opaque == NULL)
1311 {
1312 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1313 png_safe_error, png_safe_warning);
1314
1315 /* And set the rest of the structure to NULL to ensure that the various
1316 * fields are consistent.
1317 */
1318 memset(image, 0, (sizeof *image));
1319 image->version = PNG_IMAGE_VERSION;
1320
1321 if (png_ptr != NULL)
1322 {
1323 png_infop info_ptr = png_create_info_struct(png_ptr);
1324
1325 if (info_ptr != NULL)
1326 {
1327 png_controlp control = png_voidcast(png_controlp,
1328 png_malloc_warn(png_ptr, (sizeof *control)));
1329
1330 if (control != NULL)
1331 {
1332 memset(control, 0, (sizeof *control));
1333
1334 control->png_ptr = png_ptr;
1335 control->info_ptr = info_ptr;
1336 control->for_write = 0;
1337
1338 image->opaque = control;
1339 return 1;
1340 }
1341
1342 /* Error clean up */
1343 png_destroy_info_struct(png_ptr, &info_ptr);
1344 }
1345
1346 png_destroy_read_struct(&png_ptr, NULL, NULL);
1347 }
1348
1349 return png_image_error(image, "png_image_read: out of memory");
1350 }
1351
1352 return png_image_error(image, "png_image_read: opaque pointer not NULL");
1353}
1354
1355/* Utility to find the base format of a PNG file from a png_struct. */
1356static png_uint_32
1357png_image_format(png_structrp png_ptr)
1358{
1359 png_uint_32 format = 0;
1360
1361 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1362 format |= PNG_FORMAT_FLAG_COLOR;
1363
1364 if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
1365 format |= PNG_FORMAT_FLAG_ALPHA;
1366
1367 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1368 * sets the png_struct fields; that's all we are interested in here. The
1369 * precise interaction with an app call to png_set_tRNS and PNG file reading
1370 * is unclear.
1371 */
1372 else if (png_ptr->num_trans > 0)
1373 format |= PNG_FORMAT_FLAG_ALPHA;
1374
1375 if (png_ptr->bit_depth == 16)
1376 format |= PNG_FORMAT_FLAG_LINEAR;
1377
1378 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
1379 format |= PNG_FORMAT_FLAG_COLORMAP;
1380
1381 return format;
1382}
1383
1384/* Is the given gamma significantly different from sRGB? The test is the same
1385 * one used in pngrtran.c when deciding whether to do gamma correction. The
1386 * arithmetic optimizes the division by using the fact that the inverse of the
1387 * file sRGB gamma is 2.2
1388 */
1389static int
1390png_gamma_not_sRGB(png_fixed_point g)
1391{
1392 if (g < PNG_FP_1)
1393 {
1394 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1395 if (g == 0)
1396 return 0;
1397
1398 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1399 }
1400
1401 return 1;
1402}
1403
1404/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1405 * header and fill in all the information. This is executed in a safe context,
1406 * unlike the init routine above.
1407 */
1408static int
1409png_image_read_header(png_voidp argument)
1410{
1411 png_imagep image = png_voidcast(png_imagep, argument);
1412 png_structrp png_ptr = image->opaque->png_ptr;
1413 png_inforp info_ptr = image->opaque->info_ptr;
1414
1415#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1416 png_set_benign_errors(png_ptr, 1/*warn*/);
1417#endif
1418 png_read_info(png_ptr, info_ptr);
1419
1420 /* Do this the fast way; just read directly out of png_struct. */
1421 image->width = png_ptr->width;
1422 image->height = png_ptr->height;
1423
1424 {
1425 png_uint_32 format = png_image_format(png_ptr);
1426
1427 image->format = format;
1428
1429#ifdef PNG_COLORSPACE_SUPPORTED
1430 /* Does the colorspace match sRGB? If there is no color endpoint
1431 * (colorant) information assume yes, otherwise require the
1432 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1433 * colorspace has been determined to be invalid ignore it.
1434 */
1435 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1436 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1437 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1438 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1439#endif
1440 }
1441
1442 /* We need the maximum number of entries regardless of the format the
1443 * application sets here.
1444 */
1445 {
1446 png_uint_32 cmap_entries;
1447
1448 switch (png_ptr->color_type)
1449 {
1450 case PNG_COLOR_TYPE_GRAY:
1451 cmap_entries = 1U << png_ptr->bit_depth;
1452 break;
1453
1454 case PNG_COLOR_TYPE_PALETTE:
1455 cmap_entries = (png_uint_32)png_ptr->num_palette;
1456 break;
1457
1458 default:
1459 cmap_entries = 256;
1460 break;
1461 }
1462
1463 if (cmap_entries > 256)
1464 cmap_entries = 256;
1465
1466 image->colormap_entries = cmap_entries;
1467 }
1468
1469 return 1;
1470}
1471
1472#ifdef PNG_STDIO_SUPPORTED
1473int PNGAPI
1474png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1475{
1476 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1477 {
1478 if (file != NULL)
1479 {
1480 if (png_image_read_init(image) != 0)
1481 {
1482 /* This is slightly evil, but png_init_io doesn't do anything other
1483 * than this and we haven't changed the standard IO functions so
1484 * this saves a 'safe' function.
1485 */
1486 image->opaque->png_ptr->io_ptr = file;
1487 return png_safe_execute(image, png_image_read_header, image);
1488 }
1489 }
1490
1491 else
1492 return png_image_error(image,
1493 "png_image_begin_read_from_stdio: invalid argument");
1494 }
1495
1496 else if (image != NULL)
1497 return png_image_error(image,
1498 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1499
1500 return 0;
1501}
1502
1503int PNGAPI
1504png_image_begin_read_from_file(png_imagep image, const char *file_name)
1505{
1506 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1507 {
1508 if (file_name != NULL)
1509 {
1510 FILE *fp = fopen(file_name, "rb");
1511
1512 if (fp != NULL)
1513 {
1514 if (png_image_read_init(image) != 0)
1515 {
1516 image->opaque->png_ptr->io_ptr = fp;
1517 image->opaque->owned_file = 1;
1518 return png_safe_execute(image, png_image_read_header, image);
1519 }
1520
1521 /* Clean up: just the opened file. */
1522 (void)fclose(fp);
1523 }
1524
1525 else
1526 return png_image_error(image, strerror(errno));
1527 }
1528
1529 else
1530 return png_image_error(image,
1531 "png_image_begin_read_from_file: invalid argument");
1532 }
1533
1534 else if (image != NULL)
1535 return png_image_error(image,
1536 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1537
1538 return 0;
1539}
1540#endif /* STDIO */
1541
1542static void PNGCBAPI
1543png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need)
1544{
1545 if (png_ptr != NULL)
1546 {
1547 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1548 if (image != NULL)
1549 {
1550 png_controlp cp = image->opaque;
1551 if (cp != NULL)
1552 {
1553 png_const_bytep memory = cp->memory;
1554 size_t size = cp->size;
1555
1556 if (memory != NULL && size >= need)
1557 {
1558 memcpy(out, memory, need);
1559 cp->memory = memory + need;
1560 cp->size = size - need;
1561 return;
1562 }
1563
1564 png_error(png_ptr, "read beyond end of data");
1565 }
1566 }
1567
1568 png_error(png_ptr, "invalid memory read");
1569 }
1570}
1571
1572int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1573 png_const_voidp memory, size_t size)
1574{
1575 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1576 {
1577 if (memory != NULL && size > 0)
1578 {
1579 if (png_image_read_init(image) != 0)
1580 {
1581 /* Now set the IO functions to read from the memory buffer and
1582 * store it into io_ptr. Again do this in-place to avoid calling a
1583 * libpng function that requires error handling.
1584 */
1585 image->opaque->memory = png_voidcast(png_const_bytep, memory);
1586 image->opaque->size = size;
1587 image->opaque->png_ptr->io_ptr = image;
1588 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1589
1590 return png_safe_execute(image, png_image_read_header, image);
1591 }
1592 }
1593
1594 else
1595 return png_image_error(image,
1596 "png_image_begin_read_from_memory: invalid argument");
1597 }
1598
1599 else if (image != NULL)
1600 return png_image_error(image,
1601 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1602
1603 return 0;
1604}
1605
1606/* Utility function to skip chunks that are not used by the simplified image
1607 * read functions and an appropriate macro to call it.
1608 */
1609#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1610static void
1611png_image_skip_unused_chunks(png_structrp png_ptr)
1612{
1613 /* Prepare the reader to ignore all recognized chunks whose data will not
1614 * be used, i.e., all chunks recognized by libpng except for those
1615 * involved in basic image reading:
1616 *
1617 * IHDR, PLTE, IDAT, IEND
1618 *
1619 * Or image data handling:
1620 *
1621 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1622 *
1623 * This provides a small performance improvement and eliminates any
1624 * potential vulnerability to security problems in the unused chunks.
1625 *
1626 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1627 * too. This allows the simplified API to be compiled without iCCP support,
1628 * however if the support is there the chunk is still checked to detect
1629 * errors (which are unfortunately quite common.)
1630 */
1631 {
1632 static const png_byte chunks_to_process[] = {
1633 98, 75, 71, 68, '\0', /* bKGD */
1634 99, 72, 82, 77, '\0', /* cHRM */
1635 103, 65, 77, 65, '\0', /* gAMA */
1636# ifdef PNG_READ_iCCP_SUPPORTED
1637 105, 67, 67, 80, '\0', /* iCCP */
1638# endif
1639 115, 66, 73, 84, '\0', /* sBIT */
1640 115, 82, 71, 66, '\0', /* sRGB */
1641 };
1642
1643 /* Ignore unknown chunks and all other chunks except for the
1644 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1645 */
1646 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1647 NULL, -1);
1648
1649 /* But do not ignore image data handling chunks */
1650 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1651 chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
1652 }
1653}
1654
1655# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1656#else
1657# define PNG_SKIP_CHUNKS(p) ((void)0)
1658#endif /* HANDLE_AS_UNKNOWN */
1659
1660/* The following macro gives the exact rounded answer for all values in the
1661 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1662 * the correct numbers 0..5
1663 */
1664#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1665
1666/* Utility functions to make particular color-maps */
1667static void
1668set_file_encoding(png_image_read_control *display)
1669{
1670 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
1671 if (png_gamma_significant(g) != 0)
1672 {
1673 if (png_gamma_not_sRGB(g) != 0)
1674 {
1675 display->file_encoding = P_FILE;
1676 display->gamma_to_linear = png_reciprocal(g);
1677 }
1678
1679 else
1680 display->file_encoding = P_sRGB;
1681 }
1682
1683 else
1684 display->file_encoding = P_LINEAR8;
1685}
1686
1687static unsigned int
1688decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1689{
1690 if (encoding == P_FILE) /* double check */
1691 encoding = display->file_encoding;
1692
1693 if (encoding == P_NOTSET) /* must be the file encoding */
1694 {
1695 set_file_encoding(display);
1696 encoding = display->file_encoding;
1697 }
1698
1699 switch (encoding)
1700 {
1701 case P_FILE:
1702 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1703 break;
1704
1705 case P_sRGB:
1706 value = png_sRGB_table[value];
1707 break;
1708
1709 case P_LINEAR:
1710 break;
1711
1712 case P_LINEAR8:
1713 value *= 257;
1714 break;
1715
1716#ifdef __GNUC__
1717 default:
1718 png_error(display->image->opaque->png_ptr,
1719 "unexpected encoding (internal error)");
1720#endif
1721 }
1722
1723 return value;
1724}
1725
1726static png_uint_32
1727png_colormap_compose(png_image_read_control *display,
1728 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1729 png_uint_32 background, int encoding)
1730{
1731 /* The file value is composed on the background, the background has the given
1732 * encoding and so does the result, the file is encoded with P_FILE and the
1733 * file and alpha are 8-bit values. The (output) encoding will always be
1734 * P_LINEAR or P_sRGB.
1735 */
1736 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1737 png_uint_32 b = decode_gamma(display, background, encoding);
1738
1739 /* The alpha is always an 8-bit value (it comes from the palette), the value
1740 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1741 */
1742 f = f * alpha + b * (255-alpha);
1743
1744 if (encoding == P_LINEAR)
1745 {
1746 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1747 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1748 */
1749 f *= 257; /* Now scaled by 65535 */
1750 f += f >> 16;
1751 f = (f+32768) >> 16;
1752 }
1753
1754 else /* P_sRGB */
1755 f = PNG_sRGB_FROM_LINEAR(f);
1756
1757 return f;
1758}
1759
1760/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1761 * be 8-bit.
1762 */
1763static void
1764png_create_colormap_entry(png_image_read_control *display,
1765 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1766 png_uint_32 alpha, int encoding)
1767{
1768 png_imagep image = display->image;
1769 int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1770 P_LINEAR : P_sRGB;
1771 int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1772 (red != green || green != blue);
1773
1774 if (ip > 255)
1775 png_error(image->opaque->png_ptr, "color-map index out of range");
1776
1777 /* Update the cache with whether the file gamma is significantly different
1778 * from sRGB.
1779 */
1780 if (encoding == P_FILE)
1781 {
1782 if (display->file_encoding == P_NOTSET)
1783 set_file_encoding(display);
1784
1785 /* Note that the cached value may be P_FILE too, but if it is then the
1786 * gamma_to_linear member has been set.
1787 */
1788 encoding = display->file_encoding;
1789 }
1790
1791 if (encoding == P_FILE)
1792 {
1793 png_fixed_point g = display->gamma_to_linear;
1794
1795 red = png_gamma_16bit_correct(red*257, g);
1796 green = png_gamma_16bit_correct(green*257, g);
1797 blue = png_gamma_16bit_correct(blue*257, g);
1798
1799 if (convert_to_Y != 0 || output_encoding == P_LINEAR)
1800 {
1801 alpha *= 257;
1802 encoding = P_LINEAR;
1803 }
1804
1805 else
1806 {
1807 red = PNG_sRGB_FROM_LINEAR(red * 255);
1808 green = PNG_sRGB_FROM_LINEAR(green * 255);
1809 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1810 encoding = P_sRGB;
1811 }
1812 }
1813
1814 else if (encoding == P_LINEAR8)
1815 {
1816 /* This encoding occurs quite frequently in test cases because PngSuite
1817 * includes a gAMA 1.0 chunk with most images.
1818 */
1819 red *= 257;
1820 green *= 257;
1821 blue *= 257;
1822 alpha *= 257;
1823 encoding = P_LINEAR;
1824 }
1825
1826 else if (encoding == P_sRGB &&
1827 (convert_to_Y != 0 || output_encoding == P_LINEAR))
1828 {
1829 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1830 * linear.
1831 */
1832 red = png_sRGB_table[red];
1833 green = png_sRGB_table[green];
1834 blue = png_sRGB_table[blue];
1835 alpha *= 257;
1836 encoding = P_LINEAR;
1837 }
1838
1839 /* This is set if the color isn't gray but the output is. */
1840 if (encoding == P_LINEAR)
1841 {
1842 if (convert_to_Y != 0)
1843 {
1844 /* NOTE: these values are copied from png_do_rgb_to_gray */
1845 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1846 (png_uint_32)2366 * blue;
1847
1848 if (output_encoding == P_LINEAR)
1849 y = (y + 16384) >> 15;
1850
1851 else
1852 {
1853 /* y is scaled by 32768, we need it scaled by 255: */
1854 y = (y + 128) >> 8;
1855 y *= 255;
1856 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1857 alpha = PNG_DIV257(alpha);
1858 encoding = P_sRGB;
1859 }
1860
1861 blue = red = green = y;
1862 }
1863
1864 else if (output_encoding == P_sRGB)
1865 {
1866 red = PNG_sRGB_FROM_LINEAR(red * 255);
1867 green = PNG_sRGB_FROM_LINEAR(green * 255);
1868 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1869 alpha = PNG_DIV257(alpha);
1870 encoding = P_sRGB;
1871 }
1872 }
1873
1874 if (encoding != output_encoding)
1875 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1876
1877 /* Store the value. */
1878 {
1879# ifdef PNG_FORMAT_AFIRST_SUPPORTED
1880 int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1881 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1882# else
1883# define afirst 0
1884# endif
1885# ifdef PNG_FORMAT_BGR_SUPPORTED
1886 int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1887# else
1888# define bgr 0
1889# endif
1890
1891 if (output_encoding == P_LINEAR)
1892 {
1893 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1894
1895 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1896
1897 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1898 * value, if less than 65535 (this is, effectively, composite on black
1899 * if the alpha channel is removed.)
1900 */
1901 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1902 {
1903 case 4:
1904 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1905 /* FALLTHROUGH */
1906
1907 case 3:
1908 if (alpha < 65535)
1909 {
1910 if (alpha > 0)
1911 {
1912 blue = (blue * alpha + 32767U)/65535U;
1913 green = (green * alpha + 32767U)/65535U;
1914 red = (red * alpha + 32767U)/65535U;
1915 }
1916
1917 else
1918 red = green = blue = 0;
1919 }
1920 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1921 entry[afirst + 1] = (png_uint_16)green;
1922 entry[afirst + bgr] = (png_uint_16)red;
1923 break;
1924
1925 case 2:
1926 entry[1 ^ afirst] = (png_uint_16)alpha;
1927 /* FALLTHROUGH */
1928
1929 case 1:
1930 if (alpha < 65535)
1931 {
1932 if (alpha > 0)
1933 green = (green * alpha + 32767U)/65535U;
1934
1935 else
1936 green = 0;
1937 }
1938 entry[afirst] = (png_uint_16)green;
1939 break;
1940
1941 default:
1942 break;
1943 }
1944 }
1945
1946 else /* output encoding is P_sRGB */
1947 {
1948 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1949
1950 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1951
1952 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1953 {
1954 case 4:
1955 entry[afirst ? 0 : 3] = (png_byte)alpha;
1956 /* FALLTHROUGH */
1957 case 3:
1958 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1959 entry[afirst + 1] = (png_byte)green;
1960 entry[afirst + bgr] = (png_byte)red;
1961 break;
1962
1963 case 2:
1964 entry[1 ^ afirst] = (png_byte)alpha;
1965 /* FALLTHROUGH */
1966 case 1:
1967 entry[afirst] = (png_byte)green;
1968 break;
1969
1970 default:
1971 break;
1972 }
1973 }
1974
1975# ifdef afirst
1976# undef afirst
1977# endif
1978# ifdef bgr
1979# undef bgr
1980# endif
1981 }
1982}
1983
1984static int
1985make_gray_file_colormap(png_image_read_control *display)
1986{
1987 unsigned int i;
1988
1989 for (i=0; i<256; ++i)
1990 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1991
1992 return (int)i;
1993}
1994
1995static int
1996make_gray_colormap(png_image_read_control *display)
1997{
1998 unsigned int i;
1999
2000 for (i=0; i<256; ++i)
2001 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
2002
2003 return (int)i;
2004}
2005#define PNG_GRAY_COLORMAP_ENTRIES 256
2006
2007static int
2008make_ga_colormap(png_image_read_control *display)
2009{
2010 unsigned int i, a;
2011
2012 /* Alpha is retained, the output will be a color-map with entries
2013 * selected by six levels of alpha. One transparent entry, 6 gray
2014 * levels for all the intermediate alpha values, leaving 230 entries
2015 * for the opaque grays. The color-map entries are the six values
2016 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2017 * relevant entry.
2018 *
2019 * if (alpha > 229) // opaque
2020 * {
2021 * // The 231 entries are selected to make the math below work:
2022 * base = 0;
2023 * entry = (231 * gray + 128) >> 8;
2024 * }
2025 * else if (alpha < 26) // transparent
2026 * {
2027 * base = 231;
2028 * entry = 0;
2029 * }
2030 * else // partially opaque
2031 * {
2032 * base = 226 + 6 * PNG_DIV51(alpha);
2033 * entry = PNG_DIV51(gray);
2034 * }
2035 */
2036 i = 0;
2037 while (i < 231)
2038 {
2039 unsigned int gray = (i * 256 + 115) / 231;
2040 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
2041 }
2042
2043 /* 255 is used here for the component values for consistency with the code
2044 * that undoes premultiplication in pngwrite.c.
2045 */
2046 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
2047
2048 for (a=1; a<5; ++a)
2049 {
2050 unsigned int g;
2051
2052 for (g=0; g<6; ++g)
2053 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
2054 P_sRGB);
2055 }
2056
2057 return (int)i;
2058}
2059
2060#define PNG_GA_COLORMAP_ENTRIES 256
2061
2062static int
2063make_rgb_colormap(png_image_read_control *display)
2064{
2065 unsigned int i, r;
2066
2067 /* Build a 6x6x6 opaque RGB cube */
2068 for (i=r=0; r<6; ++r)
2069 {
2070 unsigned int g;
2071
2072 for (g=0; g<6; ++g)
2073 {
2074 unsigned int b;
2075
2076 for (b=0; b<6; ++b)
2077 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
2078 P_sRGB);
2079 }
2080 }
2081
2082 return (int)i;
2083}
2084
2085#define PNG_RGB_COLORMAP_ENTRIES 216
2086
2087/* Return a palette index to the above palette given three 8-bit sRGB values. */
2088#define PNG_RGB_INDEX(r,g,b) \
2089 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2090
2091static int
2092png_image_read_colormap(png_voidp argument)
2093{
2094 png_image_read_control *display =
2095 png_voidcast(png_image_read_control*, argument);
2096 png_imagep image = display->image;
2097
2098 png_structrp png_ptr = image->opaque->png_ptr;
2099 png_uint_32 output_format = image->format;
2100 int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
2101 P_LINEAR : P_sRGB;
2102
2103 unsigned int cmap_entries;
2104 unsigned int output_processing; /* Output processing option */
2105 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
2106
2107 /* Background information; the background color and the index of this color
2108 * in the color-map if it exists (else 256).
2109 */
2110 unsigned int background_index = 256;
2111 png_uint_32 back_r, back_g, back_b;
2112
2113 /* Flags to accumulate things that need to be done to the input. */
2114 int expand_tRNS = 0;
2115
2116 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2117 * very difficult to do, the results look awful, and it is difficult to see
2118 * what possible use it is because the application can't control the
2119 * color-map.
2120 */
2121 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2122 png_ptr->num_trans > 0) /* alpha in input */ &&
2123 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2124 {
2125 if (output_encoding == P_LINEAR) /* compose on black */
2126 back_b = back_g = back_r = 0;
2127
2128 else if (display->background == NULL /* no way to remove it */)
2129 png_error(png_ptr,
2130 "background color must be supplied to remove alpha/transparency");
2131
2132 /* Get a copy of the background color (this avoids repeating the checks
2133 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2134 * output format.
2135 */
2136 else
2137 {
2138 back_g = display->background->green;
2139 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
2140 {
2141 back_r = display->background->red;
2142 back_b = display->background->blue;
2143 }
2144 else
2145 back_b = back_r = back_g;
2146 }
2147 }
2148
2149 else if (output_encoding == P_LINEAR)
2150 back_b = back_r = back_g = 65535;
2151
2152 else
2153 back_b = back_r = back_g = 255;
2154
2155 /* Default the input file gamma if required - this is necessary because
2156 * libpng assumes that if no gamma information is present the data is in the
2157 * output format, but the simplified API deduces the gamma from the input
2158 * format.
2159 */
2160 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2161 {
2162 /* Do this directly, not using the png_colorspace functions, to ensure
2163 * that it happens even if the colorspace is invalid (though probably if
2164 * it is the setting will be ignored) Note that the same thing can be
2165 * achieved at the application interface with png_set_gAMA.
2166 */
2167 if (png_ptr->bit_depth == 16 &&
2168 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2169 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2170
2171 else
2172 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2173
2174 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2175 }
2176
2177 /* Decide what to do based on the PNG color type of the input data. The
2178 * utility function png_create_colormap_entry deals with most aspects of the
2179 * output transformations; this code works out how to produce bytes of
2180 * color-map entries from the original format.
2181 */
2182 switch (png_ptr->color_type)
2183 {
2184 case PNG_COLOR_TYPE_GRAY:
2185 if (png_ptr->bit_depth <= 8)
2186 {
2187 /* There at most 256 colors in the output, regardless of
2188 * transparency.
2189 */
2190 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2191
2192 cmap_entries = 1U << png_ptr->bit_depth;
2193 if (cmap_entries > image->colormap_entries)
2194 png_error(png_ptr, "gray[8] color-map: too few entries");
2195
2196 step = 255 / (cmap_entries - 1);
2197 output_processing = PNG_CMAP_NONE;
2198
2199 /* If there is a tRNS chunk then this either selects a transparent
2200 * value or, if the output has no alpha, the background color.
2201 */
2202 if (png_ptr->num_trans > 0)
2203 {
2204 trans = png_ptr->trans_color.gray;
2205
2206 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2207 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2208 }
2209
2210 /* png_create_colormap_entry just takes an RGBA and writes the
2211 * corresponding color-map entry using the format from 'image',
2212 * including the required conversion to sRGB or linear as
2213 * appropriate. The input values are always either sRGB (if the
2214 * gamma correction flag is 0) or 0..255 scaled file encoded values
2215 * (if the function must gamma correct them).
2216 */
2217 for (i=val=0; i<cmap_entries; ++i, val += step)
2218 {
2219 /* 'i' is a file value. While this will result in duplicated
2220 * entries for 8-bit non-sRGB encoded files it is necessary to
2221 * have non-gamma corrected values to do tRNS handling.
2222 */
2223 if (i != trans)
2224 png_create_colormap_entry(display, i, val, val, val, 255,
2225 P_FILE/*8-bit with file gamma*/);
2226
2227 /* Else this entry is transparent. The colors don't matter if
2228 * there is an alpha channel (back_alpha == 0), but it does no
2229 * harm to pass them in; the values are not set above so this
2230 * passes in white.
2231 *
2232 * NOTE: this preserves the full precision of the application
2233 * supplied background color when it is used.
2234 */
2235 else
2236 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2237 back_alpha, output_encoding);
2238 }
2239
2240 /* We need libpng to preserve the original encoding. */
2241 data_encoding = P_FILE;
2242
2243 /* The rows from libpng, while technically gray values, are now also
2244 * color-map indices; however, they may need to be expanded to 1
2245 * byte per pixel. This is what png_set_packing does (i.e., it
2246 * unpacks the bit values into bytes.)
2247 */
2248 if (png_ptr->bit_depth < 8)
2249 png_set_packing(png_ptr);
2250 }
2251
2252 else /* bit depth is 16 */
2253 {
2254 /* The 16-bit input values can be converted directly to 8-bit gamma
2255 * encoded values; however, if a tRNS chunk is present 257 color-map
2256 * entries are required. This means that the extra entry requires
2257 * special processing; add an alpha channel, sacrifice gray level
2258 * 254 and convert transparent (alpha==0) entries to that.
2259 *
2260 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2261 * same time to minimize quality loss. If a tRNS chunk is present
2262 * this means libpng must handle it too; otherwise it is impossible
2263 * to do the exact match on the 16-bit value.
2264 *
2265 * If the output has no alpha channel *and* the background color is
2266 * gray then it is possible to let libpng handle the substitution by
2267 * ensuring that the corresponding gray level matches the background
2268 * color exactly.
2269 */
2270 data_encoding = P_sRGB;
2271
2272 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2273 png_error(png_ptr, "gray[16] color-map: too few entries");
2274
2275 cmap_entries = (unsigned int)make_gray_colormap(display);
2276
2277 if (png_ptr->num_trans > 0)
2278 {
2279 unsigned int back_alpha;
2280
2281 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2282 back_alpha = 0;
2283
2284 else
2285 {
2286 if (back_r == back_g && back_g == back_b)
2287 {
2288 /* Background is gray; no special processing will be
2289 * required.
2290 */
2291 png_color_16 c;
2292 png_uint_32 gray = back_g;
2293
2294 if (output_encoding == P_LINEAR)
2295 {
2296 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2297
2298 /* And make sure the corresponding palette entry
2299 * matches.
2300 */
2301 png_create_colormap_entry(display, gray, back_g, back_g,
2302 back_g, 65535, P_LINEAR);
2303 }
2304
2305 /* The background passed to libpng, however, must be the
2306 * sRGB value.
2307 */
2308 c.index = 0; /*unused*/
2309 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2310
2311 /* NOTE: does this work without expanding tRNS to alpha?
2312 * It should be the color->gray case below apparently
2313 * doesn't.
2314 */
2315 png_set_background_fixed(png_ptr, &c,
2316 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2317 0/*gamma: not used*/);
2318
2319 output_processing = PNG_CMAP_NONE;
2320 break;
2321 }
2322#ifdef __COVERITY__
2323 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2324 * here.
2325 */
2326 back_alpha = 255;
2327#else
2328 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2329#endif
2330 }
2331
2332 /* output_processing means that the libpng-processed row will be
2333 * 8-bit GA and it has to be processing to single byte color-map
2334 * values. Entry 254 is replaced by either a completely
2335 * transparent entry or by the background color at full
2336 * precision (and the background color is not a simple gray
2337 * level in this case.)
2338 */
2339 expand_tRNS = 1;
2340 output_processing = PNG_CMAP_TRANS;
2341 background_index = 254;
2342
2343 /* And set (overwrite) color-map entry 254 to the actual
2344 * background color at full precision.
2345 */
2346 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2347 back_alpha, output_encoding);
2348 }
2349
2350 else
2351 output_processing = PNG_CMAP_NONE;
2352 }
2353 break;
2354
2355 case PNG_COLOR_TYPE_GRAY_ALPHA:
2356 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2357 * of 65536 combinations. If, however, the alpha channel is to be
2358 * removed there are only 256 possibilities if the background is gray.
2359 * (Otherwise there is a subset of the 65536 possibilities defined by
2360 * the triangle between black, white and the background color.)
2361 *
2362 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2363 * worry about tRNS matching - tRNS is ignored if there is an alpha
2364 * channel.
2365 */
2366 data_encoding = P_sRGB;
2367
2368 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2369 {
2370 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2371 png_error(png_ptr, "gray+alpha color-map: too few entries");
2372
2373 cmap_entries = (unsigned int)make_ga_colormap(display);
2374
2375 background_index = PNG_CMAP_GA_BACKGROUND;
2376 output_processing = PNG_CMAP_GA;
2377 }
2378
2379 else /* alpha is removed */
2380 {
2381 /* Alpha must be removed as the PNG data is processed when the
2382 * background is a color because the G and A channels are
2383 * independent and the vector addition (non-parallel vectors) is a
2384 * 2-D problem.
2385 *
2386 * This can be reduced to the same algorithm as above by making a
2387 * colormap containing gray levels (for the opaque grays), a
2388 * background entry (for a transparent pixel) and a set of four six
2389 * level color values, one set for each intermediate alpha value.
2390 * See the comments in make_ga_colormap for how this works in the
2391 * per-pixel processing.
2392 *
2393 * If the background is gray, however, we only need a 256 entry gray
2394 * level color map. It is sufficient to make the entry generated
2395 * for the background color be exactly the color specified.
2396 */
2397 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2398 (back_r == back_g && back_g == back_b))
2399 {
2400 /* Background is gray; no special processing will be required. */
2401 png_color_16 c;
2402 png_uint_32 gray = back_g;
2403
2404 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2405 png_error(png_ptr, "gray-alpha color-map: too few entries");
2406
2407 cmap_entries = (unsigned int)make_gray_colormap(display);
2408
2409 if (output_encoding == P_LINEAR)
2410 {
2411 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2412
2413 /* And make sure the corresponding palette entry matches. */
2414 png_create_colormap_entry(display, gray, back_g, back_g,
2415 back_g, 65535, P_LINEAR);
2416 }
2417
2418 /* The background passed to libpng, however, must be the sRGB
2419 * value.
2420 */
2421 c.index = 0; /*unused*/
2422 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2423
2424 png_set_background_fixed(png_ptr, &c,
2425 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2426 0/*gamma: not used*/);
2427
2428 output_processing = PNG_CMAP_NONE;
2429 }
2430
2431 else
2432 {
2433 png_uint_32 i, a;
2434
2435 /* This is the same as png_make_ga_colormap, above, except that
2436 * the entries are all opaque.
2437 */
2438 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2439 png_error(png_ptr, "ga-alpha color-map: too few entries");
2440
2441 i = 0;
2442 while (i < 231)
2443 {
2444 png_uint_32 gray = (i * 256 + 115) / 231;
2445 png_create_colormap_entry(display, i++, gray, gray, gray,
2446 255, P_sRGB);
2447 }
2448
2449 /* NOTE: this preserves the full precision of the application
2450 * background color.
2451 */
2452 background_index = i;
2453 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2454#ifdef __COVERITY__
2455 /* Coverity claims that output_encoding
2456 * cannot be 2 (P_LINEAR) here.
2457 */ 255U,
2458#else
2459 output_encoding == P_LINEAR ? 65535U : 255U,
2460#endif
2461 output_encoding);
2462
2463 /* For non-opaque input composite on the sRGB background - this
2464 * requires inverting the encoding for each component. The input
2465 * is still converted to the sRGB encoding because this is a
2466 * reasonable approximate to the logarithmic curve of human
2467 * visual sensitivity, at least over the narrow range which PNG
2468 * represents. Consequently 'G' is always sRGB encoded, while
2469 * 'A' is linear. We need the linear background colors.
2470 */
2471 if (output_encoding == P_sRGB) /* else already linear */
2472 {
2473 /* This may produce a value not exactly matching the
2474 * background, but that's ok because these numbers are only
2475 * used when alpha != 0
2476 */
2477 back_r = png_sRGB_table[back_r];
2478 back_g = png_sRGB_table[back_g];
2479 back_b = png_sRGB_table[back_b];
2480 }
2481
2482 for (a=1; a<5; ++a)
2483 {
2484 unsigned int g;
2485
2486 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2487 * by an 8-bit alpha value (0..255).
2488 */
2489 png_uint_32 alpha = 51 * a;
2490 png_uint_32 back_rx = (255-alpha) * back_r;
2491 png_uint_32 back_gx = (255-alpha) * back_g;
2492 png_uint_32 back_bx = (255-alpha) * back_b;
2493
2494 for (g=0; g<6; ++g)
2495 {
2496 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2497
2498 png_create_colormap_entry(display, i++,
2499 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2500 PNG_sRGB_FROM_LINEAR(gray + back_gx),
2501 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
2502 }
2503 }
2504
2505 cmap_entries = i;
2506 output_processing = PNG_CMAP_GA;
2507 }
2508 }
2509 break;
2510
2511 case PNG_COLOR_TYPE_RGB:
2512 case PNG_COLOR_TYPE_RGB_ALPHA:
2513 /* Exclude the case where the output is gray; we can always handle this
2514 * with the cases above.
2515 */
2516 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2517 {
2518 /* The color-map will be grayscale, so we may as well convert the
2519 * input RGB values to a simple grayscale and use the grayscale
2520 * code above.
2521 *
2522 * NOTE: calling this apparently damages the recognition of the
2523 * transparent color in background color handling; call
2524 * png_set_tRNS_to_alpha before png_set_background_fixed.
2525 */
2526 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2527 -1);
2528 data_encoding = P_sRGB;
2529
2530 /* The output will now be one or two 8-bit gray or gray+alpha
2531 * channels. The more complex case arises when the input has alpha.
2532 */
2533 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2534 png_ptr->num_trans > 0) &&
2535 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2536 {
2537 /* Both input and output have an alpha channel, so no background
2538 * processing is required; just map the GA bytes to the right
2539 * color-map entry.
2540 */
2541 expand_tRNS = 1;
2542
2543 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2544 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2545
2546 cmap_entries = (unsigned int)make_ga_colormap(display);
2547 background_index = PNG_CMAP_GA_BACKGROUND;
2548 output_processing = PNG_CMAP_GA;
2549 }
2550
2551 else
2552 {
2553 /* Either the input or the output has no alpha channel, so there
2554 * will be no non-opaque pixels in the color-map; it will just be
2555 * grayscale.
2556 */
2557 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2558 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2559
2560 /* Ideally this code would use libpng to do the gamma correction,
2561 * but if an input alpha channel is to be removed we will hit the
2562 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2563 * correction bug). Fix this by dropping the gamma correction in
2564 * this case and doing it in the palette; this will result in
2565 * duplicate palette entries, but that's better than the
2566 * alternative of double gamma correction.
2567 */
2568 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2569 png_ptr->num_trans > 0) &&
2570 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
2571 {
2572 cmap_entries = (unsigned int)make_gray_file_colormap(display);
2573 data_encoding = P_FILE;
2574 }
2575
2576 else
2577 cmap_entries = (unsigned int)make_gray_colormap(display);
2578
2579 /* But if the input has alpha or transparency it must be removed
2580 */
2581 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2582 png_ptr->num_trans > 0)
2583 {
2584 png_color_16 c;
2585 png_uint_32 gray = back_g;
2586
2587 /* We need to ensure that the application background exists in
2588 * the colormap and that completely transparent pixels map to
2589 * it. Achieve this simply by ensuring that the entry
2590 * selected for the background really is the background color.
2591 */
2592 if (data_encoding == P_FILE) /* from the fixup above */
2593 {
2594 /* The app supplied a gray which is in output_encoding, we
2595 * need to convert it to a value of the input (P_FILE)
2596 * encoding then set this palette entry to the required
2597 * output encoding.
2598 */
2599 if (output_encoding == P_sRGB)
2600 gray = png_sRGB_table[gray]; /* now P_LINEAR */
2601
2602 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
2603 png_ptr->colorspace.gamma)); /* now P_FILE */
2604
2605 /* And make sure the corresponding palette entry contains
2606 * exactly the required sRGB value.
2607 */
2608 png_create_colormap_entry(display, gray, back_g, back_g,
2609 back_g, 0/*unused*/, output_encoding);
2610 }
2611
2612 else if (output_encoding == P_LINEAR)
2613 {
2614 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2615
2616 /* And make sure the corresponding palette entry matches.
2617 */
2618 png_create_colormap_entry(display, gray, back_g, back_g,
2619 back_g, 0/*unused*/, P_LINEAR);
2620 }
2621
2622 /* The background passed to libpng, however, must be the
2623 * output (normally sRGB) value.
2624 */
2625 c.index = 0; /*unused*/
2626 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2627
2628 /* NOTE: the following is apparently a bug in libpng. Without
2629 * it the transparent color recognition in
2630 * png_set_background_fixed seems to go wrong.
2631 */
2632 expand_tRNS = 1;
2633 png_set_background_fixed(png_ptr, &c,
2634 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2635 0/*gamma: not used*/);
2636 }
2637
2638 output_processing = PNG_CMAP_NONE;
2639 }
2640 }
2641
2642 else /* output is color */
2643 {
2644 /* We could use png_quantize here so long as there is no transparent
2645 * color or alpha; png_quantize ignores alpha. Easier overall just
2646 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2647 * Consequently we always want libpng to produce sRGB data.
2648 */
2649 data_encoding = P_sRGB;
2650
2651 /* Is there any transparency or alpha? */
2652 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2653 png_ptr->num_trans > 0)
2654 {
2655 /* Is there alpha in the output too? If so all four channels are
2656 * processed into a special RGB cube with alpha support.
2657 */
2658 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2659 {
2660 png_uint_32 r;
2661
2662 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2663 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2664
2665 cmap_entries = (unsigned int)make_rgb_colormap(display);
2666
2667 /* Add a transparent entry. */
2668 png_create_colormap_entry(display, cmap_entries, 255, 255,
2669 255, 0, P_sRGB);
2670
2671 /* This is stored as the background index for the processing
2672 * algorithm.
2673 */
2674 background_index = cmap_entries++;
2675
2676 /* Add 27 r,g,b entries each with alpha 0.5. */
2677 for (r=0; r<256; r = (r << 1) | 0x7f)
2678 {
2679 png_uint_32 g;
2680
2681 for (g=0; g<256; g = (g << 1) | 0x7f)
2682 {
2683 png_uint_32 b;
2684
2685 /* This generates components with the values 0, 127 and
2686 * 255
2687 */
2688 for (b=0; b<256; b = (b << 1) | 0x7f)
2689 png_create_colormap_entry(display, cmap_entries++,
2690 r, g, b, 128, P_sRGB);
2691 }
2692 }
2693
2694 expand_tRNS = 1;
2695 output_processing = PNG_CMAP_RGB_ALPHA;
2696 }
2697
2698 else
2699 {
2700 /* Alpha/transparency must be removed. The background must
2701 * exist in the color map (achieved by setting adding it after
2702 * the 666 color-map). If the standard processing code will
2703 * pick up this entry automatically that's all that is
2704 * required; libpng can be called to do the background
2705 * processing.
2706 */
2707 unsigned int sample_size =
2708 PNG_IMAGE_SAMPLE_SIZE(output_format);
2709 png_uint_32 r, g, b; /* sRGB background */
2710
2711 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2712 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2713
2714 cmap_entries = (unsigned int)make_rgb_colormap(display);
2715
2716 png_create_colormap_entry(display, cmap_entries, back_r,
2717 back_g, back_b, 0/*unused*/, output_encoding);
2718
2719 if (output_encoding == P_LINEAR)
2720 {
2721 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2722 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2723 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2724 }
2725
2726 else
2727 {
2728 r = back_r;
2729 g = back_g;
2730 b = back_g;
2731 }
2732
2733 /* Compare the newly-created color-map entry with the one the
2734 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2735 * match, add the new one and set this as the background
2736 * index.
2737 */
2738 if (memcmp((png_const_bytep)display->colormap +
2739 sample_size * cmap_entries,
2740 (png_const_bytep)display->colormap +
2741 sample_size * PNG_RGB_INDEX(r,g,b),
2742 sample_size) != 0)
2743 {
2744 /* The background color must be added. */
2745 background_index = cmap_entries++;
2746
2747 /* Add 27 r,g,b entries each with created by composing with
2748 * the background at alpha 0.5.
2749 */
2750 for (r=0; r<256; r = (r << 1) | 0x7f)
2751 {
2752 for (g=0; g<256; g = (g << 1) | 0x7f)
2753 {
2754 /* This generates components with the values 0, 127
2755 * and 255
2756 */
2757 for (b=0; b<256; b = (b << 1) | 0x7f)
2758 png_create_colormap_entry(display, cmap_entries++,
2759 png_colormap_compose(display, r, P_sRGB, 128,
2760 back_r, output_encoding),
2761 png_colormap_compose(display, g, P_sRGB, 128,
2762 back_g, output_encoding),
2763 png_colormap_compose(display, b, P_sRGB, 128,
2764 back_b, output_encoding),
2765 0/*unused*/, output_encoding);
2766 }
2767 }
2768
2769 expand_tRNS = 1;
2770 output_processing = PNG_CMAP_RGB_ALPHA;
2771 }
2772
2773 else /* background color is in the standard color-map */
2774 {
2775 png_color_16 c;
2776
2777 c.index = 0; /*unused*/
2778 c.red = (png_uint_16)back_r;
2779 c.gray = c.green = (png_uint_16)back_g;
2780 c.blue = (png_uint_16)back_b;
2781
2782 png_set_background_fixed(png_ptr, &c,
2783 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2784 0/*gamma: not used*/);
2785
2786 output_processing = PNG_CMAP_RGB;
2787 }
2788 }
2789 }
2790
2791 else /* no alpha or transparency in the input */
2792 {
2793 /* Alpha in the output is irrelevant, simply map the opaque input
2794 * pixels to the 6x6x6 color-map.
2795 */
2796 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2797 png_error(png_ptr, "rgb color-map: too few entries");
2798
2799 cmap_entries = (unsigned int)make_rgb_colormap(display);
2800 output_processing = PNG_CMAP_RGB;
2801 }
2802 }
2803 break;
2804
2805 case PNG_COLOR_TYPE_PALETTE:
2806 /* It's already got a color-map. It may be necessary to eliminate the
2807 * tRNS entries though.
2808 */
2809 {
2810 unsigned int num_trans = png_ptr->num_trans;
2811 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2812 png_const_colorp colormap = png_ptr->palette;
2813 int do_background = trans != NULL &&
2814 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2815 unsigned int i;
2816
2817 /* Just in case: */
2818 if (trans == NULL)
2819 num_trans = 0;
2820
2821 output_processing = PNG_CMAP_NONE;
2822 data_encoding = P_FILE; /* Don't change from color-map indices */
2823 cmap_entries = (unsigned int)png_ptr->num_palette;
2824 if (cmap_entries > 256)
2825 cmap_entries = 256;
2826
2827 if (cmap_entries > (unsigned int)image->colormap_entries)
2828 png_error(png_ptr, "palette color-map: too few entries");
2829
2830 for (i=0; i < cmap_entries; ++i)
2831 {
2832 if (do_background != 0 && i < num_trans && trans[i] < 255)
2833 {
2834 if (trans[i] == 0)
2835 png_create_colormap_entry(display, i, back_r, back_g,
2836 back_b, 0, output_encoding);
2837
2838 else
2839 {
2840 /* Must compose the PNG file color in the color-map entry
2841 * on the sRGB color in 'back'.
2842 */
2843 png_create_colormap_entry(display, i,
2844 png_colormap_compose(display, colormap[i].red,
2845 P_FILE, trans[i], back_r, output_encoding),
2846 png_colormap_compose(display, colormap[i].green,
2847 P_FILE, trans[i], back_g, output_encoding),
2848 png_colormap_compose(display, colormap[i].blue,
2849 P_FILE, trans[i], back_b, output_encoding),
2850 output_encoding == P_LINEAR ? trans[i] * 257U :
2851 trans[i],
2852 output_encoding);
2853 }
2854 }
2855
2856 else
2857 png_create_colormap_entry(display, i, colormap[i].red,
2858 colormap[i].green, colormap[i].blue,
2859 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2860 }
2861
2862 /* The PNG data may have indices packed in fewer than 8 bits, it
2863 * must be expanded if so.
2864 */
2865 if (png_ptr->bit_depth < 8)
2866 png_set_packing(png_ptr);
2867 }
2868 break;
2869
2870 default:
2871 png_error(png_ptr, "invalid PNG color type");
2872 /*NOT REACHED*/
2873 }
2874
2875 /* Now deal with the output processing */
2876 if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2877 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2878 png_set_tRNS_to_alpha(png_ptr);
2879
2880 switch (data_encoding)
2881 {
2882 case P_sRGB:
2883 /* Change to 8-bit sRGB */
2884 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2885 /* FALLTHROUGH */
2886
2887 case P_FILE:
2888 if (png_ptr->bit_depth > 8)
2889 png_set_scale_16(png_ptr);
2890 break;
2891
2892#ifdef __GNUC__
2893 default:
2894 png_error(png_ptr, "bad data option (internal error)");
2895#endif
2896 }
2897
2898 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2899 png_error(png_ptr, "color map overflow (BAD internal error)");
2900
2901 image->colormap_entries = cmap_entries;
2902
2903 /* Double check using the recorded background index */
2904 switch (output_processing)
2905 {
2906 case PNG_CMAP_NONE:
2907 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2908 goto bad_background;
2909 break;
2910
2911 case PNG_CMAP_GA:
2912 if (background_index != PNG_CMAP_GA_BACKGROUND)
2913 goto bad_background;
2914 break;
2915
2916 case PNG_CMAP_TRANS:
2917 if (background_index >= cmap_entries ||
2918 background_index != PNG_CMAP_TRANS_BACKGROUND)
2919 goto bad_background;
2920 break;
2921
2922 case PNG_CMAP_RGB:
2923 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2924 goto bad_background;
2925 break;
2926
2927 case PNG_CMAP_RGB_ALPHA:
2928 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2929 goto bad_background;
2930 break;
2931
2932 default:
2933 png_error(png_ptr, "bad processing option (internal error)");
2934
2935 bad_background:
2936 png_error(png_ptr, "bad background index (internal error)");
2937 }
2938
2939 display->colormap_processing = (int)output_processing;
2940
2941 return 1/*ok*/;
2942}
2943
2944/* The final part of the color-map read called from png_image_finish_read. */
2945static int
2946png_image_read_and_map(png_voidp argument)
2947{
2948 png_image_read_control *display = png_voidcast(png_image_read_control*,
2949 argument);
2950 png_imagep image = display->image;
2951 png_structrp png_ptr = image->opaque->png_ptr;
2952 int passes;
2953
2954 /* Called when the libpng data must be transformed into the color-mapped
2955 * form. There is a local row buffer in display->local and this routine must
2956 * do the interlace handling.
2957 */
2958 switch (png_ptr->interlaced)
2959 {
2960 case PNG_INTERLACE_NONE:
2961 passes = 1;
2962 break;
2963
2964 case PNG_INTERLACE_ADAM7:
2965 passes = PNG_INTERLACE_ADAM7_PASSES;
2966 break;
2967
2968 default:
2969 png_error(png_ptr, "unknown interlace type");
2970 }
2971
2972 {
2973 png_uint_32 height = image->height;
2974 png_uint_32 width = image->width;
2975 int proc = display->colormap_processing;
2976 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2977 ptrdiff_t step_row = display->row_bytes;
2978 int pass;
2979
2980 for (pass = 0; pass < passes; ++pass)
2981 {
2982 unsigned int startx, stepx, stepy;
2983 png_uint_32 y;
2984
2985 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
2986 {
2987 /* The row may be empty for a short image: */
2988 if (PNG_PASS_COLS(width, pass) == 0)
2989 continue;
2990
2991 startx = PNG_PASS_START_COL(pass);
2992 stepx = PNG_PASS_COL_OFFSET(pass);
2993 y = PNG_PASS_START_ROW(pass);
2994 stepy = PNG_PASS_ROW_OFFSET(pass);
2995 }
2996
2997 else
2998 {
2999 y = 0;
3000 startx = 0;
3001 stepx = stepy = 1;
3002 }
3003
3004 for (; y<height; y += stepy)
3005 {
3006 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3007 png_bytep outrow = first_row + y * step_row;
3008 png_const_bytep end_row = outrow + width;
3009
3010 /* Read read the libpng data into the temporary buffer. */
3011 png_read_row(png_ptr, inrow, NULL);
3012
3013 /* Now process the row according to the processing option, note
3014 * that the caller verifies that the format of the libpng output
3015 * data is as required.
3016 */
3017 outrow += startx;
3018 switch (proc)
3019 {
3020 case PNG_CMAP_GA:
3021 for (; outrow < end_row; outrow += stepx)
3022 {
3023 /* The data is always in the PNG order */
3024 unsigned int gray = *inrow++;
3025 unsigned int alpha = *inrow++;
3026 unsigned int entry;
3027
3028 /* NOTE: this code is copied as a comment in
3029 * make_ga_colormap above. Please update the
3030 * comment if you change this code!
3031 */
3032 if (alpha > 229) /* opaque */
3033 {
3034 entry = (231 * gray + 128) >> 8;
3035 }
3036 else if (alpha < 26) /* transparent */
3037 {
3038 entry = 231;
3039 }
3040 else /* partially opaque */
3041 {
3042 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
3043 }
3044
3045 *outrow = (png_byte)entry;
3046 }
3047 break;
3048
3049 case PNG_CMAP_TRANS:
3050 for (; outrow < end_row; outrow += stepx)
3051 {
3052 png_byte gray = *inrow++;
3053 png_byte alpha = *inrow++;
3054
3055 if (alpha == 0)
3056 *outrow = PNG_CMAP_TRANS_BACKGROUND;
3057
3058 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
3059 *outrow = gray;
3060
3061 else
3062 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
3063 }
3064 break;
3065
3066 case PNG_CMAP_RGB:
3067 for (; outrow < end_row; outrow += stepx)
3068 {
3069 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
3070 inrow += 3;
3071 }
3072 break;
3073
3074 case PNG_CMAP_RGB_ALPHA:
3075 for (; outrow < end_row; outrow += stepx)
3076 {
3077 unsigned int alpha = inrow[3];
3078
3079 /* Because the alpha entries only hold alpha==0.5 values
3080 * split the processing at alpha==0.25 (64) and 0.75
3081 * (196).
3082 */
3083
3084 if (alpha >= 196)
3085 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
3086 inrow[2]);
3087
3088 else if (alpha < 64)
3089 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
3090
3091 else
3092 {
3093 /* Likewise there are three entries for each of r, g
3094 * and b. We could select the entry by popcount on
3095 * the top two bits on those architectures that
3096 * support it, this is what the code below does,
3097 * crudely.
3098 */
3099 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
3100
3101 /* Here are how the values map:
3102 *
3103 * 0x00 .. 0x3f -> 0
3104 * 0x40 .. 0xbf -> 1
3105 * 0xc0 .. 0xff -> 2
3106 *
3107 * So, as above with the explicit alpha checks, the
3108 * breakpoints are at 64 and 196.
3109 */
3110 if (inrow[0] & 0x80) back_i += 9; /* red */
3111 if (inrow[0] & 0x40) back_i += 9;
3112 if (inrow[0] & 0x80) back_i += 3; /* green */
3113 if (inrow[0] & 0x40) back_i += 3;
3114 if (inrow[0] & 0x80) back_i += 1; /* blue */
3115 if (inrow[0] & 0x40) back_i += 1;
3116
3117 *outrow = (png_byte)back_i;
3118 }
3119
3120 inrow += 4;
3121 }
3122 break;
3123
3124 default:
3125 break;
3126 }
3127 }
3128 }
3129 }
3130
3131 return 1;
3132}
3133
3134static int
3135png_image_read_colormapped(png_voidp argument)
3136{
3137 png_image_read_control *display = png_voidcast(png_image_read_control*,
3138 argument);
3139 png_imagep image = display->image;
3140 png_controlp control = image->opaque;
3141 png_structrp png_ptr = control->png_ptr;
3142 png_inforp info_ptr = control->info_ptr;
3143
3144 int passes = 0; /* As a flag */
3145
3146 PNG_SKIP_CHUNKS(png_ptr);
3147
3148 /* Update the 'info' structure and make sure the result is as required; first
3149 * make sure to turn on the interlace handling if it will be required
3150 * (because it can't be turned on *after* the call to png_read_update_info!)
3151 */
3152 if (display->colormap_processing == PNG_CMAP_NONE)
3153 passes = png_set_interlace_handling(png_ptr);
3154
3155 png_read_update_info(png_ptr, info_ptr);
3156
3157 /* The expected output can be deduced from the colormap_processing option. */
3158 switch (display->colormap_processing)
3159 {
3160 case PNG_CMAP_NONE:
3161 /* Output must be one channel and one byte per pixel, the output
3162 * encoding can be anything.
3163 */
3164 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3165 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3166 info_ptr->bit_depth == 8)
3167 break;
3168
3169 goto bad_output;
3170
3171 case PNG_CMAP_TRANS:
3172 case PNG_CMAP_GA:
3173 /* Output must be two channels and the 'G' one must be sRGB, the latter
3174 * can be checked with an exact number because it should have been set
3175 * to this number above!
3176 */
3177 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3178 info_ptr->bit_depth == 8 &&
3179 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3180 image->colormap_entries == 256)
3181 break;
3182
3183 goto bad_output;
3184
3185 case PNG_CMAP_RGB:
3186 /* Output must be 8-bit sRGB encoded RGB */
3187 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3188 info_ptr->bit_depth == 8 &&
3189 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3190 image->colormap_entries == 216)
3191 break;
3192
3193 goto bad_output;
3194
3195 case PNG_CMAP_RGB_ALPHA:
3196 /* Output must be 8-bit sRGB encoded RGBA */
3197 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3198 info_ptr->bit_depth == 8 &&
3199 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3200 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3201 break;
3202
3203 goto bad_output;
3204
3205 default:
3206 bad_output:
3207 png_error(png_ptr, "bad color-map processing (internal error)");
3208 }
3209
3210 /* Now read the rows. Do this here if it is possible to read directly into
3211 * the output buffer, otherwise allocate a local row buffer of the maximum
3212 * size libpng requires and call the relevant processing routine safely.
3213 */
3214 {
3215 png_voidp first_row = display->buffer;
3216 ptrdiff_t row_bytes = display->row_stride;
3217
3218 /* The following expression is designed to work correctly whether it gives
3219 * a signed or an unsigned result.
3220 */
3221 if (row_bytes < 0)
3222 {
3223 char *ptr = png_voidcast(char*, first_row);
3224 ptr += (image->height-1) * (-row_bytes);
3225 first_row = png_voidcast(png_voidp, ptr);
3226 }
3227
3228 display->first_row = first_row;
3229 display->row_bytes = row_bytes;
3230 }
3231
3232 if (passes == 0)
3233 {
3234 int result;
3235 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3236
3237 display->local_row = row;
3238 result = png_safe_execute(image, png_image_read_and_map, display);
3239 display->local_row = NULL;
3240 png_free(png_ptr, row);
3241
3242 return result;
3243 }
3244
3245 else
3246 {
3247 png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
3248
3249 while (--passes >= 0)
3250 {
3251 png_uint_32 y = image->height;
3252 png_bytep row = png_voidcast(png_bytep, display->first_row);
3253
3254 for (; y > 0; --y)
3255 {
3256 png_read_row(png_ptr, row, NULL);
3257 row += row_bytes;
3258 }
3259 }
3260
3261 return 1;
3262 }
3263}
3264
3265/* Just the row reading part of png_image_read. */
3266static int
3267png_image_read_composite(png_voidp argument)
3268{
3269 png_image_read_control *display = png_voidcast(png_image_read_control*,
3270 argument);
3271 png_imagep image = display->image;
3272 png_structrp png_ptr = image->opaque->png_ptr;
3273 int passes;
3274
3275 switch (png_ptr->interlaced)
3276 {
3277 case PNG_INTERLACE_NONE:
3278 passes = 1;
3279 break;
3280
3281 case PNG_INTERLACE_ADAM7:
3282 passes = PNG_INTERLACE_ADAM7_PASSES;
3283 break;
3284
3285 default:
3286 png_error(png_ptr, "unknown interlace type");
3287 }
3288
3289 {
3290 png_uint_32 height = image->height;
3291 png_uint_32 width = image->width;
3292 ptrdiff_t step_row = display->row_bytes;
3293 unsigned int channels =
3294 (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
3295 int pass;
3296
3297 for (pass = 0; pass < passes; ++pass)
3298 {
3299 unsigned int startx, stepx, stepy;
3300 png_uint_32 y;
3301
3302 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3303 {
3304 /* The row may be empty for a short image: */
3305 if (PNG_PASS_COLS(width, pass) == 0)
3306 continue;
3307
3308 startx = PNG_PASS_START_COL(pass) * channels;
3309 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3310 y = PNG_PASS_START_ROW(pass);
3311 stepy = PNG_PASS_ROW_OFFSET(pass);
3312 }
3313
3314 else
3315 {
3316 y = 0;
3317 startx = 0;
3318 stepx = channels;
3319 stepy = 1;
3320 }
3321
3322 for (; y<height; y += stepy)
3323 {
3324 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3325 png_bytep outrow;
3326 png_const_bytep end_row;
3327
3328 /* Read the row, which is packed: */
3329 png_read_row(png_ptr, inrow, NULL);
3330
3331 outrow = png_voidcast(png_bytep, display->first_row);
3332 outrow += y * step_row;
3333 end_row = outrow + width * channels;
3334
3335 /* Now do the composition on each pixel in this row. */
3336 outrow += startx;
3337 for (; outrow < end_row; outrow += stepx)
3338 {
3339 png_byte alpha = inrow[channels];
3340
3341 if (alpha > 0) /* else no change to the output */
3342 {
3343 unsigned int c;
3344
3345 for (c=0; c<channels; ++c)
3346 {
3347 png_uint_32 component = inrow[c];
3348
3349 if (alpha < 255) /* else just use component */
3350 {
3351 /* This is PNG_OPTIMIZED_ALPHA, the component value
3352 * is a linear 8-bit value. Combine this with the
3353 * current outrow[c] value which is sRGB encoded.
3354 * Arithmetic here is 16-bits to preserve the output
3355 * values correctly.
3356 */
3357 component *= 257*255; /* =65535 */
3358 component += (255-alpha)*png_sRGB_table[outrow[c]];
3359
3360 /* So 'component' is scaled by 255*65535 and is
3361 * therefore appropriate for the sRGB to linear
3362 * conversion table.
3363 */
3364 component = PNG_sRGB_FROM_LINEAR(component);
3365 }
3366
3367 outrow[c] = (png_byte)component;
3368 }
3369 }
3370
3371 inrow += channels+1; /* components and alpha channel */
3372 }
3373 }
3374 }
3375 }
3376
3377 return 1;
3378}
3379
3380/* The do_local_background case; called when all the following transforms are to
3381 * be done:
3382 *
3383 * PNG_RGB_TO_GRAY
3384 * PNG_COMPOSITE
3385 * PNG_GAMMA
3386 *
3387 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3388 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3389 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3390 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3391 * row and handles the removal or pre-multiplication of the alpha channel.
3392 */
3393static int
3394png_image_read_background(png_voidp argument)
3395{
3396 png_image_read_control *display = png_voidcast(png_image_read_control*,
3397 argument);
3398 png_imagep image = display->image;
3399 png_structrp png_ptr = image->opaque->png_ptr;
3400 png_inforp info_ptr = image->opaque->info_ptr;
3401 png_uint_32 height = image->height;
3402 png_uint_32 width = image->width;
3403 int pass, passes;
3404
3405 /* Double check the convoluted logic below. We expect to get here with
3406 * libpng doing rgb to gray and gamma correction but background processing
3407 * left to the png_image_read_background function. The rows libpng produce
3408 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3409 */
3410 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3411 png_error(png_ptr, "lost rgb to gray");
3412
3413 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3414 png_error(png_ptr, "unexpected compose");
3415
3416 if (png_get_channels(png_ptr, info_ptr) != 2)
3417 png_error(png_ptr, "lost/gained channels");
3418
3419 /* Expect the 8-bit case to always remove the alpha channel */
3420 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3421 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3422 png_error(png_ptr, "unexpected 8-bit transformation");
3423
3424 switch (png_ptr->interlaced)
3425 {
3426 case PNG_INTERLACE_NONE:
3427 passes = 1;
3428 break;
3429
3430 case PNG_INTERLACE_ADAM7:
3431 passes = PNG_INTERLACE_ADAM7_PASSES;
3432 break;
3433
3434 default:
3435 png_error(png_ptr, "unknown interlace type");
3436 }
3437
3438 /* Use direct access to info_ptr here because otherwise the simplified API
3439 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3440 * checking the value after libpng expansions, not the original value in the
3441 * PNG.
3442 */
3443 switch (info_ptr->bit_depth)
3444 {
3445 case 8:
3446 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3447 * to be removed by composing on a background: either the row if
3448 * display->background is NULL or display->background->green if not.
3449 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3450 */
3451 {
3452 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3453 ptrdiff_t step_row = display->row_bytes;
3454
3455 for (pass = 0; pass < passes; ++pass)
3456 {
3457 unsigned int startx, stepx, stepy;
3458 png_uint_32 y;
3459
3460 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3461 {
3462 /* The row may be empty for a short image: */
3463 if (PNG_PASS_COLS(width, pass) == 0)
3464 continue;
3465
3466 startx = PNG_PASS_START_COL(pass);
3467 stepx = PNG_PASS_COL_OFFSET(pass);
3468 y = PNG_PASS_START_ROW(pass);
3469 stepy = PNG_PASS_ROW_OFFSET(pass);
3470 }
3471
3472 else
3473 {
3474 y = 0;
3475 startx = 0;
3476 stepx = stepy = 1;
3477 }
3478
3479 if (display->background == NULL)
3480 {
3481 for (; y<height; y += stepy)
3482 {
3483 png_bytep inrow = png_voidcast(png_bytep,
3484 display->local_row);
3485 png_bytep outrow = first_row + y * step_row;
3486 png_const_bytep end_row = outrow + width;
3487
3488 /* Read the row, which is packed: */
3489 png_read_row(png_ptr, inrow, NULL);
3490
3491 /* Now do the composition on each pixel in this row. */
3492 outrow += startx;
3493 for (; outrow < end_row; outrow += stepx)
3494 {
3495 png_byte alpha = inrow[1];
3496
3497 if (alpha > 0) /* else no change to the output */
3498 {
3499 png_uint_32 component = inrow[0];
3500
3501 if (alpha < 255) /* else just use component */
3502 {
3503 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3504 * necessary to invert the sRGB transfer
3505 * function and multiply the alpha out.
3506 */
3507 component = png_sRGB_table[component] * alpha;
3508 component += png_sRGB_table[outrow[0]] *
3509 (255-alpha);
3510 component = PNG_sRGB_FROM_LINEAR(component);
3511 }
3512
3513 outrow[0] = (png_byte)component;
3514 }
3515
3516 inrow += 2; /* gray and alpha channel */
3517 }
3518 }
3519 }
3520
3521 else /* constant background value */
3522 {
3523 png_byte background8 = display->background->green;
3524 png_uint_16 background = png_sRGB_table[background8];
3525
3526 for (; y<height; y += stepy)
3527 {
3528 png_bytep inrow = png_voidcast(png_bytep,
3529 display->local_row);
3530 png_bytep outrow = first_row + y * step_row;
3531 png_const_bytep end_row = outrow + width;
3532
3533 /* Read the row, which is packed: */
3534 png_read_row(png_ptr, inrow, NULL);
3535
3536 /* Now do the composition on each pixel in this row. */
3537 outrow += startx;
3538 for (; outrow < end_row; outrow += stepx)
3539 {
3540 png_byte alpha = inrow[1];
3541
3542 if (alpha > 0) /* else use background */
3543 {
3544 png_uint_32 component = inrow[0];
3545
3546 if (alpha < 255) /* else just use component */
3547 {
3548 component = png_sRGB_table[component] * alpha;
3549 component += background * (255-alpha);
3550 component = PNG_sRGB_FROM_LINEAR(component);
3551 }
3552
3553 outrow[0] = (png_byte)component;
3554 }
3555
3556 else
3557 outrow[0] = background8;
3558
3559 inrow += 2; /* gray and alpha channel */
3560 }
3561 }
3562 }
3563 }
3564 }
3565 break;
3566
3567 case 16:
3568 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3569 * still be done and, maybe, the alpha channel removed. This code also
3570 * handles the alpha-first option.
3571 */
3572 {
3573 png_uint_16p first_row = png_voidcast(png_uint_16p,
3574 display->first_row);
3575 /* The division by two is safe because the caller passed in a
3576 * stride which was multiplied by 2 (below) to get row_bytes.
3577 */
3578 ptrdiff_t step_row = display->row_bytes / 2;
3579 unsigned int preserve_alpha = (image->format &
3580 PNG_FORMAT_FLAG_ALPHA) != 0;
3581 unsigned int outchannels = 1U+preserve_alpha;
3582 int swap_alpha = 0;
3583
3584# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3585 if (preserve_alpha != 0 &&
3586 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3587 swap_alpha = 1;
3588# endif
3589
3590 for (pass = 0; pass < passes; ++pass)
3591 {
3592 unsigned int startx, stepx, stepy;
3593 png_uint_32 y;
3594
3595 /* The 'x' start and step are adjusted to output components here.
3596 */
3597 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3598 {
3599 /* The row may be empty for a short image: */
3600 if (PNG_PASS_COLS(width, pass) == 0)
3601 continue;
3602
3603 startx = PNG_PASS_START_COL(pass) * outchannels;
3604 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
3605 y = PNG_PASS_START_ROW(pass);
3606 stepy = PNG_PASS_ROW_OFFSET(pass);
3607 }
3608
3609 else
3610 {
3611 y = 0;
3612 startx = 0;
3613 stepx = outchannels;
3614 stepy = 1;
3615 }
3616
3617 for (; y<height; y += stepy)
3618 {
3619 png_const_uint_16p inrow;
3620 png_uint_16p outrow = first_row + y*step_row;
3621 png_uint_16p end_row = outrow + width * outchannels;
3622
3623 /* Read the row, which is packed: */
3624 png_read_row(png_ptr, png_voidcast(png_bytep,
3625 display->local_row), NULL);
3626 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3627
3628 /* Now do the pre-multiplication on each pixel in this row.
3629 */
3630 outrow += startx;
3631 for (; outrow < end_row; outrow += stepx)
3632 {
3633 png_uint_32 component = inrow[0];
3634 png_uint_16 alpha = inrow[1];
3635
3636 if (alpha > 0) /* else 0 */
3637 {
3638 if (alpha < 65535) /* else just use component */
3639 {
3640 component *= alpha;
3641 component += 32767;
3642 component /= 65535;
3643 }
3644 }
3645
3646 else
3647 component = 0;
3648
3649 outrow[swap_alpha] = (png_uint_16)component;
3650 if (preserve_alpha != 0)
3651 outrow[1 ^ swap_alpha] = alpha;
3652
3653 inrow += 2; /* components and alpha channel */
3654 }
3655 }
3656 }
3657 }
3658 break;
3659
3660#ifdef __GNUC__
3661 default:
3662 png_error(png_ptr, "unexpected bit depth");
3663#endif
3664 }
3665
3666 return 1;
3667}
3668
3669/* The guts of png_image_finish_read as a png_safe_execute callback. */
3670static int
3671png_image_read_direct(png_voidp argument)
3672{
3673 png_image_read_control *display = png_voidcast(png_image_read_control*,
3674 argument);
3675 png_imagep image = display->image;
3676 png_structrp png_ptr = image->opaque->png_ptr;
3677 png_inforp info_ptr = image->opaque->info_ptr;
3678
3679 png_uint_32 format = image->format;
3680 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3681 int do_local_compose = 0;
3682 int do_local_background = 0; /* to avoid double gamma correction bug */
3683 int passes = 0;
3684
3685 /* Add transforms to ensure the correct output format is produced then check
3686 * that the required implementation support is there. Always expand; always
3687 * need 8 bits minimum, no palette and expanded tRNS.
3688 */
3689 png_set_expand(png_ptr);
3690
3691 /* Now check the format to see if it was modified. */
3692 {
3693 png_uint_32 base_format = png_image_format(png_ptr) &
3694 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3695 png_uint_32 change = format ^ base_format;
3696 png_fixed_point output_gamma;
3697 int mode; /* alpha mode */
3698
3699 /* Do this first so that we have a record if rgb to gray is happening. */
3700 if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
3701 {
3702 /* gray<->color transformation required. */
3703 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3704 png_set_gray_to_rgb(png_ptr);
3705
3706 else
3707 {
3708 /* libpng can't do both rgb to gray and
3709 * background/pre-multiplication if there is also significant gamma
3710 * correction, because both operations require linear colors and
3711 * the code only supports one transform doing the gamma correction.
3712 * Handle this by doing the pre-multiplication or background
3713 * operation in this code, if necessary.
3714 *
3715 * TODO: fix this by rewriting pngrtran.c (!)
3716 *
3717 * For the moment (given that fixing this in pngrtran.c is an
3718 * enormous change) 'do_local_background' is used to indicate that
3719 * the problem exists.
3720 */
3721 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3722 do_local_background = 1/*maybe*/;
3723
3724 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3725 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3726 }
3727
3728 change &= ~PNG_FORMAT_FLAG_COLOR;
3729 }
3730
3731 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3732 */
3733 {
3734 png_fixed_point input_gamma_default;
3735
3736 if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3737 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
3738 input_gamma_default = PNG_GAMMA_LINEAR;
3739 else
3740 input_gamma_default = PNG_DEFAULT_sRGB;
3741
3742 /* Call png_set_alpha_mode to set the default for the input gamma; the
3743 * output gamma is set by a second call below.
3744 */
3745 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3746 }
3747
3748 if (linear != 0)
3749 {
3750 /* If there *is* an alpha channel in the input it must be multiplied
3751 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3752 */
3753 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3754 mode = PNG_ALPHA_STANDARD; /* associated alpha */
3755
3756 else
3757 mode = PNG_ALPHA_PNG;
3758
3759 output_gamma = PNG_GAMMA_LINEAR;
3760 }
3761
3762 else
3763 {
3764 mode = PNG_ALPHA_PNG;
3765 output_gamma = PNG_DEFAULT_sRGB;
3766 }
3767
3768 if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
3769 {
3770 mode = PNG_ALPHA_OPTIMIZED;
3771 change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3772 }
3773
3774 /* If 'do_local_background' is set check for the presence of gamma
3775 * correction; this is part of the work-round for the libpng bug
3776 * described above.
3777 *
3778 * TODO: fix libpng and remove this.
3779 */
3780 if (do_local_background != 0)
3781 {
3782 png_fixed_point gtest;
3783
3784 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3785 * gamma correction, the screen gamma hasn't been set on png_struct
3786 * yet; it's set below. png_struct::gamma, however, is set to the
3787 * final value.
3788 */
3789 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3790 PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
3791 do_local_background = 0;
3792
3793 else if (mode == PNG_ALPHA_STANDARD)
3794 {
3795 do_local_background = 2/*required*/;
3796 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3797 }
3798
3799 /* else leave as 1 for the checks below */
3800 }
3801
3802 /* If the bit-depth changes then handle that here. */
3803 if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
3804 {
3805 if (linear != 0 /*16-bit output*/)
3806 png_set_expand_16(png_ptr);
3807
3808 else /* 8-bit output */
3809 png_set_scale_16(png_ptr);
3810
3811 change &= ~PNG_FORMAT_FLAG_LINEAR;
3812 }
3813
3814 /* Now the background/alpha channel changes. */
3815 if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
3816 {
3817 /* Removing an alpha channel requires composition for the 8-bit
3818 * formats; for the 16-bit it is already done, above, by the
3819 * pre-multiplication and the channel just needs to be stripped.
3820 */
3821 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3822 {
3823 /* If RGB->gray is happening the alpha channel must be left and the
3824 * operation completed locally.
3825 *
3826 * TODO: fix libpng and remove this.
3827 */
3828 if (do_local_background != 0)
3829 do_local_background = 2/*required*/;
3830
3831 /* 16-bit output: just remove the channel */
3832 else if (linear != 0) /* compose on black (well, pre-multiply) */
3833 png_set_strip_alpha(png_ptr);
3834
3835 /* 8-bit output: do an appropriate compose */
3836 else if (display->background != NULL)
3837 {
3838 png_color_16 c;
3839
3840 c.index = 0; /*unused*/
3841 c.red = display->background->red;
3842 c.green = display->background->green;
3843 c.blue = display->background->blue;
3844 c.gray = display->background->green;
3845
3846 /* This is always an 8-bit sRGB value, using the 'green' channel
3847 * for gray is much better than calculating the luminance here;
3848 * we can get off-by-one errors in that calculation relative to
3849 * the app expectations and that will show up in transparent
3850 * pixels.
3851 */
3852 png_set_background_fixed(png_ptr, &c,
3853 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3854 0/*gamma: not used*/);
3855 }
3856
3857 else /* compose on row: implemented below. */
3858 {
3859 do_local_compose = 1;
3860 /* This leaves the alpha channel in the output, so it has to be
3861 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3862 * one so the code only has to hack on the pixels that require
3863 * composition.
3864 */
3865 mode = PNG_ALPHA_OPTIMIZED;
3866 }
3867 }
3868
3869 else /* output needs an alpha channel */
3870 {
3871 /* This is tricky because it happens before the swap operation has
3872 * been accomplished; however, the swap does *not* swap the added
3873 * alpha channel (weird API), so it must be added in the correct
3874 * place.
3875 */
3876 png_uint_32 filler; /* opaque filler */
3877 int where;
3878
3879 if (linear != 0)
3880 filler = 65535;
3881
3882 else
3883 filler = 255;
3884
3885#ifdef PNG_FORMAT_AFIRST_SUPPORTED
3886 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3887 {
3888 where = PNG_FILLER_BEFORE;
3889 change &= ~PNG_FORMAT_FLAG_AFIRST;
3890 }
3891
3892 else
3893#endif
3894 where = PNG_FILLER_AFTER;
3895
3896 png_set_add_alpha(png_ptr, filler, where);
3897 }
3898
3899 /* This stops the (irrelevant) call to swap_alpha below. */
3900 change &= ~PNG_FORMAT_FLAG_ALPHA;
3901 }
3902
3903 /* Now set the alpha mode correctly; this is always done, even if there is
3904 * no alpha channel in either the input or the output because it correctly
3905 * sets the output gamma.
3906 */
3907 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3908
3909# ifdef PNG_FORMAT_BGR_SUPPORTED
3910 if ((change & PNG_FORMAT_FLAG_BGR) != 0)
3911 {
3912 /* Check only the output format; PNG is never BGR; don't do this if
3913 * the output is gray, but fix up the 'format' value in that case.
3914 */
3915 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3916 png_set_bgr(png_ptr);
3917
3918 else
3919 format &= ~PNG_FORMAT_FLAG_BGR;
3920
3921 change &= ~PNG_FORMAT_FLAG_BGR;
3922 }
3923# endif
3924
3925# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3926 if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
3927 {
3928 /* Only relevant if there is an alpha channel - it's particularly
3929 * important to handle this correctly because do_local_compose may
3930 * be set above and then libpng will keep the alpha channel for this
3931 * code to remove.
3932 */
3933 if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
3934 {
3935 /* Disable this if doing a local background,
3936 * TODO: remove this when local background is no longer required.
3937 */
3938 if (do_local_background != 2)
3939 png_set_swap_alpha(png_ptr);
3940 }
3941
3942 else
3943 format &= ~PNG_FORMAT_FLAG_AFIRST;
3944
3945 change &= ~PNG_FORMAT_FLAG_AFIRST;
3946 }
3947# endif
3948
3949 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3950 * architecture.
3951 */
3952 if (linear != 0)
3953 {
3954 png_uint_16 le = 0x0001;
3955
3956 if ((*(png_const_bytep) & le) != 0)
3957 png_set_swap(png_ptr);
3958 }
3959
3960 /* If change is not now 0 some transformation is missing - error out. */
3961 if (change != 0)
3962 png_error(png_ptr, "png_read_image: unsupported transformation");
3963 }
3964
3965 PNG_SKIP_CHUNKS(png_ptr);
3966
3967 /* Update the 'info' structure and make sure the result is as required; first
3968 * make sure to turn on the interlace handling if it will be required
3969 * (because it can't be turned on *after* the call to png_read_update_info!)
3970 *
3971 * TODO: remove the do_local_background fixup below.
3972 */
3973 if (do_local_compose == 0 && do_local_background != 2)
3974 passes = png_set_interlace_handling(png_ptr);
3975
3976 png_read_update_info(png_ptr, info_ptr);
3977
3978 {
3979 png_uint_32 info_format = 0;
3980
3981 if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3982 info_format |= PNG_FORMAT_FLAG_COLOR;
3983
3984 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
3985 {
3986 /* do_local_compose removes this channel below. */
3987 if (do_local_compose == 0)
3988 {
3989 /* do_local_background does the same if required. */
3990 if (do_local_background != 2 ||
3991 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3992 info_format |= PNG_FORMAT_FLAG_ALPHA;
3993 }
3994 }
3995
3996 else if (do_local_compose != 0) /* internal error */
3997 png_error(png_ptr, "png_image_read: alpha channel lost");
3998
3999 if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
4000 info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
4001 }
4002
4003 if (info_ptr->bit_depth == 16)
4004 info_format |= PNG_FORMAT_FLAG_LINEAR;
4005
4006#ifdef PNG_FORMAT_BGR_SUPPORTED
4007 if ((png_ptr->transformations & PNG_BGR) != 0)
4008 info_format |= PNG_FORMAT_FLAG_BGR;
4009#endif
4010
4011#ifdef PNG_FORMAT_AFIRST_SUPPORTED
4012 if (do_local_background == 2)
4013 {
4014 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
4015 info_format |= PNG_FORMAT_FLAG_AFIRST;
4016 }
4017
4018 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
4019 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
4020 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
4021 {
4022 if (do_local_background == 2)
4023 png_error(png_ptr, "unexpected alpha swap transformation");
4024
4025 info_format |= PNG_FORMAT_FLAG_AFIRST;
4026 }
4027# endif
4028
4029 /* This is actually an internal error. */
4030 if (info_format != format)
4031 png_error(png_ptr, "png_read_image: invalid transformations");
4032 }
4033
4034 /* Now read the rows. If do_local_compose is set then it is necessary to use
4035 * a local row buffer. The output will be GA, RGBA or BGRA and must be
4036 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
4037 * display acts as a flag.
4038 */
4039 {
4040 png_voidp first_row = display->buffer;
4041 ptrdiff_t row_bytes = display->row_stride;
4042
4043 if (linear != 0)
4044 row_bytes *= 2;
4045
4046 /* The following expression is designed to work correctly whether it gives
4047 * a signed or an unsigned result.
4048 */
4049 if (row_bytes < 0)
4050 {
4051 char *ptr = png_voidcast(char*, first_row);
4052 ptr += (image->height-1) * (-row_bytes);
4053 first_row = png_voidcast(png_voidp, ptr);
4054 }
4055
4056 display->first_row = first_row;
4057 display->row_bytes = row_bytes;
4058 }
4059
4060 if (do_local_compose != 0)
4061 {
4062 int result;
4063 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4064
4065 display->local_row = row;
4066 result = png_safe_execute(image, png_image_read_composite, display);
4067 display->local_row = NULL;
4068 png_free(png_ptr, row);
4069
4070 return result;
4071 }
4072
4073 else if (do_local_background == 2)
4074 {
4075 int result;
4076 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4077
4078 display->local_row = row;
4079 result = png_safe_execute(image, png_image_read_background, display);
4080 display->local_row = NULL;
4081 png_free(png_ptr, row);
4082
4083 return result;
4084 }
4085
4086 else
4087 {
4088 png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
4089
4090 while (--passes >= 0)
4091 {
4092 png_uint_32 y = image->height;
4093 png_bytep row = png_voidcast(png_bytep, display->first_row);
4094
4095 for (; y > 0; --y)
4096 {
4097 png_read_row(png_ptr, row, NULL);
4098 row += row_bytes;
4099 }
4100 }
4101
4102 return 1;
4103 }
4104}
4105
4106int PNGAPI
4107png_image_finish_read(png_imagep image, png_const_colorp background,
4108 void *buffer, png_int_32 row_stride, void *colormap)
4109{
4110 if (image != NULL && image->version == PNG_IMAGE_VERSION)
4111 {
4112 /* Check for row_stride overflow. This check is not performed on the
4113 * original PNG format because it may not occur in the output PNG format
4114 * and libpng deals with the issues of reading the original.
4115 */
4116 unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
4117
4118 /* The following checks just the 'row_stride' calculation to ensure it
4119 * fits in a signed 32-bit value. Because channels/components can be
4120 * either 1 or 2 bytes in size the length of a row can still overflow 32
4121 * bits; this is just to verify that the 'row_stride' argument can be
4122 * represented.
4123 */
4124 if (image->width <= 0x7fffffffU/channels) /* no overflow */
4125 {
4126 png_uint_32 check;
4127 png_uint_32 png_row_stride = image->width * channels;
4128
4129 if (row_stride == 0)
4130 row_stride = (png_int_32)/*SAFE*/png_row_stride;
4131
4132 if (row_stride < 0)
4133 check = (png_uint_32)(-row_stride);
4134
4135 else
4136 check = (png_uint_32)row_stride;
4137
4138 /* This verifies 'check', the absolute value of the actual stride
4139 * passed in and detects overflow in the application calculation (i.e.
4140 * if the app did actually pass in a non-zero 'row_stride'.
4141 */
4142 if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
4143 {
4144 /* Now check for overflow of the image buffer calculation; this
4145 * limits the whole image size to 32 bits for API compatibility with
4146 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4147 *
4148 * The PNG_IMAGE_BUFFER_SIZE macro is:
4149 *
4150 * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
4151 *
4152 * And the component size is always 1 or 2, so make sure that the
4153 * number of *bytes* that the application is saying are available
4154 * does actually fit into a 32-bit number.
4155 *
4156 * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
4157 * will be changed to use png_alloc_size_t; bigger images can be
4158 * accommodated on 64-bit systems.
4159 */
4160 if (image->height <=
4161 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
4162 {
4163 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4164 (image->colormap_entries > 0 && colormap != NULL))
4165 {
4166 int result;
4167 png_image_read_control display;
4168
4169 memset(&display, 0, (sizeof display));
4170 display.image = image;
4171 display.buffer = buffer;
4172 display.row_stride = row_stride;
4173 display.colormap = colormap;
4174 display.background = background;
4175 display.local_row = NULL;
4176
4177 /* Choose the correct 'end' routine; for the color-map case
4178 * all the setup has already been done.
4179 */
4180 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
4181 result =
4182 png_safe_execute(image,
4183 png_image_read_colormap, &display) &&
4184 png_safe_execute(image,
4185 png_image_read_colormapped, &display);
4186
4187 else
4188 result =
4189 png_safe_execute(image,
4190 png_image_read_direct, &display);
4191
4192 png_image_free(image);
4193 return result;
4194 }
4195
4196 else
4197 return png_image_error(image,
4198 "png_image_finish_read[color-map]: no color-map");
4199 }
4200
4201 else
4202 return png_image_error(image,
4203 "png_image_finish_read: image too large");
4204 }
4205
4206 else
4207 return png_image_error(image,
4208 "png_image_finish_read: invalid argument");
4209 }
4210
4211 else
4212 return png_image_error(image,
4213 "png_image_finish_read: row_stride too large");
4214 }
4215
4216 else if (image != NULL)
4217 return png_image_error(image,
4218 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4219
4220 return 0;
4221}
4222
4223#endif /* SIMPLIFIED_READ */
4224#endif /* READ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette