pngset.c 37 KB

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