pngset.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Last changed in libpng 1.5.7 [December 15, 2011]
  4. * Copyright (c) 1998-2011 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * The functions here are used during reads to store data from the file
  13. * into the info struct, and during writes to store application data
  14. * into the info struct for writing into the file. This abstracts the
  15. * info struct and allows us to change the structure in the future.
  16. */
  17. #include "pngpriv.h"
  18. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  19. #ifdef PNG_bKGD_SUPPORTED
  20. void PNGAPI
  21. png_set_bKGD(png_structp png_ptr, png_infop info_ptr,
  22. png_const_color_16p background)
  23. {
  24. png_debug1(1, "in %s storage function", "bKGD");
  25. if (png_ptr == NULL || info_ptr == NULL)
  26. return;
  27. png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
  28. info_ptr->valid |= PNG_INFO_bKGD;
  29. }
  30. #endif
  31. #ifdef PNG_cHRM_SUPPORTED
  32. void PNGFAPI
  33. png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  34. png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  35. png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  36. png_fixed_point blue_x, png_fixed_point blue_y)
  37. {
  38. png_debug1(1, "in %s storage function", "cHRM fixed");
  39. if (png_ptr == NULL || info_ptr == NULL)
  40. return;
  41. # ifdef PNG_CHECK_cHRM_SUPPORTED
  42. if (png_check_cHRM_fixed(png_ptr,
  43. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
  44. # endif
  45. {
  46. info_ptr->x_white = white_x;
  47. info_ptr->y_white = white_y;
  48. info_ptr->x_red = red_x;
  49. info_ptr->y_red = red_y;
  50. info_ptr->x_green = green_x;
  51. info_ptr->y_green = green_y;
  52. info_ptr->x_blue = blue_x;
  53. info_ptr->y_blue = blue_y;
  54. info_ptr->valid |= PNG_INFO_cHRM;
  55. }
  56. }
  57. void PNGFAPI
  58. png_set_cHRM_XYZ_fixed(png_structp png_ptr, png_infop info_ptr,
  59. png_fixed_point int_red_X, png_fixed_point int_red_Y,
  60. png_fixed_point int_red_Z, png_fixed_point int_green_X,
  61. png_fixed_point int_green_Y, png_fixed_point int_green_Z,
  62. png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
  63. png_fixed_point int_blue_Z)
  64. {
  65. png_XYZ XYZ;
  66. png_xy xy;
  67. png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
  68. if (png_ptr == NULL || info_ptr == NULL)
  69. return;
  70. XYZ.redX = int_red_X;
  71. XYZ.redY = int_red_Y;
  72. XYZ.redZ = int_red_Z;
  73. XYZ.greenX = int_green_X;
  74. XYZ.greenY = int_green_Y;
  75. XYZ.greenZ = int_green_Z;
  76. XYZ.blueX = int_blue_X;
  77. XYZ.blueY = int_blue_Y;
  78. XYZ.blueZ = int_blue_Z;
  79. if (png_xy_from_XYZ(&xy, XYZ))
  80. png_error(png_ptr, "XYZ values out of representable range");
  81. png_set_cHRM_fixed(png_ptr, info_ptr, xy.whitex, xy.whitey, xy.redx, xy.redy,
  82. xy.greenx, xy.greeny, xy.bluex, xy.bluey);
  83. }
  84. # ifdef PNG_FLOATING_POINT_SUPPORTED
  85. void PNGAPI
  86. png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
  87. double white_x, double white_y, double red_x, double red_y,
  88. double green_x, double green_y, double blue_x, double blue_y)
  89. {
  90. png_set_cHRM_fixed(png_ptr, info_ptr,
  91. png_fixed(png_ptr, white_x, "cHRM White X"),
  92. png_fixed(png_ptr, white_y, "cHRM White Y"),
  93. png_fixed(png_ptr, red_x, "cHRM Red X"),
  94. png_fixed(png_ptr, red_y, "cHRM Red Y"),
  95. png_fixed(png_ptr, green_x, "cHRM Green X"),
  96. png_fixed(png_ptr, green_y, "cHRM Green Y"),
  97. png_fixed(png_ptr, blue_x, "cHRM Blue X"),
  98. png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
  99. }
  100. void PNGAPI
  101. png_set_cHRM_XYZ(png_structp png_ptr, png_infop info_ptr, double red_X,
  102. double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
  103. double blue_X, double blue_Y, double blue_Z)
  104. {
  105. png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
  106. png_fixed(png_ptr, red_X, "cHRM Red X"),
  107. png_fixed(png_ptr, red_Y, "cHRM Red Y"),
  108. png_fixed(png_ptr, red_Z, "cHRM Red Z"),
  109. png_fixed(png_ptr, green_X, "cHRM Red X"),
  110. png_fixed(png_ptr, green_Y, "cHRM Red Y"),
  111. png_fixed(png_ptr, green_Z, "cHRM Red Z"),
  112. png_fixed(png_ptr, blue_X, "cHRM Red X"),
  113. png_fixed(png_ptr, blue_Y, "cHRM Red Y"),
  114. png_fixed(png_ptr, blue_Z, "cHRM Red Z"));
  115. }
  116. # endif /* PNG_FLOATING_POINT_SUPPORTED */
  117. #endif /* PNG_cHRM_SUPPORTED */
  118. #ifdef PNG_gAMA_SUPPORTED
  119. void PNGFAPI
  120. png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  121. file_gamma)
  122. {
  123. png_debug1(1, "in %s storage function", "gAMA");
  124. if (png_ptr == NULL || info_ptr == NULL)
  125. return;
  126. /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
  127. * occur. Since the fixed point representation is assymetrical it is
  128. * possible for 1/gamma to overflow the limit of 21474 and this means the
  129. * gamma value must be at least 5/100000 and hence at most 20000.0. For
  130. * safety the limits here are a little narrower. The values are 0.00016 to
  131. * 6250.0, which are truly ridiculous gammma values (and will produce
  132. * displays that are all black or all white.)
  133. */
  134. if (file_gamma < 16 || file_gamma > 625000000)
  135. png_warning(png_ptr, "Out of range gamma value ignored");
  136. else
  137. {
  138. info_ptr->gamma = file_gamma;
  139. info_ptr->valid |= PNG_INFO_gAMA;
  140. }
  141. }
  142. # ifdef PNG_FLOATING_POINT_SUPPORTED
  143. void PNGAPI
  144. png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  145. {
  146. png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
  147. "png_set_gAMA"));
  148. }
  149. # endif
  150. #endif
  151. #ifdef PNG_hIST_SUPPORTED
  152. void PNGAPI
  153. png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_const_uint_16p hist)
  154. {
  155. int i;
  156. png_debug1(1, "in %s storage function", "hIST");
  157. if (png_ptr == NULL || info_ptr == NULL)
  158. return;
  159. if (info_ptr->num_palette == 0 || info_ptr->num_palette
  160. > PNG_MAX_PALETTE_LENGTH)
  161. {
  162. png_warning(png_ptr,
  163. "Invalid palette size, hIST allocation skipped");
  164. return;
  165. }
  166. png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  167. /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
  168. * version 1.2.1
  169. */
  170. png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  171. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
  172. if (png_ptr->hist == NULL)
  173. {
  174. png_warning(png_ptr, "Insufficient memory for hIST chunk data");
  175. return;
  176. }
  177. for (i = 0; i < info_ptr->num_palette; i++)
  178. png_ptr->hist[i] = hist[i];
  179. info_ptr->hist = png_ptr->hist;
  180. info_ptr->valid |= PNG_INFO_hIST;
  181. info_ptr->free_me |= PNG_FREE_HIST;
  182. }
  183. #endif
  184. void PNGAPI
  185. png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  186. png_uint_32 width, png_uint_32 height, int bit_depth,
  187. int color_type, int interlace_type, int compression_type,
  188. int filter_type)
  189. {
  190. png_debug1(1, "in %s storage function", "IHDR");
  191. if (png_ptr == NULL || info_ptr == NULL)
  192. return;
  193. info_ptr->width = width;
  194. info_ptr->height = height;
  195. info_ptr->bit_depth = (png_byte)bit_depth;
  196. info_ptr->color_type = (png_byte)color_type;
  197. info_ptr->compression_type = (png_byte)compression_type;
  198. info_ptr->filter_type = (png_byte)filter_type;
  199. info_ptr->interlace_type = (png_byte)interlace_type;
  200. png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
  201. info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
  202. info_ptr->compression_type, info_ptr->filter_type);
  203. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  204. info_ptr->channels = 1;
  205. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  206. info_ptr->channels = 3;
  207. else
  208. info_ptr->channels = 1;
  209. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  210. info_ptr->channels++;
  211. info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  212. /* Check for potential overflow */
  213. if (width >
  214. (PNG_UINT_32_MAX >> 3) /* 8-byte RRGGBBAA pixels */
  215. - 48 /* bigrowbuf hack */
  216. - 1 /* filter byte */
  217. - 7*8 /* rounding of width to multiple of 8 pixels */
  218. - 8) /* extra max_pixel_depth pad */
  219. info_ptr->rowbytes = 0;
  220. else
  221. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  222. }
  223. #ifdef PNG_oFFs_SUPPORTED
  224. void PNGAPI
  225. png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  226. png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  227. {
  228. png_debug1(1, "in %s storage function", "oFFs");
  229. if (png_ptr == NULL || info_ptr == NULL)
  230. return;
  231. info_ptr->x_offset = offset_x;
  232. info_ptr->y_offset = offset_y;
  233. info_ptr->offset_unit_type = (png_byte)unit_type;
  234. info_ptr->valid |= PNG_INFO_oFFs;
  235. }
  236. #endif
  237. #ifdef PNG_pCAL_SUPPORTED
  238. void PNGAPI
  239. png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  240. png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
  241. int nparams, png_const_charp units, png_charpp params)
  242. {
  243. png_size_t length;
  244. int i;
  245. png_debug1(1, "in %s storage function", "pCAL");
  246. if (png_ptr == NULL || info_ptr == NULL)
  247. return;
  248. length = png_strlen(purpose) + 1;
  249. png_debug1(3, "allocating purpose for info (%lu bytes)",
  250. (unsigned long)length);
  251. /* TODO: validate format of calibration name and unit name */
  252. /* Check that the type matches the specification. */
  253. if (type < 0 || type > 3)
  254. png_error(png_ptr, "Invalid pCAL equation type");
  255. /* Validate params[nparams] */
  256. for (i=0; i<nparams; ++i)
  257. if (!png_check_fp_string(params[i], png_strlen(params[i])))
  258. png_error(png_ptr, "Invalid format for pCAL parameter");
  259. info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  260. if (info_ptr->pcal_purpose == NULL)
  261. {
  262. png_warning(png_ptr, "Insufficient memory for pCAL purpose");
  263. return;
  264. }
  265. png_memcpy(info_ptr->pcal_purpose, purpose, length);
  266. png_debug(3, "storing X0, X1, type, and nparams in info");
  267. info_ptr->pcal_X0 = X0;
  268. info_ptr->pcal_X1 = X1;
  269. info_ptr->pcal_type = (png_byte)type;
  270. info_ptr->pcal_nparams = (png_byte)nparams;
  271. length = png_strlen(units) + 1;
  272. png_debug1(3, "allocating units for info (%lu bytes)",
  273. (unsigned long)length);
  274. info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  275. if (info_ptr->pcal_units == NULL)
  276. {
  277. png_warning(png_ptr, "Insufficient memory for pCAL units");
  278. return;
  279. }
  280. png_memcpy(info_ptr->pcal_units, units, length);
  281. info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  282. (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
  283. if (info_ptr->pcal_params == NULL)
  284. {
  285. png_warning(png_ptr, "Insufficient memory for pCAL params");
  286. return;
  287. }
  288. png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
  289. for (i = 0; i < nparams; i++)
  290. {
  291. length = png_strlen(params[i]) + 1;
  292. png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
  293. (unsigned long)length);
  294. info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  295. if (info_ptr->pcal_params[i] == NULL)
  296. {
  297. png_warning(png_ptr, "Insufficient memory for pCAL parameter");
  298. return;
  299. }
  300. png_memcpy(info_ptr->pcal_params[i], params[i], length);
  301. }
  302. info_ptr->valid |= PNG_INFO_pCAL;
  303. info_ptr->free_me |= PNG_FREE_PCAL;
  304. }
  305. #endif
  306. #ifdef PNG_sCAL_SUPPORTED
  307. void PNGAPI
  308. png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  309. int unit, png_const_charp swidth, png_const_charp sheight)
  310. {
  311. png_size_t lengthw = 0, lengthh = 0;
  312. png_debug1(1, "in %s storage function", "sCAL");
  313. if (png_ptr == NULL || info_ptr == NULL)
  314. return;
  315. /* Double check the unit (should never get here with an invalid
  316. * unit unless this is an API call.)
  317. */
  318. if (unit != 1 && unit != 2)
  319. png_error(png_ptr, "Invalid sCAL unit");
  320. if (swidth == NULL || (lengthw = png_strlen(swidth)) == 0 ||
  321. swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
  322. png_error(png_ptr, "Invalid sCAL width");
  323. if (sheight == NULL || (lengthh = png_strlen(sheight)) == 0 ||
  324. sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
  325. png_error(png_ptr, "Invalid sCAL height");
  326. info_ptr->scal_unit = (png_byte)unit;
  327. ++lengthw;
  328. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
  329. info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, lengthw);
  330. if (info_ptr->scal_s_width == NULL)
  331. {
  332. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  333. return;
  334. }
  335. png_memcpy(info_ptr->scal_s_width, swidth, lengthw);
  336. ++lengthh;
  337. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
  338. info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, lengthh);
  339. if (info_ptr->scal_s_height == NULL)
  340. {
  341. png_free (png_ptr, info_ptr->scal_s_width);
  342. info_ptr->scal_s_width = NULL;
  343. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  344. return;
  345. }
  346. png_memcpy(info_ptr->scal_s_height, sheight, lengthh);
  347. info_ptr->valid |= PNG_INFO_sCAL;
  348. info_ptr->free_me |= PNG_FREE_SCAL;
  349. }
  350. # ifdef PNG_FLOATING_POINT_SUPPORTED
  351. void PNGAPI
  352. png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width,
  353. double height)
  354. {
  355. png_debug1(1, "in %s storage function", "sCAL");
  356. /* Check the arguments. */
  357. if (width <= 0)
  358. png_warning(png_ptr, "Invalid sCAL width ignored");
  359. else if (height <= 0)
  360. png_warning(png_ptr, "Invalid sCAL height ignored");
  361. else
  362. {
  363. /* Convert 'width' and 'height' to ASCII. */
  364. char swidth[PNG_sCAL_MAX_DIGITS+1];
  365. char sheight[PNG_sCAL_MAX_DIGITS+1];
  366. png_ascii_from_fp(png_ptr, swidth, sizeof swidth, width,
  367. PNG_sCAL_PRECISION);
  368. png_ascii_from_fp(png_ptr, sheight, sizeof sheight, height,
  369. PNG_sCAL_PRECISION);
  370. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  371. }
  372. }
  373. # endif
  374. # ifdef PNG_FIXED_POINT_SUPPORTED
  375. void PNGAPI
  376. png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit,
  377. png_fixed_point width, png_fixed_point height)
  378. {
  379. png_debug1(1, "in %s storage function", "sCAL");
  380. /* Check the arguments. */
  381. if (width <= 0)
  382. png_warning(png_ptr, "Invalid sCAL width ignored");
  383. else if (height <= 0)
  384. png_warning(png_ptr, "Invalid sCAL height ignored");
  385. else
  386. {
  387. /* Convert 'width' and 'height' to ASCII. */
  388. char swidth[PNG_sCAL_MAX_DIGITS+1];
  389. char sheight[PNG_sCAL_MAX_DIGITS+1];
  390. png_ascii_from_fixed(png_ptr, swidth, sizeof swidth, width);
  391. png_ascii_from_fixed(png_ptr, sheight, sizeof sheight, height);
  392. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  393. }
  394. }
  395. # endif
  396. #endif
  397. #ifdef PNG_pHYs_SUPPORTED
  398. void PNGAPI
  399. png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  400. png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  401. {
  402. png_debug1(1, "in %s storage function", "pHYs");
  403. if (png_ptr == NULL || info_ptr == NULL)
  404. return;
  405. info_ptr->x_pixels_per_unit = res_x;
  406. info_ptr->y_pixels_per_unit = res_y;
  407. info_ptr->phys_unit_type = (png_byte)unit_type;
  408. info_ptr->valid |= PNG_INFO_pHYs;
  409. }
  410. #endif
  411. void PNGAPI
  412. png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  413. png_const_colorp palette, int num_palette)
  414. {
  415. png_debug1(1, "in %s storage function", "PLTE");
  416. if (png_ptr == NULL || info_ptr == NULL)
  417. return;
  418. if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  419. {
  420. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  421. png_error(png_ptr, "Invalid palette length");
  422. else
  423. {
  424. png_warning(png_ptr, "Invalid palette length");
  425. return;
  426. }
  427. }
  428. /* It may not actually be necessary to set png_ptr->palette here;
  429. * we do it for backward compatibility with the way the png_handle_tRNS
  430. * function used to do the allocation.
  431. */
  432. png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  433. /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  434. * of num_palette entries, in case of an invalid PNG file that has
  435. * too-large sample values.
  436. */
  437. png_ptr->palette = (png_colorp)png_calloc(png_ptr,
  438. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  439. png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  440. info_ptr->palette = png_ptr->palette;
  441. info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  442. info_ptr->free_me |= PNG_FREE_PLTE;
  443. info_ptr->valid |= PNG_INFO_PLTE;
  444. }
  445. #ifdef PNG_sBIT_SUPPORTED
  446. void PNGAPI
  447. png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  448. png_const_color_8p sig_bit)
  449. {
  450. png_debug1(1, "in %s storage function", "sBIT");
  451. if (png_ptr == NULL || info_ptr == NULL)
  452. return;
  453. png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  454. info_ptr->valid |= PNG_INFO_sBIT;
  455. }
  456. #endif
  457. #ifdef PNG_sRGB_SUPPORTED
  458. void PNGAPI
  459. png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int srgb_intent)
  460. {
  461. png_debug1(1, "in %s storage function", "sRGB");
  462. if (png_ptr == NULL || info_ptr == NULL)
  463. return;
  464. info_ptr->srgb_intent = (png_byte)srgb_intent;
  465. info_ptr->valid |= PNG_INFO_sRGB;
  466. }
  467. void PNGAPI
  468. png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  469. int srgb_intent)
  470. {
  471. png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
  472. if (png_ptr == NULL || info_ptr == NULL)
  473. return;
  474. png_set_sRGB(png_ptr, info_ptr, srgb_intent);
  475. # ifdef PNG_gAMA_SUPPORTED
  476. png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
  477. # endif
  478. # ifdef PNG_cHRM_SUPPORTED
  479. png_set_cHRM_fixed(png_ptr, info_ptr,
  480. /* color x y */
  481. /* white */ 31270, 32900,
  482. /* red */ 64000, 33000,
  483. /* green */ 30000, 60000,
  484. /* blue */ 15000, 6000
  485. );
  486. # endif /* cHRM */
  487. }
  488. #endif /* sRGB */
  489. #ifdef PNG_iCCP_SUPPORTED
  490. void PNGAPI
  491. png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  492. png_const_charp name, int compression_type,
  493. png_const_bytep profile, png_uint_32 proflen)
  494. {
  495. png_charp new_iccp_name;
  496. png_bytep new_iccp_profile;
  497. png_size_t length;
  498. png_debug1(1, "in %s storage function", "iCCP");
  499. if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  500. return;
  501. length = png_strlen(name)+1;
  502. new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  503. if (new_iccp_name == NULL)
  504. {
  505. png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
  506. return;
  507. }
  508. png_memcpy(new_iccp_name, name, length);
  509. new_iccp_profile = (png_bytep)png_malloc_warn(png_ptr, proflen);
  510. if (new_iccp_profile == NULL)
  511. {
  512. png_free (png_ptr, new_iccp_name);
  513. png_warning(png_ptr,
  514. "Insufficient memory to process iCCP profile");
  515. return;
  516. }
  517. png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  518. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  519. info_ptr->iccp_proflen = proflen;
  520. info_ptr->iccp_name = new_iccp_name;
  521. info_ptr->iccp_profile = new_iccp_profile;
  522. /* Compression is always zero but is here so the API and info structure
  523. * does not have to change if we introduce multiple compression types
  524. */
  525. info_ptr->iccp_compression = (png_byte)compression_type;
  526. info_ptr->free_me |= PNG_FREE_ICCP;
  527. info_ptr->valid |= PNG_INFO_iCCP;
  528. }
  529. #endif
  530. #ifdef PNG_TEXT_SUPPORTED
  531. void PNGAPI
  532. png_set_text(png_structp png_ptr, png_infop info_ptr, png_const_textp text_ptr,
  533. int num_text)
  534. {
  535. int ret;
  536. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  537. if (ret)
  538. png_error(png_ptr, "Insufficient memory to store text");
  539. }
  540. int /* PRIVATE */
  541. png_set_text_2(png_structp png_ptr, png_infop info_ptr,
  542. png_const_textp text_ptr, int num_text)
  543. {
  544. int i;
  545. png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
  546. (unsigned long)png_ptr->chunk_name);
  547. if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  548. return(0);
  549. /* Make sure we have enough space in the "text" array in info_struct
  550. * to hold all of the incoming text_ptr objects.
  551. */
  552. if (info_ptr->num_text + num_text > info_ptr->max_text)
  553. {
  554. if (info_ptr->text != NULL)
  555. {
  556. png_textp old_text;
  557. int old_max;
  558. old_max = info_ptr->max_text;
  559. info_ptr->max_text = info_ptr->num_text + num_text + 8;
  560. old_text = info_ptr->text;
  561. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  562. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  563. if (info_ptr->text == NULL)
  564. {
  565. png_free(png_ptr, old_text);
  566. return(1);
  567. }
  568. png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
  569. png_sizeof(png_text)));
  570. png_free(png_ptr, old_text);
  571. }
  572. else
  573. {
  574. info_ptr->max_text = num_text + 8;
  575. info_ptr->num_text = 0;
  576. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  577. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  578. if (info_ptr->text == NULL)
  579. return(1);
  580. info_ptr->free_me |= PNG_FREE_TEXT;
  581. }
  582. png_debug1(3, "allocated %d entries for info_ptr->text",
  583. info_ptr->max_text);
  584. }
  585. for (i = 0; i < num_text; i++)
  586. {
  587. png_size_t text_length, key_len;
  588. png_size_t lang_len, lang_key_len;
  589. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  590. if (text_ptr[i].key == NULL)
  591. continue;
  592. if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
  593. text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
  594. {
  595. png_warning(png_ptr, "text compression mode is out of range");
  596. continue;
  597. }
  598. key_len = png_strlen(text_ptr[i].key);
  599. if (text_ptr[i].compression <= 0)
  600. {
  601. lang_len = 0;
  602. lang_key_len = 0;
  603. }
  604. else
  605. # ifdef PNG_iTXt_SUPPORTED
  606. {
  607. /* Set iTXt data */
  608. if (text_ptr[i].lang != NULL)
  609. lang_len = png_strlen(text_ptr[i].lang);
  610. else
  611. lang_len = 0;
  612. if (text_ptr[i].lang_key != NULL)
  613. lang_key_len = png_strlen(text_ptr[i].lang_key);
  614. else
  615. lang_key_len = 0;
  616. }
  617. # else /* PNG_iTXt_SUPPORTED */
  618. {
  619. png_warning(png_ptr, "iTXt chunk not supported");
  620. continue;
  621. }
  622. # endif
  623. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  624. {
  625. text_length = 0;
  626. # ifdef PNG_iTXt_SUPPORTED
  627. if (text_ptr[i].compression > 0)
  628. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  629. else
  630. # endif
  631. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  632. }
  633. else
  634. {
  635. text_length = png_strlen(text_ptr[i].text);
  636. textp->compression = text_ptr[i].compression;
  637. }
  638. textp->key = (png_charp)png_malloc_warn(png_ptr,
  639. (png_size_t)
  640. (key_len + text_length + lang_len + lang_key_len + 4));
  641. if (textp->key == NULL)
  642. return(1);
  643. png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
  644. (unsigned long)(png_uint_32)
  645. (key_len + lang_len + lang_key_len + text_length + 4),
  646. textp->key);
  647. png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
  648. *(textp->key + key_len) = '\0';
  649. if (text_ptr[i].compression > 0)
  650. {
  651. textp->lang = textp->key + key_len + 1;
  652. png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  653. *(textp->lang + lang_len) = '\0';
  654. textp->lang_key = textp->lang + lang_len + 1;
  655. png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  656. *(textp->lang_key + lang_key_len) = '\0';
  657. textp->text = textp->lang_key + lang_key_len + 1;
  658. }
  659. else
  660. {
  661. textp->lang=NULL;
  662. textp->lang_key=NULL;
  663. textp->text = textp->key + key_len + 1;
  664. }
  665. if (text_length)
  666. png_memcpy(textp->text, text_ptr[i].text,
  667. (png_size_t)(text_length));
  668. *(textp->text + text_length) = '\0';
  669. # ifdef PNG_iTXt_SUPPORTED
  670. if (textp->compression > 0)
  671. {
  672. textp->text_length = 0;
  673. textp->itxt_length = text_length;
  674. }
  675. else
  676. # endif
  677. {
  678. textp->text_length = text_length;
  679. textp->itxt_length = 0;
  680. }
  681. info_ptr->num_text++;
  682. png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  683. }
  684. return(0);
  685. }
  686. #endif
  687. #ifdef PNG_tIME_SUPPORTED
  688. void PNGAPI
  689. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time)
  690. {
  691. png_debug1(1, "in %s storage function", "tIME");
  692. if (png_ptr == NULL || info_ptr == NULL ||
  693. (png_ptr->mode & PNG_WROTE_tIME))
  694. return;
  695. if (mod_time->month == 0 || mod_time->month > 12 ||
  696. mod_time->day == 0 || mod_time->day > 31 ||
  697. mod_time->hour > 23 || mod_time->minute > 59 ||
  698. mod_time->second > 60)
  699. {
  700. png_warning(png_ptr, "Ignoring invalid time value");
  701. return;
  702. }
  703. png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  704. info_ptr->valid |= PNG_INFO_tIME;
  705. }
  706. #endif
  707. #ifdef PNG_tRNS_SUPPORTED
  708. void PNGAPI
  709. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  710. png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
  711. {
  712. png_debug1(1, "in %s storage function", "tRNS");
  713. if (png_ptr == NULL || info_ptr == NULL)
  714. return;
  715. if (trans_alpha != NULL)
  716. {
  717. /* It may not actually be necessary to set png_ptr->trans_alpha here;
  718. * we do it for backward compatibility with the way the png_handle_tRNS
  719. * function used to do the allocation.
  720. */
  721. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  722. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  723. png_ptr->trans_alpha = info_ptr->trans_alpha =
  724. (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH);
  725. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  726. png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
  727. }
  728. if (trans_color != NULL)
  729. {
  730. int sample_max = (1 << info_ptr->bit_depth);
  731. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  732. (int)trans_color->gray > sample_max) ||
  733. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  734. ((int)trans_color->red > sample_max ||
  735. (int)trans_color->green > sample_max ||
  736. (int)trans_color->blue > sample_max)))
  737. png_warning(png_ptr,
  738. "tRNS chunk has out-of-range samples for bit_depth");
  739. png_memcpy(&(info_ptr->trans_color), trans_color,
  740. png_sizeof(png_color_16));
  741. if (num_trans == 0)
  742. num_trans = 1;
  743. }
  744. info_ptr->num_trans = (png_uint_16)num_trans;
  745. if (num_trans != 0)
  746. {
  747. info_ptr->valid |= PNG_INFO_tRNS;
  748. info_ptr->free_me |= PNG_FREE_TRNS;
  749. }
  750. }
  751. #endif
  752. #ifdef PNG_sPLT_SUPPORTED
  753. void PNGAPI
  754. png_set_sPLT(png_structp png_ptr,
  755. png_infop info_ptr, png_const_sPLT_tp entries, int nentries)
  756. /*
  757. * entries - array of png_sPLT_t structures
  758. * to be added to the list of palettes
  759. * in the info structure.
  760. *
  761. * nentries - number of palette structures to be
  762. * added.
  763. */
  764. {
  765. png_sPLT_tp np;
  766. int i;
  767. if (png_ptr == NULL || info_ptr == NULL)
  768. return;
  769. np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  770. (info_ptr->splt_palettes_num + nentries) *
  771. (png_size_t)png_sizeof(png_sPLT_t));
  772. if (np == NULL)
  773. {
  774. png_warning(png_ptr, "No memory for sPLT palettes");
  775. return;
  776. }
  777. png_memcpy(np, info_ptr->splt_palettes,
  778. info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  779. png_free(png_ptr, info_ptr->splt_palettes);
  780. info_ptr->splt_palettes=NULL;
  781. for (i = 0; i < nentries; i++)
  782. {
  783. png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  784. png_const_sPLT_tp from = entries + i;
  785. png_size_t length;
  786. length = png_strlen(from->name) + 1;
  787. to->name = (png_charp)png_malloc_warn(png_ptr, length);
  788. if (to->name == NULL)
  789. {
  790. png_warning(png_ptr,
  791. "Out of memory while processing sPLT chunk");
  792. continue;
  793. }
  794. png_memcpy(to->name, from->name, length);
  795. to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  796. from->nentries * png_sizeof(png_sPLT_entry));
  797. if (to->entries == NULL)
  798. {
  799. png_warning(png_ptr,
  800. "Out of memory while processing sPLT chunk");
  801. png_free(png_ptr, to->name);
  802. to->name = NULL;
  803. continue;
  804. }
  805. png_memcpy(to->entries, from->entries,
  806. from->nentries * png_sizeof(png_sPLT_entry));
  807. to->nentries = from->nentries;
  808. to->depth = from->depth;
  809. }
  810. info_ptr->splt_palettes = np;
  811. info_ptr->splt_palettes_num += nentries;
  812. info_ptr->valid |= PNG_INFO_sPLT;
  813. info_ptr->free_me |= PNG_FREE_SPLT;
  814. }
  815. #endif /* PNG_sPLT_SUPPORTED */
  816. #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  817. void PNGAPI
  818. png_set_unknown_chunks(png_structp png_ptr,
  819. png_infop info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  820. {
  821. png_unknown_chunkp np;
  822. int i;
  823. if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  824. return;
  825. np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  826. (png_size_t)(info_ptr->unknown_chunks_num + num_unknowns) *
  827. png_sizeof(png_unknown_chunk));
  828. if (np == NULL)
  829. {
  830. png_warning(png_ptr,
  831. "Out of memory while processing unknown chunk");
  832. return;
  833. }
  834. png_memcpy(np, info_ptr->unknown_chunks,
  835. (png_size_t)info_ptr->unknown_chunks_num *
  836. png_sizeof(png_unknown_chunk));
  837. png_free(png_ptr, info_ptr->unknown_chunks);
  838. info_ptr->unknown_chunks = NULL;
  839. for (i = 0; i < num_unknowns; i++)
  840. {
  841. png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  842. png_const_unknown_chunkp from = unknowns + i;
  843. png_memcpy(to->name, from->name, png_sizeof(from->name));
  844. to->name[png_sizeof(to->name)-1] = '\0';
  845. to->size = from->size;
  846. /* Note our location in the read or write sequence */
  847. to->location = (png_byte)(png_ptr->mode & 0xff);
  848. if (from->size == 0)
  849. to->data=NULL;
  850. else
  851. {
  852. to->data = (png_bytep)png_malloc_warn(png_ptr,
  853. (png_size_t)from->size);
  854. if (to->data == NULL)
  855. {
  856. png_warning(png_ptr,
  857. "Out of memory while processing unknown chunk");
  858. to->size = 0;
  859. }
  860. else
  861. png_memcpy(to->data, from->data, from->size);
  862. }
  863. }
  864. info_ptr->unknown_chunks = np;
  865. info_ptr->unknown_chunks_num += num_unknowns;
  866. info_ptr->free_me |= PNG_FREE_UNKN;
  867. }
  868. void PNGAPI
  869. png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  870. int chunk, int location)
  871. {
  872. if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  873. info_ptr->unknown_chunks_num)
  874. info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  875. }
  876. #endif
  877. #ifdef PNG_MNG_FEATURES_SUPPORTED
  878. png_uint_32 PNGAPI
  879. png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  880. {
  881. png_debug(1, "in png_permit_mng_features");
  882. if (png_ptr == NULL)
  883. return (png_uint_32)0;
  884. png_ptr->mng_features_permitted =
  885. (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  886. return (png_uint_32)png_ptr->mng_features_permitted;
  887. }
  888. #endif
  889. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  890. void PNGAPI
  891. png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_const_bytep
  892. chunk_list, int num_chunks)
  893. {
  894. png_bytep new_list, p;
  895. int i, old_num_chunks;
  896. if (png_ptr == NULL)
  897. return;
  898. if (num_chunks == 0)
  899. {
  900. if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  901. png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  902. else
  903. png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  904. if (keep == PNG_HANDLE_CHUNK_ALWAYS)
  905. png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  906. else
  907. png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  908. return;
  909. }
  910. if (chunk_list == NULL)
  911. return;
  912. old_num_chunks = png_ptr->num_chunk_list;
  913. new_list=(png_bytep)png_malloc(png_ptr,
  914. (png_size_t)(5*(num_chunks + old_num_chunks)));
  915. if (png_ptr->chunk_list != NULL)
  916. {
  917. png_memcpy(new_list, png_ptr->chunk_list,
  918. (png_size_t)(5*old_num_chunks));
  919. png_free(png_ptr, png_ptr->chunk_list);
  920. png_ptr->chunk_list=NULL;
  921. }
  922. png_memcpy(new_list + 5*old_num_chunks, chunk_list,
  923. (png_size_t)(5*num_chunks));
  924. for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
  925. *p=(png_byte)keep;
  926. png_ptr->num_chunk_list = old_num_chunks + num_chunks;
  927. png_ptr->chunk_list = new_list;
  928. png_ptr->free_me |= PNG_FREE_LIST;
  929. }
  930. #endif
  931. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  932. void PNGAPI
  933. png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  934. png_user_chunk_ptr read_user_chunk_fn)
  935. {
  936. png_debug(1, "in png_set_read_user_chunk_fn");
  937. if (png_ptr == NULL)
  938. return;
  939. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  940. png_ptr->user_chunk_ptr = user_chunk_ptr;
  941. }
  942. #endif
  943. #ifdef PNG_INFO_IMAGE_SUPPORTED
  944. void PNGAPI
  945. png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  946. {
  947. png_debug1(1, "in %s storage function", "rows");
  948. if (png_ptr == NULL || info_ptr == NULL)
  949. return;
  950. if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  951. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  952. info_ptr->row_pointers = row_pointers;
  953. if (row_pointers)
  954. info_ptr->valid |= PNG_INFO_IDAT;
  955. }
  956. #endif
  957. void PNGAPI
  958. png_set_compression_buffer_size(png_structp png_ptr, png_size_t size)
  959. {
  960. if (png_ptr == NULL)
  961. return;
  962. png_free(png_ptr, png_ptr->zbuf);
  963. if (size > ZLIB_IO_MAX)
  964. {
  965. png_warning(png_ptr, "Attempt to set buffer size beyond max ignored");
  966. png_ptr->zbuf_size = ZLIB_IO_MAX;
  967. size = ZLIB_IO_MAX; /* must fit */
  968. }
  969. else
  970. png_ptr->zbuf_size = (uInt)size;
  971. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  972. /* The following ensures a relatively safe failure if this gets called while
  973. * the buffer is actually in use.
  974. */
  975. png_ptr->zstream.next_out = png_ptr->zbuf;
  976. png_ptr->zstream.avail_out = 0;
  977. png_ptr->zstream.avail_in = 0;
  978. }
  979. void PNGAPI
  980. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  981. {
  982. if (png_ptr && info_ptr)
  983. info_ptr->valid &= ~mask;
  984. }
  985. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  986. /* This function was added to libpng 1.2.6 */
  987. void PNGAPI
  988. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  989. png_uint_32 user_height_max)
  990. {
  991. /* Images with dimensions larger than these limits will be
  992. * rejected by png_set_IHDR(). To accept any PNG datastream
  993. * regardless of dimensions, set both limits to 0x7ffffffL.
  994. */
  995. if (png_ptr == NULL)
  996. return;
  997. png_ptr->user_width_max = user_width_max;
  998. png_ptr->user_height_max = user_height_max;
  999. }
  1000. /* This function was added to libpng 1.4.0 */
  1001. void PNGAPI
  1002. png_set_chunk_cache_max (png_structp png_ptr,
  1003. png_uint_32 user_chunk_cache_max)
  1004. {
  1005. if (png_ptr)
  1006. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1007. }
  1008. /* This function was added to libpng 1.4.1 */
  1009. void PNGAPI
  1010. png_set_chunk_malloc_max (png_structp png_ptr,
  1011. png_alloc_size_t user_chunk_malloc_max)
  1012. {
  1013. if (png_ptr)
  1014. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1015. }
  1016. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1017. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1018. void PNGAPI
  1019. png_set_benign_errors(png_structp png_ptr, int allowed)
  1020. {
  1021. png_debug(1, "in png_set_benign_errors");
  1022. if (allowed)
  1023. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  1024. else
  1025. png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
  1026. }
  1027. #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
  1028. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */