2 /* pngset.c - storage of image information into info struct
4 * Last changed in libpng 1.2.34 [December 18, 2008]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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.
18 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
20 #if defined(PNG_bKGD_SUPPORTED)
22 png_set_bKGD(png_structp png_ptr
, png_infop info_ptr
, png_color_16p background
)
24 png_debug1(1, "in %s storage function", "bKGD");
25 if (png_ptr
== NULL
|| info_ptr
== NULL
)
28 png_memcpy(&(info_ptr
->background
), background
, png_sizeof(png_color_16
));
29 info_ptr
->valid
|= PNG_INFO_bKGD
;
33 #if defined(PNG_cHRM_SUPPORTED)
34 #ifdef PNG_FLOATING_POINT_SUPPORTED
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
)
40 png_debug1(1, "in %s storage function", "cHRM");
41 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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);
62 info_ptr
->valid
|= PNG_INFO_cHRM
;
65 #ifdef PNG_FIXED_POINT_SUPPORTED
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
)
72 png_debug1(1, "in %s storage function", "cHRM fixed");
73 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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
))
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
;
89 #ifdef PNG_FLOATING_POINT_SUPPORTED
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.);
99 info_ptr
->valid
|= PNG_INFO_cHRM
;
102 #endif /* PNG_FIXED_POINT_SUPPORTED */
103 #endif /* PNG_cHRM_SUPPORTED */
105 #if defined(PNG_gAMA_SUPPORTED)
106 #ifdef PNG_FLOATING_POINT_SUPPORTED
108 png_set_gAMA(png_structp png_ptr
, png_infop info_ptr
, double file_gamma
)
111 png_debug1(1, "in %s storage function", "gAMA");
112 if (png_ptr
== NULL
|| info_ptr
== NULL
)
115 /* Check for overflow */
116 if (file_gamma
> 21474.83)
118 png_warning(png_ptr
, "Limiting gamma to 21474.83");
123 info_ptr
->gamma
= (float)gamma
;
124 #ifdef PNG_FIXED_POINT_SUPPORTED
125 info_ptr
->int_gamma
= (int)(gamma
*100000.+.5);
127 info_ptr
->valid
|= PNG_INFO_gAMA
;
129 png_warning(png_ptr
, "Setting gamma=0");
133 png_set_gAMA_fixed(png_structp png_ptr
, png_infop info_ptr
, png_fixed_point
136 png_fixed_point gamma
;
138 png_debug1(1, "in %s storage function", "gAMA");
139 if (png_ptr
== NULL
|| info_ptr
== NULL
)
142 if (int_gamma
> (png_fixed_point
) PNG_UINT_31_MAX
)
144 png_warning(png_ptr
, "Limiting gamma to 21474.83");
145 gamma
=PNG_UINT_31_MAX
;
151 png_warning(png_ptr
, "Setting negative gamma to zero");
157 #ifdef PNG_FLOATING_POINT_SUPPORTED
158 info_ptr
->gamma
= (float)(gamma
/100000.);
160 #ifdef PNG_FIXED_POINT_SUPPORTED
161 info_ptr
->int_gamma
= gamma
;
163 info_ptr
->valid
|= PNG_INFO_gAMA
;
165 png_warning(png_ptr
, "Setting gamma=0");
169 #if defined(PNG_hIST_SUPPORTED)
171 png_set_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_16p hist
)
175 png_debug1(1, "in %s storage function", "hIST");
176 if (png_ptr
== NULL
|| info_ptr
== NULL
)
178 if (info_ptr
->num_palette
== 0 || info_ptr
->num_palette
179 > PNG_MAX_PALETTE_LENGTH
)
182 "Invalid palette size, hIST allocation skipped.");
186 #ifdef PNG_FREE_ME_SUPPORTED
187 png_free_data(png_ptr
, info_ptr
, PNG_FREE_HIST
, 0);
189 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
191 png_ptr
->hist
= (png_uint_16p
)png_malloc_warn(png_ptr
,
192 (png_uint_32
)(PNG_MAX_PALETTE_LENGTH
* png_sizeof(png_uint_16
)));
193 if (png_ptr
->hist
== NULL
)
195 png_warning(png_ptr
, "Insufficient memory for hIST chunk data.");
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
;
204 #ifdef PNG_FREE_ME_SUPPORTED
205 info_ptr
->free_me
|= PNG_FREE_HIST
;
207 png_ptr
->flags
|= PNG_FLAG_FREE_HIST
;
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
,
218 png_debug1(1, "in %s storage function", "IHDR");
219 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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");
229 if (width
> PNG_USER_WIDTH_MAX
|| height
> PNG_USER_HEIGHT_MAX
)
230 png_error(png_ptr
, "image size exceeds user limits in IHDR");
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");
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");
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");
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");
257 if (interlace_type
>= PNG_INTERLACE_LAST
)
258 png_error(png_ptr
, "Unknown interlace method in IHDR");
260 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
261 png_error(png_ptr
, "Unknown compression method in IHDR");
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
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
)
277 if (!((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
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");
283 if (png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
)
284 png_warning(png_ptr
, "Invalid filter method in IHDR");
287 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
288 png_error(png_ptr
, "Unknown filter method in IHDR");
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;
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
);
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;
317 info_ptr
->rowbytes
= PNG_ROWBYTES(info_ptr
->pixel_depth
, width
);
320 #if defined(PNG_oFFs_SUPPORTED)
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
)
325 png_debug1(1, "in %s storage function", "oFFs");
326 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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
;
336 #if defined(PNG_pCAL_SUPPORTED)
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
)
345 png_debug1(1, "in %s storage function", "pCAL");
346 if (png_ptr
== NULL
|| info_ptr
== NULL
)
349 length
= png_strlen(purpose
) + 1;
350 png_debug1(3, "allocating purpose for info (%lu bytes)",
351 (unsigned long)length
);
352 info_ptr
->pcal_purpose
= (png_charp
)png_malloc_warn(png_ptr
, length
);
353 if (info_ptr
->pcal_purpose
== NULL
)
355 png_warning(png_ptr
, "Insufficient memory for pCAL purpose.");
358 png_memcpy(info_ptr
->pcal_purpose
, purpose
, (png_size_t
)length
);
360 png_debug(3, "storing X0, X1, type, and nparams in info");
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
;
366 length
= png_strlen(units
) + 1;
367 png_debug1(3, "allocating units for info (%lu bytes)",
368 (unsigned long)length
);
369 info_ptr
->pcal_units
= (png_charp
)png_malloc_warn(png_ptr
, length
);
370 if (info_ptr
->pcal_units
== NULL
)
372 png_warning(png_ptr
, "Insufficient memory for pCAL units.");
375 png_memcpy(info_ptr
->pcal_units
, units
, (png_size_t
)length
);
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
)
381 png_warning(png_ptr
, "Insufficient memory for pCAL params.");
385 info_ptr
->pcal_params
[nparams
] = NULL
;
387 for (i
= 0; i
< nparams
; i
++)
389 length
= png_strlen(params
[i
]) + 1;
390 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i
,
391 (unsigned long)length
);
392 info_ptr
->pcal_params
[i
] = (png_charp
)png_malloc_warn(png_ptr
, length
);
393 if (info_ptr
->pcal_params
[i
] == NULL
)
395 png_warning(png_ptr
, "Insufficient memory for pCAL parameter.");
398 png_memcpy(info_ptr
->pcal_params
[i
], params
[i
], (png_size_t
)length
);
401 info_ptr
->valid
|= PNG_INFO_pCAL
;
402 #ifdef PNG_FREE_ME_SUPPORTED
403 info_ptr
->free_me
|= PNG_FREE_PCAL
;
408 #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
409 #ifdef PNG_FLOATING_POINT_SUPPORTED
411 png_set_sCAL(png_structp png_ptr
, png_infop info_ptr
,
412 int unit
, double width
, double height
)
414 png_debug1(1, "in %s storage function", "sCAL");
415 if (png_ptr
== NULL
|| info_ptr
== NULL
)
418 info_ptr
->scal_unit
= (png_byte
)unit
;
419 info_ptr
->scal_pixel_width
= width
;
420 info_ptr
->scal_pixel_height
= height
;
422 info_ptr
->valid
|= PNG_INFO_sCAL
;
425 #ifdef PNG_FIXED_POINT_SUPPORTED
427 png_set_sCAL_s(png_structp png_ptr
, png_infop info_ptr
,
428 int unit
, png_charp swidth
, png_charp sheight
)
432 png_debug1(1, "in %s storage function", "sCAL");
433 if (png_ptr
== NULL
|| info_ptr
== NULL
)
436 info_ptr
->scal_unit
= (png_byte
)unit
;
438 length
= png_strlen(swidth
) + 1;
439 png_debug1(3, "allocating unit for info (%u bytes)",
440 (unsigned int)length
);
441 info_ptr
->scal_s_width
= (png_charp
)png_malloc_warn(png_ptr
, length
);
442 if (info_ptr
->scal_s_width
== NULL
)
445 "Memory allocation failed while processing sCAL.");
448 png_memcpy(info_ptr
->scal_s_width
, swidth
, (png_size_t
)length
);
450 length
= png_strlen(sheight
) + 1;
451 png_debug1(3, "allocating unit for info (%u bytes)",
452 (unsigned int)length
);
453 info_ptr
->scal_s_height
= (png_charp
)png_malloc_warn(png_ptr
, length
);
454 if (info_ptr
->scal_s_height
== NULL
)
456 png_free (png_ptr
, info_ptr
->scal_s_width
);
457 info_ptr
->scal_s_width
= NULL
;
459 "Memory allocation failed while processing sCAL.");
462 png_memcpy(info_ptr
->scal_s_height
, sheight
, (png_size_t
)length
);
463 info_ptr
->valid
|= PNG_INFO_sCAL
;
464 #ifdef PNG_FREE_ME_SUPPORTED
465 info_ptr
->free_me
|= PNG_FREE_SCAL
;
472 #if defined(PNG_pHYs_SUPPORTED)
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
)
477 png_debug1(1, "in %s storage function", "pHYs");
478 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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
;
489 png_set_PLTE(png_structp png_ptr
, png_infop info_ptr
,
490 png_colorp palette
, int num_palette
)
493 png_debug1(1, "in %s storage function", "PLTE");
494 if (png_ptr
== NULL
|| info_ptr
== NULL
)
497 if (num_palette
< 0 || num_palette
> PNG_MAX_PALETTE_LENGTH
)
499 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
500 png_error(png_ptr
, "Invalid palette length");
503 png_warning(png_ptr
, "Invalid palette length");
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.
513 #ifdef PNG_FREE_ME_SUPPORTED
514 png_free_data(png_ptr
, info_ptr
, PNG_FREE_PLTE
, 0);
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
));
524 png_memcpy(png_ptr
->palette
, palette
, num_palette
* png_sizeof(png_color
));
525 info_ptr
->palette
= png_ptr
->palette
;
526 info_ptr
->num_palette
= png_ptr
->num_palette
= (png_uint_16
)num_palette
;
528 #ifdef PNG_FREE_ME_SUPPORTED
529 info_ptr
->free_me
|= PNG_FREE_PLTE
;
531 png_ptr
->flags
|= PNG_FLAG_FREE_PLTE
;
534 info_ptr
->valid
|= PNG_INFO_PLTE
;
537 #if defined(PNG_sBIT_SUPPORTED)
539 png_set_sBIT(png_structp png_ptr
, png_infop info_ptr
,
540 png_color_8p sig_bit
)
542 png_debug1(1, "in %s storage function", "sBIT");
543 if (png_ptr
== NULL
|| info_ptr
== NULL
)
546 png_memcpy(&(info_ptr
->sig_bit
), sig_bit
, png_sizeof(png_color_8
));
547 info_ptr
->valid
|= PNG_INFO_sBIT
;
551 #if defined(PNG_sRGB_SUPPORTED)
553 png_set_sRGB(png_structp png_ptr
, png_infop info_ptr
, int intent
)
555 png_debug1(1, "in %s storage function", "sRGB");
556 if (png_ptr
== NULL
|| info_ptr
== NULL
)
559 info_ptr
->srgb_intent
= (png_byte
)intent
;
560 info_ptr
->valid
|= PNG_INFO_sRGB
;
564 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr
, png_infop info_ptr
,
567 #if defined(PNG_gAMA_SUPPORTED)
568 #ifdef PNG_FLOATING_POINT_SUPPORTED
571 #ifdef PNG_FIXED_POINT_SUPPORTED
572 png_fixed_point int_file_gamma
;
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
;
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
;
582 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
583 if (png_ptr
== NULL
|| info_ptr
== NULL
)
586 png_set_sRGB(png_ptr
, info_ptr
, intent
);
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
);
593 #ifdef PNG_FIXED_POINT_SUPPORTED
594 int_file_gamma
= 45455L;
595 png_set_gAMA_fixed(png_ptr
, info_ptr
, int_file_gamma
);
599 #if defined(PNG_cHRM_SUPPORTED)
600 int_white_x
= 31270L;
601 int_white_y
= 32900L;
604 int_green_x
= 30000L;
605 int_green_y
= 60000L;
609 #ifdef PNG_FLOATING_POINT_SUPPORTED
610 white_x
= (float).3127;
611 white_y
= (float).3290;
614 green_x
= (float).30;
615 green_y
= (float).60;
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
))
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
);
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
);
641 #if defined(PNG_iCCP_SUPPORTED)
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
)
647 png_charp new_iccp_name
;
648 png_charp new_iccp_profile
;
651 png_debug1(1, "in %s storage function", "iCCP");
652 if (png_ptr
== NULL
|| info_ptr
== NULL
|| name
== NULL
|| profile
== NULL
)
655 length
= png_strlen(name
)+1;
656 new_iccp_name
= (png_charp
)png_malloc_warn(png_ptr
, length
);
657 if (new_iccp_name
== NULL
)
659 png_warning(png_ptr
, "Insufficient memory to process iCCP chunk.");
662 png_memcpy(new_iccp_name
, name
, length
);
663 new_iccp_profile
= (png_charp
)png_malloc_warn(png_ptr
, proflen
);
664 if (new_iccp_profile
== NULL
)
666 png_free (png_ptr
, new_iccp_name
);
668 "Insufficient memory to process iCCP profile.");
671 png_memcpy(new_iccp_profile
, profile
, (png_size_t
)proflen
);
673 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ICCP
, 0);
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
;
684 info_ptr
->valid
|= PNG_INFO_iCCP
;
688 #if defined(PNG_TEXT_SUPPORTED)
690 png_set_text(png_structp png_ptr
, png_infop info_ptr
, png_textp text_ptr
,
694 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, num_text
);
696 png_error(png_ptr
, "Insufficient memory to store text");
700 png_set_text_2(png_structp png_ptr
, png_infop info_ptr
, png_textp text_ptr
,
705 png_debug1(1, "in %s storage function", (png_ptr
->chunk_name
[0] == '\0' ?
706 "text" : (png_const_charp
)png_ptr
->chunk_name
));
708 if (png_ptr
== NULL
|| info_ptr
== NULL
|| num_text
== 0)
711 /* Make sure we have enough space in the "text" array in info_struct
712 * to hold all of the incoming text_ptr objects.
714 if (info_ptr
->num_text
+ num_text
> info_ptr
->max_text
)
716 if (info_ptr
->text
!= NULL
)
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
,
725 (png_uint_32
)(info_ptr
->max_text
* png_sizeof(png_text
)));
726 if (info_ptr
->text
== NULL
)
728 png_free(png_ptr
, old_text
);
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
);
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
,
740 (png_uint_32
)(info_ptr
->max_text
* png_sizeof(png_text
)));
741 if (info_ptr
->text
== NULL
)
743 #ifdef PNG_FREE_ME_SUPPORTED
744 info_ptr
->free_me
|= PNG_FREE_TEXT
;
747 png_debug1(3, "allocated %d entries for info_ptr->text",
750 for (i
= 0; i
< num_text
; i
++)
752 png_size_t text_length
, key_len
;
753 png_size_t lang_len
, lang_key_len
;
754 png_textp textp
= &(info_ptr
->text
[info_ptr
->num_text
]);
756 if (text_ptr
[i
].key
== NULL
)
759 key_len
= png_strlen(text_ptr
[i
].key
);
761 if (text_ptr
[i
].compression
<= 0)
767 #ifdef PNG_iTXt_SUPPORTED
770 if (text_ptr
[i
].lang
!= NULL
)
771 lang_len
= png_strlen(text_ptr
[i
].lang
);
774 if (text_ptr
[i
].lang_key
!= NULL
)
775 lang_key_len
= png_strlen(text_ptr
[i
].lang_key
);
781 png_warning(png_ptr
, "iTXt chunk not supported.");
786 if (text_ptr
[i
].text
== NULL
|| text_ptr
[i
].text
[0] == '\0')
789 #ifdef PNG_iTXt_SUPPORTED
790 if (text_ptr
[i
].compression
> 0)
791 textp
->compression
= PNG_ITXT_COMPRESSION_NONE
;
794 textp
->compression
= PNG_TEXT_COMPRESSION_NONE
;
798 text_length
= png_strlen(text_ptr
[i
].text
);
799 textp
->compression
= text_ptr
[i
].compression
;
802 textp
->key
= (png_charp
)png_malloc_warn(png_ptr
,
804 (key_len
+ text_length
+ lang_len
+ lang_key_len
+ 4));
805 if (textp
->key
== NULL
)
807 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
809 (key_len
+ lang_len
+ lang_key_len
+ text_length
+ 4),
812 png_memcpy(textp
->key
, text_ptr
[i
].key
,
813 (png_size_t
)(key_len
));
814 *(textp
->key
+ key_len
) = '\0';
815 #ifdef PNG_iTXt_SUPPORTED
816 if (text_ptr
[i
].compression
> 0)
818 textp
->lang
= textp
->key
+ key_len
+ 1;
819 png_memcpy(textp
->lang
, text_ptr
[i
].lang
, lang_len
);
820 *(textp
->lang
+ lang_len
) = '\0';
821 textp
->lang_key
= textp
->lang
+ lang_len
+ 1;
822 png_memcpy(textp
->lang_key
, text_ptr
[i
].lang_key
, lang_key_len
);
823 *(textp
->lang_key
+ lang_key_len
) = '\0';
824 textp
->text
= textp
->lang_key
+ lang_key_len
+ 1;
829 #ifdef PNG_iTXt_SUPPORTED
831 textp
->lang_key
=NULL
;
833 textp
->text
= textp
->key
+ key_len
+ 1;
836 png_memcpy(textp
->text
, text_ptr
[i
].text
,
837 (png_size_t
)(text_length
));
838 *(textp
->text
+ text_length
) = '\0';
840 #ifdef PNG_iTXt_SUPPORTED
841 if (textp
->compression
> 0)
843 textp
->text_length
= 0;
844 textp
->itxt_length
= text_length
;
849 textp
->text_length
= text_length
;
850 #ifdef PNG_iTXt_SUPPORTED
851 textp
->itxt_length
= 0;
854 info_ptr
->num_text
++;
855 png_debug1(3, "transferred text chunk %d", info_ptr
->num_text
);
861 #if defined(PNG_tIME_SUPPORTED)
863 png_set_tIME(png_structp png_ptr
, png_infop info_ptr
, png_timep mod_time
)
865 png_debug1(1, "in %s storage function", "tIME");
866 if (png_ptr
== NULL
|| info_ptr
== NULL
||
867 (png_ptr
->mode
& PNG_WROTE_tIME
))
870 png_memcpy(&(info_ptr
->mod_time
), mod_time
, png_sizeof(png_time
));
871 info_ptr
->valid
|= PNG_INFO_tIME
;
875 #if defined(PNG_tRNS_SUPPORTED)
877 png_set_tRNS(png_structp png_ptr
, png_infop info_ptr
,
878 png_bytep trans
, int num_trans
, png_color_16p trans_values
)
880 png_debug1(1, "in %s storage function", "tRNS");
881 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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.
892 #ifdef PNG_FREE_ME_SUPPORTED
893 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TRNS
, 0);
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
);
899 if (num_trans
> 0 && num_trans
<= PNG_MAX_PALETTE_LENGTH
)
900 png_memcpy(info_ptr
->trans
, trans
, (png_size_t
)num_trans
);
903 if (trans_values
!= NULL
)
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
)))
913 "tRNS chunk has out-of-range samples for bit_depth");
914 png_memcpy(&(info_ptr
->trans_values
), trans_values
,
915 png_sizeof(png_color_16
));
920 info_ptr
->num_trans
= (png_uint_16
)num_trans
;
923 info_ptr
->valid
|= PNG_INFO_tRNS
;
924 #ifdef PNG_FREE_ME_SUPPORTED
925 info_ptr
->free_me
|= PNG_FREE_TRNS
;
927 png_ptr
->flags
|= PNG_FLAG_FREE_TRNS
;
933 #if defined(PNG_sPLT_SUPPORTED)
935 png_set_sPLT(png_structp png_ptr
,
936 png_infop info_ptr
, png_sPLT_tp entries
, int nentries
)
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
948 if (png_ptr
== NULL
|| info_ptr
== NULL
)
951 np
= (png_sPLT_tp
)png_malloc_warn(png_ptr
,
952 (info_ptr
->splt_palettes_num
+ nentries
) *
953 (png_uint_32
)png_sizeof(png_sPLT_t
));
956 png_warning(png_ptr
, "No memory for sPLT palettes.");
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
;
965 for (i
= 0; i
< nentries
; i
++)
967 png_sPLT_tp to
= np
+ info_ptr
->splt_palettes_num
+ i
;
968 png_sPLT_tp from
= entries
+ i
;
971 length
= png_strlen(from
->name
) + 1;
972 to
->name
= (png_charp
)png_malloc_warn(png_ptr
, length
);
973 if (to
->name
== NULL
)
976 "Out of memory while processing sPLT chunk");
979 png_memcpy(to
->name
, from
->name
, length
);
980 to
->entries
= (png_sPLT_entryp
)png_malloc_warn(png_ptr
,
981 (png_uint_32
)(from
->nentries
* png_sizeof(png_sPLT_entry
)));
982 if (to
->entries
== NULL
)
985 "Out of memory while processing sPLT chunk");
986 png_free(png_ptr
, to
->name
);
990 png_memcpy(to
->entries
, from
->entries
,
991 from
->nentries
* png_sizeof(png_sPLT_entry
));
992 to
->nentries
= from
->nentries
;
993 to
->depth
= from
->depth
;
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
;
1003 #endif /* PNG_sPLT_SUPPORTED */
1005 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1007 png_set_unknown_chunks(png_structp png_ptr
,
1008 png_infop info_ptr
, png_unknown_chunkp unknowns
, int num_unknowns
)
1010 png_unknown_chunkp np
;
1013 if (png_ptr
== NULL
|| info_ptr
== NULL
|| num_unknowns
== 0)
1016 np
= (png_unknown_chunkp
)png_malloc_warn(png_ptr
,
1017 (png_uint_32
)((info_ptr
->unknown_chunks_num
+ num_unknowns
) *
1018 png_sizeof(png_unknown_chunk
)));
1021 png_warning(png_ptr
,
1022 "Out of memory while processing unknown chunk.");
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
;
1031 for (i
= 0; i
< num_unknowns
; i
++)
1033 png_unknown_chunkp to
= np
+ info_ptr
->unknown_chunks_num
+ i
;
1034 png_unknown_chunkp from
= unknowns
+ i
;
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);
1044 if (from
->size
== 0)
1048 to
->data
= (png_bytep
)png_malloc_warn(png_ptr
,
1049 (png_uint_32
)from
->size
);
1050 if (to
->data
== NULL
)
1052 png_warning(png_ptr
,
1053 "Out of memory while processing unknown chunk.");
1057 png_memcpy(to
->data
, from
->data
, from
->size
);
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
;
1068 png_set_unknown_chunk_location(png_structp png_ptr
, png_infop info_ptr
,
1069 int chunk
, int location
)
1071 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& chunk
>= 0 && chunk
<
1072 (int)info_ptr
->unknown_chunks_num
)
1073 info_ptr
->unknown_chunks
[chunk
].location
= (png_byte
)location
;
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)
1081 png_permit_empty_plte (png_structp png_ptr
, int empty_plte_permitted
)
1083 /* This function is deprecated in favor of png_permit_mng_features()
1084 and will be removed from libpng-1.3.0 */
1085 png_debug(1, "in png_permit_empty_plte, DEPRECATED.");
1086 if (png_ptr
== NULL
)
1088 png_ptr
->mng_features_permitted
= (png_byte
)
1089 ((png_ptr
->mng_features_permitted
& (~PNG_FLAG_MNG_EMPTY_PLTE
)) |
1090 ((empty_plte_permitted
& PNG_FLAG_MNG_EMPTY_PLTE
)));
1095 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1097 png_permit_mng_features (png_structp png_ptr
, png_uint_32 mng_features
)
1099 png_debug(1, "in png_permit_mng_features");
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
;
1108 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1110 png_set_keep_unknown_chunks(png_structp png_ptr
, int keep
, png_bytep
1111 chunk_list
, int num_chunks
)
1113 png_bytep new_list
, p
;
1114 int i
, old_num_chunks
;
1115 if (png_ptr
== NULL
)
1117 if (num_chunks
== 0)
1119 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
|| keep
== PNG_HANDLE_CHUNK_IF_SAFE
)
1120 png_ptr
->flags
|= PNG_FLAG_KEEP_UNKNOWN_CHUNKS
;
1122 png_ptr
->flags
&= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS
;
1124 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
)
1125 png_ptr
->flags
|= PNG_FLAG_KEEP_UNSAFE_CHUNKS
;
1127 png_ptr
->flags
&= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS
;
1130 if (chunk_list
== NULL
)
1132 old_num_chunks
= png_ptr
->num_chunk_list
;
1133 new_list
=(png_bytep
)png_malloc(png_ptr
,
1135 (5*(num_chunks
+ old_num_chunks
)));
1136 if (png_ptr
->chunk_list
!= NULL
)
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
;
1143 png_memcpy(new_list
+ 5*old_num_chunks
, chunk_list
,
1144 (png_size_t
)(5*num_chunks
));
1145 for (p
= new_list
+ 5*old_num_chunks
+ 4, i
= 0; i
<num_chunks
; i
++, p
+= 5)
1147 png_ptr
->num_chunk_list
= old_num_chunks
+ num_chunks
;
1148 png_ptr
->chunk_list
= new_list
;
1149 #ifdef PNG_FREE_ME_SUPPORTED
1150 png_ptr
->free_me
|= PNG_FREE_LIST
;
1155 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
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
)
1160 png_debug(1, "in png_set_read_user_chunk_fn");
1161 if (png_ptr
== NULL
)
1163 png_ptr
->read_user_chunk_fn
= read_user_chunk_fn
;
1164 png_ptr
->user_chunk_ptr
= user_chunk_ptr
;
1168 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1170 png_set_rows(png_structp png_ptr
, png_infop info_ptr
, png_bytepp row_pointers
)
1172 png_debug1(1, "in %s storage function", "rows");
1174 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1177 if (info_ptr
->row_pointers
&& (info_ptr
->row_pointers
!= row_pointers
))
1178 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1179 info_ptr
->row_pointers
= row_pointers
;
1181 info_ptr
->valid
|= PNG_INFO_IDAT
;
1185 #ifdef PNG_WRITE_SUPPORTED
1187 png_set_compression_buffer_size(png_structp png_ptr
,
1190 if (png_ptr
== NULL
)
1192 png_free(png_ptr
, png_ptr
->zbuf
);
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
;
1201 png_set_invalid(png_structp png_ptr
, png_infop info_ptr
, int mask
)
1203 if (png_ptr
&& info_ptr
)
1204 info_ptr
->valid
&= ~mask
;
1209 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
1210 /* function was added to libpng 1.2.0 and should always exist by default */
1212 png_set_asm_flags (png_structp png_ptr
, png_uint_32 asm_flags
)
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;
1217 asm_flags
= asm_flags
; /* Quiet the compiler */
1220 /* this function was added to libpng 1.2.0 */
1222 png_set_mmx_thresholds (png_structp png_ptr
,
1223 png_byte mmx_bitdepth_threshold
,
1224 png_uint_32 mmx_rowbytes_threshold
)
1226 /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
1227 if (png_ptr
== NULL
)
1229 /* Quiet the compiler */
1230 mmx_bitdepth_threshold
= mmx_bitdepth_threshold
;
1231 mmx_rowbytes_threshold
= mmx_rowbytes_threshold
;
1233 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
1235 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
1236 /* this function was added to libpng 1.2.6 */
1238 png_set_user_limits (png_structp png_ptr
, png_uint_32 user_width_max
,
1239 png_uint_32 user_height_max
)
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.
1245 if (png_ptr
== NULL
) return;
1246 png_ptr
->user_width_max
= user_width_max
;
1247 png_ptr
->user_height_max
= user_height_max
;
1249 #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1251 #endif /* ?PNG_1_0_X */
1252 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */