pngset.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Last changed in libpng 1.5.14 [January 24, 2013]
  4. * Copyright (c) 1998-2013 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 gamma 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 (num_text < 0 ||
  553. num_text > INT_MAX - info_ptr->num_text - 8 ||
  554. (unsigned int)/*SAFE*/(num_text +/*SAFE*/
  555. info_ptr->num_text + 8) >=
  556. PNG_SIZE_MAX/png_sizeof(png_text))
  557. {
  558. png_warning(png_ptr, "too many text chunks");
  559. return(0);
  560. }
  561. if (info_ptr->num_text + num_text > info_ptr->max_text)
  562. {
  563. int old_max_text = info_ptr->max_text;
  564. int old_num_text = info_ptr->num_text;
  565. if (info_ptr->text != NULL)
  566. {
  567. png_textp old_text;
  568. info_ptr->max_text = info_ptr->num_text + num_text + 8;
  569. old_text = info_ptr->text;
  570. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  571. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  572. if (info_ptr->text == NULL)
  573. {
  574. /* Restore to previous condition */
  575. info_ptr->max_text = old_max_text;
  576. info_ptr->text = old_text;
  577. return(1);
  578. }
  579. png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
  580. png_sizeof(png_text)));
  581. png_free(png_ptr, old_text);
  582. }
  583. else
  584. {
  585. info_ptr->max_text = num_text + 8;
  586. info_ptr->num_text = 0;
  587. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  588. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  589. if (info_ptr->text == NULL)
  590. {
  591. /* Restore to previous condition */
  592. info_ptr->num_text = old_num_text;
  593. info_ptr->max_text = old_max_text;
  594. return(1);
  595. }
  596. info_ptr->free_me |= PNG_FREE_TEXT;
  597. }
  598. png_debug1(3, "allocated %d entries for info_ptr->text",
  599. info_ptr->max_text);
  600. }
  601. for (i = 0; i < num_text; i++)
  602. {
  603. png_size_t text_length, key_len;
  604. png_size_t lang_len, lang_key_len;
  605. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  606. if (text_ptr[i].key == NULL)
  607. continue;
  608. if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
  609. text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
  610. {
  611. png_warning(png_ptr, "text compression mode is out of range");
  612. continue;
  613. }
  614. key_len = png_strlen(text_ptr[i].key);
  615. if (text_ptr[i].compression <= 0)
  616. {
  617. lang_len = 0;
  618. lang_key_len = 0;
  619. }
  620. else
  621. # ifdef PNG_iTXt_SUPPORTED
  622. {
  623. /* Set iTXt data */
  624. if (text_ptr[i].lang != NULL)
  625. lang_len = png_strlen(text_ptr[i].lang);
  626. else
  627. lang_len = 0;
  628. if (text_ptr[i].lang_key != NULL)
  629. lang_key_len = png_strlen(text_ptr[i].lang_key);
  630. else
  631. lang_key_len = 0;
  632. }
  633. # else /* PNG_iTXt_SUPPORTED */
  634. {
  635. png_warning(png_ptr, "iTXt chunk not supported");
  636. continue;
  637. }
  638. # endif
  639. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  640. {
  641. text_length = 0;
  642. # ifdef PNG_iTXt_SUPPORTED
  643. if (text_ptr[i].compression > 0)
  644. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  645. else
  646. # endif
  647. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  648. }
  649. else
  650. {
  651. text_length = png_strlen(text_ptr[i].text);
  652. textp->compression = text_ptr[i].compression;
  653. }
  654. textp->key = (png_charp)png_malloc_warn(png_ptr,
  655. (png_size_t)
  656. (key_len + text_length + lang_len + lang_key_len + 4));
  657. if (textp->key == NULL)
  658. return(1);
  659. png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
  660. (unsigned long)(png_uint_32)
  661. (key_len + lang_len + lang_key_len + text_length + 4),
  662. textp->key);
  663. png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
  664. *(textp->key + key_len) = '\0';
  665. if (text_ptr[i].compression > 0)
  666. {
  667. textp->lang = textp->key + key_len + 1;
  668. png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  669. *(textp->lang + lang_len) = '\0';
  670. textp->lang_key = textp->lang + lang_len + 1;
  671. png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  672. *(textp->lang_key + lang_key_len) = '\0';
  673. textp->text = textp->lang_key + lang_key_len + 1;
  674. }
  675. else
  676. {
  677. textp->lang=NULL;
  678. textp->lang_key=NULL;
  679. textp->text = textp->key + key_len + 1;
  680. }
  681. if (text_length)
  682. png_memcpy(textp->text, text_ptr[i].text,
  683. (png_size_t)(text_length));
  684. *(textp->text + text_length) = '\0';
  685. # ifdef PNG_iTXt_SUPPORTED
  686. if (textp->compression > 0)
  687. {
  688. textp->text_length = 0;
  689. textp->itxt_length = text_length;
  690. }
  691. else
  692. # endif
  693. {
  694. textp->text_length = text_length;
  695. textp->itxt_length = 0;
  696. }
  697. info_ptr->num_text++;
  698. png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  699. }
  700. return(0);
  701. }
  702. #endif
  703. #ifdef PNG_tIME_SUPPORTED
  704. void PNGAPI
  705. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time)
  706. {
  707. png_debug1(1, "in %s storage function", "tIME");
  708. if (png_ptr == NULL || info_ptr == NULL ||
  709. (png_ptr->mode & PNG_WROTE_tIME))
  710. return;
  711. if (mod_time->month == 0 || mod_time->month > 12 ||
  712. mod_time->day == 0 || mod_time->day > 31 ||
  713. mod_time->hour > 23 || mod_time->minute > 59 ||
  714. mod_time->second > 60)
  715. {
  716. png_warning(png_ptr, "Ignoring invalid time value");
  717. return;
  718. }
  719. png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  720. info_ptr->valid |= PNG_INFO_tIME;
  721. }
  722. #endif
  723. #ifdef PNG_tRNS_SUPPORTED
  724. void PNGAPI
  725. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  726. png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
  727. {
  728. png_debug1(1, "in %s storage function", "tRNS");
  729. if (png_ptr == NULL || info_ptr == NULL)
  730. return;
  731. if (num_trans < 0 || num_trans > PNG_MAX_PALETTE_LENGTH)
  732. {
  733. png_warning(png_ptr, "Ignoring invalid num_trans value");
  734. return;
  735. }
  736. if (trans_alpha != NULL)
  737. {
  738. /* It may not actually be necessary to set png_ptr->trans_alpha here;
  739. * we do it for backward compatibility with the way the png_handle_tRNS
  740. * function used to do the allocation.
  741. */
  742. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  743. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  744. png_ptr->trans_alpha = info_ptr->trans_alpha =
  745. (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH);
  746. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  747. png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
  748. }
  749. if (trans_color != NULL)
  750. {
  751. int sample_max = (1 << info_ptr->bit_depth);
  752. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  753. (int)trans_color->gray > sample_max) ||
  754. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  755. ((int)trans_color->red > sample_max ||
  756. (int)trans_color->green > sample_max ||
  757. (int)trans_color->blue > sample_max)))
  758. png_warning(png_ptr,
  759. "tRNS chunk has out-of-range samples for bit_depth");
  760. png_memcpy(&(info_ptr->trans_color), trans_color,
  761. png_sizeof(png_color_16));
  762. if (num_trans == 0)
  763. num_trans = 1;
  764. }
  765. info_ptr->num_trans = (png_uint_16)num_trans;
  766. if (num_trans != 0)
  767. {
  768. info_ptr->valid |= PNG_INFO_tRNS;
  769. info_ptr->free_me |= PNG_FREE_TRNS;
  770. }
  771. }
  772. #endif
  773. #ifdef PNG_sPLT_SUPPORTED
  774. void PNGAPI
  775. png_set_sPLT(png_structp png_ptr,
  776. png_infop info_ptr, png_const_sPLT_tp entries, int nentries)
  777. /*
  778. * entries - array of png_sPLT_t structures
  779. * to be added to the list of palettes
  780. * in the info structure.
  781. *
  782. * nentries - number of palette structures to be
  783. * added.
  784. */
  785. {
  786. png_sPLT_tp np;
  787. int i;
  788. if (png_ptr == NULL || info_ptr == NULL)
  789. return;
  790. if (nentries < 0 ||
  791. nentries > INT_MAX-info_ptr->splt_palettes_num ||
  792. (unsigned int)/*SAFE*/(nentries +/*SAFE*/
  793. info_ptr->splt_palettes_num) >=
  794. PNG_SIZE_MAX/png_sizeof(png_sPLT_t))
  795. np=NULL;
  796. else
  797. np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  798. (info_ptr->splt_palettes_num + nentries) *
  799. (png_size_t)png_sizeof(png_sPLT_t));
  800. if (np == NULL)
  801. {
  802. png_warning(png_ptr, "No memory for sPLT palettes");
  803. return;
  804. }
  805. png_memcpy(np, info_ptr->splt_palettes,
  806. info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  807. png_free(png_ptr, info_ptr->splt_palettes);
  808. info_ptr->splt_palettes=NULL;
  809. for (i = 0; i < nentries; i++)
  810. {
  811. png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  812. png_const_sPLT_tp from = entries + i;
  813. png_size_t length;
  814. length = png_strlen(from->name) + 1;
  815. to->name = (png_charp)png_malloc_warn(png_ptr, length);
  816. if (to->name == NULL)
  817. {
  818. png_warning(png_ptr,
  819. "Out of memory while processing sPLT chunk");
  820. continue;
  821. }
  822. png_memcpy(to->name, from->name, length);
  823. to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  824. from->nentries * png_sizeof(png_sPLT_entry));
  825. if (to->entries == NULL)
  826. {
  827. png_warning(png_ptr,
  828. "Out of memory while processing sPLT chunk");
  829. png_free(png_ptr, to->name);
  830. to->name = NULL;
  831. continue;
  832. }
  833. png_memcpy(to->entries, from->entries,
  834. from->nentries * png_sizeof(png_sPLT_entry));
  835. to->nentries = from->nentries;
  836. to->depth = from->depth;
  837. }
  838. info_ptr->splt_palettes = np;
  839. info_ptr->splt_palettes_num += nentries;
  840. info_ptr->valid |= PNG_INFO_sPLT;
  841. info_ptr->free_me |= PNG_FREE_SPLT;
  842. }
  843. #endif /* PNG_sPLT_SUPPORTED */
  844. #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  845. void PNGAPI
  846. png_set_unknown_chunks(png_structp png_ptr,
  847. png_infop info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  848. {
  849. png_unknown_chunkp np;
  850. int i;
  851. if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  852. return;
  853. if (num_unknowns < 0 ||
  854. num_unknowns > INT_MAX-info_ptr->unknown_chunks_num ||
  855. (unsigned int)/*SAFE*/(num_unknowns +/*SAFE*/
  856. info_ptr->unknown_chunks_num) >=
  857. PNG_SIZE_MAX/png_sizeof(png_unknown_chunk))
  858. np=NULL;
  859. else
  860. np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  861. (png_size_t)(info_ptr->unknown_chunks_num + num_unknowns) *
  862. png_sizeof(png_unknown_chunk));
  863. if (np == NULL)
  864. {
  865. png_warning(png_ptr,
  866. "Out of memory while processing unknown chunk");
  867. return;
  868. }
  869. png_memcpy(np, info_ptr->unknown_chunks,
  870. (png_size_t)info_ptr->unknown_chunks_num *
  871. png_sizeof(png_unknown_chunk));
  872. png_free(png_ptr, info_ptr->unknown_chunks);
  873. info_ptr->unknown_chunks = NULL;
  874. for (i = 0; i < num_unknowns; i++)
  875. {
  876. png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  877. png_const_unknown_chunkp from = unknowns + i;
  878. png_memcpy(to->name, from->name, png_sizeof(from->name));
  879. to->name[png_sizeof(to->name)-1] = '\0';
  880. to->size = from->size;
  881. /* Note our location in the read or write sequence */
  882. to->location = (png_byte)(png_ptr->mode & 0xff);
  883. if (from->size == 0)
  884. to->data=NULL;
  885. else
  886. {
  887. to->data = (png_bytep)png_malloc_warn(png_ptr,
  888. (png_size_t)from->size);
  889. if (to->data == NULL)
  890. {
  891. png_warning(png_ptr,
  892. "Out of memory while processing unknown chunk");
  893. to->size = 0;
  894. }
  895. else
  896. png_memcpy(to->data, from->data, from->size);
  897. }
  898. }
  899. info_ptr->unknown_chunks = np;
  900. info_ptr->unknown_chunks_num += num_unknowns;
  901. info_ptr->free_me |= PNG_FREE_UNKN;
  902. }
  903. void PNGAPI
  904. png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  905. int chunk, int location)
  906. {
  907. if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  908. info_ptr->unknown_chunks_num)
  909. info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  910. }
  911. #endif
  912. #ifdef PNG_MNG_FEATURES_SUPPORTED
  913. png_uint_32 PNGAPI
  914. png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  915. {
  916. png_debug(1, "in png_permit_mng_features");
  917. if (png_ptr == NULL)
  918. return (png_uint_32)0;
  919. png_ptr->mng_features_permitted =
  920. (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  921. return (png_uint_32)png_ptr->mng_features_permitted;
  922. }
  923. #endif
  924. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  925. void PNGAPI
  926. png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_const_bytep
  927. chunk_list, int num_chunks)
  928. {
  929. png_bytep new_list, p;
  930. int i, old_num_chunks;
  931. if (png_ptr == NULL)
  932. return;
  933. if (num_chunks == 0)
  934. {
  935. if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  936. png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  937. else
  938. png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  939. if (keep == PNG_HANDLE_CHUNK_ALWAYS)
  940. png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  941. else
  942. png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  943. return;
  944. }
  945. if (chunk_list == NULL)
  946. return;
  947. old_num_chunks = png_ptr->num_chunk_list;
  948. new_list=(png_bytep)png_malloc(png_ptr,
  949. (png_size_t)(5*(num_chunks + old_num_chunks)));
  950. if (png_ptr->chunk_list != NULL)
  951. {
  952. png_memcpy(new_list, png_ptr->chunk_list,
  953. (png_size_t)(5*old_num_chunks));
  954. png_free(png_ptr, png_ptr->chunk_list);
  955. png_ptr->chunk_list=NULL;
  956. }
  957. png_memcpy(new_list + 5*old_num_chunks, chunk_list,
  958. (png_size_t)(5*num_chunks));
  959. for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
  960. *p=(png_byte)keep;
  961. png_ptr->num_chunk_list = old_num_chunks + num_chunks;
  962. png_ptr->chunk_list = new_list;
  963. png_ptr->free_me |= PNG_FREE_LIST;
  964. }
  965. #endif
  966. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  967. void PNGAPI
  968. png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  969. png_user_chunk_ptr read_user_chunk_fn)
  970. {
  971. png_debug(1, "in png_set_read_user_chunk_fn");
  972. if (png_ptr == NULL)
  973. return;
  974. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  975. png_ptr->user_chunk_ptr = user_chunk_ptr;
  976. }
  977. #endif
  978. #ifdef PNG_INFO_IMAGE_SUPPORTED
  979. void PNGAPI
  980. png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  981. {
  982. png_debug1(1, "in %s storage function", "rows");
  983. if (png_ptr == NULL || info_ptr == NULL)
  984. return;
  985. if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  986. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  987. info_ptr->row_pointers = row_pointers;
  988. if (row_pointers)
  989. info_ptr->valid |= PNG_INFO_IDAT;
  990. }
  991. #endif
  992. void PNGAPI
  993. png_set_compression_buffer_size(png_structp png_ptr, png_size_t size)
  994. {
  995. if (png_ptr == NULL)
  996. return;
  997. png_free(png_ptr, png_ptr->zbuf);
  998. if (size > ZLIB_IO_MAX)
  999. {
  1000. png_warning(png_ptr, "Attempt to set buffer size beyond max ignored");
  1001. png_ptr->zbuf_size = ZLIB_IO_MAX;
  1002. size = ZLIB_IO_MAX; /* must fit */
  1003. }
  1004. else
  1005. png_ptr->zbuf_size = (uInt)size;
  1006. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1007. /* The following ensures a relatively safe failure if this gets called while
  1008. * the buffer is actually in use.
  1009. */
  1010. png_ptr->zstream.next_out = png_ptr->zbuf;
  1011. png_ptr->zstream.avail_out = 0;
  1012. png_ptr->zstream.avail_in = 0;
  1013. }
  1014. void PNGAPI
  1015. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1016. {
  1017. if (png_ptr && info_ptr)
  1018. info_ptr->valid &= ~mask;
  1019. }
  1020. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1021. /* This function was added to libpng 1.2.6 */
  1022. void PNGAPI
  1023. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1024. png_uint_32 user_height_max)
  1025. {
  1026. /* Images with dimensions larger than these limits will be
  1027. * rejected by png_set_IHDR(). To accept any PNG datastream
  1028. * regardless of dimensions, set both limits to 0x7ffffffL.
  1029. */
  1030. if (png_ptr == NULL)
  1031. return;
  1032. png_ptr->user_width_max = user_width_max;
  1033. png_ptr->user_height_max = user_height_max;
  1034. }
  1035. /* This function was added to libpng 1.4.0 */
  1036. void PNGAPI
  1037. png_set_chunk_cache_max (png_structp png_ptr,
  1038. png_uint_32 user_chunk_cache_max)
  1039. {
  1040. if (png_ptr)
  1041. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1042. }
  1043. /* This function was added to libpng 1.4.1 */
  1044. void PNGAPI
  1045. png_set_chunk_malloc_max (png_structp png_ptr,
  1046. png_alloc_size_t user_chunk_malloc_max)
  1047. {
  1048. if (png_ptr)
  1049. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1050. }
  1051. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1052. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1053. void PNGAPI
  1054. png_set_benign_errors(png_structp png_ptr, int allowed)
  1055. {
  1056. png_debug(1, "in png_set_benign_errors");
  1057. if (allowed)
  1058. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  1059. else
  1060. png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
  1061. }
  1062. #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
  1063. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  1064. /* Whether to report invalid palette index; added at libng-1.5.10
  1065. * allowed - one of 0: disable; 1: enable
  1066. */
  1067. void PNGAPI
  1068. png_set_check_for_invalid_index(png_structp png_ptr, int allowed)
  1069. {
  1070. png_debug(1, "in png_set_check_for_invalid_index");
  1071. if (allowed)
  1072. png_ptr->num_palette_max = 0;
  1073. else
  1074. png_ptr->num_palette_max = -1;
  1075. }
  1076. #endif
  1077. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */