/src/3rdparty/libpng/pngread.c

https://bitbucket.org/ultra_iter/qt-vtl · C · 1457 lines · 1068 code · 227 blank · 162 comment · 293 complexity · da04a38d843f7177e3ceb9839a056895 MD5 · raw file

  1. /* pngread.c - read a PNG file
  2. *
  3. * Last changed in libpng 1.5.4 [July 7, 2011]
  4. * Copyright (c) 1998-2011 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that an application calls directly to
  13. * read a PNG file or stream.
  14. */
  15. #include "pngpriv.h"
  16. namespace PrivatePng {
  17. #ifdef PNG_READ_SUPPORTED
  18. /* Create a PNG structure for reading, and allocate any memory needed. */
  19. PNG_FUNCTION(png_structp,PNGAPI
  20. png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  21. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  22. {
  23. #ifdef PNG_USER_MEM_SUPPORTED
  24. return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  25. warn_fn, NULL, NULL, NULL));
  26. }
  27. /* Alternate create PNG structure for reading, and allocate any memory
  28. * needed.
  29. */
  30. PNG_FUNCTION(png_structp,PNGAPI
  31. png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  32. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  33. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  34. {
  35. #endif /* PNG_USER_MEM_SUPPORTED */
  36. #ifdef PNG_SETJMP_SUPPORTED
  37. volatile
  38. #endif
  39. png_structp png_ptr;
  40. volatile int png_cleanup_needed = 0;
  41. #ifdef PNG_SETJMP_SUPPORTED
  42. #ifdef USE_FAR_KEYWORD
  43. jmp_buf tmp_jmpbuf;
  44. #endif
  45. #endif
  46. png_debug(1, "in png_create_read_struct");
  47. #ifdef PNG_USER_MEM_SUPPORTED
  48. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  49. malloc_fn, mem_ptr);
  50. #else
  51. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  52. #endif
  53. if (png_ptr == NULL)
  54. return (NULL);
  55. /* Added at libpng-1.2.6 */
  56. #ifdef PNG_USER_LIMITS_SUPPORTED
  57. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  58. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  59. # ifdef PNG_USER_CHUNK_CACHE_MAX
  60. /* Added at libpng-1.2.43 and 1.4.0 */
  61. png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
  62. # endif
  63. # ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
  64. /* Added at libpng-1.2.43 and 1.4.1 */
  65. png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
  66. # endif
  67. #endif
  68. #ifdef PNG_SETJMP_SUPPORTED
  69. /* Applications that neglect to set up their own setjmp() and then
  70. encounter a png_error() will longjmp here. Since the jmpbuf is
  71. then meaningless we abort instead of returning. */
  72. #ifdef USE_FAR_KEYWORD
  73. if (setjmp(tmp_jmpbuf))
  74. #else
  75. if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
  76. #endif
  77. PNG_ABORT();
  78. #ifdef USE_FAR_KEYWORD
  79. png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
  80. #endif
  81. #endif /* PNG_SETJMP_SUPPORTED */
  82. #ifdef PNG_USER_MEM_SUPPORTED
  83. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  84. #endif
  85. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  86. /* Call the general version checker (shared with read and write code): */
  87. if (!png_user_version_check(png_ptr, user_png_ver))
  88. png_cleanup_needed = 1;
  89. if (!png_cleanup_needed)
  90. {
  91. /* Initialize zbuf - compression buffer */
  92. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  93. png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
  94. if (png_ptr->zbuf == NULL)
  95. png_cleanup_needed = 1;
  96. }
  97. png_ptr->zstream.zalloc = png_zalloc;
  98. png_ptr->zstream.zfree = png_zfree;
  99. png_ptr->zstream.opaque = (voidpf)png_ptr;
  100. if (!png_cleanup_needed)
  101. {
  102. switch (inflateInit(&png_ptr->zstream))
  103. {
  104. case Z_OK:
  105. break; /* Do nothing */
  106. case Z_MEM_ERROR:
  107. png_warning(png_ptr, "zlib memory error");
  108. png_cleanup_needed = 1;
  109. break;
  110. case Z_STREAM_ERROR:
  111. png_warning(png_ptr, "zlib stream error");
  112. png_cleanup_needed = 1;
  113. break;
  114. case Z_VERSION_ERROR:
  115. png_warning(png_ptr, "zlib version error");
  116. png_cleanup_needed = 1;
  117. break;
  118. default: png_warning(png_ptr, "Unknown zlib error");
  119. png_cleanup_needed = 1;
  120. }
  121. }
  122. if (png_cleanup_needed)
  123. {
  124. /* Clean up PNG structure and deallocate any memory. */
  125. png_free(png_ptr, png_ptr->zbuf);
  126. png_ptr->zbuf = NULL;
  127. #ifdef PNG_USER_MEM_SUPPORTED
  128. png_destroy_struct_2((png_voidp)png_ptr,
  129. (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  130. #else
  131. png_destroy_struct((png_voidp)png_ptr);
  132. #endif
  133. return (NULL);
  134. }
  135. png_ptr->zstream.next_out = png_ptr->zbuf;
  136. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  137. png_set_read_fn(png_ptr, NULL, NULL);
  138. return (png_ptr);
  139. }
  140. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  141. /* Read the information before the actual image data. This has been
  142. * changed in v0.90 to allow reading a file that already has the magic
  143. * bytes read from the stream. You can tell libpng how many bytes have
  144. * been read from the beginning of the stream (up to the maximum of 8)
  145. * via png_set_sig_bytes(), and we will only check the remaining bytes
  146. * here. The application can then have access to the signature bytes we
  147. * read if it is determined that this isn't a valid PNG file.
  148. */
  149. void PNGAPI
  150. png_read_info(png_structp png_ptr, png_infop info_ptr)
  151. {
  152. png_debug(1, "in png_read_info");
  153. if (png_ptr == NULL || info_ptr == NULL)
  154. return;
  155. /* Read and check the PNG file signature. */
  156. png_read_sig(png_ptr, info_ptr);
  157. for (;;)
  158. {
  159. PNG_IHDR;
  160. PNG_IDAT;
  161. PNG_IEND;
  162. PNG_PLTE;
  163. #ifdef PNG_READ_bKGD_SUPPORTED
  164. PNG_bKGD;
  165. #endif
  166. #ifdef PNG_READ_cHRM_SUPPORTED
  167. PNG_cHRM;
  168. #endif
  169. #ifdef PNG_READ_gAMA_SUPPORTED
  170. PNG_gAMA;
  171. #endif
  172. #ifdef PNG_READ_hIST_SUPPORTED
  173. PNG_hIST;
  174. #endif
  175. #ifdef PNG_READ_iCCP_SUPPORTED
  176. PNG_iCCP;
  177. #endif
  178. #ifdef PNG_READ_iTXt_SUPPORTED
  179. PNG_iTXt;
  180. #endif
  181. #ifdef PNG_READ_oFFs_SUPPORTED
  182. PNG_oFFs;
  183. #endif
  184. #ifdef PNG_READ_pCAL_SUPPORTED
  185. PNG_pCAL;
  186. #endif
  187. #ifdef PNG_READ_pHYs_SUPPORTED
  188. PNG_pHYs;
  189. #endif
  190. #ifdef PNG_READ_sBIT_SUPPORTED
  191. PNG_sBIT;
  192. #endif
  193. #ifdef PNG_READ_sCAL_SUPPORTED
  194. PNG_sCAL;
  195. #endif
  196. #ifdef PNG_READ_sPLT_SUPPORTED
  197. PNG_sPLT;
  198. #endif
  199. #ifdef PNG_READ_sRGB_SUPPORTED
  200. PNG_sRGB;
  201. #endif
  202. #ifdef PNG_READ_tEXt_SUPPORTED
  203. PNG_tEXt;
  204. #endif
  205. #ifdef PNG_READ_tIME_SUPPORTED
  206. PNG_tIME;
  207. #endif
  208. #ifdef PNG_READ_tRNS_SUPPORTED
  209. PNG_tRNS;
  210. #endif
  211. #ifdef PNG_READ_zTXt_SUPPORTED
  212. PNG_zTXt;
  213. #endif
  214. png_uint_32 length = png_read_chunk_header(png_ptr);
  215. PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  216. /* This should be a binary subdivision search or a hash for
  217. * matching the chunk name rather than a linear search.
  218. */
  219. if (!png_memcmp(chunk_name, png_IDAT, 4))
  220. if (png_ptr->mode & PNG_AFTER_IDAT)
  221. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  222. if (!png_memcmp(chunk_name, png_IHDR, 4))
  223. png_handle_IHDR(png_ptr, info_ptr, length);
  224. else if (!png_memcmp(chunk_name, png_IEND, 4))
  225. png_handle_IEND(png_ptr, info_ptr, length);
  226. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  227. else if (png_handle_as_unknown(png_ptr, chunk_name))
  228. {
  229. if (!png_memcmp(chunk_name, png_IDAT, 4))
  230. png_ptr->mode |= PNG_HAVE_IDAT;
  231. png_handle_unknown(png_ptr, info_ptr, length);
  232. if (!png_memcmp(chunk_name, png_PLTE, 4))
  233. png_ptr->mode |= PNG_HAVE_PLTE;
  234. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  235. {
  236. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  237. png_error(png_ptr, "Missing IHDR before IDAT");
  238. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  239. !(png_ptr->mode & PNG_HAVE_PLTE))
  240. png_error(png_ptr, "Missing PLTE before IDAT");
  241. break;
  242. }
  243. }
  244. #endif
  245. else if (!png_memcmp(chunk_name, png_PLTE, 4))
  246. png_handle_PLTE(png_ptr, info_ptr, length);
  247. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  248. {
  249. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  250. png_error(png_ptr, "Missing IHDR before IDAT");
  251. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  252. !(png_ptr->mode & PNG_HAVE_PLTE))
  253. png_error(png_ptr, "Missing PLTE before IDAT");
  254. png_ptr->idat_size = length;
  255. png_ptr->mode |= PNG_HAVE_IDAT;
  256. break;
  257. }
  258. #ifdef PNG_READ_bKGD_SUPPORTED
  259. else if (!png_memcmp(chunk_name, png_bKGD, 4))
  260. png_handle_bKGD(png_ptr, info_ptr, length);
  261. #endif
  262. #ifdef PNG_READ_cHRM_SUPPORTED
  263. else if (!png_memcmp(chunk_name, png_cHRM, 4))
  264. png_handle_cHRM(png_ptr, info_ptr, length);
  265. #endif
  266. #ifdef PNG_READ_gAMA_SUPPORTED
  267. else if (!png_memcmp(chunk_name, png_gAMA, 4))
  268. png_handle_gAMA(png_ptr, info_ptr, length);
  269. #endif
  270. #ifdef PNG_READ_hIST_SUPPORTED
  271. else if (!png_memcmp(chunk_name, png_hIST, 4))
  272. png_handle_hIST(png_ptr, info_ptr, length);
  273. #endif
  274. #ifdef PNG_READ_oFFs_SUPPORTED
  275. else if (!png_memcmp(chunk_name, png_oFFs, 4))
  276. png_handle_oFFs(png_ptr, info_ptr, length);
  277. #endif
  278. #ifdef PNG_READ_pCAL_SUPPORTED
  279. else if (!png_memcmp(chunk_name, png_pCAL, 4))
  280. png_handle_pCAL(png_ptr, info_ptr, length);
  281. #endif
  282. #ifdef PNG_READ_sCAL_SUPPORTED
  283. else if (!png_memcmp(chunk_name, png_sCAL, 4))
  284. png_handle_sCAL(png_ptr, info_ptr, length);
  285. #endif
  286. #ifdef PNG_READ_pHYs_SUPPORTED
  287. else if (!png_memcmp(chunk_name, png_pHYs, 4))
  288. png_handle_pHYs(png_ptr, info_ptr, length);
  289. #endif
  290. #ifdef PNG_READ_sBIT_SUPPORTED
  291. else if (!png_memcmp(chunk_name, png_sBIT, 4))
  292. png_handle_sBIT(png_ptr, info_ptr, length);
  293. #endif
  294. #ifdef PNG_READ_sRGB_SUPPORTED
  295. else if (!png_memcmp(chunk_name, png_sRGB, 4))
  296. png_handle_sRGB(png_ptr, info_ptr, length);
  297. #endif
  298. #ifdef PNG_READ_iCCP_SUPPORTED
  299. else if (!png_memcmp(chunk_name, png_iCCP, 4))
  300. png_handle_iCCP(png_ptr, info_ptr, length);
  301. #endif
  302. #ifdef PNG_READ_sPLT_SUPPORTED
  303. else if (!png_memcmp(chunk_name, png_sPLT, 4))
  304. png_handle_sPLT(png_ptr, info_ptr, length);
  305. #endif
  306. #ifdef PNG_READ_tEXt_SUPPORTED
  307. else if (!png_memcmp(chunk_name, png_tEXt, 4))
  308. png_handle_tEXt(png_ptr, info_ptr, length);
  309. #endif
  310. #ifdef PNG_READ_tIME_SUPPORTED
  311. else if (!png_memcmp(chunk_name, png_tIME, 4))
  312. png_handle_tIME(png_ptr, info_ptr, length);
  313. #endif
  314. #ifdef PNG_READ_tRNS_SUPPORTED
  315. else if (!png_memcmp(chunk_name, png_tRNS, 4))
  316. png_handle_tRNS(png_ptr, info_ptr, length);
  317. #endif
  318. #ifdef PNG_READ_zTXt_SUPPORTED
  319. else if (!png_memcmp(chunk_name, png_zTXt, 4))
  320. png_handle_zTXt(png_ptr, info_ptr, length);
  321. #endif
  322. #ifdef PNG_READ_iTXt_SUPPORTED
  323. else if (!png_memcmp(chunk_name, png_iTXt, 4))
  324. png_handle_iTXt(png_ptr, info_ptr, length);
  325. #endif
  326. else
  327. png_handle_unknown(png_ptr, info_ptr, length);
  328. }
  329. }
  330. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  331. /* Optional call to update the users info_ptr structure */
  332. void PNGAPI
  333. png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  334. {
  335. png_debug(1, "in png_read_update_info");
  336. if (png_ptr == NULL)
  337. return;
  338. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  339. png_read_start_row(png_ptr);
  340. else
  341. png_warning(png_ptr,
  342. "Ignoring extra png_read_update_info() call;"
  343. " row buffer not reallocated");
  344. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  345. png_read_transform_info(png_ptr, info_ptr);
  346. #else
  347. PNG_UNUSED(info_ptr)
  348. #endif
  349. }
  350. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  351. /* Initialize palette, background, etc, after transformations
  352. * are set, but before any reading takes place. This allows
  353. * the user to obtain a gamma-corrected palette, for example.
  354. * If the user doesn't call this, we will do it ourselves.
  355. */
  356. void PNGAPI
  357. png_start_read_image(png_structp png_ptr)
  358. {
  359. png_debug(1, "in png_start_read_image");
  360. if (png_ptr == NULL)
  361. return;
  362. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  363. png_read_start_row(png_ptr);
  364. else
  365. png_warning(png_ptr,
  366. "Ignoring extra png_start_read_image() call;"
  367. " row buffer not reallocated");
  368. }
  369. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  370. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  371. void PNGAPI
  372. png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  373. {
  374. PNG_IDAT;
  375. #ifdef PNG_READ_INTERLACING_SUPPORTED
  376. PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  377. 0xff};
  378. PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  379. #endif
  380. int ret;
  381. if (png_ptr == NULL)
  382. return;
  383. png_debug2(1, "in png_read_row (row %lu, pass %d)",
  384. (unsigned long)png_ptr->row_number, png_ptr->pass);
  385. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  386. png_read_start_row(png_ptr);
  387. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  388. {
  389. /* Check for transforms that have been set but were defined out */
  390. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  391. if (png_ptr->transformations & PNG_INVERT_MONO)
  392. png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
  393. #endif
  394. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  395. if (png_ptr->transformations & PNG_FILLER)
  396. png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
  397. #endif
  398. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  399. !defined(PNG_READ_PACKSWAP_SUPPORTED)
  400. if (png_ptr->transformations & PNG_PACKSWAP)
  401. png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
  402. #endif
  403. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  404. if (png_ptr->transformations & PNG_PACK)
  405. png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
  406. #endif
  407. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  408. if (png_ptr->transformations & PNG_SHIFT)
  409. png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
  410. #endif
  411. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  412. if (png_ptr->transformations & PNG_BGR)
  413. png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
  414. #endif
  415. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  416. if (png_ptr->transformations & PNG_SWAP_BYTES)
  417. png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
  418. #endif
  419. }
  420. #ifdef PNG_READ_INTERLACING_SUPPORTED
  421. /* If interlaced and we do not need a new row, combine row and return */
  422. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  423. {
  424. switch (png_ptr->pass)
  425. {
  426. case 0:
  427. if (png_ptr->row_number & 0x07)
  428. {
  429. if (dsp_row != NULL)
  430. png_combine_row(png_ptr, dsp_row,
  431. png_pass_dsp_mask[png_ptr->pass]);
  432. png_read_finish_row(png_ptr);
  433. return;
  434. }
  435. break;
  436. case 1:
  437. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  438. {
  439. if (dsp_row != NULL)
  440. png_combine_row(png_ptr, dsp_row,
  441. png_pass_dsp_mask[png_ptr->pass]);
  442. png_read_finish_row(png_ptr);
  443. return;
  444. }
  445. break;
  446. case 2:
  447. if ((png_ptr->row_number & 0x07) != 4)
  448. {
  449. if (dsp_row != NULL && (png_ptr->row_number & 4))
  450. png_combine_row(png_ptr, dsp_row,
  451. png_pass_dsp_mask[png_ptr->pass]);
  452. png_read_finish_row(png_ptr);
  453. return;
  454. }
  455. break;
  456. case 3:
  457. if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  458. {
  459. if (dsp_row != NULL)
  460. png_combine_row(png_ptr, dsp_row,
  461. png_pass_dsp_mask[png_ptr->pass]);
  462. png_read_finish_row(png_ptr);
  463. return;
  464. }
  465. break;
  466. case 4:
  467. if ((png_ptr->row_number & 3) != 2)
  468. {
  469. if (dsp_row != NULL && (png_ptr->row_number & 2))
  470. png_combine_row(png_ptr, dsp_row,
  471. png_pass_dsp_mask[png_ptr->pass]);
  472. png_read_finish_row(png_ptr);
  473. return;
  474. }
  475. break;
  476. case 5:
  477. if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  478. {
  479. if (dsp_row != NULL)
  480. png_combine_row(png_ptr, dsp_row,
  481. png_pass_dsp_mask[png_ptr->pass]);
  482. png_read_finish_row(png_ptr);
  483. return;
  484. }
  485. break;
  486. default:
  487. case 6:
  488. if (!(png_ptr->row_number & 1))
  489. {
  490. png_read_finish_row(png_ptr);
  491. return;
  492. }
  493. break;
  494. }
  495. }
  496. #endif
  497. if (!(png_ptr->mode & PNG_HAVE_IDAT))
  498. png_error(png_ptr, "Invalid attempt to read row data");
  499. png_ptr->zstream.next_out = png_ptr->row_buf;
  500. png_ptr->zstream.avail_out =
  501. (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  502. png_ptr->iwidth) + 1);
  503. do
  504. {
  505. if (!(png_ptr->zstream.avail_in))
  506. {
  507. while (!png_ptr->idat_size)
  508. {
  509. png_crc_finish(png_ptr, 0);
  510. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  511. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  512. png_error(png_ptr, "Not enough image data");
  513. }
  514. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  515. png_ptr->zstream.next_in = png_ptr->zbuf;
  516. if (png_ptr->zbuf_size > png_ptr->idat_size)
  517. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  518. png_crc_read(png_ptr, png_ptr->zbuf,
  519. (png_size_t)png_ptr->zstream.avail_in);
  520. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  521. }
  522. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  523. if (ret == Z_STREAM_END)
  524. {
  525. if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  526. png_ptr->idat_size)
  527. png_benign_error(png_ptr, "Extra compressed data");
  528. png_ptr->mode |= PNG_AFTER_IDAT;
  529. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  530. break;
  531. }
  532. if (ret != Z_OK)
  533. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  534. "Decompression error");
  535. } while (png_ptr->zstream.avail_out);
  536. png_ptr->row_info.color_type = png_ptr->color_type;
  537. png_ptr->row_info.width = png_ptr->iwidth;
  538. png_ptr->row_info.channels = png_ptr->channels;
  539. png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  540. png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  541. png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  542. png_ptr->row_info.width);
  543. if (png_ptr->row_buf[0])
  544. png_read_filter_row(png_ptr, &(png_ptr->row_info),
  545. png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  546. (int)(png_ptr->row_buf[0]));
  547. png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
  548. #ifdef PNG_MNG_FEATURES_SUPPORTED
  549. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  550. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  551. {
  552. /* Intrapixel differencing */
  553. png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  554. }
  555. #endif
  556. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  557. if (png_ptr->transformations)
  558. png_do_read_transformations(png_ptr);
  559. #endif
  560. #ifdef PNG_READ_INTERLACING_SUPPORTED
  561. /* Blow up interlaced rows to full size */
  562. if (png_ptr->interlaced &&
  563. (png_ptr->transformations & PNG_INTERLACE))
  564. {
  565. if (png_ptr->pass < 6)
  566. /* Old interface (pre-1.0.9):
  567. * png_do_read_interlace(&(png_ptr->row_info),
  568. * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  569. */
  570. png_do_read_interlace(png_ptr);
  571. if (dsp_row != NULL)
  572. png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]);
  573. if (row != NULL)
  574. png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]);
  575. }
  576. else
  577. #endif
  578. {
  579. if (row != NULL)
  580. png_combine_row(png_ptr, row, 0xff);
  581. if (dsp_row != NULL)
  582. png_combine_row(png_ptr, dsp_row, 0xff);
  583. }
  584. png_read_finish_row(png_ptr);
  585. if (png_ptr->read_row_fn != NULL)
  586. (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  587. }
  588. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  589. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  590. /* Read one or more rows of image data. If the image is interlaced,
  591. * and png_set_interlace_handling() has been called, the rows need to
  592. * contain the contents of the rows from the previous pass. If the
  593. * image has alpha or transparency, and png_handle_alpha()[*] has been
  594. * called, the rows contents must be initialized to the contents of the
  595. * screen.
  596. *
  597. * "row" holds the actual image, and pixels are placed in it
  598. * as they arrive. If the image is displayed after each pass, it will
  599. * appear to "sparkle" in. "display_row" can be used to display a
  600. * "chunky" progressive image, with finer detail added as it becomes
  601. * available. If you do not want this "chunky" display, you may pass
  602. * NULL for display_row. If you do not want the sparkle display, and
  603. * you have not called png_handle_alpha(), you may pass NULL for rows.
  604. * If you have called png_handle_alpha(), and the image has either an
  605. * alpha channel or a transparency chunk, you must provide a buffer for
  606. * rows. In this case, you do not have to provide a display_row buffer
  607. * also, but you may. If the image is not interlaced, or if you have
  608. * not called png_set_interlace_handling(), the display_row buffer will
  609. * be ignored, so pass NULL to it.
  610. *
  611. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  612. */
  613. void PNGAPI
  614. png_read_rows(png_structp png_ptr, png_bytepp row,
  615. png_bytepp display_row, png_uint_32 num_rows)
  616. {
  617. png_uint_32 i;
  618. png_bytepp rp;
  619. png_bytepp dp;
  620. png_debug(1, "in png_read_rows");
  621. if (png_ptr == NULL)
  622. return;
  623. rp = row;
  624. dp = display_row;
  625. if (rp != NULL && dp != NULL)
  626. for (i = 0; i < num_rows; i++)
  627. {
  628. png_bytep rptr = *rp++;
  629. png_bytep dptr = *dp++;
  630. png_read_row(png_ptr, rptr, dptr);
  631. }
  632. else if (rp != NULL)
  633. for (i = 0; i < num_rows; i++)
  634. {
  635. png_bytep rptr = *rp;
  636. png_read_row(png_ptr, rptr, NULL);
  637. rp++;
  638. }
  639. else if (dp != NULL)
  640. for (i = 0; i < num_rows; i++)
  641. {
  642. png_bytep dptr = *dp;
  643. png_read_row(png_ptr, NULL, dptr);
  644. dp++;
  645. }
  646. }
  647. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  648. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  649. /* Read the entire image. If the image has an alpha channel or a tRNS
  650. * chunk, and you have called png_handle_alpha()[*], you will need to
  651. * initialize the image to the current image that PNG will be overlaying.
  652. * We set the num_rows again here, in case it was incorrectly set in
  653. * png_read_start_row() by a call to png_read_update_info() or
  654. * png_start_read_image() if png_set_interlace_handling() wasn't called
  655. * prior to either of these functions like it should have been. You can
  656. * only call this function once. If you desire to have an image for
  657. * each pass of a interlaced image, use png_read_rows() instead.
  658. *
  659. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  660. */
  661. void PNGAPI
  662. png_read_image(png_structp png_ptr, png_bytepp image)
  663. {
  664. png_uint_32 i, image_height;
  665. int pass, j;
  666. png_bytepp rp;
  667. png_debug(1, "in png_read_image");
  668. if (png_ptr == NULL)
  669. return;
  670. #ifdef PNG_READ_INTERLACING_SUPPORTED
  671. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  672. {
  673. pass = png_set_interlace_handling(png_ptr);
  674. /* And make sure transforms are initialized. */
  675. png_start_read_image(png_ptr);
  676. }
  677. else
  678. {
  679. if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
  680. {
  681. /* Caller called png_start_read_image or png_read_update_info without
  682. * first turning on the PNG_INTERLACE transform. We can fix this here,
  683. * but the caller should do it!
  684. */
  685. png_warning(png_ptr, "Interlace handling should be turned on when "
  686. "using png_read_image");
  687. /* Make sure this is set correctly */
  688. png_ptr->num_rows = png_ptr->height;
  689. }
  690. /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
  691. * the above error case.
  692. */
  693. pass = png_set_interlace_handling(png_ptr);
  694. }
  695. #else
  696. if (png_ptr->interlaced)
  697. png_error(png_ptr,
  698. "Cannot read interlaced image -- interlace handler disabled");
  699. pass = 1;
  700. #endif
  701. image_height=png_ptr->height;
  702. for (j = 0; j < pass; j++)
  703. {
  704. rp = image;
  705. for (i = 0; i < image_height; i++)
  706. {
  707. png_read_row(png_ptr, *rp, NULL);
  708. rp++;
  709. }
  710. }
  711. }
  712. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  713. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  714. /* Read the end of the PNG file. Will not read past the end of the
  715. * file, will verify the end is accurate, and will read any comments
  716. * or time information at the end of the file, if info is not NULL.
  717. */
  718. void PNGAPI
  719. png_read_end(png_structp png_ptr, png_infop info_ptr)
  720. {
  721. png_debug(1, "in png_read_end");
  722. if (png_ptr == NULL)
  723. return;
  724. png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  725. do
  726. {
  727. PNG_IHDR;
  728. PNG_IDAT;
  729. PNG_IEND;
  730. PNG_PLTE;
  731. #ifdef PNG_READ_bKGD_SUPPORTED
  732. PNG_bKGD;
  733. #endif
  734. #ifdef PNG_READ_cHRM_SUPPORTED
  735. PNG_cHRM;
  736. #endif
  737. #ifdef PNG_READ_gAMA_SUPPORTED
  738. PNG_gAMA;
  739. #endif
  740. #ifdef PNG_READ_hIST_SUPPORTED
  741. PNG_hIST;
  742. #endif
  743. #ifdef PNG_READ_iCCP_SUPPORTED
  744. PNG_iCCP;
  745. #endif
  746. #ifdef PNG_READ_iTXt_SUPPORTED
  747. PNG_iTXt;
  748. #endif
  749. #ifdef PNG_READ_oFFs_SUPPORTED
  750. PNG_oFFs;
  751. #endif
  752. #ifdef PNG_READ_pCAL_SUPPORTED
  753. PNG_pCAL;
  754. #endif
  755. #ifdef PNG_READ_pHYs_SUPPORTED
  756. PNG_pHYs;
  757. #endif
  758. #ifdef PNG_READ_sBIT_SUPPORTED
  759. PNG_sBIT;
  760. #endif
  761. #ifdef PNG_READ_sCAL_SUPPORTED
  762. PNG_sCAL;
  763. #endif
  764. #ifdef PNG_READ_sPLT_SUPPORTED
  765. PNG_sPLT;
  766. #endif
  767. #ifdef PNG_READ_sRGB_SUPPORTED
  768. PNG_sRGB;
  769. #endif
  770. #ifdef PNG_READ_tEXt_SUPPORTED
  771. PNG_tEXt;
  772. #endif
  773. #ifdef PNG_READ_tIME_SUPPORTED
  774. PNG_tIME;
  775. #endif
  776. #ifdef PNG_READ_tRNS_SUPPORTED
  777. PNG_tRNS;
  778. #endif
  779. #ifdef PNG_READ_zTXt_SUPPORTED
  780. PNG_zTXt;
  781. #endif
  782. png_uint_32 length = png_read_chunk_header(png_ptr);
  783. PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  784. if (!png_memcmp(chunk_name, png_IHDR, 4))
  785. png_handle_IHDR(png_ptr, info_ptr, length);
  786. else if (!png_memcmp(chunk_name, png_IEND, 4))
  787. png_handle_IEND(png_ptr, info_ptr, length);
  788. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  789. else if (png_handle_as_unknown(png_ptr, chunk_name))
  790. {
  791. if (!png_memcmp(chunk_name, png_IDAT, 4))
  792. {
  793. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  794. png_benign_error(png_ptr, "Too many IDATs found");
  795. }
  796. png_handle_unknown(png_ptr, info_ptr, length);
  797. if (!png_memcmp(chunk_name, png_PLTE, 4))
  798. png_ptr->mode |= PNG_HAVE_PLTE;
  799. }
  800. #endif
  801. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  802. {
  803. /* Zero length IDATs are legal after the last IDAT has been
  804. * read, but not after other chunks have been read.
  805. */
  806. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  807. png_benign_error(png_ptr, "Too many IDATs found");
  808. png_crc_finish(png_ptr, length);
  809. }
  810. else if (!png_memcmp(chunk_name, png_PLTE, 4))
  811. png_handle_PLTE(png_ptr, info_ptr, length);
  812. #ifdef PNG_READ_bKGD_SUPPORTED
  813. else if (!png_memcmp(chunk_name, png_bKGD, 4))
  814. png_handle_bKGD(png_ptr, info_ptr, length);
  815. #endif
  816. #ifdef PNG_READ_cHRM_SUPPORTED
  817. else if (!png_memcmp(chunk_name, png_cHRM, 4))
  818. png_handle_cHRM(png_ptr, info_ptr, length);
  819. #endif
  820. #ifdef PNG_READ_gAMA_SUPPORTED
  821. else if (!png_memcmp(chunk_name, png_gAMA, 4))
  822. png_handle_gAMA(png_ptr, info_ptr, length);
  823. #endif
  824. #ifdef PNG_READ_hIST_SUPPORTED
  825. else if (!png_memcmp(chunk_name, png_hIST, 4))
  826. png_handle_hIST(png_ptr, info_ptr, length);
  827. #endif
  828. #ifdef PNG_READ_oFFs_SUPPORTED
  829. else if (!png_memcmp(chunk_name, png_oFFs, 4))
  830. png_handle_oFFs(png_ptr, info_ptr, length);
  831. #endif
  832. #ifdef PNG_READ_pCAL_SUPPORTED
  833. else if (!png_memcmp(chunk_name, png_pCAL, 4))
  834. png_handle_pCAL(png_ptr, info_ptr, length);
  835. #endif
  836. #ifdef PNG_READ_sCAL_SUPPORTED
  837. else if (!png_memcmp(chunk_name, png_sCAL, 4))
  838. png_handle_sCAL(png_ptr, info_ptr, length);
  839. #endif
  840. #ifdef PNG_READ_pHYs_SUPPORTED
  841. else if (!png_memcmp(chunk_name, png_pHYs, 4))
  842. png_handle_pHYs(png_ptr, info_ptr, length);
  843. #endif
  844. #ifdef PNG_READ_sBIT_SUPPORTED
  845. else if (!png_memcmp(chunk_name, png_sBIT, 4))
  846. png_handle_sBIT(png_ptr, info_ptr, length);
  847. #endif
  848. #ifdef PNG_READ_sRGB_SUPPORTED
  849. else if (!png_memcmp(chunk_name, png_sRGB, 4))
  850. png_handle_sRGB(png_ptr, info_ptr, length);
  851. #endif
  852. #ifdef PNG_READ_iCCP_SUPPORTED
  853. else if (!png_memcmp(chunk_name, png_iCCP, 4))
  854. png_handle_iCCP(png_ptr, info_ptr, length);
  855. #endif
  856. #ifdef PNG_READ_sPLT_SUPPORTED
  857. else if (!png_memcmp(chunk_name, png_sPLT, 4))
  858. png_handle_sPLT(png_ptr, info_ptr, length);
  859. #endif
  860. #ifdef PNG_READ_tEXt_SUPPORTED
  861. else if (!png_memcmp(chunk_name, png_tEXt, 4))
  862. png_handle_tEXt(png_ptr, info_ptr, length);
  863. #endif
  864. #ifdef PNG_READ_tIME_SUPPORTED
  865. else if (!png_memcmp(chunk_name, png_tIME, 4))
  866. png_handle_tIME(png_ptr, info_ptr, length);
  867. #endif
  868. #ifdef PNG_READ_tRNS_SUPPORTED
  869. else if (!png_memcmp(chunk_name, png_tRNS, 4))
  870. png_handle_tRNS(png_ptr, info_ptr, length);
  871. #endif
  872. #ifdef PNG_READ_zTXt_SUPPORTED
  873. else if (!png_memcmp(chunk_name, png_zTXt, 4))
  874. png_handle_zTXt(png_ptr, info_ptr, length);
  875. #endif
  876. #ifdef PNG_READ_iTXt_SUPPORTED
  877. else if (!png_memcmp(chunk_name, png_iTXt, 4))
  878. png_handle_iTXt(png_ptr, info_ptr, length);
  879. #endif
  880. else
  881. png_handle_unknown(png_ptr, info_ptr, length);
  882. } while (!(png_ptr->mode & PNG_HAVE_IEND));
  883. }
  884. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  885. /* Free all memory used by the read */
  886. void PNGAPI
  887. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  888. png_infopp end_info_ptr_ptr)
  889. {
  890. png_structp png_ptr = NULL;
  891. png_infop info_ptr = NULL, end_info_ptr = NULL;
  892. #ifdef PNG_USER_MEM_SUPPORTED
  893. png_free_ptr free_fn = NULL;
  894. png_voidp mem_ptr = NULL;
  895. #endif
  896. png_debug(1, "in png_destroy_read_struct");
  897. if (png_ptr_ptr != NULL)
  898. png_ptr = *png_ptr_ptr;
  899. if (png_ptr == NULL)
  900. return;
  901. #ifdef PNG_USER_MEM_SUPPORTED
  902. free_fn = png_ptr->free_fn;
  903. mem_ptr = png_ptr->mem_ptr;
  904. #endif
  905. if (info_ptr_ptr != NULL)
  906. info_ptr = *info_ptr_ptr;
  907. if (end_info_ptr_ptr != NULL)
  908. end_info_ptr = *end_info_ptr_ptr;
  909. png_read_destroy(png_ptr, info_ptr, end_info_ptr);
  910. if (info_ptr != NULL)
  911. {
  912. #ifdef PNG_TEXT_SUPPORTED
  913. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
  914. #endif
  915. #ifdef PNG_USER_MEM_SUPPORTED
  916. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  917. (png_voidp)mem_ptr);
  918. #else
  919. png_destroy_struct((png_voidp)info_ptr);
  920. #endif
  921. *info_ptr_ptr = NULL;
  922. }
  923. if (end_info_ptr != NULL)
  924. {
  925. #ifdef PNG_READ_TEXT_SUPPORTED
  926. png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
  927. #endif
  928. #ifdef PNG_USER_MEM_SUPPORTED
  929. png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
  930. (png_voidp)mem_ptr);
  931. #else
  932. png_destroy_struct((png_voidp)end_info_ptr);
  933. #endif
  934. *end_info_ptr_ptr = NULL;
  935. }
  936. if (png_ptr != NULL)
  937. {
  938. #ifdef PNG_USER_MEM_SUPPORTED
  939. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  940. (png_voidp)mem_ptr);
  941. #else
  942. png_destroy_struct((png_voidp)png_ptr);
  943. #endif
  944. *png_ptr_ptr = NULL;
  945. }
  946. }
  947. /* Free all memory used by the read (old method) */
  948. void /* PRIVATE */
  949. png_read_destroy(png_structp png_ptr, png_infop info_ptr,
  950. png_infop end_info_ptr)
  951. {
  952. #ifdef PNG_SETJMP_SUPPORTED
  953. jmp_buf tmp_jmp;
  954. #endif
  955. png_error_ptr error_fn;
  956. #ifdef PNG_WARNINGS_SUPPORTED
  957. png_error_ptr warning_fn;
  958. #endif
  959. png_voidp error_ptr;
  960. #ifdef PNG_USER_MEM_SUPPORTED
  961. png_free_ptr free_fn;
  962. #endif
  963. png_debug(1, "in png_read_destroy");
  964. if (info_ptr != NULL)
  965. png_info_destroy(png_ptr, info_ptr);
  966. if (end_info_ptr != NULL)
  967. png_info_destroy(png_ptr, end_info_ptr);
  968. png_free(png_ptr, png_ptr->zbuf);
  969. png_free(png_ptr, png_ptr->big_row_buf);
  970. png_free(png_ptr, png_ptr->prev_row);
  971. png_free(png_ptr, png_ptr->chunkdata);
  972. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  973. png_free(png_ptr, png_ptr->palette_lookup);
  974. png_free(png_ptr, png_ptr->quantize_index);
  975. #endif
  976. #ifdef PNG_READ_GAMMA_SUPPORTED
  977. png_free(png_ptr, png_ptr->gamma_table);
  978. #endif
  979. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  980. png_free(png_ptr, png_ptr->gamma_from_1);
  981. png_free(png_ptr, png_ptr->gamma_to_1);
  982. #endif
  983. if (png_ptr->free_me & PNG_FREE_PLTE)
  984. png_zfree(png_ptr, png_ptr->palette);
  985. png_ptr->free_me &= ~PNG_FREE_PLTE;
  986. #if defined(PNG_tRNS_SUPPORTED) || \
  987. defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  988. if (png_ptr->free_me & PNG_FREE_TRNS)
  989. png_free(png_ptr, png_ptr->trans_alpha);
  990. png_ptr->free_me &= ~PNG_FREE_TRNS;
  991. #endif
  992. #ifdef PNG_READ_hIST_SUPPORTED
  993. if (png_ptr->free_me & PNG_FREE_HIST)
  994. png_free(png_ptr, png_ptr->hist);
  995. png_ptr->free_me &= ~PNG_FREE_HIST;
  996. #endif
  997. #ifdef PNG_READ_GAMMA_SUPPORTED
  998. if (png_ptr->gamma_16_table != NULL)
  999. {
  1000. int i;
  1001. int istop = (1 << (8 - png_ptr->gamma_shift));
  1002. for (i = 0; i < istop; i++)
  1003. {
  1004. png_free(png_ptr, png_ptr->gamma_16_table[i]);
  1005. }
  1006. png_free(png_ptr, png_ptr->gamma_16_table);
  1007. }
  1008. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1009. if (png_ptr->gamma_16_from_1 != NULL)
  1010. {
  1011. int i;
  1012. int istop = (1 << (8 - png_ptr->gamma_shift));
  1013. for (i = 0; i < istop; i++)
  1014. {
  1015. png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
  1016. }
  1017. png_free(png_ptr, png_ptr->gamma_16_from_1);
  1018. }
  1019. if (png_ptr->gamma_16_to_1 != NULL)
  1020. {
  1021. int i;
  1022. int istop = (1 << (8 - png_ptr->gamma_shift));
  1023. for (i = 0; i < istop; i++)
  1024. {
  1025. png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
  1026. }
  1027. png_free(png_ptr, png_ptr->gamma_16_to_1);
  1028. }
  1029. #endif
  1030. #endif
  1031. inflateEnd(&png_ptr->zstream);
  1032. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1033. png_free(png_ptr, png_ptr->save_buffer);
  1034. #endif
  1035. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1036. #ifdef PNG_TEXT_SUPPORTED
  1037. png_free(png_ptr, png_ptr->current_text);
  1038. #endif /* PNG_TEXT_SUPPORTED */
  1039. #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
  1040. /* Save the important info out of the png_struct, in case it is
  1041. * being used again.
  1042. */
  1043. #ifdef PNG_SETJMP_SUPPORTED
  1044. png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
  1045. #endif
  1046. error_fn = png_ptr->error_fn;
  1047. #ifdef PNG_WARNINGS_SUPPORTED
  1048. warning_fn = png_ptr->warning_fn;
  1049. #endif
  1050. error_ptr = png_ptr->error_ptr;
  1051. #ifdef PNG_USER_MEM_SUPPORTED
  1052. free_fn = png_ptr->free_fn;
  1053. #endif
  1054. png_memset(png_ptr, 0, png_sizeof(png_struct));
  1055. png_ptr->error_fn = error_fn;
  1056. #ifdef PNG_WARNINGS_SUPPORTED
  1057. png_ptr->warning_fn = warning_fn;
  1058. #endif
  1059. png_ptr->error_ptr = error_ptr;
  1060. #ifdef PNG_USER_MEM_SUPPORTED
  1061. png_ptr->free_fn = free_fn;
  1062. #endif
  1063. #ifdef PNG_SETJMP_SUPPORTED
  1064. png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
  1065. #endif
  1066. }
  1067. void PNGAPI
  1068. png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
  1069. {
  1070. if (png_ptr == NULL)
  1071. return;
  1072. png_ptr->read_row_fn = read_row_fn;
  1073. }
  1074. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  1075. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1076. void PNGAPI
  1077. png_read_png(png_structp png_ptr, png_infop info_ptr,
  1078. int transforms,
  1079. voidp params)
  1080. {
  1081. int row;
  1082. if (png_ptr == NULL || info_ptr == NULL)
  1083. return;
  1084. /* png_read_info() gives us all of the information from the
  1085. * PNG file before the first IDAT (image data chunk).
  1086. */
  1087. png_read_info(png_ptr, info_ptr);
  1088. if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
  1089. png_error(png_ptr, "Image is too high to process with png_read_png()");
  1090. /* -------------- image transformations start here ------------------- */
  1091. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1092. /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
  1093. */
  1094. if (transforms & PNG_TRANSFORM_SCALE_16)
  1095. {
  1096. /* Added at libpng-1.5.4. "strip_16" produces the same result that it
  1097. * did in earlier versions, while "scale_16" is now more accurate.
  1098. */
  1099. png_set_scale_16(png_ptr);
  1100. }
  1101. #endif
  1102. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1103. /* If both SCALE and STRIP are required pngrtran will effectively cancel the
  1104. * latter by doing SCALE first. This is ok and allows apps not to check for
  1105. * which is supported to get the right answer.
  1106. */
  1107. if (transforms & PNG_TRANSFORM_STRIP_16)
  1108. png_set_strip_16(png_ptr);
  1109. #endif
  1110. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1111. /* Strip alpha bytes from the input data without combining with
  1112. * the background (not recommended).
  1113. */
  1114. if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
  1115. png_set_strip_alpha(png_ptr);
  1116. #endif
  1117. #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
  1118. /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  1119. * byte into separate bytes (useful for paletted and grayscale images).
  1120. */
  1121. if (transforms & PNG_TRANSFORM_PACKING)
  1122. png_set_packing(png_ptr);
  1123. #endif
  1124. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  1125. /* Change the order of packed pixels to least significant bit first
  1126. * (not useful if you are using png_set_packing).
  1127. */
  1128. if (transforms & PNG_TRANSFORM_PACKSWAP)
  1129. png_set_packswap(png_ptr);
  1130. #endif
  1131. #ifdef PNG_READ_EXPAND_SUPPORTED
  1132. /* Expand paletted colors into true RGB triplets
  1133. * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  1134. * Expand paletted or RGB images with transparency to full alpha
  1135. * channels so the data will be available as RGBA quartets.
  1136. */
  1137. if (transforms & PNG_TRANSFORM_EXPAND)
  1138. if ((png_ptr->bit_depth < 8) ||
  1139. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  1140. (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
  1141. png_set_expand(png_ptr);
  1142. #endif
  1143. /* We don't handle background color or gamma transformation or quantizing.
  1144. */
  1145. #ifdef PNG_READ_INVERT_SUPPORTED
  1146. /* Invert monochrome files to have 0 as white and 1 as black
  1147. */
  1148. if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1149. png_set_invert_mono(png_ptr);
  1150. #endif
  1151. #ifdef PNG_READ_SHIFT_SUPPORTED
  1152. /* If you want to shift the pixel values from the range [0,255] or
  1153. * [0,65535] to the original [0,7] or [0,31], or whatever range the
  1154. * colors were originally in:
  1155. */
  1156. if ((transforms & PNG_TRANSFORM_SHIFT)
  1157. && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
  1158. {
  1159. png_color_8p sig_bit;
  1160. png_get_sBIT(png_ptr, info_ptr, &sig_bit);
  1161. png_set_shift(png_ptr, sig_bit);
  1162. }
  1163. #endif
  1164. #ifdef PNG_READ_BGR_SUPPORTED
  1165. /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
  1166. if (transforms & PNG_TRANSFORM_BGR)
  1167. png_set_bgr(png_ptr);
  1168. #endif
  1169. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  1170. /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
  1171. if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1172. png_set_swap_alpha(png_ptr);
  1173. #endif
  1174. #ifdef PNG_READ_SWAP_SUPPORTED
  1175. /* Swap bytes of 16-bit files to least significant byte first */
  1176. if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1177. png_set_swap(png_ptr);
  1178. #endif
  1179. /* Added at libpng-1.2.41 */
  1180. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1181. /* Invert the alpha channel from opacity to transparency */
  1182. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1183. png_set_invert_alpha(png_ptr);
  1184. #endif
  1185. /* Added at libpng-1.2.41 */
  1186. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1187. /* Expand grayscale image to RGB */
  1188. if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
  1189. png_set_gray_to_rgb(png_ptr);
  1190. #endif
  1191. /* Added at libpng-1.5.4 */
  1192. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1193. if (transforms & PNG_TRANSFORM_EXPAND_16)
  1194. png_set_expand_16(png_ptr);
  1195. #endif
  1196. /* We don't handle adding filler bytes */
  1197. /* We use png_read_image and rely on that for interlace handling, but we also
  1198. * call png_read_update_info therefore must turn on interlace handling now:
  1199. */
  1200. (void)png_set_interlace_handling(png_ptr);
  1201. /* Optional call to gamma correct and add the background to the palette
  1202. * and update info structure. REQUIRED if you are expecting libpng to
  1203. * update the palette for you (i.e., you selected such a transform above).
  1204. */
  1205. png_read_update_info(png_ptr, info_ptr);
  1206. /* -------------- image transformations end here ------------------- */
  1207. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1208. if (info_ptr->row_pointers == NULL)
  1209. {
  1210. png_uint_32 iptr;
  1211. info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
  1212. info_ptr->height * png_sizeof(png_bytep));
  1213. for (iptr=0; iptr<info_ptr->height; iptr++)
  1214. info_ptr->row_pointers[iptr] = NULL;
  1215. info_ptr->free_me |= PNG_FREE_ROWS;
  1216. for (row = 0; row < (int)info_ptr->height; row++)
  1217. info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
  1218. png_get_rowbytes(png_ptr, info_ptr));
  1219. }
  1220. png_read_image(png_ptr, info_ptr->row_pointers);
  1221. info_ptr->valid |= PNG_INFO_IDAT;
  1222. /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1223. png_read_end(png_ptr, info_ptr);
  1224. PNG_UNUSED(transforms) /* Quiet compiler warnings */
  1225. PNG_UNUSED(params)
  1226. }
  1227. #endif /* PNG_INFO_IMAGE_SUPPORTED */
  1228. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  1229. #endif /* PNG_READ_SUPPORTED */
  1230. } // namespace PrivatePng