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.

795 lines
23KB

  1. /* png.c - location for general purpose libpng functions
  2. *
  3. * Last changed in libpng 1.2.21 [October 4, 2007]
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2007 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. */
  9. #define PNG_INTERNAL
  10. #define PNG_NO_EXTERN
  11. #include "png.h"
  12. /* Generate a compiler error if there is an old png.h in the search path. */
  13. typedef version_1_2_21 Your_png_h_is_not_version_1_2_21;
  14. /* Version information for C files. This had better match the version
  15. * string defined in png.h. */
  16. #ifdef PNG_USE_GLOBAL_ARRAYS
  17. /* png_libpng_ver was changed to a function in version 1.0.5c */
  18. PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
  19. #ifdef PNG_READ_SUPPORTED
  20. /* png_sig was changed to a function in version 1.0.5c */
  21. /* Place to hold the signature string for a PNG file. */
  22. PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  23. #endif /* PNG_READ_SUPPORTED */
  24. /* Invoke global declarations for constant strings for known chunk types */
  25. PNG_IHDR;
  26. PNG_IDAT;
  27. PNG_IEND;
  28. PNG_PLTE;
  29. PNG_bKGD;
  30. PNG_cHRM;
  31. PNG_gAMA;
  32. PNG_hIST;
  33. PNG_iCCP;
  34. PNG_iTXt;
  35. PNG_oFFs;
  36. PNG_pCAL;
  37. PNG_sCAL;
  38. PNG_pHYs;
  39. PNG_sBIT;
  40. PNG_sPLT;
  41. PNG_sRGB;
  42. PNG_tEXt;
  43. PNG_tIME;
  44. PNG_tRNS;
  45. PNG_zTXt;
  46. #ifdef PNG_READ_SUPPORTED
  47. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  48. /* start of interlace block */
  49. PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  50. /* offset to next interlace block */
  51. PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  52. /* start of interlace block in the y direction */
  53. PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  54. /* offset to next interlace block in the y direction */
  55. PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  56. /* Height of interlace block. This is not currently used - if you need
  57. * it, uncomment it here and in png.h
  58. PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  59. */
  60. /* Mask to determine which pixels are valid in a pass */
  61. PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  62. /* Mask to determine which pixels to overwrite while displaying */
  63. PNG_CONST int FARDATA png_pass_dsp_mask[]
  64. = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  65. #endif /* PNG_READ_SUPPORTED */
  66. #endif /* PNG_USE_GLOBAL_ARRAYS */
  67. /* Tells libpng that we have already handled the first "num_bytes" bytes
  68. * of the PNG file signature. If the PNG data is embedded into another
  69. * stream we can set num_bytes = 8 so that libpng will not attempt to read
  70. * or write any of the magic bytes before it starts on the IHDR.
  71. */
  72. #ifdef PNG_READ_SUPPORTED
  73. void PNGAPI
  74. png_set_sig_bytes(png_structp png_ptr, int num_bytes)
  75. {
  76. if(png_ptr == NULL) return;
  77. png_debug(1, "in png_set_sig_bytes\n");
  78. if (num_bytes > 8)
  79. png_error(png_ptr, "Too many bytes for PNG signature.");
  80. png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  81. }
  82. /* Checks whether the supplied bytes match the PNG signature. We allow
  83. * checking less than the full 8-byte signature so that those apps that
  84. * already read the first few bytes of a file to determine the file type
  85. * can simply check the remaining bytes for extra assurance. Returns
  86. * an integer less than, equal to, or greater than zero if sig is found,
  87. * respectively, to be less than, to match, or be greater than the correct
  88. * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  89. */
  90. int PNGAPI
  91. png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  92. {
  93. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  94. if (num_to_check > 8)
  95. num_to_check = 8;
  96. else if (num_to_check < 1)
  97. return (-1);
  98. if (start > 7)
  99. return (-1);
  100. if (start + num_to_check > 8)
  101. num_to_check = 8 - start;
  102. return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
  103. }
  104. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  105. /* (Obsolete) function to check signature bytes. It does not allow one
  106. * to check a partial signature. This function might be removed in the
  107. * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
  108. */
  109. int PNGAPI
  110. png_check_sig(png_bytep sig, int num)
  111. {
  112. return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
  113. }
  114. #endif
  115. #endif /* PNG_READ_SUPPORTED */
  116. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  117. /* Function to allocate memory for zlib and clear it to 0. */
  118. #ifdef PNG_1_0_X
  119. voidpf PNGAPI
  120. #else
  121. voidpf /* private */
  122. #endif
  123. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  124. {
  125. png_voidp ptr;
  126. png_structp p=(png_structp)png_ptr;
  127. png_uint_32 save_flags=p->flags;
  128. png_uint_32 num_bytes;
  129. if(png_ptr == NULL) return (NULL);
  130. if (items > PNG_UINT_32_MAX/size)
  131. {
  132. png_warning (p, "Potential overflow in png_zalloc()");
  133. return (NULL);
  134. }
  135. num_bytes = (png_uint_32)items * size;
  136. p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  137. ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
  138. p->flags=save_flags;
  139. #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
  140. if (ptr == NULL)
  141. return ((voidpf)ptr);
  142. if (num_bytes > (png_uint_32)0x8000L)
  143. {
  144. png_memset(ptr, 0, (png_size_t)0x8000L);
  145. png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  146. (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  147. }
  148. else
  149. {
  150. png_memset(ptr, 0, (png_size_t)num_bytes);
  151. }
  152. #endif
  153. return ((voidpf)ptr);
  154. }
  155. /* function to free memory for zlib */
  156. #ifdef PNG_1_0_X
  157. void PNGAPI
  158. #else
  159. void /* private */
  160. #endif
  161. png_zfree(voidpf png_ptr, voidpf ptr)
  162. {
  163. png_free((png_structp)png_ptr, (png_voidp)ptr);
  164. }
  165. /* Reset the CRC variable to 32 bits of 1's. Care must be taken
  166. * in case CRC is > 32 bits to leave the top bits 0.
  167. */
  168. void /* PRIVATE */
  169. png_reset_crc(png_structp png_ptr)
  170. {
  171. png_ptr->crc = crc32(0, Z_NULL, 0);
  172. }
  173. /* Calculate the CRC over a section of data. We can only pass as
  174. * much data to this routine as the largest single buffer size. We
  175. * also check that this data will actually be used before going to the
  176. * trouble of calculating it.
  177. */
  178. void /* PRIVATE */
  179. png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  180. {
  181. int need_crc = 1;
  182. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  183. {
  184. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  185. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  186. need_crc = 0;
  187. }
  188. else /* critical */
  189. {
  190. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  191. need_crc = 0;
  192. }
  193. if (need_crc)
  194. png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
  195. }
  196. /* Allocate the memory for an info_struct for the application. We don't
  197. * really need the png_ptr, but it could potentially be useful in the
  198. * future. This should be used in favour of malloc(png_sizeof(png_info))
  199. * and png_info_init() so that applications that want to use a shared
  200. * libpng don't have to be recompiled if png_info changes size.
  201. */
  202. png_infop PNGAPI
  203. png_create_info_struct(png_structp png_ptr)
  204. {
  205. png_infop info_ptr;
  206. png_debug(1, "in png_create_info_struct\n");
  207. if(png_ptr == NULL) return (NULL);
  208. #ifdef PNG_USER_MEM_SUPPORTED
  209. info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
  210. png_ptr->malloc_fn, png_ptr->mem_ptr);
  211. #else
  212. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  213. #endif
  214. if (info_ptr != NULL)
  215. png_info_init_3(&info_ptr, png_sizeof(png_info));
  216. return (info_ptr);
  217. }
  218. /* This function frees the memory associated with a single info struct.
  219. * Normally, one would use either png_destroy_read_struct() or
  220. * png_destroy_write_struct() to free an info struct, but this may be
  221. * useful for some applications.
  222. */
  223. void PNGAPI
  224. png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  225. {
  226. png_infop info_ptr = NULL;
  227. if(png_ptr == NULL) return;
  228. png_debug(1, "in png_destroy_info_struct\n");
  229. if (info_ptr_ptr != NULL)
  230. info_ptr = *info_ptr_ptr;
  231. if (info_ptr != NULL)
  232. {
  233. png_info_destroy(png_ptr, info_ptr);
  234. #ifdef PNG_USER_MEM_SUPPORTED
  235. png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
  236. png_ptr->mem_ptr);
  237. #else
  238. png_destroy_struct((png_voidp)info_ptr);
  239. #endif
  240. *info_ptr_ptr = NULL;
  241. }
  242. }
  243. /* Initialize the info structure. This is now an internal function (0.89)
  244. * and applications using it are urged to use png_create_info_struct()
  245. * instead.
  246. */
  247. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  248. #undef png_info_init
  249. void PNGAPI
  250. png_info_init(png_infop info_ptr)
  251. {
  252. /* We only come here via pre-1.0.12-compiled applications */
  253. png_info_init_3(&info_ptr, 0);
  254. }
  255. #endif
  256. void PNGAPI
  257. png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
  258. {
  259. png_infop info_ptr = *ptr_ptr;
  260. if(info_ptr == NULL) return;
  261. png_debug(1, "in png_info_init_3\n");
  262. if(png_sizeof(png_info) > png_info_struct_size)
  263. {
  264. png_destroy_struct(info_ptr);
  265. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  266. *ptr_ptr = info_ptr;
  267. }
  268. /* set everything to 0 */
  269. png_memset(info_ptr, 0, png_sizeof (png_info));
  270. }
  271. #ifdef PNG_FREE_ME_SUPPORTED
  272. void PNGAPI
  273. png_data_freer(png_structp png_ptr, png_infop info_ptr,
  274. int freer, png_uint_32 mask)
  275. {
  276. png_debug(1, "in png_data_freer\n");
  277. if (png_ptr == NULL || info_ptr == NULL)
  278. return;
  279. if(freer == PNG_DESTROY_WILL_FREE_DATA)
  280. info_ptr->free_me |= mask;
  281. else if(freer == PNG_USER_WILL_FREE_DATA)
  282. info_ptr->free_me &= ~mask;
  283. else
  284. png_warning(png_ptr,
  285. "Unknown freer parameter in png_data_freer.");
  286. }
  287. #endif
  288. void PNGAPI
  289. png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
  290. int num)
  291. {
  292. png_debug(1, "in png_free_data\n");
  293. if (png_ptr == NULL || info_ptr == NULL)
  294. return;
  295. #if defined(PNG_TEXT_SUPPORTED)
  296. /* free text item num or (if num == -1) all text items */
  297. #ifdef PNG_FREE_ME_SUPPORTED
  298. if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  299. #else
  300. if (mask & PNG_FREE_TEXT)
  301. #endif
  302. {
  303. if (num != -1)
  304. {
  305. if (info_ptr->text && info_ptr->text[num].key)
  306. {
  307. png_free(png_ptr, info_ptr->text[num].key);
  308. info_ptr->text[num].key = NULL;
  309. }
  310. }
  311. else
  312. {
  313. int i;
  314. for (i = 0; i < info_ptr->num_text; i++)
  315. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  316. png_free(png_ptr, info_ptr->text);
  317. info_ptr->text = NULL;
  318. info_ptr->num_text=0;
  319. }
  320. }
  321. #endif
  322. #if defined(PNG_tRNS_SUPPORTED)
  323. /* free any tRNS entry */
  324. #ifdef PNG_FREE_ME_SUPPORTED
  325. if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  326. #else
  327. if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
  328. #endif
  329. {
  330. png_free(png_ptr, info_ptr->trans);
  331. info_ptr->valid &= ~PNG_INFO_tRNS;
  332. #ifndef PNG_FREE_ME_SUPPORTED
  333. png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  334. #endif
  335. info_ptr->trans = NULL;
  336. }
  337. #endif
  338. #if defined(PNG_sCAL_SUPPORTED)
  339. /* free any sCAL entry */
  340. #ifdef PNG_FREE_ME_SUPPORTED
  341. if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  342. #else
  343. if (mask & PNG_FREE_SCAL)
  344. #endif
  345. {
  346. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  347. png_free(png_ptr, info_ptr->scal_s_width);
  348. png_free(png_ptr, info_ptr->scal_s_height);
  349. info_ptr->scal_s_width = NULL;
  350. info_ptr->scal_s_height = NULL;
  351. #endif
  352. info_ptr->valid &= ~PNG_INFO_sCAL;
  353. }
  354. #endif
  355. #if defined(PNG_pCAL_SUPPORTED)
  356. /* free any pCAL entry */
  357. #ifdef PNG_FREE_ME_SUPPORTED
  358. if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  359. #else
  360. if (mask & PNG_FREE_PCAL)
  361. #endif
  362. {
  363. png_free(png_ptr, info_ptr->pcal_purpose);
  364. png_free(png_ptr, info_ptr->pcal_units);
  365. info_ptr->pcal_purpose = NULL;
  366. info_ptr->pcal_units = NULL;
  367. if (info_ptr->pcal_params != NULL)
  368. {
  369. int i;
  370. for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
  371. {
  372. png_free(png_ptr, info_ptr->pcal_params[i]);
  373. info_ptr->pcal_params[i]=NULL;
  374. }
  375. png_free(png_ptr, info_ptr->pcal_params);
  376. info_ptr->pcal_params = NULL;
  377. }
  378. info_ptr->valid &= ~PNG_INFO_pCAL;
  379. }
  380. #endif
  381. #if defined(PNG_iCCP_SUPPORTED)
  382. /* free any iCCP entry */
  383. #ifdef PNG_FREE_ME_SUPPORTED
  384. if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  385. #else
  386. if (mask & PNG_FREE_ICCP)
  387. #endif
  388. {
  389. png_free(png_ptr, info_ptr->iccp_name);
  390. png_free(png_ptr, info_ptr->iccp_profile);
  391. info_ptr->iccp_name = NULL;
  392. info_ptr->iccp_profile = NULL;
  393. info_ptr->valid &= ~PNG_INFO_iCCP;
  394. }
  395. #endif
  396. #if defined(PNG_sPLT_SUPPORTED)
  397. /* free a given sPLT entry, or (if num == -1) all sPLT entries */
  398. #ifdef PNG_FREE_ME_SUPPORTED
  399. if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  400. #else
  401. if (mask & PNG_FREE_SPLT)
  402. #endif
  403. {
  404. if (num != -1)
  405. {
  406. if(info_ptr->splt_palettes)
  407. {
  408. png_free(png_ptr, info_ptr->splt_palettes[num].name);
  409. png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  410. info_ptr->splt_palettes[num].name = NULL;
  411. info_ptr->splt_palettes[num].entries = NULL;
  412. }
  413. }
  414. else
  415. {
  416. if(info_ptr->splt_palettes_num)
  417. {
  418. int i;
  419. for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  420. png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
  421. png_free(png_ptr, info_ptr->splt_palettes);
  422. info_ptr->splt_palettes = NULL;
  423. info_ptr->splt_palettes_num = 0;
  424. }
  425. info_ptr->valid &= ~PNG_INFO_sPLT;
  426. }
  427. }
  428. #endif
  429. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  430. if(png_ptr->unknown_chunk.data)
  431. {
  432. png_free(png_ptr, png_ptr->unknown_chunk.data);
  433. png_ptr->unknown_chunk.data = NULL;
  434. }
  435. #ifdef PNG_FREE_ME_SUPPORTED
  436. if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  437. #else
  438. if (mask & PNG_FREE_UNKN)
  439. #endif
  440. {
  441. if (num != -1)
  442. {
  443. if(info_ptr->unknown_chunks)
  444. {
  445. png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  446. info_ptr->unknown_chunks[num].data = NULL;
  447. }
  448. }
  449. else
  450. {
  451. int i;
  452. if(info_ptr->unknown_chunks_num)
  453. {
  454. for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
  455. png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
  456. png_free(png_ptr, info_ptr->unknown_chunks);
  457. info_ptr->unknown_chunks = NULL;
  458. info_ptr->unknown_chunks_num = 0;
  459. }
  460. }
  461. }
  462. #endif
  463. #if defined(PNG_hIST_SUPPORTED)
  464. /* free any hIST entry */
  465. #ifdef PNG_FREE_ME_SUPPORTED
  466. if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
  467. #else
  468. if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
  469. #endif
  470. {
  471. png_free(png_ptr, info_ptr->hist);
  472. info_ptr->hist = NULL;
  473. info_ptr->valid &= ~PNG_INFO_hIST;
  474. #ifndef PNG_FREE_ME_SUPPORTED
  475. png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  476. #endif
  477. }
  478. #endif
  479. /* free any PLTE entry that was internally allocated */
  480. #ifdef PNG_FREE_ME_SUPPORTED
  481. if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  482. #else
  483. if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
  484. #endif
  485. {
  486. png_zfree(png_ptr, info_ptr->palette);
  487. info_ptr->palette = NULL;
  488. info_ptr->valid &= ~PNG_INFO_PLTE;
  489. #ifndef PNG_FREE_ME_SUPPORTED
  490. png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  491. #endif
  492. info_ptr->num_palette = 0;
  493. }
  494. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  495. /* free any image bits attached to the info structure */
  496. #ifdef PNG_FREE_ME_SUPPORTED
  497. if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  498. #else
  499. if (mask & PNG_FREE_ROWS)
  500. #endif
  501. {
  502. if(info_ptr->row_pointers)
  503. {
  504. int row;
  505. for (row = 0; row < (int)info_ptr->height; row++)
  506. {
  507. png_free(png_ptr, info_ptr->row_pointers[row]);
  508. info_ptr->row_pointers[row]=NULL;
  509. }
  510. png_free(png_ptr, info_ptr->row_pointers);
  511. info_ptr->row_pointers=NULL;
  512. }
  513. info_ptr->valid &= ~PNG_INFO_IDAT;
  514. }
  515. #endif
  516. #ifdef PNG_FREE_ME_SUPPORTED
  517. if(num == -1)
  518. info_ptr->free_me &= ~mask;
  519. else
  520. info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
  521. #endif
  522. }
  523. /* This is an internal routine to free any memory that the info struct is
  524. * pointing to before re-using it or freeing the struct itself. Recall
  525. * that png_free() checks for NULL pointers for us.
  526. */
  527. void /* PRIVATE */
  528. png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  529. {
  530. png_debug(1, "in png_info_destroy\n");
  531. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  532. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  533. if (png_ptr->num_chunk_list)
  534. {
  535. png_free(png_ptr, png_ptr->chunk_list);
  536. png_ptr->chunk_list=NULL;
  537. png_ptr->num_chunk_list=0;
  538. }
  539. #endif
  540. png_info_init_3(&info_ptr, png_sizeof(png_info));
  541. }
  542. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  543. /* This function returns a pointer to the io_ptr associated with the user
  544. * functions. The application should free any memory associated with this
  545. * pointer before png_write_destroy() or png_read_destroy() are called.
  546. */
  547. png_voidp PNGAPI
  548. png_get_io_ptr(png_structp png_ptr)
  549. {
  550. if(png_ptr == NULL) return (NULL);
  551. return (png_ptr->io_ptr);
  552. }
  553. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  554. #if !defined(PNG_NO_STDIO)
  555. /* Initialize the default input/output functions for the PNG file. If you
  556. * use your own read or write routines, you can call either png_set_read_fn()
  557. * or png_set_write_fn() instead of png_init_io(). If you have defined
  558. * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
  559. * necessarily available.
  560. */
  561. void PNGAPI
  562. png_init_io(png_structp png_ptr, png_FILE_p fp)
  563. {
  564. png_debug(1, "in png_init_io\n");
  565. if(png_ptr == NULL) return;
  566. png_ptr->io_ptr = (png_voidp)fp;
  567. }
  568. #endif
  569. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  570. /* Convert the supplied time into an RFC 1123 string suitable for use in
  571. * a "Creation Time" or other text-based time string.
  572. */
  573. png_charp PNGAPI
  574. png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
  575. {
  576. static PNG_CONST char short_months[12][4] =
  577. {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  578. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  579. if(png_ptr == NULL) return (NULL);
  580. if (png_ptr->time_buffer == NULL)
  581. {
  582. png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
  583. png_sizeof(char)));
  584. }
  585. #if defined(_WIN32_WCE)
  586. {
  587. wchar_t time_buf[29];
  588. wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
  589. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  590. ptime->year, ptime->hour % 24, ptime->minute % 60,
  591. ptime->second % 61);
  592. WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
  593. NULL, NULL);
  594. }
  595. #else
  596. #ifdef USE_FAR_KEYWORD
  597. {
  598. char near_time_buf[29];
  599. png_snprintf6(near_time_buf,29,"%d %s %d %02d:%02d:%02d +0000",
  600. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  601. ptime->year, ptime->hour % 24, ptime->minute % 60,
  602. ptime->second % 61);
  603. png_memcpy(png_ptr->time_buffer, near_time_buf,
  604. 29*png_sizeof(char));
  605. }
  606. #else
  607. png_snprintf6(png_ptr->time_buffer,29,"%d %s %d %02d:%02d:%02d +0000",
  608. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  609. ptime->year, ptime->hour % 24, ptime->minute % 60,
  610. ptime->second % 61);
  611. #endif
  612. #endif /* _WIN32_WCE */
  613. return ((png_charp)png_ptr->time_buffer);
  614. }
  615. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  616. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  617. png_charp PNGAPI
  618. png_get_copyright(png_structp)
  619. {
  620. return ((png_charp) "\n libpng version 1.2.21 - October 4, 2007\n\
  621. Copyright (c) 1998-2007 Glenn Randers-Pehrson\n\
  622. Copyright (c) 1996-1997 Andreas Dilger\n\
  623. Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
  624. }
  625. /* The following return the library version as a short string in the
  626. * format 1.0.0 through 99.99.99zz. To get the version of *.h files
  627. * used with your application, print out PNG_LIBPNG_VER_STRING, which
  628. * is defined in png.h.
  629. * Note: now there is no difference between png_get_libpng_ver() and
  630. * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
  631. * it is guaranteed that png.c uses the correct version of png.h.
  632. */
  633. png_charp PNGAPI
  634. png_get_libpng_ver(png_structp)
  635. {
  636. /* Version of *.c files used when building libpng */
  637. return ((png_charp) PNG_LIBPNG_VER_STRING);
  638. }
  639. png_charp PNGAPI
  640. png_get_header_ver(png_structp)
  641. {
  642. /* Version of *.h files used when building libpng */
  643. return ((png_charp) PNG_LIBPNG_VER_STRING);
  644. }
  645. png_charp PNGAPI
  646. png_get_header_version(png_structp)
  647. {
  648. /* Returns longer string containing both version and date */
  649. return ((png_charp) PNG_HEADER_VERSION_STRING
  650. #ifndef PNG_READ_SUPPORTED
  651. " (NO READ SUPPORT)"
  652. #endif
  653. "\n");
  654. }
  655. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  656. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  657. int PNGAPI
  658. png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
  659. {
  660. /* check chunk_name and return "keep" value if it's on the list, else 0 */
  661. int i;
  662. png_bytep p;
  663. if(png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
  664. return 0;
  665. p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
  666. for (i = png_ptr->num_chunk_list; i; i--, p-=5)
  667. if (!png_memcmp(chunk_name, p, 4))
  668. return ((int)*(p+4));
  669. return 0;
  670. }
  671. #endif
  672. /* This function, added to libpng-1.0.6g, is untested. */
  673. int PNGAPI
  674. png_reset_zstream(png_structp png_ptr)
  675. {
  676. if (png_ptr == NULL) return Z_STREAM_ERROR;
  677. return (inflateReset(&png_ptr->zstream));
  678. }
  679. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  680. /* This function was added to libpng-1.0.7 */
  681. png_uint_32 PNGAPI
  682. png_access_version_number(void)
  683. {
  684. /* Version of *.c files used when building libpng */
  685. return((png_uint_32) PNG_LIBPNG_VER);
  686. }
  687. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
  688. #if !defined(PNG_1_0_X)
  689. /* this function was added to libpng 1.2.0 */
  690. int PNGAPI
  691. png_mmx_support(void)
  692. {
  693. /* obsolete, to be removed from libpng-1.4.0 */
  694. return -1;
  695. }
  696. #endif /* PNG_1_0_X */
  697. #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
  698. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  699. #ifdef PNG_SIZE_T
  700. /* Added at libpng version 1.2.6 */
  701. PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
  702. png_size_t PNGAPI
  703. png_convert_size(size_t size)
  704. {
  705. if (size > (png_size_t)-1)
  706. PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
  707. return ((png_size_t)size);
  708. }
  709. #endif /* PNG_SIZE_T */
  710. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */