2 /* pngrutil.c - utilities to read a PNG file
4 * Last changed in libpng 1.2.33 [October 31, 2008]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 * This file contains routines that are only called from within
11 * libpng itself during the course of reading an image.
16 #if defined(PNG_READ_SUPPORTED)
18 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
19 # define WIN32_WCE_OLD
22 #ifdef PNG_FLOATING_POINT_SUPPORTED
23 # if defined(WIN32_WCE_OLD)
24 /* strtod() function is not supported on WindowsCE */
25 __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
31 len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
32 str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
35 MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
36 result = wcstod(str, &end);
37 len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
38 *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
39 png_free(png_ptr, str);
44 # define png_strtod(p,a,b) strtod(a,b)
49 png_get_uint_31(png_structp png_ptr, png_bytep buf)
51 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
52 png_uint_32 i = png_get_uint_32(buf);
54 /* Avoid an extra function call by inlining the result. */
55 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
56 ((png_uint_32)(*(buf + 1)) << 16) +
57 ((png_uint_32)(*(buf + 2)) << 8) +
58 (png_uint_32)(*(buf + 3));
60 if (i > PNG_UINT_31_MAX)
61 png_error(png_ptr, "PNG unsigned integer out of range.");
64 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
65 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
67 png_get_uint_32(png_bytep buf)
69 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
70 ((png_uint_32)(*(buf + 1)) << 16) +
71 ((png_uint_32)(*(buf + 2)) << 8) +
72 (png_uint_32)(*(buf + 3));
77 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
78 * data is stored in the PNG file in two's complement format, and it is
79 * assumed that the machine format for signed integers is the same. */
81 png_get_int_32(png_bytep buf)
83 png_int_32 i = ((png_int_32)(*buf) << 24) +
84 ((png_int_32)(*(buf + 1)) << 16) +
85 ((png_int_32)(*(buf + 2)) << 8) +
86 (png_int_32)(*(buf + 3));
91 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
93 png_get_uint_16(png_bytep buf)
95 png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
96 (png_uint_16)(*(buf + 1)));
100 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
102 /* Read the chunk header (length + type name).
103 * Put the type name into png_ptr->chunk_name, and return the length.
105 png_uint_32 /* PRIVATE */
106 png_read_chunk_header(png_structp png_ptr)
111 /* read the length and the chunk name */
112 png_read_data(png_ptr, buf, 8);
113 length = png_get_uint_31(png_ptr, buf);
115 /* put the chunk name into png_ptr->chunk_name */
116 png_memcpy(png_ptr->chunk_name, buf + 4, 4);
118 png_debug2(0, "Reading %s chunk, length = %lu\n",
119 png_ptr->chunk_name, length);
121 /* reset the crc and run it over the chunk name */
122 png_reset_crc(png_ptr);
123 png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
125 /* check to see if chunk name is valid */
126 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
131 /* Read data, and (optionally) run it through the CRC. */
133 png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
135 if (png_ptr == NULL) return;
136 png_read_data(png_ptr, buf, length);
137 png_calculate_crc(png_ptr, buf, length);
140 /* Optionally skip data and then check the CRC. Depending on whether we
141 are reading a ancillary or critical chunk, and how the program has set
142 things up, we may calculate the CRC on the data and print a message.
143 Returns '1' if there was a CRC error, '0' otherwise. */
145 png_crc_finish(png_structp png_ptr, png_uint_32 skip)
148 png_size_t istop = png_ptr->zbuf_size;
150 for (i = (png_size_t)skip; i > istop; i -= istop)
152 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
156 png_crc_read(png_ptr, png_ptr->zbuf, i);
159 if (png_crc_error(png_ptr))
161 if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
162 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
163 (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
164 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
166 png_chunk_warning(png_ptr, "CRC error");
170 png_chunk_error(png_ptr, "CRC error");
178 /* Compare the CRC stored in the PNG file with that calculated by libpng from
179 the data it has read thus far. */
181 png_crc_error(png_structp png_ptr)
183 png_byte crc_bytes[4];
187 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
189 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
190 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
195 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
199 png_read_data(png_ptr, crc_bytes, 4);
203 crc = png_get_uint_32(crc_bytes);
204 return ((int)(crc != png_ptr->crc));
210 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
211 defined(PNG_READ_iCCP_SUPPORTED)
213 * Decompress trailing data in a chunk. The assumption is that chunkdata
214 * points at an allocated area holding the contents of a chunk with a
215 * trailing compressed part. What we get back is an allocated area
216 * holding the original prefix part and an uncompressed version of the
217 * trailing part (the malloc area passed in is freed).
220 png_decompress_chunk(png_structp png_ptr, int comp_type,
221 png_size_t chunklength,
222 png_size_t prefix_size, png_size_t *newlength)
224 static PNG_CONST char msg[] = "Error decoding compressed text";
226 png_size_t text_size;
228 if (comp_type == PNG_COMPRESSION_TYPE_BASE)
231 png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
232 png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
233 png_ptr->zstream.next_out = png_ptr->zbuf;
234 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
239 while (png_ptr->zstream.avail_in)
241 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
242 if (ret != Z_OK && ret != Z_STREAM_END)
244 if (png_ptr->zstream.msg != NULL)
245 png_warning(png_ptr, png_ptr->zstream.msg);
247 png_warning(png_ptr, msg);
248 inflateReset(&png_ptr->zstream);
249 png_ptr->zstream.avail_in = 0;
253 text_size = prefix_size + png_sizeof(msg) + 1;
254 text = (png_charp)png_malloc_warn(png_ptr, text_size);
257 png_free(png_ptr, png_ptr->chunkdata);
258 png_ptr->chunkdata = NULL;
259 png_error(png_ptr, "Not enough memory to decompress chunk");
261 png_memcpy(text, png_ptr->chunkdata, prefix_size);
264 text[text_size - 1] = 0x00;
266 /* Copy what we can of the error message into the text chunk */
267 text_size = (png_size_t)(chunklength -
268 (text - png_ptr->chunkdata) - 1);
269 if (text_size > png_sizeof(msg))
270 text_size = png_sizeof(msg);
271 png_memcpy(text + prefix_size, msg, text_size);
274 if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
278 text_size = prefix_size +
279 png_ptr->zbuf_size - png_ptr->zstream.avail_out;
280 text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
283 png_free(png_ptr, png_ptr->chunkdata);
284 png_ptr->chunkdata = NULL;
286 "Not enough memory to decompress chunk.");
288 png_memcpy(text + prefix_size, png_ptr->zbuf,
289 text_size - prefix_size);
290 png_memcpy(text, png_ptr->chunkdata, prefix_size);
291 *(text + text_size) = 0x00;
298 text = (png_charp)png_malloc_warn(png_ptr,
299 (png_uint_32)(text_size +
300 png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
303 png_free(png_ptr, tmp);
304 png_free(png_ptr, png_ptr->chunkdata);
305 png_ptr->chunkdata = NULL;
307 "Not enough memory to decompress chunk..");
309 png_memcpy(text, tmp, text_size);
310 png_free(png_ptr, tmp);
311 png_memcpy(text + text_size, png_ptr->zbuf,
312 (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
313 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
314 *(text + text_size) = 0x00;
316 if (ret == Z_STREAM_END)
320 png_ptr->zstream.next_out = png_ptr->zbuf;
321 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
325 if (ret != Z_STREAM_END)
327 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
330 if (ret == Z_BUF_ERROR)
331 png_snprintf(umsg, 52,
332 "Buffer error in compressed datastream in %s chunk",
333 png_ptr->chunk_name);
334 else if (ret == Z_DATA_ERROR)
335 png_snprintf(umsg, 52,
336 "Data error in compressed datastream in %s chunk",
337 png_ptr->chunk_name);
339 png_snprintf(umsg, 52,
340 "Incomplete compressed datastream in %s chunk",
341 png_ptr->chunk_name);
342 png_warning(png_ptr, umsg);
345 "Incomplete compressed datastream in chunk other than IDAT");
347 text_size = prefix_size;
350 text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
353 png_free(png_ptr, png_ptr->chunkdata);
354 png_ptr->chunkdata = NULL;
355 png_error(png_ptr, "Not enough memory for text.");
357 png_memcpy(text, png_ptr->chunkdata, prefix_size);
359 *(text + text_size) = 0x00;
362 inflateReset(&png_ptr->zstream);
363 png_ptr->zstream.avail_in = 0;
365 png_free(png_ptr, png_ptr->chunkdata);
366 png_ptr->chunkdata = text;
367 *newlength=text_size;
369 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
371 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
374 png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
375 png_warning(png_ptr, umsg);
377 png_warning(png_ptr, "Unknown zTXt compression type");
380 *(png_ptr->chunkdata + prefix_size) = 0x00;
381 *newlength = prefix_size;
386 /* read and check the IDHR chunk */
388 png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
391 png_uint_32 width, height;
392 int bit_depth, color_type, compression_type, filter_type;
395 png_debug(1, "in png_handle_IHDR\n");
397 if (png_ptr->mode & PNG_HAVE_IHDR)
398 png_error(png_ptr, "Out of place IHDR");
400 /* check the length */
402 png_error(png_ptr, "Invalid IHDR chunk");
404 png_ptr->mode |= PNG_HAVE_IHDR;
406 png_crc_read(png_ptr, buf, 13);
407 png_crc_finish(png_ptr, 0);
409 width = png_get_uint_31(png_ptr, buf);
410 height = png_get_uint_31(png_ptr, buf + 4);
413 compression_type = buf[10];
414 filter_type = buf[11];
415 interlace_type = buf[12];
417 /* set internal variables */
418 png_ptr->width = width;
419 png_ptr->height = height;
420 png_ptr->bit_depth = (png_byte)bit_depth;
421 png_ptr->interlaced = (png_byte)interlace_type;
422 png_ptr->color_type = (png_byte)color_type;
423 #if defined(PNG_MNG_FEATURES_SUPPORTED)
424 png_ptr->filter_type = (png_byte)filter_type;
426 png_ptr->compression_type = (png_byte)compression_type;
428 /* find number of channels */
429 switch (png_ptr->color_type)
431 case PNG_COLOR_TYPE_GRAY:
432 case PNG_COLOR_TYPE_PALETTE:
433 png_ptr->channels = 1;
435 case PNG_COLOR_TYPE_RGB:
436 png_ptr->channels = 3;
438 case PNG_COLOR_TYPE_GRAY_ALPHA:
439 png_ptr->channels = 2;
441 case PNG_COLOR_TYPE_RGB_ALPHA:
442 png_ptr->channels = 4;
446 /* set up other useful info */
447 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
449 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
450 png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
451 png_debug1(3, "channels = %d\n", png_ptr->channels);
452 png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
453 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
454 color_type, interlace_type, compression_type, filter_type);
457 /* read and check the palette */
459 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
461 png_color palette[PNG_MAX_PALETTE_LENGTH];
463 #ifndef PNG_NO_POINTER_INDEXING
467 png_debug(1, "in png_handle_PLTE\n");
469 if (!(png_ptr->mode & PNG_HAVE_IHDR))
470 png_error(png_ptr, "Missing IHDR before PLTE");
471 else if (png_ptr->mode & PNG_HAVE_IDAT)
473 png_warning(png_ptr, "Invalid PLTE after IDAT");
474 png_crc_finish(png_ptr, length);
477 else if (png_ptr->mode & PNG_HAVE_PLTE)
478 png_error(png_ptr, "Duplicate PLTE chunk");
480 png_ptr->mode |= PNG_HAVE_PLTE;
482 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
485 "Ignoring PLTE chunk in grayscale PNG");
486 png_crc_finish(png_ptr, length);
489 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
490 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
492 png_crc_finish(png_ptr, length);
497 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
499 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
501 png_warning(png_ptr, "Invalid palette chunk");
502 png_crc_finish(png_ptr, length);
507 png_error(png_ptr, "Invalid palette chunk");
511 num = (int)length / 3;
513 #ifndef PNG_NO_POINTER_INDEXING
514 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
518 png_crc_read(png_ptr, buf, 3);
519 pal_ptr->red = buf[0];
520 pal_ptr->green = buf[1];
521 pal_ptr->blue = buf[2];
524 for (i = 0; i < num; i++)
528 png_crc_read(png_ptr, buf, 3);
529 /* don't depend upon png_color being any order */
530 palette[i].red = buf[0];
531 palette[i].green = buf[1];
532 palette[i].blue = buf[2];
536 /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
537 whatever the normal CRC configuration tells us. However, if we
538 have an RGB image, the PLTE can be considered ancillary, so
539 we will act as though it is. */
540 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
541 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
544 png_crc_finish(png_ptr, 0);
546 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
547 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
549 /* If we don't want to use the data from an ancillary chunk,
550 we have two options: an error abort, or a warning and we
551 ignore the data in this chunk (which should be OK, since
552 it's considered ancillary for a RGB or RGBA image). */
553 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
555 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
557 png_chunk_error(png_ptr, "CRC error");
561 png_chunk_warning(png_ptr, "CRC error");
565 /* Otherwise, we (optionally) emit a warning and use the chunk. */
566 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
568 png_chunk_warning(png_ptr, "CRC error");
573 png_set_PLTE(png_ptr, info_ptr, palette, num);
575 #if defined(PNG_READ_tRNS_SUPPORTED)
576 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
578 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
580 if (png_ptr->num_trans > (png_uint_16)num)
582 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
583 png_ptr->num_trans = (png_uint_16)num;
585 if (info_ptr->num_trans > (png_uint_16)num)
587 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
588 info_ptr->num_trans = (png_uint_16)num;
597 png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
599 png_debug(1, "in png_handle_IEND\n");
601 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
603 png_error(png_ptr, "No image in file");
606 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
610 png_warning(png_ptr, "Incorrect IEND chunk length");
612 png_crc_finish(png_ptr, length);
614 info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
617 #if defined(PNG_READ_gAMA_SUPPORTED)
619 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
621 png_fixed_point igamma;
622 #ifdef PNG_FLOATING_POINT_SUPPORTED
627 png_debug(1, "in png_handle_gAMA\n");
629 if (!(png_ptr->mode & PNG_HAVE_IHDR))
630 png_error(png_ptr, "Missing IHDR before gAMA");
631 else if (png_ptr->mode & PNG_HAVE_IDAT)
633 png_warning(png_ptr, "Invalid gAMA after IDAT");
634 png_crc_finish(png_ptr, length);
637 else if (png_ptr->mode & PNG_HAVE_PLTE)
638 /* Should be an error, but we can cope with it */
639 png_warning(png_ptr, "Out of place gAMA chunk");
641 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
642 #if defined(PNG_READ_sRGB_SUPPORTED)
643 && !(info_ptr->valid & PNG_INFO_sRGB)
647 png_warning(png_ptr, "Duplicate gAMA chunk");
648 png_crc_finish(png_ptr, length);
654 png_warning(png_ptr, "Incorrect gAMA chunk length");
655 png_crc_finish(png_ptr, length);
659 png_crc_read(png_ptr, buf, 4);
660 if (png_crc_finish(png_ptr, 0))
663 igamma = (png_fixed_point)png_get_uint_32(buf);
664 /* check for zero gamma */
668 "Ignoring gAMA chunk with gamma=0");
672 #if defined(PNG_READ_sRGB_SUPPORTED)
673 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
674 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
677 "Ignoring incorrect gAMA value when sRGB is also present");
678 #ifndef PNG_NO_CONSOLE_IO
679 fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
683 #endif /* PNG_READ_sRGB_SUPPORTED */
685 #ifdef PNG_FLOATING_POINT_SUPPORTED
686 file_gamma = (float)igamma / (float)100000.0;
687 # ifdef PNG_READ_GAMMA_SUPPORTED
688 png_ptr->gamma = file_gamma;
690 png_set_gAMA(png_ptr, info_ptr, file_gamma);
692 #ifdef PNG_FIXED_POINT_SUPPORTED
693 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
698 #if defined(PNG_READ_sBIT_SUPPORTED)
700 png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
705 png_debug(1, "in png_handle_sBIT\n");
707 buf[0] = buf[1] = buf[2] = buf[3] = 0;
709 if (!(png_ptr->mode & PNG_HAVE_IHDR))
710 png_error(png_ptr, "Missing IHDR before sBIT");
711 else if (png_ptr->mode & PNG_HAVE_IDAT)
713 png_warning(png_ptr, "Invalid sBIT after IDAT");
714 png_crc_finish(png_ptr, length);
717 else if (png_ptr->mode & PNG_HAVE_PLTE)
719 /* Should be an error, but we can cope with it */
720 png_warning(png_ptr, "Out of place sBIT chunk");
722 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
724 png_warning(png_ptr, "Duplicate sBIT chunk");
725 png_crc_finish(png_ptr, length);
729 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
732 truelen = (png_size_t)png_ptr->channels;
734 if (length != truelen || length > 4)
736 png_warning(png_ptr, "Incorrect sBIT chunk length");
737 png_crc_finish(png_ptr, length);
741 png_crc_read(png_ptr, buf, truelen);
742 if (png_crc_finish(png_ptr, 0))
745 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
747 png_ptr->sig_bit.red = buf[0];
748 png_ptr->sig_bit.green = buf[1];
749 png_ptr->sig_bit.blue = buf[2];
750 png_ptr->sig_bit.alpha = buf[3];
754 png_ptr->sig_bit.gray = buf[0];
755 png_ptr->sig_bit.red = buf[0];
756 png_ptr->sig_bit.green = buf[0];
757 png_ptr->sig_bit.blue = buf[0];
758 png_ptr->sig_bit.alpha = buf[1];
760 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
764 #if defined(PNG_READ_cHRM_SUPPORTED)
766 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
769 #ifdef PNG_FLOATING_POINT_SUPPORTED
770 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
772 png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
773 int_y_green, int_x_blue, int_y_blue;
775 png_uint_32 uint_x, uint_y;
777 png_debug(1, "in png_handle_cHRM\n");
779 if (!(png_ptr->mode & PNG_HAVE_IHDR))
780 png_error(png_ptr, "Missing IHDR before cHRM");
781 else if (png_ptr->mode & PNG_HAVE_IDAT)
783 png_warning(png_ptr, "Invalid cHRM after IDAT");
784 png_crc_finish(png_ptr, length);
787 else if (png_ptr->mode & PNG_HAVE_PLTE)
788 /* Should be an error, but we can cope with it */
789 png_warning(png_ptr, "Missing PLTE before cHRM");
791 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
792 #if defined(PNG_READ_sRGB_SUPPORTED)
793 && !(info_ptr->valid & PNG_INFO_sRGB)
797 png_warning(png_ptr, "Duplicate cHRM chunk");
798 png_crc_finish(png_ptr, length);
804 png_warning(png_ptr, "Incorrect cHRM chunk length");
805 png_crc_finish(png_ptr, length);
809 png_crc_read(png_ptr, buf, 32);
810 if (png_crc_finish(png_ptr, 0))
813 uint_x = png_get_uint_32(buf);
814 uint_y = png_get_uint_32(buf + 4);
815 if (uint_x > 80000L || uint_y > 80000L ||
816 uint_x + uint_y > 100000L)
818 png_warning(png_ptr, "Invalid cHRM white point");
821 int_x_white = (png_fixed_point)uint_x;
822 int_y_white = (png_fixed_point)uint_y;
824 uint_x = png_get_uint_32(buf + 8);
825 uint_y = png_get_uint_32(buf + 12);
826 if (uint_x + uint_y > 100000L)
828 png_warning(png_ptr, "Invalid cHRM red point");
831 int_x_red = (png_fixed_point)uint_x;
832 int_y_red = (png_fixed_point)uint_y;
834 uint_x = png_get_uint_32(buf + 16);
835 uint_y = png_get_uint_32(buf + 20);
836 if (uint_x + uint_y > 100000L)
838 png_warning(png_ptr, "Invalid cHRM green point");
841 int_x_green = (png_fixed_point)uint_x;
842 int_y_green = (png_fixed_point)uint_y;
844 uint_x = png_get_uint_32(buf + 24);
845 uint_y = png_get_uint_32(buf + 28);
846 if (uint_x + uint_y > 100000L)
848 png_warning(png_ptr, "Invalid cHRM blue point");
851 int_x_blue = (png_fixed_point)uint_x;
852 int_y_blue = (png_fixed_point)uint_y;
854 #ifdef PNG_FLOATING_POINT_SUPPORTED
855 white_x = (float)int_x_white / (float)100000.0;
856 white_y = (float)int_y_white / (float)100000.0;
857 red_x = (float)int_x_red / (float)100000.0;
858 red_y = (float)int_y_red / (float)100000.0;
859 green_x = (float)int_x_green / (float)100000.0;
860 green_y = (float)int_y_green / (float)100000.0;
861 blue_x = (float)int_x_blue / (float)100000.0;
862 blue_y = (float)int_y_blue / (float)100000.0;
865 #if defined(PNG_READ_sRGB_SUPPORTED)
866 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
868 if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) ||
869 PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) ||
870 PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) ||
871 PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) ||
872 PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) ||
873 PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
874 PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) ||
875 PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000))
878 "Ignoring incorrect cHRM value when sRGB is also present");
879 #ifndef PNG_NO_CONSOLE_IO
880 #ifdef PNG_FLOATING_POINT_SUPPORTED
881 fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
882 white_x, white_y, red_x, red_y);
883 fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
884 green_x, green_y, blue_x, blue_y);
886 fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
887 int_x_white, int_y_white, int_x_red, int_y_red);
888 fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
889 int_x_green, int_y_green, int_x_blue, int_y_blue);
891 #endif /* PNG_NO_CONSOLE_IO */
895 #endif /* PNG_READ_sRGB_SUPPORTED */
897 #ifdef PNG_FLOATING_POINT_SUPPORTED
898 png_set_cHRM(png_ptr, info_ptr,
899 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
901 #ifdef PNG_FIXED_POINT_SUPPORTED
902 png_set_cHRM_fixed(png_ptr, info_ptr,
903 int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
904 int_y_green, int_x_blue, int_y_blue);
909 #if defined(PNG_READ_sRGB_SUPPORTED)
911 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
916 png_debug(1, "in png_handle_sRGB\n");
918 if (!(png_ptr->mode & PNG_HAVE_IHDR))
919 png_error(png_ptr, "Missing IHDR before sRGB");
920 else if (png_ptr->mode & PNG_HAVE_IDAT)
922 png_warning(png_ptr, "Invalid sRGB after IDAT");
923 png_crc_finish(png_ptr, length);
926 else if (png_ptr->mode & PNG_HAVE_PLTE)
927 /* Should be an error, but we can cope with it */
928 png_warning(png_ptr, "Out of place sRGB chunk");
930 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
932 png_warning(png_ptr, "Duplicate sRGB chunk");
933 png_crc_finish(png_ptr, length);
939 png_warning(png_ptr, "Incorrect sRGB chunk length");
940 png_crc_finish(png_ptr, length);
944 png_crc_read(png_ptr, buf, 1);
945 if (png_crc_finish(png_ptr, 0))
949 /* check for bad intent */
950 if (intent >= PNG_sRGB_INTENT_LAST)
952 png_warning(png_ptr, "Unknown sRGB intent");
956 #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
957 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
959 png_fixed_point igamma;
960 #ifdef PNG_FIXED_POINT_SUPPORTED
961 igamma=info_ptr->int_gamma;
963 # ifdef PNG_FLOATING_POINT_SUPPORTED
964 igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
967 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
970 "Ignoring incorrect gAMA value when sRGB is also present");
971 #ifndef PNG_NO_CONSOLE_IO
972 # ifdef PNG_FIXED_POINT_SUPPORTED
973 fprintf(stderr, "incorrect gamma=(%d/100000)\n",
974 (int)png_ptr->int_gamma);
976 # ifdef PNG_FLOATING_POINT_SUPPORTED
977 fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
983 #endif /* PNG_READ_gAMA_SUPPORTED */
985 #ifdef PNG_READ_cHRM_SUPPORTED
986 #ifdef PNG_FIXED_POINT_SUPPORTED
987 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
988 if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) ||
989 PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) ||
990 PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) ||
991 PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) ||
992 PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) ||
993 PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
994 PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) ||
995 PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000))
998 "Ignoring incorrect cHRM value when sRGB is also present");
1000 #endif /* PNG_FIXED_POINT_SUPPORTED */
1001 #endif /* PNG_READ_cHRM_SUPPORTED */
1003 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
1005 #endif /* PNG_READ_sRGB_SUPPORTED */
1007 #if defined(PNG_READ_iCCP_SUPPORTED)
1009 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1010 /* Note: this does not properly handle chunks that are > 64K under DOS */
1012 png_byte compression_type;
1015 png_uint_32 skip = 0;
1016 png_uint_32 profile_size, profile_length;
1017 png_size_t slength, prefix_length, data_length;
1019 png_debug(1, "in png_handle_iCCP\n");
1021 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1022 png_error(png_ptr, "Missing IHDR before iCCP");
1023 else if (png_ptr->mode & PNG_HAVE_IDAT)
1025 png_warning(png_ptr, "Invalid iCCP after IDAT");
1026 png_crc_finish(png_ptr, length);
1029 else if (png_ptr->mode & PNG_HAVE_PLTE)
1030 /* Should be an error, but we can cope with it */
1031 png_warning(png_ptr, "Out of place iCCP chunk");
1033 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
1035 png_warning(png_ptr, "Duplicate iCCP chunk");
1036 png_crc_finish(png_ptr, length);
1040 #ifdef PNG_MAX_MALLOC_64K
1041 if (length > (png_uint_32)65535L)
1043 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
1044 skip = length - (png_uint_32)65535L;
1045 length = (png_uint_32)65535L;
1049 png_free(png_ptr, png_ptr->chunkdata);
1050 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1051 slength = (png_size_t)length;
1052 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1054 if (png_crc_finish(png_ptr, skip))
1056 png_free(png_ptr, png_ptr->chunkdata);
1057 png_ptr->chunkdata = NULL;
1061 png_ptr->chunkdata[slength] = 0x00;
1063 for (profile = png_ptr->chunkdata; *profile; profile++)
1064 /* empty loop to find end of name */ ;
1068 /* there should be at least one zero (the compression type byte)
1069 following the separator, and we should be on it */
1070 if ( profile >= png_ptr->chunkdata + slength - 1)
1072 png_free(png_ptr, png_ptr->chunkdata);
1073 png_ptr->chunkdata = NULL;
1074 png_warning(png_ptr, "Malformed iCCP chunk");
1078 /* compression_type should always be zero */
1079 compression_type = *profile++;
1080 if (compression_type)
1082 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
1083 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
1087 prefix_length = profile - png_ptr->chunkdata;
1088 png_decompress_chunk(png_ptr, compression_type,
1089 slength, prefix_length, &data_length);
1091 profile_length = data_length - prefix_length;
1093 if ( prefix_length > data_length || profile_length < 4)
1095 png_free(png_ptr, png_ptr->chunkdata);
1096 png_ptr->chunkdata = NULL;
1097 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
1101 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
1102 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
1103 profile_size = ((*(pC ))<<24) |
1108 if (profile_size < profile_length)
1109 profile_length = profile_size;
1111 if (profile_size > profile_length)
1113 png_free(png_ptr, png_ptr->chunkdata);
1114 png_ptr->chunkdata = NULL;
1115 png_warning(png_ptr, "Ignoring truncated iCCP profile.");
1119 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
1120 compression_type, png_ptr->chunkdata + prefix_length, profile_length);
1121 png_free(png_ptr, png_ptr->chunkdata);
1122 png_ptr->chunkdata = NULL;
1124 #endif /* PNG_READ_iCCP_SUPPORTED */
1126 #if defined(PNG_READ_sPLT_SUPPORTED)
1128 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1129 /* Note: this does not properly handle chunks that are > 64K under DOS */
1131 png_bytep entry_start;
1132 png_sPLT_t new_palette;
1133 #ifdef PNG_NO_POINTER_INDEXING
1136 int data_length, entry_size, i;
1137 png_uint_32 skip = 0;
1140 png_debug(1, "in png_handle_sPLT\n");
1142 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1143 png_error(png_ptr, "Missing IHDR before sPLT");
1144 else if (png_ptr->mode & PNG_HAVE_IDAT)
1146 png_warning(png_ptr, "Invalid sPLT after IDAT");
1147 png_crc_finish(png_ptr, length);
1151 #ifdef PNG_MAX_MALLOC_64K
1152 if (length > (png_uint_32)65535L)
1154 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1155 skip = length - (png_uint_32)65535L;
1156 length = (png_uint_32)65535L;
1160 png_free(png_ptr, png_ptr->chunkdata);
1161 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1162 slength = (png_size_t)length;
1163 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1165 if (png_crc_finish(png_ptr, skip))
1167 png_free(png_ptr, png_ptr->chunkdata);
1168 png_ptr->chunkdata = NULL;
1172 png_ptr->chunkdata[slength] = 0x00;
1174 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
1175 /* empty loop to find end of name */ ;
1178 /* a sample depth should follow the separator, and we should be on it */
1179 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
1181 png_free(png_ptr, png_ptr->chunkdata);
1182 png_ptr->chunkdata = NULL;
1183 png_warning(png_ptr, "malformed sPLT chunk");
1187 new_palette.depth = *entry_start++;
1188 entry_size = (new_palette.depth == 8 ? 6 : 10);
1189 data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
1191 /* integrity-check the data length */
1192 if (data_length % entry_size)
1194 png_free(png_ptr, png_ptr->chunkdata);
1195 png_ptr->chunkdata = NULL;
1196 png_warning(png_ptr, "sPLT chunk has bad length");
1200 new_palette.nentries = (png_int_32) ( data_length / entry_size);
1201 if ((png_uint_32) new_palette.nentries >
1202 (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
1204 png_warning(png_ptr, "sPLT chunk too long");
1207 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1208 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
1209 if (new_palette.entries == NULL)
1211 png_warning(png_ptr, "sPLT chunk requires too much memory");
1215 #ifndef PNG_NO_POINTER_INDEXING
1216 for (i = 0; i < new_palette.nentries; i++)
1218 png_sPLT_entryp pp = new_palette.entries + i;
1220 if (new_palette.depth == 8)
1222 pp->red = *entry_start++;
1223 pp->green = *entry_start++;
1224 pp->blue = *entry_start++;
1225 pp->alpha = *entry_start++;
1229 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1230 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1231 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1232 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1234 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1237 pp = new_palette.entries;
1238 for (i = 0; i < new_palette.nentries; i++)
1241 if (new_palette.depth == 8)
1243 pp[i].red = *entry_start++;
1244 pp[i].green = *entry_start++;
1245 pp[i].blue = *entry_start++;
1246 pp[i].alpha = *entry_start++;
1250 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1251 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1252 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1253 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1255 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1259 /* discard all chunk data except the name and stash that */
1260 new_palette.name = png_ptr->chunkdata;
1262 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1264 png_free(png_ptr, png_ptr->chunkdata);
1265 png_ptr->chunkdata = NULL;
1266 png_free(png_ptr, new_palette.entries);
1268 #endif /* PNG_READ_sPLT_SUPPORTED */
1270 #if defined(PNG_READ_tRNS_SUPPORTED)
1272 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1274 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1276 png_debug(1, "in png_handle_tRNS\n");
1278 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1279 png_error(png_ptr, "Missing IHDR before tRNS");
1280 else if (png_ptr->mode & PNG_HAVE_IDAT)
1282 png_warning(png_ptr, "Invalid tRNS after IDAT");
1283 png_crc_finish(png_ptr, length);
1286 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1288 png_warning(png_ptr, "Duplicate tRNS chunk");
1289 png_crc_finish(png_ptr, length);
1293 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1299 png_warning(png_ptr, "Incorrect tRNS chunk length");
1300 png_crc_finish(png_ptr, length);
1304 png_crc_read(png_ptr, buf, 2);
1305 png_ptr->num_trans = 1;
1306 png_ptr->trans_values.gray = png_get_uint_16(buf);
1308 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1314 png_warning(png_ptr, "Incorrect tRNS chunk length");
1315 png_crc_finish(png_ptr, length);
1318 png_crc_read(png_ptr, buf, (png_size_t)length);
1319 png_ptr->num_trans = 1;
1320 png_ptr->trans_values.red = png_get_uint_16(buf);
1321 png_ptr->trans_values.green = png_get_uint_16(buf + 2);
1322 png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
1324 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1326 if (!(png_ptr->mode & PNG_HAVE_PLTE))
1328 /* Should be an error, but we can cope with it. */
1329 png_warning(png_ptr, "Missing PLTE before tRNS");
1331 if (length > (png_uint_32)png_ptr->num_palette ||
1332 length > PNG_MAX_PALETTE_LENGTH)
1334 png_warning(png_ptr, "Incorrect tRNS chunk length");
1335 png_crc_finish(png_ptr, length);
1340 png_warning(png_ptr, "Zero length tRNS chunk");
1341 png_crc_finish(png_ptr, length);
1344 png_crc_read(png_ptr, readbuf, (png_size_t)length);
1345 png_ptr->num_trans = (png_uint_16)length;
1349 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
1350 png_crc_finish(png_ptr, length);
1354 if (png_crc_finish(png_ptr, 0))
1356 png_ptr->num_trans = 0;
1360 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1361 &(png_ptr->trans_values));
1365 #if defined(PNG_READ_bKGD_SUPPORTED)
1367 png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1372 png_debug(1, "in png_handle_bKGD\n");
1374 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1375 png_error(png_ptr, "Missing IHDR before bKGD");
1376 else if (png_ptr->mode & PNG_HAVE_IDAT)
1378 png_warning(png_ptr, "Invalid bKGD after IDAT");
1379 png_crc_finish(png_ptr, length);
1382 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1383 !(png_ptr->mode & PNG_HAVE_PLTE))
1385 png_warning(png_ptr, "Missing PLTE before bKGD");
1386 png_crc_finish(png_ptr, length);
1389 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
1391 png_warning(png_ptr, "Duplicate bKGD chunk");
1392 png_crc_finish(png_ptr, length);
1396 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1398 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1403 if (length != truelen)
1405 png_warning(png_ptr, "Incorrect bKGD chunk length");
1406 png_crc_finish(png_ptr, length);
1410 png_crc_read(png_ptr, buf, truelen);
1411 if (png_crc_finish(png_ptr, 0))
1414 /* We convert the index value into RGB components so that we can allow
1415 * arbitrary RGB values for background when we have transparency, and
1416 * so it is easy to determine the RGB values of the background color
1417 * from the info_ptr struct. */
1418 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1420 png_ptr->background.index = buf[0];
1421 if (info_ptr && info_ptr->num_palette)
1423 if (buf[0] > info_ptr->num_palette)
1425 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1428 png_ptr->background.red =
1429 (png_uint_16)png_ptr->palette[buf[0]].red;
1430 png_ptr->background.green =
1431 (png_uint_16)png_ptr->palette[buf[0]].green;
1432 png_ptr->background.blue =
1433 (png_uint_16)png_ptr->palette[buf[0]].blue;
1436 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
1438 png_ptr->background.red =
1439 png_ptr->background.green =
1440 png_ptr->background.blue =
1441 png_ptr->background.gray = png_get_uint_16(buf);
1445 png_ptr->background.red = png_get_uint_16(buf);
1446 png_ptr->background.green = png_get_uint_16(buf + 2);
1447 png_ptr->background.blue = png_get_uint_16(buf + 4);
1450 png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
1454 #if defined(PNG_READ_hIST_SUPPORTED)
1456 png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1458 unsigned int num, i;
1459 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1461 png_debug(1, "in png_handle_hIST\n");
1463 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1464 png_error(png_ptr, "Missing IHDR before hIST");
1465 else if (png_ptr->mode & PNG_HAVE_IDAT)
1467 png_warning(png_ptr, "Invalid hIST after IDAT");
1468 png_crc_finish(png_ptr, length);
1471 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
1473 png_warning(png_ptr, "Missing PLTE before hIST");
1474 png_crc_finish(png_ptr, length);
1477 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
1479 png_warning(png_ptr, "Duplicate hIST chunk");
1480 png_crc_finish(png_ptr, length);
1485 if (num != (unsigned int) png_ptr->num_palette || num >
1486 (unsigned int) PNG_MAX_PALETTE_LENGTH)
1488 png_warning(png_ptr, "Incorrect hIST chunk length");
1489 png_crc_finish(png_ptr, length);
1493 for (i = 0; i < num; i++)
1497 png_crc_read(png_ptr, buf, 2);
1498 readbuf[i] = png_get_uint_16(buf);
1501 if (png_crc_finish(png_ptr, 0))
1504 png_set_hIST(png_ptr, info_ptr, readbuf);
1508 #if defined(PNG_READ_pHYs_SUPPORTED)
1510 png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1513 png_uint_32 res_x, res_y;
1516 png_debug(1, "in png_handle_pHYs\n");
1518 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1519 png_error(png_ptr, "Missing IHDR before pHYs");
1520 else if (png_ptr->mode & PNG_HAVE_IDAT)
1522 png_warning(png_ptr, "Invalid pHYs after IDAT");
1523 png_crc_finish(png_ptr, length);
1526 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
1528 png_warning(png_ptr, "Duplicate pHYs chunk");
1529 png_crc_finish(png_ptr, length);
1535 png_warning(png_ptr, "Incorrect pHYs chunk length");
1536 png_crc_finish(png_ptr, length);
1540 png_crc_read(png_ptr, buf, 9);
1541 if (png_crc_finish(png_ptr, 0))
1544 res_x = png_get_uint_32(buf);
1545 res_y = png_get_uint_32(buf + 4);
1547 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
1551 #if defined(PNG_READ_oFFs_SUPPORTED)
1553 png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1556 png_int_32 offset_x, offset_y;
1559 png_debug(1, "in png_handle_oFFs\n");
1561 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1562 png_error(png_ptr, "Missing IHDR before oFFs");
1563 else if (png_ptr->mode & PNG_HAVE_IDAT)
1565 png_warning(png_ptr, "Invalid oFFs after IDAT");
1566 png_crc_finish(png_ptr, length);
1569 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
1571 png_warning(png_ptr, "Duplicate oFFs chunk");
1572 png_crc_finish(png_ptr, length);
1578 png_warning(png_ptr, "Incorrect oFFs chunk length");
1579 png_crc_finish(png_ptr, length);
1583 png_crc_read(png_ptr, buf, 9);
1584 if (png_crc_finish(png_ptr, 0))
1587 offset_x = png_get_int_32(buf);
1588 offset_y = png_get_int_32(buf + 4);
1590 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1594 #if defined(PNG_READ_pCAL_SUPPORTED)
1595 /* read the pCAL chunk (described in the PNG Extensions document) */
1597 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1600 png_byte type, nparams;
1601 png_charp buf, units, endptr;
1606 png_debug(1, "in png_handle_pCAL\n");
1608 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1609 png_error(png_ptr, "Missing IHDR before pCAL");
1610 else if (png_ptr->mode & PNG_HAVE_IDAT)
1612 png_warning(png_ptr, "Invalid pCAL after IDAT");
1613 png_crc_finish(png_ptr, length);
1616 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
1618 png_warning(png_ptr, "Duplicate pCAL chunk");
1619 png_crc_finish(png_ptr, length);
1623 png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
1625 png_free(png_ptr, png_ptr->chunkdata);
1626 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1627 if (png_ptr->chunkdata == NULL)
1629 png_warning(png_ptr, "No memory for pCAL purpose.");
1632 slength = (png_size_t)length;
1633 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1635 if (png_crc_finish(png_ptr, 0))
1637 png_free(png_ptr, png_ptr->chunkdata);
1638 png_ptr->chunkdata = NULL;
1642 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
1644 png_debug(3, "Finding end of pCAL purpose string\n");
1645 for (buf = png_ptr->chunkdata; *buf; buf++)
1648 endptr = png_ptr->chunkdata + slength;
1650 /* We need to have at least 12 bytes after the purpose string
1651 in order to get the parameter information. */
1652 if (endptr <= buf + 12)
1654 png_warning(png_ptr, "Invalid pCAL data");
1655 png_free(png_ptr, png_ptr->chunkdata);
1656 png_ptr->chunkdata = NULL;
1660 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
1661 X0 = png_get_int_32((png_bytep)buf+1);
1662 X1 = png_get_int_32((png_bytep)buf+5);
1667 png_debug(3, "Checking pCAL equation type and number of parameters\n");
1668 /* Check that we have the right number of parameters for known
1670 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
1671 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
1672 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
1673 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
1675 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
1676 png_free(png_ptr, png_ptr->chunkdata);
1677 png_ptr->chunkdata = NULL;
1680 else if (type >= PNG_EQUATION_LAST)
1682 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1685 for (buf = units; *buf; buf++)
1686 /* Empty loop to move past the units string. */ ;
1688 png_debug(3, "Allocating pCAL parameters array\n");
1689 params = (png_charpp)png_malloc_warn(png_ptr,
1690 (png_uint_32)(nparams * png_sizeof(png_charp))) ;
1693 png_free(png_ptr, png_ptr->chunkdata);
1694 png_ptr->chunkdata = NULL;
1695 png_warning(png_ptr, "No memory for pCAL params.");
1699 /* Get pointers to the start of each parameter string. */
1700 for (i = 0; i < (int)nparams; i++)
1702 buf++; /* Skip the null string terminator from previous parameter. */
1704 png_debug1(3, "Reading pCAL parameter %d\n", i);
1705 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
1706 /* Empty loop to move past each parameter string */ ;
1708 /* Make sure we haven't run out of data yet */
1711 png_warning(png_ptr, "Invalid pCAL data");
1712 png_free(png_ptr, png_ptr->chunkdata);
1713 png_ptr->chunkdata = NULL;
1714 png_free(png_ptr, params);
1719 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
1722 png_free(png_ptr, png_ptr->chunkdata);
1723 png_ptr->chunkdata = NULL;
1724 png_free(png_ptr, params);
1728 #if defined(PNG_READ_sCAL_SUPPORTED)
1729 /* read the sCAL chunk */
1731 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1734 #ifdef PNG_FLOATING_POINT_SUPPORTED
1735 double width, height;
1738 #ifdef PNG_FIXED_POINT_SUPPORTED
1739 png_charp swidth, sheight;
1744 png_debug(1, "in png_handle_sCAL\n");
1746 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1747 png_error(png_ptr, "Missing IHDR before sCAL");
1748 else if (png_ptr->mode & PNG_HAVE_IDAT)
1750 png_warning(png_ptr, "Invalid sCAL after IDAT");
1751 png_crc_finish(png_ptr, length);
1754 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
1756 png_warning(png_ptr, "Duplicate sCAL chunk");
1757 png_crc_finish(png_ptr, length);
1761 png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
1763 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1764 if (png_ptr->chunkdata == NULL)
1766 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
1769 slength = (png_size_t)length;
1770 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1772 if (png_crc_finish(png_ptr, 0))
1774 png_free(png_ptr, png_ptr->chunkdata);
1775 png_ptr->chunkdata = NULL;
1779 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
1781 ep = png_ptr->chunkdata + 1; /* skip unit byte */
1783 #ifdef PNG_FLOATING_POINT_SUPPORTED
1784 width = png_strtod(png_ptr, ep, &vp);
1787 png_warning(png_ptr, "malformed width string in sCAL chunk");
1791 #ifdef PNG_FIXED_POINT_SUPPORTED
1792 swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
1795 png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
1798 png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
1802 for (ep = png_ptr->chunkdata; *ep; ep++)
1806 if (png_ptr->chunkdata + slength < ep)
1808 png_warning(png_ptr, "Truncated sCAL chunk");
1809 #if defined(PNG_FIXED_POINT_SUPPORTED) && \
1810 !defined(PNG_FLOATING_POINT_SUPPORTED)
1811 png_free(png_ptr, swidth);
1813 png_free(png_ptr, png_ptr->chunkdata);
1814 png_ptr->chunkdata = NULL;
1818 #ifdef PNG_FLOATING_POINT_SUPPORTED
1819 height = png_strtod(png_ptr, ep, &vp);
1822 png_warning(png_ptr, "malformed height string in sCAL chunk");
1826 #ifdef PNG_FIXED_POINT_SUPPORTED
1827 sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
1828 if (sheight == NULL)
1830 png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
1833 png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
1837 if (png_ptr->chunkdata + slength < ep
1838 #ifdef PNG_FLOATING_POINT_SUPPORTED
1839 || width <= 0. || height <= 0.
1843 png_warning(png_ptr, "Invalid sCAL data");
1844 png_free(png_ptr, png_ptr->chunkdata);
1845 png_ptr->chunkdata = NULL;
1846 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1847 png_free(png_ptr, swidth);
1848 png_free(png_ptr, sheight);
1854 #ifdef PNG_FLOATING_POINT_SUPPORTED
1855 png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
1857 #ifdef PNG_FIXED_POINT_SUPPORTED
1858 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
1862 png_free(png_ptr, png_ptr->chunkdata);
1863 png_ptr->chunkdata = NULL;
1864 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1865 png_free(png_ptr, swidth);
1866 png_free(png_ptr, sheight);
1871 #if defined(PNG_READ_tIME_SUPPORTED)
1873 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1878 png_debug(1, "in png_handle_tIME\n");
1880 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1881 png_error(png_ptr, "Out of place tIME chunk");
1882 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
1884 png_warning(png_ptr, "Duplicate tIME chunk");
1885 png_crc_finish(png_ptr, length);
1889 if (png_ptr->mode & PNG_HAVE_IDAT)
1890 png_ptr->mode |= PNG_AFTER_IDAT;
1894 png_warning(png_ptr, "Incorrect tIME chunk length");
1895 png_crc_finish(png_ptr, length);
1899 png_crc_read(png_ptr, buf, 7);
1900 if (png_crc_finish(png_ptr, 0))
1903 mod_time.second = buf[6];
1904 mod_time.minute = buf[5];
1905 mod_time.hour = buf[4];
1906 mod_time.day = buf[3];
1907 mod_time.month = buf[2];
1908 mod_time.year = png_get_uint_16(buf);
1910 png_set_tIME(png_ptr, info_ptr, &mod_time);
1914 #if defined(PNG_READ_tEXt_SUPPORTED)
1915 /* Note: this does not properly handle chunks that are > 64K under DOS */
1917 png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1922 png_uint_32 skip = 0;
1926 png_debug(1, "in png_handle_tEXt\n");
1928 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1929 png_error(png_ptr, "Missing IHDR before tEXt");
1931 if (png_ptr->mode & PNG_HAVE_IDAT)
1932 png_ptr->mode |= PNG_AFTER_IDAT;
1934 #ifdef PNG_MAX_MALLOC_64K
1935 if (length > (png_uint_32)65535L)
1937 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
1938 skip = length - (png_uint_32)65535L;
1939 length = (png_uint_32)65535L;
1943 png_free(png_ptr, png_ptr->chunkdata);
1944 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1945 if (png_ptr->chunkdata == NULL)
1947 png_warning(png_ptr, "No memory to process text chunk.");
1950 slength = (png_size_t)length;
1951 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1953 if (png_crc_finish(png_ptr, skip))
1955 png_free(png_ptr, png_ptr->chunkdata);
1956 png_ptr->chunkdata = NULL;
1960 key = png_ptr->chunkdata;
1961 key[slength] = 0x00;
1963 for (text = key; *text; text++)
1964 /* empty loop to find end of key */ ;
1966 if (text != key + slength)
1969 text_ptr = (png_textp)png_malloc_warn(png_ptr,
1970 (png_uint_32)png_sizeof(png_text));
1971 if (text_ptr == NULL)
1973 png_warning(png_ptr, "Not enough memory to process text chunk.");
1974 png_free(png_ptr, png_ptr->chunkdata);
1975 png_ptr->chunkdata = NULL;
1978 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1979 text_ptr->key = key;
1980 #ifdef PNG_iTXt_SUPPORTED
1981 text_ptr->lang = NULL;
1982 text_ptr->lang_key = NULL;
1983 text_ptr->itxt_length = 0;
1985 text_ptr->text = text;
1986 text_ptr->text_length = png_strlen(text);
1988 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1990 png_free(png_ptr, png_ptr->chunkdata);
1991 png_ptr->chunkdata = NULL;
1992 png_free(png_ptr, text_ptr);
1994 png_warning(png_ptr, "Insufficient memory to process text chunk.");
1998 #if defined(PNG_READ_zTXt_SUPPORTED)
1999 /* note: this does not correctly handle chunks that are > 64K under DOS */
2001 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2007 png_size_t slength, prefix_len, data_len;
2009 png_debug(1, "in png_handle_zTXt\n");
2010 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2011 png_error(png_ptr, "Missing IHDR before zTXt");
2013 if (png_ptr->mode & PNG_HAVE_IDAT)
2014 png_ptr->mode |= PNG_AFTER_IDAT;
2016 #ifdef PNG_MAX_MALLOC_64K
2017 /* We will no doubt have problems with chunks even half this size, but
2018 there is no hard and fast rule to tell us where to stop. */
2019 if (length > (png_uint_32)65535L)
2021 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
2022 png_crc_finish(png_ptr, length);
2027 png_free(png_ptr, png_ptr->chunkdata);
2028 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2029 if (png_ptr->chunkdata == NULL)
2031 png_warning(png_ptr, "Out of memory processing zTXt chunk.");
2034 slength = (png_size_t)length;
2035 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2036 if (png_crc_finish(png_ptr, 0))
2038 png_free(png_ptr, png_ptr->chunkdata);
2039 png_ptr->chunkdata = NULL;
2043 png_ptr->chunkdata[slength] = 0x00;
2045 for (text = png_ptr->chunkdata; *text; text++)
2048 /* zTXt must have some text after the chunkdataword */
2049 if (text >= png_ptr->chunkdata + slength - 2)
2051 png_warning(png_ptr, "Truncated zTXt chunk");
2052 png_free(png_ptr, png_ptr->chunkdata);
2053 png_ptr->chunkdata = NULL;
2058 comp_type = *(++text);
2059 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
2061 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
2062 comp_type = PNG_TEXT_COMPRESSION_zTXt;
2064 text++; /* skip the compression_method byte */
2066 prefix_len = text - png_ptr->chunkdata;
2068 png_decompress_chunk(png_ptr, comp_type,
2069 (png_size_t)length, prefix_len, &data_len);
2071 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2072 (png_uint_32)png_sizeof(png_text));
2073 if (text_ptr == NULL)
2075 png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
2076 png_free(png_ptr, png_ptr->chunkdata);
2077 png_ptr->chunkdata = NULL;
2080 text_ptr->compression = comp_type;
2081 text_ptr->key = png_ptr->chunkdata;
2082 #ifdef PNG_iTXt_SUPPORTED
2083 text_ptr->lang = NULL;
2084 text_ptr->lang_key = NULL;
2085 text_ptr->itxt_length = 0;
2087 text_ptr->text = png_ptr->chunkdata + prefix_len;
2088 text_ptr->text_length = data_len;
2090 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2092 png_free(png_ptr, text_ptr);
2093 png_free(png_ptr, png_ptr->chunkdata);
2094 png_ptr->chunkdata = NULL;
2096 png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
2100 #if defined(PNG_READ_iTXt_SUPPORTED)
2101 /* note: this does not correctly handle chunks that are > 64K under DOS */
2103 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2106 png_charp key, lang, text, lang_key;
2110 png_size_t slength, prefix_len, data_len;
2112 png_debug(1, "in png_handle_iTXt\n");
2114 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2115 png_error(png_ptr, "Missing IHDR before iTXt");
2117 if (png_ptr->mode & PNG_HAVE_IDAT)
2118 png_ptr->mode |= PNG_AFTER_IDAT;
2120 #ifdef PNG_MAX_MALLOC_64K
2121 /* We will no doubt have problems with chunks even half this size, but
2122 there is no hard and fast rule to tell us where to stop. */
2123 if (length > (png_uint_32)65535L)
2125 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2126 png_crc_finish(png_ptr, length);
2131 png_free(png_ptr, png_ptr->chunkdata);
2132 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2133 if (png_ptr->chunkdata == NULL)
2135 png_warning(png_ptr, "No memory to process iTXt chunk.");
2138 slength = (png_size_t)length;
2139 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2140 if (png_crc_finish(png_ptr, 0))
2142 png_free(png_ptr, png_ptr->chunkdata);
2143 png_ptr->chunkdata = NULL;
2147 png_ptr->chunkdata[slength] = 0x00;
2149 for (lang = png_ptr->chunkdata; *lang; lang++)
2151 lang++; /* skip NUL separator */
2153 /* iTXt must have a language tag (possibly empty), two compression bytes,
2154 translated keyword (possibly empty), and possibly some text after the
2157 if (lang >= png_ptr->chunkdata + slength - 3)
2159 png_warning(png_ptr, "Truncated iTXt chunk");
2160 png_free(png_ptr, png_ptr->chunkdata);
2161 png_ptr->chunkdata = NULL;
2166 comp_flag = *lang++;
2167 comp_type = *lang++;
2170 for (lang_key = lang; *lang_key; lang_key++)
2172 lang_key++; /* skip NUL separator */
2174 if (lang_key >= png_ptr->chunkdata + slength)
2176 png_warning(png_ptr, "Truncated iTXt chunk");
2177 png_free(png_ptr, png_ptr->chunkdata);
2178 png_ptr->chunkdata = NULL;
2182 for (text = lang_key; *text; text++)
2184 text++; /* skip NUL separator */
2185 if (text >= png_ptr->chunkdata + slength)
2187 png_warning(png_ptr, "Malformed iTXt chunk");
2188 png_free(png_ptr, png_ptr->chunkdata);
2189 png_ptr->chunkdata = NULL;
2193 prefix_len = text - png_ptr->chunkdata;
2195 key=png_ptr->chunkdata;
2197 png_decompress_chunk(png_ptr, comp_type,
2198 (size_t)length, prefix_len, &data_len);
2200 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2201 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2202 (png_uint_32)png_sizeof(png_text));
2203 if (text_ptr == NULL)
2205 png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
2206 png_free(png_ptr, png_ptr->chunkdata);
2207 png_ptr->chunkdata = NULL;
2210 text_ptr->compression = (int)comp_flag + 1;
2211 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2212 text_ptr->lang = png_ptr->chunkdata + (lang - key);
2213 text_ptr->itxt_length = data_len;
2214 text_ptr->text_length = 0;
2215 text_ptr->key = png_ptr->chunkdata;
2216 text_ptr->text = png_ptr->chunkdata + prefix_len;
2218 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2220 png_free(png_ptr, text_ptr);
2221 png_free(png_ptr, png_ptr->chunkdata);
2222 png_ptr->chunkdata = NULL;
2224 png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
2228 /* This function is called when we haven't found a handler for a
2229 chunk. If there isn't a problem with the chunk itself (ie bad
2230 chunk name, CRC, or a critical chunk), the chunk is silently ignored
2231 -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2232 case it will be saved away to be written out later. */
2234 png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2236 png_uint_32 skip = 0;
2238 png_debug(1, "in png_handle_unknown\n");
2240 if (png_ptr->mode & PNG_HAVE_IDAT)
2242 #ifdef PNG_USE_LOCAL_ARRAYS
2245 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */
2246 png_ptr->mode |= PNG_AFTER_IDAT;
2249 if (!(png_ptr->chunk_name[0] & 0x20))
2251 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2252 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2253 PNG_HANDLE_CHUNK_ALWAYS
2254 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2255 && png_ptr->read_user_chunk_fn == NULL
2259 png_chunk_error(png_ptr, "unknown critical chunk");
2262 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2263 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
2264 (png_ptr->read_user_chunk_fn != NULL))
2266 #ifdef PNG_MAX_MALLOC_64K
2267 if (length > (png_uint_32)65535L)
2269 png_warning(png_ptr, "unknown chunk too large to fit in memory");
2270 skip = length - (png_uint_32)65535L;
2271 length = (png_uint_32)65535L;
2274 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
2275 (png_charp)png_ptr->chunk_name,
2276 png_sizeof(png_ptr->unknown_chunk.name));
2277 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
2278 png_ptr->unknown_chunk.size = (png_size_t)length;
2280 png_ptr->unknown_chunk.data = NULL;
2283 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
2284 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
2286 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2287 if (png_ptr->read_user_chunk_fn != NULL)
2289 /* callback to user unknown chunk handler */
2291 ret = (*(png_ptr->read_user_chunk_fn))
2292 (png_ptr, &png_ptr->unknown_chunk);
2294 png_chunk_error(png_ptr, "error in user chunk");
2297 if (!(png_ptr->chunk_name[0] & 0x20))
2298 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2299 PNG_HANDLE_CHUNK_ALWAYS)
2300 png_chunk_error(png_ptr, "unknown critical chunk");
2301 png_set_unknown_chunks(png_ptr, info_ptr,
2302 &png_ptr->unknown_chunk, 1);
2307 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2308 png_free(png_ptr, png_ptr->unknown_chunk.data);
2309 png_ptr->unknown_chunk.data = NULL;
2315 png_crc_finish(png_ptr, skip);
2317 #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2318 info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
2322 /* This function is called to verify that a chunk name is valid.
2323 This function can't have the "critical chunk check" incorporated
2324 into it, since in the future we will need to be able to call user
2325 functions to handle unknown critical chunks after we check that
2326 the chunk name itself is valid. */
2328 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2331 png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
2333 png_debug(1, "in png_check_chunk_name\n");
2334 if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
2335 isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
2337 png_chunk_error(png_ptr, "invalid chunk type");
2341 /* Combines the row recently read in with the existing pixels in the
2342 row. This routine takes care of alpha and transparency if requested.
2343 This routine also handles the two methods of progressive display
2344 of interlaced images, depending on the mask value.
2345 The mask value describes which pixels are to be combined with
2346 the row. The pattern always repeats every 8 pixels, so just 8
2347 bits are needed. A one indicates the pixel is to be combined,
2348 a zero indicates the pixel is to be skipped. This is in addition
2349 to any alpha or transparency value associated with the pixel. If
2350 you want all pixels to be combined, pass 0xff (255) in mask. */
2353 png_combine_row(png_structp png_ptr, png_bytep row, int mask)
2355 png_debug(1, "in png_combine_row\n");
2358 png_memcpy(row, png_ptr->row_buf + 1,
2359 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
2363 switch (png_ptr->row_info.pixel_depth)
2367 png_bytep sp = png_ptr->row_buf + 1;
2369 int s_inc, s_start, s_end;
2373 png_uint_32 row_width = png_ptr->width;
2375 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2376 if (png_ptr->transformations & PNG_PACKSWAP)
2392 for (i = 0; i < row_width; i++)
2398 value = (*sp >> shift) & 0x01;
2399 *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2400 *dp |= (png_byte)(value << shift);
2421 png_bytep sp = png_ptr->row_buf + 1;
2423 int s_start, s_end, s_inc;
2427 png_uint_32 row_width = png_ptr->width;
2430 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2431 if (png_ptr->transformations & PNG_PACKSWAP)
2447 for (i = 0; i < row_width; i++)
2451 value = (*sp >> shift) & 0x03;
2452 *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2453 *dp |= (png_byte)(value << shift);
2473 png_bytep sp = png_ptr->row_buf + 1;
2475 int s_start, s_end, s_inc;
2479 png_uint_32 row_width = png_ptr->width;
2482 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2483 if (png_ptr->transformations & PNG_PACKSWAP)
2498 for (i = 0; i < row_width; i++)
2502 value = (*sp >> shift) & 0xf;
2503 *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2504 *dp |= (png_byte)(value << shift);
2524 png_bytep sp = png_ptr->row_buf + 1;
2526 png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
2528 png_uint_32 row_width = png_ptr->width;
2532 for (i = 0; i < row_width; i++)
2536 png_memcpy(dp, sp, pixel_bytes);
2553 #ifdef PNG_READ_INTERLACING_SUPPORTED
2554 /* OLD pre-1.0.9 interface:
2555 void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
2556 png_uint_32 transformations)
2559 png_do_read_interlace(png_structp png_ptr)
2561 png_row_infop row_info = &(png_ptr->row_info);
2562 png_bytep row = png_ptr->row_buf + 1;
2563 int pass = png_ptr->pass;
2564 png_uint_32 transformations = png_ptr->transformations;
2565 #ifdef PNG_USE_LOCAL_ARRAYS
2566 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2567 /* offset to next interlace block */
2568 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2571 png_debug(1, "in png_do_read_interlace\n");
2572 if (row != NULL && row_info != NULL)
2574 png_uint_32 final_width;
2576 final_width = row_info->width * png_pass_inc[pass];
2578 switch (row_info->pixel_depth)
2582 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
2583 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
2585 int s_start, s_end, s_inc;
2586 int jstop = png_pass_inc[pass];
2591 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2592 if (transformations & PNG_PACKSWAP)
2594 sshift = (int)((row_info->width + 7) & 0x07);
2595 dshift = (int)((final_width + 7) & 0x07);
2603 sshift = 7 - (int)((row_info->width + 7) & 0x07);
2604 dshift = 7 - (int)((final_width + 7) & 0x07);
2610 for (i = 0; i < row_info->width; i++)
2612 v = (png_byte)((*sp >> sshift) & 0x01);
2613 for (j = 0; j < jstop; j++)
2615 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
2616 *dp |= (png_byte)(v << dshift);
2617 if (dshift == s_end)
2625 if (sshift == s_end)
2637 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
2638 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
2640 int s_start, s_end, s_inc;
2641 int jstop = png_pass_inc[pass];
2644 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2645 if (transformations & PNG_PACKSWAP)
2647 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
2648 dshift = (int)(((final_width + 3) & 0x03) << 1);
2656 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
2657 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
2663 for (i = 0; i < row_info->width; i++)
2668 v = (png_byte)((*sp >> sshift) & 0x03);
2669 for (j = 0; j < jstop; j++)
2671 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
2672 *dp |= (png_byte)(v << dshift);
2673 if (dshift == s_end)
2681 if (sshift == s_end)
2693 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
2694 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
2696 int s_start, s_end, s_inc;
2698 int jstop = png_pass_inc[pass];
2700 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2701 if (transformations & PNG_PACKSWAP)
2703 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
2704 dshift = (int)(((final_width + 1) & 0x01) << 2);
2712 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
2713 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
2719 for (i = 0; i < row_info->width; i++)
2721 png_byte v = (png_byte)((*sp >> sshift) & 0xf);
2724 for (j = 0; j < jstop; j++)
2726 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
2727 *dp |= (png_byte)(v << dshift);
2728 if (dshift == s_end)
2736 if (sshift == s_end)
2748 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
2749 png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
2750 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
2752 int jstop = png_pass_inc[pass];
2755 for (i = 0; i < row_info->width; i++)
2760 png_memcpy(v, sp, pixel_bytes);
2761 for (j = 0; j < jstop; j++)
2763 png_memcpy(dp, v, pixel_bytes);
2771 row_info->width = final_width;
2772 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
2774 #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
2775 transformations = transformations; /* silence compiler warning */
2778 #endif /* PNG_READ_INTERLACING_SUPPORTED */
2781 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
2782 png_bytep prev_row, int filter)
2784 png_debug(1, "in png_read_filter_row\n");
2785 png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
2788 case PNG_FILTER_VALUE_NONE:
2790 case PNG_FILTER_VALUE_SUB:
2793 png_uint_32 istop = row_info->rowbytes;
2794 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2795 png_bytep rp = row + bpp;
2798 for (i = bpp; i < istop; i++)
2800 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
2805 case PNG_FILTER_VALUE_UP:
2808 png_uint_32 istop = row_info->rowbytes;
2810 png_bytep pp = prev_row;
2812 for (i = 0; i < istop; i++)
2814 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
2819 case PNG_FILTER_VALUE_AVG:
2823 png_bytep pp = prev_row;
2825 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2826 png_uint_32 istop = row_info->rowbytes - bpp;
2828 for (i = 0; i < bpp; i++)
2830 *rp = (png_byte)(((int)(*rp) +
2831 ((int)(*pp++) / 2 )) & 0xff);
2835 for (i = 0; i < istop; i++)
2837 *rp = (png_byte)(((int)(*rp) +
2838 (int)(*pp++ + *lp++) / 2 ) & 0xff);
2843 case PNG_FILTER_VALUE_PAETH:
2847 png_bytep pp = prev_row;
2849 png_bytep cp = prev_row;
2850 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2851 png_uint_32 istop=row_info->rowbytes - bpp;
2853 for (i = 0; i < bpp; i++)
2855 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
2859 for (i = 0; i < istop; i++) /* use leftover rp,pp */
2861 int a, b, c, pa, pb, pc, p;
2875 pa = p < 0 ? -p : p;
2876 pb = pc < 0 ? -pc : pc;
2877 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2881 if (pa <= pb && pa <= pc)
2889 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
2891 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
2897 png_warning(png_ptr, "Ignoring bad adaptive filter type");
2904 png_read_finish_row(png_structp png_ptr)
2906 #ifdef PNG_USE_LOCAL_ARRAYS
2907 #ifdef PNG_READ_INTERLACING_SUPPORTED
2908 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2910 /* start of interlace block */
2911 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2913 /* offset to next interlace block */
2914 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2916 /* start of interlace block in the y direction */
2917 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2919 /* offset to next interlace block in the y direction */
2920 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
2921 #endif /* PNG_READ_INTERLACING_SUPPORTED */
2924 png_debug(1, "in png_read_finish_row\n");
2925 png_ptr->row_number++;
2926 if (png_ptr->row_number < png_ptr->num_rows)
2929 #ifdef PNG_READ_INTERLACING_SUPPORTED
2930 if (png_ptr->interlaced)
2932 png_ptr->row_number = 0;
2933 png_memset_check(png_ptr, png_ptr->prev_row, 0,
2934 png_ptr->rowbytes + 1);
2938 if (png_ptr->pass >= 7)
2940 png_ptr->iwidth = (png_ptr->width +
2941 png_pass_inc[png_ptr->pass] - 1 -
2942 png_pass_start[png_ptr->pass]) /
2943 png_pass_inc[png_ptr->pass];
2945 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
2946 png_ptr->iwidth) + 1;
2948 if (!(png_ptr->transformations & PNG_INTERLACE))
2950 png_ptr->num_rows = (png_ptr->height +
2951 png_pass_yinc[png_ptr->pass] - 1 -
2952 png_pass_ystart[png_ptr->pass]) /
2953 png_pass_yinc[png_ptr->pass];
2954 if (!(png_ptr->num_rows))
2957 else /* if (png_ptr->transformations & PNG_INTERLACE) */
2959 } while (png_ptr->iwidth == 0);
2961 if (png_ptr->pass < 7)
2964 #endif /* PNG_READ_INTERLACING_SUPPORTED */
2966 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
2968 #ifdef PNG_USE_LOCAL_ARRAYS
2974 png_ptr->zstream.next_out = (Byte *)&extra;
2975 png_ptr->zstream.avail_out = (uInt)1;
2978 if (!(png_ptr->zstream.avail_in))
2980 while (!png_ptr->idat_size)
2982 png_byte chunk_length[4];
2984 png_crc_finish(png_ptr, 0);
2986 png_read_data(png_ptr, chunk_length, 4);
2987 png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
2988 png_reset_crc(png_ptr);
2989 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
2990 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
2991 png_error(png_ptr, "Not enough image data");
2994 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
2995 png_ptr->zstream.next_in = png_ptr->zbuf;
2996 if (png_ptr->zbuf_size > png_ptr->idat_size)
2997 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
2998 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
2999 png_ptr->idat_size -= png_ptr->zstream.avail_in;
3001 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
3002 if (ret == Z_STREAM_END)
3004 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
3006 png_warning(png_ptr, "Extra compressed data");
3007 png_ptr->mode |= PNG_AFTER_IDAT;
3008 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3012 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
3013 "Decompression Error");
3015 if (!(png_ptr->zstream.avail_out))
3017 png_warning(png_ptr, "Extra compressed data.");
3018 png_ptr->mode |= PNG_AFTER_IDAT;
3019 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3024 png_ptr->zstream.avail_out = 0;
3027 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
3028 png_warning(png_ptr, "Extra compression data");
3030 inflateReset(&png_ptr->zstream);
3032 png_ptr->mode |= PNG_AFTER_IDAT;
3036 png_read_start_row(png_structp png_ptr)
3038 #ifdef PNG_USE_LOCAL_ARRAYS
3039 #ifdef PNG_READ_INTERLACING_SUPPORTED
3040 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3042 /* start of interlace block */
3043 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
3045 /* offset to next interlace block */
3046 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3048 /* start of interlace block in the y direction */
3049 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
3051 /* offset to next interlace block in the y direction */
3052 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
3056 int max_pixel_depth;
3057 png_size_t row_bytes;
3059 png_debug(1, "in png_read_start_row\n");
3060 png_ptr->zstream.avail_in = 0;
3061 png_init_read_transformations(png_ptr);
3062 #ifdef PNG_READ_INTERLACING_SUPPORTED
3063 if (png_ptr->interlaced)
3065 if (!(png_ptr->transformations & PNG_INTERLACE))
3066 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
3067 png_pass_ystart[0]) / png_pass_yinc[0];
3069 png_ptr->num_rows = png_ptr->height;
3071 png_ptr->iwidth = (png_ptr->width +
3072 png_pass_inc[png_ptr->pass] - 1 -
3073 png_pass_start[png_ptr->pass]) /
3074 png_pass_inc[png_ptr->pass];
3076 png_ptr->irowbytes =
3077 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
3080 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3082 png_ptr->num_rows = png_ptr->height;
3083 png_ptr->iwidth = png_ptr->width;
3084 png_ptr->irowbytes = png_ptr->rowbytes + 1;
3086 max_pixel_depth = png_ptr->pixel_depth;
3088 #if defined(PNG_READ_PACK_SUPPORTED)
3089 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
3090 max_pixel_depth = 8;
3093 #if defined(PNG_READ_EXPAND_SUPPORTED)
3094 if (png_ptr->transformations & PNG_EXPAND)
3096 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3098 if (png_ptr->num_trans)
3099 max_pixel_depth = 32;
3101 max_pixel_depth = 24;
3103 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3105 if (max_pixel_depth < 8)
3106 max_pixel_depth = 8;
3107 if (png_ptr->num_trans)
3108 max_pixel_depth *= 2;
3110 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3112 if (png_ptr->num_trans)
3114 max_pixel_depth *= 4;
3115 max_pixel_depth /= 3;
3121 #if defined(PNG_READ_FILLER_SUPPORTED)
3122 if (png_ptr->transformations & (PNG_FILLER))
3124 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
3125 max_pixel_depth = 32;
3126 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
3128 if (max_pixel_depth <= 8)
3129 max_pixel_depth = 16;
3131 max_pixel_depth = 32;
3133 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
3135 if (max_pixel_depth <= 32)
3136 max_pixel_depth = 32;
3138 max_pixel_depth = 64;
3143 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
3144 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3147 #if defined(PNG_READ_EXPAND_SUPPORTED)
3148 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
3150 #if defined(PNG_READ_FILLER_SUPPORTED)
3151 (png_ptr->transformations & (PNG_FILLER)) ||
3153 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3155 if (max_pixel_depth <= 16)
3156 max_pixel_depth = 32;
3158 max_pixel_depth = 64;
3162 if (max_pixel_depth <= 8)
3164 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3165 max_pixel_depth = 32;
3167 max_pixel_depth = 24;
3169 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3170 max_pixel_depth = 64;
3172 max_pixel_depth = 48;
3177 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3178 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3179 if (png_ptr->transformations & PNG_USER_TRANSFORM)
3181 int user_pixel_depth = png_ptr->user_transform_depth*
3182 png_ptr->user_transform_channels;
3183 if (user_pixel_depth > max_pixel_depth)
3184 max_pixel_depth=user_pixel_depth;
3188 /* align the width on the next larger 8 pixels. Mainly used
3190 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
3191 /* calculate the maximum bytes needed, adding a byte and a pixel
3192 for safety's sake */
3193 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
3194 1 + ((max_pixel_depth + 7) >> 3);
3195 #ifdef PNG_MAX_MALLOC_64K
3196 if (row_bytes > (png_uint_32)65536L)
3197 png_error(png_ptr, "This image requires a row greater than 64KB");
3200 if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
3202 png_free(png_ptr, png_ptr->big_row_buf);
3203 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
3204 png_ptr->row_buf = png_ptr->big_row_buf+32;
3205 png_ptr->old_big_row_buf_size = row_bytes+64;
3208 #ifdef PNG_MAX_MALLOC_64K
3209 if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
3210 png_error(png_ptr, "This image requires a row greater than 64KB");
3212 if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
3213 png_error(png_ptr, "Row has too many bytes to allocate in memory.");
3215 if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
3217 png_free(png_ptr, png_ptr->prev_row);
3218 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
3219 png_ptr->rowbytes + 1));
3220 png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
3223 png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3225 png_debug1(3, "width = %lu,\n", png_ptr->width);
3226 png_debug1(3, "height = %lu,\n", png_ptr->height);
3227 png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
3228 png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
3229 png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
3230 png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
3232 png_ptr->flags |= PNG_FLAG_ROW_INIT;
3234 #endif /* PNG_READ_SUPPORTED */