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.

1244 lines
39KB

  1. /* pngset.c - storage of image information into info struct
  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. * The functions here are used during reads to store data from the file
  10. * into the info struct, and during writes to store application data
  11. * into the info struct for writing into the file. This abstracts the
  12. * info struct and allows us to change the structure in the future.
  13. */
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  17. #if defined(PNG_bKGD_SUPPORTED)
  18. void PNGAPI
  19. png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
  20. {
  21. png_debug1(1, "in %s storage function\n", "bKGD");
  22. if (png_ptr == NULL || info_ptr == NULL)
  23. return;
  24. png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
  25. info_ptr->valid |= PNG_INFO_bKGD;
  26. }
  27. #endif
  28. #if defined(PNG_cHRM_SUPPORTED)
  29. #ifdef PNG_FLOATING_POINT_SUPPORTED
  30. void PNGAPI
  31. png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
  32. double white_x, double white_y, double red_x, double red_y,
  33. double green_x, double green_y, double blue_x, double blue_y)
  34. {
  35. png_debug1(1, "in %s storage function\n", "cHRM");
  36. if (png_ptr == NULL || info_ptr == NULL)
  37. return;
  38. if (white_x < 0.0 || white_y < 0.0 ||
  39. red_x < 0.0 || red_y < 0.0 ||
  40. green_x < 0.0 || green_y < 0.0 ||
  41. blue_x < 0.0 || blue_y < 0.0)
  42. {
  43. png_warning(png_ptr,
  44. "Ignoring attempt to set negative chromaticity value");
  45. return;
  46. }
  47. if (white_x > 21474.83 || white_y > 21474.83 ||
  48. red_x > 21474.83 || red_y > 21474.83 ||
  49. green_x > 21474.83 || green_y > 21474.83 ||
  50. blue_x > 21474.83 || blue_y > 21474.83)
  51. {
  52. png_warning(png_ptr,
  53. "Ignoring attempt to set chromaticity value exceeding 21474.83");
  54. return;
  55. }
  56. info_ptr->x_white = (float)white_x;
  57. info_ptr->y_white = (float)white_y;
  58. info_ptr->x_red = (float)red_x;
  59. info_ptr->y_red = (float)red_y;
  60. info_ptr->x_green = (float)green_x;
  61. info_ptr->y_green = (float)green_y;
  62. info_ptr->x_blue = (float)blue_x;
  63. info_ptr->y_blue = (float)blue_y;
  64. #ifdef PNG_FIXED_POINT_SUPPORTED
  65. info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
  66. info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
  67. info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5);
  68. info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5);
  69. info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
  70. info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
  71. info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5);
  72. info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5);
  73. #endif
  74. info_ptr->valid |= PNG_INFO_cHRM;
  75. }
  76. #endif
  77. #ifdef PNG_FIXED_POINT_SUPPORTED
  78. void PNGAPI
  79. png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  80. png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  81. png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  82. png_fixed_point blue_x, png_fixed_point blue_y)
  83. {
  84. png_debug1(1, "in %s storage function\n", "cHRM");
  85. if (png_ptr == NULL || info_ptr == NULL)
  86. return;
  87. if (white_x < 0 || white_y < 0 ||
  88. red_x < 0 || red_y < 0 ||
  89. green_x < 0 || green_y < 0 ||
  90. blue_x < 0 || blue_y < 0)
  91. {
  92. png_warning(png_ptr,
  93. "Ignoring attempt to set negative chromaticity value");
  94. return;
  95. }
  96. #ifdef PNG_FLOATING_POINT_SUPPORTED
  97. if (white_x > (double) PNG_UINT_31_MAX ||
  98. white_y > (double) PNG_UINT_31_MAX ||
  99. red_x > (double) PNG_UINT_31_MAX ||
  100. red_y > (double) PNG_UINT_31_MAX ||
  101. green_x > (double) PNG_UINT_31_MAX ||
  102. green_y > (double) PNG_UINT_31_MAX ||
  103. blue_x > (double) PNG_UINT_31_MAX ||
  104. blue_y > (double) PNG_UINT_31_MAX)
  105. #else
  106. if (white_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  107. white_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  108. red_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  109. red_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  110. green_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  111. green_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  112. blue_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
  113. blue_y > (png_fixed_point) PNG_UINT_31_MAX/100000L)
  114. #endif
  115. {
  116. png_warning(png_ptr,
  117. "Ignoring attempt to set chromaticity value exceeding 21474.83");
  118. return;
  119. }
  120. info_ptr->int_x_white = white_x;
  121. info_ptr->int_y_white = white_y;
  122. info_ptr->int_x_red = red_x;
  123. info_ptr->int_y_red = red_y;
  124. info_ptr->int_x_green = green_x;
  125. info_ptr->int_y_green = green_y;
  126. info_ptr->int_x_blue = blue_x;
  127. info_ptr->int_y_blue = blue_y;
  128. #ifdef PNG_FLOATING_POINT_SUPPORTED
  129. info_ptr->x_white = (float)(white_x/100000.);
  130. info_ptr->y_white = (float)(white_y/100000.);
  131. info_ptr->x_red = (float)( red_x/100000.);
  132. info_ptr->y_red = (float)( red_y/100000.);
  133. info_ptr->x_green = (float)(green_x/100000.);
  134. info_ptr->y_green = (float)(green_y/100000.);
  135. info_ptr->x_blue = (float)( blue_x/100000.);
  136. info_ptr->y_blue = (float)( blue_y/100000.);
  137. #endif
  138. info_ptr->valid |= PNG_INFO_cHRM;
  139. }
  140. #endif
  141. #endif
  142. #if defined(PNG_gAMA_SUPPORTED)
  143. #ifdef PNG_FLOATING_POINT_SUPPORTED
  144. void PNGAPI
  145. png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  146. {
  147. double gamma;
  148. png_debug1(1, "in %s storage function\n", "gAMA");
  149. if (png_ptr == NULL || info_ptr == NULL)
  150. return;
  151. /* Check for overflow */
  152. if (file_gamma > 21474.83)
  153. {
  154. png_warning(png_ptr, "Limiting gamma to 21474.83");
  155. gamma=21474.83;
  156. }
  157. else
  158. gamma=file_gamma;
  159. info_ptr->gamma = (float)gamma;
  160. #ifdef PNG_FIXED_POINT_SUPPORTED
  161. info_ptr->int_gamma = (int)(gamma*100000.+.5);
  162. #endif
  163. info_ptr->valid |= PNG_INFO_gAMA;
  164. if(gamma == 0.0)
  165. png_warning(png_ptr, "Setting gamma=0");
  166. }
  167. #endif
  168. void PNGAPI
  169. png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  170. int_gamma)
  171. {
  172. png_fixed_point gamma;
  173. png_debug1(1, "in %s storage function\n", "gAMA");
  174. if (png_ptr == NULL || info_ptr == NULL)
  175. return;
  176. if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
  177. {
  178. png_warning(png_ptr, "Limiting gamma to 21474.83");
  179. gamma=PNG_UINT_31_MAX;
  180. }
  181. else
  182. {
  183. if (int_gamma < 0)
  184. {
  185. png_warning(png_ptr, "Setting negative gamma to zero");
  186. gamma=0;
  187. }
  188. else
  189. gamma=int_gamma;
  190. }
  191. #ifdef PNG_FLOATING_POINT_SUPPORTED
  192. info_ptr->gamma = (float)(gamma/100000.);
  193. #endif
  194. #ifdef PNG_FIXED_POINT_SUPPORTED
  195. info_ptr->int_gamma = gamma;
  196. #endif
  197. info_ptr->valid |= PNG_INFO_gAMA;
  198. if(gamma == 0)
  199. png_warning(png_ptr, "Setting gamma=0");
  200. }
  201. #endif
  202. #if defined(PNG_hIST_SUPPORTED)
  203. void PNGAPI
  204. png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
  205. {
  206. int i;
  207. png_debug1(1, "in %s storage function\n", "hIST");
  208. if (png_ptr == NULL || info_ptr == NULL)
  209. return;
  210. if (info_ptr->num_palette == 0 || info_ptr->num_palette
  211. > PNG_MAX_PALETTE_LENGTH)
  212. {
  213. png_warning(png_ptr,
  214. "Invalid palette size, hIST allocation skipped.");
  215. return;
  216. }
  217. #ifdef PNG_FREE_ME_SUPPORTED
  218. png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  219. #endif
  220. /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
  221. 1.2.1 */
  222. png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  223. (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof (png_uint_16)));
  224. if (png_ptr->hist == NULL)
  225. {
  226. png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
  227. return;
  228. }
  229. for (i = 0; i < info_ptr->num_palette; i++)
  230. png_ptr->hist[i] = hist[i];
  231. info_ptr->hist = png_ptr->hist;
  232. info_ptr->valid |= PNG_INFO_hIST;
  233. #ifdef PNG_FREE_ME_SUPPORTED
  234. info_ptr->free_me |= PNG_FREE_HIST;
  235. #else
  236. png_ptr->flags |= PNG_FLAG_FREE_HIST;
  237. #endif
  238. }
  239. #endif
  240. void PNGAPI
  241. png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  242. png_uint_32 width, png_uint_32 height, int bit_depth,
  243. int color_type, int interlace_type, int compression_type,
  244. int filter_type)
  245. {
  246. png_debug1(1, "in %s storage function\n", "IHDR");
  247. if (png_ptr == NULL || info_ptr == NULL)
  248. return;
  249. /* check for width and height valid values */
  250. if (width == 0 || height == 0)
  251. png_error(png_ptr, "Image width or height is zero in IHDR");
  252. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  253. if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
  254. png_error(png_ptr, "image size exceeds user limits in IHDR");
  255. #else
  256. if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
  257. png_error(png_ptr, "image size exceeds user limits in IHDR");
  258. #endif
  259. if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
  260. png_error(png_ptr, "Invalid image size in IHDR");
  261. if ( width > (PNG_UINT_32_MAX
  262. >> 3) /* 8-byte RGBA pixels */
  263. - 64 /* bigrowbuf hack */
  264. - 1 /* filter byte */
  265. - 7*8 /* rounding of width to multiple of 8 pixels */
  266. - 8) /* extra max_pixel_depth pad */
  267. png_warning(png_ptr, "Width is too large for libpng to process pixels");
  268. /* check other values */
  269. if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  270. bit_depth != 8 && bit_depth != 16)
  271. png_error(png_ptr, "Invalid bit depth in IHDR");
  272. if (color_type < 0 || color_type == 1 ||
  273. color_type == 5 || color_type > 6)
  274. png_error(png_ptr, "Invalid color type in IHDR");
  275. if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  276. ((color_type == PNG_COLOR_TYPE_RGB ||
  277. color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  278. color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  279. png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
  280. if (interlace_type >= PNG_INTERLACE_LAST)
  281. png_error(png_ptr, "Unknown interlace method in IHDR");
  282. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  283. png_error(png_ptr, "Unknown compression method in IHDR");
  284. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  285. /* Accept filter_method 64 (intrapixel differencing) only if
  286. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  287. * 2. Libpng did not read a PNG signature (this filter_method is only
  288. * used in PNG datastreams that are embedded in MNG datastreams) and
  289. * 3. The application called png_permit_mng_features with a mask that
  290. * included PNG_FLAG_MNG_FILTER_64 and
  291. * 4. The filter_method is 64 and
  292. * 5. The color_type is RGB or RGBA
  293. */
  294. if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
  295. png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
  296. if(filter_type != PNG_FILTER_TYPE_BASE)
  297. {
  298. if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  299. (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  300. ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  301. (color_type == PNG_COLOR_TYPE_RGB ||
  302. color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  303. png_error(png_ptr, "Unknown filter method in IHDR");
  304. if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
  305. png_warning(png_ptr, "Invalid filter method in IHDR");
  306. }
  307. #else
  308. if(filter_type != PNG_FILTER_TYPE_BASE)
  309. png_error(png_ptr, "Unknown filter method in IHDR");
  310. #endif
  311. info_ptr->width = width;
  312. info_ptr->height = height;
  313. info_ptr->bit_depth = (png_byte)bit_depth;
  314. info_ptr->color_type =(png_byte) color_type;
  315. info_ptr->compression_type = (png_byte)compression_type;
  316. info_ptr->filter_type = (png_byte)filter_type;
  317. info_ptr->interlace_type = (png_byte)interlace_type;
  318. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  319. info_ptr->channels = 1;
  320. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  321. info_ptr->channels = 3;
  322. else
  323. info_ptr->channels = 1;
  324. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  325. info_ptr->channels++;
  326. info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  327. /* check for potential overflow */
  328. if (width > (PNG_UINT_32_MAX
  329. >> 3) /* 8-byte RGBA pixels */
  330. - 64 /* bigrowbuf hack */
  331. - 1 /* filter byte */
  332. - 7*8 /* rounding of width to multiple of 8 pixels */
  333. - 8) /* extra max_pixel_depth pad */
  334. info_ptr->rowbytes = (png_size_t)0;
  335. else
  336. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,width);
  337. }
  338. #if defined(PNG_oFFs_SUPPORTED)
  339. void PNGAPI
  340. png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  341. png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  342. {
  343. png_debug1(1, "in %s storage function\n", "oFFs");
  344. if (png_ptr == NULL || info_ptr == NULL)
  345. return;
  346. info_ptr->x_offset = offset_x;
  347. info_ptr->y_offset = offset_y;
  348. info_ptr->offset_unit_type = (png_byte)unit_type;
  349. info_ptr->valid |= PNG_INFO_oFFs;
  350. }
  351. #endif
  352. #if defined(PNG_pCAL_SUPPORTED)
  353. void PNGAPI
  354. png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  355. png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
  356. png_charp units, png_charpp params)
  357. {
  358. png_uint_32 length;
  359. int i;
  360. png_debug1(1, "in %s storage function\n", "pCAL");
  361. if (png_ptr == NULL || info_ptr == NULL)
  362. return;
  363. length = png_strlen(purpose) + 1;
  364. png_debug1(3, "allocating purpose for info (%lu bytes)\n", length);
  365. info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  366. if (info_ptr->pcal_purpose == NULL)
  367. {
  368. png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
  369. return;
  370. }
  371. png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
  372. png_debug(3, "storing X0, X1, type, and nparams in info\n");
  373. info_ptr->pcal_X0 = X0;
  374. info_ptr->pcal_X1 = X1;
  375. info_ptr->pcal_type = (png_byte)type;
  376. info_ptr->pcal_nparams = (png_byte)nparams;
  377. length = png_strlen(units) + 1;
  378. png_debug1(3, "allocating units for info (%lu bytes)\n", length);
  379. info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  380. if (info_ptr->pcal_units == NULL)
  381. {
  382. png_warning(png_ptr, "Insufficient memory for pCAL units.");
  383. return;
  384. }
  385. png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
  386. info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  387. (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
  388. if (info_ptr->pcal_params == NULL)
  389. {
  390. png_warning(png_ptr, "Insufficient memory for pCAL params.");
  391. return;
  392. }
  393. info_ptr->pcal_params[nparams] = NULL;
  394. for (i = 0; i < nparams; i++)
  395. {
  396. length = png_strlen(params[i]) + 1;
  397. png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length);
  398. info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  399. if (info_ptr->pcal_params[i] == NULL)
  400. {
  401. png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
  402. return;
  403. }
  404. png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
  405. }
  406. info_ptr->valid |= PNG_INFO_pCAL;
  407. #ifdef PNG_FREE_ME_SUPPORTED
  408. info_ptr->free_me |= PNG_FREE_PCAL;
  409. #endif
  410. }
  411. #endif
  412. #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
  413. #ifdef PNG_FLOATING_POINT_SUPPORTED
  414. void PNGAPI
  415. png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
  416. int unit, double width, double height)
  417. {
  418. png_debug1(1, "in %s storage function\n", "sCAL");
  419. if (png_ptr == NULL || info_ptr == NULL)
  420. return;
  421. info_ptr->scal_unit = (png_byte)unit;
  422. info_ptr->scal_pixel_width = width;
  423. info_ptr->scal_pixel_height = height;
  424. info_ptr->valid |= PNG_INFO_sCAL;
  425. }
  426. #else
  427. #ifdef PNG_FIXED_POINT_SUPPORTED
  428. void PNGAPI
  429. png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  430. int unit, png_charp swidth, png_charp sheight)
  431. {
  432. png_uint_32 length;
  433. png_debug1(1, "in %s storage function\n", "sCAL");
  434. if (png_ptr == NULL || info_ptr == NULL)
  435. return;
  436. info_ptr->scal_unit = (png_byte)unit;
  437. length = png_strlen(swidth) + 1;
  438. png_debug1(3, "allocating unit for info (%d bytes)\n", length);
  439. info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
  440. if (info_ptr->scal_s_width == NULL)
  441. {
  442. png_warning(png_ptr,
  443. "Memory allocation failed while processing sCAL.");
  444. }
  445. png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
  446. length = png_strlen(sheight) + 1;
  447. png_debug1(3, "allocating unit for info (%d bytes)\n", length);
  448. info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
  449. if (info_ptr->scal_s_height == NULL)
  450. {
  451. png_free (png_ptr, info_ptr->scal_s_width);
  452. png_warning(png_ptr,
  453. "Memory allocation failed while processing sCAL.");
  454. }
  455. png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
  456. info_ptr->valid |= PNG_INFO_sCAL;
  457. #ifdef PNG_FREE_ME_SUPPORTED
  458. info_ptr->free_me |= PNG_FREE_SCAL;
  459. #endif
  460. }
  461. #endif
  462. #endif
  463. #endif
  464. #if defined(PNG_pHYs_SUPPORTED)
  465. void PNGAPI
  466. png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  467. png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  468. {
  469. png_debug1(1, "in %s storage function\n", "pHYs");
  470. if (png_ptr == NULL || info_ptr == NULL)
  471. return;
  472. info_ptr->x_pixels_per_unit = res_x;
  473. info_ptr->y_pixels_per_unit = res_y;
  474. info_ptr->phys_unit_type = (png_byte)unit_type;
  475. info_ptr->valid |= PNG_INFO_pHYs;
  476. }
  477. #endif
  478. void PNGAPI
  479. png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  480. png_colorp palette, int num_palette)
  481. {
  482. png_debug1(1, "in %s storage function\n", "PLTE");
  483. if (png_ptr == NULL || info_ptr == NULL)
  484. return;
  485. if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  486. {
  487. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  488. png_error(png_ptr, "Invalid palette length");
  489. else
  490. {
  491. png_warning(png_ptr, "Invalid palette length");
  492. return;
  493. }
  494. }
  495. /*
  496. * It may not actually be necessary to set png_ptr->palette here;
  497. * we do it for backward compatibility with the way the png_handle_tRNS
  498. * function used to do the allocation.
  499. */
  500. #ifdef PNG_FREE_ME_SUPPORTED
  501. png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  502. #endif
  503. /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  504. of num_palette entries,
  505. in case of an invalid PNG file that has too-large sample values. */
  506. png_ptr->palette = (png_colorp)png_malloc(png_ptr,
  507. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  508. png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
  509. png_sizeof(png_color));
  510. png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof (png_color));
  511. info_ptr->palette = png_ptr->palette;
  512. info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  513. #ifdef PNG_FREE_ME_SUPPORTED
  514. info_ptr->free_me |= PNG_FREE_PLTE;
  515. #else
  516. png_ptr->flags |= PNG_FLAG_FREE_PLTE;
  517. #endif
  518. info_ptr->valid |= PNG_INFO_PLTE;
  519. }
  520. #if defined(PNG_sBIT_SUPPORTED)
  521. void PNGAPI
  522. png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  523. png_color_8p sig_bit)
  524. {
  525. png_debug1(1, "in %s storage function\n", "sBIT");
  526. if (png_ptr == NULL || info_ptr == NULL)
  527. return;
  528. png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof (png_color_8));
  529. info_ptr->valid |= PNG_INFO_sBIT;
  530. }
  531. #endif
  532. #if defined(PNG_sRGB_SUPPORTED)
  533. void PNGAPI
  534. png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
  535. {
  536. png_debug1(1, "in %s storage function\n", "sRGB");
  537. if (png_ptr == NULL || info_ptr == NULL)
  538. return;
  539. info_ptr->srgb_intent = (png_byte)intent;
  540. info_ptr->valid |= PNG_INFO_sRGB;
  541. }
  542. void PNGAPI
  543. png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  544. int intent)
  545. {
  546. #if defined(PNG_gAMA_SUPPORTED)
  547. #ifdef PNG_FLOATING_POINT_SUPPORTED
  548. float file_gamma;
  549. #endif
  550. #ifdef PNG_FIXED_POINT_SUPPORTED
  551. png_fixed_point int_file_gamma;
  552. #endif
  553. #endif
  554. #if defined(PNG_cHRM_SUPPORTED)
  555. #ifdef PNG_FLOATING_POINT_SUPPORTED
  556. float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  557. #endif
  558. #ifdef PNG_FIXED_POINT_SUPPORTED
  559. png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  560. int_green_y, int_blue_x, int_blue_y;
  561. #endif
  562. #endif
  563. png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
  564. if (png_ptr == NULL || info_ptr == NULL)
  565. return;
  566. png_set_sRGB(png_ptr, info_ptr, intent);
  567. #if defined(PNG_gAMA_SUPPORTED)
  568. #ifdef PNG_FLOATING_POINT_SUPPORTED
  569. file_gamma = (float).45455;
  570. png_set_gAMA(png_ptr, info_ptr, file_gamma);
  571. #endif
  572. #ifdef PNG_FIXED_POINT_SUPPORTED
  573. int_file_gamma = 45455L;
  574. png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
  575. #endif
  576. #endif
  577. #if defined(PNG_cHRM_SUPPORTED)
  578. #ifdef PNG_FIXED_POINT_SUPPORTED
  579. int_white_x = 31270L;
  580. int_white_y = 32900L;
  581. int_red_x = 64000L;
  582. int_red_y = 33000L;
  583. int_green_x = 30000L;
  584. int_green_y = 60000L;
  585. int_blue_x = 15000L;
  586. int_blue_y = 6000L;
  587. png_set_cHRM_fixed(png_ptr, info_ptr,
  588. int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
  589. int_blue_x, int_blue_y);
  590. #endif
  591. #ifdef PNG_FLOATING_POINT_SUPPORTED
  592. white_x = (float).3127;
  593. white_y = (float).3290;
  594. red_x = (float).64;
  595. red_y = (float).33;
  596. green_x = (float).30;
  597. green_y = (float).60;
  598. blue_x = (float).15;
  599. blue_y = (float).06;
  600. png_set_cHRM(png_ptr, info_ptr,
  601. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  602. #endif
  603. #endif
  604. }
  605. #endif
  606. #if defined(PNG_iCCP_SUPPORTED)
  607. void PNGAPI
  608. png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  609. png_charp name, int compression_type,
  610. png_charp profile, png_uint_32 proflen)
  611. {
  612. png_charp new_iccp_name;
  613. png_charp new_iccp_profile;
  614. png_debug1(1, "in %s storage function\n", "iCCP");
  615. if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  616. return;
  617. new_iccp_name = (png_charp)png_malloc_warn(png_ptr, png_strlen(name)+1);
  618. if (new_iccp_name == NULL)
  619. {
  620. png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
  621. return;
  622. }
  623. png_strncpy(new_iccp_name, name, png_strlen(name)+1);
  624. new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
  625. if (new_iccp_profile == NULL)
  626. {
  627. png_free (png_ptr, new_iccp_name);
  628. png_warning(png_ptr, "Insufficient memory to process iCCP profile.");
  629. return;
  630. }
  631. png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  632. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  633. info_ptr->iccp_proflen = proflen;
  634. info_ptr->iccp_name = new_iccp_name;
  635. info_ptr->iccp_profile = new_iccp_profile;
  636. /* Compression is always zero but is here so the API and info structure
  637. * does not have to change if we introduce multiple compression types */
  638. info_ptr->iccp_compression = (png_byte)compression_type;
  639. #ifdef PNG_FREE_ME_SUPPORTED
  640. info_ptr->free_me |= PNG_FREE_ICCP;
  641. #endif
  642. info_ptr->valid |= PNG_INFO_iCCP;
  643. }
  644. #endif
  645. #if defined(PNG_TEXT_SUPPORTED)
  646. void PNGAPI
  647. png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  648. int num_text)
  649. {
  650. int ret;
  651. ret=png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  652. if (ret)
  653. png_error(png_ptr, "Insufficient memory to store text");
  654. }
  655. int /* PRIVATE */
  656. png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  657. int num_text)
  658. {
  659. int i;
  660. png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
  661. "text" : (png_const_charp)png_ptr->chunk_name));
  662. if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  663. return(0);
  664. /* Make sure we have enough space in the "text" array in info_struct
  665. * to hold all of the incoming text_ptr objects.
  666. */
  667. if (info_ptr->num_text + num_text > info_ptr->max_text)
  668. {
  669. if (info_ptr->text != NULL)
  670. {
  671. png_textp old_text;
  672. int old_max;
  673. old_max = info_ptr->max_text;
  674. info_ptr->max_text = info_ptr->num_text + num_text + 8;
  675. old_text = info_ptr->text;
  676. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  677. (png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
  678. if (info_ptr->text == NULL)
  679. {
  680. png_free(png_ptr, old_text);
  681. return(1);
  682. }
  683. png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
  684. png_sizeof(png_text)));
  685. png_free(png_ptr, old_text);
  686. }
  687. else
  688. {
  689. info_ptr->max_text = num_text + 8;
  690. info_ptr->num_text = 0;
  691. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  692. (png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
  693. if (info_ptr->text == NULL)
  694. return(1);
  695. #ifdef PNG_FREE_ME_SUPPORTED
  696. info_ptr->free_me |= PNG_FREE_TEXT;
  697. #endif
  698. }
  699. png_debug1(3, "allocated %d entries for info_ptr->text\n",
  700. info_ptr->max_text);
  701. }
  702. for (i = 0; i < num_text; i++)
  703. {
  704. png_size_t text_length,key_len;
  705. png_size_t lang_len,lang_key_len;
  706. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  707. if (text_ptr[i].key == NULL)
  708. continue;
  709. key_len = png_strlen(text_ptr[i].key);
  710. if(text_ptr[i].compression <= 0)
  711. {
  712. lang_len = 0;
  713. lang_key_len = 0;
  714. }
  715. else
  716. #ifdef PNG_iTXt_SUPPORTED
  717. {
  718. /* set iTXt data */
  719. if (text_ptr[i].lang != NULL)
  720. lang_len = png_strlen(text_ptr[i].lang);
  721. else
  722. lang_len = 0;
  723. if (text_ptr[i].lang_key != NULL)
  724. lang_key_len = png_strlen(text_ptr[i].lang_key);
  725. else
  726. lang_key_len = 0;
  727. }
  728. #else
  729. {
  730. png_warning(png_ptr, "iTXt chunk not supported.");
  731. continue;
  732. }
  733. #endif
  734. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  735. {
  736. text_length = 0;
  737. #ifdef PNG_iTXt_SUPPORTED
  738. if(text_ptr[i].compression > 0)
  739. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  740. else
  741. #endif
  742. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  743. }
  744. else
  745. {
  746. text_length = png_strlen(text_ptr[i].text);
  747. textp->compression = text_ptr[i].compression;
  748. }
  749. textp->key = (png_charp)png_malloc_warn(png_ptr,
  750. (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4));
  751. if (textp->key == NULL)
  752. return(1);
  753. png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
  754. (png_uint_32)(key_len + lang_len + lang_key_len + text_length + 4),
  755. (int)textp->key);
  756. png_memcpy(textp->key, text_ptr[i].key,
  757. (png_size_t)(key_len));
  758. *(textp->key+key_len) = '\0';
  759. #ifdef PNG_iTXt_SUPPORTED
  760. if (text_ptr[i].compression > 0)
  761. {
  762. textp->lang=textp->key + key_len + 1;
  763. png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  764. *(textp->lang+lang_len) = '\0';
  765. textp->lang_key=textp->lang + lang_len + 1;
  766. png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  767. *(textp->lang_key+lang_key_len) = '\0';
  768. textp->text=textp->lang_key + lang_key_len + 1;
  769. }
  770. else
  771. #endif
  772. {
  773. #ifdef PNG_iTXt_SUPPORTED
  774. textp->lang=NULL;
  775. textp->lang_key=NULL;
  776. #endif
  777. textp->text=textp->key + key_len + 1;
  778. }
  779. if(text_length)
  780. png_memcpy(textp->text, text_ptr[i].text,
  781. (png_size_t)(text_length));
  782. *(textp->text+text_length) = '\0';
  783. #ifdef PNG_iTXt_SUPPORTED
  784. if(textp->compression > 0)
  785. {
  786. textp->text_length = 0;
  787. textp->itxt_length = text_length;
  788. }
  789. else
  790. #endif
  791. {
  792. textp->text_length = text_length;
  793. #ifdef PNG_iTXt_SUPPORTED
  794. textp->itxt_length = 0;
  795. #endif
  796. }
  797. info_ptr->num_text++;
  798. png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
  799. }
  800. return(0);
  801. }
  802. #endif
  803. #if defined(PNG_tIME_SUPPORTED)
  804. void PNGAPI
  805. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
  806. {
  807. png_debug1(1, "in %s storage function\n", "tIME");
  808. if (png_ptr == NULL || info_ptr == NULL ||
  809. (png_ptr->mode & PNG_WROTE_tIME))
  810. return;
  811. png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof (png_time));
  812. info_ptr->valid |= PNG_INFO_tIME;
  813. }
  814. #endif
  815. #if defined(PNG_tRNS_SUPPORTED)
  816. void PNGAPI
  817. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  818. png_bytep trans, int num_trans, png_color_16p trans_values)
  819. {
  820. png_debug1(1, "in %s storage function\n", "tRNS");
  821. if (png_ptr == NULL || info_ptr == NULL)
  822. return;
  823. if (trans != NULL)
  824. {
  825. /*
  826. * It may not actually be necessary to set png_ptr->trans here;
  827. * we do it for backward compatibility with the way the png_handle_tRNS
  828. * function used to do the allocation.
  829. */
  830. #ifdef PNG_FREE_ME_SUPPORTED
  831. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  832. #endif
  833. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  834. png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
  835. (png_uint_32)PNG_MAX_PALETTE_LENGTH);
  836. if (num_trans <= PNG_MAX_PALETTE_LENGTH)
  837. png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
  838. #ifdef PNG_FREE_ME_SUPPORTED
  839. info_ptr->free_me |= PNG_FREE_TRNS;
  840. #else
  841. png_ptr->flags |= PNG_FLAG_FREE_TRNS;
  842. #endif
  843. }
  844. if (trans_values != NULL)
  845. {
  846. png_memcpy(&(info_ptr->trans_values), trans_values,
  847. png_sizeof(png_color_16));
  848. if (num_trans == 0)
  849. num_trans = 1;
  850. }
  851. info_ptr->num_trans = (png_uint_16)num_trans;
  852. info_ptr->valid |= PNG_INFO_tRNS;
  853. }
  854. #endif
  855. #if defined(PNG_sPLT_SUPPORTED)
  856. void PNGAPI
  857. png_set_sPLT(png_structp png_ptr,
  858. png_infop info_ptr, png_sPLT_tp entries, int nentries)
  859. {
  860. png_sPLT_tp np;
  861. int i;
  862. if (png_ptr == NULL || info_ptr == NULL)
  863. return;
  864. np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  865. (info_ptr->splt_palettes_num + nentries) * png_sizeof(png_sPLT_t));
  866. if (np == NULL)
  867. {
  868. png_warning(png_ptr, "No memory for sPLT palettes.");
  869. return;
  870. }
  871. png_memcpy(np, info_ptr->splt_palettes,
  872. info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  873. png_free(png_ptr, info_ptr->splt_palettes);
  874. info_ptr->splt_palettes=NULL;
  875. for (i = 0; i < nentries; i++)
  876. {
  877. png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  878. png_sPLT_tp from = entries + i;
  879. to->name = (png_charp)png_malloc_warn(png_ptr,
  880. png_strlen(from->name) + 1);
  881. if (to->name == NULL)
  882. {
  883. png_warning(png_ptr,
  884. "Out of memory while processing sPLT chunk");
  885. }
  886. /* TODO: use png_malloc_warn */
  887. png_strncpy(to->name, from->name, png_strlen(from->name)+1);
  888. to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  889. from->nentries * png_sizeof(png_sPLT_entry));
  890. /* TODO: use png_malloc_warn */
  891. png_memcpy(to->entries, from->entries,
  892. from->nentries * png_sizeof(png_sPLT_entry));
  893. if (to->entries == NULL)
  894. {
  895. png_warning(png_ptr,
  896. "Out of memory while processing sPLT chunk");
  897. png_free(png_ptr,to->name);
  898. to->name = NULL;
  899. }
  900. to->nentries = from->nentries;
  901. to->depth = from->depth;
  902. }
  903. info_ptr->splt_palettes = np;
  904. info_ptr->splt_palettes_num += nentries;
  905. info_ptr->valid |= PNG_INFO_sPLT;
  906. #ifdef PNG_FREE_ME_SUPPORTED
  907. info_ptr->free_me |= PNG_FREE_SPLT;
  908. #endif
  909. }
  910. #endif /* PNG_sPLT_SUPPORTED */
  911. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  912. void PNGAPI
  913. png_set_unknown_chunks(png_structp png_ptr,
  914. png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
  915. {
  916. png_unknown_chunkp np;
  917. int i;
  918. if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  919. return;
  920. np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  921. (info_ptr->unknown_chunks_num + num_unknowns) *
  922. png_sizeof(png_unknown_chunk));
  923. if (np == NULL)
  924. {
  925. png_warning(png_ptr,
  926. "Out of memory while processing unknown chunk.");
  927. return;
  928. }
  929. png_memcpy(np, info_ptr->unknown_chunks,
  930. info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
  931. png_free(png_ptr, info_ptr->unknown_chunks);
  932. info_ptr->unknown_chunks=NULL;
  933. for (i = 0; i < num_unknowns; i++)
  934. {
  935. png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  936. png_unknown_chunkp from = unknowns + i;
  937. png_strncpy((png_charp)to->name, (png_charp)from->name, 5);
  938. to->data = (png_bytep)png_malloc_warn(png_ptr, from->size);
  939. if (to->data == NULL)
  940. {
  941. png_warning(png_ptr,
  942. "Out of memory while processing unknown chunk.");
  943. }
  944. else
  945. {
  946. png_memcpy(to->data, from->data, from->size);
  947. to->size = from->size;
  948. /* note our location in the read or write sequence */
  949. to->location = (png_byte)(png_ptr->mode & 0xff);
  950. }
  951. }
  952. info_ptr->unknown_chunks = np;
  953. info_ptr->unknown_chunks_num += num_unknowns;
  954. #ifdef PNG_FREE_ME_SUPPORTED
  955. info_ptr->free_me |= PNG_FREE_UNKN;
  956. #endif
  957. }
  958. void PNGAPI
  959. png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  960. int chunk, int location)
  961. {
  962. if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  963. (int)info_ptr->unknown_chunks_num)
  964. info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  965. }
  966. #endif
  967. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  968. #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
  969. defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
  970. void PNGAPI
  971. png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
  972. {
  973. /* This function is deprecated in favor of png_permit_mng_features()
  974. and will be removed from libpng-1.3.0 */
  975. png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
  976. if (png_ptr == NULL)
  977. return;
  978. png_ptr->mng_features_permitted = (png_byte)
  979. ((png_ptr->mng_features_permitted & (~(PNG_FLAG_MNG_EMPTY_PLTE))) |
  980. ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
  981. }
  982. #endif
  983. #endif
  984. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  985. png_uint_32 PNGAPI
  986. png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  987. {
  988. png_debug(1, "in png_permit_mng_features\n");
  989. if (png_ptr == NULL)
  990. return (png_uint_32)0;
  991. png_ptr->mng_features_permitted =
  992. (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  993. return (png_uint_32)png_ptr->mng_features_permitted;
  994. }
  995. #endif
  996. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  997. void PNGAPI
  998. png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
  999. chunk_list, int num_chunks)
  1000. {
  1001. png_bytep new_list, p;
  1002. int i, old_num_chunks;
  1003. if (png_ptr == NULL)
  1004. return;
  1005. if (num_chunks == 0)
  1006. {
  1007. if(keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  1008. png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1009. else
  1010. png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1011. if(keep == PNG_HANDLE_CHUNK_ALWAYS)
  1012. png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1013. else
  1014. png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1015. return;
  1016. }
  1017. if (chunk_list == NULL)
  1018. return;
  1019. old_num_chunks=png_ptr->num_chunk_list;
  1020. new_list=(png_bytep)png_malloc(png_ptr,
  1021. (png_uint_32)(5*(num_chunks+old_num_chunks)));
  1022. if(png_ptr->chunk_list != NULL)
  1023. {
  1024. png_memcpy(new_list, png_ptr->chunk_list,
  1025. (png_size_t)(5*old_num_chunks));
  1026. png_free(png_ptr, png_ptr->chunk_list);
  1027. png_ptr->chunk_list=NULL;
  1028. }
  1029. png_memcpy(new_list+5*old_num_chunks, chunk_list,
  1030. (png_size_t)(5*num_chunks));
  1031. for (p=new_list+5*old_num_chunks+4, i=0; i<num_chunks; i++, p+=5)
  1032. *p=(png_byte)keep;
  1033. png_ptr->num_chunk_list=old_num_chunks+num_chunks;
  1034. png_ptr->chunk_list=new_list;
  1035. #ifdef PNG_FREE_ME_SUPPORTED
  1036. png_ptr->free_me |= PNG_FREE_LIST;
  1037. #endif
  1038. }
  1039. #endif
  1040. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1041. void PNGAPI
  1042. png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  1043. png_user_chunk_ptr read_user_chunk_fn)
  1044. {
  1045. png_debug(1, "in png_set_read_user_chunk_fn\n");
  1046. if (png_ptr == NULL)
  1047. return;
  1048. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1049. png_ptr->user_chunk_ptr = user_chunk_ptr;
  1050. }
  1051. #endif
  1052. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  1053. void PNGAPI
  1054. png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  1055. {
  1056. png_debug1(1, "in %s storage function\n", "rows");
  1057. if (png_ptr == NULL || info_ptr == NULL)
  1058. return;
  1059. if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  1060. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1061. info_ptr->row_pointers = row_pointers;
  1062. if(row_pointers)
  1063. info_ptr->valid |= PNG_INFO_IDAT;
  1064. }
  1065. #endif
  1066. #ifdef PNG_WRITE_SUPPORTED
  1067. void PNGAPI
  1068. png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
  1069. {
  1070. if (png_ptr == NULL)
  1071. return;
  1072. if(png_ptr->zbuf)
  1073. png_free(png_ptr, png_ptr->zbuf);
  1074. png_ptr->zbuf_size = (png_size_t)size;
  1075. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1076. png_ptr->zstream.next_out = png_ptr->zbuf;
  1077. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1078. }
  1079. #endif
  1080. void PNGAPI
  1081. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1082. {
  1083. if (png_ptr && info_ptr)
  1084. info_ptr->valid &= ~(mask);
  1085. }
  1086. #ifndef PNG_1_0_X
  1087. #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  1088. /* function was added to libpng 1.2.0 and should always exist by default */
  1089. void PNGAPI
  1090. png_set_asm_flags (png_structp png_ptr, png_uint_32)
  1091. {
  1092. /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1093. if (png_ptr != NULL)
  1094. png_ptr->asm_flags = 0;
  1095. }
  1096. /* this function was added to libpng 1.2.0 */
  1097. void PNGAPI
  1098. png_set_mmx_thresholds (png_structp png_ptr,
  1099. png_byte,
  1100. png_uint_32)
  1101. {
  1102. /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1103. if (png_ptr == NULL)
  1104. return;
  1105. }
  1106. #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
  1107. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1108. /* this function was added to libpng 1.2.6 */
  1109. void PNGAPI
  1110. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1111. png_uint_32 user_height_max)
  1112. {
  1113. /* Images with dimensions larger than these limits will be
  1114. * rejected by png_set_IHDR(). To accept any PNG datastream
  1115. * regardless of dimensions, set both limits to 0x7ffffffL.
  1116. */
  1117. if(png_ptr == NULL) return;
  1118. png_ptr->user_width_max = user_width_max;
  1119. png_ptr->user_height_max = user_height_max;
  1120. }
  1121. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1122. #endif /* ?PNG_1_0_X */
  1123. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */