annotate lib/libpng/pngset.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents 6e178010fc29
children
rev   line source
2296
Ritor1
parents:
diff changeset
1
Ritor1
parents:
diff changeset
2 /* pngset.c - storage of image information into info struct
Ritor1
parents:
diff changeset
3 *
Ritor1
parents:
diff changeset
4 * Last changed in libpng 1.6.8 [December 19, 2013]
Ritor1
parents:
diff changeset
5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
Ritor1
parents:
diff changeset
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Ritor1
parents:
diff changeset
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Ritor1
parents:
diff changeset
8 *
Ritor1
parents:
diff changeset
9 * This code is released under the libpng license.
Ritor1
parents:
diff changeset
10 * For conditions of distribution and use, see the disclaimer
Ritor1
parents:
diff changeset
11 * and license in png.h
Ritor1
parents:
diff changeset
12 *
Ritor1
parents:
diff changeset
13 * The functions here are used during reads to store data from the file
Ritor1
parents:
diff changeset
14 * into the info struct, and during writes to store application data
Ritor1
parents:
diff changeset
15 * into the info struct for writing into the file. This abstracts the
Ritor1
parents:
diff changeset
16 * info struct and allows us to change the structure in the future.
Ritor1
parents:
diff changeset
17 */
Ritor1
parents:
diff changeset
18
Ritor1
parents:
diff changeset
19 #include "pngpriv.h"
Ritor1
parents:
diff changeset
20
Ritor1
parents:
diff changeset
21 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Ritor1
parents:
diff changeset
22
Ritor1
parents:
diff changeset
23 #ifdef PNG_bKGD_SUPPORTED
Ritor1
parents:
diff changeset
24 void PNGAPI
Ritor1
parents:
diff changeset
25 png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
26 png_const_color_16p background)
Ritor1
parents:
diff changeset
27 {
Ritor1
parents:
diff changeset
28 png_debug1(1, "in %s storage function", "bKGD");
Ritor1
parents:
diff changeset
29
Ritor1
parents:
diff changeset
30 if (png_ptr == NULL || info_ptr == NULL || background == NULL)
Ritor1
parents:
diff changeset
31 return;
Ritor1
parents:
diff changeset
32
Ritor1
parents:
diff changeset
33 info_ptr->background = *background;
Ritor1
parents:
diff changeset
34 info_ptr->valid |= PNG_INFO_bKGD;
Ritor1
parents:
diff changeset
35 }
Ritor1
parents:
diff changeset
36 #endif
Ritor1
parents:
diff changeset
37
Ritor1
parents:
diff changeset
38 #ifdef PNG_cHRM_SUPPORTED
Ritor1
parents:
diff changeset
39 void PNGFAPI
Ritor1
parents:
diff changeset
40 png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
41 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
Ritor1
parents:
diff changeset
42 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
Ritor1
parents:
diff changeset
43 png_fixed_point blue_x, png_fixed_point blue_y)
Ritor1
parents:
diff changeset
44 {
Ritor1
parents:
diff changeset
45 png_xy xy;
Ritor1
parents:
diff changeset
46
Ritor1
parents:
diff changeset
47 png_debug1(1, "in %s storage function", "cHRM fixed");
Ritor1
parents:
diff changeset
48
Ritor1
parents:
diff changeset
49 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
50 return;
Ritor1
parents:
diff changeset
51
Ritor1
parents:
diff changeset
52 xy.redx = red_x;
Ritor1
parents:
diff changeset
53 xy.redy = red_y;
Ritor1
parents:
diff changeset
54 xy.greenx = green_x;
Ritor1
parents:
diff changeset
55 xy.greeny = green_y;
Ritor1
parents:
diff changeset
56 xy.bluex = blue_x;
Ritor1
parents:
diff changeset
57 xy.bluey = blue_y;
Ritor1
parents:
diff changeset
58 xy.whitex = white_x;
Ritor1
parents:
diff changeset
59 xy.whitey = white_y;
Ritor1
parents:
diff changeset
60
Ritor1
parents:
diff changeset
61 if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,
Ritor1
parents:
diff changeset
62 2/* override with app values*/))
Ritor1
parents:
diff changeset
63 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
Ritor1
parents:
diff changeset
64
Ritor1
parents:
diff changeset
65 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
66 }
Ritor1
parents:
diff changeset
67
Ritor1
parents:
diff changeset
68 void PNGFAPI
Ritor1
parents:
diff changeset
69 png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
70 png_fixed_point int_red_X, png_fixed_point int_red_Y,
Ritor1
parents:
diff changeset
71 png_fixed_point int_red_Z, png_fixed_point int_green_X,
Ritor1
parents:
diff changeset
72 png_fixed_point int_green_Y, png_fixed_point int_green_Z,
Ritor1
parents:
diff changeset
73 png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
Ritor1
parents:
diff changeset
74 png_fixed_point int_blue_Z)
Ritor1
parents:
diff changeset
75 {
Ritor1
parents:
diff changeset
76 png_XYZ XYZ;
Ritor1
parents:
diff changeset
77
Ritor1
parents:
diff changeset
78 png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
Ritor1
parents:
diff changeset
79
Ritor1
parents:
diff changeset
80 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
81 return;
Ritor1
parents:
diff changeset
82
Ritor1
parents:
diff changeset
83 XYZ.red_X = int_red_X;
Ritor1
parents:
diff changeset
84 XYZ.red_Y = int_red_Y;
Ritor1
parents:
diff changeset
85 XYZ.red_Z = int_red_Z;
Ritor1
parents:
diff changeset
86 XYZ.green_X = int_green_X;
Ritor1
parents:
diff changeset
87 XYZ.green_Y = int_green_Y;
Ritor1
parents:
diff changeset
88 XYZ.green_Z = int_green_Z;
Ritor1
parents:
diff changeset
89 XYZ.blue_X = int_blue_X;
Ritor1
parents:
diff changeset
90 XYZ.blue_Y = int_blue_Y;
Ritor1
parents:
diff changeset
91 XYZ.blue_Z = int_blue_Z;
Ritor1
parents:
diff changeset
92
Ritor1
parents:
diff changeset
93 if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace, &XYZ, 2))
Ritor1
parents:
diff changeset
94 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
Ritor1
parents:
diff changeset
95
Ritor1
parents:
diff changeset
96 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
97 }
Ritor1
parents:
diff changeset
98
Ritor1
parents:
diff changeset
99 # ifdef PNG_FLOATING_POINT_SUPPORTED
Ritor1
parents:
diff changeset
100 void PNGAPI
Ritor1
parents:
diff changeset
101 png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
102 double white_x, double white_y, double red_x, double red_y,
Ritor1
parents:
diff changeset
103 double green_x, double green_y, double blue_x, double blue_y)
Ritor1
parents:
diff changeset
104 {
Ritor1
parents:
diff changeset
105 png_set_cHRM_fixed(png_ptr, info_ptr,
Ritor1
parents:
diff changeset
106 png_fixed(png_ptr, white_x, "cHRM White X"),
Ritor1
parents:
diff changeset
107 png_fixed(png_ptr, white_y, "cHRM White Y"),
Ritor1
parents:
diff changeset
108 png_fixed(png_ptr, red_x, "cHRM Red X"),
Ritor1
parents:
diff changeset
109 png_fixed(png_ptr, red_y, "cHRM Red Y"),
Ritor1
parents:
diff changeset
110 png_fixed(png_ptr, green_x, "cHRM Green X"),
Ritor1
parents:
diff changeset
111 png_fixed(png_ptr, green_y, "cHRM Green Y"),
Ritor1
parents:
diff changeset
112 png_fixed(png_ptr, blue_x, "cHRM Blue X"),
Ritor1
parents:
diff changeset
113 png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
Ritor1
parents:
diff changeset
114 }
Ritor1
parents:
diff changeset
115
Ritor1
parents:
diff changeset
116 void PNGAPI
Ritor1
parents:
diff changeset
117 png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
Ritor1
parents:
diff changeset
118 double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
Ritor1
parents:
diff changeset
119 double blue_X, double blue_Y, double blue_Z)
Ritor1
parents:
diff changeset
120 {
Ritor1
parents:
diff changeset
121 png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
Ritor1
parents:
diff changeset
122 png_fixed(png_ptr, red_X, "cHRM Red X"),
Ritor1
parents:
diff changeset
123 png_fixed(png_ptr, red_Y, "cHRM Red Y"),
Ritor1
parents:
diff changeset
124 png_fixed(png_ptr, red_Z, "cHRM Red Z"),
Ritor1
parents:
diff changeset
125 png_fixed(png_ptr, green_X, "cHRM Red X"),
Ritor1
parents:
diff changeset
126 png_fixed(png_ptr, green_Y, "cHRM Red Y"),
Ritor1
parents:
diff changeset
127 png_fixed(png_ptr, green_Z, "cHRM Red Z"),
Ritor1
parents:
diff changeset
128 png_fixed(png_ptr, blue_X, "cHRM Red X"),
Ritor1
parents:
diff changeset
129 png_fixed(png_ptr, blue_Y, "cHRM Red Y"),
Ritor1
parents:
diff changeset
130 png_fixed(png_ptr, blue_Z, "cHRM Red Z"));
Ritor1
parents:
diff changeset
131 }
Ritor1
parents:
diff changeset
132 # endif /* PNG_FLOATING_POINT_SUPPORTED */
Ritor1
parents:
diff changeset
133
Ritor1
parents:
diff changeset
134 #endif /* PNG_cHRM_SUPPORTED */
Ritor1
parents:
diff changeset
135
Ritor1
parents:
diff changeset
136 #ifdef PNG_gAMA_SUPPORTED
Ritor1
parents:
diff changeset
137 void PNGFAPI
Ritor1
parents:
diff changeset
138 png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
139 png_fixed_point file_gamma)
Ritor1
parents:
diff changeset
140 {
Ritor1
parents:
diff changeset
141 png_debug1(1, "in %s storage function", "gAMA");
Ritor1
parents:
diff changeset
142
Ritor1
parents:
diff changeset
143 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
144 return;
Ritor1
parents:
diff changeset
145
Ritor1
parents:
diff changeset
146 png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);
Ritor1
parents:
diff changeset
147 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
148 }
Ritor1
parents:
diff changeset
149
Ritor1
parents:
diff changeset
150 # ifdef PNG_FLOATING_POINT_SUPPORTED
Ritor1
parents:
diff changeset
151 void PNGAPI
Ritor1
parents:
diff changeset
152 png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
Ritor1
parents:
diff changeset
153 {
Ritor1
parents:
diff changeset
154 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
Ritor1
parents:
diff changeset
155 "png_set_gAMA"));
Ritor1
parents:
diff changeset
156 }
Ritor1
parents:
diff changeset
157 # endif
Ritor1
parents:
diff changeset
158 #endif
Ritor1
parents:
diff changeset
159
Ritor1
parents:
diff changeset
160 #ifdef PNG_hIST_SUPPORTED
Ritor1
parents:
diff changeset
161 void PNGAPI
Ritor1
parents:
diff changeset
162 png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
163 png_const_uint_16p hist)
Ritor1
parents:
diff changeset
164 {
Ritor1
parents:
diff changeset
165 int i;
Ritor1
parents:
diff changeset
166
Ritor1
parents:
diff changeset
167 png_debug1(1, "in %s storage function", "hIST");
Ritor1
parents:
diff changeset
168
Ritor1
parents:
diff changeset
169 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
170 return;
Ritor1
parents:
diff changeset
171
Ritor1
parents:
diff changeset
172 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Ritor1
parents:
diff changeset
173 > PNG_MAX_PALETTE_LENGTH)
Ritor1
parents:
diff changeset
174 {
Ritor1
parents:
diff changeset
175 png_warning(png_ptr,
Ritor1
parents:
diff changeset
176 "Invalid palette size, hIST allocation skipped");
Ritor1
parents:
diff changeset
177
Ritor1
parents:
diff changeset
178 return;
Ritor1
parents:
diff changeset
179 }
Ritor1
parents:
diff changeset
180
Ritor1
parents:
diff changeset
181 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Ritor1
parents:
diff changeset
182
Ritor1
parents:
diff changeset
183 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
Ritor1
parents:
diff changeset
184 * version 1.2.1
Ritor1
parents:
diff changeset
185 */
Ritor1
parents:
diff changeset
186 info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
Ritor1
parents:
diff changeset
187 PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
Ritor1
parents:
diff changeset
188
Ritor1
parents:
diff changeset
189 if (info_ptr->hist == NULL)
Ritor1
parents:
diff changeset
190 {
Ritor1
parents:
diff changeset
191 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
Ritor1
parents:
diff changeset
192 return;
Ritor1
parents:
diff changeset
193 }
Ritor1
parents:
diff changeset
194
Ritor1
parents:
diff changeset
195 info_ptr->free_me |= PNG_FREE_HIST;
Ritor1
parents:
diff changeset
196
Ritor1
parents:
diff changeset
197 for (i = 0; i < info_ptr->num_palette; i++)
Ritor1
parents:
diff changeset
198 info_ptr->hist[i] = hist[i];
Ritor1
parents:
diff changeset
199
Ritor1
parents:
diff changeset
200 info_ptr->valid |= PNG_INFO_hIST;
Ritor1
parents:
diff changeset
201 }
Ritor1
parents:
diff changeset
202 #endif
Ritor1
parents:
diff changeset
203
Ritor1
parents:
diff changeset
204 void PNGAPI
Ritor1
parents:
diff changeset
205 png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
206 png_uint_32 width, png_uint_32 height, int bit_depth,
Ritor1
parents:
diff changeset
207 int color_type, int interlace_type, int compression_type,
Ritor1
parents:
diff changeset
208 int filter_type)
Ritor1
parents:
diff changeset
209 {
Ritor1
parents:
diff changeset
210 png_debug1(1, "in %s storage function", "IHDR");
Ritor1
parents:
diff changeset
211
Ritor1
parents:
diff changeset
212 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
213 return;
Ritor1
parents:
diff changeset
214
Ritor1
parents:
diff changeset
215 info_ptr->width = width;
Ritor1
parents:
diff changeset
216 info_ptr->height = height;
Ritor1
parents:
diff changeset
217 info_ptr->bit_depth = (png_byte)bit_depth;
Ritor1
parents:
diff changeset
218 info_ptr->color_type = (png_byte)color_type;
Ritor1
parents:
diff changeset
219 info_ptr->compression_type = (png_byte)compression_type;
Ritor1
parents:
diff changeset
220 info_ptr->filter_type = (png_byte)filter_type;
Ritor1
parents:
diff changeset
221 info_ptr->interlace_type = (png_byte)interlace_type;
Ritor1
parents:
diff changeset
222
Ritor1
parents:
diff changeset
223 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
Ritor1
parents:
diff changeset
224 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
Ritor1
parents:
diff changeset
225 info_ptr->compression_type, info_ptr->filter_type);
Ritor1
parents:
diff changeset
226
Ritor1
parents:
diff changeset
227 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Ritor1
parents:
diff changeset
228 info_ptr->channels = 1;
Ritor1
parents:
diff changeset
229
Ritor1
parents:
diff changeset
230 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Ritor1
parents:
diff changeset
231 info_ptr->channels = 3;
Ritor1
parents:
diff changeset
232
Ritor1
parents:
diff changeset
233 else
Ritor1
parents:
diff changeset
234 info_ptr->channels = 1;
Ritor1
parents:
diff changeset
235
Ritor1
parents:
diff changeset
236 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
Ritor1
parents:
diff changeset
237 info_ptr->channels++;
Ritor1
parents:
diff changeset
238
Ritor1
parents:
diff changeset
239 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Ritor1
parents:
diff changeset
240
Ritor1
parents:
diff changeset
241 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Ritor1
parents:
diff changeset
242 }
Ritor1
parents:
diff changeset
243
Ritor1
parents:
diff changeset
244 #ifdef PNG_oFFs_SUPPORTED
Ritor1
parents:
diff changeset
245 void PNGAPI
Ritor1
parents:
diff changeset
246 png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
247 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Ritor1
parents:
diff changeset
248 {
Ritor1
parents:
diff changeset
249 png_debug1(1, "in %s storage function", "oFFs");
Ritor1
parents:
diff changeset
250
Ritor1
parents:
diff changeset
251 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
252 return;
Ritor1
parents:
diff changeset
253
Ritor1
parents:
diff changeset
254 info_ptr->x_offset = offset_x;
Ritor1
parents:
diff changeset
255 info_ptr->y_offset = offset_y;
Ritor1
parents:
diff changeset
256 info_ptr->offset_unit_type = (png_byte)unit_type;
Ritor1
parents:
diff changeset
257 info_ptr->valid |= PNG_INFO_oFFs;
Ritor1
parents:
diff changeset
258 }
Ritor1
parents:
diff changeset
259 #endif
Ritor1
parents:
diff changeset
260
Ritor1
parents:
diff changeset
261 #ifdef PNG_pCAL_SUPPORTED
Ritor1
parents:
diff changeset
262 void PNGAPI
Ritor1
parents:
diff changeset
263 png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
264 png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
Ritor1
parents:
diff changeset
265 int nparams, png_const_charp units, png_charpp params)
Ritor1
parents:
diff changeset
266 {
Ritor1
parents:
diff changeset
267 png_size_t length;
Ritor1
parents:
diff changeset
268 int i;
Ritor1
parents:
diff changeset
269
Ritor1
parents:
diff changeset
270 png_debug1(1, "in %s storage function", "pCAL");
Ritor1
parents:
diff changeset
271
Ritor1
parents:
diff changeset
272 if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
Ritor1
parents:
diff changeset
273 || (nparams > 0 && params == NULL))
Ritor1
parents:
diff changeset
274 return;
Ritor1
parents:
diff changeset
275
Ritor1
parents:
diff changeset
276 length = strlen(purpose) + 1;
Ritor1
parents:
diff changeset
277 png_debug1(3, "allocating purpose for info (%lu bytes)",
Ritor1
parents:
diff changeset
278 (unsigned long)length);
Ritor1
parents:
diff changeset
279
Ritor1
parents:
diff changeset
280 /* TODO: validate format of calibration name and unit name */
Ritor1
parents:
diff changeset
281
Ritor1
parents:
diff changeset
282 /* Check that the type matches the specification. */
Ritor1
parents:
diff changeset
283 if (type < 0 || type > 3)
Ritor1
parents:
diff changeset
284 png_error(png_ptr, "Invalid pCAL equation type");
Ritor1
parents:
diff changeset
285
Ritor1
parents:
diff changeset
286 if (nparams < 0 || nparams > 255)
Ritor1
parents:
diff changeset
287 png_error(png_ptr, "Invalid pCAL parameter count");
Ritor1
parents:
diff changeset
288
Ritor1
parents:
diff changeset
289 /* Validate params[nparams] */
Ritor1
parents:
diff changeset
290 for (i=0; i<nparams; ++i)
Ritor1
parents:
diff changeset
291 if (params[i] == NULL ||
Ritor1
parents:
diff changeset
292 !png_check_fp_string(params[i], strlen(params[i])))
Ritor1
parents:
diff changeset
293 png_error(png_ptr, "Invalid format for pCAL parameter");
Ritor1
parents:
diff changeset
294
Ritor1
parents:
diff changeset
295 info_ptr->pcal_purpose = png_voidcast(png_charp,
Ritor1
parents:
diff changeset
296 png_malloc_warn(png_ptr, length));
Ritor1
parents:
diff changeset
297
Ritor1
parents:
diff changeset
298 if (info_ptr->pcal_purpose == NULL)
Ritor1
parents:
diff changeset
299 {
Ritor1
parents:
diff changeset
300 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Ritor1
parents:
diff changeset
301 return;
Ritor1
parents:
diff changeset
302 }
Ritor1
parents:
diff changeset
303
Ritor1
parents:
diff changeset
304 memcpy(info_ptr->pcal_purpose, purpose, length);
Ritor1
parents:
diff changeset
305
Ritor1
parents:
diff changeset
306 png_debug(3, "storing X0, X1, type, and nparams in info");
Ritor1
parents:
diff changeset
307 info_ptr->pcal_X0 = X0;
Ritor1
parents:
diff changeset
308 info_ptr->pcal_X1 = X1;
Ritor1
parents:
diff changeset
309 info_ptr->pcal_type = (png_byte)type;
Ritor1
parents:
diff changeset
310 info_ptr->pcal_nparams = (png_byte)nparams;
Ritor1
parents:
diff changeset
311
Ritor1
parents:
diff changeset
312 length = strlen(units) + 1;
Ritor1
parents:
diff changeset
313 png_debug1(3, "allocating units for info (%lu bytes)",
Ritor1
parents:
diff changeset
314 (unsigned long)length);
Ritor1
parents:
diff changeset
315
Ritor1
parents:
diff changeset
316 info_ptr->pcal_units = png_voidcast(png_charp,
Ritor1
parents:
diff changeset
317 png_malloc_warn(png_ptr, length));
Ritor1
parents:
diff changeset
318
Ritor1
parents:
diff changeset
319 if (info_ptr->pcal_units == NULL)
Ritor1
parents:
diff changeset
320 {
Ritor1
parents:
diff changeset
321 png_warning(png_ptr, "Insufficient memory for pCAL units");
Ritor1
parents:
diff changeset
322 return;
Ritor1
parents:
diff changeset
323 }
Ritor1
parents:
diff changeset
324
Ritor1
parents:
diff changeset
325 memcpy(info_ptr->pcal_units, units, length);
Ritor1
parents:
diff changeset
326
Ritor1
parents:
diff changeset
327 info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Ritor1
parents:
diff changeset
328 (png_size_t)((nparams + 1) * (sizeof (png_charp)))));
Ritor1
parents:
diff changeset
329
Ritor1
parents:
diff changeset
330 if (info_ptr->pcal_params == NULL)
Ritor1
parents:
diff changeset
331 {
Ritor1
parents:
diff changeset
332 png_warning(png_ptr, "Insufficient memory for pCAL params");
Ritor1
parents:
diff changeset
333 return;
Ritor1
parents:
diff changeset
334 }
Ritor1
parents:
diff changeset
335
Ritor1
parents:
diff changeset
336 memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp)));
Ritor1
parents:
diff changeset
337
Ritor1
parents:
diff changeset
338 for (i = 0; i < nparams; i++)
Ritor1
parents:
diff changeset
339 {
Ritor1
parents:
diff changeset
340 length = strlen(params[i]) + 1;
Ritor1
parents:
diff changeset
341 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Ritor1
parents:
diff changeset
342 (unsigned long)length);
Ritor1
parents:
diff changeset
343
Ritor1
parents:
diff changeset
344 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Ritor1
parents:
diff changeset
345
Ritor1
parents:
diff changeset
346 if (info_ptr->pcal_params[i] == NULL)
Ritor1
parents:
diff changeset
347 {
Ritor1
parents:
diff changeset
348 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
Ritor1
parents:
diff changeset
349 return;
Ritor1
parents:
diff changeset
350 }
Ritor1
parents:
diff changeset
351
Ritor1
parents:
diff changeset
352 memcpy(info_ptr->pcal_params[i], params[i], length);
Ritor1
parents:
diff changeset
353 }
Ritor1
parents:
diff changeset
354
Ritor1
parents:
diff changeset
355 info_ptr->valid |= PNG_INFO_pCAL;
Ritor1
parents:
diff changeset
356 info_ptr->free_me |= PNG_FREE_PCAL;
Ritor1
parents:
diff changeset
357 }
Ritor1
parents:
diff changeset
358 #endif
Ritor1
parents:
diff changeset
359
Ritor1
parents:
diff changeset
360 #ifdef PNG_sCAL_SUPPORTED
Ritor1
parents:
diff changeset
361 void PNGAPI
Ritor1
parents:
diff changeset
362 png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
363 int unit, png_const_charp swidth, png_const_charp sheight)
Ritor1
parents:
diff changeset
364 {
Ritor1
parents:
diff changeset
365 png_size_t lengthw = 0, lengthh = 0;
Ritor1
parents:
diff changeset
366
Ritor1
parents:
diff changeset
367 png_debug1(1, "in %s storage function", "sCAL");
Ritor1
parents:
diff changeset
368
Ritor1
parents:
diff changeset
369 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
370 return;
Ritor1
parents:
diff changeset
371
Ritor1
parents:
diff changeset
372 /* Double check the unit (should never get here with an invalid
Ritor1
parents:
diff changeset
373 * unit unless this is an API call.)
Ritor1
parents:
diff changeset
374 */
Ritor1
parents:
diff changeset
375 if (unit != 1 && unit != 2)
Ritor1
parents:
diff changeset
376 png_error(png_ptr, "Invalid sCAL unit");
Ritor1
parents:
diff changeset
377
Ritor1
parents:
diff changeset
378 if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
Ritor1
parents:
diff changeset
379 swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
Ritor1
parents:
diff changeset
380 png_error(png_ptr, "Invalid sCAL width");
Ritor1
parents:
diff changeset
381
Ritor1
parents:
diff changeset
382 if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
Ritor1
parents:
diff changeset
383 sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
Ritor1
parents:
diff changeset
384 png_error(png_ptr, "Invalid sCAL height");
Ritor1
parents:
diff changeset
385
Ritor1
parents:
diff changeset
386 info_ptr->scal_unit = (png_byte)unit;
Ritor1
parents:
diff changeset
387
Ritor1
parents:
diff changeset
388 ++lengthw;
Ritor1
parents:
diff changeset
389
Ritor1
parents:
diff changeset
390 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
Ritor1
parents:
diff changeset
391
Ritor1
parents:
diff changeset
392 info_ptr->scal_s_width = png_voidcast(png_charp,
Ritor1
parents:
diff changeset
393 png_malloc_warn(png_ptr, lengthw));
Ritor1
parents:
diff changeset
394
Ritor1
parents:
diff changeset
395 if (info_ptr->scal_s_width == NULL)
Ritor1
parents:
diff changeset
396 {
Ritor1
parents:
diff changeset
397 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Ritor1
parents:
diff changeset
398 return;
Ritor1
parents:
diff changeset
399 }
Ritor1
parents:
diff changeset
400
Ritor1
parents:
diff changeset
401 memcpy(info_ptr->scal_s_width, swidth, lengthw);
Ritor1
parents:
diff changeset
402
Ritor1
parents:
diff changeset
403 ++lengthh;
Ritor1
parents:
diff changeset
404
Ritor1
parents:
diff changeset
405 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
Ritor1
parents:
diff changeset
406
Ritor1
parents:
diff changeset
407 info_ptr->scal_s_height = png_voidcast(png_charp,
Ritor1
parents:
diff changeset
408 png_malloc_warn(png_ptr, lengthh));
Ritor1
parents:
diff changeset
409
Ritor1
parents:
diff changeset
410 if (info_ptr->scal_s_height == NULL)
Ritor1
parents:
diff changeset
411 {
Ritor1
parents:
diff changeset
412 png_free (png_ptr, info_ptr->scal_s_width);
Ritor1
parents:
diff changeset
413 info_ptr->scal_s_width = NULL;
Ritor1
parents:
diff changeset
414
Ritor1
parents:
diff changeset
415 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Ritor1
parents:
diff changeset
416 return;
Ritor1
parents:
diff changeset
417 }
Ritor1
parents:
diff changeset
418
Ritor1
parents:
diff changeset
419 memcpy(info_ptr->scal_s_height, sheight, lengthh);
Ritor1
parents:
diff changeset
420
Ritor1
parents:
diff changeset
421 info_ptr->valid |= PNG_INFO_sCAL;
Ritor1
parents:
diff changeset
422 info_ptr->free_me |= PNG_FREE_SCAL;
Ritor1
parents:
diff changeset
423 }
Ritor1
parents:
diff changeset
424
Ritor1
parents:
diff changeset
425 # ifdef PNG_FLOATING_POINT_SUPPORTED
Ritor1
parents:
diff changeset
426 void PNGAPI
Ritor1
parents:
diff changeset
427 png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Ritor1
parents:
diff changeset
428 double width, double height)
Ritor1
parents:
diff changeset
429 {
Ritor1
parents:
diff changeset
430 png_debug1(1, "in %s storage function", "sCAL");
Ritor1
parents:
diff changeset
431
Ritor1
parents:
diff changeset
432 /* Check the arguments. */
Ritor1
parents:
diff changeset
433 if (width <= 0)
Ritor1
parents:
diff changeset
434 png_warning(png_ptr, "Invalid sCAL width ignored");
Ritor1
parents:
diff changeset
435
Ritor1
parents:
diff changeset
436 else if (height <= 0)
Ritor1
parents:
diff changeset
437 png_warning(png_ptr, "Invalid sCAL height ignored");
Ritor1
parents:
diff changeset
438
Ritor1
parents:
diff changeset
439 else
Ritor1
parents:
diff changeset
440 {
Ritor1
parents:
diff changeset
441 /* Convert 'width' and 'height' to ASCII. */
Ritor1
parents:
diff changeset
442 char swidth[PNG_sCAL_MAX_DIGITS+1];
Ritor1
parents:
diff changeset
443 char sheight[PNG_sCAL_MAX_DIGITS+1];
Ritor1
parents:
diff changeset
444
Ritor1
parents:
diff changeset
445 png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
Ritor1
parents:
diff changeset
446 PNG_sCAL_PRECISION);
Ritor1
parents:
diff changeset
447 png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
Ritor1
parents:
diff changeset
448 PNG_sCAL_PRECISION);
Ritor1
parents:
diff changeset
449
Ritor1
parents:
diff changeset
450 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
Ritor1
parents:
diff changeset
451 }
Ritor1
parents:
diff changeset
452 }
Ritor1
parents:
diff changeset
453 # endif
Ritor1
parents:
diff changeset
454
Ritor1
parents:
diff changeset
455 # ifdef PNG_FIXED_POINT_SUPPORTED
Ritor1
parents:
diff changeset
456 void PNGAPI
Ritor1
parents:
diff changeset
457 png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Ritor1
parents:
diff changeset
458 png_fixed_point width, png_fixed_point height)
Ritor1
parents:
diff changeset
459 {
Ritor1
parents:
diff changeset
460 png_debug1(1, "in %s storage function", "sCAL");
Ritor1
parents:
diff changeset
461
Ritor1
parents:
diff changeset
462 /* Check the arguments. */
Ritor1
parents:
diff changeset
463 if (width <= 0)
Ritor1
parents:
diff changeset
464 png_warning(png_ptr, "Invalid sCAL width ignored");
Ritor1
parents:
diff changeset
465
Ritor1
parents:
diff changeset
466 else if (height <= 0)
Ritor1
parents:
diff changeset
467 png_warning(png_ptr, "Invalid sCAL height ignored");
Ritor1
parents:
diff changeset
468
Ritor1
parents:
diff changeset
469 else
Ritor1
parents:
diff changeset
470 {
Ritor1
parents:
diff changeset
471 /* Convert 'width' and 'height' to ASCII. */
Ritor1
parents:
diff changeset
472 char swidth[PNG_sCAL_MAX_DIGITS+1];
Ritor1
parents:
diff changeset
473 char sheight[PNG_sCAL_MAX_DIGITS+1];
Ritor1
parents:
diff changeset
474
Ritor1
parents:
diff changeset
475 png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
Ritor1
parents:
diff changeset
476 png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
Ritor1
parents:
diff changeset
477
Ritor1
parents:
diff changeset
478 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
Ritor1
parents:
diff changeset
479 }
Ritor1
parents:
diff changeset
480 }
Ritor1
parents:
diff changeset
481 # endif
Ritor1
parents:
diff changeset
482 #endif
Ritor1
parents:
diff changeset
483
Ritor1
parents:
diff changeset
484 #ifdef PNG_pHYs_SUPPORTED
Ritor1
parents:
diff changeset
485 void PNGAPI
Ritor1
parents:
diff changeset
486 png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
487 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Ritor1
parents:
diff changeset
488 {
Ritor1
parents:
diff changeset
489 png_debug1(1, "in %s storage function", "pHYs");
Ritor1
parents:
diff changeset
490
Ritor1
parents:
diff changeset
491 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
492 return;
Ritor1
parents:
diff changeset
493
Ritor1
parents:
diff changeset
494 info_ptr->x_pixels_per_unit = res_x;
Ritor1
parents:
diff changeset
495 info_ptr->y_pixels_per_unit = res_y;
Ritor1
parents:
diff changeset
496 info_ptr->phys_unit_type = (png_byte)unit_type;
Ritor1
parents:
diff changeset
497 info_ptr->valid |= PNG_INFO_pHYs;
Ritor1
parents:
diff changeset
498 }
Ritor1
parents:
diff changeset
499 #endif
Ritor1
parents:
diff changeset
500
Ritor1
parents:
diff changeset
501 void PNGAPI
Ritor1
parents:
diff changeset
502 png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
503 png_const_colorp palette, int num_palette)
Ritor1
parents:
diff changeset
504 {
Ritor1
parents:
diff changeset
505
Ritor1
parents:
diff changeset
506 png_debug1(1, "in %s storage function", "PLTE");
Ritor1
parents:
diff changeset
507
Ritor1
parents:
diff changeset
508 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
509 return;
Ritor1
parents:
diff changeset
510
Ritor1
parents:
diff changeset
511 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Ritor1
parents:
diff changeset
512 {
Ritor1
parents:
diff changeset
513 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Ritor1
parents:
diff changeset
514 png_error(png_ptr, "Invalid palette length");
Ritor1
parents:
diff changeset
515
Ritor1
parents:
diff changeset
516 else
Ritor1
parents:
diff changeset
517 {
Ritor1
parents:
diff changeset
518 png_warning(png_ptr, "Invalid palette length");
Ritor1
parents:
diff changeset
519 return;
Ritor1
parents:
diff changeset
520 }
Ritor1
parents:
diff changeset
521 }
Ritor1
parents:
diff changeset
522
Ritor1
parents:
diff changeset
523 if ((num_palette > 0 && palette == NULL) ||
Ritor1
parents:
diff changeset
524 (num_palette == 0
Ritor1
parents:
diff changeset
525 # ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
526 && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
Ritor1
parents:
diff changeset
527 # endif
Ritor1
parents:
diff changeset
528 ))
Ritor1
parents:
diff changeset
529 {
Ritor1
parents:
diff changeset
530 png_error(png_ptr, "Invalid palette");
Ritor1
parents:
diff changeset
531 return;
Ritor1
parents:
diff changeset
532 }
Ritor1
parents:
diff changeset
533
Ritor1
parents:
diff changeset
534 /* It may not actually be necessary to set png_ptr->palette here;
Ritor1
parents:
diff changeset
535 * we do it for backward compatibility with the way the png_handle_tRNS
Ritor1
parents:
diff changeset
536 * function used to do the allocation.
Ritor1
parents:
diff changeset
537 *
Ritor1
parents:
diff changeset
538 * 1.6.0: the above statement appears to be incorrect; something has to set
Ritor1
parents:
diff changeset
539 * the palette inside png_struct on read.
Ritor1
parents:
diff changeset
540 */
Ritor1
parents:
diff changeset
541 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Ritor1
parents:
diff changeset
542
Ritor1
parents:
diff changeset
543 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Ritor1
parents:
diff changeset
544 * of num_palette entries, in case of an invalid PNG file that has
Ritor1
parents:
diff changeset
545 * too-large sample values.
Ritor1
parents:
diff changeset
546 */
Ritor1
parents:
diff changeset
547 png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
Ritor1
parents:
diff changeset
548 PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
Ritor1
parents:
diff changeset
549
Ritor1
parents:
diff changeset
550 if (num_palette > 0)
Ritor1
parents:
diff changeset
551 memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
Ritor1
parents:
diff changeset
552 info_ptr->palette = png_ptr->palette;
Ritor1
parents:
diff changeset
553 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Ritor1
parents:
diff changeset
554
Ritor1
parents:
diff changeset
555 info_ptr->free_me |= PNG_FREE_PLTE;
Ritor1
parents:
diff changeset
556
Ritor1
parents:
diff changeset
557 info_ptr->valid |= PNG_INFO_PLTE;
Ritor1
parents:
diff changeset
558 }
Ritor1
parents:
diff changeset
559
Ritor1
parents:
diff changeset
560 #ifdef PNG_sBIT_SUPPORTED
Ritor1
parents:
diff changeset
561 void PNGAPI
Ritor1
parents:
diff changeset
562 png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
563 png_const_color_8p sig_bit)
Ritor1
parents:
diff changeset
564 {
Ritor1
parents:
diff changeset
565 png_debug1(1, "in %s storage function", "sBIT");
Ritor1
parents:
diff changeset
566
Ritor1
parents:
diff changeset
567 if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
Ritor1
parents:
diff changeset
568 return;
Ritor1
parents:
diff changeset
569
Ritor1
parents:
diff changeset
570 info_ptr->sig_bit = *sig_bit;
Ritor1
parents:
diff changeset
571 info_ptr->valid |= PNG_INFO_sBIT;
Ritor1
parents:
diff changeset
572 }
Ritor1
parents:
diff changeset
573 #endif
Ritor1
parents:
diff changeset
574
Ritor1
parents:
diff changeset
575 #ifdef PNG_sRGB_SUPPORTED
Ritor1
parents:
diff changeset
576 void PNGAPI
Ritor1
parents:
diff changeset
577 png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
Ritor1
parents:
diff changeset
578 {
Ritor1
parents:
diff changeset
579 png_debug1(1, "in %s storage function", "sRGB");
Ritor1
parents:
diff changeset
580
Ritor1
parents:
diff changeset
581 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
582 return;
Ritor1
parents:
diff changeset
583
Ritor1
parents:
diff changeset
584 (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);
Ritor1
parents:
diff changeset
585 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
586 }
Ritor1
parents:
diff changeset
587
Ritor1
parents:
diff changeset
588 void PNGAPI
Ritor1
parents:
diff changeset
589 png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
590 int srgb_intent)
Ritor1
parents:
diff changeset
591 {
Ritor1
parents:
diff changeset
592 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Ritor1
parents:
diff changeset
593
Ritor1
parents:
diff changeset
594 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
595 return;
Ritor1
parents:
diff changeset
596
Ritor1
parents:
diff changeset
597 if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent))
Ritor1
parents:
diff changeset
598 {
Ritor1
parents:
diff changeset
599 /* This causes the gAMA and cHRM to be written too */
Ritor1
parents:
diff changeset
600 info_ptr->colorspace.flags |=
Ritor1
parents:
diff changeset
601 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
Ritor1
parents:
diff changeset
602 }
Ritor1
parents:
diff changeset
603
Ritor1
parents:
diff changeset
604 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
605 }
Ritor1
parents:
diff changeset
606 #endif /* sRGB */
Ritor1
parents:
diff changeset
607
Ritor1
parents:
diff changeset
608
Ritor1
parents:
diff changeset
609 #ifdef PNG_iCCP_SUPPORTED
Ritor1
parents:
diff changeset
610 void PNGAPI
Ritor1
parents:
diff changeset
611 png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
612 png_const_charp name, int compression_type,
Ritor1
parents:
diff changeset
613 png_const_bytep profile, png_uint_32 proflen)
Ritor1
parents:
diff changeset
614 {
Ritor1
parents:
diff changeset
615 png_charp new_iccp_name;
Ritor1
parents:
diff changeset
616 png_bytep new_iccp_profile;
Ritor1
parents:
diff changeset
617 png_size_t length;
Ritor1
parents:
diff changeset
618
Ritor1
parents:
diff changeset
619 png_debug1(1, "in %s storage function", "iCCP");
Ritor1
parents:
diff changeset
620
Ritor1
parents:
diff changeset
621 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
Ritor1
parents:
diff changeset
622 return;
Ritor1
parents:
diff changeset
623
Ritor1
parents:
diff changeset
624 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Ritor1
parents:
diff changeset
625 png_app_error(png_ptr, "Invalid iCCP compression method");
Ritor1
parents:
diff changeset
626
Ritor1
parents:
diff changeset
627 /* Set the colorspace first because this validates the profile; do not
Ritor1
parents:
diff changeset
628 * override previously set app cHRM or gAMA here (because likely as not the
Ritor1
parents:
diff changeset
629 * application knows better than libpng what the correct values are.) Pass
Ritor1
parents:
diff changeset
630 * the info_ptr color_type field to png_colorspace_set_ICC because in the
Ritor1
parents:
diff changeset
631 * write case it has not yet been stored in png_ptr.
Ritor1
parents:
diff changeset
632 */
Ritor1
parents:
diff changeset
633 {
Ritor1
parents:
diff changeset
634 int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
Ritor1
parents:
diff changeset
635 proflen, profile, info_ptr->color_type);
Ritor1
parents:
diff changeset
636
Ritor1
parents:
diff changeset
637 png_colorspace_sync_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
638
Ritor1
parents:
diff changeset
639 /* Don't do any of the copying if the profile was bad, or inconsistent. */
Ritor1
parents:
diff changeset
640 if (!result)
Ritor1
parents:
diff changeset
641 return;
Ritor1
parents:
diff changeset
642
Ritor1
parents:
diff changeset
643 /* But do write the gAMA and cHRM chunks from the profile. */
Ritor1
parents:
diff changeset
644 info_ptr->colorspace.flags |=
Ritor1
parents:
diff changeset
645 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
Ritor1
parents:
diff changeset
646 }
Ritor1
parents:
diff changeset
647
Ritor1
parents:
diff changeset
648 length = strlen(name)+1;
Ritor1
parents:
diff changeset
649 new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Ritor1
parents:
diff changeset
650
Ritor1
parents:
diff changeset
651 if (new_iccp_name == NULL)
Ritor1
parents:
diff changeset
652 {
Ritor1
parents:
diff changeset
653 png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
Ritor1
parents:
diff changeset
654 return;
Ritor1
parents:
diff changeset
655 }
Ritor1
parents:
diff changeset
656
Ritor1
parents:
diff changeset
657 memcpy(new_iccp_name, name, length);
Ritor1
parents:
diff changeset
658 new_iccp_profile = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
659 png_malloc_warn(png_ptr, proflen));
Ritor1
parents:
diff changeset
660
Ritor1
parents:
diff changeset
661 if (new_iccp_profile == NULL)
Ritor1
parents:
diff changeset
662 {
Ritor1
parents:
diff changeset
663 png_free(png_ptr, new_iccp_name);
Ritor1
parents:
diff changeset
664 png_benign_error(png_ptr,
Ritor1
parents:
diff changeset
665 "Insufficient memory to process iCCP profile");
Ritor1
parents:
diff changeset
666 return;
Ritor1
parents:
diff changeset
667 }
Ritor1
parents:
diff changeset
668
Ritor1
parents:
diff changeset
669 memcpy(new_iccp_profile, profile, proflen);
Ritor1
parents:
diff changeset
670
Ritor1
parents:
diff changeset
671 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Ritor1
parents:
diff changeset
672
Ritor1
parents:
diff changeset
673 info_ptr->iccp_proflen = proflen;
Ritor1
parents:
diff changeset
674 info_ptr->iccp_name = new_iccp_name;
Ritor1
parents:
diff changeset
675 info_ptr->iccp_profile = new_iccp_profile;
Ritor1
parents:
diff changeset
676 info_ptr->free_me |= PNG_FREE_ICCP;
Ritor1
parents:
diff changeset
677 info_ptr->valid |= PNG_INFO_iCCP;
Ritor1
parents:
diff changeset
678 }
Ritor1
parents:
diff changeset
679 #endif
Ritor1
parents:
diff changeset
680
Ritor1
parents:
diff changeset
681 #ifdef PNG_TEXT_SUPPORTED
Ritor1
parents:
diff changeset
682 void PNGAPI
Ritor1
parents:
diff changeset
683 png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
684 png_const_textp text_ptr, int num_text)
Ritor1
parents:
diff changeset
685 {
Ritor1
parents:
diff changeset
686 int ret;
Ritor1
parents:
diff changeset
687 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Ritor1
parents:
diff changeset
688
Ritor1
parents:
diff changeset
689 if (ret)
Ritor1
parents:
diff changeset
690 png_error(png_ptr, "Insufficient memory to store text");
Ritor1
parents:
diff changeset
691 }
Ritor1
parents:
diff changeset
692
Ritor1
parents:
diff changeset
693 int /* PRIVATE */
Ritor1
parents:
diff changeset
694 png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
695 png_const_textp text_ptr, int num_text)
Ritor1
parents:
diff changeset
696 {
Ritor1
parents:
diff changeset
697 int i;
Ritor1
parents:
diff changeset
698
Ritor1
parents:
diff changeset
699 png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
Ritor1
parents:
diff changeset
700 (unsigned long)png_ptr->chunk_name);
Ritor1
parents:
diff changeset
701
Ritor1
parents:
diff changeset
702 if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
Ritor1
parents:
diff changeset
703 return(0);
Ritor1
parents:
diff changeset
704
Ritor1
parents:
diff changeset
705 /* Make sure we have enough space in the "text" array in info_struct
Ritor1
parents:
diff changeset
706 * to hold all of the incoming text_ptr objects. This compare can't overflow
Ritor1
parents:
diff changeset
707 * because max_text >= num_text (anyway, subtract of two positive integers
Ritor1
parents:
diff changeset
708 * can't overflow in any case.)
Ritor1
parents:
diff changeset
709 */
Ritor1
parents:
diff changeset
710 if (num_text > info_ptr->max_text - info_ptr->num_text)
Ritor1
parents:
diff changeset
711 {
Ritor1
parents:
diff changeset
712 int old_num_text = info_ptr->num_text;
Ritor1
parents:
diff changeset
713 int max_text;
Ritor1
parents:
diff changeset
714 png_textp new_text = NULL;
Ritor1
parents:
diff changeset
715
Ritor1
parents:
diff changeset
716 /* Calculate an appropriate max_text, checking for overflow. */
Ritor1
parents:
diff changeset
717 max_text = old_num_text;
Ritor1
parents:
diff changeset
718 if (num_text <= INT_MAX - max_text)
Ritor1
parents:
diff changeset
719 {
Ritor1
parents:
diff changeset
720 max_text += num_text;
Ritor1
parents:
diff changeset
721
Ritor1
parents:
diff changeset
722 /* Round up to a multiple of 8 */
Ritor1
parents:
diff changeset
723 if (max_text < INT_MAX-8)
Ritor1
parents:
diff changeset
724 max_text = (max_text + 8) & ~0x7;
Ritor1
parents:
diff changeset
725
Ritor1
parents:
diff changeset
726 else
Ritor1
parents:
diff changeset
727 max_text = INT_MAX;
Ritor1
parents:
diff changeset
728
Ritor1
parents:
diff changeset
729 /* Now allocate a new array and copy the old members in, this does all
Ritor1
parents:
diff changeset
730 * the overflow checks.
Ritor1
parents:
diff changeset
731 */
Ritor1
parents:
diff changeset
732 new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
Ritor1
parents:
diff changeset
733 info_ptr->text, old_num_text, max_text-old_num_text,
Ritor1
parents:
diff changeset
734 sizeof *new_text));
Ritor1
parents:
diff changeset
735 }
Ritor1
parents:
diff changeset
736
Ritor1
parents:
diff changeset
737 if (new_text == NULL)
Ritor1
parents:
diff changeset
738 {
Ritor1
parents:
diff changeset
739 png_chunk_report(png_ptr, "too many text chunks",
Ritor1
parents:
diff changeset
740 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
741 return 1;
Ritor1
parents:
diff changeset
742 }
Ritor1
parents:
diff changeset
743
Ritor1
parents:
diff changeset
744 png_free(png_ptr, info_ptr->text);
Ritor1
parents:
diff changeset
745
Ritor1
parents:
diff changeset
746 info_ptr->text = new_text;
Ritor1
parents:
diff changeset
747 info_ptr->free_me |= PNG_FREE_TEXT;
Ritor1
parents:
diff changeset
748 info_ptr->max_text = max_text;
Ritor1
parents:
diff changeset
749 /* num_text is adjusted below as the entries are copied in */
Ritor1
parents:
diff changeset
750
Ritor1
parents:
diff changeset
751 png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
Ritor1
parents:
diff changeset
752 }
Ritor1
parents:
diff changeset
753
Ritor1
parents:
diff changeset
754 for (i = 0; i < num_text; i++)
Ritor1
parents:
diff changeset
755 {
Ritor1
parents:
diff changeset
756 size_t text_length, key_len;
Ritor1
parents:
diff changeset
757 size_t lang_len, lang_key_len;
Ritor1
parents:
diff changeset
758 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
Ritor1
parents:
diff changeset
759
Ritor1
parents:
diff changeset
760 if (text_ptr[i].key == NULL)
Ritor1
parents:
diff changeset
761 continue;
Ritor1
parents:
diff changeset
762
Ritor1
parents:
diff changeset
763 if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
Ritor1
parents:
diff changeset
764 text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
Ritor1
parents:
diff changeset
765 {
Ritor1
parents:
diff changeset
766 png_chunk_report(png_ptr, "text compression mode is out of range",
Ritor1
parents:
diff changeset
767 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
768 continue;
Ritor1
parents:
diff changeset
769 }
Ritor1
parents:
diff changeset
770
Ritor1
parents:
diff changeset
771 key_len = strlen(text_ptr[i].key);
Ritor1
parents:
diff changeset
772
Ritor1
parents:
diff changeset
773 if (text_ptr[i].compression <= 0)
Ritor1
parents:
diff changeset
774 {
Ritor1
parents:
diff changeset
775 lang_len = 0;
Ritor1
parents:
diff changeset
776 lang_key_len = 0;
Ritor1
parents:
diff changeset
777 }
Ritor1
parents:
diff changeset
778
Ritor1
parents:
diff changeset
779 else
Ritor1
parents:
diff changeset
780 # ifdef PNG_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
781 {
Ritor1
parents:
diff changeset
782 /* Set iTXt data */
Ritor1
parents:
diff changeset
783
Ritor1
parents:
diff changeset
784 if (text_ptr[i].lang != NULL)
Ritor1
parents:
diff changeset
785 lang_len = strlen(text_ptr[i].lang);
Ritor1
parents:
diff changeset
786
Ritor1
parents:
diff changeset
787 else
Ritor1
parents:
diff changeset
788 lang_len = 0;
Ritor1
parents:
diff changeset
789
Ritor1
parents:
diff changeset
790 if (text_ptr[i].lang_key != NULL)
Ritor1
parents:
diff changeset
791 lang_key_len = strlen(text_ptr[i].lang_key);
Ritor1
parents:
diff changeset
792
Ritor1
parents:
diff changeset
793 else
Ritor1
parents:
diff changeset
794 lang_key_len = 0;
Ritor1
parents:
diff changeset
795 }
Ritor1
parents:
diff changeset
796 # else /* PNG_iTXt_SUPPORTED */
Ritor1
parents:
diff changeset
797 {
Ritor1
parents:
diff changeset
798 png_chunk_report(png_ptr, "iTXt chunk not supported",
Ritor1
parents:
diff changeset
799 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
800 continue;
Ritor1
parents:
diff changeset
801 }
Ritor1
parents:
diff changeset
802 # endif
Ritor1
parents:
diff changeset
803
Ritor1
parents:
diff changeset
804 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Ritor1
parents:
diff changeset
805 {
Ritor1
parents:
diff changeset
806 text_length = 0;
Ritor1
parents:
diff changeset
807 # ifdef PNG_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
808 if (text_ptr[i].compression > 0)
Ritor1
parents:
diff changeset
809 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Ritor1
parents:
diff changeset
810
Ritor1
parents:
diff changeset
811 else
Ritor1
parents:
diff changeset
812 # endif
Ritor1
parents:
diff changeset
813 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Ritor1
parents:
diff changeset
814 }
Ritor1
parents:
diff changeset
815
Ritor1
parents:
diff changeset
816 else
Ritor1
parents:
diff changeset
817 {
Ritor1
parents:
diff changeset
818 text_length = strlen(text_ptr[i].text);
Ritor1
parents:
diff changeset
819 textp->compression = text_ptr[i].compression;
Ritor1
parents:
diff changeset
820 }
Ritor1
parents:
diff changeset
821
Ritor1
parents:
diff changeset
822 textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
Ritor1
parents:
diff changeset
823 key_len + text_length + lang_len + lang_key_len + 4));
Ritor1
parents:
diff changeset
824
Ritor1
parents:
diff changeset
825 if (textp->key == NULL)
Ritor1
parents:
diff changeset
826 {
Ritor1
parents:
diff changeset
827 png_chunk_report(png_ptr, "text chunk: out of memory",
Ritor1
parents:
diff changeset
828 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
829 return 1;
Ritor1
parents:
diff changeset
830 }
Ritor1
parents:
diff changeset
831
Ritor1
parents:
diff changeset
832 png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
Ritor1
parents:
diff changeset
833 (unsigned long)(png_uint_32)
Ritor1
parents:
diff changeset
834 (key_len + lang_len + lang_key_len + text_length + 4),
Ritor1
parents:
diff changeset
835 textp->key);
Ritor1
parents:
diff changeset
836
Ritor1
parents:
diff changeset
837 memcpy(textp->key, text_ptr[i].key, key_len);
Ritor1
parents:
diff changeset
838 *(textp->key + key_len) = '\0';
Ritor1
parents:
diff changeset
839
Ritor1
parents:
diff changeset
840 if (text_ptr[i].compression > 0)
Ritor1
parents:
diff changeset
841 {
Ritor1
parents:
diff changeset
842 textp->lang = textp->key + key_len + 1;
Ritor1
parents:
diff changeset
843 memcpy(textp->lang, text_ptr[i].lang, lang_len);
Ritor1
parents:
diff changeset
844 *(textp->lang + lang_len) = '\0';
Ritor1
parents:
diff changeset
845 textp->lang_key = textp->lang + lang_len + 1;
Ritor1
parents:
diff changeset
846 memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Ritor1
parents:
diff changeset
847 *(textp->lang_key + lang_key_len) = '\0';
Ritor1
parents:
diff changeset
848 textp->text = textp->lang_key + lang_key_len + 1;
Ritor1
parents:
diff changeset
849 }
Ritor1
parents:
diff changeset
850
Ritor1
parents:
diff changeset
851 else
Ritor1
parents:
diff changeset
852 {
Ritor1
parents:
diff changeset
853 textp->lang=NULL;
Ritor1
parents:
diff changeset
854 textp->lang_key=NULL;
Ritor1
parents:
diff changeset
855 textp->text = textp->key + key_len + 1;
Ritor1
parents:
diff changeset
856 }
Ritor1
parents:
diff changeset
857
Ritor1
parents:
diff changeset
858 if (text_length)
Ritor1
parents:
diff changeset
859 memcpy(textp->text, text_ptr[i].text, text_length);
Ritor1
parents:
diff changeset
860
Ritor1
parents:
diff changeset
861 *(textp->text + text_length) = '\0';
Ritor1
parents:
diff changeset
862
Ritor1
parents:
diff changeset
863 # ifdef PNG_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
864 if (textp->compression > 0)
Ritor1
parents:
diff changeset
865 {
Ritor1
parents:
diff changeset
866 textp->text_length = 0;
Ritor1
parents:
diff changeset
867 textp->itxt_length = text_length;
Ritor1
parents:
diff changeset
868 }
Ritor1
parents:
diff changeset
869
Ritor1
parents:
diff changeset
870 else
Ritor1
parents:
diff changeset
871 # endif
Ritor1
parents:
diff changeset
872 {
Ritor1
parents:
diff changeset
873 textp->text_length = text_length;
Ritor1
parents:
diff changeset
874 textp->itxt_length = 0;
Ritor1
parents:
diff changeset
875 }
Ritor1
parents:
diff changeset
876
Ritor1
parents:
diff changeset
877 info_ptr->num_text++;
Ritor1
parents:
diff changeset
878 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Ritor1
parents:
diff changeset
879 }
Ritor1
parents:
diff changeset
880
Ritor1
parents:
diff changeset
881 return(0);
Ritor1
parents:
diff changeset
882 }
Ritor1
parents:
diff changeset
883 #endif
Ritor1
parents:
diff changeset
884
Ritor1
parents:
diff changeset
885 #ifdef PNG_tIME_SUPPORTED
Ritor1
parents:
diff changeset
886 void PNGAPI
Ritor1
parents:
diff changeset
887 png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
888 png_const_timep mod_time)
Ritor1
parents:
diff changeset
889 {
Ritor1
parents:
diff changeset
890 png_debug1(1, "in %s storage function", "tIME");
Ritor1
parents:
diff changeset
891
Ritor1
parents:
diff changeset
892 if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
Ritor1
parents:
diff changeset
893 (png_ptr->mode & PNG_WROTE_tIME))
Ritor1
parents:
diff changeset
894 return;
Ritor1
parents:
diff changeset
895
Ritor1
parents:
diff changeset
896 if (mod_time->month == 0 || mod_time->month > 12 ||
Ritor1
parents:
diff changeset
897 mod_time->day == 0 || mod_time->day > 31 ||
Ritor1
parents:
diff changeset
898 mod_time->hour > 23 || mod_time->minute > 59 ||
Ritor1
parents:
diff changeset
899 mod_time->second > 60)
Ritor1
parents:
diff changeset
900 {
Ritor1
parents:
diff changeset
901 png_warning(png_ptr, "Ignoring invalid time value");
Ritor1
parents:
diff changeset
902 return;
Ritor1
parents:
diff changeset
903 }
Ritor1
parents:
diff changeset
904
Ritor1
parents:
diff changeset
905 info_ptr->mod_time = *mod_time;
Ritor1
parents:
diff changeset
906 info_ptr->valid |= PNG_INFO_tIME;
Ritor1
parents:
diff changeset
907 }
Ritor1
parents:
diff changeset
908 #endif
Ritor1
parents:
diff changeset
909
Ritor1
parents:
diff changeset
910 #ifdef PNG_tRNS_SUPPORTED
Ritor1
parents:
diff changeset
911 void PNGAPI
Ritor1
parents:
diff changeset
912 png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
913 png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Ritor1
parents:
diff changeset
914 {
Ritor1
parents:
diff changeset
915 png_debug1(1, "in %s storage function", "tRNS");
Ritor1
parents:
diff changeset
916
Ritor1
parents:
diff changeset
917 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
918 return;
Ritor1
parents:
diff changeset
919
Ritor1
parents:
diff changeset
920 if (trans_alpha != NULL)
Ritor1
parents:
diff changeset
921 {
Ritor1
parents:
diff changeset
922 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Ritor1
parents:
diff changeset
923 * we do it for backward compatibility with the way the png_handle_tRNS
Ritor1
parents:
diff changeset
924 * function used to do the allocation.
Ritor1
parents:
diff changeset
925 *
Ritor1
parents:
diff changeset
926 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
Ritor1
parents:
diff changeset
927 * relies on png_set_tRNS storing the information in png_struct
Ritor1
parents:
diff changeset
928 * (otherwise it won't be there for the code in pngrtran.c).
Ritor1
parents:
diff changeset
929 */
Ritor1
parents:
diff changeset
930
Ritor1
parents:
diff changeset
931 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Ritor1
parents:
diff changeset
932
Ritor1
parents:
diff changeset
933 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Ritor1
parents:
diff changeset
934 png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
935 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
Ritor1
parents:
diff changeset
936
Ritor1
parents:
diff changeset
937 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Ritor1
parents:
diff changeset
938 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Ritor1
parents:
diff changeset
939 }
Ritor1
parents:
diff changeset
940
Ritor1
parents:
diff changeset
941 if (trans_color != NULL)
Ritor1
parents:
diff changeset
942 {
Ritor1
parents:
diff changeset
943 int sample_max = (1 << info_ptr->bit_depth);
Ritor1
parents:
diff changeset
944
Ritor1
parents:
diff changeset
945 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Ritor1
parents:
diff changeset
946 trans_color->gray > sample_max) ||
Ritor1
parents:
diff changeset
947 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Ritor1
parents:
diff changeset
948 (trans_color->red > sample_max ||
Ritor1
parents:
diff changeset
949 trans_color->green > sample_max ||
Ritor1
parents:
diff changeset
950 trans_color->blue > sample_max)))
Ritor1
parents:
diff changeset
951 png_warning(png_ptr,
Ritor1
parents:
diff changeset
952 "tRNS chunk has out-of-range samples for bit_depth");
Ritor1
parents:
diff changeset
953
Ritor1
parents:
diff changeset
954 info_ptr->trans_color = *trans_color;
Ritor1
parents:
diff changeset
955
Ritor1
parents:
diff changeset
956 if (num_trans == 0)
Ritor1
parents:
diff changeset
957 num_trans = 1;
Ritor1
parents:
diff changeset
958 }
Ritor1
parents:
diff changeset
959
Ritor1
parents:
diff changeset
960 info_ptr->num_trans = (png_uint_16)num_trans;
Ritor1
parents:
diff changeset
961
Ritor1
parents:
diff changeset
962 if (num_trans != 0)
Ritor1
parents:
diff changeset
963 {
Ritor1
parents:
diff changeset
964 info_ptr->valid |= PNG_INFO_tRNS;
Ritor1
parents:
diff changeset
965 info_ptr->free_me |= PNG_FREE_TRNS;
Ritor1
parents:
diff changeset
966 }
Ritor1
parents:
diff changeset
967 }
Ritor1
parents:
diff changeset
968 #endif
Ritor1
parents:
diff changeset
969
Ritor1
parents:
diff changeset
970 #ifdef PNG_sPLT_SUPPORTED
Ritor1
parents:
diff changeset
971 void PNGAPI
Ritor1
parents:
diff changeset
972 png_set_sPLT(png_const_structrp png_ptr,
Ritor1
parents:
diff changeset
973 png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Ritor1
parents:
diff changeset
974 /*
Ritor1
parents:
diff changeset
975 * entries - array of png_sPLT_t structures
Ritor1
parents:
diff changeset
976 * to be added to the list of palettes
Ritor1
parents:
diff changeset
977 * in the info structure.
Ritor1
parents:
diff changeset
978 *
Ritor1
parents:
diff changeset
979 * nentries - number of palette structures to be
Ritor1
parents:
diff changeset
980 * added.
Ritor1
parents:
diff changeset
981 */
Ritor1
parents:
diff changeset
982 {
Ritor1
parents:
diff changeset
983 png_sPLT_tp np;
Ritor1
parents:
diff changeset
984
Ritor1
parents:
diff changeset
985 if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
Ritor1
parents:
diff changeset
986 return;
Ritor1
parents:
diff changeset
987
Ritor1
parents:
diff changeset
988 /* Use the internal realloc function, which checks for all the possible
Ritor1
parents:
diff changeset
989 * overflows. Notice that the parameters are (int) and (size_t)
Ritor1
parents:
diff changeset
990 */
Ritor1
parents:
diff changeset
991 np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
Ritor1
parents:
diff changeset
992 info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
Ritor1
parents:
diff changeset
993 sizeof *np));
Ritor1
parents:
diff changeset
994
Ritor1
parents:
diff changeset
995 if (np == NULL)
Ritor1
parents:
diff changeset
996 {
Ritor1
parents:
diff changeset
997 /* Out of memory or too many chunks */
Ritor1
parents:
diff changeset
998 png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
999 return;
Ritor1
parents:
diff changeset
1000 }
Ritor1
parents:
diff changeset
1001
Ritor1
parents:
diff changeset
1002 png_free(png_ptr, info_ptr->splt_palettes);
Ritor1
parents:
diff changeset
1003 info_ptr->splt_palettes = np;
Ritor1
parents:
diff changeset
1004 info_ptr->free_me |= PNG_FREE_SPLT;
Ritor1
parents:
diff changeset
1005
Ritor1
parents:
diff changeset
1006 np += info_ptr->splt_palettes_num;
Ritor1
parents:
diff changeset
1007
Ritor1
parents:
diff changeset
1008 do
Ritor1
parents:
diff changeset
1009 {
Ritor1
parents:
diff changeset
1010 png_size_t length;
Ritor1
parents:
diff changeset
1011
Ritor1
parents:
diff changeset
1012 /* Skip invalid input entries */
Ritor1
parents:
diff changeset
1013 if (entries->name == NULL || entries->entries == NULL)
Ritor1
parents:
diff changeset
1014 {
Ritor1
parents:
diff changeset
1015 /* png_handle_sPLT doesn't do this, so this is an app error */
Ritor1
parents:
diff changeset
1016 png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
Ritor1
parents:
diff changeset
1017 /* Just skip the invalid entry */
Ritor1
parents:
diff changeset
1018 continue;
Ritor1
parents:
diff changeset
1019 }
Ritor1
parents:
diff changeset
1020
Ritor1
parents:
diff changeset
1021 np->depth = entries->depth;
Ritor1
parents:
diff changeset
1022
Ritor1
parents:
diff changeset
1023 /* In the even of out-of-memory just return - there's no point keeping on
Ritor1
parents:
diff changeset
1024 * trying to add sPLT chunks.
Ritor1
parents:
diff changeset
1025 */
Ritor1
parents:
diff changeset
1026 length = strlen(entries->name) + 1;
Ritor1
parents:
diff changeset
1027 np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
Ritor1
parents:
diff changeset
1028
Ritor1
parents:
diff changeset
1029 if (np->name == NULL)
Ritor1
parents:
diff changeset
1030 break;
Ritor1
parents:
diff changeset
1031
Ritor1
parents:
diff changeset
1032 memcpy(np->name, entries->name, length);
Ritor1
parents:
diff changeset
1033
Ritor1
parents:
diff changeset
1034 /* IMPORTANT: we have memory now that won't get freed if something else
Ritor1
parents:
diff changeset
1035 * goes wrong, this code must free it. png_malloc_array produces no
Ritor1
parents:
diff changeset
1036 * warnings, use a png_chunk_report (below) if there is an error.
Ritor1
parents:
diff changeset
1037 */
Ritor1
parents:
diff changeset
1038 np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
Ritor1
parents:
diff changeset
1039 entries->nentries, sizeof (png_sPLT_entry)));
Ritor1
parents:
diff changeset
1040
Ritor1
parents:
diff changeset
1041 if (np->entries == NULL)
Ritor1
parents:
diff changeset
1042 {
Ritor1
parents:
diff changeset
1043 png_free(png_ptr, np->name);
Ritor1
parents:
diff changeset
1044 break;
Ritor1
parents:
diff changeset
1045 }
Ritor1
parents:
diff changeset
1046
Ritor1
parents:
diff changeset
1047 np->nentries = entries->nentries;
Ritor1
parents:
diff changeset
1048 /* This multiply can't overflow because png_malloc_array has already
Ritor1
parents:
diff changeset
1049 * checked it when doing the allocation.
Ritor1
parents:
diff changeset
1050 */
Ritor1
parents:
diff changeset
1051 memcpy(np->entries, entries->entries,
Ritor1
parents:
diff changeset
1052 entries->nentries * sizeof (png_sPLT_entry));
Ritor1
parents:
diff changeset
1053
Ritor1
parents:
diff changeset
1054 /* Note that 'continue' skips the advance of the out pointer and out
Ritor1
parents:
diff changeset
1055 * count, so an invalid entry is not added.
Ritor1
parents:
diff changeset
1056 */
Ritor1
parents:
diff changeset
1057 info_ptr->valid |= PNG_INFO_sPLT;
Ritor1
parents:
diff changeset
1058 ++(info_ptr->splt_palettes_num);
Ritor1
parents:
diff changeset
1059 ++np;
Ritor1
parents:
diff changeset
1060 }
Ritor1
parents:
diff changeset
1061 while (++entries, --nentries);
Ritor1
parents:
diff changeset
1062
Ritor1
parents:
diff changeset
1063 if (nentries > 0)
Ritor1
parents:
diff changeset
1064 png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
1065 }
Ritor1
parents:
diff changeset
1066 #endif /* PNG_sPLT_SUPPORTED */
Ritor1
parents:
diff changeset
1067
Ritor1
parents:
diff changeset
1068 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
Ritor1
parents:
diff changeset
1069 static png_byte
Ritor1
parents:
diff changeset
1070 check_location(png_const_structrp png_ptr, int location)
Ritor1
parents:
diff changeset
1071 {
Ritor1
parents:
diff changeset
1072 location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
Ritor1
parents:
diff changeset
1073
Ritor1
parents:
diff changeset
1074 /* New in 1.6.0; copy the location and check it. This is an API
Ritor1
parents:
diff changeset
1075 * change, previously the app had to use the
Ritor1
parents:
diff changeset
1076 * png_set_unknown_chunk_location API below for each chunk.
Ritor1
parents:
diff changeset
1077 */
Ritor1
parents:
diff changeset
1078 if (location == 0 && !(png_ptr->mode & PNG_IS_READ_STRUCT))
Ritor1
parents:
diff changeset
1079 {
Ritor1
parents:
diff changeset
1080 /* Write struct, so unknown chunks come from the app */
Ritor1
parents:
diff changeset
1081 png_app_warning(png_ptr,
Ritor1
parents:
diff changeset
1082 "png_set_unknown_chunks now expects a valid location");
Ritor1
parents:
diff changeset
1083 /* Use the old behavior */
Ritor1
parents:
diff changeset
1084 location = (png_byte)(png_ptr->mode &
Ritor1
parents:
diff changeset
1085 (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
Ritor1
parents:
diff changeset
1086 }
Ritor1
parents:
diff changeset
1087
Ritor1
parents:
diff changeset
1088 /* This need not be an internal error - if the app calls
Ritor1
parents:
diff changeset
1089 * png_set_unknown_chunks on a read pointer it must get the location right.
Ritor1
parents:
diff changeset
1090 */
Ritor1
parents:
diff changeset
1091 if (location == 0)
Ritor1
parents:
diff changeset
1092 png_error(png_ptr, "invalid location in png_set_unknown_chunks");
Ritor1
parents:
diff changeset
1093
Ritor1
parents:
diff changeset
1094 /* Now reduce the location to the top-most set bit by removing each least
Ritor1
parents:
diff changeset
1095 * significant bit in turn.
Ritor1
parents:
diff changeset
1096 */
Ritor1
parents:
diff changeset
1097 while (location != (location & -location))
Ritor1
parents:
diff changeset
1098 location &= ~(location & -location);
Ritor1
parents:
diff changeset
1099
Ritor1
parents:
diff changeset
1100 /* The cast is safe because 'location' is a bit mask and only the low four
Ritor1
parents:
diff changeset
1101 * bits are significant.
Ritor1
parents:
diff changeset
1102 */
Ritor1
parents:
diff changeset
1103 return (png_byte)location;
Ritor1
parents:
diff changeset
1104 }
Ritor1
parents:
diff changeset
1105
Ritor1
parents:
diff changeset
1106 void PNGAPI
Ritor1
parents:
diff changeset
1107 png_set_unknown_chunks(png_const_structrp png_ptr,
Ritor1
parents:
diff changeset
1108 png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Ritor1
parents:
diff changeset
1109 {
Ritor1
parents:
diff changeset
1110 png_unknown_chunkp np;
Ritor1
parents:
diff changeset
1111
Ritor1
parents:
diff changeset
1112 if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
Ritor1
parents:
diff changeset
1113 unknowns == NULL)
Ritor1
parents:
diff changeset
1114 return;
Ritor1
parents:
diff changeset
1115
Ritor1
parents:
diff changeset
1116 /* Check for the failure cases where support has been disabled at compile
Ritor1
parents:
diff changeset
1117 * time. This code is hardly ever compiled - it's here because
Ritor1
parents:
diff changeset
1118 * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
Ritor1
parents:
diff changeset
1119 * code) but may be meaningless if the read or write handling of unknown
Ritor1
parents:
diff changeset
1120 * chunks is not compiled in.
Ritor1
parents:
diff changeset
1121 */
Ritor1
parents:
diff changeset
1122 # if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
Ritor1
parents:
diff changeset
1123 defined(PNG_READ_SUPPORTED)
Ritor1
parents:
diff changeset
1124 if (png_ptr->mode & PNG_IS_READ_STRUCT)
Ritor1
parents:
diff changeset
1125 {
Ritor1
parents:
diff changeset
1126 png_app_error(png_ptr, "no unknown chunk support on read");
Ritor1
parents:
diff changeset
1127 return;
Ritor1
parents:
diff changeset
1128 }
Ritor1
parents:
diff changeset
1129 # endif
Ritor1
parents:
diff changeset
1130 # if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
Ritor1
parents:
diff changeset
1131 defined(PNG_WRITE_SUPPORTED)
Ritor1
parents:
diff changeset
1132 if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
Ritor1
parents:
diff changeset
1133 {
Ritor1
parents:
diff changeset
1134 png_app_error(png_ptr, "no unknown chunk support on write");
Ritor1
parents:
diff changeset
1135 return;
Ritor1
parents:
diff changeset
1136 }
Ritor1
parents:
diff changeset
1137 # endif
Ritor1
parents:
diff changeset
1138
Ritor1
parents:
diff changeset
1139 /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
Ritor1
parents:
diff changeset
1140 * unknown critical chunks could be lost with just a warning resulting in
Ritor1
parents:
diff changeset
1141 * undefined behavior. Now png_chunk_report is used to provide behavior
Ritor1
parents:
diff changeset
1142 * appropriate to read or write.
Ritor1
parents:
diff changeset
1143 */
Ritor1
parents:
diff changeset
1144 np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
Ritor1
parents:
diff changeset
1145 info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
Ritor1
parents:
diff changeset
1146 sizeof *np));
Ritor1
parents:
diff changeset
1147
Ritor1
parents:
diff changeset
1148 if (np == NULL)
Ritor1
parents:
diff changeset
1149 {
Ritor1
parents:
diff changeset
1150 png_chunk_report(png_ptr, "too many unknown chunks",
Ritor1
parents:
diff changeset
1151 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
1152 return;
Ritor1
parents:
diff changeset
1153 }
Ritor1
parents:
diff changeset
1154
Ritor1
parents:
diff changeset
1155 png_free(png_ptr, info_ptr->unknown_chunks);
Ritor1
parents:
diff changeset
1156 info_ptr->unknown_chunks = np; /* safe because it is initialized */
Ritor1
parents:
diff changeset
1157 info_ptr->free_me |= PNG_FREE_UNKN;
Ritor1
parents:
diff changeset
1158
Ritor1
parents:
diff changeset
1159 np += info_ptr->unknown_chunks_num;
Ritor1
parents:
diff changeset
1160
Ritor1
parents:
diff changeset
1161 /* Increment unknown_chunks_num each time round the loop to protect the
Ritor1
parents:
diff changeset
1162 * just-allocated chunk data.
Ritor1
parents:
diff changeset
1163 */
Ritor1
parents:
diff changeset
1164 for (; num_unknowns > 0; --num_unknowns, ++unknowns)
Ritor1
parents:
diff changeset
1165 {
Ritor1
parents:
diff changeset
1166 memcpy(np->name, unknowns->name, (sizeof np->name));
Ritor1
parents:
diff changeset
1167 np->name[(sizeof np->name)-1] = '\0';
Ritor1
parents:
diff changeset
1168 np->location = check_location(png_ptr, unknowns->location);
Ritor1
parents:
diff changeset
1169
Ritor1
parents:
diff changeset
1170 if (unknowns->size == 0)
Ritor1
parents:
diff changeset
1171 {
Ritor1
parents:
diff changeset
1172 np->data = NULL;
Ritor1
parents:
diff changeset
1173 np->size = 0;
Ritor1
parents:
diff changeset
1174 }
Ritor1
parents:
diff changeset
1175
Ritor1
parents:
diff changeset
1176 else
Ritor1
parents:
diff changeset
1177 {
Ritor1
parents:
diff changeset
1178 np->data = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
1179 png_malloc_base(png_ptr, unknowns->size));
Ritor1
parents:
diff changeset
1180
Ritor1
parents:
diff changeset
1181 if (np->data == NULL)
Ritor1
parents:
diff changeset
1182 {
Ritor1
parents:
diff changeset
1183 png_chunk_report(png_ptr, "unknown chunk: out of memory",
Ritor1
parents:
diff changeset
1184 PNG_CHUNK_WRITE_ERROR);
Ritor1
parents:
diff changeset
1185 /* But just skip storing the unknown chunk */
Ritor1
parents:
diff changeset
1186 continue;
Ritor1
parents:
diff changeset
1187 }
Ritor1
parents:
diff changeset
1188
Ritor1
parents:
diff changeset
1189 memcpy(np->data, unknowns->data, unknowns->size);
Ritor1
parents:
diff changeset
1190 np->size = unknowns->size;
Ritor1
parents:
diff changeset
1191 }
Ritor1
parents:
diff changeset
1192
Ritor1
parents:
diff changeset
1193 /* These increments are skipped on out-of-memory for the data - the
Ritor1
parents:
diff changeset
1194 * unknown chunk entry gets overwritten if the png_chunk_report returns.
Ritor1
parents:
diff changeset
1195 * This is correct in the read case (the chunk is just dropped.)
Ritor1
parents:
diff changeset
1196 */
Ritor1
parents:
diff changeset
1197 ++np;
Ritor1
parents:
diff changeset
1198 ++(info_ptr->unknown_chunks_num);
Ritor1
parents:
diff changeset
1199 }
Ritor1
parents:
diff changeset
1200 }
Ritor1
parents:
diff changeset
1201
Ritor1
parents:
diff changeset
1202 void PNGAPI
Ritor1
parents:
diff changeset
1203 png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
1204 int chunk, int location)
Ritor1
parents:
diff changeset
1205 {
Ritor1
parents:
diff changeset
1206 /* This API is pretty pointless in 1.6.0 because the location can be set
Ritor1
parents:
diff changeset
1207 * before the call to png_set_unknown_chunks.
Ritor1
parents:
diff changeset
1208 *
Ritor1
parents:
diff changeset
1209 * TODO: add a png_app_warning in 1.7
Ritor1
parents:
diff changeset
1210 */
Ritor1
parents:
diff changeset
1211 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
Ritor1
parents:
diff changeset
1212 chunk < info_ptr->unknown_chunks_num)
Ritor1
parents:
diff changeset
1213 {
Ritor1
parents:
diff changeset
1214 if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
Ritor1
parents:
diff changeset
1215 {
Ritor1
parents:
diff changeset
1216 png_app_error(png_ptr, "invalid unknown chunk location");
Ritor1
parents:
diff changeset
1217 /* Fake out the pre 1.6.0 behavior: */
Ritor1
parents:
diff changeset
1218 if ((location & PNG_HAVE_IDAT)) /* undocumented! */
Ritor1
parents:
diff changeset
1219 location = PNG_AFTER_IDAT;
Ritor1
parents:
diff changeset
1220
Ritor1
parents:
diff changeset
1221 else
Ritor1
parents:
diff changeset
1222 location = PNG_HAVE_IHDR; /* also undocumented */
Ritor1
parents:
diff changeset
1223 }
Ritor1
parents:
diff changeset
1224
Ritor1
parents:
diff changeset
1225 info_ptr->unknown_chunks[chunk].location =
Ritor1
parents:
diff changeset
1226 check_location(png_ptr, location);
Ritor1
parents:
diff changeset
1227 }
Ritor1
parents:
diff changeset
1228 }
Ritor1
parents:
diff changeset
1229 #endif
Ritor1
parents:
diff changeset
1230
Ritor1
parents:
diff changeset
1231
Ritor1
parents:
diff changeset
1232 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
1233 png_uint_32 PNGAPI
Ritor1
parents:
diff changeset
1234 png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
Ritor1
parents:
diff changeset
1235 {
Ritor1
parents:
diff changeset
1236 png_debug(1, "in png_permit_mng_features");
Ritor1
parents:
diff changeset
1237
Ritor1
parents:
diff changeset
1238 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
1239 return 0;
Ritor1
parents:
diff changeset
1240
Ritor1
parents:
diff changeset
1241 png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
Ritor1
parents:
diff changeset
1242
Ritor1
parents:
diff changeset
1243 return png_ptr->mng_features_permitted;
Ritor1
parents:
diff changeset
1244 }
Ritor1
parents:
diff changeset
1245 #endif
Ritor1
parents:
diff changeset
1246
Ritor1
parents:
diff changeset
1247 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
1248 static unsigned int
Ritor1
parents:
diff changeset
1249 add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
Ritor1
parents:
diff changeset
1250 {
Ritor1
parents:
diff changeset
1251 unsigned int i;
Ritor1
parents:
diff changeset
1252
Ritor1
parents:
diff changeset
1253 /* Utility function: update the 'keep' state of a chunk if it is already in
Ritor1
parents:
diff changeset
1254 * the list, otherwise add it to the list.
Ritor1
parents:
diff changeset
1255 */
Ritor1
parents:
diff changeset
1256 for (i=0; i<count; ++i, list += 5) if (memcmp(list, add, 4) == 0)
Ritor1
parents:
diff changeset
1257 {
Ritor1
parents:
diff changeset
1258 list[4] = (png_byte)keep;
Ritor1
parents:
diff changeset
1259 return count;
Ritor1
parents:
diff changeset
1260 }
Ritor1
parents:
diff changeset
1261
Ritor1
parents:
diff changeset
1262 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
Ritor1
parents:
diff changeset
1263 {
Ritor1
parents:
diff changeset
1264 ++count;
Ritor1
parents:
diff changeset
1265 memcpy(list, add, 4);
Ritor1
parents:
diff changeset
1266 list[4] = (png_byte)keep;
Ritor1
parents:
diff changeset
1267 }
Ritor1
parents:
diff changeset
1268
Ritor1
parents:
diff changeset
1269 return count;
Ritor1
parents:
diff changeset
1270 }
Ritor1
parents:
diff changeset
1271
Ritor1
parents:
diff changeset
1272 void PNGAPI
Ritor1
parents:
diff changeset
1273 png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
Ritor1
parents:
diff changeset
1274 png_const_bytep chunk_list, int num_chunks_in)
Ritor1
parents:
diff changeset
1275 {
Ritor1
parents:
diff changeset
1276 png_bytep new_list;
Ritor1
parents:
diff changeset
1277 unsigned int num_chunks, old_num_chunks;
Ritor1
parents:
diff changeset
1278
Ritor1
parents:
diff changeset
1279 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
1280 return;
Ritor1
parents:
diff changeset
1281
Ritor1
parents:
diff changeset
1282 if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
Ritor1
parents:
diff changeset
1283 {
Ritor1
parents:
diff changeset
1284 png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
Ritor1
parents:
diff changeset
1285 return;
Ritor1
parents:
diff changeset
1286 }
Ritor1
parents:
diff changeset
1287
Ritor1
parents:
diff changeset
1288 if (num_chunks_in <= 0)
Ritor1
parents:
diff changeset
1289 {
Ritor1
parents:
diff changeset
1290 png_ptr->unknown_default = keep;
Ritor1
parents:
diff changeset
1291
Ritor1
parents:
diff changeset
1292 /* '0' means just set the flags, so stop here */
Ritor1
parents:
diff changeset
1293 if (num_chunks_in == 0)
Ritor1
parents:
diff changeset
1294 return;
Ritor1
parents:
diff changeset
1295 }
Ritor1
parents:
diff changeset
1296
Ritor1
parents:
diff changeset
1297 if (num_chunks_in < 0)
Ritor1
parents:
diff changeset
1298 {
Ritor1
parents:
diff changeset
1299 /* Ignore all unknown chunks and all chunks recognized by
Ritor1
parents:
diff changeset
1300 * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
Ritor1
parents:
diff changeset
1301 */
Ritor1
parents:
diff changeset
1302 static PNG_CONST png_byte chunks_to_ignore[] = {
Ritor1
parents:
diff changeset
1303 98, 75, 71, 68, '\0', /* bKGD */
Ritor1
parents:
diff changeset
1304 99, 72, 82, 77, '\0', /* cHRM */
Ritor1
parents:
diff changeset
1305 103, 65, 77, 65, '\0', /* gAMA */
Ritor1
parents:
diff changeset
1306 104, 73, 83, 84, '\0', /* hIST */
Ritor1
parents:
diff changeset
1307 105, 67, 67, 80, '\0', /* iCCP */
Ritor1
parents:
diff changeset
1308 105, 84, 88, 116, '\0', /* iTXt */
Ritor1
parents:
diff changeset
1309 111, 70, 70, 115, '\0', /* oFFs */
Ritor1
parents:
diff changeset
1310 112, 67, 65, 76, '\0', /* pCAL */
Ritor1
parents:
diff changeset
1311 112, 72, 89, 115, '\0', /* pHYs */
Ritor1
parents:
diff changeset
1312 115, 66, 73, 84, '\0', /* sBIT */
Ritor1
parents:
diff changeset
1313 115, 67, 65, 76, '\0', /* sCAL */
Ritor1
parents:
diff changeset
1314 115, 80, 76, 84, '\0', /* sPLT */
Ritor1
parents:
diff changeset
1315 115, 84, 69, 82, '\0', /* sTER */
Ritor1
parents:
diff changeset
1316 115, 82, 71, 66, '\0', /* sRGB */
Ritor1
parents:
diff changeset
1317 116, 69, 88, 116, '\0', /* tEXt */
Ritor1
parents:
diff changeset
1318 116, 73, 77, 69, '\0', /* tIME */
Ritor1
parents:
diff changeset
1319 122, 84, 88, 116, '\0' /* zTXt */
Ritor1
parents:
diff changeset
1320 };
Ritor1
parents:
diff changeset
1321
Ritor1
parents:
diff changeset
1322 chunk_list = chunks_to_ignore;
Ritor1
parents:
diff changeset
1323 num_chunks = (sizeof chunks_to_ignore)/5;
Ritor1
parents:
diff changeset
1324 }
Ritor1
parents:
diff changeset
1325
Ritor1
parents:
diff changeset
1326 else /* num_chunks_in > 0 */
Ritor1
parents:
diff changeset
1327 {
Ritor1
parents:
diff changeset
1328 if (chunk_list == NULL)
Ritor1
parents:
diff changeset
1329 {
Ritor1
parents:
diff changeset
1330 /* Prior to 1.6.0 this was silently ignored, now it is an app_error
Ritor1
parents:
diff changeset
1331 * which can be switched off.
Ritor1
parents:
diff changeset
1332 */
Ritor1
parents:
diff changeset
1333 png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
Ritor1
parents:
diff changeset
1334 return;
Ritor1
parents:
diff changeset
1335 }
Ritor1
parents:
diff changeset
1336
Ritor1
parents:
diff changeset
1337 num_chunks = num_chunks_in;
Ritor1
parents:
diff changeset
1338 }
Ritor1
parents:
diff changeset
1339
Ritor1
parents:
diff changeset
1340 old_num_chunks = png_ptr->num_chunk_list;
Ritor1
parents:
diff changeset
1341 if (png_ptr->chunk_list == NULL)
Ritor1
parents:
diff changeset
1342 old_num_chunks = 0;
Ritor1
parents:
diff changeset
1343
Ritor1
parents:
diff changeset
1344 /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
Ritor1
parents:
diff changeset
1345 */
Ritor1
parents:
diff changeset
1346 if (num_chunks + old_num_chunks > UINT_MAX/5)
Ritor1
parents:
diff changeset
1347 {
Ritor1
parents:
diff changeset
1348 png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
Ritor1
parents:
diff changeset
1349 return;
Ritor1
parents:
diff changeset
1350 }
Ritor1
parents:
diff changeset
1351
Ritor1
parents:
diff changeset
1352 /* If these chunks are being reset to the default then no more memory is
Ritor1
parents:
diff changeset
1353 * required because add_one_chunk above doesn't extend the list if the 'keep'
Ritor1
parents:
diff changeset
1354 * parameter is the default.
Ritor1
parents:
diff changeset
1355 */
Ritor1
parents:
diff changeset
1356 if (keep)
Ritor1
parents:
diff changeset
1357 {
Ritor1
parents:
diff changeset
1358 new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
Ritor1
parents:
diff changeset
1359 5 * (num_chunks + old_num_chunks)));
Ritor1
parents:
diff changeset
1360
Ritor1
parents:
diff changeset
1361 if (old_num_chunks > 0)
Ritor1
parents:
diff changeset
1362 memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
Ritor1
parents:
diff changeset
1363 }
Ritor1
parents:
diff changeset
1364
Ritor1
parents:
diff changeset
1365 else if (old_num_chunks > 0)
Ritor1
parents:
diff changeset
1366 new_list = png_ptr->chunk_list;
Ritor1
parents:
diff changeset
1367
Ritor1
parents:
diff changeset
1368 else
Ritor1
parents:
diff changeset
1369 new_list = NULL;
Ritor1
parents:
diff changeset
1370
Ritor1
parents:
diff changeset
1371 /* Add the new chunks together with each one's handling code. If the chunk
Ritor1
parents:
diff changeset
1372 * already exists the code is updated, otherwise the chunk is added to the
Ritor1
parents:
diff changeset
1373 * end. (In libpng 1.6.0 order no longer matters because this code enforces
Ritor1
parents:
diff changeset
1374 * the earlier convention that the last setting is the one that is used.)
Ritor1
parents:
diff changeset
1375 */
Ritor1
parents:
diff changeset
1376 if (new_list != NULL)
Ritor1
parents:
diff changeset
1377 {
Ritor1
parents:
diff changeset
1378 png_const_bytep inlist;
Ritor1
parents:
diff changeset
1379 png_bytep outlist;
Ritor1
parents:
diff changeset
1380 unsigned int i;
Ritor1
parents:
diff changeset
1381
Ritor1
parents:
diff changeset
1382 for (i=0; i<num_chunks; ++i)
Ritor1
parents:
diff changeset
1383 old_num_chunks = add_one_chunk(new_list, old_num_chunks,
Ritor1
parents:
diff changeset
1384 chunk_list+5*i, keep);
Ritor1
parents:
diff changeset
1385
Ritor1
parents:
diff changeset
1386 /* Now remove any spurious 'default' entries. */
Ritor1
parents:
diff changeset
1387 num_chunks = 0;
Ritor1
parents:
diff changeset
1388 for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
Ritor1
parents:
diff changeset
1389 if (inlist[4])
Ritor1
parents:
diff changeset
1390 {
Ritor1
parents:
diff changeset
1391 if (outlist != inlist)
Ritor1
parents:
diff changeset
1392 memcpy(outlist, inlist, 5);
Ritor1
parents:
diff changeset
1393 outlist += 5;
Ritor1
parents:
diff changeset
1394 ++num_chunks;
Ritor1
parents:
diff changeset
1395 }
Ritor1
parents:
diff changeset
1396
Ritor1
parents:
diff changeset
1397 /* This means the application has removed all the specialized handling. */
Ritor1
parents:
diff changeset
1398 if (num_chunks == 0)
Ritor1
parents:
diff changeset
1399 {
Ritor1
parents:
diff changeset
1400 if (png_ptr->chunk_list != new_list)
Ritor1
parents:
diff changeset
1401 png_free(png_ptr, new_list);
Ritor1
parents:
diff changeset
1402
Ritor1
parents:
diff changeset
1403 new_list = NULL;
Ritor1
parents:
diff changeset
1404 }
Ritor1
parents:
diff changeset
1405 }
Ritor1
parents:
diff changeset
1406
Ritor1
parents:
diff changeset
1407 else
Ritor1
parents:
diff changeset
1408 num_chunks = 0;
Ritor1
parents:
diff changeset
1409
Ritor1
parents:
diff changeset
1410 png_ptr->num_chunk_list = num_chunks;
Ritor1
parents:
diff changeset
1411
Ritor1
parents:
diff changeset
1412 if (png_ptr->chunk_list != new_list)
Ritor1
parents:
diff changeset
1413 {
Ritor1
parents:
diff changeset
1414 if (png_ptr->chunk_list != NULL)
Ritor1
parents:
diff changeset
1415 png_free(png_ptr, png_ptr->chunk_list);
Ritor1
parents:
diff changeset
1416
Ritor1
parents:
diff changeset
1417 png_ptr->chunk_list = new_list;
Ritor1
parents:
diff changeset
1418 }
Ritor1
parents:
diff changeset
1419 }
Ritor1
parents:
diff changeset
1420 #endif
Ritor1
parents:
diff changeset
1421
Ritor1
parents:
diff changeset
1422 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Ritor1
parents:
diff changeset
1423 void PNGAPI
Ritor1
parents:
diff changeset
1424 png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
Ritor1
parents:
diff changeset
1425 png_user_chunk_ptr read_user_chunk_fn)
Ritor1
parents:
diff changeset
1426 {
Ritor1
parents:
diff changeset
1427 png_debug(1, "in png_set_read_user_chunk_fn");
Ritor1
parents:
diff changeset
1428
Ritor1
parents:
diff changeset
1429 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
1430 return;
Ritor1
parents:
diff changeset
1431
Ritor1
parents:
diff changeset
1432 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
Ritor1
parents:
diff changeset
1433 png_ptr->user_chunk_ptr = user_chunk_ptr;
Ritor1
parents:
diff changeset
1434 }
Ritor1
parents:
diff changeset
1435 #endif
Ritor1
parents:
diff changeset
1436
Ritor1
parents:
diff changeset
1437 #ifdef PNG_INFO_IMAGE_SUPPORTED
Ritor1
parents:
diff changeset
1438 void PNGAPI
Ritor1
parents:
diff changeset
1439 png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
1440 png_bytepp row_pointers)
Ritor1
parents:
diff changeset
1441 {
Ritor1
parents:
diff changeset
1442 png_debug1(1, "in %s storage function", "rows");
Ritor1
parents:
diff changeset
1443
Ritor1
parents:
diff changeset
1444 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
1445 return;
Ritor1
parents:
diff changeset
1446
Ritor1
parents:
diff changeset
1447 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Ritor1
parents:
diff changeset
1448 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Ritor1
parents:
diff changeset
1449
Ritor1
parents:
diff changeset
1450 info_ptr->row_pointers = row_pointers;
Ritor1
parents:
diff changeset
1451
Ritor1
parents:
diff changeset
1452 if (row_pointers)
Ritor1
parents:
diff changeset
1453 info_ptr->valid |= PNG_INFO_IDAT;
Ritor1
parents:
diff changeset
1454 }
Ritor1
parents:
diff changeset
1455 #endif
Ritor1
parents:
diff changeset
1456
Ritor1
parents:
diff changeset
1457 void PNGAPI
Ritor1
parents:
diff changeset
1458 png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
Ritor1
parents:
diff changeset
1459 {
Ritor1
parents:
diff changeset
1460 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
1461 return;
Ritor1
parents:
diff changeset
1462
Ritor1
parents:
diff changeset
1463 if (size == 0 || size > PNG_UINT_31_MAX)
Ritor1
parents:
diff changeset
1464 png_error(png_ptr, "invalid compression buffer size");
Ritor1
parents:
diff changeset
1465
Ritor1
parents:
diff changeset
1466 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
1467 if (png_ptr->mode & PNG_IS_READ_STRUCT)
Ritor1
parents:
diff changeset
1468 {
Ritor1
parents:
diff changeset
1469 png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
Ritor1
parents:
diff changeset
1470 return;
Ritor1
parents:
diff changeset
1471 }
Ritor1
parents:
diff changeset
1472 # endif
Ritor1
parents:
diff changeset
1473
Ritor1
parents:
diff changeset
1474 # ifdef PNG_WRITE_SUPPORTED
Ritor1
parents:
diff changeset
1475 if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
Ritor1
parents:
diff changeset
1476 {
Ritor1
parents:
diff changeset
1477 if (png_ptr->zowner != 0)
Ritor1
parents:
diff changeset
1478 {
Ritor1
parents:
diff changeset
1479 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1480 "Compression buffer size cannot be changed because it is in use");
Ritor1
parents:
diff changeset
1481 return;
Ritor1
parents:
diff changeset
1482 }
Ritor1
parents:
diff changeset
1483
Ritor1
parents:
diff changeset
1484 if (size > ZLIB_IO_MAX)
Ritor1
parents:
diff changeset
1485 {
Ritor1
parents:
diff changeset
1486 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1487 "Compression buffer size limited to system maximum");
Ritor1
parents:
diff changeset
1488 size = ZLIB_IO_MAX; /* must fit */
Ritor1
parents:
diff changeset
1489 }
Ritor1
parents:
diff changeset
1490
Ritor1
parents:
diff changeset
1491 else if (size < 6)
Ritor1
parents:
diff changeset
1492 {
Ritor1
parents:
diff changeset
1493 /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
Ritor1
parents:
diff changeset
1494 * if this is permitted.
Ritor1
parents:
diff changeset
1495 */
Ritor1
parents:
diff changeset
1496 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1497 "Compression buffer size cannot be reduced below 6");
Ritor1
parents:
diff changeset
1498 return;
Ritor1
parents:
diff changeset
1499 }
Ritor1
parents:
diff changeset
1500
Ritor1
parents:
diff changeset
1501 if (png_ptr->zbuffer_size != size)
Ritor1
parents:
diff changeset
1502 {
Ritor1
parents:
diff changeset
1503 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
Ritor1
parents:
diff changeset
1504 png_ptr->zbuffer_size = (uInt)size;
Ritor1
parents:
diff changeset
1505 }
Ritor1
parents:
diff changeset
1506 }
Ritor1
parents:
diff changeset
1507 # endif
Ritor1
parents:
diff changeset
1508 }
Ritor1
parents:
diff changeset
1509
Ritor1
parents:
diff changeset
1510 void PNGAPI
Ritor1
parents:
diff changeset
1511 png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
Ritor1
parents:
diff changeset
1512 {
Ritor1
parents:
diff changeset
1513 if (png_ptr && info_ptr)
Ritor1
parents:
diff changeset
1514 info_ptr->valid &= ~mask;
Ritor1
parents:
diff changeset
1515 }
Ritor1
parents:
diff changeset
1516
Ritor1
parents:
diff changeset
1517
Ritor1
parents:
diff changeset
1518 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
Ritor1
parents:
diff changeset
1519 /* This function was added to libpng 1.2.6 */
Ritor1
parents:
diff changeset
1520 void PNGAPI
Ritor1
parents:
diff changeset
1521 png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
Ritor1
parents:
diff changeset
1522 png_uint_32 user_height_max)
Ritor1
parents:
diff changeset
1523 {
Ritor1
parents:
diff changeset
1524 /* Images with dimensions larger than these limits will be
Ritor1
parents:
diff changeset
1525 * rejected by png_set_IHDR(). To accept any PNG datastream
Ritor1
parents:
diff changeset
1526 * regardless of dimensions, set both limits to 0x7ffffffL.
Ritor1
parents:
diff changeset
1527 */
Ritor1
parents:
diff changeset
1528 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
1529 return;
Ritor1
parents:
diff changeset
1530
Ritor1
parents:
diff changeset
1531 png_ptr->user_width_max = user_width_max;
Ritor1
parents:
diff changeset
1532 png_ptr->user_height_max = user_height_max;
Ritor1
parents:
diff changeset
1533 }
Ritor1
parents:
diff changeset
1534
Ritor1
parents:
diff changeset
1535 /* This function was added to libpng 1.4.0 */
Ritor1
parents:
diff changeset
1536 void PNGAPI
Ritor1
parents:
diff changeset
1537 png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
Ritor1
parents:
diff changeset
1538 {
Ritor1
parents:
diff changeset
1539 if (png_ptr)
Ritor1
parents:
diff changeset
1540 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
Ritor1
parents:
diff changeset
1541 }
Ritor1
parents:
diff changeset
1542
Ritor1
parents:
diff changeset
1543 /* This function was added to libpng 1.4.1 */
Ritor1
parents:
diff changeset
1544 void PNGAPI
Ritor1
parents:
diff changeset
1545 png_set_chunk_malloc_max (png_structrp png_ptr,
Ritor1
parents:
diff changeset
1546 png_alloc_size_t user_chunk_malloc_max)
Ritor1
parents:
diff changeset
1547 {
Ritor1
parents:
diff changeset
1548 if (png_ptr)
Ritor1
parents:
diff changeset
1549 png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
Ritor1
parents:
diff changeset
1550 }
Ritor1
parents:
diff changeset
1551 #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
Ritor1
parents:
diff changeset
1552
Ritor1
parents:
diff changeset
1553
Ritor1
parents:
diff changeset
1554 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
Ritor1
parents:
diff changeset
1555 void PNGAPI
Ritor1
parents:
diff changeset
1556 png_set_benign_errors(png_structrp png_ptr, int allowed)
Ritor1
parents:
diff changeset
1557 {
Ritor1
parents:
diff changeset
1558 png_debug(1, "in png_set_benign_errors");
Ritor1
parents:
diff changeset
1559
Ritor1
parents:
diff changeset
1560 /* If allowed is 1, png_benign_error() is treated as a warning.
Ritor1
parents:
diff changeset
1561 *
Ritor1
parents:
diff changeset
1562 * If allowed is 0, png_benign_error() is treated as an error (which
Ritor1
parents:
diff changeset
1563 * is the default behavior if png_set_benign_errors() is not called).
Ritor1
parents:
diff changeset
1564 */
Ritor1
parents:
diff changeset
1565
Ritor1
parents:
diff changeset
1566 if (allowed)
Ritor1
parents:
diff changeset
1567 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
Ritor1
parents:
diff changeset
1568 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
Ritor1
parents:
diff changeset
1569
Ritor1
parents:
diff changeset
1570 else
Ritor1
parents:
diff changeset
1571 png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
Ritor1
parents:
diff changeset
1572 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
Ritor1
parents:
diff changeset
1573 }
Ritor1
parents:
diff changeset
1574 #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Ritor1
parents:
diff changeset
1575
Ritor1
parents:
diff changeset
1576 #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
Ritor1
parents:
diff changeset
1577 /* Whether to report invalid palette index; added at libng-1.5.10.
Ritor1
parents:
diff changeset
1578 * It is possible for an indexed (color-type==3) PNG file to contain
Ritor1
parents:
diff changeset
1579 * pixels with invalid (out-of-range) indexes if the PLTE chunk has
Ritor1
parents:
diff changeset
1580 * fewer entries than the image's bit-depth would allow. We recover
Ritor1
parents:
diff changeset
1581 * from this gracefully by filling any incomplete palette with zeroes
Ritor1
parents:
diff changeset
1582 * (opaque black). By default, when this occurs libpng will issue
Ritor1
parents:
diff changeset
1583 * a benign error. This API can be used to override that behavior.
Ritor1
parents:
diff changeset
1584 */
Ritor1
parents:
diff changeset
1585 void PNGAPI
Ritor1
parents:
diff changeset
1586 png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
Ritor1
parents:
diff changeset
1587 {
Ritor1
parents:
diff changeset
1588 png_debug(1, "in png_set_check_for_invalid_index");
Ritor1
parents:
diff changeset
1589
Ritor1
parents:
diff changeset
1590 if (allowed > 0)
Ritor1
parents:
diff changeset
1591 png_ptr->num_palette_max = 0;
Ritor1
parents:
diff changeset
1592
Ritor1
parents:
diff changeset
1593 else
Ritor1
parents:
diff changeset
1594 png_ptr->num_palette_max = -1;
Ritor1
parents:
diff changeset
1595 }
Ritor1
parents:
diff changeset
1596 #endif
Ritor1
parents:
diff changeset
1597 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */