Counter Strike : Global Offensive Source Code
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.

1473 lines
42 KiB

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