pngset.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Last changed in libpng 1.5.23 [July 23, 2015]
  4. * Copyright (c) 1998-2015 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. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  213. }
  214. #ifdef PNG_oFFs_SUPPORTED
  215. void PNGAPI
  216. png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  217. png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  218. {
  219. png_debug1(1, "in %s storage function", "oFFs");
  220. if (png_ptr == NULL || info_ptr == NULL)
  221. return;
  222. info_ptr->x_offset = offset_x;
  223. info_ptr->y_offset = offset_y;
  224. info_ptr->offset_unit_type = (png_byte)unit_type;
  225. info_ptr->valid |= PNG_INFO_oFFs;
  226. }
  227. #endif
  228. #ifdef PNG_pCAL_SUPPORTED
  229. void PNGAPI
  230. png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  231. png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
  232. int nparams, png_const_charp units, png_charpp params)
  233. {
  234. png_size_t length;
  235. int i;
  236. png_debug1(1, "in %s storage function", "pCAL");
  237. if (png_ptr == NULL || info_ptr == NULL)
  238. return;
  239. length = png_strlen(purpose) + 1;
  240. png_debug1(3, "allocating purpose for info (%lu bytes)",
  241. (unsigned long)length);
  242. /* TODO: validate format of calibration name and unit name */
  243. /* Check that the type matches the specification. */
  244. if (type < 0 || type > 3)
  245. png_error(png_ptr, "Invalid pCAL equation type");
  246. /* Validate params[nparams] */
  247. for (i=0; i<nparams; ++i)
  248. if (!png_check_fp_string(params[i], png_strlen(params[i])))
  249. png_error(png_ptr, "Invalid format for pCAL parameter");
  250. info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  251. if (info_ptr->pcal_purpose == NULL)
  252. {
  253. png_warning(png_ptr, "Insufficient memory for pCAL purpose");
  254. return;
  255. }
  256. png_memcpy(info_ptr->pcal_purpose, purpose, length);
  257. png_debug(3, "storing X0, X1, type, and nparams in info");
  258. info_ptr->pcal_X0 = X0;
  259. info_ptr->pcal_X1 = X1;
  260. info_ptr->pcal_type = (png_byte)type;
  261. info_ptr->pcal_nparams = (png_byte)nparams;
  262. length = png_strlen(units) + 1;
  263. png_debug1(3, "allocating units for info (%lu bytes)",
  264. (unsigned long)length);
  265. info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  266. if (info_ptr->pcal_units == NULL)
  267. {
  268. png_warning(png_ptr, "Insufficient memory for pCAL units");
  269. return;
  270. }
  271. png_memcpy(info_ptr->pcal_units, units, length);
  272. info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  273. (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
  274. if (info_ptr->pcal_params == NULL)
  275. {
  276. png_warning(png_ptr, "Insufficient memory for pCAL params");
  277. return;
  278. }
  279. png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
  280. for (i = 0; i < nparams; i++)
  281. {
  282. length = png_strlen(params[i]) + 1;
  283. png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
  284. (unsigned long)length);
  285. info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  286. if (info_ptr->pcal_params[i] == NULL)
  287. {
  288. png_warning(png_ptr, "Insufficient memory for pCAL parameter");
  289. return;
  290. }
  291. png_memcpy(info_ptr->pcal_params[i], params[i], length);
  292. }
  293. info_ptr->valid |= PNG_INFO_pCAL;
  294. info_ptr->free_me |= PNG_FREE_PCAL;
  295. }
  296. #endif
  297. #ifdef PNG_sCAL_SUPPORTED
  298. void PNGAPI
  299. png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  300. int unit, png_const_charp swidth, png_const_charp sheight)
  301. {
  302. png_size_t lengthw = 0, lengthh = 0;
  303. png_debug1(1, "in %s storage function", "sCAL");
  304. if (png_ptr == NULL || info_ptr == NULL)
  305. return;
  306. /* Double check the unit (should never get here with an invalid
  307. * unit unless this is an API call.)
  308. */
  309. if (unit != 1 && unit != 2)
  310. png_error(png_ptr, "Invalid sCAL unit");
  311. if (swidth == NULL || (lengthw = png_strlen(swidth)) == 0 ||
  312. swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
  313. png_error(png_ptr, "Invalid sCAL width");
  314. if (sheight == NULL || (lengthh = png_strlen(sheight)) == 0 ||
  315. sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
  316. png_error(png_ptr, "Invalid sCAL height");
  317. info_ptr->scal_unit = (png_byte)unit;
  318. ++lengthw;
  319. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
  320. info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, lengthw);
  321. if (info_ptr->scal_s_width == NULL)
  322. {
  323. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  324. return;
  325. }
  326. png_memcpy(info_ptr->scal_s_width, swidth, lengthw);
  327. ++lengthh;
  328. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
  329. info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, lengthh);
  330. if (info_ptr->scal_s_height == NULL)
  331. {
  332. png_free (png_ptr, info_ptr->scal_s_width);
  333. info_ptr->scal_s_width = NULL;
  334. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  335. return;
  336. }
  337. png_memcpy(info_ptr->scal_s_height, sheight, lengthh);
  338. info_ptr->valid |= PNG_INFO_sCAL;
  339. info_ptr->free_me |= PNG_FREE_SCAL;
  340. }
  341. # ifdef PNG_FLOATING_POINT_SUPPORTED
  342. void PNGAPI
  343. png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width,
  344. double height)
  345. {
  346. png_debug1(1, "in %s storage function", "sCAL");
  347. /* Check the arguments. */
  348. if (width <= 0)
  349. png_warning(png_ptr, "Invalid sCAL width ignored");
  350. else if (height <= 0)
  351. png_warning(png_ptr, "Invalid sCAL height ignored");
  352. else
  353. {
  354. /* Convert 'width' and 'height' to ASCII. */
  355. char swidth[PNG_sCAL_MAX_DIGITS+1];
  356. char sheight[PNG_sCAL_MAX_DIGITS+1];
  357. png_ascii_from_fp(png_ptr, swidth, sizeof swidth, width,
  358. PNG_sCAL_PRECISION);
  359. png_ascii_from_fp(png_ptr, sheight, sizeof sheight, height,
  360. PNG_sCAL_PRECISION);
  361. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  362. }
  363. }
  364. # endif
  365. # ifdef PNG_FIXED_POINT_SUPPORTED
  366. void PNGAPI
  367. png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit,
  368. png_fixed_point width, png_fixed_point height)
  369. {
  370. png_debug1(1, "in %s storage function", "sCAL");
  371. /* Check the arguments. */
  372. if (width <= 0)
  373. png_warning(png_ptr, "Invalid sCAL width ignored");
  374. else if (height <= 0)
  375. png_warning(png_ptr, "Invalid sCAL height ignored");
  376. else
  377. {
  378. /* Convert 'width' and 'height' to ASCII. */
  379. char swidth[PNG_sCAL_MAX_DIGITS+1];
  380. char sheight[PNG_sCAL_MAX_DIGITS+1];
  381. png_ascii_from_fixed(png_ptr, swidth, sizeof swidth, width);
  382. png_ascii_from_fixed(png_ptr, sheight, sizeof sheight, height);
  383. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  384. }
  385. }
  386. # endif
  387. #endif
  388. #ifdef PNG_pHYs_SUPPORTED
  389. void PNGAPI
  390. png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  391. png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  392. {
  393. png_debug1(1, "in %s storage function", "pHYs");
  394. if (png_ptr == NULL || info_ptr == NULL)
  395. return;
  396. info_ptr->x_pixels_per_unit = res_x;
  397. info_ptr->y_pixels_per_unit = res_y;
  398. info_ptr->phys_unit_type = (png_byte)unit_type;
  399. info_ptr->valid |= PNG_INFO_pHYs;
  400. }
  401. #endif
  402. void PNGAPI
  403. png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  404. png_const_colorp palette, int num_palette)
  405. {
  406. png_debug1(1, "in %s storage function", "PLTE");
  407. if (png_ptr == NULL || info_ptr == NULL)
  408. return;
  409. if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  410. {
  411. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  412. png_error(png_ptr, "Invalid palette length");
  413. else
  414. {
  415. png_warning(png_ptr, "Invalid palette length");
  416. return;
  417. }
  418. }
  419. /* It may not actually be necessary to set png_ptr->palette here;
  420. * we do it for backward compatibility with the way the png_handle_tRNS
  421. * function used to do the allocation.
  422. */
  423. png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  424. /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  425. * of num_palette entries, in case of an invalid PNG file that has
  426. * too-large sample values.
  427. */
  428. png_ptr->palette = (png_colorp)png_calloc(png_ptr,
  429. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  430. png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  431. info_ptr->palette = png_ptr->palette;
  432. info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  433. info_ptr->free_me |= PNG_FREE_PLTE;
  434. info_ptr->valid |= PNG_INFO_PLTE;
  435. }
  436. #ifdef PNG_sBIT_SUPPORTED
  437. void PNGAPI
  438. png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  439. png_const_color_8p sig_bit)
  440. {
  441. png_debug1(1, "in %s storage function", "sBIT");
  442. if (png_ptr == NULL || info_ptr == NULL)
  443. return;
  444. png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  445. info_ptr->valid |= PNG_INFO_sBIT;
  446. }
  447. #endif
  448. #ifdef PNG_sRGB_SUPPORTED
  449. void PNGAPI
  450. png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int srgb_intent)
  451. {
  452. png_debug1(1, "in %s storage function", "sRGB");
  453. if (png_ptr == NULL || info_ptr == NULL)
  454. return;
  455. info_ptr->srgb_intent = (png_byte)srgb_intent;
  456. info_ptr->valid |= PNG_INFO_sRGB;
  457. }
  458. void PNGAPI
  459. png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  460. int srgb_intent)
  461. {
  462. png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
  463. if (png_ptr == NULL || info_ptr == NULL)
  464. return;
  465. png_set_sRGB(png_ptr, info_ptr, srgb_intent);
  466. # ifdef PNG_gAMA_SUPPORTED
  467. png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
  468. # endif
  469. # ifdef PNG_cHRM_SUPPORTED
  470. png_set_cHRM_fixed(png_ptr, info_ptr,
  471. /* color x y */
  472. /* white */ 31270, 32900,
  473. /* red */ 64000, 33000,
  474. /* green */ 30000, 60000,
  475. /* blue */ 15000, 6000
  476. );
  477. # endif /* cHRM */
  478. }
  479. #endif /* sRGB */
  480. #ifdef PNG_iCCP_SUPPORTED
  481. void PNGAPI
  482. png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  483. png_const_charp name, int compression_type,
  484. png_const_bytep profile, png_uint_32 proflen)
  485. {
  486. png_charp new_iccp_name;
  487. png_bytep new_iccp_profile;
  488. png_size_t length;
  489. png_debug1(1, "in %s storage function", "iCCP");
  490. if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  491. return;
  492. length = png_strlen(name)+1;
  493. new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  494. if (new_iccp_name == NULL)
  495. {
  496. png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
  497. return;
  498. }
  499. png_memcpy(new_iccp_name, name, length);
  500. new_iccp_profile = (png_bytep)png_malloc_warn(png_ptr, proflen);
  501. if (new_iccp_profile == NULL)
  502. {
  503. png_free (png_ptr, new_iccp_name);
  504. png_warning(png_ptr,
  505. "Insufficient memory to process iCCP profile");
  506. return;
  507. }
  508. png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  509. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  510. info_ptr->iccp_proflen = proflen;
  511. info_ptr->iccp_name = new_iccp_name;
  512. info_ptr->iccp_profile = new_iccp_profile;
  513. /* Compression is always zero but is here so the API and info structure
  514. * does not have to change if we introduce multiple compression types
  515. */
  516. info_ptr->iccp_compression = (png_byte)compression_type;
  517. info_ptr->free_me |= PNG_FREE_ICCP;
  518. info_ptr->valid |= PNG_INFO_iCCP;
  519. }
  520. #endif
  521. #ifdef PNG_TEXT_SUPPORTED
  522. void PNGAPI
  523. png_set_text(png_structp png_ptr, png_infop info_ptr, png_const_textp text_ptr,
  524. int num_text)
  525. {
  526. int ret;
  527. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  528. if (ret != 0)
  529. png_error(png_ptr, "Insufficient memory to store text");
  530. }
  531. int /* PRIVATE */
  532. png_set_text_2(png_structp png_ptr, png_infop info_ptr,
  533. png_const_textp text_ptr, int num_text)
  534. {
  535. int i;
  536. size_t element_size;
  537. png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11 :
  538. (unsigned long)png_ptr->chunk_name);
  539. if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  540. return(0);
  541. /* Make sure we have enough space in the "text" array in info_struct
  542. * to hold all of the incoming text_ptr objects.
  543. */
  544. element_size=png_sizeof(png_text);
  545. if (num_text < 0 ||
  546. num_text > INT_MAX - info_ptr->num_text - 8 ||
  547. (unsigned int)/*SAFE*/(num_text +/*SAFE*/
  548. info_ptr->num_text + 8) >=
  549. PNG_SIZE_MAX/element_size)
  550. {
  551. png_warning(png_ptr, "too many text chunks");
  552. return(0);
  553. }
  554. if (info_ptr->num_text + num_text > info_ptr->max_text)
  555. {
  556. int old_max_text = info_ptr->max_text;
  557. int old_num_text = info_ptr->num_text;
  558. if (info_ptr->text != NULL)
  559. {
  560. png_textp old_text;
  561. info_ptr->max_text = info_ptr->num_text + num_text + 8;
  562. old_text = info_ptr->text;
  563. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  564. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  565. if (info_ptr->text == NULL)
  566. {
  567. /* Restore to previous condition */
  568. info_ptr->max_text = old_max_text;
  569. info_ptr->text = old_text;
  570. return(1);
  571. }
  572. png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
  573. png_sizeof(png_text)));
  574. png_free(png_ptr, old_text);
  575. }
  576. else
  577. {
  578. info_ptr->max_text = num_text + 8;
  579. info_ptr->num_text = 0;
  580. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  581. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  582. if (info_ptr->text == NULL)
  583. {
  584. /* Restore to previous condition */
  585. info_ptr->num_text = old_num_text;
  586. info_ptr->max_text = old_max_text;
  587. return(1);
  588. }
  589. info_ptr->free_me |= PNG_FREE_TEXT;
  590. }
  591. png_debug1(3, "allocated %d entries for info_ptr->text",
  592. info_ptr->max_text);
  593. }
  594. for (i = 0; i < num_text; i++)
  595. {
  596. png_size_t text_length, key_len;
  597. png_size_t lang_len, lang_key_len;
  598. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  599. if (text_ptr[i].key == NULL)
  600. continue;
  601. if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
  602. text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
  603. {
  604. png_warning(png_ptr, "text compression mode is out of range");
  605. continue;
  606. }
  607. key_len = png_strlen(text_ptr[i].key);
  608. if (text_ptr[i].compression <= 0)
  609. {
  610. lang_len = 0;
  611. lang_key_len = 0;
  612. }
  613. else
  614. # ifdef PNG_iTXt_SUPPORTED
  615. {
  616. /* Set iTXt data */
  617. if (text_ptr[i].lang != NULL)
  618. lang_len = png_strlen(text_ptr[i].lang);
  619. else
  620. lang_len = 0;
  621. if (text_ptr[i].lang_key != NULL)
  622. lang_key_len = png_strlen(text_ptr[i].lang_key);
  623. else
  624. lang_key_len = 0;
  625. }
  626. # else /* PNG_iTXt_SUPPORTED */
  627. {
  628. png_warning(png_ptr, "iTXt chunk not supported");
  629. continue;
  630. }
  631. # endif
  632. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  633. {
  634. text_length = 0;
  635. # ifdef PNG_iTXt_SUPPORTED
  636. if (text_ptr[i].compression > 0)
  637. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  638. else
  639. # endif
  640. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  641. }
  642. else
  643. {
  644. text_length = png_strlen(text_ptr[i].text);
  645. textp->compression = text_ptr[i].compression;
  646. }
  647. textp->key = (png_charp)png_malloc_warn(png_ptr,
  648. (png_size_t)
  649. (key_len + text_length + lang_len + lang_key_len + 4));
  650. if (textp->key == NULL)
  651. return(1);
  652. png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
  653. (unsigned long)(png_uint_32)
  654. (key_len + lang_len + lang_key_len + text_length + 4),
  655. textp->key);
  656. png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
  657. *(textp->key + key_len) = '\0';
  658. if (text_ptr[i].compression > 0)
  659. {
  660. textp->lang = textp->key + key_len + 1;
  661. png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  662. *(textp->lang + lang_len) = '\0';
  663. textp->lang_key = textp->lang + lang_len + 1;
  664. png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  665. *(textp->lang_key + lang_key_len) = '\0';
  666. textp->text = textp->lang_key + lang_key_len + 1;
  667. }
  668. else
  669. {
  670. textp->lang=NULL;
  671. textp->lang_key=NULL;
  672. textp->text = textp->key + key_len + 1;
  673. }
  674. if (text_length != 0)
  675. png_memcpy(textp->text, text_ptr[i].text,
  676. (png_size_t)(text_length));
  677. *(textp->text + text_length) = '\0';
  678. # ifdef PNG_iTXt_SUPPORTED
  679. if (textp->compression > 0)
  680. {
  681. textp->text_length = 0;
  682. textp->itxt_length = text_length;
  683. }
  684. else
  685. # endif
  686. {
  687. textp->text_length = text_length;
  688. textp->itxt_length = 0;
  689. }
  690. info_ptr->num_text++;
  691. png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  692. }
  693. return(0);
  694. }
  695. #endif
  696. #ifdef PNG_tIME_SUPPORTED
  697. void PNGAPI
  698. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time)
  699. {
  700. png_debug1(1, "in %s storage function", "tIME");
  701. if (png_ptr == NULL || info_ptr == NULL ||
  702. (png_ptr->mode & PNG_WROTE_tIME))
  703. return;
  704. if (mod_time->month == 0 || mod_time->month > 12 ||
  705. mod_time->day == 0 || mod_time->day > 31 ||
  706. mod_time->hour > 23 || mod_time->minute > 59 ||
  707. mod_time->second > 60)
  708. {
  709. png_warning(png_ptr, "Ignoring invalid time value");
  710. return;
  711. }
  712. png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  713. info_ptr->valid |= PNG_INFO_tIME;
  714. }
  715. #endif
  716. #ifdef PNG_tRNS_SUPPORTED
  717. void PNGAPI
  718. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  719. png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
  720. {
  721. png_debug1(1, "in %s storage function", "tRNS");
  722. if (png_ptr == NULL || info_ptr == NULL)
  723. return;
  724. if (num_trans < 0 || num_trans > PNG_MAX_PALETTE_LENGTH)
  725. {
  726. png_warning(png_ptr, "Ignoring invalid num_trans value");
  727. return;
  728. }
  729. if (trans_alpha != NULL)
  730. {
  731. /* It may not actually be necessary to set png_ptr->trans_alpha here;
  732. * we do it for backward compatibility with the way the png_handle_tRNS
  733. * function used to do the allocation.
  734. */
  735. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  736. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  737. png_ptr->trans_alpha = info_ptr->trans_alpha =
  738. (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH);
  739. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  740. png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
  741. }
  742. if (trans_color != NULL)
  743. {
  744. if (info_ptr->bit_depth < 16)
  745. {
  746. unsigned int sample_max = (1U << info_ptr->bit_depth) - 1U;
  747. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  748. trans_color->gray > sample_max) ||
  749. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  750. (trans_color->red > sample_max ||
  751. trans_color->green > sample_max ||
  752. trans_color->blue > sample_max)))
  753. png_warning(png_ptr,
  754. "tRNS chunk has out-of-range samples for bit_depth");
  755. }
  756. png_memcpy(&(info_ptr->trans_color), trans_color,
  757. png_sizeof(png_color_16));
  758. if (num_trans == 0)
  759. num_trans = 1;
  760. }
  761. info_ptr->num_trans = (png_uint_16)num_trans;
  762. if (num_trans != 0)
  763. {
  764. info_ptr->valid |= PNG_INFO_tRNS;
  765. info_ptr->free_me |= PNG_FREE_TRNS;
  766. }
  767. }
  768. #endif
  769. #ifdef PNG_sPLT_SUPPORTED
  770. void PNGAPI
  771. png_set_sPLT(png_structp png_ptr,
  772. png_infop info_ptr, png_const_sPLT_tp entries, int nentries)
  773. /*
  774. * entries - array of png_sPLT_t structures
  775. * to be added to the list of palettes
  776. * in the info structure.
  777. *
  778. * nentries - number of palette structures to be
  779. * added.
  780. */
  781. {
  782. png_sPLT_tp np;
  783. int i;
  784. size_t element_size;
  785. if (png_ptr == NULL || info_ptr == NULL)
  786. return;
  787. element_size = png_sizeof(png_sPLT_t);
  788. if (nentries < 0 ||
  789. nentries > INT_MAX-info_ptr->splt_palettes_num ||
  790. (unsigned int)/*SAFE*/(nentries +/*SAFE*/
  791. info_ptr->splt_palettes_num) >=
  792. PNG_SIZE_MAX/element_size)
  793. np=NULL;
  794. else
  795. np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  796. (info_ptr->splt_palettes_num + nentries) *
  797. (png_size_t)png_sizeof(png_sPLT_t));
  798. if (np == NULL)
  799. {
  800. png_warning(png_ptr, "No memory for sPLT palettes");
  801. return;
  802. }
  803. png_memcpy(np, info_ptr->splt_palettes,
  804. info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  805. png_free(png_ptr, info_ptr->splt_palettes);
  806. info_ptr->splt_palettes=NULL;
  807. for (i = 0; i < nentries; i++)
  808. {
  809. png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  810. png_const_sPLT_tp from = entries + i;
  811. png_size_t length;
  812. length = png_strlen(from->name) + 1;
  813. to->name = (png_charp)png_malloc_warn(png_ptr, length);
  814. if (to->name == NULL)
  815. {
  816. png_warning(png_ptr,
  817. "Out of memory while processing sPLT chunk");
  818. continue;
  819. }
  820. png_memcpy(to->name, from->name, length);
  821. to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  822. from->nentries * png_sizeof(png_sPLT_entry));
  823. if (to->entries == NULL)
  824. {
  825. png_warning(png_ptr,
  826. "Out of memory while processing sPLT chunk");
  827. png_free(png_ptr, to->name);
  828. to->name = NULL;
  829. continue;
  830. }
  831. png_memcpy(to->entries, from->entries,
  832. from->nentries * png_sizeof(png_sPLT_entry));
  833. to->nentries = from->nentries;
  834. to->depth = from->depth;
  835. }
  836. info_ptr->splt_palettes = np;
  837. info_ptr->splt_palettes_num += nentries;
  838. info_ptr->valid |= PNG_INFO_sPLT;
  839. info_ptr->free_me |= PNG_FREE_SPLT;
  840. }
  841. #endif /* PNG_sPLT_SUPPORTED */
  842. #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  843. void PNGAPI
  844. png_set_unknown_chunks(png_structp png_ptr,
  845. png_infop info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  846. {
  847. png_unknown_chunkp np;
  848. int i;
  849. size_t element_size;
  850. if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  851. return;
  852. element_size = png_sizeof(png_unknown_chunk);
  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/element_size)
  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 != NULL)
  989. info_ptr->valid |= PNG_INFO_IDAT;
  990. }
  991. #endif
  992. #ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
  993. void PNGAPI
  994. png_set_compression_buffer_size(png_structp png_ptr, png_size_t size)
  995. {
  996. if (png_ptr == NULL)
  997. return;
  998. png_free(png_ptr, png_ptr->zbuf);
  999. if (size > ZLIB_IO_MAX)
  1000. {
  1001. png_warning(png_ptr, "Attempt to set buffer size beyond max ignored");
  1002. png_ptr->zbuf_size = ZLIB_IO_MAX;
  1003. size = ZLIB_IO_MAX; /* must fit */
  1004. }
  1005. else
  1006. png_ptr->zbuf_size = (uInt)size;
  1007. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1008. /* The following ensures a relatively safe failure if this gets called while
  1009. * the buffer is actually in use.
  1010. */
  1011. png_ptr->zstream.next_out = png_ptr->zbuf;
  1012. png_ptr->zstream.avail_out = 0;
  1013. png_ptr->zstream.avail_in = 0;
  1014. }
  1015. #endif /* WRITE_CUSTOMIZE_COMPRESSION */
  1016. void PNGAPI
  1017. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1018. {
  1019. if (png_ptr && info_ptr)
  1020. info_ptr->valid &= ~mask;
  1021. }
  1022. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1023. /* This function was added to libpng 1.2.6 */
  1024. void PNGAPI
  1025. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1026. png_uint_32 user_height_max)
  1027. {
  1028. /* Images with dimensions larger than these limits will be
  1029. * rejected by png_set_IHDR(). To accept any PNG datastream
  1030. * regardless of dimensions, set both limits to 0x7ffffffL.
  1031. */
  1032. if (png_ptr == NULL)
  1033. return;
  1034. png_ptr->user_width_max = user_width_max;
  1035. png_ptr->user_height_max = user_height_max;
  1036. }
  1037. /* This function was added to libpng 1.4.0 */
  1038. void PNGAPI
  1039. png_set_chunk_cache_max (png_structp png_ptr,
  1040. png_uint_32 user_chunk_cache_max)
  1041. {
  1042. if (png_ptr != NULL)
  1043. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1044. }
  1045. /* This function was added to libpng 1.4.1 */
  1046. void PNGAPI
  1047. png_set_chunk_malloc_max (png_structp png_ptr,
  1048. png_alloc_size_t user_chunk_malloc_max)
  1049. {
  1050. if (png_ptr != NULL)
  1051. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1052. }
  1053. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1054. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1055. void PNGAPI
  1056. png_set_benign_errors(png_structp png_ptr, int allowed)
  1057. {
  1058. png_debug(1, "in png_set_benign_errors");
  1059. if (allowed != 0)
  1060. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  1061. else
  1062. png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
  1063. }
  1064. #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
  1065. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  1066. /* Whether to report invalid palette index; added at libng-1.5.10
  1067. * allowed - one of 0: disable; 1: enable
  1068. */
  1069. void PNGAPI
  1070. png_set_check_for_invalid_index(png_structp png_ptr, int allowed)
  1071. {
  1072. png_debug(1, "in png_set_check_for_invalid_index");
  1073. if (allowed != 0)
  1074. png_ptr->num_palette_max = 0;
  1075. else
  1076. png_ptr->num_palette_max = -1;
  1077. }
  1078. #endif
  1079. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */