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.

1473 lines
48KB

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