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