pngset.c 39 KB

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