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)
31 len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
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);
44 # define png_strtod(p,a,b) strtod(a,b)
51 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
61 png_error(png_ptr,
"PNG unsigned integer out of range.");
64 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
118 png_debug2(0,
"Reading %s chunk, length = %lu\n",
119 png_ptr->chunk_name,
length);
135 if (png_ptr == NULL)
return;
152 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
161 if (((png_ptr->chunk_name[0] & 0x20) &&
162 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
163 (!(png_ptr->chunk_name[0] & 0x20) &&
164 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
187 if (png_ptr->chunk_name[0] & 0x20)
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)
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)
224 static PNG_CONST char msg[] =
"Error decoding compressed text";
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)
244 if (png_ptr->zstream.msg != NULL)
249 png_ptr->zstream.avail_in = 0;
253 text_size = prefix_size +
png_sizeof(msg) + 1;
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;
268 (text - png_ptr->chunkdata) - 1);
271 png_memcpy(text + prefix_size, msg, text_size);
278 text_size = prefix_size +
279 png_ptr->zbuf_size - png_ptr->zstream.avail_out;
283 png_free(png_ptr, png_ptr->chunkdata);
284 png_ptr->chunkdata = NULL;
286 "Not enough memory to decompress chunk.");
289 text_size - prefix_size);
290 png_memcpy(text, png_ptr->chunkdata, prefix_size);
291 *(text + text_size) = 0x00;
300 png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
304 png_free(png_ptr, png_ptr->chunkdata);
305 png_ptr->chunkdata = NULL;
307 "Not enough memory to decompress chunk..");
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;
320 png_ptr->zstream.next_out = png_ptr->zbuf;
321 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
327 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
332 "Buffer error in compressed datastream in %s chunk",
333 png_ptr->chunk_name);
336 "Data error in compressed datastream in %s chunk",
337 png_ptr->chunk_name);
340 "Incomplete compressed datastream in %s chunk",
341 png_ptr->chunk_name);
345 "Incomplete compressed datastream in chunk other than IDAT");
347 text_size = prefix_size;
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;
363 png_ptr->zstream.avail_in = 0;
365 png_free(png_ptr, png_ptr->chunkdata);
366 png_ptr->chunkdata = text;
367 *newlength=text_size;
371 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
374 png_snprintf(umsg, 50,
"Unknown zTXt compression type %d", comp_type);
377 png_warning(png_ptr,
"Unknown zTXt compression type");
380 *(png_ptr->chunkdata + prefix_size) = 0x00;
381 *newlength = prefix_size;
402 png_error(png_ptr,
"Invalid IHDR chunk");
414 filter_type =
buf[11];
415 interlace_type =
buf[12];
418 png_ptr->width =
width;
421 png_ptr->interlaced = (
png_byte)interlace_type;
423 #if defined(PNG_MNG_FEATURES_SUPPORTED)
424 png_ptr->filter_type = (
png_byte)filter_type;
429 switch (png_ptr->color_type)
433 png_ptr->channels = 1;
436 png_ptr->channels = 3;
439 png_ptr->channels = 2;
442 png_ptr->channels = 4;
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);
463 #ifndef PNG_NO_POINTER_INDEXING
470 png_error(png_ptr,
"Missing IHDR before PLTE");
478 png_error(png_ptr,
"Duplicate PLTE chunk");
485 "Ignoring PLTE chunk in grayscale PNG");
489 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
507 png_error(png_ptr,
"Invalid palette chunk");
513 #ifndef PNG_NO_POINTER_INDEXING
519 pal_ptr->red =
buf[0];
520 pal_ptr->green =
buf[1];
521 pal_ptr->blue =
buf[2];
540 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
546 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
553 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
555 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
566 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
575 #if defined(PNG_READ_tRNS_SUPPORTED)
582 png_warning(png_ptr,
"Truncating incorrect tRNS chunk length");
587 png_warning(png_ptr,
"Truncating incorrect info tRNS chunk length");
610 png_warning(png_ptr,
"Incorrect IEND chunk length");
617 #if defined(PNG_READ_gAMA_SUPPORTED)
622 #ifdef PNG_FLOATING_POINT_SUPPORTED
630 png_error(png_ptr,
"Missing IHDR before gAMA");
654 png_warning(png_ptr,
"Incorrect gAMA chunk length");
668 "Ignoring gAMA chunk with gamma=0");
672 #if defined(PNG_READ_sRGB_SUPPORTED)
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);
685 #ifdef PNG_FLOATING_POINT_SUPPORTED
687 # ifdef PNG_READ_GAMMA_SUPPORTED
692 #ifdef PNG_FIXED_POINT_SUPPORTED
698 #if defined(PNG_READ_sBIT_SUPPORTED)
710 png_error(png_ptr,
"Missing IHDR before sBIT");
736 png_warning(png_ptr,
"Incorrect sBIT chunk length");
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];
764 #if defined(PNG_READ_cHRM_SUPPORTED)
769 #ifdef PNG_FLOATING_POINT_SUPPORTED
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;
780 png_error(png_ptr,
"Missing IHDR before cHRM");
804 png_warning(png_ptr,
"Incorrect cHRM chunk length");
815 if (uint_x > 80000L || uint_y > 80000L ||
816 uint_x + uint_y > 100000L)
826 if (uint_x + uint_y > 100000L)
836 if (uint_x + uint_y > 100000L)
846 if (uint_x + uint_y > 100000L)
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)
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",
883 fprintf(stderr,
"gx=%f, gy=%f, bx=%f, by=%f\n",
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);
897 #ifdef PNG_FLOATING_POINT_SUPPORTED
901 #ifdef PNG_FIXED_POINT_SUPPORTED
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)
919 png_error(png_ptr,
"Missing IHDR before sRGB");
939 png_warning(png_ptr,
"Incorrect sRGB chunk length");
956 #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
960 #ifdef PNG_FIXED_POINT_SUPPORTED
963 # ifdef PNG_FLOATING_POINT_SUPPORTED
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);
985 #ifdef PNG_READ_cHRM_SUPPORTED
986 #ifdef PNG_FIXED_POINT_SUPPORTED
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");
1007 #if defined(PNG_READ_iCCP_SUPPORTED)
1017 png_size_t slength, prefix_length, data_length;
1022 png_error(png_ptr,
"Missing IHDR before iCCP");
1040 #ifdef PNG_MAX_MALLOC_64K
1043 png_warning(png_ptr,
"iCCP chunk too large to fit in memory");
1049 png_free(png_ptr, png_ptr->chunkdata);
1056 png_free(png_ptr, png_ptr->chunkdata);
1057 png_ptr->chunkdata = NULL;
1061 png_ptr->chunkdata[slength] = 0x00;
1070 if (
profile >= png_ptr->chunkdata + slength - 1)
1072 png_free(png_ptr, png_ptr->chunkdata);
1073 png_ptr->chunkdata = NULL;
1082 png_warning(png_ptr,
"Ignoring nonzero compression type in iCCP chunk");
1087 prefix_length =
profile - png_ptr->chunkdata;
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");
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.");
1121 png_free(png_ptr, png_ptr->chunkdata);
1122 png_ptr->chunkdata = NULL;
1126 #if defined(PNG_READ_sPLT_SUPPORTED)
1133 #ifdef PNG_NO_POINTER_INDEXING
1136 int data_length, entry_size,
i;
1143 png_error(png_ptr,
"Missing IHDR before sPLT");
1151 #ifdef PNG_MAX_MALLOC_64K
1154 png_warning(png_ptr,
"sPLT chunk too large to fit in memory");
1160 png_free(png_ptr, png_ptr->chunkdata);
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++)
1179 if (entry_start > (
png_bytep)png_ptr->chunkdata + slength - 2)
1181 png_free(png_ptr, png_ptr->chunkdata);
1182 png_ptr->chunkdata = NULL;
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));
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");
1209 if (new_palette.
entries == NULL)
1211 png_warning(png_ptr,
"sPLT chunk requires too much memory");
1215 #ifndef PNG_NO_POINTER_INDEXING
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++;
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++;
1260 new_palette.
name = png_ptr->chunkdata;
1264 png_free(png_ptr, png_ptr->chunkdata);
1265 png_ptr->chunkdata = NULL;
1270 #if defined(PNG_READ_tRNS_SUPPORTED)
1279 png_error(png_ptr,
"Missing IHDR before tRNS");
1299 png_warning(png_ptr,
"Incorrect tRNS chunk length");
1305 png_ptr->num_trans = 1;
1314 png_warning(png_ptr,
"Incorrect tRNS chunk length");
1319 png_ptr->num_trans = 1;
1334 png_warning(png_ptr,
"Incorrect tRNS chunk length");
1349 png_warning(png_ptr,
"tRNS chunk not allowed with alpha channel");
1356 png_ptr->num_trans = 0;
1361 &(png_ptr->trans_values));
1365 #if defined(PNG_READ_bKGD_SUPPORTED)
1375 png_error(png_ptr,
"Missing IHDR before bKGD");
1405 png_warning(png_ptr,
"Incorrect bKGD chunk length");
1420 png_ptr->background.index =
buf[0];
1425 png_warning(png_ptr,
"Incorrect bKGD chunk index value");
1428 png_ptr->background.red =
1430 png_ptr->background.green =
1432 png_ptr->background.blue =
1438 png_ptr->background.red =
1439 png_ptr->background.green =
1440 png_ptr->background.blue =
1454 #if defined(PNG_READ_hIST_SUPPORTED)
1458 unsigned int num,
i;
1464 png_error(png_ptr,
"Missing IHDR before hIST");
1485 if (
num != (
unsigned int) png_ptr->num_palette ||
num >
1488 png_warning(png_ptr,
"Incorrect hIST chunk length");
1493 for (
i = 0;
i <
num;
i++)
1508 #if defined(PNG_READ_pHYs_SUPPORTED)
1519 png_error(png_ptr,
"Missing IHDR before pHYs");
1535 png_warning(png_ptr,
"Incorrect pHYs chunk length");
1551 #if defined(PNG_READ_oFFs_SUPPORTED)
1562 png_error(png_ptr,
"Missing IHDR before oFFs");
1578 png_warning(png_ptr,
"Incorrect oFFs chunk length");
1594 #if defined(PNG_READ_pCAL_SUPPORTED)
1609 png_error(png_ptr,
"Missing IHDR before pCAL");
1623 png_debug1(2,
"Allocating and reading pCAL chunk data (%lu bytes)\n",
1625 png_free(png_ptr, png_ptr->chunkdata);
1627 if (png_ptr->chunkdata == NULL)
1629 png_warning(png_ptr,
"No memory for pCAL purpose.");
1637 png_free(png_ptr, png_ptr->chunkdata);
1638 png_ptr->chunkdata = NULL;
1642 png_ptr->chunkdata[slength] = 0x00;
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;
1652 if (endptr <=
buf + 12)
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");
1667 png_debug(3,
"Checking pCAL equation type and number of parameters\n");
1675 png_warning(png_ptr,
"Invalid pCAL parameters for equation type");
1676 png_free(png_ptr, png_ptr->chunkdata);
1677 png_ptr->chunkdata = NULL;
1682 png_warning(png_ptr,
"Unrecognized equation type for pCAL chunk");
1688 png_debug(3,
"Allocating pCAL parameters array\n");
1693 png_free(png_ptr, png_ptr->chunkdata);
1694 png_ptr->chunkdata = NULL;
1695 png_warning(png_ptr,
"No memory for pCAL params.");
1712 png_free(png_ptr, png_ptr->chunkdata);
1713 png_ptr->chunkdata = NULL;
1722 png_free(png_ptr, png_ptr->chunkdata);
1723 png_ptr->chunkdata = NULL;
1728 #if defined(PNG_READ_sCAL_SUPPORTED)
1734 #ifdef PNG_FLOATING_POINT_SUPPORTED
1738 #ifdef PNG_FIXED_POINT_SUPPORTED
1747 png_error(png_ptr,
"Missing IHDR before sCAL");
1761 png_debug1(2,
"Allocating and reading sCAL chunk data (%lu bytes)\n",
1764 if (png_ptr->chunkdata == NULL)
1766 png_warning(png_ptr,
"Out of memory while processing sCAL chunk");
1774 png_free(png_ptr, png_ptr->chunkdata);
1775 png_ptr->chunkdata = NULL;
1779 png_ptr->chunkdata[slength] = 0x00;
1781 ep = png_ptr->chunkdata + 1;
1783 #ifdef PNG_FLOATING_POINT_SUPPORTED
1787 png_warning(png_ptr,
"malformed width string in sCAL chunk");
1791 #ifdef PNG_FIXED_POINT_SUPPORTED
1795 png_warning(png_ptr,
"Out of memory while processing sCAL chunk width");
1802 for (ep = png_ptr->chunkdata; *ep; ep++)
1806 if (png_ptr->chunkdata + slength < ep)
1809 #if defined(PNG_FIXED_POINT_SUPPORTED) && \
1810 !defined(PNG_FLOATING_POINT_SUPPORTED)
1813 png_free(png_ptr, png_ptr->chunkdata);
1814 png_ptr->chunkdata = NULL;
1818 #ifdef PNG_FLOATING_POINT_SUPPORTED
1822 png_warning(png_ptr,
"malformed height string in sCAL chunk");
1826 #ifdef PNG_FIXED_POINT_SUPPORTED
1828 if (sheight == NULL)
1830 png_warning(png_ptr,
"Out of memory while processing sCAL chunk height");
1837 if (png_ptr->chunkdata + slength < ep
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)
1854 #ifdef PNG_FLOATING_POINT_SUPPORTED
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)
1871 #if defined(PNG_READ_tIME_SUPPORTED)
1881 png_error(png_ptr,
"Out of place tIME chunk");
1894 png_warning(png_ptr,
"Incorrect tIME chunk length");
1914 #if defined(PNG_READ_tEXt_SUPPORTED)
1929 png_error(png_ptr,
"Missing IHDR before tEXt");
1934 #ifdef PNG_MAX_MALLOC_64K
1937 png_warning(png_ptr,
"tEXt chunk too large to fit in memory");
1946 png_warning(png_ptr,
"No memory to process text chunk.");
1958 key[slength] = 0x00;
1960 for (text = key; *text; text++)
1963 if (text != key + slength)
1970 png_warning(png_ptr,
"Not enough memory to process text chunk.");
1976 #ifdef PNG_iTXt_SUPPORTED
1989 png_warning(png_ptr,
"Insufficient memory to process text chunk.");
1993 #if defined(PNG_READ_zTXt_SUPPORTED)
2006 png_error(png_ptr,
"Missing IHDR before zTXt");
2011 #ifdef PNG_MAX_MALLOC_64K
2016 png_warning(png_ptr,
"zTXt chunk too large to fit in memory");
2022 png_free(png_ptr,png_ptr->chunkdata);
2024 if (png_ptr->chunkdata == NULL)
2026 png_warning(png_ptr,
"Out of memory processing zTXt chunk.");
2033 png_free(png_ptr, png_ptr->chunkdata);
2034 png_ptr->chunkdata = NULL;
2038 png_ptr->chunkdata[slength] = 0x00;
2040 for (text = png_ptr->chunkdata; *text; text++)
2044 if (text >= png_ptr->chunkdata + slength - 2)
2047 png_free(png_ptr, png_ptr->chunkdata);
2048 png_ptr->chunkdata = NULL;
2053 comp_type = *(++text);
2056 png_warning(png_ptr,
"Unknown compression type in zTXt chunk");
2061 prefix_len = text - png_ptr->chunkdata;
2070 png_warning(png_ptr,
"Not enough memory to process zTXt chunk.");
2071 png_free(png_ptr, png_ptr->chunkdata);
2072 png_ptr->chunkdata = NULL;
2076 text_ptr->key = png_ptr->chunkdata;
2077 #ifdef PNG_iTXt_SUPPORTED
2082 text_ptr->text = png_ptr->chunkdata + prefix_len;
2088 png_free(png_ptr, png_ptr->chunkdata);
2089 png_ptr->chunkdata = NULL;
2091 png_error(png_ptr,
"Insufficient memory to store zTXt chunk.");
2095 #if defined(PNG_READ_iTXt_SUPPORTED)
2110 png_error(png_ptr,
"Missing IHDR before iTXt");
2115 #ifdef PNG_MAX_MALLOC_64K
2120 png_warning(png_ptr,
"iTXt chunk too large to fit in memory");
2126 png_free(png_ptr, png_ptr->chunkdata);
2128 if (png_ptr->chunkdata == NULL)
2130 png_warning(png_ptr,
"No memory to process iTXt chunk.");
2137 png_free(png_ptr, png_ptr->chunkdata);
2138 png_ptr->chunkdata = NULL;
2142 png_ptr->chunkdata[slength] = 0x00;
2144 for (lang = png_ptr->chunkdata; *lang; lang++)
2152 if (lang >= png_ptr->chunkdata + slength - 3)
2155 png_free(png_ptr, png_ptr->chunkdata);
2156 png_ptr->chunkdata = NULL;
2161 comp_flag = *lang++;
2162 comp_type = *lang++;
2165 for (lang_key = lang; *lang_key; lang_key++)
2169 if (lang_key >= png_ptr->chunkdata + slength)
2172 png_free(png_ptr, png_ptr->chunkdata);
2173 png_ptr->chunkdata = NULL;
2177 for (text = lang_key; *text; text++)
2180 if (text >= png_ptr->chunkdata + slength)
2183 png_free(png_ptr, png_ptr->chunkdata);
2184 png_ptr->chunkdata = NULL;
2188 prefix_len = text - png_ptr->chunkdata;
2190 key=png_ptr->chunkdata;
2193 (
size_t)
length, prefix_len, &data_len);
2195 data_len =
png_strlen(png_ptr->chunkdata + prefix_len);
2200 png_warning(png_ptr,
"Not enough memory to process iTXt chunk.");
2201 png_free(png_ptr, png_ptr->chunkdata);
2202 png_ptr->chunkdata = NULL;
2206 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2207 text_ptr->lang = png_ptr->chunkdata + (lang - key);
2210 text_ptr->key = png_ptr->chunkdata;
2211 text_ptr->text = png_ptr->chunkdata + prefix_len;
2216 png_free(png_ptr, png_ptr->chunkdata);
2217 png_ptr->chunkdata = NULL;
2219 png_error(png_ptr,
"Insufficient memory to store iTXt chunk.");
2233 png_debug(1,
"in png_handle_unknown\n");
2237 #ifdef PNG_USE_LOCAL_ARRAYS
2240 if (
png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
2244 if (!(png_ptr->chunk_name[0] & 0x20))
2246 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2250 && png_ptr->read_user_chunk_fn == NULL
2257 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2258 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
2259 (png_ptr->read_user_chunk_fn != NULL))
2261 #ifdef PNG_MAX_MALLOC_64K
2264 png_warning(png_ptr,
"unknown chunk too large to fit in memory");
2272 png_ptr->unknown_chunk.name[
png_sizeof(png_ptr->unknown_chunk.name)-1] =
'\0';
2275 png_ptr->unknown_chunk.data = NULL;
2281 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2282 if (png_ptr->read_user_chunk_fn != NULL)
2286 ret = (*(png_ptr->read_user_chunk_fn))
2287 (png_ptr, &png_ptr->unknown_chunk);
2292 if (!(png_ptr->chunk_name[0] & 0x20))
2297 &png_ptr->unknown_chunk, 1);
2303 png_free(png_ptr, png_ptr->unknown_chunk.data);
2304 png_ptr->unknown_chunk.data = NULL;
2312 #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2323 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
2328 png_debug(1,
"in png_check_chunk_name\n");
2354 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
2358 switch (png_ptr->row_info.pixel_depth)
2364 int s_inc, s_start, s_end;
2370 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2371 if (png_ptr->transformations & PNG_PACKSWAP)
2387 for (
i = 0;
i < row_width;
i++)
2393 value = (*sp >> shift) & 0x01;
2394 *dp &= (
png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2418 int s_start, s_end, s_inc;
2425 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2426 if (png_ptr->transformations & PNG_PACKSWAP)
2442 for (
i = 0;
i < row_width;
i++)
2446 value = (*sp >> shift) & 0x03;
2447 *dp &= (
png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2470 int s_start, s_end, s_inc;
2477 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2478 if (png_ptr->transformations & PNG_PACKSWAP)
2493 for (
i = 0;
i < row_width;
i++)
2497 value = (*sp >> shift) & 0xf;
2498 *dp &= (
png_byte)((0xf0f >> (4 - shift)) & 0xff);
2521 png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
2527 for (
i = 0;
i < row_width;
i++)
2548 #ifdef PNG_READ_INTERLACING_SUPPORTED
2558 int pass = png_ptr->pass;
2559 png_uint_32 transformations = png_ptr->transformations;
2560 #ifdef PNG_USE_LOCAL_ARRAYS
2566 png_debug(1,
"in png_do_read_interlace\n");
2567 if (
row != NULL && row_info != NULL)
2573 switch (row_info->pixel_depth)
2580 int s_start, s_end, s_inc;
2586 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2587 if (transformations & PNG_PACKSWAP)
2589 sshift = (
int)((row_info->width + 7) & 0x07);
2590 dshift = (
int)((final_width + 7) & 0x07);
2598 sshift = 7 - (
int)((row_info->width + 7) & 0x07);
2599 dshift = 7 - (
int)((final_width + 7) & 0x07);
2605 for (
i = 0;
i < row_info->width;
i++)
2607 v = (
png_byte)((*sp >> sshift) & 0x01);
2608 for (j = 0; j < jstop; j++)
2610 *dp &= (
png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
2612 if (dshift == s_end)
2620 if (sshift == s_end)
2635 int s_start, s_end, s_inc;
2639 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2640 if (transformations & PNG_PACKSWAP)
2642 sshift = (
int)(((row_info->width + 3) & 0x03) << 1);
2643 dshift = (
int)(((final_width + 3) & 0x03) << 1);
2651 sshift = (
int)((3 - ((row_info->width + 3) & 0x03)) << 1);
2652 dshift = (
int)((3 - ((final_width + 3) & 0x03)) << 1);
2658 for (
i = 0;
i < row_info->width;
i++)
2663 v = (
png_byte)((*sp >> sshift) & 0x03);
2664 for (j = 0; j < jstop; j++)
2666 *dp &= (
png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
2668 if (dshift == s_end)
2676 if (sshift == s_end)
2691 int s_start, s_end, s_inc;
2695 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
2696 if (transformations & PNG_PACKSWAP)
2698 sshift = (
int)(((row_info->width + 1) & 0x01) << 2);
2699 dshift = (
int)(((final_width + 1) & 0x01) << 2);
2707 sshift = (
int)((1 - ((row_info->width + 1) & 0x01)) << 2);
2708 dshift = (
int)((1 - ((final_width + 1) & 0x01)) << 2);
2714 for (
i = 0;
i < row_info->width;
i++)
2719 for (j = 0; j < jstop; j++)
2721 *dp &= (
png_byte)((0xf0f >> (4 - dshift)) & 0xff);
2723 if (dshift == s_end)
2731 if (sshift == s_end)
2743 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
2750 for (
i = 0;
i < row_info->width;
i++)
2756 for (j = 0; j < jstop; j++)
2766 row_info->width = final_width;
2767 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
2769 #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
2770 transformations = transformations;
2779 png_debug(1,
"in png_read_filter_row\n");
2780 png_debug2(2,
"row = %lu, filter = %d\n", png_ptr->row_number, filter);
2789 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2793 for (
i = bpp;
i < istop;
i++)
2795 *rp = (
png_byte)(((
int)(*rp) + (
int)(*lp++)) & 0xff);
2807 for (
i = 0;
i < istop;
i++)
2809 *rp = (
png_byte)(((
int)(*rp) + (
int)(*pp++)) & 0xff);
2820 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2823 for (
i = 0;
i < bpp;
i++)
2826 ((
int)(*pp++) / 2 )) & 0xff);
2830 for (
i = 0;
i < istop;
i++)
2833 (
int)(*pp++ + *lp++) / 2 ) & 0xff);
2845 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2848 for (
i = 0;
i < bpp;
i++)
2850 *rp = (
png_byte)(((
int)(*rp) + (
int)(*pp++)) & 0xff);
2854 for (
i = 0;
i < istop;
i++)
2856 int a,
b,
c, pa, pb, pc, p;
2870 pa = p < 0 ? -p : p;
2871 pb = pc < 0 ? -pc : pc;
2872 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2884 p = (pa <= pb && pa <= pc) ?
a : (pb <= pc) ?
b :
c;
2886 *rp = (
png_byte)(((
int)(*rp) + p) & 0xff);
2892 png_warning(png_ptr,
"Ignoring bad adaptive filter type");
2901 #ifdef PNG_USE_LOCAL_ARRAYS
2902 #ifdef PNG_READ_INTERLACING_SUPPORTED
2919 png_debug(1,
"in png_read_finish_row\n");
2920 png_ptr->row_number++;
2921 if (png_ptr->row_number < png_ptr->num_rows)
2924 #ifdef PNG_READ_INTERLACING_SUPPORTED
2925 if (png_ptr->interlaced)
2927 png_ptr->row_number = 0;
2929 png_ptr->rowbytes + 1);
2933 if (png_ptr->pass >= 7)
2935 png_ptr->iwidth = (png_ptr->width +
2940 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
2941 png_ptr->iwidth) + 1;
2943 if (!(png_ptr->transformations & PNG_INTERLACE))
2945 png_ptr->num_rows = (png_ptr->height +
2949 if (!(png_ptr->num_rows))
2954 }
while (png_ptr->iwidth == 0);
2956 if (png_ptr->pass < 7)
2961 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
2963 #ifdef PNG_USE_LOCAL_ARRAYS
2969 png_ptr->zstream.next_out = (
Byte *)&extra;
2970 png_ptr->zstream.avail_out = (
uInt)1;
2973 if (!(png_ptr->zstream.avail_in))
2975 while (!png_ptr->idat_size)
2985 if (
png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
2986 png_error(png_ptr,
"Not enough image data");
2989 png_ptr->zstream.avail_in = (
uInt)png_ptr->zbuf_size;
2990 png_ptr->zstream.next_in = png_ptr->zbuf;
2991 if (png_ptr->zbuf_size > png_ptr->idat_size)
2992 png_ptr->zstream.avail_in = (
uInt)png_ptr->idat_size;
2993 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
2994 png_ptr->idat_size -= png_ptr->zstream.avail_in;
2999 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
3003 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3007 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
3008 "Decompression Error");
3010 if (!(png_ptr->zstream.avail_out))
3014 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
3019 png_ptr->zstream.avail_out = 0;
3022 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
3033 #ifdef PNG_USE_LOCAL_ARRAYS
3034 #ifdef PNG_READ_INTERLACING_SUPPORTED
3051 int max_pixel_depth;
3054 png_debug(1,
"in png_read_start_row\n");
3055 png_ptr->zstream.avail_in = 0;
3057 #ifdef PNG_READ_INTERLACING_SUPPORTED
3058 if (png_ptr->interlaced)
3060 if (!(png_ptr->transformations & PNG_INTERLACE))
3061 png_ptr->num_rows = (png_ptr->height +
png_pass_yinc[0] - 1 -
3064 png_ptr->num_rows = png_ptr->height;
3066 png_ptr->iwidth = (png_ptr->width +
3071 png_ptr->irowbytes =
3072 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
3077 png_ptr->num_rows = png_ptr->height;
3078 png_ptr->iwidth = png_ptr->width;
3079 png_ptr->irowbytes = png_ptr->rowbytes + 1;
3081 max_pixel_depth = png_ptr->pixel_depth;
3083 #if defined(PNG_READ_PACK_SUPPORTED)
3084 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
3085 max_pixel_depth = 8;
3088 #if defined(PNG_READ_EXPAND_SUPPORTED)
3089 if (png_ptr->transformations & PNG_EXPAND)
3093 if (png_ptr->num_trans)
3094 max_pixel_depth = 32;
3096 max_pixel_depth = 24;
3100 if (max_pixel_depth < 8)
3101 max_pixel_depth = 8;
3102 if (png_ptr->num_trans)
3103 max_pixel_depth *= 2;
3107 if (png_ptr->num_trans)
3109 max_pixel_depth *= 4;
3110 max_pixel_depth /= 3;
3116 #if defined(PNG_READ_FILLER_SUPPORTED)
3117 if (png_ptr->transformations & (PNG_FILLER))
3120 max_pixel_depth = 32;
3123 if (max_pixel_depth <= 8)
3124 max_pixel_depth = 16;
3126 max_pixel_depth = 32;
3130 if (max_pixel_depth <= 32)
3131 max_pixel_depth = 32;
3133 max_pixel_depth = 64;
3138 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
3139 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
3143 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
3145 #if defined(PNG_READ_FILLER_SUPPORTED)
3146 (png_ptr->transformations & (PNG_FILLER)) ||
3150 if (max_pixel_depth <= 16)
3151 max_pixel_depth = 32;
3153 max_pixel_depth = 64;
3157 if (max_pixel_depth <= 8)
3160 max_pixel_depth = 32;
3162 max_pixel_depth = 24;
3165 max_pixel_depth = 64;
3167 max_pixel_depth = 48;
3172 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3173 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3174 if (png_ptr->transformations & PNG_USER_TRANSFORM)
3176 int user_pixel_depth = png_ptr->user_transform_depth*
3177 png_ptr->user_transform_channels;
3178 if (user_pixel_depth > max_pixel_depth)
3179 max_pixel_depth=user_pixel_depth;
3185 row_bytes = ((png_ptr->width + 7) & ~((
png_uint_32)7));
3188 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
3189 1 + ((max_pixel_depth + 7) >> 3);
3190 #ifdef PNG_MAX_MALLOC_64K
3192 png_error(png_ptr,
"This image requires a row greater than 64KB");
3195 if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
3197 png_free(png_ptr, png_ptr->big_row_buf);
3199 png_ptr->row_buf = png_ptr->big_row_buf+32;
3200 png_ptr->old_big_row_buf_size = row_bytes+64;
3203 #ifdef PNG_MAX_MALLOC_64K
3205 png_error(png_ptr,
"This image requires a row greater than 64KB");
3208 png_error(png_ptr,
"Row has too many bytes to allocate in memory.");
3210 if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
3212 png_free(png_ptr, png_ptr->prev_row);
3214 png_ptr->rowbytes + 1));
3215 png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
3220 png_debug1(3,
"width = %lu,\n", png_ptr->width);
3221 png_debug1(3,
"height = %lu,\n", png_ptr->height);
3222 png_debug1(3,
"iwidth = %lu,\n", png_ptr->iwidth);
3223 png_debug1(3,
"num_rows = %lu\n", png_ptr->num_rows);
3224 png_debug1(3,
"rowbytes = %lu,\n", png_ptr->rowbytes);
3225 png_debug1(3,
"irowbytes = %lu,\n", png_ptr->irowbytes);
3227 png_ptr->flags |= PNG_FLAG_ROW_INIT;