blob: 7cd23ff39accabcf4a42c6548f961fc5d79481a0 [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngset.c - storage of image information into info struct
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson67ee8ce2011-12-22 08:21:00 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * The functions here are used during reads to store data from the file
14 * into the info struct, and during writes to store application data
15 * into the info struct for writing into the file. This abstracts the
16 * info struct and allows us to change the structure in the future.
17 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050023#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -060025png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -050026 png_const_color_16p background)
Andreas Dilger47a0c421997-05-16 02:46:07 -050027{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050028 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050029
John Bowlerfcd301d2011-12-28 21:34:27 -060030 if (png_ptr == NULL || info_ptr == NULL || background == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050031 return;
32
John Bowlerfcd301d2011-12-28 21:34:27 -060033 info_ptr->background = *background;
Andreas Dilger47a0c421997-05-16 02:46:07 -050034 info_ptr->valid |= PNG_INFO_bKGD;
35}
36#endif
37
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050038#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050039void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -060040png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060041 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
42 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
43 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060044{
John Bowlerb11b31a2012-03-21 07:55:46 -050045 png_xy xy;
46
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060047 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050048
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060049 if (png_ptr == NULL || info_ptr == NULL)
50 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050051
John Bowlerb11b31a2012-03-21 07:55:46 -050052 xy.redx = red_x;
53 xy.redy = red_y;
54 xy.greenx = green_x;
55 xy.greeny = green_y;
56 xy.bluex = blue_x;
57 xy.bluey = blue_y;
58 xy.whitex = white_x;
59 xy.whitey = white_y;
60
61 if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,
62 2/* override with app values*/))
63 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
64
65 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050067
John Bowler736f40f2011-08-25 16:19:44 -050068void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -060069png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
John Bowler736f40f2011-08-25 16:19:44 -050070 png_fixed_point int_red_X, png_fixed_point int_red_Y,
71 png_fixed_point int_red_Z, png_fixed_point int_green_X,
72 png_fixed_point int_green_Y, png_fixed_point int_green_Z,
73 png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
74 png_fixed_point int_blue_Z)
75{
76 png_XYZ XYZ;
John Bowler736f40f2011-08-25 16:19:44 -050077
78 png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
79
80 if (png_ptr == NULL || info_ptr == NULL)
81 return;
82
83 XYZ.redX = int_red_X;
84 XYZ.redY = int_red_Y;
85 XYZ.redZ = int_red_Z;
86 XYZ.greenX = int_green_X;
87 XYZ.greenY = int_green_Y;
88 XYZ.greenZ = int_green_Z;
89 XYZ.blueX = int_blue_X;
90 XYZ.blueY = int_blue_Y;
91 XYZ.blueZ = int_blue_Z;
92
John Bowlerb11b31a2012-03-21 07:55:46 -050093 if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace, &XYZ, 2))
94 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
John Bowler736f40f2011-08-25 16:19:44 -050095
John Bowlerb11b31a2012-03-21 07:55:46 -050096 png_colorspace_sync_info(png_ptr, info_ptr);
John Bowler736f40f2011-08-25 16:19:44 -050097}
98
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -050099# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500100void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600101png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500102 double white_x, double white_y, double red_x, double red_y,
103 double green_x, double green_y, double blue_x, double blue_y)
104{
105 png_set_cHRM_fixed(png_ptr, info_ptr,
106 png_fixed(png_ptr, white_x, "cHRM White X"),
107 png_fixed(png_ptr, white_y, "cHRM White Y"),
108 png_fixed(png_ptr, red_x, "cHRM Red X"),
109 png_fixed(png_ptr, red_y, "cHRM Red Y"),
110 png_fixed(png_ptr, green_x, "cHRM Green X"),
111 png_fixed(png_ptr, green_y, "cHRM Green Y"),
112 png_fixed(png_ptr, blue_x, "cHRM Blue X"),
113 png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
114}
John Bowler736f40f2011-08-25 16:19:44 -0500115
116void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600117png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
John Bowler736f40f2011-08-25 16:19:44 -0500118 double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
119 double blue_X, double blue_Y, double blue_Z)
120{
121 png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
122 png_fixed(png_ptr, red_X, "cHRM Red X"),
123 png_fixed(png_ptr, red_Y, "cHRM Red Y"),
124 png_fixed(png_ptr, red_Z, "cHRM Red Z"),
125 png_fixed(png_ptr, green_X, "cHRM Red X"),
126 png_fixed(png_ptr, green_Y, "cHRM Red Y"),
127 png_fixed(png_ptr, green_Z, "cHRM Red Z"),
128 png_fixed(png_ptr, blue_X, "cHRM Red X"),
129 png_fixed(png_ptr, blue_Y, "cHRM Red Y"),
130 png_fixed(png_ptr, blue_Z, "cHRM Red Z"));
131}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500132# endif /* PNG_FLOATING_POINT_SUPPORTED */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500133
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600134#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600135
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500136#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500137void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -0600138png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
139 png_fixed_point file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500140{
141 png_debug1(1, "in %s storage function", "gAMA");
142
143 if (png_ptr == NULL || info_ptr == NULL)
144 return;
145
John Bowlerd2f0bc22011-06-11 06:42:06 -0500146 /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
147 * occur. Since the fixed point representation is assymetrical it is
148 * possible for 1/gamma to overflow the limit of 21474 and this means the
149 * gamma value must be at least 5/100000 and hence at most 20000.0. For
150 * safety the limits here are a little narrower. The values are 0.00016 to
Glenn Randers-Pehrson8f4d6722012-05-23 12:30:59 -0500151 * 6250.0, which are truly ridiculous gamma values (and will produce
John Bowlerd2f0bc22011-06-11 06:42:06 -0500152 * displays that are all black or all white.)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500153 */
John Bowlerd2f0bc22011-06-11 06:42:06 -0500154 if (file_gamma < 16 || file_gamma > 625000000)
John Bowlerb11b31a2012-03-21 07:55:46 -0500155 png_app_error(png_ptr, "Out of range gamma value ignored");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500156
157 else
158 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500159 if (png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma,
160 2/* overrided with app value */))
161 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_gAMA;
162
163 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500164 }
165}
166
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500167# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500168void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600169png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500171 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500172 "png_set_gAMA"));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500174# endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500175#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500178void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600179png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
180 png_const_uint_16p hist)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500182 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600183
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500184 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500185
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600186 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500188
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500189 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600190 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500191 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500192 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600193 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500195 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600198 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500199
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500200 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
201 * version 1.2.1
202 */
John Bowler4f67e402011-12-28 08:43:37 -0600203 info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500204 PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500205
John Bowler4f67e402011-12-28 08:43:37 -0600206 if (info_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 {
208 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
209 return;
210 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600211
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600212 info_ptr->free_me |= PNG_FREE_HIST;
John Bowler4f67e402011-12-28 08:43:37 -0600213
214 for (i = 0; i < info_ptr->num_palette; i++)
215 info_ptr->hist[i] = hist[i];
216
217 info_ptr->valid |= PNG_INFO_hIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218}
219#endif
220
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500221void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600222png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500223 png_uint_32 width, png_uint_32 height, int bit_depth,
224 int color_type, int interlace_type, int compression_type,
225 int filter_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500227 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500228
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600229 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 return;
231
232 info_ptr->width = width;
233 info_ptr->height = height;
234 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500235 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 info_ptr->compression_type = (png_byte)compression_type;
237 info_ptr->filter_type = (png_byte)filter_type;
238 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500239
240 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
241 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
242 info_ptr->compression_type, info_ptr->filter_type);
243
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500244 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
245 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500246
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500247 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 else
251 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500252
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
254 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500255
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600257
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500258 /* Check for potential overflow */
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500259 if (width >
260 (PNG_UINT_32_MAX >> 3) /* 8-byte RRGGBBAA pixels */
261 - 48 /* bigrowbuf hack */
262 - 1 /* filter byte */
263 - 7*8 /* rounding of width to multiple of 8 pixels */
264 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500266 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268}
269
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500270#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500271void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600272png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600273 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500275 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500276
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600277 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 return;
279
280 info_ptr->x_offset = offset_x;
281 info_ptr->y_offset = offset_y;
282 info_ptr->offset_unit_type = (png_byte)unit_type;
283 info_ptr->valid |= PNG_INFO_oFFs;
284}
285#endif
286
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500287#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500288void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600289png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500290 png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
291 int nparams, png_const_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500292{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500293 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600294 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500296 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500297
John Bowlerfcd301d2011-12-28 21:34:27 -0600298 if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
299 || (nparams > 0 && params == NULL))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 return;
301
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500302 length = strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500303 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600304 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500306 /* TODO: validate format of calibration name and unit name */
307
308 /* Check that the type matches the specification. */
309 if (type < 0 || type > 3)
310 png_error(png_ptr, "Invalid pCAL equation type");
311
John Bowlerfcd301d2011-12-28 21:34:27 -0600312 if (nparams < 0 || nparams > 255)
313 png_error(png_ptr, "Invalid pCAL parameter count");
314
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500315 /* Validate params[nparams] */
316 for (i=0; i<nparams; ++i)
John Bowlerfcd301d2011-12-28 21:34:27 -0600317 if (params[i] == NULL ||
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500318 !png_check_fp_string(params[i], strlen(params[i])))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500319 png_error(png_ptr, "Invalid format for pCAL parameter");
320
John Bowlerfcd301d2011-12-28 21:34:27 -0600321 info_ptr->pcal_purpose = png_voidcast(png_charp,
322 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500323
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500324 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500326 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500327 return;
328 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500329
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500330 memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500332 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333 info_ptr->pcal_X0 = X0;
334 info_ptr->pcal_X1 = X1;
335 info_ptr->pcal_type = (png_byte)type;
336 info_ptr->pcal_nparams = (png_byte)nparams;
337
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500338 length = strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500339 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500340 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500341
John Bowlerfcd301d2011-12-28 21:34:27 -0600342 info_ptr->pcal_units = png_voidcast(png_charp,
343 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500344
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500345 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500347 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 return;
349 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500350
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500351 memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352
John Bowlerfcd301d2011-12-28 21:34:27 -0600353 info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500354 (png_size_t)((nparams + 1) * (sizeof (png_charp)))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500355
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500356 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500357 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500358 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 return;
360 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500361
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500362 memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363
364 for (i = 0; i < nparams; i++)
365 {
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500366 length = strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500367 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500368 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500369
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500370 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500371
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500372 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500374 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
375 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500376 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500377
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500378 memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 }
380
381 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500382 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383}
384#endif
385
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500386#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500387void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600388png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500389 int unit, png_const_charp swidth, png_const_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500391 png_size_t lengthw = 0, lengthh = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500393 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500394
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 if (png_ptr == NULL || info_ptr == NULL)
396 return;
397
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500398 /* Double check the unit (should never get here with an invalid
399 * unit unless this is an API call.)
400 */
401 if (unit != 1 && unit != 2)
402 png_error(png_ptr, "Invalid sCAL unit");
403
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500404 if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500405 swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500406 png_error(png_ptr, "Invalid sCAL width");
407
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500408 if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500409 sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500410 png_error(png_ptr, "Invalid sCAL height");
411
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600412 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500414 ++lengthw;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600416 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500417
John Bowlerfcd301d2011-12-28 21:34:27 -0600418 info_ptr->scal_s_width = png_voidcast(png_charp,
419 png_malloc_warn(png_ptr, lengthw));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500420
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500421 if (info_ptr->scal_s_width == NULL)
422 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500423 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500424 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500425 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500427 memcpy(info_ptr->scal_s_width, swidth, lengthw);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600428
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500429 ++lengthh;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600431 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500432
John Bowlerfcd301d2011-12-28 21:34:27 -0600433 info_ptr->scal_s_height = png_voidcast(png_charp,
434 png_malloc_warn(png_ptr, lengthh));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500436 if (info_ptr->scal_s_height == NULL)
437 {
438 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500439 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500440
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500441 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500442 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500443 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500444
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500445 memcpy(info_ptr->scal_s_height, sheight, lengthh);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500446
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600447 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500448 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600449}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500450
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500451# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500452void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600453png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
454 double width, double height)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500455{
456 png_debug1(1, "in %s storage function", "sCAL");
457
458 /* Check the arguments. */
459 if (width <= 0)
460 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500461
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500462 else if (height <= 0)
463 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500464
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500465 else
466 {
467 /* Convert 'width' and 'height' to ASCII. */
468 char swidth[PNG_sCAL_MAX_DIGITS+1];
469 char sheight[PNG_sCAL_MAX_DIGITS+1];
470
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500471 png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500472 PNG_sCAL_PRECISION);
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500473 png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500474 PNG_sCAL_PRECISION);
475
476 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
477 }
478}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500479# endif
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500480
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500481# ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500482void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600483png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500484 png_fixed_point width, png_fixed_point height)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500485{
486 png_debug1(1, "in %s storage function", "sCAL");
487
488 /* Check the arguments. */
489 if (width <= 0)
490 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500491
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500492 else if (height <= 0)
493 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500494
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500495 else
496 {
497 /* Convert 'width' and 'height' to ASCII. */
498 char swidth[PNG_sCAL_MAX_DIGITS+1];
499 char sheight[PNG_sCAL_MAX_DIGITS+1];
500
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500501 png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
502 png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500503
504 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
505 }
506}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500507# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600508#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600509
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500510#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500511void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600512png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600513 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500515 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500516
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600517 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 return;
519
520 info_ptr->x_pixels_per_unit = res_x;
521 info_ptr->y_pixels_per_unit = res_y;
522 info_ptr->phys_unit_type = (png_byte)unit_type;
523 info_ptr->valid |= PNG_INFO_pHYs;
524}
525#endif
526
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600528png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500529 png_const_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600531
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500532 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500533
John Bowlerfcd301d2011-12-28 21:34:27 -0600534 if (png_ptr == NULL || info_ptr == NULL || palette == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 return;
536
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600537 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500538 {
539 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600540 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500542 else
543 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600544 png_warning(png_ptr, "Invalid palette length");
545 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500546 }
547 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600548
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600549 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600550 * we do it for backward compatibility with the way the png_handle_tRNS
551 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600552 *
553 * 1.6.0: the above statement appears to be incorrect; something has to set
554 * the palette inside png_struct on read.
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600555 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600556 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500557
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600558 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500559 * of num_palette entries, in case of an invalid PNG file that has
560 * too-large sample values.
561 */
John Bowlerfcd301d2011-12-28 21:34:27 -0600562 png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500563 PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500564
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500565 memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600566 info_ptr->palette = png_ptr->palette;
567 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600568
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600569 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600570
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600571 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572}
573
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500574#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500575void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600576png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500577 png_const_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500579 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500580
John Bowlerfcd301d2011-12-28 21:34:27 -0600581 if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582 return;
583
John Bowlerfcd301d2011-12-28 21:34:27 -0600584 info_ptr->sig_bit = *sig_bit;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 info_ptr->valid |= PNG_INFO_sBIT;
586}
587#endif
588
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500589#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600591png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500593 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500594
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600595 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600596 return;
597
John Bowlerb11b31a2012-03-21 07:55:46 -0500598 (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent,
599 2/* app value overrides*/);
600
601 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600602}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600603
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600605png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson8d0bc0f2011-01-25 22:15:58 -0600606 int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600607{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500608 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500609
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600610 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611 return;
612
John Bowlerb11b31a2012-03-21 07:55:46 -0500613 if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent,
614 2/* app value overrides*/))
615 {
616 /* And cause the gAMA and cHRM to be written too */
617 info_ptr->colorspace.flags |=
618 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
619 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600620
John Bowlerb11b31a2012-03-21 07:55:46 -0500621 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600622}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600623#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600624
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600625
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500626#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500627void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600628png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500629 png_const_charp name, int compression_type,
630 png_const_bytep profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600631{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500632 png_charp new_iccp_name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500633 png_bytep new_iccp_profile;
John Bowlerf3f7e142011-09-09 07:32:37 -0500634 png_size_t length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500635
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500636 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500637
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600638 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
639 return;
640
John Bowlerb11b31a2012-03-21 07:55:46 -0500641 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
642 png_app_error(png_ptr, "Invalid iCCP compression method");
643
644 /* Set the colorspace first because this validates the profile; do not
645 * override previously set app cHRM or gAMA here (because likely as not the
646 * application knows better than libpng what the correct values are.)
647 */
648 {
649 int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
650 proflen, profile, 0/* do *not* override the app cHRM or gAMA */);
651
652 png_colorspace_sync_info(png_ptr, info_ptr);
653
654 /* Don't do any of the copying if the profile was bad, or inconsistent. */
655 if (!result)
656 return;
657
658 /* But do write the gAMA and cHRM chunks from the profile. */
659 info_ptr->colorspace.flags |=
660 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
661 }
662
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500663 length = strlen(name)+1;
John Bowlerfcd301d2011-12-28 21:34:27 -0600664 new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500665
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500666 if (new_iccp_name == NULL)
667 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500668 png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500669 return;
670 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500671
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500672 memcpy(new_iccp_name, name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -0600673 new_iccp_profile = png_voidcast(png_bytep,
674 png_malloc_warn(png_ptr, proflen));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500675
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500676 if (new_iccp_profile == NULL)
677 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500678 png_free(png_ptr, new_iccp_name);
679 png_benign_error(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600680 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500681 return;
682 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500683
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500684 memcpy(new_iccp_profile, profile, proflen);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500685
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500686 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500687
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600688 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500689 info_ptr->iccp_name = new_iccp_name;
690 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600691 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600692 info_ptr->valid |= PNG_INFO_iCCP;
693}
694#endif
695
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500696#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500697void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600698png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
699 png_const_textp text_ptr, int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500701 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500702 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500703
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500704 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500705 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500706}
707
708int /* PRIVATE */
John Bowler4f67e402011-12-28 08:43:37 -0600709png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500710 png_const_textp text_ptr, int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500711{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712 int i;
713
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500714 png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
715 (unsigned long)png_ptr->chunk_name);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600717 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500718 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719
720 /* Make sure we have enough space in the "text" array in info_struct
721 * to hold all of the incoming text_ptr objects.
722 */
723 if (info_ptr->num_text + num_text > info_ptr->max_text)
724 {
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500725 int old_max_text = info_ptr->max_text;
726 int old_num_text = info_ptr->num_text;
727
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 if (info_ptr->text != NULL)
729 {
730 png_textp old_text;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731
Andreas Dilger47a0c421997-05-16 02:46:07 -0500732 info_ptr->max_text = info_ptr->num_text + num_text + 8;
733 old_text = info_ptr->text;
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500734
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500735 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500736 (png_size_t)(info_ptr->max_text * (sizeof (png_text))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500737
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500738 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500739 {
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500740 /* Restore to previous condition */
741 info_ptr->max_text = old_max_text;
742 info_ptr->text = old_text;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500743 return(1);
744 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500745
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500746 memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500747 (sizeof (png_text))));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 png_free(png_ptr, old_text);
749 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500750
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751 else
752 {
753 info_ptr->max_text = num_text + 8;
754 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500755 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500756 (png_size_t)(info_ptr->max_text * (sizeof (png_text))));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500757 if (info_ptr->text == NULL)
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500758 {
759 /* Restore to previous condition */
760 info_ptr->num_text = old_num_text;
761 info_ptr->max_text = old_max_text;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500762 return(1);
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500763 }
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500764 info_ptr->free_me |= PNG_FREE_TEXT;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500766
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500767 png_debug1(3, "allocated %d entries for info_ptr->text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600768 info_ptr->max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500769 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 for (i = 0; i < num_text; i++)
771 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500772 png_size_t text_length, key_len;
773 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500774 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
775
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500776 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600777 continue;
778
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500779 if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
780 text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500781 {
782 png_warning(png_ptr, "text compression mode is out of range");
783 continue;
784 }
785
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500786 key_len = strlen(text_ptr[i].key);
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500787
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500788 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500789 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500790 lang_len = 0;
791 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500792 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500793
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500794 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500795# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600796 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500797 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600798
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500799 if (text_ptr[i].lang != NULL)
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500800 lang_len = strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500801
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500802 else
803 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500804
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 if (text_ptr[i].lang_key != NULL)
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500806 lang_key_len = strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500808 else
809 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600810 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500811# else /* PNG_iTXt_SUPPORTED */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500812 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500813 png_warning(png_ptr, "iTXt chunk not supported");
814 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500815 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500816# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500817
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500818 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600820 text_length = 0;
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500821# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500822 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600823 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500824
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500826# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600827 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500828 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500829
Andreas Dilger47a0c421997-05-16 02:46:07 -0500830 else
831 {
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500832 text_length = strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500833 textp->compression = text_ptr[i].compression;
834 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500835
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500836 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600837 (png_size_t)
838 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500839
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500840 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500841 return(1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500842
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600843 png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600844 (unsigned long)(png_uint_32)
845 (key_len + lang_len + lang_key_len + text_length + 4),
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600846 textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500847
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500848 memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500850
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600851 if (text_ptr[i].compression > 0)
852 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500853 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500854 memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500855 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500856 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500857 memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500858 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500859 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500861
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600862 else
863 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500864 textp->lang=NULL;
865 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500866 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600867 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500868
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500869 if (text_length)
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500870 memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600871 (png_size_t)(text_length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500872
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500873 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600874
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500875# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500876 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600877 {
878 textp->text_length = 0;
879 textp->itxt_length = text_length;
880 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500881
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600882 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500883# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600884 {
885 textp->text_length = text_length;
886 textp->itxt_length = 0;
887 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500888
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500890 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500892 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500893}
894#endif
895
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500896#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500897void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600898png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
899 png_const_timep mod_time)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500901 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500902
John Bowlerfcd301d2011-12-28 21:34:27 -0600903 if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600904 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500905 return;
906
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600907 if (mod_time->month == 0 || mod_time->month > 12 ||
908 mod_time->day == 0 || mod_time->day > 31 ||
909 mod_time->hour > 23 || mod_time->minute > 59 ||
910 mod_time->second > 60)
911 {
912 png_warning(png_ptr, "Ignoring invalid time value");
913 return;
914 }
915
John Bowlerfcd301d2011-12-28 21:34:27 -0600916 info_ptr->mod_time = *mod_time;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917 info_ptr->valid |= PNG_INFO_tIME;
918}
919#endif
920
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500922void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600923png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500924 png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500925{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500926 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500927
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600928 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929 return;
930
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500931 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600932 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500933 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500934 * we do it for backward compatibility with the way the png_handle_tRNS
935 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600936 *
937 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
938 * relies on png_set_tRNS storing the information in png_struct
939 * (otherwise it won't be there for the code in pngrtran.c).
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500940 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500941
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600942 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500943
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600944 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
John Bowler4f67e402011-12-28 08:43:37 -0600945 png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep,
946 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500947
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500948 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500949 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600950 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500952 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500953 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500954 int sample_max = (1 << info_ptr->bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500955
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500956 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
John Bowler4f67e402011-12-28 08:43:37 -0600957 trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500958 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
John Bowler4f67e402011-12-28 08:43:37 -0600959 (trans_color->red > sample_max ||
960 trans_color->green > sample_max ||
961 trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500962 png_warning(png_ptr,
963 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500964
John Bowlerfcd301d2011-12-28 21:34:27 -0600965 info_ptr->trans_color = *trans_color;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500966
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600967 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500968 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500969 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500970
Andreas Dilger47a0c421997-05-16 02:46:07 -0500971 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500972
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500973 if (num_trans != 0)
974 {
975 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500976 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500977 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978}
979#endif
980
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500981#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500982void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600983png_set_sPLT(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -0600984 png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600985/*
986 * entries - array of png_sPLT_t structures
987 * to be added to the list of palettes
988 * in the info structure.
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500989 *
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600990 * nentries - number of palette structures to be
991 * added.
992 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500994 png_sPLT_tp np;
995 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600996
John Bowlerfcd301d2011-12-28 21:34:27 -0600997 if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 ||
998 entries == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500999 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001000
John Bowlerfcd301d2011-12-28 21:34:27 -06001001 np = png_voidcast(png_sPLT_tp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001002 (info_ptr->splt_palettes_num + nentries) * (sizeof (png_sPLT_t))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001003
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001004 if (np == NULL)
1005 {
1006 png_warning(png_ptr, "No memory for sPLT palettes");
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001007 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001008 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001010 memcpy(np, info_ptr->splt_palettes,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001011 info_ptr->splt_palettes_num * (sizeof (png_sPLT_t)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001012
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001013 png_free(png_ptr, info_ptr->splt_palettes);
1014 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015
John Bowlerfcd301d2011-12-28 21:34:27 -06001016 /* TODO: fix this, it apparently leaves NULL entries in the event of OOM
1017 * below.
1018 */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001019 for (i = 0; i < nentries; i++)
1020 {
1021 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001022 png_const_sPLT_tp from = entries + i;
John Bowlerf3f7e142011-09-09 07:32:37 -05001023 png_size_t length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024
John Bowlerfcd301d2011-12-28 21:34:27 -06001025 /* In event of error below the name and entries fields must be set to
1026 * NULL, otherwise libpng will crash later on while trying to free the
1027 * uninitialized pointers.
1028 */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001029 memset(to, 0, (sizeof *to));
John Bowlerfcd301d2011-12-28 21:34:27 -06001030
1031 if (from->name == NULL || from->entries == NULL)
1032 continue;
1033
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -05001034 length = strlen(from->name) + 1;
John Bowlerfcd301d2011-12-28 21:34:27 -06001035 to->name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001036
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037 if (to->name == NULL)
1038 {
1039 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001040 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001041 continue;
1042 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001043
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001044 memcpy(to->name, from->name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -06001045 to->entries = png_voidcast(png_sPLT_entryp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001046 from->nentries * (sizeof (png_sPLT_entry))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001047
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048 if (to->entries == NULL)
1049 {
John Bowlerfcd301d2011-12-28 21:34:27 -06001050 png_warning(png_ptr, "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001051 png_free(png_ptr, to->name);
1052 to->name = NULL;
1053 continue;
1054 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001055
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001056 memcpy(to->entries, from->entries,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001057 from->nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001058
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001059 to->nentries = from->nentries;
1060 to->depth = from->depth;
1061 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001062
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001063 info_ptr->splt_palettes = np;
1064 info_ptr->splt_palettes_num += nentries;
1065 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001066 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067}
1068#endif /* PNG_sPLT_SUPPORTED */
1069
John Bowlere9567512012-08-15 22:53:00 -05001070#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1071static png_byte
1072check_location(png_const_structrp png_ptr, unsigned int location)
1073{
1074 location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1075
1076 /* New in 1.6.0; copy the location and check it. This is an API
1077 * change, previously the app had to use the
1078 * png_set_unknown_chunk_location API below for each chunk.
1079 */
1080 if (location == 0 && !(png_ptr->mode & PNG_IS_READ_STRUCT))
1081 {
1082 /* Write struct, so unknown chunks come from the app */
1083 png_app_warning(png_ptr,
1084 "png_set_unknown_chunks now expects a valid location");
1085 /* Use the old behavior */
1086 location = png_ptr->mode &
1087 (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1088 }
1089
1090 if (location == 0)
1091 png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1092
1093 /* Now reduce the location to the top-most set bit by removing each least
1094 * significant bit in turn.
1095 */
1096 while (location != (location & -location))
1097 location &= (png_byte)~(location & -location);
1098
1099 /* The cast is safe because 'location' is a bit mask and only the low four
1100 * bits are significant.
1101 */
1102 return (png_byte)location;
1103}
1104
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001105void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001106png_set_unknown_chunks(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -06001107 png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001108{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001109 png_unknown_chunkp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001110
John Bowlere9567512012-08-15 22:53:00 -05001111 if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001112 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001113
John Bowlere9567512012-08-15 22:53:00 -05001114 /* Prior to 1.6.0 this code used png_malloc_warn, however this meant that
1115 * unknown critical chunks could be lost with just a warning resulting in
1116 * undefined behavior. Changing to png_malloc fixes this by producing a
1117 * png_error. The (png_size_t) cast was also removed as it hides a potential
1118 * overflow.
1119 *
1120 * TODO: fix the potential overflow in the multiply
1121 */
1122 np = png_voidcast(png_unknown_chunkp, png_malloc(png_ptr,
1123 (info_ptr->unknown_chunks_num + (unsigned int)num_unknowns) *
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001124 (sizeof (png_unknown_chunk))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001125
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001126 memcpy(np, info_ptr->unknown_chunks,
John Bowlere9567512012-08-15 22:53:00 -05001127 info_ptr->unknown_chunks_num * (sizeof (png_unknown_chunk)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001128
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001129 png_free(png_ptr, info_ptr->unknown_chunks);
John Bowlere9567512012-08-15 22:53:00 -05001130 info_ptr->unknown_chunks = np; /* safe because it is initialized */
1131 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001132
John Bowlere9567512012-08-15 22:53:00 -05001133 np += info_ptr->unknown_chunks_num;
1134
1135 /* Increment unknown_chunks_num each time round the loop to protect the
1136 * just-allocated chunk data.
1137 */
1138 for (; --num_unknowns >= 0;
1139 ++np, ++unknowns, ++(info_ptr->unknown_chunks_num))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001140 {
John Bowlere9567512012-08-15 22:53:00 -05001141 memcpy(np->name, unknowns->name, (sizeof unknowns->name));
1142 np->name[(sizeof np->name)-1] = '\0';
1143 np->size = unknowns->size;
1144 np->location = check_location(png_ptr, unknowns->location);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001145
John Bowlere9567512012-08-15 22:53:00 -05001146 if (unknowns->size == 0)
1147 np->data = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001148
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001149 else
1150 {
John Bowlere9567512012-08-15 22:53:00 -05001151 /* png_error is safe here because the list is stored in png_ptr */
1152 np->data = png_voidcast(png_bytep,
1153 png_malloc(png_ptr, unknowns->size));
1154 memcpy(np->data, unknowns->data, unknowns->size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001155 }
1156 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001157}
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001158
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001159void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001160png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001161 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001162{
John Bowlere9567512012-08-15 22:53:00 -05001163 /* This API is pretty pointless in 1.6.0 because the location can be set
1164 * before the call to png_set_unknown_chunks.
1165 *
1166 * TODO: add a png_app_warning in 1.7
1167 */
1168 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1169 (unsigned int)chunk < info_ptr->unknown_chunks_num)
1170 {
1171 if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1172 {
1173 png_app_error(png_ptr, "invalid unknown chunk location");
1174 /* Fake out the pre 1.6.0 behavior: */
1175 if ((location & PNG_HAVE_IDAT)) /* undocumented! */
1176 location = PNG_AFTER_IDAT;
1177
1178 else
1179 location = PNG_HAVE_IHDR; /* also undocumented */
1180 }
1181
1182 info_ptr->unknown_chunks[chunk].location =
1183 check_location(png_ptr, (png_byte)location);
1184 }
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001185}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001186#endif
1187
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001188
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001189#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001190png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001191png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001192{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001193 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001194
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001195 if (png_ptr == NULL)
John Bowler4f67e402011-12-28 08:43:37 -06001196 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001197
John Bowler4f67e402011-12-28 08:43:37 -06001198 png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001199
John Bowler4f67e402011-12-28 08:43:37 -06001200 return png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001201}
1202#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001203
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001204#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -05001205static unsigned int
1206add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1207{
1208 unsigned int i;
1209
1210 /* Utility function: update the 'keep' state of a chunk if it is already in
1211 * the list, otherwise add it to the list.
1212 */
1213 for (i=0; i<count; ++i, list += 5) if (memcmp(list, add, 4) == 0)
1214 {
1215 list[4] = (png_byte)keep;
1216 return count;
1217 }
1218
1219 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1220 {
1221 ++count;
1222 memcpy(list, add, 4);
1223 list[4] = (png_byte)keep;
1224 }
1225
1226 return count;
1227}
1228
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001229void PNGAPI
John Bowlere9567512012-08-15 22:53:00 -05001230png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
John Bowlerfcd301d2011-12-28 21:34:27 -06001231 png_const_bytep chunk_list, int num_chunksIn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001232{
John Bowlere9567512012-08-15 22:53:00 -05001233 png_bytep new_list;
1234 unsigned int num_chunks, old_num_chunks;
1235
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001236 if (png_ptr == NULL)
1237 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001238
John Bowlere9567512012-08-15 22:53:00 -05001239 if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
John Bowleraa9dea52012-08-10 19:04:08 -05001240 {
John Bowlere9567512012-08-15 22:53:00 -05001241 png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
1242 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001243 }
1244
John Bowlerfcd301d2011-12-28 21:34:27 -06001245 if (num_chunksIn <= 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001246 {
John Bowlere9567512012-08-15 22:53:00 -05001247 png_ptr->unknown_default = keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001248
John Bowlere9567512012-08-15 22:53:00 -05001249 /* '0' means just set the flags, so stop here */
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001250 if (num_chunksIn == 0)
1251 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001252 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001253
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001254 if (num_chunksIn < 0)
John Bowleraa9dea52012-08-10 19:04:08 -05001255 {
1256 /* Ignore all unknown chunks and all chunks recognized by
1257 * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1258 */
1259 static PNG_CONST png_byte chunks_to_ignore[] = {
1260 98, 75, 71, 68, '\0', /* bKGD */
1261 99, 72, 82, 77, '\0', /* cHRM */
1262 103, 65, 77, 65, '\0', /* gAMA */
1263 104, 73, 83, 84, '\0', /* hIST */
1264 105, 67, 67, 80, '\0', /* iCCP */
1265 105, 84, 88, 116, '\0', /* iTXt */
1266 111, 70, 70, 115, '\0', /* oFFs */
1267 112, 67, 65, 76, '\0', /* pCAL */
1268 112, 72, 89, 115, '\0', /* pHYs */
1269 115, 66, 73, 84, '\0', /* sBIT */
1270 115, 67, 65, 76, '\0', /* sCAL */
1271 115, 80, 76, 84, '\0', /* sPLT */
1272 115, 84, 69, 82, '\0', /* sTER */
1273 115, 82, 71, 66, '\0', /* sRGB */
1274 116, 69, 88, 116, '\0', /* tEXt */
1275 116, 73, 77, 69, '\0', /* tIME */
1276 122, 84, 88, 116, '\0' /* zTXt */
1277 };
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001278
John Bowleraa9dea52012-08-10 19:04:08 -05001279 chunk_list = chunks_to_ignore;
1280 num_chunks = (sizeof chunks_to_ignore)/5;
1281 }
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001282
John Bowleraa9dea52012-08-10 19:04:08 -05001283 else /* num_chunksIn > 0 */
1284 {
1285 if (chunk_list == NULL)
John Bowlere9567512012-08-15 22:53:00 -05001286 {
1287 /* Prior to 1.6.0 this was silently ignored, now it is an app_error
1288 * which can be switched off.
1289 */
1290 png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
John Bowleraa9dea52012-08-10 19:04:08 -05001291 return;
John Bowlere9567512012-08-15 22:53:00 -05001292 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001293
John Bowleraa9dea52012-08-10 19:04:08 -05001294 num_chunks = num_chunksIn;
1295 }
John Bowlerfcd301d2011-12-28 21:34:27 -06001296
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001297 old_num_chunks = png_ptr->num_chunk_list;
John Bowlere9567512012-08-15 22:53:00 -05001298 if (png_ptr->chunk_list == NULL)
1299 old_num_chunks = 0;
John Bowleraa9dea52012-08-10 19:04:08 -05001300
1301 /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1302 */
1303 if (num_chunks + old_num_chunks > UINT_MAX/5)
1304 {
1305 png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
1306 return;
1307 }
1308
John Bowlere9567512012-08-15 22:53:00 -05001309 /* If these chunks are being reset to the default then no more memory is
1310 * required because add_one_chunk above doesn't extend the list if the 'keep'
1311 * parameter is the default.
1312 */
1313 if (keep)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001314 {
John Bowlere9567512012-08-15 22:53:00 -05001315 new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1316 5 * (num_chunks + old_num_chunks)));
1317
1318 if (old_num_chunks > 0)
1319 memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001320 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001321
John Bowlere9567512012-08-15 22:53:00 -05001322 else if (old_num_chunks > 0)
1323 new_list = png_ptr->chunk_list;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001324
John Bowlere9567512012-08-15 22:53:00 -05001325 else
1326 new_list = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001327
John Bowlere9567512012-08-15 22:53:00 -05001328 /* Add the new chunks together with each one's handling code. If the chunk
1329 * already exists the code is updated, otherwise the chunk is added to the
1330 * end. (In libpng 1.6.0 order no longer matters because this code enforces
1331 * the earlier convention that the last setting is the one that is used.)
1332 */
1333 if (new_list != NULL)
1334 {
1335 png_const_bytep inlist;
1336 png_bytep outlist;
1337 unsigned int i;
1338
1339 for (i=0; i<num_chunks; ++i)
1340 old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1341 chunk_list+5*i, keep);
1342
1343 /* Now remove any spurious 'default' entries. */
1344 num_chunks = 0;
1345 for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1346 if (inlist[4])
1347 {
1348 if (outlist != inlist)
1349 memcpy(outlist, inlist, 5);
1350 outlist += 5;
1351 ++num_chunks;
1352 }
1353
1354 /* This means the application has removed all the specialized handling. */
1355 if (num_chunks == 0)
1356 {
1357 if (png_ptr->chunk_list != new_list)
1358 png_free(png_ptr, new_list);
1359
1360 new_list = NULL;
1361 }
1362 }
1363
1364 else
1365 num_chunks = 0;
1366
1367 png_ptr->num_chunk_list = num_chunks;
1368
1369 if (png_ptr->chunk_list != new_list)
1370 {
1371 if (png_ptr->chunk_list != NULL)
1372 png_free(png_ptr, png_ptr->chunk_list);
1373
1374 png_ptr->chunk_list = new_list;
1375 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001376}
1377#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001378
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001379#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001380void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001381png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001382 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001383{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001384 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001385
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001386 if (png_ptr == NULL)
1387 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001388
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001389 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1390 png_ptr->user_chunk_ptr = user_chunk_ptr;
1391}
1392#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001393
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001394#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001395void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001396png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1397 png_bytepp row_pointers)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001398{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001399 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001400
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001401 if (png_ptr == NULL || info_ptr == NULL)
1402 return;
1403
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001404 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001405 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001406
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001407 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001408
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001409 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001410 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001411}
1412#endif
1413
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001414void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001415png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001416{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001417 if (png_ptr == NULL)
1418 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001419
John Bowlerb5d00512012-03-09 09:15:18 -06001420 if (size == 0 || size > PNG_UINT_31_MAX)
1421 png_error(png_ptr, "invalid compression buffer size");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001422
John Bowlerb5d00512012-03-09 09:15:18 -06001423# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1424 if (png_ptr->mode & PNG_IS_READ_STRUCT)
1425 {
1426 png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1427 return;
1428 }
1429# endif
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001430
John Bowler134c5762012-08-10 10:46:45 -05001431# ifdef PNG_WRITE_SUPPORTED
John Bowlerb5d00512012-03-09 09:15:18 -06001432 if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
1433 {
1434 if (png_ptr->zowner != 0)
1435 {
1436 png_warning(png_ptr,
1437 "Compression buffer size cannot be changed because it is in use");
1438 return;
1439 }
John Bowleraa9dea52012-08-10 19:04:08 -05001440
John Bowlerb5d00512012-03-09 09:15:18 -06001441 if (size > ZLIB_IO_MAX)
1442 {
1443 png_warning(png_ptr,
1444 "Compression buffer size limited to system maximum");
1445 size = ZLIB_IO_MAX; /* must fit */
1446 }
John Bowleraa9dea52012-08-10 19:04:08 -05001447
John Bowlerb5d00512012-03-09 09:15:18 -06001448 else if (size < 6)
1449 {
1450 /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1451 * if this is permitted.
1452 */
1453 png_warning(png_ptr,
1454 "Compression buffer size cannot be reduced below 6");
1455 return;
1456 }
John Bowleraa9dea52012-08-10 19:04:08 -05001457
John Bowlerb5d00512012-03-09 09:15:18 -06001458 if (png_ptr->zbuffer_size != size)
1459 {
1460 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1461 png_ptr->zbuffer_size = (uInt)size;
1462 }
1463 }
1464# endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001465}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001466
1467void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001468png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001469{
1470 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001471 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001472}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001473
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001474
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001475#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001476/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001477void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001478png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001479 png_uint_32 user_height_max)
1480{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481 /* Images with dimensions larger than these limits will be
1482 * rejected by png_set_IHDR(). To accept any PNG datastream
1483 * regardless of dimensions, set both limits to 0x7ffffffL.
1484 */
1485 if (png_ptr == NULL)
1486 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001487
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001488 png_ptr->user_width_max = user_width_max;
1489 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001490}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001491
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001492/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001493void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001494png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001495{
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001496 if (png_ptr)
1497 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1498}
1499
1500/* This function was added to libpng 1.4.1 */
1501void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001502png_set_chunk_malloc_max (png_structrp png_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001503 png_alloc_size_t user_chunk_malloc_max)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001504{
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001505 if (png_ptr)
1506 png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001507}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001508#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1509
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001510
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001511#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001512void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001513png_set_benign_errors(png_structrp png_ptr, int allowed)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001514{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001515 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001516
Glenn Randers-Pehrsondd6ecbe2012-03-16 07:33:38 -05001517 /* If allowed is 1, png_benign_error() is treated as a warning.
1518 *
1519 * If allowed is 0, png_benign_error() is treated as an error (which
1520 * is the default behavior if png_set_benign_errors() is not called).
1521 */
1522
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001523 if (allowed)
Glenn Randers-Pehrsonce9d8a32012-03-18 22:39:52 -05001524 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1525 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001526
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001527 else
Glenn Randers-Pehrsonce9d8a32012-03-18 22:39:52 -05001528 png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1529 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001530}
1531#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001532
Glenn Randers-Pehrsone243a1f2012-04-13 12:35:21 -05001533#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001534 /* Do not report invalid palette index; added at libng-1.5.10 */
1535void PNGAPI
1536png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1537{
1538 png_debug(1, "in png_set_check_for_invalid_index");
1539
Glenn Randers-Pehrson945cb1f2012-03-10 08:48:04 -06001540 if (allowed > 0)
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001541 png_ptr->num_palette_max = 0;
1542
1543 else
1544 png_ptr->num_palette_max = -1;
1545}
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001546#endif
1547
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001548#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */