2 /* pngrutil.c - utilities to read a PNG file
4 * Last changed in libpng 1.2.45 [July 7, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains routines that are only called from within
14 * libpng itself during the course of reading an image.
18 #define PNG_NO_PEDANTIC_WARNINGS
20 #ifdef PNG_READ_SUPPORTED
22 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
23 # define WIN32_WCE_OLD
26 #ifdef PNG_FLOATING_POINT_SUPPORTED
28 /* The strtod() function is not supported on WindowsCE */
29 __inline
double png_strtod(png_structp png_ptr
, PNG_CONST
char *nptr
,
36 len
= MultiByteToWideChar(CP_ACP
, 0, nptr
, -1, NULL
, 0);
37 str
= (wchar_t *)png_malloc(png_ptr
, len
* png_sizeof(wchar_t));
40 MultiByteToWideChar(CP_ACP
, 0, nptr
, -1, str
, len
);
41 result
= wcstod(str
, &end
);
42 len
= WideCharToMultiByte(CP_ACP
, 0, end
, -1, NULL
, 0, NULL
, NULL
);
43 *endptr
= (char *)nptr
+ (png_strlen(nptr
) - len
+ 1);
44 png_free(png_ptr
, str
);
49 # define png_strtod(p,a,b) strtod(a,b)
54 png_get_uint_31(png_structp png_ptr
, png_bytep buf
)
56 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
57 png_uint_32 i
= png_get_uint_32(buf
);
59 /* Avoid an extra function call by inlining the result. */
60 png_uint_32 i
= ((png_uint_32
)(*buf
) << 24) +
61 ((png_uint_32
)(*(buf
+ 1)) << 16) +
62 ((png_uint_32
)(*(buf
+ 2)) << 8) +
63 (png_uint_32
)(*(buf
+ 3));
65 if (i
> PNG_UINT_31_MAX
)
66 png_error(png_ptr
, "PNG unsigned integer out of range.");
69 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
70 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
72 png_get_uint_32(png_bytep buf
)
74 png_uint_32 i
= ((png_uint_32
)(*buf
) << 24) +
75 ((png_uint_32
)(*(buf
+ 1)) << 16) +
76 ((png_uint_32
)(*(buf
+ 2)) << 8) +
77 (png_uint_32
)(*(buf
+ 3));
82 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
83 * data is stored in the PNG file in two's complement format, and it is
84 * assumed that the machine format for signed integers is the same.
87 png_get_int_32(png_bytep buf
)
89 png_int_32 i
= ((png_int_32
)(*buf
) << 24) +
90 ((png_int_32
)(*(buf
+ 1)) << 16) +
91 ((png_int_32
)(*(buf
+ 2)) << 8) +
92 (png_int_32
)(*(buf
+ 3));
97 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
99 png_get_uint_16(png_bytep buf
)
101 png_uint_16 i
= (png_uint_16
)(((png_uint_16
)(*buf
) << 8) +
102 (png_uint_16
)(*(buf
+ 1)));
106 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
108 /* Read the chunk header (length + type name).
109 * Put the type name into png_ptr->chunk_name, and return the length.
111 png_uint_32
/* PRIVATE */
112 png_read_chunk_header(png_structp png_ptr
)
117 /* Read the length and the chunk name */
118 png_read_data(png_ptr
, buf
, 8);
119 length
= png_get_uint_31(png_ptr
, buf
);
121 /* Put the chunk name into png_ptr->chunk_name */
122 png_memcpy(png_ptr
->chunk_name
, buf
+ 4, 4);
124 png_debug2(0, "Reading %s chunk, length = %lu",
125 png_ptr
->chunk_name
, length
);
127 /* Reset the crc and run it over the chunk name */
128 png_reset_crc(png_ptr
);
129 png_calculate_crc(png_ptr
, png_ptr
->chunk_name
, 4);
131 /* Check to see if chunk name is valid */
132 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
137 /* Read data, and (optionally) run it through the CRC. */
139 png_crc_read(png_structp png_ptr
, png_bytep buf
, png_size_t length
)
143 png_read_data(png_ptr
, buf
, length
);
144 png_calculate_crc(png_ptr
, buf
, length
);
147 /* Optionally skip data and then check the CRC. Depending on whether we
148 * are reading a ancillary or critical chunk, and how the program has set
149 * things up, we may calculate the CRC on the data and print a message.
150 * Returns '1' if there was a CRC error, '0' otherwise.
153 png_crc_finish(png_structp png_ptr
, png_uint_32 skip
)
156 png_size_t istop
= png_ptr
->zbuf_size
;
158 for (i
= (png_size_t
)skip
; i
> istop
; i
-= istop
)
160 png_crc_read(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
164 png_crc_read(png_ptr
, png_ptr
->zbuf
, i
);
167 if (png_crc_error(png_ptr
))
169 if (((png_ptr
->chunk_name
[0] & 0x20) && /* Ancillary */
170 !(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
)) ||
171 (!(png_ptr
->chunk_name
[0] & 0x20) && /* Critical */
172 (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_USE
)))
174 png_chunk_warning(png_ptr
, "CRC error");
178 png_chunk_error(png_ptr
, "CRC error");
186 /* Compare the CRC stored in the PNG file with that calculated by libpng from
187 * the data it has read thus far.
190 png_crc_error(png_structp png_ptr
)
192 png_byte crc_bytes
[4];
196 if (png_ptr
->chunk_name
[0] & 0x20) /* ancillary */
198 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_MASK
) ==
199 (PNG_FLAG_CRC_ANCILLARY_USE
| PNG_FLAG_CRC_ANCILLARY_NOWARN
))
204 if (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_IGNORE
)
208 png_read_data(png_ptr
, crc_bytes
, 4);
212 crc
= png_get_uint_32(crc_bytes
);
213 return ((int)(crc
!= png_ptr
->crc
));
219 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
220 defined(PNG_READ_iCCP_SUPPORTED)
222 png_inflate(png_structp png_ptr
, const png_byte
*data
, png_size_t size
,
223 png_bytep output
, png_size_t output_size
)
225 png_size_t count
= 0;
227 png_ptr
->zstream
.next_in
= (png_bytep
)data
; /* const_cast: VALID */
228 png_ptr
->zstream
.avail_in
= size
;
234 /* Reset the output buffer each time round - we empty it
235 * after every inflate call.
237 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
238 png_ptr
->zstream
.avail_out
= png_ptr
->zbuf_size
;
240 ret
= inflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
241 avail
= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
243 /* First copy/count any new output - but only if we didn't
246 if ((ret
== Z_OK
|| ret
== Z_STREAM_END
) && avail
> 0)
248 if (output
!= 0 && output_size
> count
)
250 png_size_t copy
= output_size
- count
;
251 if ((png_size_t
) avail
< copy
) copy
= (png_size_t
) avail
;
252 png_memcpy(output
+ count
, png_ptr
->zbuf
, copy
);
260 /* Termination conditions - always reset the zstream, it
261 * must be left in inflateInit state.
263 png_ptr
->zstream
.avail_in
= 0;
264 inflateReset(&png_ptr
->zstream
);
266 if (ret
== Z_STREAM_END
)
267 return count
; /* NOTE: may be zero. */
269 /* Now handle the error codes - the API always returns 0
270 * and the error message is dumped into the uncompressed
271 * buffer if available.
275 if (png_ptr
->zstream
.msg
!= 0)
276 msg
= png_ptr
->zstream
.msg
;
279 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
285 msg
= "Buffer error in compressed datastream in %s chunk";
288 msg
= "Data error in compressed datastream in %s chunk";
291 msg
= "Incomplete compressed datastream in %s chunk";
295 png_snprintf(umsg
, sizeof umsg
, msg
, png_ptr
->chunk_name
);
298 msg
= "Damaged compressed datastream in chunk other than IDAT";
302 png_warning(png_ptr
, msg
);
305 /* 0 means an error - notice that this code simple ignores
306 * zero length compressed chunks as a result.
313 * Decompress trailing data in a chunk. The assumption is that chunkdata
314 * points at an allocated area holding the contents of a chunk with a
315 * trailing compressed part. What we get back is an allocated area
316 * holding the original prefix part and an uncompressed version of the
317 * trailing part (the malloc area passed in is freed).
320 png_decompress_chunk(png_structp png_ptr
, int comp_type
,
321 png_size_t chunklength
,
322 png_size_t prefix_size
, png_size_t
*newlength
)
324 /* The caller should guarantee this */
325 if (prefix_size
> chunklength
)
327 /* The recovery is to delete the chunk. */
328 png_warning(png_ptr
, "invalid chunklength");
329 prefix_size
= 0; /* To delete everything */
332 else if (comp_type
== PNG_COMPRESSION_TYPE_BASE
)
334 png_size_t expanded_size
= png_inflate(png_ptr
,
335 (png_bytep
)(png_ptr
->chunkdata
+ prefix_size
),
336 chunklength
- prefix_size
,
337 0/*output*/, 0/*output size*/);
339 /* Now check the limits on this chunk - if the limit fails the
340 * compressed data will be removed, the prefix will remain.
342 #ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
343 if (png_ptr
->user_chunk_malloc_max
&&
344 (prefix_size
+ expanded_size
>= png_ptr
->user_chunk_malloc_max
- 1))
346 # ifdef PNG_USER_CHUNK_MALLOC_MAX
347 if ((PNG_USER_CHUNK_MALLOC_MAX
> 0) &&
348 prefix_size
+ expanded_size
>= PNG_USER_CHUNK_MALLOC_MAX
- 1)
351 png_warning(png_ptr
, "Exceeded size limit while expanding chunk");
353 /* If the size is zero either there was an error and a message
354 * has already been output (warning) or the size really is zero
355 * and we have nothing to do - the code will exit through the
358 #if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \
359 defined(PNG_USER_CHUNK_MALLOC_MAX)
362 if (expanded_size
> 0)
364 /* Success (maybe) - really uncompress the chunk. */
365 png_size_t new_size
= 0;
366 png_charp text
= NULL
;
367 /* Need to check for both truncation (64-bit platforms) and integer
370 if (prefix_size
+ expanded_size
> prefix_size
&&
371 prefix_size
+ expanded_size
< 0xffffffffU
)
373 text
= png_malloc_warn(png_ptr
, prefix_size
+ expanded_size
+ 1);
378 png_memcpy(text
, png_ptr
->chunkdata
, prefix_size
);
379 new_size
= png_inflate(png_ptr
,
380 (png_bytep
)(png_ptr
->chunkdata
+ prefix_size
),
381 chunklength
- prefix_size
,
382 (png_bytep
)(text
+ prefix_size
), expanded_size
);
383 text
[prefix_size
+ expanded_size
] = 0; /* just in case */
385 if (new_size
== expanded_size
)
387 png_free(png_ptr
, png_ptr
->chunkdata
);
388 png_ptr
->chunkdata
= text
;
389 *newlength
= prefix_size
+ expanded_size
;
390 return; /* The success return! */
393 png_warning(png_ptr
, "png_inflate logic error");
394 png_free(png_ptr
, text
);
397 png_warning(png_ptr
, "Not enough memory to decompress chunk.");
401 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
403 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
406 png_snprintf(umsg
, sizeof umsg
, "Unknown zTXt compression type %d",
408 png_warning(png_ptr
, umsg
);
410 png_warning(png_ptr
, "Unknown zTXt compression type");
413 /* The recovery is to simply drop the data. */
416 /* Generic error return - leave the prefix, delete the compressed
417 * data, reallocate the chunkdata to remove the potentially large
418 * amount of compressed data.
421 png_charp text
= png_malloc_warn(png_ptr
, prefix_size
+ 1);
425 png_memcpy(text
, png_ptr
->chunkdata
, prefix_size
);
426 png_free(png_ptr
, png_ptr
->chunkdata
);
427 png_ptr
->chunkdata
= text
;
429 /* This is an extra zero in the 'uncompressed' part. */
430 *(png_ptr
->chunkdata
+ prefix_size
) = 0x00;
432 /* Ignore a malloc error here - it is safe. */
435 *newlength
= prefix_size
;
439 /* Read and check the IDHR chunk */
441 png_handle_IHDR(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
444 png_uint_32 width
, height
;
445 int bit_depth
, color_type
, compression_type
, filter_type
;
448 png_debug(1, "in png_handle_IHDR");
450 if (png_ptr
->mode
& PNG_HAVE_IHDR
)
451 png_error(png_ptr
, "Out of place IHDR");
453 /* Check the length */
455 png_error(png_ptr
, "Invalid IHDR chunk");
457 png_ptr
->mode
|= PNG_HAVE_IHDR
;
459 png_crc_read(png_ptr
, buf
, 13);
460 png_crc_finish(png_ptr
, 0);
462 width
= png_get_uint_31(png_ptr
, buf
);
463 height
= png_get_uint_31(png_ptr
, buf
+ 4);
466 compression_type
= buf
[10];
467 filter_type
= buf
[11];
468 interlace_type
= buf
[12];
470 /* Set internal variables */
471 png_ptr
->width
= width
;
472 png_ptr
->height
= height
;
473 png_ptr
->bit_depth
= (png_byte
)bit_depth
;
474 png_ptr
->interlaced
= (png_byte
)interlace_type
;
475 png_ptr
->color_type
= (png_byte
)color_type
;
476 #ifdef PNG_MNG_FEATURES_SUPPORTED
477 png_ptr
->filter_type
= (png_byte
)filter_type
;
479 png_ptr
->compression_type
= (png_byte
)compression_type
;
481 /* Find number of channels */
482 switch (png_ptr
->color_type
)
484 case PNG_COLOR_TYPE_GRAY
:
485 case PNG_COLOR_TYPE_PALETTE
:
486 png_ptr
->channels
= 1;
489 case PNG_COLOR_TYPE_RGB
:
490 png_ptr
->channels
= 3;
493 case PNG_COLOR_TYPE_GRAY_ALPHA
:
494 png_ptr
->channels
= 2;
497 case PNG_COLOR_TYPE_RGB_ALPHA
:
498 png_ptr
->channels
= 4;
502 /* Set up other useful info */
503 png_ptr
->pixel_depth
= (png_byte
)(png_ptr
->bit_depth
*
505 png_ptr
->rowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->width
);
506 png_debug1(3, "bit_depth = %d", png_ptr
->bit_depth
);
507 png_debug1(3, "channels = %d", png_ptr
->channels
);
508 png_debug1(3, "rowbytes = %lu", png_ptr
->rowbytes
);
509 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, bit_depth
,
510 color_type
, interlace_type
, compression_type
, filter_type
);
513 /* Read and check the palette */
515 png_handle_PLTE(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
517 png_color palette
[PNG_MAX_PALETTE_LENGTH
];
519 #ifdef PNG_POINTER_INDEXING_SUPPORTED
523 png_debug(1, "in png_handle_PLTE");
525 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
526 png_error(png_ptr
, "Missing IHDR before PLTE");
528 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
530 png_warning(png_ptr
, "Invalid PLTE after IDAT");
531 png_crc_finish(png_ptr
, length
);
535 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
536 png_error(png_ptr
, "Duplicate PLTE chunk");
538 png_ptr
->mode
|= PNG_HAVE_PLTE
;
540 if (!(png_ptr
->color_type
&PNG_COLOR_MASK_COLOR
))
543 "Ignoring PLTE chunk in grayscale PNG");
544 png_crc_finish(png_ptr
, length
);
547 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
548 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
550 png_crc_finish(png_ptr
, length
);
555 if (length
> 3*PNG_MAX_PALETTE_LENGTH
|| length
% 3)
557 if (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
559 png_warning(png_ptr
, "Invalid palette chunk");
560 png_crc_finish(png_ptr
, length
);
566 png_error(png_ptr
, "Invalid palette chunk");
570 num
= (int)length
/ 3;
572 #ifdef PNG_POINTER_INDEXING_SUPPORTED
573 for (i
= 0, pal_ptr
= palette
; i
< num
; i
++, pal_ptr
++)
577 png_crc_read(png_ptr
, buf
, 3);
578 pal_ptr
->red
= buf
[0];
579 pal_ptr
->green
= buf
[1];
580 pal_ptr
->blue
= buf
[2];
583 for (i
= 0; i
< num
; i
++)
587 png_crc_read(png_ptr
, buf
, 3);
588 /* Don't depend upon png_color being any order */
589 palette
[i
].red
= buf
[0];
590 palette
[i
].green
= buf
[1];
591 palette
[i
].blue
= buf
[2];
595 /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
596 * whatever the normal CRC configuration tells us. However, if we
597 * have an RGB image, the PLTE can be considered ancillary, so
598 * we will act as though it is.
600 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
601 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
604 png_crc_finish(png_ptr
, 0);
606 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
607 else if (png_crc_error(png_ptr
)) /* Only if we have a CRC error */
609 /* If we don't want to use the data from an ancillary chunk,
610 we have two options: an error abort, or a warning and we
611 ignore the data in this chunk (which should be OK, since
612 it's considered ancillary for a RGB or RGBA image). */
613 if (!(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_USE
))
615 if (png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
)
617 png_chunk_error(png_ptr
, "CRC error");
621 png_chunk_warning(png_ptr
, "CRC error");
625 /* Otherwise, we (optionally) emit a warning and use the chunk. */
626 else if (!(png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_NOWARN
))
628 png_chunk_warning(png_ptr
, "CRC error");
633 png_set_PLTE(png_ptr
, info_ptr
, palette
, num
);
635 #ifdef PNG_READ_tRNS_SUPPORTED
636 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
638 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
))
640 if (png_ptr
->num_trans
> (png_uint_16
)num
)
642 png_warning(png_ptr
, "Truncating incorrect tRNS chunk length");
643 png_ptr
->num_trans
= (png_uint_16
)num
;
645 if (info_ptr
->num_trans
> (png_uint_16
)num
)
647 png_warning(png_ptr
, "Truncating incorrect info tRNS chunk length");
648 info_ptr
->num_trans
= (png_uint_16
)num
;
657 png_handle_IEND(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
659 png_debug(1, "in png_handle_IEND");
661 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
) || !(png_ptr
->mode
& PNG_HAVE_IDAT
))
663 png_error(png_ptr
, "No image in file");
666 png_ptr
->mode
|= (PNG_AFTER_IDAT
| PNG_HAVE_IEND
);
670 png_warning(png_ptr
, "Incorrect IEND chunk length");
672 png_crc_finish(png_ptr
, length
);
674 info_ptr
= info_ptr
; /* Quiet compiler warnings about unused info_ptr */
677 #ifdef PNG_READ_gAMA_SUPPORTED
679 png_handle_gAMA(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
681 png_fixed_point igamma
;
682 #ifdef PNG_FLOATING_POINT_SUPPORTED
687 png_debug(1, "in png_handle_gAMA");
689 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
690 png_error(png_ptr
, "Missing IHDR before gAMA");
691 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
693 png_warning(png_ptr
, "Invalid gAMA after IDAT");
694 png_crc_finish(png_ptr
, length
);
697 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
698 /* Should be an error, but we can cope with it */
699 png_warning(png_ptr
, "Out of place gAMA chunk");
701 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_gAMA
)
702 #ifdef PNG_READ_sRGB_SUPPORTED
703 && !(info_ptr
->valid
& PNG_INFO_sRGB
)
707 png_warning(png_ptr
, "Duplicate gAMA chunk");
708 png_crc_finish(png_ptr
, length
);
714 png_warning(png_ptr
, "Incorrect gAMA chunk length");
715 png_crc_finish(png_ptr
, length
);
719 png_crc_read(png_ptr
, buf
, 4);
720 if (png_crc_finish(png_ptr
, 0))
723 igamma
= (png_fixed_point
)png_get_uint_32(buf
);
724 /* Check for zero gamma */
728 "Ignoring gAMA chunk with gamma=0");
732 #ifdef PNG_READ_sRGB_SUPPORTED
733 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sRGB
))
734 if (PNG_OUT_OF_RANGE(igamma
, 45500L, 500))
737 "Ignoring incorrect gAMA value when sRGB is also present");
738 #ifdef PNG_CONSOLE_IO_SUPPORTED
739 fprintf(stderr
, "gamma = (%d/100000)", (int)igamma
);
743 #endif /* PNG_READ_sRGB_SUPPORTED */
745 #ifdef PNG_FLOATING_POINT_SUPPORTED
746 file_gamma
= (float)igamma
/ (float)100000.0;
747 # ifdef PNG_READ_GAMMA_SUPPORTED
748 png_ptr
->gamma
= file_gamma
;
750 png_set_gAMA(png_ptr
, info_ptr
, file_gamma
);
752 #ifdef PNG_FIXED_POINT_SUPPORTED
753 png_set_gAMA_fixed(png_ptr
, info_ptr
, igamma
);
758 #ifdef PNG_READ_sBIT_SUPPORTED
760 png_handle_sBIT(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
765 png_debug(1, "in png_handle_sBIT");
767 buf
[0] = buf
[1] = buf
[2] = buf
[3] = 0;
769 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
770 png_error(png_ptr
, "Missing IHDR before sBIT");
771 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
773 png_warning(png_ptr
, "Invalid sBIT after IDAT");
774 png_crc_finish(png_ptr
, length
);
777 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
779 /* Should be an error, but we can cope with it */
780 png_warning(png_ptr
, "Out of place sBIT chunk");
782 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sBIT
))
784 png_warning(png_ptr
, "Duplicate sBIT chunk");
785 png_crc_finish(png_ptr
, length
);
789 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
792 truelen
= (png_size_t
)png_ptr
->channels
;
794 if (length
!= truelen
|| length
> 4)
796 png_warning(png_ptr
, "Incorrect sBIT chunk length");
797 png_crc_finish(png_ptr
, length
);
801 png_crc_read(png_ptr
, buf
, truelen
);
802 if (png_crc_finish(png_ptr
, 0))
805 if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
807 png_ptr
->sig_bit
.red
= buf
[0];
808 png_ptr
->sig_bit
.green
= buf
[1];
809 png_ptr
->sig_bit
.blue
= buf
[2];
810 png_ptr
->sig_bit
.alpha
= buf
[3];
814 png_ptr
->sig_bit
.gray
= buf
[0];
815 png_ptr
->sig_bit
.red
= buf
[0];
816 png_ptr
->sig_bit
.green
= buf
[0];
817 png_ptr
->sig_bit
.blue
= buf
[0];
818 png_ptr
->sig_bit
.alpha
= buf
[1];
820 png_set_sBIT(png_ptr
, info_ptr
, &(png_ptr
->sig_bit
));
824 #ifdef PNG_READ_cHRM_SUPPORTED
826 png_handle_cHRM(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
829 #ifdef PNG_FLOATING_POINT_SUPPORTED
830 float white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
;
832 png_fixed_point int_x_white
, int_y_white
, int_x_red
, int_y_red
, int_x_green
,
833 int_y_green
, int_x_blue
, int_y_blue
;
835 png_uint_32 uint_x
, uint_y
;
837 png_debug(1, "in png_handle_cHRM");
839 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
840 png_error(png_ptr
, "Missing IHDR before cHRM");
841 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
843 png_warning(png_ptr
, "Invalid cHRM after IDAT");
844 png_crc_finish(png_ptr
, length
);
847 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
848 /* Should be an error, but we can cope with it */
849 png_warning(png_ptr
, "Missing PLTE before cHRM");
851 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_cHRM
)
852 #ifdef PNG_READ_sRGB_SUPPORTED
853 && !(info_ptr
->valid
& PNG_INFO_sRGB
)
857 png_warning(png_ptr
, "Duplicate cHRM chunk");
858 png_crc_finish(png_ptr
, length
);
864 png_warning(png_ptr
, "Incorrect cHRM chunk length");
865 png_crc_finish(png_ptr
, length
);
869 png_crc_read(png_ptr
, buf
, 32);
870 if (png_crc_finish(png_ptr
, 0))
873 uint_x
= png_get_uint_32(buf
);
874 uint_y
= png_get_uint_32(buf
+ 4);
875 int_x_white
= (png_fixed_point
)uint_x
;
876 int_y_white
= (png_fixed_point
)uint_y
;
878 uint_x
= png_get_uint_32(buf
+ 8);
879 uint_y
= png_get_uint_32(buf
+ 12);
880 int_x_red
= (png_fixed_point
)uint_x
;
881 int_y_red
= (png_fixed_point
)uint_y
;
883 uint_x
= png_get_uint_32(buf
+ 16);
884 uint_y
= png_get_uint_32(buf
+ 20);
885 int_x_green
= (png_fixed_point
)uint_x
;
886 int_y_green
= (png_fixed_point
)uint_y
;
888 uint_x
= png_get_uint_32(buf
+ 24);
889 uint_y
= png_get_uint_32(buf
+ 28);
890 int_x_blue
= (png_fixed_point
)uint_x
;
891 int_y_blue
= (png_fixed_point
)uint_y
;
893 #ifdef PNG_FLOATING_POINT_SUPPORTED
894 white_x
= (float)int_x_white
/ (float)100000.0;
895 white_y
= (float)int_y_white
/ (float)100000.0;
896 red_x
= (float)int_x_red
/ (float)100000.0;
897 red_y
= (float)int_y_red
/ (float)100000.0;
898 green_x
= (float)int_x_green
/ (float)100000.0;
899 green_y
= (float)int_y_green
/ (float)100000.0;
900 blue_x
= (float)int_x_blue
/ (float)100000.0;
901 blue_y
= (float)int_y_blue
/ (float)100000.0;
904 #ifdef PNG_READ_sRGB_SUPPORTED
905 if ((info_ptr
!= NULL
) && (info_ptr
->valid
& PNG_INFO_sRGB
))
907 if (PNG_OUT_OF_RANGE(int_x_white
, 31270, 1000) ||
908 PNG_OUT_OF_RANGE(int_y_white
, 32900, 1000) ||
909 PNG_OUT_OF_RANGE(int_x_red
, 64000L, 1000) ||
910 PNG_OUT_OF_RANGE(int_y_red
, 33000, 1000) ||
911 PNG_OUT_OF_RANGE(int_x_green
, 30000, 1000) ||
912 PNG_OUT_OF_RANGE(int_y_green
, 60000L, 1000) ||
913 PNG_OUT_OF_RANGE(int_x_blue
, 15000, 1000) ||
914 PNG_OUT_OF_RANGE(int_y_blue
, 6000, 1000))
917 "Ignoring incorrect cHRM value when sRGB is also present");
918 #ifdef PNG_CONSOLE_IO_SUPPORTED
919 #ifdef PNG_FLOATING_POINT_SUPPORTED
920 fprintf(stderr
, "wx=%f, wy=%f, rx=%f, ry=%f\n",
921 white_x
, white_y
, red_x
, red_y
);
922 fprintf(stderr
, "gx=%f, gy=%f, bx=%f, by=%f\n",
923 green_x
, green_y
, blue_x
, blue_y
);
925 fprintf(stderr
, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
926 (long)int_x_white
, (long)int_y_white
,
927 (long)int_x_red
, (long)int_y_red
);
928 fprintf(stderr
, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
929 (long)int_x_green
, (long)int_y_green
,
930 (long)int_x_blue
, (long)int_y_blue
);
932 #endif /* PNG_CONSOLE_IO_SUPPORTED */
936 #endif /* PNG_READ_sRGB_SUPPORTED */
938 #ifdef PNG_FLOATING_POINT_SUPPORTED
939 png_set_cHRM(png_ptr
, info_ptr
,
940 white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
);
942 #ifdef PNG_FIXED_POINT_SUPPORTED
943 png_set_cHRM_fixed(png_ptr
, info_ptr
,
944 int_x_white
, int_y_white
, int_x_red
, int_y_red
, int_x_green
,
945 int_y_green
, int_x_blue
, int_y_blue
);
950 #ifdef PNG_READ_sRGB_SUPPORTED
952 png_handle_sRGB(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
957 png_debug(1, "in png_handle_sRGB");
959 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
960 png_error(png_ptr
, "Missing IHDR before sRGB");
961 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
963 png_warning(png_ptr
, "Invalid sRGB after IDAT");
964 png_crc_finish(png_ptr
, length
);
967 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
968 /* Should be an error, but we can cope with it */
969 png_warning(png_ptr
, "Out of place sRGB chunk");
971 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sRGB
))
973 png_warning(png_ptr
, "Duplicate sRGB chunk");
974 png_crc_finish(png_ptr
, length
);
980 png_warning(png_ptr
, "Incorrect sRGB chunk length");
981 png_crc_finish(png_ptr
, length
);
985 png_crc_read(png_ptr
, buf
, 1);
986 if (png_crc_finish(png_ptr
, 0))
990 /* Check for bad intent */
991 if (intent
>= PNG_sRGB_INTENT_LAST
)
993 png_warning(png_ptr
, "Unknown sRGB intent");
997 #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
998 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_gAMA
))
1000 png_fixed_point igamma
;
1001 #ifdef PNG_FIXED_POINT_SUPPORTED
1002 igamma
=info_ptr
->int_gamma
;
1004 # ifdef PNG_FLOATING_POINT_SUPPORTED
1005 igamma
=(png_fixed_point
)(info_ptr
->gamma
* 100000.);
1008 if (PNG_OUT_OF_RANGE(igamma
, 45500L, 500))
1010 png_warning(png_ptr
,
1011 "Ignoring incorrect gAMA value when sRGB is also present");
1012 #ifdef PNG_CONSOLE_IO_SUPPORTED
1013 # ifdef PNG_FIXED_POINT_SUPPORTED
1014 fprintf(stderr
, "incorrect gamma=(%d/100000)\n",
1015 (int)png_ptr
->int_gamma
);
1017 # ifdef PNG_FLOATING_POINT_SUPPORTED
1018 fprintf(stderr
, "incorrect gamma=%f\n", png_ptr
->gamma
);
1024 #endif /* PNG_READ_gAMA_SUPPORTED */
1026 #ifdef PNG_READ_cHRM_SUPPORTED
1027 #ifdef PNG_FIXED_POINT_SUPPORTED
1028 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_cHRM
))
1029 if (PNG_OUT_OF_RANGE(info_ptr
->int_x_white
, 31270, 1000) ||
1030 PNG_OUT_OF_RANGE(info_ptr
->int_y_white
, 32900, 1000) ||
1031 PNG_OUT_OF_RANGE(info_ptr
->int_x_red
, 64000L, 1000) ||
1032 PNG_OUT_OF_RANGE(info_ptr
->int_y_red
, 33000, 1000) ||
1033 PNG_OUT_OF_RANGE(info_ptr
->int_x_green
, 30000, 1000) ||
1034 PNG_OUT_OF_RANGE(info_ptr
->int_y_green
, 60000L, 1000) ||
1035 PNG_OUT_OF_RANGE(info_ptr
->int_x_blue
, 15000, 1000) ||
1036 PNG_OUT_OF_RANGE(info_ptr
->int_y_blue
, 6000, 1000))
1038 png_warning(png_ptr
,
1039 "Ignoring incorrect cHRM value when sRGB is also present");
1041 #endif /* PNG_FIXED_POINT_SUPPORTED */
1042 #endif /* PNG_READ_cHRM_SUPPORTED */
1044 png_set_sRGB_gAMA_and_cHRM(png_ptr
, info_ptr
, intent
);
1046 #endif /* PNG_READ_sRGB_SUPPORTED */
1048 #ifdef PNG_READ_iCCP_SUPPORTED
1050 png_handle_iCCP(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1051 /* Note: this does not properly handle chunks that are > 64K under DOS */
1053 png_byte compression_type
;
1056 png_uint_32 skip
= 0;
1057 png_uint_32 profile_size
, profile_length
;
1058 png_size_t slength
, prefix_length
, data_length
;
1060 png_debug(1, "in png_handle_iCCP");
1062 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1063 png_error(png_ptr
, "Missing IHDR before iCCP");
1064 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1066 png_warning(png_ptr
, "Invalid iCCP after IDAT");
1067 png_crc_finish(png_ptr
, length
);
1070 else if (png_ptr
->mode
& PNG_HAVE_PLTE
)
1071 /* Should be an error, but we can cope with it */
1072 png_warning(png_ptr
, "Out of place iCCP chunk");
1074 if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_iCCP
))
1076 png_warning(png_ptr
, "Duplicate iCCP chunk");
1077 png_crc_finish(png_ptr
, length
);
1081 #ifdef PNG_MAX_MALLOC_64K
1082 if (length
> (png_uint_32
)65535L)
1084 png_warning(png_ptr
, "iCCP chunk too large to fit in memory");
1085 skip
= length
- (png_uint_32
)65535L;
1086 length
= (png_uint_32
)65535L;
1090 png_free(png_ptr
, png_ptr
->chunkdata
);
1091 png_ptr
->chunkdata
= (png_charp
)png_malloc(png_ptr
, length
+ 1);
1092 slength
= (png_size_t
)length
;
1093 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
1095 if (png_crc_finish(png_ptr
, skip
))
1097 png_free(png_ptr
, png_ptr
->chunkdata
);
1098 png_ptr
->chunkdata
= NULL
;
1102 png_ptr
->chunkdata
[slength
] = 0x00;
1104 for (profile
= png_ptr
->chunkdata
; *profile
; profile
++)
1105 /* Empty loop to find end of name */ ;
1109 /* There should be at least one zero (the compression type byte)
1110 * following the separator, and we should be on it
1112 if ( profile
>= png_ptr
->chunkdata
+ slength
- 1)
1114 png_free(png_ptr
, png_ptr
->chunkdata
);
1115 png_ptr
->chunkdata
= NULL
;
1116 png_warning(png_ptr
, "Malformed iCCP chunk");
1120 /* Compression_type should always be zero */
1121 compression_type
= *profile
++;
1122 if (compression_type
)
1124 png_warning(png_ptr
, "Ignoring nonzero compression type in iCCP chunk");
1125 compression_type
= 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
1129 prefix_length
= profile
- png_ptr
->chunkdata
;
1130 png_decompress_chunk(png_ptr
, compression_type
,
1131 slength
, prefix_length
, &data_length
);
1133 profile_length
= data_length
- prefix_length
;
1135 if ( prefix_length
> data_length
|| profile_length
< 4)
1137 png_free(png_ptr
, png_ptr
->chunkdata
);
1138 png_ptr
->chunkdata
= NULL
;
1139 png_warning(png_ptr
, "Profile size field missing from iCCP chunk");
1143 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
1144 pC
= (png_bytep
)(png_ptr
->chunkdata
+ prefix_length
);
1145 profile_size
= ((*(pC
))<<24) |
1150 if (profile_size
< profile_length
)
1151 profile_length
= profile_size
;
1153 if (profile_size
> profile_length
)
1155 png_free(png_ptr
, png_ptr
->chunkdata
);
1156 png_ptr
->chunkdata
= NULL
;
1157 png_warning(png_ptr
, "Ignoring truncated iCCP profile.");
1161 png_set_iCCP(png_ptr
, info_ptr
, png_ptr
->chunkdata
,
1162 compression_type
, png_ptr
->chunkdata
+ prefix_length
, profile_length
);
1163 png_free(png_ptr
, png_ptr
->chunkdata
);
1164 png_ptr
->chunkdata
= NULL
;
1166 #endif /* PNG_READ_iCCP_SUPPORTED */
1168 #ifdef PNG_READ_sPLT_SUPPORTED
1170 png_handle_sPLT(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1171 /* Note: this does not properly handle chunks that are > 64K under DOS */
1173 png_bytep entry_start
;
1174 png_sPLT_t new_palette
;
1175 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1178 int data_length
, entry_size
, i
;
1179 png_uint_32 skip
= 0;
1182 png_debug(1, "in png_handle_sPLT");
1184 #ifdef PNG_USER_LIMITS_SUPPORTED
1186 if (png_ptr
->user_chunk_cache_max
!= 0)
1188 if (png_ptr
->user_chunk_cache_max
== 1)
1190 png_crc_finish(png_ptr
, length
);
1193 if (--png_ptr
->user_chunk_cache_max
== 1)
1195 png_warning(png_ptr
, "No space in chunk cache for sPLT");
1196 png_crc_finish(png_ptr
, length
);
1202 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1203 png_error(png_ptr
, "Missing IHDR before sPLT");
1204 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1206 png_warning(png_ptr
, "Invalid sPLT after IDAT");
1207 png_crc_finish(png_ptr
, length
);
1211 #ifdef PNG_MAX_MALLOC_64K
1212 if (length
> (png_uint_32
)65535L)
1214 png_warning(png_ptr
, "sPLT chunk too large to fit in memory");
1215 skip
= length
- (png_uint_32
)65535L;
1216 length
= (png_uint_32
)65535L;
1220 png_free(png_ptr
, png_ptr
->chunkdata
);
1221 png_ptr
->chunkdata
= (png_charp
)png_malloc(png_ptr
, length
+ 1);
1222 slength
= (png_size_t
)length
;
1223 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
1225 if (png_crc_finish(png_ptr
, skip
))
1227 png_free(png_ptr
, png_ptr
->chunkdata
);
1228 png_ptr
->chunkdata
= NULL
;
1232 png_ptr
->chunkdata
[slength
] = 0x00;
1234 for (entry_start
= (png_bytep
)png_ptr
->chunkdata
; *entry_start
;
1236 /* Empty loop to find end of name */ ;
1239 /* A sample depth should follow the separator, and we should be on it */
1240 if (entry_start
> (png_bytep
)png_ptr
->chunkdata
+ slength
- 2)
1242 png_free(png_ptr
, png_ptr
->chunkdata
);
1243 png_ptr
->chunkdata
= NULL
;
1244 png_warning(png_ptr
, "malformed sPLT chunk");
1248 new_palette
.depth
= *entry_start
++;
1249 entry_size
= (new_palette
.depth
== 8 ? 6 : 10);
1250 data_length
= (slength
- (entry_start
- (png_bytep
)png_ptr
->chunkdata
));
1252 /* Integrity-check the data length */
1253 if (data_length
% entry_size
)
1255 png_free(png_ptr
, png_ptr
->chunkdata
);
1256 png_ptr
->chunkdata
= NULL
;
1257 png_warning(png_ptr
, "sPLT chunk has bad length");
1261 new_palette
.nentries
= (png_int_32
) ( data_length
/ entry_size
);
1262 if ((png_uint_32
) new_palette
.nentries
>
1263 (png_uint_32
) (PNG_SIZE_MAX
/ png_sizeof(png_sPLT_entry
)))
1265 png_warning(png_ptr
, "sPLT chunk too long");
1268 new_palette
.entries
= (png_sPLT_entryp
)png_malloc_warn(
1269 png_ptr
, new_palette
.nentries
* png_sizeof(png_sPLT_entry
));
1270 if (new_palette
.entries
== NULL
)
1272 png_warning(png_ptr
, "sPLT chunk requires too much memory");
1276 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1277 for (i
= 0; i
< new_palette
.nentries
; i
++)
1279 pp
= new_palette
.entries
+ i
;
1281 if (new_palette
.depth
== 8)
1283 pp
->red
= *entry_start
++;
1284 pp
->green
= *entry_start
++;
1285 pp
->blue
= *entry_start
++;
1286 pp
->alpha
= *entry_start
++;
1290 pp
->red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1291 pp
->green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1292 pp
->blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1293 pp
->alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1295 pp
->frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1298 pp
= new_palette
.entries
;
1299 for (i
= 0; i
< new_palette
.nentries
; i
++)
1302 if (new_palette
.depth
== 8)
1304 pp
[i
].red
= *entry_start
++;
1305 pp
[i
].green
= *entry_start
++;
1306 pp
[i
].blue
= *entry_start
++;
1307 pp
[i
].alpha
= *entry_start
++;
1311 pp
[i
].red
= png_get_uint_16(entry_start
); entry_start
+= 2;
1312 pp
[i
].green
= png_get_uint_16(entry_start
); entry_start
+= 2;
1313 pp
[i
].blue
= png_get_uint_16(entry_start
); entry_start
+= 2;
1314 pp
[i
].alpha
= png_get_uint_16(entry_start
); entry_start
+= 2;
1316 pp
->frequency
= png_get_uint_16(entry_start
); entry_start
+= 2;
1320 /* Discard all chunk data except the name and stash that */
1321 new_palette
.name
= png_ptr
->chunkdata
;
1323 png_set_sPLT(png_ptr
, info_ptr
, &new_palette
, 1);
1325 png_free(png_ptr
, png_ptr
->chunkdata
);
1326 png_ptr
->chunkdata
= NULL
;
1327 png_free(png_ptr
, new_palette
.entries
);
1329 #endif /* PNG_READ_sPLT_SUPPORTED */
1331 #ifdef PNG_READ_tRNS_SUPPORTED
1333 png_handle_tRNS(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1335 png_byte readbuf
[PNG_MAX_PALETTE_LENGTH
];
1337 png_debug(1, "in png_handle_tRNS");
1339 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1340 png_error(png_ptr
, "Missing IHDR before tRNS");
1341 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1343 png_warning(png_ptr
, "Invalid tRNS after IDAT");
1344 png_crc_finish(png_ptr
, length
);
1347 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tRNS
))
1349 png_warning(png_ptr
, "Duplicate tRNS chunk");
1350 png_crc_finish(png_ptr
, length
);
1354 if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
1360 png_warning(png_ptr
, "Incorrect tRNS chunk length");
1361 png_crc_finish(png_ptr
, length
);
1365 png_crc_read(png_ptr
, buf
, 2);
1366 png_ptr
->num_trans
= 1;
1367 png_ptr
->trans_values
.gray
= png_get_uint_16(buf
);
1369 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
1375 png_warning(png_ptr
, "Incorrect tRNS chunk length");
1376 png_crc_finish(png_ptr
, length
);
1379 png_crc_read(png_ptr
, buf
, (png_size_t
)length
);
1380 png_ptr
->num_trans
= 1;
1381 png_ptr
->trans_values
.red
= png_get_uint_16(buf
);
1382 png_ptr
->trans_values
.green
= png_get_uint_16(buf
+ 2);
1383 png_ptr
->trans_values
.blue
= png_get_uint_16(buf
+ 4);
1385 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1387 if (!(png_ptr
->mode
& PNG_HAVE_PLTE
))
1389 /* Should be an error, but we can cope with it. */
1390 png_warning(png_ptr
, "Missing PLTE before tRNS");
1392 if (length
> (png_uint_32
)png_ptr
->num_palette
||
1393 length
> PNG_MAX_PALETTE_LENGTH
)
1395 png_warning(png_ptr
, "Incorrect tRNS chunk length");
1396 png_crc_finish(png_ptr
, length
);
1401 png_warning(png_ptr
, "Zero length tRNS chunk");
1402 png_crc_finish(png_ptr
, length
);
1405 png_crc_read(png_ptr
, readbuf
, (png_size_t
)length
);
1406 png_ptr
->num_trans
= (png_uint_16
)length
;
1410 png_warning(png_ptr
, "tRNS chunk not allowed with alpha channel");
1411 png_crc_finish(png_ptr
, length
);
1415 if (png_crc_finish(png_ptr
, 0))
1417 png_ptr
->num_trans
= 0;
1421 png_set_tRNS(png_ptr
, info_ptr
, readbuf
, png_ptr
->num_trans
,
1422 &(png_ptr
->trans_values
));
1426 #ifdef PNG_READ_bKGD_SUPPORTED
1428 png_handle_bKGD(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1433 png_debug(1, "in png_handle_bKGD");
1435 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1436 png_error(png_ptr
, "Missing IHDR before bKGD");
1437 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1439 png_warning(png_ptr
, "Invalid bKGD after IDAT");
1440 png_crc_finish(png_ptr
, length
);
1443 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
1444 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
1446 png_warning(png_ptr
, "Missing PLTE before bKGD");
1447 png_crc_finish(png_ptr
, length
);
1450 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
))
1452 png_warning(png_ptr
, "Duplicate bKGD chunk");
1453 png_crc_finish(png_ptr
, length
);
1457 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1459 else if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
1464 if (length
!= truelen
)
1466 png_warning(png_ptr
, "Incorrect bKGD chunk length");
1467 png_crc_finish(png_ptr
, length
);
1471 png_crc_read(png_ptr
, buf
, truelen
);
1472 if (png_crc_finish(png_ptr
, 0))
1475 /* We convert the index value into RGB components so that we can allow
1476 * arbitrary RGB values for background when we have transparency, and
1477 * so it is easy to determine the RGB values of the background color
1478 * from the info_ptr struct. */
1479 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1481 png_ptr
->background
.index
= buf
[0];
1482 if (info_ptr
&& info_ptr
->num_palette
)
1484 if (buf
[0] >= info_ptr
->num_palette
)
1486 png_warning(png_ptr
, "Incorrect bKGD chunk index value");
1489 png_ptr
->background
.red
=
1490 (png_uint_16
)png_ptr
->palette
[buf
[0]].red
;
1491 png_ptr
->background
.green
=
1492 (png_uint_16
)png_ptr
->palette
[buf
[0]].green
;
1493 png_ptr
->background
.blue
=
1494 (png_uint_16
)png_ptr
->palette
[buf
[0]].blue
;
1497 else if (!(png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)) /* GRAY */
1499 png_ptr
->background
.red
=
1500 png_ptr
->background
.green
=
1501 png_ptr
->background
.blue
=
1502 png_ptr
->background
.gray
= png_get_uint_16(buf
);
1506 png_ptr
->background
.red
= png_get_uint_16(buf
);
1507 png_ptr
->background
.green
= png_get_uint_16(buf
+ 2);
1508 png_ptr
->background
.blue
= png_get_uint_16(buf
+ 4);
1511 png_set_bKGD(png_ptr
, info_ptr
, &(png_ptr
->background
));
1515 #ifdef PNG_READ_hIST_SUPPORTED
1517 png_handle_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1519 unsigned int num
, i
;
1520 png_uint_16 readbuf
[PNG_MAX_PALETTE_LENGTH
];
1522 png_debug(1, "in png_handle_hIST");
1524 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1525 png_error(png_ptr
, "Missing IHDR before hIST");
1526 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1528 png_warning(png_ptr
, "Invalid hIST after IDAT");
1529 png_crc_finish(png_ptr
, length
);
1532 else if (!(png_ptr
->mode
& PNG_HAVE_PLTE
))
1534 png_warning(png_ptr
, "Missing PLTE before hIST");
1535 png_crc_finish(png_ptr
, length
);
1538 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
))
1540 png_warning(png_ptr
, "Duplicate hIST chunk");
1541 png_crc_finish(png_ptr
, length
);
1546 if (num
!= (unsigned int) png_ptr
->num_palette
|| num
>
1547 (unsigned int) PNG_MAX_PALETTE_LENGTH
)
1549 png_warning(png_ptr
, "Incorrect hIST chunk length");
1550 png_crc_finish(png_ptr
, length
);
1554 for (i
= 0; i
< num
; i
++)
1558 png_crc_read(png_ptr
, buf
, 2);
1559 readbuf
[i
] = png_get_uint_16(buf
);
1562 if (png_crc_finish(png_ptr
, 0))
1565 png_set_hIST(png_ptr
, info_ptr
, readbuf
);
1569 #ifdef PNG_READ_pHYs_SUPPORTED
1571 png_handle_pHYs(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1574 png_uint_32 res_x
, res_y
;
1577 png_debug(1, "in png_handle_pHYs");
1579 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1580 png_error(png_ptr
, "Missing IHDR before pHYs");
1581 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1583 png_warning(png_ptr
, "Invalid pHYs after IDAT");
1584 png_crc_finish(png_ptr
, length
);
1587 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
1589 png_warning(png_ptr
, "Duplicate pHYs chunk");
1590 png_crc_finish(png_ptr
, length
);
1596 png_warning(png_ptr
, "Incorrect pHYs chunk length");
1597 png_crc_finish(png_ptr
, length
);
1601 png_crc_read(png_ptr
, buf
, 9);
1602 if (png_crc_finish(png_ptr
, 0))
1605 res_x
= png_get_uint_32(buf
);
1606 res_y
= png_get_uint_32(buf
+ 4);
1608 png_set_pHYs(png_ptr
, info_ptr
, res_x
, res_y
, unit_type
);
1612 #ifdef PNG_READ_oFFs_SUPPORTED
1614 png_handle_oFFs(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1617 png_int_32 offset_x
, offset_y
;
1620 png_debug(1, "in png_handle_oFFs");
1622 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1623 png_error(png_ptr
, "Missing IHDR before oFFs");
1624 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1626 png_warning(png_ptr
, "Invalid oFFs after IDAT");
1627 png_crc_finish(png_ptr
, length
);
1630 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
1632 png_warning(png_ptr
, "Duplicate oFFs chunk");
1633 png_crc_finish(png_ptr
, length
);
1639 png_warning(png_ptr
, "Incorrect oFFs chunk length");
1640 png_crc_finish(png_ptr
, length
);
1644 png_crc_read(png_ptr
, buf
, 9);
1645 if (png_crc_finish(png_ptr
, 0))
1648 offset_x
= png_get_int_32(buf
);
1649 offset_y
= png_get_int_32(buf
+ 4);
1651 png_set_oFFs(png_ptr
, info_ptr
, offset_x
, offset_y
, unit_type
);
1655 #ifdef PNG_READ_pCAL_SUPPORTED
1656 /* Read the pCAL chunk (described in the PNG Extensions document) */
1658 png_handle_pCAL(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1661 png_byte type
, nparams
;
1662 png_charp buf
, units
, endptr
;
1667 png_debug(1, "in png_handle_pCAL");
1669 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1670 png_error(png_ptr
, "Missing IHDR before pCAL");
1671 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1673 png_warning(png_ptr
, "Invalid pCAL after IDAT");
1674 png_crc_finish(png_ptr
, length
);
1677 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pCAL
))
1679 png_warning(png_ptr
, "Duplicate pCAL chunk");
1680 png_crc_finish(png_ptr
, length
);
1684 png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)",
1686 png_free(png_ptr
, png_ptr
->chunkdata
);
1687 png_ptr
->chunkdata
= (png_charp
)png_malloc_warn(png_ptr
, length
+ 1);
1688 if (png_ptr
->chunkdata
== NULL
)
1690 png_warning(png_ptr
, "No memory for pCAL purpose.");
1693 slength
= (png_size_t
)length
;
1694 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
1696 if (png_crc_finish(png_ptr
, 0))
1698 png_free(png_ptr
, png_ptr
->chunkdata
);
1699 png_ptr
->chunkdata
= NULL
;
1703 png_ptr
->chunkdata
[slength
] = 0x00; /* Null terminate the last string */
1705 png_debug(3, "Finding end of pCAL purpose string");
1706 for (buf
= png_ptr
->chunkdata
; *buf
; buf
++)
1709 endptr
= png_ptr
->chunkdata
+ slength
;
1711 /* We need to have at least 12 bytes after the purpose string
1712 in order to get the parameter information. */
1713 if (endptr
<= buf
+ 12)
1715 png_warning(png_ptr
, "Invalid pCAL data");
1716 png_free(png_ptr
, png_ptr
->chunkdata
);
1717 png_ptr
->chunkdata
= NULL
;
1721 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
1722 X0
= png_get_int_32((png_bytep
)buf
+1);
1723 X1
= png_get_int_32((png_bytep
)buf
+5);
1728 png_debug(3, "Checking pCAL equation type and number of parameters");
1729 /* Check that we have the right number of parameters for known
1731 if ((type
== PNG_EQUATION_LINEAR
&& nparams
!= 2) ||
1732 (type
== PNG_EQUATION_BASE_E
&& nparams
!= 3) ||
1733 (type
== PNG_EQUATION_ARBITRARY
&& nparams
!= 3) ||
1734 (type
== PNG_EQUATION_HYPERBOLIC
&& nparams
!= 4))
1736 png_warning(png_ptr
, "Invalid pCAL parameters for equation type");
1737 png_free(png_ptr
, png_ptr
->chunkdata
);
1738 png_ptr
->chunkdata
= NULL
;
1741 else if (type
>= PNG_EQUATION_LAST
)
1743 png_warning(png_ptr
, "Unrecognized equation type for pCAL chunk");
1746 for (buf
= units
; *buf
; buf
++)
1747 /* Empty loop to move past the units string. */ ;
1749 png_debug(3, "Allocating pCAL parameters array");
1750 params
= (png_charpp
)png_malloc_warn(png_ptr
,
1751 (png_uint_32
)(nparams
* png_sizeof(png_charp
))) ;
1754 png_free(png_ptr
, png_ptr
->chunkdata
);
1755 png_ptr
->chunkdata
= NULL
;
1756 png_warning(png_ptr
, "No memory for pCAL params.");
1760 /* Get pointers to the start of each parameter string. */
1761 for (i
= 0; i
< (int)nparams
; i
++)
1763 buf
++; /* Skip the null string terminator from previous parameter. */
1765 png_debug1(3, "Reading pCAL parameter %d", i
);
1766 for (params
[i
] = buf
; buf
<= endptr
&& *buf
!= 0x00; buf
++)
1767 /* Empty loop to move past each parameter string */ ;
1769 /* Make sure we haven't run out of data yet */
1772 png_warning(png_ptr
, "Invalid pCAL data");
1773 png_free(png_ptr
, png_ptr
->chunkdata
);
1774 png_ptr
->chunkdata
= NULL
;
1775 png_free(png_ptr
, params
);
1780 png_set_pCAL(png_ptr
, info_ptr
, png_ptr
->chunkdata
, X0
, X1
, type
, nparams
,
1783 png_free(png_ptr
, png_ptr
->chunkdata
);
1784 png_ptr
->chunkdata
= NULL
;
1785 png_free(png_ptr
, params
);
1789 #ifdef PNG_READ_sCAL_SUPPORTED
1790 /* Read the sCAL chunk */
1792 png_handle_sCAL(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1795 #ifdef PNG_FLOATING_POINT_SUPPORTED
1796 double width
, height
;
1799 #ifdef PNG_FIXED_POINT_SUPPORTED
1800 png_charp swidth
, sheight
;
1805 png_debug(1, "in png_handle_sCAL");
1807 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1808 png_error(png_ptr
, "Missing IHDR before sCAL");
1809 else if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1811 png_warning(png_ptr
, "Invalid sCAL after IDAT");
1812 png_crc_finish(png_ptr
, length
);
1815 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sCAL
))
1817 png_warning(png_ptr
, "Duplicate sCAL chunk");
1818 png_crc_finish(png_ptr
, length
);
1822 /* Need unit type, width, \0, height: minimum 4 bytes */
1823 else if (length
< 4)
1825 png_warning(png_ptr
, "sCAL chunk too short");
1826 png_crc_finish(png_ptr
, length
);
1830 png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)",
1832 png_ptr
->chunkdata
= (png_charp
)png_malloc_warn(png_ptr
, length
+ 1);
1833 if (png_ptr
->chunkdata
== NULL
)
1835 png_warning(png_ptr
, "Out of memory while processing sCAL chunk");
1836 png_crc_finish(png_ptr
, length
);
1839 slength
= (png_size_t
)length
;
1840 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
1842 if (png_crc_finish(png_ptr
, 0))
1844 png_free(png_ptr
, png_ptr
->chunkdata
);
1845 png_ptr
->chunkdata
= NULL
;
1849 png_ptr
->chunkdata
[slength
] = 0x00; /* Null terminate the last string */
1851 ep
= png_ptr
->chunkdata
+ 1; /* Skip unit byte */
1853 #ifdef PNG_FLOATING_POINT_SUPPORTED
1854 width
= png_strtod(png_ptr
, ep
, &vp
);
1857 png_warning(png_ptr
, "malformed width string in sCAL chunk");
1858 png_free(png_ptr
, png_ptr
->chunkdata
);
1859 png_ptr
->chunkdata
= NULL
;
1863 #ifdef PNG_FIXED_POINT_SUPPORTED
1864 swidth
= (png_charp
)png_malloc_warn(png_ptr
, png_strlen(ep
) + 1);
1867 png_warning(png_ptr
, "Out of memory while processing sCAL chunk width");
1868 png_free(png_ptr
, png_ptr
->chunkdata
);
1869 png_ptr
->chunkdata
= NULL
;
1872 png_memcpy(swidth
, ep
, (png_size_t
)png_strlen(ep
));
1876 for (ep
= png_ptr
->chunkdata
; *ep
; ep
++)
1880 if (png_ptr
->chunkdata
+ slength
< ep
)
1882 png_warning(png_ptr
, "Truncated sCAL chunk");
1883 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1884 png_free(png_ptr
, swidth
);
1886 png_free(png_ptr
, png_ptr
->chunkdata
);
1887 png_ptr
->chunkdata
= NULL
;
1891 #ifdef PNG_FLOATING_POINT_SUPPORTED
1892 height
= png_strtod(png_ptr
, ep
, &vp
);
1895 png_warning(png_ptr
, "malformed height string in sCAL chunk");
1896 png_free(png_ptr
, png_ptr
->chunkdata
);
1897 png_ptr
->chunkdata
= NULL
;
1898 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1899 png_free(png_ptr
, swidth
);
1904 #ifdef PNG_FIXED_POINT_SUPPORTED
1905 sheight
= (png_charp
)png_malloc_warn(png_ptr
, png_strlen(ep
) + 1);
1906 if (sheight
== NULL
)
1908 png_warning(png_ptr
, "Out of memory while processing sCAL chunk height");
1909 png_free(png_ptr
, png_ptr
->chunkdata
);
1910 png_ptr
->chunkdata
= NULL
;
1911 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1912 png_free(png_ptr
, swidth
);
1916 png_memcpy(sheight
, ep
, (png_size_t
)png_strlen(ep
));
1920 if (png_ptr
->chunkdata
+ slength
< ep
1921 #ifdef PNG_FLOATING_POINT_SUPPORTED
1922 || width
<= 0. || height
<= 0.
1926 png_warning(png_ptr
, "Invalid sCAL data");
1927 png_free(png_ptr
, png_ptr
->chunkdata
);
1928 png_ptr
->chunkdata
= NULL
;
1929 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1930 png_free(png_ptr
, swidth
);
1931 png_free(png_ptr
, sheight
);
1937 #ifdef PNG_FLOATING_POINT_SUPPORTED
1938 png_set_sCAL(png_ptr
, info_ptr
, png_ptr
->chunkdata
[0], width
, height
);
1940 #ifdef PNG_FIXED_POINT_SUPPORTED
1941 png_set_sCAL_s(png_ptr
, info_ptr
, png_ptr
->chunkdata
[0], swidth
, sheight
);
1945 png_free(png_ptr
, png_ptr
->chunkdata
);
1946 png_ptr
->chunkdata
= NULL
;
1947 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1948 png_free(png_ptr
, swidth
);
1949 png_free(png_ptr
, sheight
);
1954 #ifdef PNG_READ_tIME_SUPPORTED
1956 png_handle_tIME(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1961 png_debug(1, "in png_handle_tIME");
1963 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
1964 png_error(png_ptr
, "Out of place tIME chunk");
1965 else if (info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_tIME
))
1967 png_warning(png_ptr
, "Duplicate tIME chunk");
1968 png_crc_finish(png_ptr
, length
);
1972 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
1973 png_ptr
->mode
|= PNG_AFTER_IDAT
;
1977 png_warning(png_ptr
, "Incorrect tIME chunk length");
1978 png_crc_finish(png_ptr
, length
);
1982 png_crc_read(png_ptr
, buf
, 7);
1983 if (png_crc_finish(png_ptr
, 0))
1986 mod_time
.second
= buf
[6];
1987 mod_time
.minute
= buf
[5];
1988 mod_time
.hour
= buf
[4];
1989 mod_time
.day
= buf
[3];
1990 mod_time
.month
= buf
[2];
1991 mod_time
.year
= png_get_uint_16(buf
);
1993 png_set_tIME(png_ptr
, info_ptr
, &mod_time
);
1997 #ifdef PNG_READ_tEXt_SUPPORTED
1998 /* Note: this does not properly handle chunks that are > 64K under DOS */
2000 png_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
2005 png_uint_32 skip
= 0;
2009 png_debug(1, "in png_handle_tEXt");
2011 #ifdef PNG_USER_LIMITS_SUPPORTED
2012 if (png_ptr
->user_chunk_cache_max
!= 0)
2014 if (png_ptr
->user_chunk_cache_max
== 1)
2016 png_crc_finish(png_ptr
, length
);
2019 if (--png_ptr
->user_chunk_cache_max
== 1)
2021 png_warning(png_ptr
, "No space in chunk cache for tEXt");
2022 png_crc_finish(png_ptr
, length
);
2028 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2029 png_error(png_ptr
, "Missing IHDR before tEXt");
2031 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2032 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2034 #ifdef PNG_MAX_MALLOC_64K
2035 if (length
> (png_uint_32
)65535L)
2037 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
2038 skip
= length
- (png_uint_32
)65535L;
2039 length
= (png_uint_32
)65535L;
2043 png_free(png_ptr
, png_ptr
->chunkdata
);
2045 png_ptr
->chunkdata
= (png_charp
)png_malloc_warn(png_ptr
, length
+ 1);
2046 if (png_ptr
->chunkdata
== NULL
)
2048 png_warning(png_ptr
, "No memory to process text chunk.");
2051 slength
= (png_size_t
)length
;
2052 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
2054 if (png_crc_finish(png_ptr
, skip
))
2056 png_free(png_ptr
, png_ptr
->chunkdata
);
2057 png_ptr
->chunkdata
= NULL
;
2061 key
= png_ptr
->chunkdata
;
2063 key
[slength
] = 0x00;
2065 for (text
= key
; *text
; text
++)
2066 /* Empty loop to find end of key */ ;
2068 if (text
!= key
+ slength
)
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 text chunk.");
2076 png_free(png_ptr
, png_ptr
->chunkdata
);
2077 png_ptr
->chunkdata
= NULL
;
2080 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
2081 text_ptr
->key
= key
;
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
= text
;
2088 text_ptr
->text_length
= png_strlen(text
);
2090 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
2092 png_free(png_ptr
, png_ptr
->chunkdata
);
2093 png_ptr
->chunkdata
= NULL
;
2094 png_free(png_ptr
, text_ptr
);
2096 png_warning(png_ptr
, "Insufficient memory to process text chunk.");
2100 #ifdef PNG_READ_zTXt_SUPPORTED
2101 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2103 png_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
2109 png_size_t slength
, prefix_len
, data_len
;
2111 png_debug(1, "in png_handle_zTXt");
2113 #ifdef PNG_USER_LIMITS_SUPPORTED
2114 if (png_ptr
->user_chunk_cache_max
!= 0)
2116 if (png_ptr
->user_chunk_cache_max
== 1)
2118 png_crc_finish(png_ptr
, length
);
2121 if (--png_ptr
->user_chunk_cache_max
== 1)
2123 png_warning(png_ptr
, "No space in chunk cache for zTXt");
2124 png_crc_finish(png_ptr
, length
);
2130 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2131 png_error(png_ptr
, "Missing IHDR before zTXt");
2133 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2134 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2136 #ifdef PNG_MAX_MALLOC_64K
2137 /* We will no doubt have problems with chunks even half this size, but
2138 there is no hard and fast rule to tell us where to stop. */
2139 if (length
> (png_uint_32
)65535L)
2141 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
2142 png_crc_finish(png_ptr
, length
);
2147 png_free(png_ptr
, png_ptr
->chunkdata
);
2148 png_ptr
->chunkdata
= (png_charp
)png_malloc_warn(png_ptr
, length
+ 1);
2149 if (png_ptr
->chunkdata
== NULL
)
2151 png_warning(png_ptr
, "Out of memory processing zTXt chunk.");
2154 slength
= (png_size_t
)length
;
2155 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
2156 if (png_crc_finish(png_ptr
, 0))
2158 png_free(png_ptr
, png_ptr
->chunkdata
);
2159 png_ptr
->chunkdata
= NULL
;
2163 png_ptr
->chunkdata
[slength
] = 0x00;
2165 for (text
= png_ptr
->chunkdata
; *text
; text
++)
2168 /* zTXt must have some text after the chunkdataword */
2169 if (text
>= png_ptr
->chunkdata
+ slength
- 2)
2171 png_warning(png_ptr
, "Truncated zTXt chunk");
2172 png_free(png_ptr
, png_ptr
->chunkdata
);
2173 png_ptr
->chunkdata
= NULL
;
2178 comp_type
= *(++text
);
2179 if (comp_type
!= PNG_TEXT_COMPRESSION_zTXt
)
2181 png_warning(png_ptr
, "Unknown compression type in zTXt chunk");
2182 comp_type
= PNG_TEXT_COMPRESSION_zTXt
;
2184 text
++; /* Skip the compression_method byte */
2186 prefix_len
= text
- png_ptr
->chunkdata
;
2188 png_decompress_chunk(png_ptr
, comp_type
,
2189 (png_size_t
)length
, prefix_len
, &data_len
);
2191 text_ptr
= (png_textp
)png_malloc_warn(png_ptr
,
2192 (png_uint_32
)png_sizeof(png_text
));
2193 if (text_ptr
== NULL
)
2195 png_warning(png_ptr
, "Not enough memory to process zTXt chunk.");
2196 png_free(png_ptr
, png_ptr
->chunkdata
);
2197 png_ptr
->chunkdata
= NULL
;
2200 text_ptr
->compression
= comp_type
;
2201 text_ptr
->key
= png_ptr
->chunkdata
;
2202 #ifdef PNG_iTXt_SUPPORTED
2203 text_ptr
->lang
= NULL
;
2204 text_ptr
->lang_key
= NULL
;
2205 text_ptr
->itxt_length
= 0;
2207 text_ptr
->text
= png_ptr
->chunkdata
+ prefix_len
;
2208 text_ptr
->text_length
= data_len
;
2210 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
2212 png_free(png_ptr
, text_ptr
);
2213 png_free(png_ptr
, png_ptr
->chunkdata
);
2214 png_ptr
->chunkdata
= NULL
;
2216 png_error(png_ptr
, "Insufficient memory to store zTXt chunk.");
2220 #ifdef PNG_READ_iTXt_SUPPORTED
2221 /* Note: this does not correctly handle chunks that are > 64K under DOS */
2223 png_handle_iTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
2226 png_charp key
, lang
, text
, lang_key
;
2230 png_size_t slength
, prefix_len
, data_len
;
2232 png_debug(1, "in png_handle_iTXt");
2234 #ifdef PNG_USER_LIMITS_SUPPORTED
2235 if (png_ptr
->user_chunk_cache_max
!= 0)
2237 if (png_ptr
->user_chunk_cache_max
== 1)
2239 png_crc_finish(png_ptr
, length
);
2242 if (--png_ptr
->user_chunk_cache_max
== 1)
2244 png_warning(png_ptr
, "No space in chunk cache for iTXt");
2245 png_crc_finish(png_ptr
, length
);
2251 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
2252 png_error(png_ptr
, "Missing IHDR before iTXt");
2254 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2255 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2257 #ifdef PNG_MAX_MALLOC_64K
2258 /* We will no doubt have problems with chunks even half this size, but
2259 there is no hard and fast rule to tell us where to stop. */
2260 if (length
> (png_uint_32
)65535L)
2262 png_warning(png_ptr
, "iTXt chunk too large to fit in memory");
2263 png_crc_finish(png_ptr
, length
);
2268 png_free(png_ptr
, png_ptr
->chunkdata
);
2269 png_ptr
->chunkdata
= (png_charp
)png_malloc_warn(png_ptr
, length
+ 1);
2270 if (png_ptr
->chunkdata
== NULL
)
2272 png_warning(png_ptr
, "No memory to process iTXt chunk.");
2275 slength
= (png_size_t
)length
;
2276 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->chunkdata
, slength
);
2277 if (png_crc_finish(png_ptr
, 0))
2279 png_free(png_ptr
, png_ptr
->chunkdata
);
2280 png_ptr
->chunkdata
= NULL
;
2284 png_ptr
->chunkdata
[slength
] = 0x00;
2286 for (lang
= png_ptr
->chunkdata
; *lang
; lang
++)
2288 lang
++; /* Skip NUL separator */
2290 /* iTXt must have a language tag (possibly empty), two compression bytes,
2291 * translated keyword (possibly empty), and possibly some text after the
2295 if (lang
>= png_ptr
->chunkdata
+ slength
- 3)
2297 png_warning(png_ptr
, "Truncated iTXt chunk");
2298 png_free(png_ptr
, png_ptr
->chunkdata
);
2299 png_ptr
->chunkdata
= NULL
;
2304 comp_flag
= *lang
++;
2305 comp_type
= *lang
++;
2308 for (lang_key
= lang
; *lang_key
; lang_key
++)
2310 lang_key
++; /* Skip NUL separator */
2312 if (lang_key
>= png_ptr
->chunkdata
+ slength
)
2314 png_warning(png_ptr
, "Truncated iTXt chunk");
2315 png_free(png_ptr
, png_ptr
->chunkdata
);
2316 png_ptr
->chunkdata
= NULL
;
2320 for (text
= lang_key
; *text
; text
++)
2322 text
++; /* Skip NUL separator */
2323 if (text
>= png_ptr
->chunkdata
+ slength
)
2325 png_warning(png_ptr
, "Malformed iTXt chunk");
2326 png_free(png_ptr
, png_ptr
->chunkdata
);
2327 png_ptr
->chunkdata
= NULL
;
2331 prefix_len
= text
- png_ptr
->chunkdata
;
2333 key
=png_ptr
->chunkdata
;
2335 png_decompress_chunk(png_ptr
, comp_type
,
2336 (size_t)length
, prefix_len
, &data_len
);
2338 data_len
= png_strlen(png_ptr
->chunkdata
+ prefix_len
);
2339 text_ptr
= (png_textp
)png_malloc_warn(png_ptr
,
2340 (png_uint_32
)png_sizeof(png_text
));
2341 if (text_ptr
== NULL
)
2343 png_warning(png_ptr
, "Not enough memory to process iTXt chunk.");
2344 png_free(png_ptr
, png_ptr
->chunkdata
);
2345 png_ptr
->chunkdata
= NULL
;
2348 text_ptr
->compression
= (int)comp_flag
+ 1;
2349 text_ptr
->lang_key
= png_ptr
->chunkdata
+ (lang_key
- key
);
2350 text_ptr
->lang
= png_ptr
->chunkdata
+ (lang
- key
);
2351 text_ptr
->itxt_length
= data_len
;
2352 text_ptr
->text_length
= 0;
2353 text_ptr
->key
= png_ptr
->chunkdata
;
2354 text_ptr
->text
= png_ptr
->chunkdata
+ prefix_len
;
2356 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, 1);
2358 png_free(png_ptr
, text_ptr
);
2359 png_free(png_ptr
, png_ptr
->chunkdata
);
2360 png_ptr
->chunkdata
= NULL
;
2362 png_error(png_ptr
, "Insufficient memory to store iTXt chunk.");
2366 /* This function is called when we haven't found a handler for a
2367 chunk. If there isn't a problem with the chunk itself (ie bad
2368 chunk name, CRC, or a critical chunk), the chunk is silently ignored
2369 -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2370 case it will be saved away to be written out later. */
2372 png_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
2374 png_uint_32 skip
= 0;
2376 png_debug(1, "in png_handle_unknown");
2378 #ifdef PNG_USER_LIMITS_SUPPORTED
2379 if (png_ptr
->user_chunk_cache_max
!= 0)
2381 if (png_ptr
->user_chunk_cache_max
== 1)
2383 png_crc_finish(png_ptr
, length
);
2386 if (--png_ptr
->user_chunk_cache_max
== 1)
2388 png_warning(png_ptr
, "No space in chunk cache for unknown chunk");
2389 png_crc_finish(png_ptr
, length
);
2395 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
2397 #ifdef PNG_USE_LOCAL_ARRAYS
2400 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4)) /* Not an IDAT */
2401 png_ptr
->mode
|= PNG_AFTER_IDAT
;
2404 if (!(png_ptr
->chunk_name
[0] & 0x20))
2406 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2407 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
2408 PNG_HANDLE_CHUNK_ALWAYS
2409 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2410 && png_ptr
->read_user_chunk_fn
== NULL
2414 png_chunk_error(png_ptr
, "unknown critical chunk");
2417 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2418 if ((png_ptr
->flags
& PNG_FLAG_KEEP_UNKNOWN_CHUNKS
)
2419 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2420 || (png_ptr
->read_user_chunk_fn
!= NULL
)
2424 #ifdef PNG_MAX_MALLOC_64K
2425 if (length
> (png_uint_32
)65535L)
2427 png_warning(png_ptr
, "unknown chunk too large to fit in memory");
2428 skip
= length
- (png_uint_32
)65535L;
2429 length
= (png_uint_32
)65535L;
2432 png_memcpy((png_charp
)png_ptr
->unknown_chunk
.name
,
2433 (png_charp
)png_ptr
->chunk_name
,
2434 png_sizeof(png_ptr
->unknown_chunk
.name
));
2435 png_ptr
->unknown_chunk
.name
[png_sizeof(png_ptr
->unknown_chunk
.name
)-1]
2437 png_ptr
->unknown_chunk
.size
= (png_size_t
)length
;
2439 png_ptr
->unknown_chunk
.data
= NULL
;
2442 png_ptr
->unknown_chunk
.data
= (png_bytep
)png_malloc(png_ptr
, length
);
2443 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->unknown_chunk
.data
, length
);
2445 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2446 if (png_ptr
->read_user_chunk_fn
!= NULL
)
2448 /* Callback to user unknown chunk handler */
2450 ret
= (*(png_ptr
->read_user_chunk_fn
))
2451 (png_ptr
, &png_ptr
->unknown_chunk
);
2453 png_chunk_error(png_ptr
, "error in user chunk");
2456 if (!(png_ptr
->chunk_name
[0] & 0x20))
2457 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2458 if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
) !=
2459 PNG_HANDLE_CHUNK_ALWAYS
)
2461 png_chunk_error(png_ptr
, "unknown critical chunk");
2462 png_set_unknown_chunks(png_ptr
, info_ptr
,
2463 &png_ptr
->unknown_chunk
, 1);
2468 png_set_unknown_chunks(png_ptr
, info_ptr
, &png_ptr
->unknown_chunk
, 1);
2469 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
2470 png_ptr
->unknown_chunk
.data
= NULL
;
2476 png_crc_finish(png_ptr
, skip
);
2478 #ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2479 info_ptr
= info_ptr
; /* Quiet compiler warnings about unused info_ptr */
2483 /* This function is called to verify that a chunk name is valid.
2484 This function can't have the "critical chunk check" incorporated
2485 into it, since in the future we will need to be able to call user
2486 functions to handle unknown critical chunks after we check that
2487 the chunk name itself is valid. */
2489 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2492 png_check_chunk_name(png_structp png_ptr
, png_bytep chunk_name
)
2494 png_debug(1, "in png_check_chunk_name");
2495 if (isnonalpha(chunk_name
[0]) || isnonalpha(chunk_name
[1]) ||
2496 isnonalpha(chunk_name
[2]) || isnonalpha(chunk_name
[3]))
2498 png_chunk_error(png_ptr
, "invalid chunk type");
2502 /* Combines the row recently read in with the existing pixels in the
2503 row. This routine takes care of alpha and transparency if requested.
2504 This routine also handles the two methods of progressive display
2505 of interlaced images, depending on the mask value.
2506 The mask value describes which pixels are to be combined with
2507 the row. The pattern always repeats every 8 pixels, so just 8
2508 bits are needed. A one indicates the pixel is to be combined,
2509 a zero indicates the pixel is to be skipped. This is in addition
2510 to any alpha or transparency value associated with the pixel. If
2511 you want all pixels to be combined, pass 0xff (255) in mask. */
2514 png_combine_row(png_structp png_ptr
, png_bytep row
, int mask
)
2516 png_debug(1, "in png_combine_row");
2519 png_memcpy(row
, png_ptr
->row_buf
+ 1,
2520 PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
, png_ptr
->width
));
2524 switch (png_ptr
->row_info
.pixel_depth
)
2528 png_bytep sp
= png_ptr
->row_buf
+ 1;
2530 int s_inc
, s_start
, s_end
;
2534 png_uint_32 row_width
= png_ptr
->width
;
2536 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2537 if (png_ptr
->transformations
& PNG_PACKSWAP
)
2553 for (i
= 0; i
< row_width
; i
++)
2559 value
= (*sp
>> shift
) & 0x01;
2560 *dp
&= (png_byte
)((0x7f7f >> (7 - shift
)) & 0xff);
2561 *dp
|= (png_byte
)(value
<< shift
);
2582 png_bytep sp
= png_ptr
->row_buf
+ 1;
2584 int s_start
, s_end
, s_inc
;
2588 png_uint_32 row_width
= png_ptr
->width
;
2591 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2592 if (png_ptr
->transformations
& PNG_PACKSWAP
)
2608 for (i
= 0; i
< row_width
; i
++)
2612 value
= (*sp
>> shift
) & 0x03;
2613 *dp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2614 *dp
|= (png_byte
)(value
<< shift
);
2634 png_bytep sp
= png_ptr
->row_buf
+ 1;
2636 int s_start
, s_end
, s_inc
;
2640 png_uint_32 row_width
= png_ptr
->width
;
2643 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2644 if (png_ptr
->transformations
& PNG_PACKSWAP
)
2659 for (i
= 0; i
< row_width
; i
++)
2663 value
= (*sp
>> shift
) & 0xf;
2664 *dp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2665 *dp
|= (png_byte
)(value
<< shift
);
2685 png_bytep sp
= png_ptr
->row_buf
+ 1;
2687 png_size_t pixel_bytes
= (png_ptr
->row_info
.pixel_depth
>> 3);
2689 png_uint_32 row_width
= png_ptr
->width
;
2693 for (i
= 0; i
< row_width
; i
++)
2697 png_memcpy(dp
, sp
, pixel_bytes
);
2714 #ifdef PNG_READ_INTERLACING_SUPPORTED
2715 /* OLD pre-1.0.9 interface:
2716 void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
2717 png_uint_32 transformations)
2720 png_do_read_interlace(png_structp png_ptr
)
2722 png_row_infop row_info
= &(png_ptr
->row_info
);
2723 png_bytep row
= png_ptr
->row_buf
+ 1;
2724 int pass
= png_ptr
->pass
;
2725 png_uint_32 transformations
= png_ptr
->transformations
;
2726 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2727 /* Offset to next interlace block */
2728 PNG_CONST
int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
2730 png_debug(1, "in png_do_read_interlace");
2731 if (row
!= NULL
&& row_info
!= NULL
)
2733 png_uint_32 final_width
;
2735 final_width
= row_info
->width
* png_pass_inc
[pass
];
2737 switch (row_info
->pixel_depth
)
2741 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 3);
2742 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 3);
2744 int s_start
, s_end
, s_inc
;
2745 int jstop
= png_pass_inc
[pass
];
2750 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2751 if (transformations
& PNG_PACKSWAP
)
2753 sshift
= (int)((row_info
->width
+ 7) & 0x07);
2754 dshift
= (int)((final_width
+ 7) & 0x07);
2762 sshift
= 7 - (int)((row_info
->width
+ 7) & 0x07);
2763 dshift
= 7 - (int)((final_width
+ 7) & 0x07);
2769 for (i
= 0; i
< row_info
->width
; i
++)
2771 v
= (png_byte
)((*sp
>> sshift
) & 0x01);
2772 for (j
= 0; j
< jstop
; j
++)
2774 *dp
&= (png_byte
)((0x7f7f >> (7 - dshift
)) & 0xff);
2775 *dp
|= (png_byte
)(v
<< dshift
);
2776 if (dshift
== s_end
)
2784 if (sshift
== s_end
)
2796 png_bytep sp
= row
+ (png_uint_32
)((row_info
->width
- 1) >> 2);
2797 png_bytep dp
= row
+ (png_uint_32
)((final_width
- 1) >> 2);
2799 int s_start
, s_end
, s_inc
;
2800 int jstop
= png_pass_inc
[pass
];
2803 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2804 if (transformations
& PNG_PACKSWAP
)
2806 sshift
= (int)(((row_info
->width
+ 3) & 0x03) << 1);
2807 dshift
= (int)(((final_width
+ 3) & 0x03) << 1);
2815 sshift
= (int)((3 - ((row_info
->width
+ 3) & 0x03)) << 1);
2816 dshift
= (int)((3 - ((final_width
+ 3) & 0x03)) << 1);
2822 for (i
= 0; i
< row_info
->width
; i
++)
2827 v
= (png_byte
)((*sp
>> sshift
) & 0x03);
2828 for (j
= 0; j
< jstop
; j
++)
2830 *dp
&= (png_byte
)((0x3f3f >> (6 - dshift
)) & 0xff);
2831 *dp
|= (png_byte
)(v
<< dshift
);
2832 if (dshift
== s_end
)
2840 if (sshift
== s_end
)
2852 png_bytep sp
= row
+ (png_size_t
)((row_info
->width
- 1) >> 1);
2853 png_bytep dp
= row
+ (png_size_t
)((final_width
- 1) >> 1);
2855 int s_start
, s_end
, s_inc
;
2857 int jstop
= png_pass_inc
[pass
];
2859 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2860 if (transformations
& PNG_PACKSWAP
)
2862 sshift
= (int)(((row_info
->width
+ 1) & 0x01) << 2);
2863 dshift
= (int)(((final_width
+ 1) & 0x01) << 2);
2871 sshift
= (int)((1 - ((row_info
->width
+ 1) & 0x01)) << 2);
2872 dshift
= (int)((1 - ((final_width
+ 1) & 0x01)) << 2);
2878 for (i
= 0; i
< row_info
->width
; i
++)
2880 png_byte v
= (png_byte
)((*sp
>> sshift
) & 0xf);
2883 for (j
= 0; j
< jstop
; j
++)
2885 *dp
&= (png_byte
)((0xf0f >> (4 - dshift
)) & 0xff);
2886 *dp
|= (png_byte
)(v
<< dshift
);
2887 if (dshift
== s_end
)
2895 if (sshift
== s_end
)
2907 png_size_t pixel_bytes
= (row_info
->pixel_depth
>> 3);
2908 png_bytep sp
= row
+ (png_size_t
)(row_info
->width
- 1)
2910 png_bytep dp
= row
+ (png_size_t
)(final_width
- 1) * pixel_bytes
;
2912 int jstop
= png_pass_inc
[pass
];
2915 for (i
= 0; i
< row_info
->width
; i
++)
2920 png_memcpy(v
, sp
, pixel_bytes
);
2921 for (j
= 0; j
< jstop
; j
++)
2923 png_memcpy(dp
, v
, pixel_bytes
);
2931 row_info
->width
= final_width
;
2932 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, final_width
);
2934 #ifndef PNG_READ_PACKSWAP_SUPPORTED
2935 transformations
= transformations
; /* Silence compiler warning */
2938 #endif /* PNG_READ_INTERLACING_SUPPORTED */
2941 png_read_filter_row(png_structp png_ptr
, png_row_infop row_info
, png_bytep row
,
2942 png_bytep prev_row
, int filter
)
2944 png_debug(1, "in png_read_filter_row");
2945 png_debug2(2, "row = %lu, filter = %d", png_ptr
->row_number
, filter
);
2948 case PNG_FILTER_VALUE_NONE
:
2950 case PNG_FILTER_VALUE_SUB
:
2953 png_uint_32 istop
= row_info
->rowbytes
;
2954 png_uint_32 bpp
= (row_info
->pixel_depth
+ 7) >> 3;
2955 png_bytep rp
= row
+ bpp
;
2958 for (i
= bpp
; i
< istop
; i
++)
2960 *rp
= (png_byte
)(((int)(*rp
) + (int)(*lp
++)) & 0xff);
2965 case PNG_FILTER_VALUE_UP
:
2968 png_uint_32 istop
= row_info
->rowbytes
;
2970 png_bytep pp
= prev_row
;
2972 for (i
= 0; i
< istop
; i
++)
2974 *rp
= (png_byte
)(((int)(*rp
) + (int)(*pp
++)) & 0xff);
2979 case PNG_FILTER_VALUE_AVG
:
2983 png_bytep pp
= prev_row
;
2985 png_uint_32 bpp
= (row_info
->pixel_depth
+ 7) >> 3;
2986 png_uint_32 istop
= row_info
->rowbytes
- bpp
;
2988 for (i
= 0; i
< bpp
; i
++)
2990 *rp
= (png_byte
)(((int)(*rp
) +
2991 ((int)(*pp
++) / 2 )) & 0xff);
2995 for (i
= 0; i
< istop
; i
++)
2997 *rp
= (png_byte
)(((int)(*rp
) +
2998 (int)(*pp
++ + *lp
++) / 2 ) & 0xff);
3003 case PNG_FILTER_VALUE_PAETH
:
3007 png_bytep pp
= prev_row
;
3009 png_bytep cp
= prev_row
;
3010 png_uint_32 bpp
= (row_info
->pixel_depth
+ 7) >> 3;
3011 png_uint_32 istop
=row_info
->rowbytes
- bpp
;
3013 for (i
= 0; i
< bpp
; i
++)
3015 *rp
= (png_byte
)(((int)(*rp
) + (int)(*pp
++)) & 0xff);
3019 for (i
= 0; i
< istop
; i
++) /* Use leftover rp,pp */
3021 int a
, b
, c
, pa
, pb
, pc
, p
;
3035 pa
= p
< 0 ? -p
: p
;
3036 pb
= pc
< 0 ? -pc
: pc
;
3037 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
3041 if (pa <= pb && pa <= pc)
3049 p
= (pa
<= pb
&& pa
<= pc
) ? a
: (pb
<= pc
) ? b
: c
;
3051 *rp
= (png_byte
)(((int)(*rp
) + p
) & 0xff);
3057 png_warning(png_ptr
, "Ignoring bad adaptive filter type");
3063 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
3065 png_read_finish_row(png_structp png_ptr
)
3067 #ifdef PNG_READ_INTERLACING_SUPPORTED
3068 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3070 /* Start of interlace block */
3071 PNG_CONST
int png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
3073 /* Offset to next interlace block */
3074 PNG_CONST
int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
3076 /* Start of interlace block in the y direction */
3077 PNG_CONST
int png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
3079 /* Offset to next interlace block in the y direction */
3080 PNG_CONST
int png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
3081 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3083 png_debug(1, "in png_read_finish_row");
3084 png_ptr
->row_number
++;
3085 if (png_ptr
->row_number
< png_ptr
->num_rows
)
3088 #ifdef PNG_READ_INTERLACING_SUPPORTED
3089 if (png_ptr
->interlaced
)
3091 png_ptr
->row_number
= 0;
3092 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
3093 png_ptr
->rowbytes
+ 1);
3097 if (png_ptr
->pass
>= 7)
3099 png_ptr
->iwidth
= (png_ptr
->width
+
3100 png_pass_inc
[png_ptr
->pass
] - 1 -
3101 png_pass_start
[png_ptr
->pass
]) /
3102 png_pass_inc
[png_ptr
->pass
];
3104 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
3106 png_ptr
->num_rows
= (png_ptr
->height
+
3107 png_pass_yinc
[png_ptr
->pass
] - 1 -
3108 png_pass_ystart
[png_ptr
->pass
]) /
3109 png_pass_yinc
[png_ptr
->pass
];
3110 if (!(png_ptr
->num_rows
))
3113 else /* if (png_ptr->transformations & PNG_INTERLACE) */
3115 } while (png_ptr
->iwidth
== 0);
3117 if (png_ptr
->pass
< 7)
3120 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3122 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
3124 #ifdef PNG_USE_LOCAL_ARRAYS
3130 png_ptr
->zstream
.next_out
= (Byte
*)&extra
;
3131 png_ptr
->zstream
.avail_out
= (uInt
)1;
3134 if (!(png_ptr
->zstream
.avail_in
))
3136 while (!png_ptr
->idat_size
)
3138 png_byte chunk_length
[4];
3140 png_crc_finish(png_ptr
, 0);
3142 png_read_data(png_ptr
, chunk_length
, 4);
3143 png_ptr
->idat_size
= png_get_uint_31(png_ptr
, chunk_length
);
3144 png_reset_crc(png_ptr
);
3145 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
3146 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
3147 png_error(png_ptr
, "Not enough image data");
3150 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
3151 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
3152 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
3153 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
3154 png_crc_read(png_ptr
, png_ptr
->zbuf
, png_ptr
->zstream
.avail_in
);
3155 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
3157 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
3158 if (ret
== Z_STREAM_END
)
3160 if (!(png_ptr
->zstream
.avail_out
) || png_ptr
->zstream
.avail_in
||
3162 png_warning(png_ptr
, "Extra compressed data.");
3163 png_ptr
->mode
|= PNG_AFTER_IDAT
;
3164 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
3168 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
3169 "Decompression Error");
3171 if (!(png_ptr
->zstream
.avail_out
))
3173 png_warning(png_ptr
, "Extra compressed data.");
3174 png_ptr
->mode
|= PNG_AFTER_IDAT
;
3175 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
3180 png_ptr
->zstream
.avail_out
= 0;
3183 if (png_ptr
->idat_size
|| png_ptr
->zstream
.avail_in
)
3184 png_warning(png_ptr
, "Extra compression data.");
3186 inflateReset(&png_ptr
->zstream
);
3188 png_ptr
->mode
|= PNG_AFTER_IDAT
;
3190 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
3193 png_read_start_row(png_structp png_ptr
)
3195 #ifdef PNG_READ_INTERLACING_SUPPORTED
3196 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3198 /* Start of interlace block */
3199 PNG_CONST
int png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
3201 /* Offset to next interlace block */
3202 PNG_CONST
int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
3204 /* Start of interlace block in the y direction */
3205 PNG_CONST
int png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
3207 /* Offset to next interlace block in the y direction */
3208 PNG_CONST
int png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
3211 int max_pixel_depth
;
3212 png_size_t row_bytes
;
3214 png_debug(1, "in png_read_start_row");
3215 png_ptr
->zstream
.avail_in
= 0;
3216 png_init_read_transformations(png_ptr
);
3217 #ifdef PNG_READ_INTERLACING_SUPPORTED
3218 if (png_ptr
->interlaced
)
3220 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
3221 png_ptr
->num_rows
= (png_ptr
->height
+ png_pass_yinc
[0] - 1 -
3222 png_pass_ystart
[0]) / png_pass_yinc
[0];
3224 png_ptr
->num_rows
= png_ptr
->height
;
3226 png_ptr
->iwidth
= (png_ptr
->width
+
3227 png_pass_inc
[png_ptr
->pass
] - 1 -
3228 png_pass_start
[png_ptr
->pass
]) /
3229 png_pass_inc
[png_ptr
->pass
];
3232 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3234 png_ptr
->num_rows
= png_ptr
->height
;
3235 png_ptr
->iwidth
= png_ptr
->width
;
3237 max_pixel_depth
= png_ptr
->pixel_depth
;
3239 #ifdef PNG_READ_PACK_SUPPORTED
3240 if ((png_ptr
->transformations
& PNG_PACK
) && png_ptr
->bit_depth
< 8)
3241 max_pixel_depth
= 8;
3244 #ifdef PNG_READ_EXPAND_SUPPORTED
3245 if (png_ptr
->transformations
& PNG_EXPAND
)
3247 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
3249 if (png_ptr
->num_trans
)
3250 max_pixel_depth
= 32;
3252 max_pixel_depth
= 24;
3254 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
3256 if (max_pixel_depth
< 8)
3257 max_pixel_depth
= 8;
3258 if (png_ptr
->num_trans
)
3259 max_pixel_depth
*= 2;
3261 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
3263 if (png_ptr
->num_trans
)
3265 max_pixel_depth
*= 4;
3266 max_pixel_depth
/= 3;
3272 #ifdef PNG_READ_FILLER_SUPPORTED
3273 if (png_ptr
->transformations
& (PNG_FILLER
))
3275 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
3276 max_pixel_depth
= 32;
3277 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)
3279 if (max_pixel_depth
<= 8)
3280 max_pixel_depth
= 16;
3282 max_pixel_depth
= 32;
3284 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB
)
3286 if (max_pixel_depth
<= 32)
3287 max_pixel_depth
= 32;
3289 max_pixel_depth
= 64;
3294 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
3295 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
3298 #ifdef PNG_READ_EXPAND_SUPPORTED
3299 (png_ptr
->num_trans
&& (png_ptr
->transformations
& PNG_EXPAND
)) ||
3301 #ifdef PNG_READ_FILLER_SUPPORTED
3302 (png_ptr
->transformations
& (PNG_FILLER
)) ||
3304 png_ptr
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
3306 if (max_pixel_depth
<= 16)
3307 max_pixel_depth
= 32;
3309 max_pixel_depth
= 64;
3313 if (max_pixel_depth
<= 8)
3315 if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
3316 max_pixel_depth
= 32;
3318 max_pixel_depth
= 24;
3320 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
3321 max_pixel_depth
= 64;
3323 max_pixel_depth
= 48;
3328 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3329 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3330 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
3332 int user_pixel_depth
= png_ptr
->user_transform_depth
*
3333 png_ptr
->user_transform_channels
;
3334 if (user_pixel_depth
> max_pixel_depth
)
3335 max_pixel_depth
=user_pixel_depth
;
3339 /* Align the width on the next larger 8 pixels. Mainly used
3342 row_bytes
= ((png_ptr
->width
+ 7) & ~((png_uint_32
)7));
3343 /* Calculate the maximum bytes needed, adding a byte and a pixel
3346 row_bytes
= PNG_ROWBYTES(max_pixel_depth
, row_bytes
) +
3347 1 + ((max_pixel_depth
+ 7) >> 3);
3348 #ifdef PNG_MAX_MALLOC_64K
3349 if (row_bytes
> (png_uint_32
)65536L)
3350 png_error(png_ptr
, "This image requires a row greater than 64KB");
3353 if (row_bytes
+ 64 > png_ptr
->old_big_row_buf_size
)
3355 png_free(png_ptr
, png_ptr
->big_row_buf
);
3356 if (png_ptr
->interlaced
)
3357 png_ptr
->big_row_buf
= (png_bytep
)png_calloc(png_ptr
,
3360 png_ptr
->big_row_buf
= (png_bytep
)png_malloc(png_ptr
,
3362 png_ptr
->old_big_row_buf_size
= row_bytes
+ 64;
3364 /* Use 32 bytes of padding before and after row_buf. */
3365 png_ptr
->row_buf
= png_ptr
->big_row_buf
+ 32;
3366 png_ptr
->old_big_row_buf_size
= row_bytes
+ 64;
3369 #ifdef PNG_MAX_MALLOC_64K
3370 if ((png_uint_32
)row_bytes
+ 1 > (png_uint_32
)65536L)
3371 png_error(png_ptr
, "This image requires a row greater than 64KB");
3373 if ((png_uint_32
)row_bytes
> (png_uint_32
)(PNG_SIZE_MAX
- 1))
3374 png_error(png_ptr
, "Row has too many bytes to allocate in memory.");
3376 if (row_bytes
+ 1 > png_ptr
->old_prev_row_size
)
3378 png_free(png_ptr
, png_ptr
->prev_row
);
3379 png_ptr
->prev_row
= (png_bytep
)png_malloc(png_ptr
, (png_uint_32
)(
3381 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0, row_bytes
+ 1);
3382 png_ptr
->old_prev_row_size
= row_bytes
+ 1;
3385 png_ptr
->rowbytes
= row_bytes
;
3387 png_debug1(3, "width = %lu,", png_ptr
->width
);
3388 png_debug1(3, "height = %lu,", png_ptr
->height
);
3389 png_debug1(3, "iwidth = %lu,", png_ptr
->iwidth
);
3390 png_debug1(3, "num_rows = %lu,", png_ptr
->num_rows
);
3391 png_debug1(3, "rowbytes = %lu,", png_ptr
->rowbytes
);
3392 png_debug1(3, "irowbytes = %lu",
3393 PNG_ROWBYTES(png_ptr
->pixel_depth
, png_ptr
->iwidth
) + 1);
3395 png_ptr
->flags
|= PNG_FLAG_ROW_INIT
;
3397 #endif /* PNG_READ_SUPPORTED */