The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3163 lines
93KB

  1. /* pngrutil.c - utilities to read a PNG file
  2. *
  3. * Last changed in libpng 1.2.21 [October 4, 2007]
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2007 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.)
  8. *
  9. * This file contains routines that are only called from within
  10. * libpng itself during the course of reading an image.
  11. */
  12. #define PNG_INTERNAL
  13. #include "png.h"
  14. #if defined(PNG_READ_SUPPORTED)
  15. #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
  16. # define WIN32_WCE_OLD
  17. #endif
  18. #ifdef PNG_FLOATING_POINT_SUPPORTED
  19. # if defined(WIN32_WCE_OLD)
  20. /* strtod() function is not supported on WindowsCE */
  21. __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
  22. {
  23. double result = 0;
  24. int len;
  25. wchar_t *str, *end;
  26. len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
  27. str = (wchar_t *)png_malloc(png_ptr, len * sizeof(wchar_t));
  28. if ( NULL != str )
  29. {
  30. MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
  31. result = wcstod(str, &end);
  32. len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
  33. *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
  34. png_free(png_ptr, str);
  35. }
  36. return result;
  37. }
  38. # else
  39. # define png_strtod(p,a,b) strtod(a,b)
  40. # endif
  41. #endif
  42. png_uint_32 PNGAPI
  43. png_get_uint_31(png_structp png_ptr, png_bytep buf)
  44. {
  45. png_uint_32 i = png_get_uint_32(buf);
  46. if (i > PNG_UINT_31_MAX)
  47. png_error(png_ptr, "PNG unsigned integer out of range.");
  48. return (i);
  49. }
  50. #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
  51. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  52. png_uint_32 PNGAPI
  53. png_get_uint_32(png_bytep buf)
  54. {
  55. png_uint_32 i = ((png_uint_32)(*buf) << 24) +
  56. ((png_uint_32)(*(buf + 1)) << 16) +
  57. ((png_uint_32)(*(buf + 2)) << 8) +
  58. (png_uint_32)(*(buf + 3));
  59. return (i);
  60. }
  61. /* Grab a signed 32-bit integer from a buffer in big-endian format. The
  62. * data is stored in the PNG file in two's complement format, and it is
  63. * assumed that the machine format for signed integers is the same. */
  64. png_int_32 PNGAPI
  65. png_get_int_32(png_bytep buf)
  66. {
  67. png_int_32 i = ((png_int_32)(*buf) << 24) +
  68. ((png_int_32)(*(buf + 1)) << 16) +
  69. ((png_int_32)(*(buf + 2)) << 8) +
  70. (png_int_32)(*(buf + 3));
  71. return (i);
  72. }
  73. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  74. png_uint_16 PNGAPI
  75. png_get_uint_16(png_bytep buf)
  76. {
  77. png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  78. (png_uint_16)(*(buf + 1)));
  79. return (i);
  80. }
  81. #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
  82. /* Read data, and (optionally) run it through the CRC. */
  83. void /* PRIVATE */
  84. png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  85. {
  86. if(png_ptr == NULL) return;
  87. png_read_data(png_ptr, buf, length);
  88. png_calculate_crc(png_ptr, buf, length);
  89. }
  90. /* Optionally skip data and then check the CRC. Depending on whether we
  91. are reading a ancillary or critical chunk, and how the program has set
  92. things up, we may calculate the CRC on the data and print a message.
  93. Returns '1' if there was a CRC error, '0' otherwise. */
  94. int /* PRIVATE */
  95. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  96. {
  97. png_size_t i;
  98. png_size_t istop = png_ptr->zbuf_size;
  99. for (i = (png_size_t)skip; i > istop; i -= istop)
  100. {
  101. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  102. }
  103. if (i)
  104. {
  105. png_crc_read(png_ptr, png_ptr->zbuf, i);
  106. }
  107. if (png_crc_error(png_ptr))
  108. {
  109. if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
  110. !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  111. (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
  112. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  113. {
  114. png_chunk_warning(png_ptr, "CRC error");
  115. }
  116. else
  117. {
  118. png_chunk_error(png_ptr, "CRC error");
  119. }
  120. return (1);
  121. }
  122. return (0);
  123. }
  124. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  125. the data it has read thus far. */
  126. int /* PRIVATE */
  127. png_crc_error(png_structp png_ptr)
  128. {
  129. png_byte crc_bytes[4];
  130. png_uint_32 crc;
  131. int need_crc = 1;
  132. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  133. {
  134. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  135. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  136. need_crc = 0;
  137. }
  138. else /* critical */
  139. {
  140. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  141. need_crc = 0;
  142. }
  143. png_read_data(png_ptr, crc_bytes, 4);
  144. if (need_crc)
  145. {
  146. crc = png_get_uint_32(crc_bytes);
  147. return ((int)(crc != png_ptr->crc));
  148. }
  149. else
  150. return (0);
  151. }
  152. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  153. defined(PNG_READ_iCCP_SUPPORTED)
  154. /*
  155. * Decompress trailing data in a chunk. The assumption is that chunkdata
  156. * points at an allocated area holding the contents of a chunk with a
  157. * trailing compressed part. What we get back is an allocated area
  158. * holding the original prefix part and an uncompressed version of the
  159. * trailing part (the malloc area passed in is freed).
  160. */
  161. png_charp /* PRIVATE */
  162. png_decompress_chunk(png_structp png_ptr, int comp_type,
  163. png_charp chunkdata, png_size_t chunklength,
  164. png_size_t prefix_size, png_size_t *newlength)
  165. {
  166. static PNG_CONST char msg[] = "Error decoding compressed text";
  167. png_charp text;
  168. png_size_t text_size;
  169. if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  170. {
  171. int ret = Z_OK;
  172. png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
  173. png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
  174. png_ptr->zstream.next_out = png_ptr->zbuf;
  175. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  176. text_size = 0;
  177. text = NULL;
  178. while (png_ptr->zstream.avail_in)
  179. {
  180. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  181. if (ret != Z_OK && ret != Z_STREAM_END)
  182. {
  183. if (png_ptr->zstream.msg != NULL)
  184. png_warning(png_ptr, png_ptr->zstream.msg);
  185. else
  186. png_warning(png_ptr, msg);
  187. inflateReset(&png_ptr->zstream);
  188. png_ptr->zstream.avail_in = 0;
  189. if (text == NULL)
  190. {
  191. text_size = prefix_size + png_sizeof(msg) + 1;
  192. text = (png_charp)png_malloc_warn(png_ptr, text_size);
  193. if (text == NULL)
  194. {
  195. png_free(png_ptr,chunkdata);
  196. png_error(png_ptr,"Not enough memory to decompress chunk");
  197. }
  198. png_memcpy(text, chunkdata, prefix_size);
  199. }
  200. text[text_size - 1] = 0x00;
  201. /* Copy what we can of the error message into the text chunk */
  202. text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
  203. text_size = png_sizeof(msg) > text_size ? text_size :
  204. png_sizeof(msg);
  205. png_memcpy(text + prefix_size, msg, text_size + 1);
  206. break;
  207. }
  208. if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  209. {
  210. if (text == NULL)
  211. {
  212. text_size = prefix_size +
  213. png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  214. text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
  215. if (text == NULL)
  216. {
  217. png_free(png_ptr,chunkdata);
  218. png_error(png_ptr,"Not enough memory to decompress chunk.");
  219. }
  220. png_memcpy(text + prefix_size, png_ptr->zbuf,
  221. text_size - prefix_size);
  222. png_memcpy(text, chunkdata, prefix_size);
  223. *(text + text_size) = 0x00;
  224. }
  225. else
  226. {
  227. png_charp tmp;
  228. tmp = text;
  229. text = (png_charp)png_malloc_warn(png_ptr,
  230. (png_uint_32)(text_size +
  231. png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
  232. if (text == NULL)
  233. {
  234. png_free(png_ptr, tmp);
  235. png_free(png_ptr, chunkdata);
  236. png_error(png_ptr,"Not enough memory to decompress chunk..");
  237. }
  238. png_memcpy(text, tmp, text_size);
  239. png_free(png_ptr, tmp);
  240. png_memcpy(text + text_size, png_ptr->zbuf,
  241. (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  242. text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  243. *(text + text_size) = 0x00;
  244. }
  245. if (ret == Z_STREAM_END)
  246. break;
  247. else
  248. {
  249. png_ptr->zstream.next_out = png_ptr->zbuf;
  250. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  251. }
  252. }
  253. }
  254. if (ret != Z_STREAM_END)
  255. {
  256. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  257. char umsg[52];
  258. if (ret == Z_BUF_ERROR)
  259. png_snprintf(umsg, 52,
  260. "Buffer error in compressed datastream in %s chunk",
  261. png_ptr->chunk_name);
  262. else if (ret == Z_DATA_ERROR)
  263. png_snprintf(umsg, 52,
  264. "Data error in compressed datastream in %s chunk",
  265. png_ptr->chunk_name);
  266. else
  267. png_snprintf(umsg, 52,
  268. "Incomplete compressed datastream in %s chunk",
  269. png_ptr->chunk_name);
  270. png_warning(png_ptr, umsg);
  271. #else
  272. png_warning(png_ptr,
  273. "Incomplete compressed datastream in chunk other than IDAT");
  274. #endif
  275. text_size=prefix_size;
  276. if (text == NULL)
  277. {
  278. text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
  279. if (text == NULL)
  280. {
  281. png_free(png_ptr, chunkdata);
  282. png_error(png_ptr,"Not enough memory for text.");
  283. }
  284. png_memcpy(text, chunkdata, prefix_size);
  285. }
  286. *(text + text_size) = 0x00;
  287. }
  288. inflateReset(&png_ptr->zstream);
  289. png_ptr->zstream.avail_in = 0;
  290. png_free(png_ptr, chunkdata);
  291. chunkdata = text;
  292. *newlength=text_size;
  293. }
  294. else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  295. {
  296. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  297. char umsg[50];
  298. png_snprintf(umsg, 50,
  299. "Unknown zTXt compression type %d", comp_type);
  300. png_warning(png_ptr, umsg);
  301. #else
  302. png_warning(png_ptr, "Unknown zTXt compression type");
  303. #endif
  304. *(chunkdata + prefix_size) = 0x00;
  305. *newlength=prefix_size;
  306. }
  307. return chunkdata;
  308. }
  309. #endif
  310. /* read and check the IDHR chunk */
  311. void /* PRIVATE */
  312. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  313. {
  314. png_byte buf[13];
  315. png_uint_32 width, height;
  316. int bit_depth, color_type, compression_type, filter_type;
  317. int interlace_type;
  318. png_debug(1, "in png_handle_IHDR\n");
  319. if (png_ptr->mode & PNG_HAVE_IHDR)
  320. png_error(png_ptr, "Out of place IHDR");
  321. /* check the length */
  322. if (length != 13)
  323. png_error(png_ptr, "Invalid IHDR chunk");
  324. png_ptr->mode |= PNG_HAVE_IHDR;
  325. png_crc_read(png_ptr, buf, 13);
  326. png_crc_finish(png_ptr, 0);
  327. width = png_get_uint_31(png_ptr, buf);
  328. height = png_get_uint_31(png_ptr, buf + 4);
  329. bit_depth = buf[8];
  330. color_type = buf[9];
  331. compression_type = buf[10];
  332. filter_type = buf[11];
  333. interlace_type = buf[12];
  334. /* set internal variables */
  335. png_ptr->width = width;
  336. png_ptr->height = height;
  337. png_ptr->bit_depth = (png_byte)bit_depth;
  338. png_ptr->interlaced = (png_byte)interlace_type;
  339. png_ptr->color_type = (png_byte)color_type;
  340. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  341. png_ptr->filter_type = (png_byte)filter_type;
  342. #endif
  343. png_ptr->compression_type = (png_byte)compression_type;
  344. /* find number of channels */
  345. switch (png_ptr->color_type)
  346. {
  347. case PNG_COLOR_TYPE_GRAY:
  348. case PNG_COLOR_TYPE_PALETTE:
  349. png_ptr->channels = 1;
  350. break;
  351. case PNG_COLOR_TYPE_RGB:
  352. png_ptr->channels = 3;
  353. break;
  354. case PNG_COLOR_TYPE_GRAY_ALPHA:
  355. png_ptr->channels = 2;
  356. break;
  357. case PNG_COLOR_TYPE_RGB_ALPHA:
  358. png_ptr->channels = 4;
  359. break;
  360. }
  361. /* set up other useful info */
  362. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  363. png_ptr->channels);
  364. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
  365. png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
  366. png_debug1(3,"channels = %d\n", png_ptr->channels);
  367. png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
  368. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  369. color_type, interlace_type, compression_type, filter_type);
  370. }
  371. /* read and check the palette */
  372. void /* PRIVATE */
  373. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  374. {
  375. png_color palette[PNG_MAX_PALETTE_LENGTH];
  376. int num, i;
  377. #ifndef PNG_NO_POINTER_INDEXING
  378. png_colorp pal_ptr;
  379. #endif
  380. png_debug(1, "in png_handle_PLTE\n");
  381. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  382. png_error(png_ptr, "Missing IHDR before PLTE");
  383. else if (png_ptr->mode & PNG_HAVE_IDAT)
  384. {
  385. png_warning(png_ptr, "Invalid PLTE after IDAT");
  386. png_crc_finish(png_ptr, length);
  387. return;
  388. }
  389. else if (png_ptr->mode & PNG_HAVE_PLTE)
  390. png_error(png_ptr, "Duplicate PLTE chunk");
  391. png_ptr->mode |= PNG_HAVE_PLTE;
  392. if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  393. {
  394. png_warning(png_ptr,
  395. "Ignoring PLTE chunk in grayscale PNG");
  396. png_crc_finish(png_ptr, length);
  397. return;
  398. }
  399. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  400. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  401. {
  402. png_crc_finish(png_ptr, length);
  403. return;
  404. }
  405. #endif
  406. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  407. {
  408. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  409. {
  410. png_warning(png_ptr, "Invalid palette chunk");
  411. png_crc_finish(png_ptr, length);
  412. return;
  413. }
  414. else
  415. {
  416. png_error(png_ptr, "Invalid palette chunk");
  417. }
  418. }
  419. num = (int)length / 3;
  420. #ifndef PNG_NO_POINTER_INDEXING
  421. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  422. {
  423. png_byte buf[3];
  424. png_crc_read(png_ptr, buf, 3);
  425. pal_ptr->red = buf[0];
  426. pal_ptr->green = buf[1];
  427. pal_ptr->blue = buf[2];
  428. }
  429. #else
  430. for (i = 0; i < num; i++)
  431. {
  432. png_byte buf[3];
  433. png_crc_read(png_ptr, buf, 3);
  434. /* don't depend upon png_color being any order */
  435. palette[i].red = buf[0];
  436. palette[i].green = buf[1];
  437. palette[i].blue = buf[2];
  438. }
  439. #endif
  440. /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  441. whatever the normal CRC configuration tells us. However, if we
  442. have an RGB image, the PLTE can be considered ancillary, so
  443. we will act as though it is. */
  444. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  445. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  446. #endif
  447. {
  448. png_crc_finish(png_ptr, 0);
  449. }
  450. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  451. else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
  452. {
  453. /* If we don't want to use the data from an ancillary chunk,
  454. we have two options: an error abort, or a warning and we
  455. ignore the data in this chunk (which should be OK, since
  456. it's considered ancillary for a RGB or RGBA image). */
  457. if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  458. {
  459. if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  460. {
  461. png_chunk_error(png_ptr, "CRC error");
  462. }
  463. else
  464. {
  465. png_chunk_warning(png_ptr, "CRC error");
  466. return;
  467. }
  468. }
  469. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  470. else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  471. {
  472. png_chunk_warning(png_ptr, "CRC error");
  473. }
  474. }
  475. #endif
  476. png_set_PLTE(png_ptr, info_ptr, palette, num);
  477. #if defined(PNG_READ_tRNS_SUPPORTED)
  478. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  479. {
  480. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  481. {
  482. if (png_ptr->num_trans > (png_uint_16)num)
  483. {
  484. png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  485. png_ptr->num_trans = (png_uint_16)num;
  486. }
  487. if (info_ptr->num_trans > (png_uint_16)num)
  488. {
  489. png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  490. info_ptr->num_trans = (png_uint_16)num;
  491. }
  492. }
  493. }
  494. #endif
  495. }
  496. void /* PRIVATE */
  497. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  498. {
  499. png_debug(1, "in png_handle_IEND\n");
  500. if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  501. {
  502. png_error(png_ptr, "No image in file");
  503. }
  504. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  505. if (length != 0)
  506. {
  507. png_warning(png_ptr, "Incorrect IEND chunk length");
  508. }
  509. png_crc_finish(png_ptr, length);
  510. (void) info_ptr; /* quiet compiler warnings about unused info_ptr */
  511. }
  512. #if defined(PNG_READ_gAMA_SUPPORTED)
  513. void /* PRIVATE */
  514. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  515. {
  516. png_fixed_point igamma;
  517. #ifdef PNG_FLOATING_POINT_SUPPORTED
  518. float file_gamma;
  519. #endif
  520. png_byte buf[4];
  521. png_debug(1, "in png_handle_gAMA\n");
  522. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  523. png_error(png_ptr, "Missing IHDR before gAMA");
  524. else if (png_ptr->mode & PNG_HAVE_IDAT)
  525. {
  526. png_warning(png_ptr, "Invalid gAMA after IDAT");
  527. png_crc_finish(png_ptr, length);
  528. return;
  529. }
  530. else if (png_ptr->mode & PNG_HAVE_PLTE)
  531. /* Should be an error, but we can cope with it */
  532. png_warning(png_ptr, "Out of place gAMA chunk");
  533. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  534. #if defined(PNG_READ_sRGB_SUPPORTED)
  535. && !(info_ptr->valid & PNG_INFO_sRGB)
  536. #endif
  537. )
  538. {
  539. png_warning(png_ptr, "Duplicate gAMA chunk");
  540. png_crc_finish(png_ptr, length);
  541. return;
  542. }
  543. if (length != 4)
  544. {
  545. png_warning(png_ptr, "Incorrect gAMA chunk length");
  546. png_crc_finish(png_ptr, length);
  547. return;
  548. }
  549. png_crc_read(png_ptr, buf, 4);
  550. if (png_crc_finish(png_ptr, 0))
  551. return;
  552. igamma = (png_fixed_point)png_get_uint_32(buf);
  553. /* check for zero gamma */
  554. if (igamma == 0)
  555. {
  556. png_warning(png_ptr,
  557. "Ignoring gAMA chunk with gamma=0");
  558. return;
  559. }
  560. #if defined(PNG_READ_sRGB_SUPPORTED)
  561. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  562. if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  563. {
  564. png_warning(png_ptr,
  565. "Ignoring incorrect gAMA value when sRGB is also present");
  566. #ifndef PNG_NO_CONSOLE_IO
  567. fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
  568. #endif
  569. return;
  570. }
  571. #endif /* PNG_READ_sRGB_SUPPORTED */
  572. #ifdef PNG_FLOATING_POINT_SUPPORTED
  573. file_gamma = (float)igamma / (float)100000.0;
  574. # ifdef PNG_READ_GAMMA_SUPPORTED
  575. png_ptr->gamma = file_gamma;
  576. # endif
  577. png_set_gAMA(png_ptr, info_ptr, file_gamma);
  578. #endif
  579. #ifdef PNG_FIXED_POINT_SUPPORTED
  580. png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  581. #endif
  582. }
  583. #endif
  584. #if defined(PNG_READ_sBIT_SUPPORTED)
  585. void /* PRIVATE */
  586. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  587. {
  588. png_size_t truelen;
  589. png_byte buf[4];
  590. png_debug(1, "in png_handle_sBIT\n");
  591. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  592. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  593. png_error(png_ptr, "Missing IHDR before sBIT");
  594. else if (png_ptr->mode & PNG_HAVE_IDAT)
  595. {
  596. png_warning(png_ptr, "Invalid sBIT after IDAT");
  597. png_crc_finish(png_ptr, length);
  598. return;
  599. }
  600. else if (png_ptr->mode & PNG_HAVE_PLTE)
  601. {
  602. /* Should be an error, but we can cope with it */
  603. png_warning(png_ptr, "Out of place sBIT chunk");
  604. }
  605. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  606. {
  607. png_warning(png_ptr, "Duplicate sBIT chunk");
  608. png_crc_finish(png_ptr, length);
  609. return;
  610. }
  611. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  612. truelen = 3;
  613. else
  614. truelen = (png_size_t)png_ptr->channels;
  615. if (length != truelen || length > 4)
  616. {
  617. png_warning(png_ptr, "Incorrect sBIT chunk length");
  618. png_crc_finish(png_ptr, length);
  619. return;
  620. }
  621. png_crc_read(png_ptr, buf, truelen);
  622. if (png_crc_finish(png_ptr, 0))
  623. return;
  624. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  625. {
  626. png_ptr->sig_bit.red = buf[0];
  627. png_ptr->sig_bit.green = buf[1];
  628. png_ptr->sig_bit.blue = buf[2];
  629. png_ptr->sig_bit.alpha = buf[3];
  630. }
  631. else
  632. {
  633. png_ptr->sig_bit.gray = buf[0];
  634. png_ptr->sig_bit.red = buf[0];
  635. png_ptr->sig_bit.green = buf[0];
  636. png_ptr->sig_bit.blue = buf[0];
  637. png_ptr->sig_bit.alpha = buf[1];
  638. }
  639. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  640. }
  641. #endif
  642. #if defined(PNG_READ_cHRM_SUPPORTED)
  643. void /* PRIVATE */
  644. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  645. {
  646. png_byte buf[4];
  647. #ifdef PNG_FLOATING_POINT_SUPPORTED
  648. float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  649. #endif
  650. png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  651. int_y_green, int_x_blue, int_y_blue;
  652. png_uint_32 uint_x, uint_y;
  653. png_debug(1, "in png_handle_cHRM\n");
  654. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  655. png_error(png_ptr, "Missing IHDR before cHRM");
  656. else if (png_ptr->mode & PNG_HAVE_IDAT)
  657. {
  658. png_warning(png_ptr, "Invalid cHRM after IDAT");
  659. png_crc_finish(png_ptr, length);
  660. return;
  661. }
  662. else if (png_ptr->mode & PNG_HAVE_PLTE)
  663. /* Should be an error, but we can cope with it */
  664. png_warning(png_ptr, "Missing PLTE before cHRM");
  665. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  666. #if defined(PNG_READ_sRGB_SUPPORTED)
  667. && !(info_ptr->valid & PNG_INFO_sRGB)
  668. #endif
  669. )
  670. {
  671. png_warning(png_ptr, "Duplicate cHRM chunk");
  672. png_crc_finish(png_ptr, length);
  673. return;
  674. }
  675. if (length != 32)
  676. {
  677. png_warning(png_ptr, "Incorrect cHRM chunk length");
  678. png_crc_finish(png_ptr, length);
  679. return;
  680. }
  681. png_crc_read(png_ptr, buf, 4);
  682. uint_x = png_get_uint_32(buf);
  683. png_crc_read(png_ptr, buf, 4);
  684. uint_y = png_get_uint_32(buf);
  685. if (uint_x > 80000L || uint_y > 80000L ||
  686. uint_x + uint_y > 100000L)
  687. {
  688. png_warning(png_ptr, "Invalid cHRM white point");
  689. png_crc_finish(png_ptr, 24);
  690. return;
  691. }
  692. int_x_white = (png_fixed_point)uint_x;
  693. int_y_white = (png_fixed_point)uint_y;
  694. png_crc_read(png_ptr, buf, 4);
  695. uint_x = png_get_uint_32(buf);
  696. png_crc_read(png_ptr, buf, 4);
  697. uint_y = png_get_uint_32(buf);
  698. if (uint_x + uint_y > 100000L)
  699. {
  700. png_warning(png_ptr, "Invalid cHRM red point");
  701. png_crc_finish(png_ptr, 16);
  702. return;
  703. }
  704. int_x_red = (png_fixed_point)uint_x;
  705. int_y_red = (png_fixed_point)uint_y;
  706. png_crc_read(png_ptr, buf, 4);
  707. uint_x = png_get_uint_32(buf);
  708. png_crc_read(png_ptr, buf, 4);
  709. uint_y = png_get_uint_32(buf);
  710. if (uint_x + uint_y > 100000L)
  711. {
  712. png_warning(png_ptr, "Invalid cHRM green point");
  713. png_crc_finish(png_ptr, 8);
  714. return;
  715. }
  716. int_x_green = (png_fixed_point)uint_x;
  717. int_y_green = (png_fixed_point)uint_y;
  718. png_crc_read(png_ptr, buf, 4);
  719. uint_x = png_get_uint_32(buf);
  720. png_crc_read(png_ptr, buf, 4);
  721. uint_y = png_get_uint_32(buf);
  722. if (uint_x + uint_y > 100000L)
  723. {
  724. png_warning(png_ptr, "Invalid cHRM blue point");
  725. png_crc_finish(png_ptr, 0);
  726. return;
  727. }
  728. int_x_blue = (png_fixed_point)uint_x;
  729. int_y_blue = (png_fixed_point)uint_y;
  730. #ifdef PNG_FLOATING_POINT_SUPPORTED
  731. white_x = (float)int_x_white / (float)100000.0;
  732. white_y = (float)int_y_white / (float)100000.0;
  733. red_x = (float)int_x_red / (float)100000.0;
  734. red_y = (float)int_y_red / (float)100000.0;
  735. green_x = (float)int_x_green / (float)100000.0;
  736. green_y = (float)int_y_green / (float)100000.0;
  737. blue_x = (float)int_x_blue / (float)100000.0;
  738. blue_y = (float)int_y_blue / (float)100000.0;
  739. #endif
  740. #if defined(PNG_READ_sRGB_SUPPORTED)
  741. if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
  742. {
  743. if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) ||
  744. PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) ||
  745. PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) ||
  746. PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) ||
  747. PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) ||
  748. PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
  749. PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) ||
  750. PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000))
  751. {
  752. png_warning(png_ptr,
  753. "Ignoring incorrect cHRM value when sRGB is also present");
  754. #ifndef PNG_NO_CONSOLE_IO
  755. #ifdef PNG_FLOATING_POINT_SUPPORTED
  756. fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
  757. white_x, white_y, red_x, red_y);
  758. fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
  759. green_x, green_y, blue_x, blue_y);
  760. #else
  761. fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
  762. int_x_white, int_y_white, int_x_red, int_y_red);
  763. fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
  764. int_x_green, int_y_green, int_x_blue, int_y_blue);
  765. #endif
  766. #endif /* PNG_NO_CONSOLE_IO */
  767. }
  768. png_crc_finish(png_ptr, 0);
  769. return;
  770. }
  771. #endif /* PNG_READ_sRGB_SUPPORTED */
  772. #ifdef PNG_FLOATING_POINT_SUPPORTED
  773. png_set_cHRM(png_ptr, info_ptr,
  774. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  775. #endif
  776. #ifdef PNG_FIXED_POINT_SUPPORTED
  777. png_set_cHRM_fixed(png_ptr, info_ptr,
  778. int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  779. int_y_green, int_x_blue, int_y_blue);
  780. #endif
  781. if (png_crc_finish(png_ptr, 0))
  782. return;
  783. }
  784. #endif
  785. #if defined(PNG_READ_sRGB_SUPPORTED)
  786. void /* PRIVATE */
  787. png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  788. {
  789. int intent;
  790. png_byte buf[1];
  791. png_debug(1, "in png_handle_sRGB\n");
  792. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  793. png_error(png_ptr, "Missing IHDR before sRGB");
  794. else if (png_ptr->mode & PNG_HAVE_IDAT)
  795. {
  796. png_warning(png_ptr, "Invalid sRGB after IDAT");
  797. png_crc_finish(png_ptr, length);
  798. return;
  799. }
  800. else if (png_ptr->mode & PNG_HAVE_PLTE)
  801. /* Should be an error, but we can cope with it */
  802. png_warning(png_ptr, "Out of place sRGB chunk");
  803. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  804. {
  805. png_warning(png_ptr, "Duplicate sRGB chunk");
  806. png_crc_finish(png_ptr, length);
  807. return;
  808. }
  809. if (length != 1)
  810. {
  811. png_warning(png_ptr, "Incorrect sRGB chunk length");
  812. png_crc_finish(png_ptr, length);
  813. return;
  814. }
  815. png_crc_read(png_ptr, buf, 1);
  816. if (png_crc_finish(png_ptr, 0))
  817. return;
  818. intent = buf[0];
  819. /* check for bad intent */
  820. if (intent >= PNG_sRGB_INTENT_LAST)
  821. {
  822. png_warning(png_ptr, "Unknown sRGB intent");
  823. return;
  824. }
  825. #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  826. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
  827. {
  828. png_fixed_point igamma;
  829. #ifdef PNG_FIXED_POINT_SUPPORTED
  830. igamma=info_ptr->int_gamma;
  831. #else
  832. # ifdef PNG_FLOATING_POINT_SUPPORTED
  833. igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
  834. # endif
  835. #endif
  836. if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  837. {
  838. png_warning(png_ptr,
  839. "Ignoring incorrect gAMA value when sRGB is also present");
  840. #ifndef PNG_NO_CONSOLE_IO
  841. # ifdef PNG_FIXED_POINT_SUPPORTED
  842. fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
  843. # else
  844. # ifdef PNG_FLOATING_POINT_SUPPORTED
  845. fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
  846. # endif
  847. # endif
  848. #endif
  849. }
  850. }
  851. #endif /* PNG_READ_gAMA_SUPPORTED */
  852. #ifdef PNG_READ_cHRM_SUPPORTED
  853. #ifdef PNG_FIXED_POINT_SUPPORTED
  854. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  855. if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) ||
  856. PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) ||
  857. PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) ||
  858. PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) ||
  859. PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) ||
  860. PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
  861. PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) ||
  862. PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000))
  863. {
  864. png_warning(png_ptr,
  865. "Ignoring incorrect cHRM value when sRGB is also present");
  866. }
  867. #endif /* PNG_FIXED_POINT_SUPPORTED */
  868. #endif /* PNG_READ_cHRM_SUPPORTED */
  869. png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  870. }
  871. #endif /* PNG_READ_sRGB_SUPPORTED */
  872. #if defined(PNG_READ_iCCP_SUPPORTED)
  873. void /* PRIVATE */
  874. png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  875. /* Note: this does not properly handle chunks that are > 64K under DOS */
  876. {
  877. png_charp chunkdata;
  878. png_byte compression_type;
  879. png_bytep pC;
  880. png_charp profile;
  881. png_uint_32 skip = 0;
  882. png_uint_32 profile_size, profile_length;
  883. png_size_t slength, prefix_length, data_length;
  884. png_debug(1, "in png_handle_iCCP\n");
  885. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  886. png_error(png_ptr, "Missing IHDR before iCCP");
  887. else if (png_ptr->mode & PNG_HAVE_IDAT)
  888. {
  889. png_warning(png_ptr, "Invalid iCCP after IDAT");
  890. png_crc_finish(png_ptr, length);
  891. return;
  892. }
  893. else if (png_ptr->mode & PNG_HAVE_PLTE)
  894. /* Should be an error, but we can cope with it */
  895. png_warning(png_ptr, "Out of place iCCP chunk");
  896. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
  897. {
  898. png_warning(png_ptr, "Duplicate iCCP chunk");
  899. png_crc_finish(png_ptr, length);
  900. return;
  901. }
  902. #ifdef PNG_MAX_MALLOC_64K
  903. if (length > (png_uint_32)65535L)
  904. {
  905. png_warning(png_ptr, "iCCP chunk too large to fit in memory");
  906. skip = length - (png_uint_32)65535L;
  907. length = (png_uint_32)65535L;
  908. }
  909. #endif
  910. chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  911. slength = (png_size_t)length;
  912. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  913. if (png_crc_finish(png_ptr, skip))
  914. {
  915. png_free(png_ptr, chunkdata);
  916. return;
  917. }
  918. chunkdata[slength] = 0x00;
  919. for (profile = chunkdata; *profile; profile++)
  920. /* empty loop to find end of name */ ;
  921. ++profile;
  922. /* there should be at least one zero (the compression type byte)
  923. following the separator, and we should be on it */
  924. if ( profile >= chunkdata + slength - 1)
  925. {
  926. png_free(png_ptr, chunkdata);
  927. png_warning(png_ptr, "Malformed iCCP chunk");
  928. return;
  929. }
  930. /* compression_type should always be zero */
  931. compression_type = *profile++;
  932. if (compression_type)
  933. {
  934. png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
  935. compression_type=0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
  936. wrote nonzero) */
  937. }
  938. prefix_length = profile - chunkdata;
  939. chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
  940. slength, prefix_length, &data_length);
  941. profile_length = data_length - prefix_length;
  942. if ( prefix_length > data_length || profile_length < 4)
  943. {
  944. png_free(png_ptr, chunkdata);
  945. png_warning(png_ptr, "Profile size field missing from iCCP chunk");
  946. return;
  947. }
  948. /* Check the profile_size recorded in the first 32 bits of the ICC profile */
  949. pC = (png_bytep)(chunkdata+prefix_length);
  950. profile_size = ((*(pC ))<<24) |
  951. ((*(pC+1))<<16) |
  952. ((*(pC+2))<< 8) |
  953. ((*(pC+3)) );
  954. if(profile_size < profile_length)
  955. profile_length = profile_size;
  956. if(profile_size > profile_length)
  957. {
  958. png_free(png_ptr, chunkdata);
  959. png_warning(png_ptr, "Ignoring truncated iCCP profile.");
  960. return;
  961. }
  962. png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
  963. chunkdata + prefix_length, profile_length);
  964. png_free(png_ptr, chunkdata);
  965. }
  966. #endif /* PNG_READ_iCCP_SUPPORTED */
  967. #if defined(PNG_READ_sPLT_SUPPORTED)
  968. void /* PRIVATE */
  969. png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  970. /* Note: this does not properly handle chunks that are > 64K under DOS */
  971. {
  972. png_bytep chunkdata;
  973. png_bytep entry_start;
  974. png_sPLT_t new_palette;
  975. #ifdef PNG_NO_POINTER_INDEXING
  976. png_sPLT_entryp pp;
  977. #endif
  978. int data_length, entry_size, i;
  979. png_uint_32 skip = 0;
  980. png_size_t slength;
  981. png_debug(1, "in png_handle_sPLT\n");
  982. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  983. png_error(png_ptr, "Missing IHDR before sPLT");
  984. else if (png_ptr->mode & PNG_HAVE_IDAT)
  985. {
  986. png_warning(png_ptr, "Invalid sPLT after IDAT");
  987. png_crc_finish(png_ptr, length);
  988. return;
  989. }
  990. #ifdef PNG_MAX_MALLOC_64K
  991. if (length > (png_uint_32)65535L)
  992. {
  993. png_warning(png_ptr, "sPLT chunk too large to fit in memory");
  994. skip = length - (png_uint_32)65535L;
  995. length = (png_uint_32)65535L;
  996. }
  997. #endif
  998. chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
  999. slength = (png_size_t)length;
  1000. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1001. if (png_crc_finish(png_ptr, skip))
  1002. {
  1003. png_free(png_ptr, chunkdata);
  1004. return;
  1005. }
  1006. chunkdata[slength] = 0x00;
  1007. for (entry_start = chunkdata; *entry_start; entry_start++)
  1008. /* empty loop to find end of name */ ;
  1009. ++entry_start;
  1010. /* a sample depth should follow the separator, and we should be on it */
  1011. if (entry_start > chunkdata + slength - 2)
  1012. {
  1013. png_free(png_ptr, chunkdata);
  1014. png_warning(png_ptr, "malformed sPLT chunk");
  1015. return;
  1016. }
  1017. new_palette.depth = *entry_start++;
  1018. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1019. data_length = (slength - (entry_start - chunkdata));
  1020. /* integrity-check the data length */
  1021. if (data_length % entry_size)
  1022. {
  1023. png_free(png_ptr, chunkdata);
  1024. png_warning(png_ptr, "sPLT chunk has bad length");
  1025. return;
  1026. }
  1027. new_palette.nentries = (png_int_32) ( data_length / entry_size);
  1028. if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX /
  1029. png_sizeof(png_sPLT_entry)))
  1030. {
  1031. png_warning(png_ptr, "sPLT chunk too long");
  1032. return;
  1033. }
  1034. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
  1035. png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
  1036. if (new_palette.entries == NULL)
  1037. {
  1038. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1039. return;
  1040. }
  1041. #ifndef PNG_NO_POINTER_INDEXING
  1042. for (i = 0; i < new_palette.nentries; i++)
  1043. {
  1044. png_sPLT_entryp pp = new_palette.entries + i;
  1045. if (new_palette.depth == 8)
  1046. {
  1047. pp->red = *entry_start++;
  1048. pp->green = *entry_start++;
  1049. pp->blue = *entry_start++;
  1050. pp->alpha = *entry_start++;
  1051. }
  1052. else
  1053. {
  1054. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1055. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1056. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1057. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1058. }
  1059. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1060. }
  1061. #else
  1062. pp = new_palette.entries;
  1063. for (i = 0; i < new_palette.nentries; i++)
  1064. {
  1065. if (new_palette.depth == 8)
  1066. {
  1067. pp[i].red = *entry_start++;
  1068. pp[i].green = *entry_start++;
  1069. pp[i].blue = *entry_start++;
  1070. pp[i].alpha = *entry_start++;
  1071. }
  1072. else
  1073. {
  1074. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1075. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1076. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1077. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1078. }
  1079. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1080. }
  1081. #endif
  1082. /* discard all chunk data except the name and stash that */
  1083. new_palette.name = (png_charp)chunkdata;
  1084. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1085. png_free(png_ptr, chunkdata);
  1086. png_free(png_ptr, new_palette.entries);
  1087. }
  1088. #endif /* PNG_READ_sPLT_SUPPORTED */
  1089. #if defined(PNG_READ_tRNS_SUPPORTED)
  1090. void /* PRIVATE */
  1091. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1092. {
  1093. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1094. int bit_mask;
  1095. png_debug(1, "in png_handle_tRNS\n");
  1096. /* For non-indexed color, mask off any bits in the tRNS value that
  1097. * exceed the bit depth. Some creators were writing extra bits there.
  1098. * This is not needed for indexed color. */
  1099. bit_mask = (1 << png_ptr->bit_depth) - 1;
  1100. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1101. png_error(png_ptr, "Missing IHDR before tRNS");
  1102. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1103. {
  1104. png_warning(png_ptr, "Invalid tRNS after IDAT");
  1105. png_crc_finish(png_ptr, length);
  1106. return;
  1107. }
  1108. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  1109. {
  1110. png_warning(png_ptr, "Duplicate tRNS chunk");
  1111. png_crc_finish(png_ptr, length);
  1112. return;
  1113. }
  1114. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1115. {
  1116. png_byte buf[2];
  1117. if (length != 2)
  1118. {
  1119. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1120. png_crc_finish(png_ptr, length);
  1121. return;
  1122. }
  1123. png_crc_read(png_ptr, buf, 2);
  1124. png_ptr->num_trans = 1;
  1125. png_ptr->trans_values.gray = png_get_uint_16(buf) & bit_mask;
  1126. }
  1127. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1128. {
  1129. png_byte buf[6];
  1130. if (length != 6)
  1131. {
  1132. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1133. png_crc_finish(png_ptr, length);
  1134. return;
  1135. }
  1136. png_crc_read(png_ptr, buf, (png_size_t)length);
  1137. png_ptr->num_trans = 1;
  1138. png_ptr->trans_values.red = png_get_uint_16(buf) & bit_mask;
  1139. png_ptr->trans_values.green = png_get_uint_16(buf + 2) & bit_mask;
  1140. png_ptr->trans_values.blue = png_get_uint_16(buf + 4) & bit_mask;
  1141. }
  1142. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1143. {
  1144. if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1145. {
  1146. /* Should be an error, but we can cope with it. */
  1147. png_warning(png_ptr, "Missing PLTE before tRNS");
  1148. }
  1149. if (length > (png_uint_32)png_ptr->num_palette ||
  1150. length > PNG_MAX_PALETTE_LENGTH)
  1151. {
  1152. png_warning(png_ptr, "Incorrect tRNS chunk length");
  1153. png_crc_finish(png_ptr, length);
  1154. return;
  1155. }
  1156. if (length == 0)
  1157. {
  1158. png_warning(png_ptr, "Zero length tRNS chunk");
  1159. png_crc_finish(png_ptr, length);
  1160. return;
  1161. }
  1162. png_crc_read(png_ptr, readbuf, (png_size_t)length);
  1163. png_ptr->num_trans = (png_uint_16)length;
  1164. }
  1165. else
  1166. {
  1167. png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  1168. png_crc_finish(png_ptr, length);
  1169. return;
  1170. }
  1171. if (png_crc_finish(png_ptr, 0))
  1172. {
  1173. png_ptr->num_trans = 0;
  1174. return;
  1175. }
  1176. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1177. &(png_ptr->trans_values));
  1178. }
  1179. #endif
  1180. #if defined(PNG_READ_bKGD_SUPPORTED)
  1181. void /* PRIVATE */
  1182. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1183. {
  1184. png_size_t truelen;
  1185. png_byte buf[6];
  1186. png_debug(1, "in png_handle_bKGD\n");
  1187. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1188. png_error(png_ptr, "Missing IHDR before bKGD");
  1189. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1190. {
  1191. png_warning(png_ptr, "Invalid bKGD after IDAT");
  1192. png_crc_finish(png_ptr, length);
  1193. return;
  1194. }
  1195. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1196. !(png_ptr->mode & PNG_HAVE_PLTE))
  1197. {
  1198. png_warning(png_ptr, "Missing PLTE before bKGD");
  1199. png_crc_finish(png_ptr, length);
  1200. return;
  1201. }
  1202. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
  1203. {
  1204. png_warning(png_ptr, "Duplicate bKGD chunk");
  1205. png_crc_finish(png_ptr, length);
  1206. return;
  1207. }
  1208. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1209. truelen = 1;
  1210. else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1211. truelen = 6;
  1212. else
  1213. truelen = 2;
  1214. if (length != truelen)
  1215. {
  1216. png_warning(png_ptr, "Incorrect bKGD chunk length");
  1217. png_crc_finish(png_ptr, length);
  1218. return;
  1219. }
  1220. png_crc_read(png_ptr, buf, truelen);
  1221. if (png_crc_finish(png_ptr, 0))
  1222. return;
  1223. /* We convert the index value into RGB components so that we can allow
  1224. * arbitrary RGB values for background when we have transparency, and
  1225. * so it is easy to determine the RGB values of the background color
  1226. * from the info_ptr struct. */
  1227. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1228. {
  1229. png_ptr->background.index = buf[0];
  1230. if(info_ptr->num_palette)
  1231. {
  1232. if(buf[0] > info_ptr->num_palette)
  1233. {
  1234. png_warning(png_ptr, "Incorrect bKGD chunk index value");
  1235. return;
  1236. }
  1237. png_ptr->background.red =
  1238. (png_uint_16)png_ptr->palette[buf[0]].red;
  1239. png_ptr->background.green =
  1240. (png_uint_16)png_ptr->palette[buf[0]].green;
  1241. png_ptr->background.blue =
  1242. (png_uint_16)png_ptr->palette[buf[0]].blue;
  1243. }
  1244. }
  1245. else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
  1246. {
  1247. png_ptr->background.red =
  1248. png_ptr->background.green =
  1249. png_ptr->background.blue =
  1250. png_ptr->background.gray = png_get_uint_16(buf);
  1251. }
  1252. else
  1253. {
  1254. png_ptr->background.red = png_get_uint_16(buf);
  1255. png_ptr->background.green = png_get_uint_16(buf + 2);
  1256. png_ptr->background.blue = png_get_uint_16(buf + 4);
  1257. }
  1258. png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1259. }
  1260. #endif
  1261. #if defined(PNG_READ_hIST_SUPPORTED)
  1262. void /* PRIVATE */
  1263. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1264. {
  1265. unsigned int num, i;
  1266. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1267. png_debug(1, "in png_handle_hIST\n");
  1268. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1269. png_error(png_ptr, "Missing IHDR before hIST");
  1270. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1271. {
  1272. png_warning(png_ptr, "Invalid hIST after IDAT");
  1273. png_crc_finish(png_ptr, length);
  1274. return;
  1275. }
  1276. else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1277. {
  1278. png_warning(png_ptr, "Missing PLTE before hIST");
  1279. png_crc_finish(png_ptr, length);
  1280. return;
  1281. }
  1282. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
  1283. {
  1284. png_warning(png_ptr, "Duplicate hIST chunk");
  1285. png_crc_finish(png_ptr, length);
  1286. return;
  1287. }
  1288. num = length / 2 ;
  1289. if (num != (unsigned int) png_ptr->num_palette || num >
  1290. (unsigned int) PNG_MAX_PALETTE_LENGTH)
  1291. {
  1292. png_warning(png_ptr, "Incorrect hIST chunk length");
  1293. png_crc_finish(png_ptr, length);
  1294. return;
  1295. }
  1296. for (i = 0; i < num; i++)
  1297. {
  1298. png_byte buf[2];
  1299. png_crc_read(png_ptr, buf, 2);
  1300. readbuf[i] = png_get_uint_16(buf);
  1301. }
  1302. if (png_crc_finish(png_ptr, 0))
  1303. return;
  1304. png_set_hIST(png_ptr, info_ptr, readbuf);
  1305. }
  1306. #endif
  1307. #if defined(PNG_READ_pHYs_SUPPORTED)
  1308. void /* PRIVATE */
  1309. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1310. {
  1311. png_byte buf[9];
  1312. png_uint_32 res_x, res_y;
  1313. int unit_type;
  1314. png_debug(1, "in png_handle_pHYs\n");
  1315. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1316. png_error(png_ptr, "Missing IHDR before pHYs");
  1317. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1318. {
  1319. png_warning(png_ptr, "Invalid pHYs after IDAT");
  1320. png_crc_finish(png_ptr, length);
  1321. return;
  1322. }
  1323. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  1324. {
  1325. png_warning(png_ptr, "Duplicate pHYs chunk");
  1326. png_crc_finish(png_ptr, length);
  1327. return;
  1328. }
  1329. if (length != 9)
  1330. {
  1331. png_warning(png_ptr, "Incorrect pHYs chunk length");
  1332. png_crc_finish(png_ptr, length);
  1333. return;
  1334. }
  1335. png_crc_read(png_ptr, buf, 9);
  1336. if (png_crc_finish(png_ptr, 0))
  1337. return;
  1338. res_x = png_get_uint_32(buf);
  1339. res_y = png_get_uint_32(buf + 4);
  1340. unit_type = buf[8];
  1341. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1342. }
  1343. #endif
  1344. #if defined(PNG_READ_oFFs_SUPPORTED)
  1345. void /* PRIVATE */
  1346. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1347. {
  1348. png_byte buf[9];
  1349. png_int_32 offset_x, offset_y;
  1350. int unit_type;
  1351. png_debug(1, "in png_handle_oFFs\n");
  1352. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1353. png_error(png_ptr, "Missing IHDR before oFFs");
  1354. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1355. {
  1356. png_warning(png_ptr, "Invalid oFFs after IDAT");
  1357. png_crc_finish(png_ptr, length);
  1358. return;
  1359. }
  1360. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
  1361. {
  1362. png_warning(png_ptr, "Duplicate oFFs chunk");
  1363. png_crc_finish(png_ptr, length);
  1364. return;
  1365. }
  1366. if (length != 9)
  1367. {
  1368. png_warning(png_ptr, "Incorrect oFFs chunk length");
  1369. png_crc_finish(png_ptr, length);
  1370. return;
  1371. }
  1372. png_crc_read(png_ptr, buf, 9);
  1373. if (png_crc_finish(png_ptr, 0))
  1374. return;
  1375. offset_x = png_get_int_32(buf);
  1376. offset_y = png_get_int_32(buf + 4);
  1377. unit_type = buf[8];
  1378. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1379. }
  1380. #endif
  1381. #if defined(PNG_READ_pCAL_SUPPORTED)
  1382. /* read the pCAL chunk (described in the PNG Extensions document) */
  1383. void /* PRIVATE */
  1384. png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1385. {
  1386. png_charp purpose;
  1387. png_int_32 X0, X1;
  1388. png_byte type, nparams;
  1389. png_charp buf, units, endptr;
  1390. png_charpp params;
  1391. png_size_t slength;
  1392. int i;
  1393. png_debug(1, "in png_handle_pCAL\n");
  1394. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1395. png_error(png_ptr, "Missing IHDR before pCAL");
  1396. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1397. {
  1398. png_warning(png_ptr, "Invalid pCAL after IDAT");
  1399. png_crc_finish(png_ptr, length);
  1400. return;
  1401. }
  1402. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
  1403. {
  1404. png_warning(png_ptr, "Duplicate pCAL chunk");
  1405. png_crc_finish(png_ptr, length);
  1406. return;
  1407. }
  1408. png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
  1409. length + 1);
  1410. purpose = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1411. if (purpose == NULL)
  1412. {
  1413. png_warning(png_ptr, "No memory for pCAL purpose.");
  1414. return;
  1415. }
  1416. slength = (png_size_t)length;
  1417. png_crc_read(png_ptr, (png_bytep)purpose, slength);
  1418. if (png_crc_finish(png_ptr, 0))
  1419. {
  1420. png_free(png_ptr, purpose);
  1421. return;
  1422. }
  1423. purpose[slength] = 0x00; /* null terminate the last string */
  1424. png_debug(3, "Finding end of pCAL purpose string\n");
  1425. for (buf = purpose; *buf; buf++)
  1426. /* empty loop */ ;
  1427. endptr = purpose + slength;
  1428. /* We need to have at least 12 bytes after the purpose string
  1429. in order to get the parameter information. */
  1430. if (endptr <= buf + 12)
  1431. {
  1432. png_warning(png_ptr, "Invalid pCAL data");
  1433. png_free(png_ptr, purpose);
  1434. return;
  1435. }
  1436. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
  1437. X0 = png_get_int_32((png_bytep)buf+1);
  1438. X1 = png_get_int_32((png_bytep)buf+5);
  1439. type = buf[9];
  1440. nparams = buf[10];
  1441. units = buf + 11;
  1442. png_debug(3, "Checking pCAL equation type and number of parameters\n");
  1443. /* Check that we have the right number of parameters for known
  1444. equation types. */
  1445. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1446. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1447. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1448. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1449. {
  1450. png_warning(png_ptr, "Invalid pCAL parameters for equation type");
  1451. png_free(png_ptr, purpose);
  1452. return;
  1453. }
  1454. else if (type >= PNG_EQUATION_LAST)
  1455. {
  1456. png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1457. }
  1458. for (buf = units; *buf; buf++)
  1459. /* Empty loop to move past the units string. */ ;
  1460. png_debug(3, "Allocating pCAL parameters array\n");
  1461. params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams
  1462. *png_sizeof(png_charp))) ;
  1463. if (params == NULL)
  1464. {
  1465. png_free(png_ptr, purpose);
  1466. png_warning(png_ptr, "No memory for pCAL params.");
  1467. return;
  1468. }
  1469. /* Get pointers to the start of each parameter string. */
  1470. for (i = 0; i < (int)nparams; i++)
  1471. {
  1472. buf++; /* Skip the null string terminator from previous parameter. */
  1473. png_debug1(3, "Reading pCAL parameter %d\n", i);
  1474. for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
  1475. /* Empty loop to move past each parameter string */ ;
  1476. /* Make sure we haven't run out of data yet */
  1477. if (buf > endptr)
  1478. {
  1479. png_warning(png_ptr, "Invalid pCAL data");
  1480. png_free(png_ptr, purpose);
  1481. png_free(png_ptr, params);
  1482. return;
  1483. }
  1484. }
  1485. png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
  1486. units, params);
  1487. png_free(png_ptr, purpose);
  1488. png_free(png_ptr, params);
  1489. }
  1490. #endif
  1491. #if defined(PNG_READ_sCAL_SUPPORTED)
  1492. /* read the sCAL chunk */
  1493. void /* PRIVATE */
  1494. png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1495. {
  1496. png_charp buffer, ep;
  1497. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1498. double width, height;
  1499. png_charp vp;
  1500. #else
  1501. #ifdef PNG_FIXED_POINT_SUPPORTED
  1502. png_charp swidth, sheight;
  1503. #endif
  1504. #endif
  1505. png_size_t slength;
  1506. png_debug(1, "in png_handle_sCAL\n");
  1507. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1508. png_error(png_ptr, "Missing IHDR before sCAL");
  1509. else if (png_ptr->mode & PNG_HAVE_IDAT)
  1510. {
  1511. png_warning(png_ptr, "Invalid sCAL after IDAT");
  1512. png_crc_finish(png_ptr, length);
  1513. return;
  1514. }
  1515. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
  1516. {
  1517. png_warning(png_ptr, "Duplicate sCAL chunk");
  1518. png_crc_finish(png_ptr, length);
  1519. return;
  1520. }
  1521. png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
  1522. length + 1);
  1523. buffer = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1524. if (buffer == NULL)
  1525. {
  1526. png_warning(png_ptr, "Out of memory while processing sCAL chunk");
  1527. return;
  1528. }
  1529. slength = (png_size_t)length;
  1530. png_crc_read(png_ptr, (png_bytep)buffer, slength);
  1531. if (png_crc_finish(png_ptr, 0))
  1532. {
  1533. png_free(png_ptr, buffer);
  1534. return;
  1535. }
  1536. buffer[slength] = 0x00; /* null terminate the last string */
  1537. ep = buffer + 1; /* skip unit byte */
  1538. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1539. width = png_strtod(png_ptr, ep, &vp);
  1540. if (*vp)
  1541. {
  1542. png_warning(png_ptr, "malformed width string in sCAL chunk");
  1543. return;
  1544. }
  1545. #else
  1546. #ifdef PNG_FIXED_POINT_SUPPORTED
  1547. swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
  1548. if (swidth == NULL)
  1549. {
  1550. png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
  1551. return;
  1552. }
  1553. png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
  1554. #endif
  1555. #endif
  1556. for (ep = buffer; *ep; ep++)
  1557. /* empty loop */ ;
  1558. ep++;
  1559. if (buffer + slength < ep)
  1560. {
  1561. png_warning(png_ptr, "Truncated sCAL chunk");
  1562. #if defined(PNG_FIXED_POINT_SUPPORTED) && \
  1563. !defined(PNG_FLOATING_POINT_SUPPORTED)
  1564. png_free(png_ptr, swidth);
  1565. #endif
  1566. png_free(png_ptr, buffer);
  1567. return;
  1568. }
  1569. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1570. height = png_strtod(png_ptr, ep, &vp);
  1571. if (*vp)
  1572. {
  1573. png_warning(png_ptr, "malformed height string in sCAL chunk");
  1574. return;
  1575. }
  1576. #else
  1577. #ifdef PNG_FIXED_POINT_SUPPORTED
  1578. sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
  1579. if (swidth == NULL)
  1580. {
  1581. png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
  1582. return;
  1583. }
  1584. png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
  1585. #endif
  1586. #endif
  1587. if (buffer + slength < ep
  1588. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1589. || width <= 0. || height <= 0.
  1590. #endif
  1591. )
  1592. {
  1593. png_warning(png_ptr, "Invalid sCAL data");
  1594. png_free(png_ptr, buffer);
  1595. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1596. png_free(png_ptr, swidth);
  1597. png_free(png_ptr, sheight);
  1598. #endif
  1599. return;
  1600. }
  1601. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1602. png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
  1603. #else
  1604. #ifdef PNG_FIXED_POINT_SUPPORTED
  1605. png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
  1606. #endif
  1607. #endif
  1608. png_free(png_ptr, buffer);
  1609. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1610. png_free(png_ptr, swidth);
  1611. png_free(png_ptr, sheight);
  1612. #endif
  1613. }
  1614. #endif
  1615. #if defined(PNG_READ_tIME_SUPPORTED)
  1616. void /* PRIVATE */
  1617. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1618. {
  1619. png_byte buf[7];
  1620. png_time mod_time;
  1621. png_debug(1, "in png_handle_tIME\n");
  1622. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1623. png_error(png_ptr, "Out of place tIME chunk");
  1624. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
  1625. {
  1626. png_warning(png_ptr, "Duplicate tIME chunk");
  1627. png_crc_finish(png_ptr, length);
  1628. return;
  1629. }
  1630. if (png_ptr->mode & PNG_HAVE_IDAT)
  1631. png_ptr->mode |= PNG_AFTER_IDAT;
  1632. if (length != 7)
  1633. {
  1634. png_warning(png_ptr, "Incorrect tIME chunk length");
  1635. png_crc_finish(png_ptr, length);
  1636. return;
  1637. }
  1638. png_crc_read(png_ptr, buf, 7);
  1639. if (png_crc_finish(png_ptr, 0))
  1640. return;
  1641. mod_time.second = buf[6];
  1642. mod_time.minute = buf[5];
  1643. mod_time.hour = buf[4];
  1644. mod_time.day = buf[3];
  1645. mod_time.month = buf[2];
  1646. mod_time.year = png_get_uint_16(buf);
  1647. png_set_tIME(png_ptr, info_ptr, &mod_time);
  1648. }
  1649. #endif
  1650. #if defined(PNG_READ_tEXt_SUPPORTED)
  1651. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1652. void /* PRIVATE */
  1653. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1654. {
  1655. png_textp text_ptr;
  1656. png_charp key;
  1657. png_charp text;
  1658. png_uint_32 skip = 0;
  1659. png_size_t slength;
  1660. int ret;
  1661. png_debug(1, "in png_handle_tEXt\n");
  1662. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1663. png_error(png_ptr, "Missing IHDR before tEXt");
  1664. if (png_ptr->mode & PNG_HAVE_IDAT)
  1665. png_ptr->mode |= PNG_AFTER_IDAT;
  1666. #ifdef PNG_MAX_MALLOC_64K
  1667. if (length > (png_uint_32)65535L)
  1668. {
  1669. png_warning(png_ptr, "tEXt chunk too large to fit in memory");
  1670. skip = length - (png_uint_32)65535L;
  1671. length = (png_uint_32)65535L;
  1672. }
  1673. #endif
  1674. key = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1675. if (key == NULL)
  1676. {
  1677. png_warning(png_ptr, "No memory to process text chunk.");
  1678. return;
  1679. }
  1680. slength = (png_size_t)length;
  1681. png_crc_read(png_ptr, (png_bytep)key, slength);
  1682. if (png_crc_finish(png_ptr, skip))
  1683. {
  1684. png_free(png_ptr, key);
  1685. return;
  1686. }
  1687. key[slength] = 0x00;
  1688. for (text = key; *text; text++)
  1689. /* empty loop to find end of key */ ;
  1690. if (text != key + slength)
  1691. text++;
  1692. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1693. (png_uint_32)png_sizeof(png_text));
  1694. if (text_ptr == NULL)
  1695. {
  1696. png_warning(png_ptr, "Not enough memory to process text chunk.");
  1697. png_free(png_ptr, key);
  1698. return;
  1699. }
  1700. text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1701. text_ptr->key = key;
  1702. #ifdef PNG_iTXt_SUPPORTED
  1703. text_ptr->lang = NULL;
  1704. text_ptr->lang_key = NULL;
  1705. text_ptr->itxt_length = 0;
  1706. #endif
  1707. text_ptr->text = text;
  1708. text_ptr->text_length = png_strlen(text);
  1709. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1710. png_free(png_ptr, key);
  1711. png_free(png_ptr, text_ptr);
  1712. if (ret)
  1713. png_warning(png_ptr, "Insufficient memory to process text chunk.");
  1714. }
  1715. #endif
  1716. #if defined(PNG_READ_zTXt_SUPPORTED)
  1717. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1718. void /* PRIVATE */
  1719. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1720. {
  1721. png_textp text_ptr;
  1722. png_charp chunkdata;
  1723. png_charp text;
  1724. int comp_type;
  1725. int ret;
  1726. png_size_t slength, prefix_len, data_len;
  1727. png_debug(1, "in png_handle_zTXt\n");
  1728. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1729. png_error(png_ptr, "Missing IHDR before zTXt");
  1730. if (png_ptr->mode & PNG_HAVE_IDAT)
  1731. png_ptr->mode |= PNG_AFTER_IDAT;
  1732. #ifdef PNG_MAX_MALLOC_64K
  1733. /* We will no doubt have problems with chunks even half this size, but
  1734. there is no hard and fast rule to tell us where to stop. */
  1735. if (length > (png_uint_32)65535L)
  1736. {
  1737. png_warning(png_ptr,"zTXt chunk too large to fit in memory");
  1738. png_crc_finish(png_ptr, length);
  1739. return;
  1740. }
  1741. #endif
  1742. chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1743. if (chunkdata == NULL)
  1744. {
  1745. png_warning(png_ptr,"Out of memory processing zTXt chunk.");
  1746. return;
  1747. }
  1748. slength = (png_size_t)length;
  1749. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1750. if (png_crc_finish(png_ptr, 0))
  1751. {
  1752. png_free(png_ptr, chunkdata);
  1753. return;
  1754. }
  1755. chunkdata[slength] = 0x00;
  1756. for (text = chunkdata; *text; text++)
  1757. /* empty loop */ ;
  1758. /* zTXt must have some text after the chunkdataword */
  1759. if (text >= chunkdata + slength - 2)
  1760. {
  1761. png_warning(png_ptr, "Truncated zTXt chunk");
  1762. png_free(png_ptr, chunkdata);
  1763. return;
  1764. }
  1765. else
  1766. {
  1767. comp_type = *(++text);
  1768. if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
  1769. {
  1770. png_warning(png_ptr, "Unknown compression type in zTXt chunk");
  1771. comp_type = PNG_TEXT_COMPRESSION_zTXt;
  1772. }
  1773. text++; /* skip the compression_method byte */
  1774. }
  1775. prefix_len = text - chunkdata;
  1776. chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1777. (png_size_t)length, prefix_len, &data_len);
  1778. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1779. (png_uint_32)png_sizeof(png_text));
  1780. if (text_ptr == NULL)
  1781. {
  1782. png_warning(png_ptr,"Not enough memory to process zTXt chunk.");
  1783. png_free(png_ptr, chunkdata);
  1784. return;
  1785. }
  1786. text_ptr->compression = comp_type;
  1787. text_ptr->key = chunkdata;
  1788. #ifdef PNG_iTXt_SUPPORTED
  1789. text_ptr->lang = NULL;
  1790. text_ptr->lang_key = NULL;
  1791. text_ptr->itxt_length = 0;
  1792. #endif
  1793. text_ptr->text = chunkdata + prefix_len;
  1794. text_ptr->text_length = data_len;
  1795. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1796. png_free(png_ptr, text_ptr);
  1797. png_free(png_ptr, chunkdata);
  1798. if (ret)
  1799. png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
  1800. }
  1801. #endif
  1802. #if defined(PNG_READ_iTXt_SUPPORTED)
  1803. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1804. void /* PRIVATE */
  1805. png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1806. {
  1807. png_textp text_ptr;
  1808. png_charp chunkdata;
  1809. png_charp key, lang, text, lang_key;
  1810. int comp_flag;
  1811. int comp_type = 0;
  1812. int ret;
  1813. png_size_t slength, prefix_len, data_len;
  1814. png_debug(1, "in png_handle_iTXt\n");
  1815. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1816. png_error(png_ptr, "Missing IHDR before iTXt");
  1817. if (png_ptr->mode & PNG_HAVE_IDAT)
  1818. png_ptr->mode |= PNG_AFTER_IDAT;
  1819. #ifdef PNG_MAX_MALLOC_64K
  1820. /* We will no doubt have problems with chunks even half this size, but
  1821. there is no hard and fast rule to tell us where to stop. */
  1822. if (length > (png_uint_32)65535L)
  1823. {
  1824. png_warning(png_ptr,"iTXt chunk too large to fit in memory");
  1825. png_crc_finish(png_ptr, length);
  1826. return;
  1827. }
  1828. #endif
  1829. chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
  1830. if (chunkdata == NULL)
  1831. {
  1832. png_warning(png_ptr, "No memory to process iTXt chunk.");
  1833. return;
  1834. }
  1835. slength = (png_size_t)length;
  1836. png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1837. if (png_crc_finish(png_ptr, 0))
  1838. {
  1839. png_free(png_ptr, chunkdata);
  1840. return;
  1841. }
  1842. chunkdata[slength] = 0x00;
  1843. for (lang = chunkdata; *lang; lang++)
  1844. /* empty loop */ ;
  1845. lang++; /* skip NUL separator */
  1846. /* iTXt must have a language tag (possibly empty), two compression bytes,
  1847. translated keyword (possibly empty), and possibly some text after the
  1848. keyword */
  1849. if (lang >= chunkdata + slength - 3)
  1850. {
  1851. png_warning(png_ptr, "Truncated iTXt chunk");
  1852. png_free(png_ptr, chunkdata);
  1853. return;
  1854. }
  1855. else
  1856. {
  1857. comp_flag = *lang++;
  1858. comp_type = *lang++;
  1859. }
  1860. for (lang_key = lang; *lang_key; lang_key++)
  1861. /* empty loop */ ;
  1862. lang_key++; /* skip NUL separator */
  1863. if (lang_key >= chunkdata + slength)
  1864. {
  1865. png_warning(png_ptr, "Truncated iTXt chunk");
  1866. png_free(png_ptr, chunkdata);
  1867. return;
  1868. }
  1869. for (text = lang_key; *text; text++)
  1870. /* empty loop */ ;
  1871. text++; /* skip NUL separator */
  1872. if (text >= chunkdata + slength)
  1873. {
  1874. png_warning(png_ptr, "Malformed iTXt chunk");
  1875. png_free(png_ptr, chunkdata);
  1876. return;
  1877. }
  1878. prefix_len = text - chunkdata;
  1879. key=chunkdata;
  1880. if (comp_flag)
  1881. chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1882. (size_t)length, prefix_len, &data_len);
  1883. else
  1884. data_len=png_strlen(chunkdata + prefix_len);
  1885. text_ptr = (png_textp)png_malloc_warn(png_ptr,
  1886. (png_uint_32)png_sizeof(png_text));
  1887. if (text_ptr == NULL)
  1888. {
  1889. png_warning(png_ptr,"Not enough memory to process iTXt chunk.");
  1890. png_free(png_ptr, chunkdata);
  1891. return;
  1892. }
  1893. text_ptr->compression = (int)comp_flag + 1;
  1894. text_ptr->lang_key = chunkdata+(lang_key-key);
  1895. text_ptr->lang = chunkdata+(lang-key);
  1896. text_ptr->itxt_length = data_len;
  1897. text_ptr->text_length = 0;
  1898. text_ptr->key = chunkdata;
  1899. text_ptr->text = chunkdata + prefix_len;
  1900. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
  1901. png_free(png_ptr, text_ptr);
  1902. png_free(png_ptr, chunkdata);
  1903. if (ret)
  1904. png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
  1905. }
  1906. #endif
  1907. /* This function is called when we haven't found a handler for a
  1908. chunk. If there isn't a problem with the chunk itself (ie bad
  1909. chunk name, CRC, or a critical chunk), the chunk is silently ignored
  1910. -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
  1911. case it will be saved away to be written out later. */
  1912. void /* PRIVATE */
  1913. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1914. {
  1915. png_uint_32 skip = 0;
  1916. png_debug(1, "in png_handle_unknown\n");
  1917. if (png_ptr->mode & PNG_HAVE_IDAT)
  1918. {
  1919. #ifdef PNG_USE_LOCAL_ARRAYS
  1920. PNG_CONST PNG_IDAT;
  1921. #endif
  1922. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */
  1923. png_ptr->mode |= PNG_AFTER_IDAT;
  1924. }
  1925. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  1926. if (!(png_ptr->chunk_name[0] & 0x20))
  1927. {
  1928. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1929. if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  1930. PNG_HANDLE_CHUNK_ALWAYS
  1931. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1932. && png_ptr->read_user_chunk_fn == NULL
  1933. #endif
  1934. )
  1935. #endif
  1936. png_chunk_error(png_ptr, "unknown critical chunk");
  1937. }
  1938. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1939. if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
  1940. (png_ptr->read_user_chunk_fn != NULL))
  1941. {
  1942. #ifdef PNG_MAX_MALLOC_64K
  1943. if (length > (png_uint_32)65535L)
  1944. {
  1945. png_warning(png_ptr, "unknown chunk too large to fit in memory");
  1946. skip = length - (png_uint_32)65535L;
  1947. length = (png_uint_32)65535L;
  1948. }
  1949. #endif
  1950. png_strncpy((png_charp)png_ptr->unknown_chunk.name,
  1951. (png_charp)png_ptr->chunk_name, 5);
  1952. png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
  1953. png_ptr->unknown_chunk.size = (png_size_t)length;
  1954. png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
  1955. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1956. if(png_ptr->read_user_chunk_fn != NULL)
  1957. {
  1958. /* callback to user unknown chunk handler */
  1959. int ret;
  1960. ret = (*(png_ptr->read_user_chunk_fn))
  1961. (png_ptr, &png_ptr->unknown_chunk);
  1962. if (ret < 0)
  1963. png_chunk_error(png_ptr, "error in user chunk");
  1964. if (ret == 0)
  1965. {
  1966. if (!(png_ptr->chunk_name[0] & 0x20))
  1967. if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  1968. PNG_HANDLE_CHUNK_ALWAYS)
  1969. png_chunk_error(png_ptr, "unknown critical chunk");
  1970. png_set_unknown_chunks(png_ptr, info_ptr,
  1971. &png_ptr->unknown_chunk, 1);
  1972. }
  1973. }
  1974. #else
  1975. png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
  1976. #endif
  1977. png_free(png_ptr, png_ptr->unknown_chunk.data);
  1978. png_ptr->unknown_chunk.data = NULL;
  1979. }
  1980. else
  1981. #endif
  1982. skip = length;
  1983. png_crc_finish(png_ptr, skip);
  1984. #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1985. (void) info_ptr; /* quiet compiler warnings about unused info_ptr */
  1986. #endif
  1987. }
  1988. /* This function is called to verify that a chunk name is valid.
  1989. This function can't have the "critical chunk check" incorporated
  1990. into it, since in the future we will need to be able to call user
  1991. functions to handle unknown critical chunks after we check that
  1992. the chunk name itself is valid. */
  1993. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  1994. void /* PRIVATE */
  1995. png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
  1996. {
  1997. png_debug(1, "in png_check_chunk_name\n");
  1998. if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
  1999. isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
  2000. {
  2001. png_chunk_error(png_ptr, "invalid chunk type");
  2002. }
  2003. }
  2004. /* Combines the row recently read in with the existing pixels in the
  2005. row. This routine takes care of alpha and transparency if requested.
  2006. This routine also handles the two methods of progressive display
  2007. of interlaced images, depending on the mask value.
  2008. The mask value describes which pixels are to be combined with
  2009. the row. The pattern always repeats every 8 pixels, so just 8
  2010. bits are needed. A one indicates the pixel is to be combined,
  2011. a zero indicates the pixel is to be skipped. This is in addition
  2012. to any alpha or transparency value associated with the pixel. If
  2013. you want all pixels to be combined, pass 0xff (255) in mask. */
  2014. void /* PRIVATE */
  2015. png_combine_row(png_structp png_ptr, png_bytep row, int mask)
  2016. {
  2017. png_debug(1,"in png_combine_row\n");
  2018. if (mask == 0xff)
  2019. {
  2020. png_memcpy(row, png_ptr->row_buf + 1,
  2021. PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
  2022. }
  2023. else
  2024. {
  2025. switch (png_ptr->row_info.pixel_depth)
  2026. {
  2027. case 1:
  2028. {
  2029. png_bytep sp = png_ptr->row_buf + 1;
  2030. png_bytep dp = row;
  2031. int s_inc, s_start, s_end;
  2032. int m = 0x80;
  2033. int shift;
  2034. png_uint_32 i;
  2035. png_uint_32 row_width = png_ptr->width;
  2036. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2037. if (png_ptr->transformations & PNG_PACKSWAP)
  2038. {
  2039. s_start = 0;
  2040. s_end = 7;
  2041. s_inc = 1;
  2042. }
  2043. else
  2044. #endif
  2045. {
  2046. s_start = 7;
  2047. s_end = 0;
  2048. s_inc = -1;
  2049. }
  2050. shift = s_start;
  2051. for (i = 0; i < row_width; i++)
  2052. {
  2053. if (m & mask)
  2054. {
  2055. int value;
  2056. value = (*sp >> shift) & 0x01;
  2057. *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  2058. *dp |= (png_byte)(value << shift);
  2059. }
  2060. if (shift == s_end)
  2061. {
  2062. shift = s_start;
  2063. sp++;
  2064. dp++;
  2065. }
  2066. else
  2067. shift += s_inc;
  2068. if (m == 1)
  2069. m = 0x80;
  2070. else
  2071. m >>= 1;
  2072. }
  2073. break;
  2074. }
  2075. case 2:
  2076. {
  2077. png_bytep sp = png_ptr->row_buf + 1;
  2078. png_bytep dp = row;
  2079. int s_start, s_end, s_inc;
  2080. int m = 0x80;
  2081. int shift;
  2082. png_uint_32 i;
  2083. png_uint_32 row_width = png_ptr->width;
  2084. int value;
  2085. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2086. if (png_ptr->transformations & PNG_PACKSWAP)
  2087. {
  2088. s_start = 0;
  2089. s_end = 6;
  2090. s_inc = 2;
  2091. }
  2092. else
  2093. #endif
  2094. {
  2095. s_start = 6;
  2096. s_end = 0;
  2097. s_inc = -2;
  2098. }
  2099. shift = s_start;
  2100. for (i = 0; i < row_width; i++)
  2101. {
  2102. if (m & mask)
  2103. {
  2104. value = (*sp >> shift) & 0x03;
  2105. *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2106. *dp |= (png_byte)(value << shift);
  2107. }
  2108. if (shift == s_end)
  2109. {
  2110. shift = s_start;
  2111. sp++;
  2112. dp++;
  2113. }
  2114. else
  2115. shift += s_inc;
  2116. if (m == 1)
  2117. m = 0x80;
  2118. else
  2119. m >>= 1;
  2120. }
  2121. break;
  2122. }
  2123. case 4:
  2124. {
  2125. png_bytep sp = png_ptr->row_buf + 1;
  2126. png_bytep dp = row;
  2127. int s_start, s_end, s_inc;
  2128. int m = 0x80;
  2129. int shift;
  2130. png_uint_32 i;
  2131. png_uint_32 row_width = png_ptr->width;
  2132. int value;
  2133. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2134. if (png_ptr->transformations & PNG_PACKSWAP)
  2135. {
  2136. s_start = 0;
  2137. s_end = 4;
  2138. s_inc = 4;
  2139. }
  2140. else
  2141. #endif
  2142. {
  2143. s_start = 4;
  2144. s_end = 0;
  2145. s_inc = -4;
  2146. }
  2147. shift = s_start;
  2148. for (i = 0; i < row_width; i++)
  2149. {
  2150. if (m & mask)
  2151. {
  2152. value = (*sp >> shift) & 0xf;
  2153. *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2154. *dp |= (png_byte)(value << shift);
  2155. }
  2156. if (shift == s_end)
  2157. {
  2158. shift = s_start;
  2159. sp++;
  2160. dp++;
  2161. }
  2162. else
  2163. shift += s_inc;
  2164. if (m == 1)
  2165. m = 0x80;
  2166. else
  2167. m >>= 1;
  2168. }
  2169. break;
  2170. }
  2171. default:
  2172. {
  2173. png_bytep sp = png_ptr->row_buf + 1;
  2174. png_bytep dp = row;
  2175. png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  2176. png_uint_32 i;
  2177. png_uint_32 row_width = png_ptr->width;
  2178. png_byte m = 0x80;
  2179. for (i = 0; i < row_width; i++)
  2180. {
  2181. if (m & mask)
  2182. {
  2183. png_memcpy(dp, sp, pixel_bytes);
  2184. }
  2185. sp += pixel_bytes;
  2186. dp += pixel_bytes;
  2187. if (m == 1)
  2188. m = 0x80;
  2189. else
  2190. m >>= 1;
  2191. }
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. }
  2197. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2198. /* OLD pre-1.0.9 interface:
  2199. void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  2200. png_uint_32 transformations)
  2201. */
  2202. void /* PRIVATE */
  2203. png_do_read_interlace(png_structp png_ptr)
  2204. {
  2205. png_row_infop row_info = &(png_ptr->row_info);
  2206. png_bytep row = png_ptr->row_buf + 1;
  2207. int pass = png_ptr->pass;
  2208. png_uint_32 transformations = png_ptr->transformations;
  2209. #ifdef PNG_USE_LOCAL_ARRAYS
  2210. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2211. /* offset to next interlace block */
  2212. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2213. #endif
  2214. png_debug(1,"in png_do_read_interlace\n");
  2215. if (row != NULL && row_info != NULL)
  2216. {
  2217. png_uint_32 final_width;
  2218. final_width = row_info->width * png_pass_inc[pass];
  2219. switch (row_info->pixel_depth)
  2220. {
  2221. case 1:
  2222. {
  2223. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2224. png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
  2225. int sshift, dshift;
  2226. int s_start, s_end, s_inc;
  2227. int jstop = png_pass_inc[pass];
  2228. png_byte v;
  2229. png_uint_32 i;
  2230. int j;
  2231. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2232. if (transformations & PNG_PACKSWAP)
  2233. {
  2234. sshift = (int)((row_info->width + 7) & 0x07);
  2235. dshift = (int)((final_width + 7) & 0x07);
  2236. s_start = 7;
  2237. s_end = 0;
  2238. s_inc = -1;
  2239. }
  2240. else
  2241. #endif
  2242. {
  2243. sshift = 7 - (int)((row_info->width + 7) & 0x07);
  2244. dshift = 7 - (int)((final_width + 7) & 0x07);
  2245. s_start = 0;
  2246. s_end = 7;
  2247. s_inc = 1;
  2248. }
  2249. for (i = 0; i < row_info->width; i++)
  2250. {
  2251. v = (png_byte)((*sp >> sshift) & 0x01);
  2252. for (j = 0; j < jstop; j++)
  2253. {
  2254. *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  2255. *dp |= (png_byte)(v << dshift);
  2256. if (dshift == s_end)
  2257. {
  2258. dshift = s_start;
  2259. dp--;
  2260. }
  2261. else
  2262. dshift += s_inc;
  2263. }
  2264. if (sshift == s_end)
  2265. {
  2266. sshift = s_start;
  2267. sp--;
  2268. }
  2269. else
  2270. sshift += s_inc;
  2271. }
  2272. break;
  2273. }
  2274. case 2:
  2275. {
  2276. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  2277. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  2278. int sshift, dshift;
  2279. int s_start, s_end, s_inc;
  2280. int jstop = png_pass_inc[pass];
  2281. png_uint_32 i;
  2282. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2283. if (transformations & PNG_PACKSWAP)
  2284. {
  2285. sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2286. dshift = (int)(((final_width + 3) & 0x03) << 1);
  2287. s_start = 6;
  2288. s_end = 0;
  2289. s_inc = -2;
  2290. }
  2291. else
  2292. #endif
  2293. {
  2294. sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
  2295. dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
  2296. s_start = 0;
  2297. s_end = 6;
  2298. s_inc = 2;
  2299. }
  2300. for (i = 0; i < row_info->width; i++)
  2301. {
  2302. png_byte v;
  2303. int j;
  2304. v = (png_byte)((*sp >> sshift) & 0x03);
  2305. for (j = 0; j < jstop; j++)
  2306. {
  2307. *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  2308. *dp |= (png_byte)(v << dshift);
  2309. if (dshift == s_end)
  2310. {
  2311. dshift = s_start;
  2312. dp--;
  2313. }
  2314. else
  2315. dshift += s_inc;
  2316. }
  2317. if (sshift == s_end)
  2318. {
  2319. sshift = s_start;
  2320. sp--;
  2321. }
  2322. else
  2323. sshift += s_inc;
  2324. }
  2325. break;
  2326. }
  2327. case 4:
  2328. {
  2329. png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2330. png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
  2331. int sshift, dshift;
  2332. int s_start, s_end, s_inc;
  2333. png_uint_32 i;
  2334. int jstop = png_pass_inc[pass];
  2335. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2336. if (transformations & PNG_PACKSWAP)
  2337. {
  2338. sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2339. dshift = (int)(((final_width + 1) & 0x01) << 2);
  2340. s_start = 4;
  2341. s_end = 0;
  2342. s_inc = -4;
  2343. }
  2344. else
  2345. #endif
  2346. {
  2347. sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
  2348. dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
  2349. s_start = 0;
  2350. s_end = 4;
  2351. s_inc = 4;
  2352. }
  2353. for (i = 0; i < row_info->width; i++)
  2354. {
  2355. png_byte v = (png_byte)((*sp >> sshift) & 0xf);
  2356. int j;
  2357. for (j = 0; j < jstop; j++)
  2358. {
  2359. *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  2360. *dp |= (png_byte)(v << dshift);
  2361. if (dshift == s_end)
  2362. {
  2363. dshift = s_start;
  2364. dp--;
  2365. }
  2366. else
  2367. dshift += s_inc;
  2368. }
  2369. if (sshift == s_end)
  2370. {
  2371. sshift = s_start;
  2372. sp--;
  2373. }
  2374. else
  2375. sshift += s_inc;
  2376. }
  2377. break;
  2378. }
  2379. default:
  2380. {
  2381. png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
  2382. png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
  2383. png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
  2384. int jstop = png_pass_inc[pass];
  2385. png_uint_32 i;
  2386. for (i = 0; i < row_info->width; i++)
  2387. {
  2388. png_byte v[8];
  2389. int j;
  2390. png_memcpy(v, sp, pixel_bytes);
  2391. for (j = 0; j < jstop; j++)
  2392. {
  2393. png_memcpy(dp, v, pixel_bytes);
  2394. dp -= pixel_bytes;
  2395. }
  2396. sp -= pixel_bytes;
  2397. }
  2398. break;
  2399. }
  2400. }
  2401. row_info->width = final_width;
  2402. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width);
  2403. }
  2404. #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
  2405. transformations = transformations; /* silence compiler warning */
  2406. #endif
  2407. }
  2408. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2409. void /* PRIVATE */
  2410. png_read_filter_row(png_structp, png_row_infop row_info, png_bytep row,
  2411. png_bytep prev_row, int filter)
  2412. {
  2413. png_debug(1, "in png_read_filter_row\n");
  2414. png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
  2415. switch (filter)
  2416. {
  2417. case PNG_FILTER_VALUE_NONE:
  2418. break;
  2419. case PNG_FILTER_VALUE_SUB:
  2420. {
  2421. png_uint_32 i;
  2422. png_uint_32 istop = row_info->rowbytes;
  2423. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2424. png_bytep rp = row + bpp;
  2425. png_bytep lp = row;
  2426. for (i = bpp; i < istop; i++)
  2427. {
  2428. *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
  2429. rp++;
  2430. }
  2431. break;
  2432. }
  2433. case PNG_FILTER_VALUE_UP:
  2434. {
  2435. png_uint_32 i;
  2436. png_uint_32 istop = row_info->rowbytes;
  2437. png_bytep rp = row;
  2438. png_bytep pp = prev_row;
  2439. for (i = 0; i < istop; i++)
  2440. {
  2441. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2442. rp++;
  2443. }
  2444. break;
  2445. }
  2446. case PNG_FILTER_VALUE_AVG:
  2447. {
  2448. png_uint_32 i;
  2449. png_bytep rp = row;
  2450. png_bytep pp = prev_row;
  2451. png_bytep lp = row;
  2452. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2453. png_uint_32 istop = row_info->rowbytes - bpp;
  2454. for (i = 0; i < bpp; i++)
  2455. {
  2456. *rp = (png_byte)(((int)(*rp) +
  2457. ((int)(*pp++) / 2 )) & 0xff);
  2458. rp++;
  2459. }
  2460. for (i = 0; i < istop; i++)
  2461. {
  2462. *rp = (png_byte)(((int)(*rp) +
  2463. (int)(*pp++ + *lp++) / 2 ) & 0xff);
  2464. rp++;
  2465. }
  2466. break;
  2467. }
  2468. case PNG_FILTER_VALUE_PAETH:
  2469. {
  2470. png_uint_32 i;
  2471. png_bytep rp = row;
  2472. png_bytep pp = prev_row;
  2473. png_bytep lp = row;
  2474. png_bytep cp = prev_row;
  2475. png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2476. png_uint_32 istop=row_info->rowbytes - bpp;
  2477. for (i = 0; i < bpp; i++)
  2478. {
  2479. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2480. rp++;
  2481. }
  2482. for (i = 0; i < istop; i++) /* use leftover rp,pp */
  2483. {
  2484. int a, b, c, pa, pb, pc, p;
  2485. a = *lp++;
  2486. b = *pp++;
  2487. c = *cp++;
  2488. p = b - c;
  2489. pc = a - c;
  2490. #ifdef PNG_USE_ABS
  2491. pa = abs(p);
  2492. pb = abs(pc);
  2493. pc = abs(p + pc);
  2494. #else
  2495. pa = p < 0 ? -p : p;
  2496. pb = pc < 0 ? -pc : pc;
  2497. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2498. #endif
  2499. /*
  2500. if (pa <= pb && pa <= pc)
  2501. p = a;
  2502. else if (pb <= pc)
  2503. p = b;
  2504. else
  2505. p = c;
  2506. */
  2507. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2508. *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  2509. rp++;
  2510. }
  2511. break;
  2512. }
  2513. default:
  2514. png_warning(png_ptr, "Ignoring bad adaptive filter type");
  2515. *row=0;
  2516. break;
  2517. }
  2518. }
  2519. void /* PRIVATE */
  2520. png_read_finish_row(png_structp png_ptr)
  2521. {
  2522. #ifdef PNG_USE_LOCAL_ARRAYS
  2523. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2524. /* start of interlace block */
  2525. PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2526. /* offset to next interlace block */
  2527. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2528. /* start of interlace block in the y direction */
  2529. PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2530. /* offset to next interlace block in the y direction */
  2531. PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2532. #endif
  2533. png_debug(1, "in png_read_finish_row\n");
  2534. png_ptr->row_number++;
  2535. if (png_ptr->row_number < png_ptr->num_rows)
  2536. return;
  2537. if (png_ptr->interlaced)
  2538. {
  2539. png_ptr->row_number = 0;
  2540. png_memset_check(png_ptr, png_ptr->prev_row, 0,
  2541. png_ptr->rowbytes + 1);
  2542. do
  2543. {
  2544. png_ptr->pass++;
  2545. if (png_ptr->pass >= 7)
  2546. break;
  2547. png_ptr->iwidth = (png_ptr->width +
  2548. png_pass_inc[png_ptr->pass] - 1 -
  2549. png_pass_start[png_ptr->pass]) /
  2550. png_pass_inc[png_ptr->pass];
  2551. png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
  2552. png_ptr->iwidth) + 1;
  2553. if (!(png_ptr->transformations & PNG_INTERLACE))
  2554. {
  2555. png_ptr->num_rows = (png_ptr->height +
  2556. png_pass_yinc[png_ptr->pass] - 1 -
  2557. png_pass_ystart[png_ptr->pass]) /
  2558. png_pass_yinc[png_ptr->pass];
  2559. if (!(png_ptr->num_rows))
  2560. continue;
  2561. }
  2562. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  2563. break;
  2564. } while (png_ptr->iwidth == 0);
  2565. if (png_ptr->pass < 7)
  2566. return;
  2567. }
  2568. if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  2569. {
  2570. #ifdef PNG_USE_LOCAL_ARRAYS
  2571. PNG_CONST PNG_IDAT;
  2572. #endif
  2573. char extra;
  2574. int ret;
  2575. png_ptr->zstream.next_out = (Bytef *)&extra;
  2576. png_ptr->zstream.avail_out = (uInt)1;
  2577. for(;;)
  2578. {
  2579. if (!(png_ptr->zstream.avail_in))
  2580. {
  2581. while (!png_ptr->idat_size)
  2582. {
  2583. png_byte chunk_length[4];
  2584. png_crc_finish(png_ptr, 0);
  2585. png_read_data(png_ptr, chunk_length, 4);
  2586. png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
  2587. png_reset_crc(png_ptr);
  2588. png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  2589. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  2590. png_error(png_ptr, "Not enough image data");
  2591. }
  2592. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  2593. png_ptr->zstream.next_in = png_ptr->zbuf;
  2594. if (png_ptr->zbuf_size > png_ptr->idat_size)
  2595. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  2596. png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  2597. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  2598. }
  2599. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  2600. if (ret == Z_STREAM_END)
  2601. {
  2602. if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  2603. png_ptr->idat_size)
  2604. png_warning(png_ptr, "Extra compressed data");
  2605. png_ptr->mode |= PNG_AFTER_IDAT;
  2606. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2607. break;
  2608. }
  2609. if (ret != Z_OK)
  2610. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  2611. "Decompression Error");
  2612. if (!(png_ptr->zstream.avail_out))
  2613. {
  2614. png_warning(png_ptr, "Extra compressed data.");
  2615. png_ptr->mode |= PNG_AFTER_IDAT;
  2616. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2617. break;
  2618. }
  2619. }
  2620. png_ptr->zstream.avail_out = 0;
  2621. }
  2622. if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  2623. png_warning(png_ptr, "Extra compression data");
  2624. inflateReset(&png_ptr->zstream);
  2625. png_ptr->mode |= PNG_AFTER_IDAT;
  2626. }
  2627. void /* PRIVATE */
  2628. png_read_start_row(png_structp png_ptr)
  2629. {
  2630. #ifdef PNG_USE_LOCAL_ARRAYS
  2631. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2632. /* start of interlace block */
  2633. PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2634. /* offset to next interlace block */
  2635. PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2636. /* start of interlace block in the y direction */
  2637. PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2638. /* offset to next interlace block in the y direction */
  2639. PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2640. #endif
  2641. int max_pixel_depth;
  2642. png_uint_32 row_bytes;
  2643. png_debug(1, "in png_read_start_row\n");
  2644. png_ptr->zstream.avail_in = 0;
  2645. png_init_read_transformations(png_ptr);
  2646. if (png_ptr->interlaced)
  2647. {
  2648. if (!(png_ptr->transformations & PNG_INTERLACE))
  2649. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  2650. png_pass_ystart[0]) / png_pass_yinc[0];
  2651. else
  2652. png_ptr->num_rows = png_ptr->height;
  2653. png_ptr->iwidth = (png_ptr->width +
  2654. png_pass_inc[png_ptr->pass] - 1 -
  2655. png_pass_start[png_ptr->pass]) /
  2656. png_pass_inc[png_ptr->pass];
  2657. row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1;
  2658. png_ptr->irowbytes = (png_size_t)row_bytes;
  2659. if((png_uint_32)png_ptr->irowbytes != row_bytes)
  2660. png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
  2661. }
  2662. else
  2663. {
  2664. png_ptr->num_rows = png_ptr->height;
  2665. png_ptr->iwidth = png_ptr->width;
  2666. png_ptr->irowbytes = png_ptr->rowbytes + 1;
  2667. }
  2668. max_pixel_depth = png_ptr->pixel_depth;
  2669. #if defined(PNG_READ_PACK_SUPPORTED)
  2670. if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  2671. max_pixel_depth = 8;
  2672. #endif
  2673. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2674. if (png_ptr->transformations & PNG_EXPAND)
  2675. {
  2676. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2677. {
  2678. if (png_ptr->num_trans)
  2679. max_pixel_depth = 32;
  2680. else
  2681. max_pixel_depth = 24;
  2682. }
  2683. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2684. {
  2685. if (max_pixel_depth < 8)
  2686. max_pixel_depth = 8;
  2687. if (png_ptr->num_trans)
  2688. max_pixel_depth *= 2;
  2689. }
  2690. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2691. {
  2692. if (png_ptr->num_trans)
  2693. {
  2694. max_pixel_depth *= 4;
  2695. max_pixel_depth /= 3;
  2696. }
  2697. }
  2698. }
  2699. #endif
  2700. #if defined(PNG_READ_FILLER_SUPPORTED)
  2701. if (png_ptr->transformations & (PNG_FILLER))
  2702. {
  2703. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2704. max_pixel_depth = 32;
  2705. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2706. {
  2707. if (max_pixel_depth <= 8)
  2708. max_pixel_depth = 16;
  2709. else
  2710. max_pixel_depth = 32;
  2711. }
  2712. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2713. {
  2714. if (max_pixel_depth <= 32)
  2715. max_pixel_depth = 32;
  2716. else
  2717. max_pixel_depth = 64;
  2718. }
  2719. }
  2720. #endif
  2721. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  2722. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  2723. {
  2724. if (
  2725. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2726. (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  2727. #endif
  2728. #if defined(PNG_READ_FILLER_SUPPORTED)
  2729. (png_ptr->transformations & (PNG_FILLER)) ||
  2730. #endif
  2731. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2732. {
  2733. if (max_pixel_depth <= 16)
  2734. max_pixel_depth = 32;
  2735. else
  2736. max_pixel_depth = 64;
  2737. }
  2738. else
  2739. {
  2740. if (max_pixel_depth <= 8)
  2741. {
  2742. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2743. max_pixel_depth = 32;
  2744. else
  2745. max_pixel_depth = 24;
  2746. }
  2747. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2748. max_pixel_depth = 64;
  2749. else
  2750. max_pixel_depth = 48;
  2751. }
  2752. }
  2753. #endif
  2754. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  2755. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  2756. if(png_ptr->transformations & PNG_USER_TRANSFORM)
  2757. {
  2758. int user_pixel_depth=png_ptr->user_transform_depth*
  2759. png_ptr->user_transform_channels;
  2760. if(user_pixel_depth > max_pixel_depth)
  2761. max_pixel_depth=user_pixel_depth;
  2762. }
  2763. #endif
  2764. /* align the width on the next larger 8 pixels. Mainly used
  2765. for interlacing */
  2766. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  2767. /* calculate the maximum bytes needed, adding a byte and a pixel
  2768. for safety's sake */
  2769. row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) +
  2770. 1 + ((max_pixel_depth + 7) >> 3);
  2771. #ifdef PNG_MAX_MALLOC_64K
  2772. if (row_bytes > (png_uint_32)65536L)
  2773. png_error(png_ptr, "This image requires a row greater than 64KB");
  2774. #endif
  2775. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
  2776. png_ptr->row_buf = png_ptr->big_row_buf+32;
  2777. #ifdef PNG_MAX_MALLOC_64K
  2778. if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
  2779. png_error(png_ptr, "This image requires a row greater than 64KB");
  2780. #endif
  2781. if ((png_uint_32)png_ptr->rowbytes > (png_uint_32) -2)
  2782. png_error(png_ptr, "Row has too many bytes to allocate in memory.");
  2783. png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
  2784. png_ptr->rowbytes + 1));
  2785. png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2786. png_debug1(3, "width = %lu,\n", png_ptr->width);
  2787. png_debug1(3, "height = %lu,\n", png_ptr->height);
  2788. png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
  2789. png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
  2790. png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
  2791. png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
  2792. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  2793. }
  2794. #endif /* PNG_READ_SUPPORTED */