2 /* pngset.c - storage of image information into info struct
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 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
;
64 #endif /* PNG_FLOATING_POINT_SUPPORTED */
66 #ifdef PNG_FIXED_POINT_SUPPORTED
68 png_set_cHRM_fixed(png_structp png_ptr
, png_infop info_ptr
,
69 png_fixed_point white_x
, png_fixed_point white_y
, png_fixed_point red_x
,
70 png_fixed_point red_y
, png_fixed_point green_x
, png_fixed_point green_y
,
71 png_fixed_point blue_x
, png_fixed_point blue_y
)
73 png_debug1(1, "in %s storage function", "cHRM fixed");
74 if (png_ptr
== NULL
|| info_ptr
== NULL
)
77 #if !defined(PNG_NO_CHECK_cHRM)
78 if (png_check_cHRM_fixed(png_ptr
,
79 white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
))
82 info_ptr
->int_x_white
= white_x
;
83 info_ptr
->int_y_white
= white_y
;
84 info_ptr
->int_x_red
= red_x
;
85 info_ptr
->int_y_red
= red_y
;
86 info_ptr
->int_x_green
= green_x
;
87 info_ptr
->int_y_green
= green_y
;
88 info_ptr
->int_x_blue
= blue_x
;
89 info_ptr
->int_y_blue
= blue_y
;
90 #ifdef PNG_FLOATING_POINT_SUPPORTED
91 info_ptr
->x_white
= (float)(white_x
/100000.);
92 info_ptr
->y_white
= (float)(white_y
/100000.);
93 info_ptr
->x_red
= (float)( red_x
/100000.);
94 info_ptr
->y_red
= (float)( red_y
/100000.);
95 info_ptr
->x_green
= (float)(green_x
/100000.);
96 info_ptr
->y_green
= (float)(green_y
/100000.);
97 info_ptr
->x_blue
= (float)( blue_x
/100000.);
98 info_ptr
->y_blue
= (float)( blue_y
/100000.);
100 info_ptr
->valid
|= PNG_INFO_cHRM
;
103 #endif /* PNG_FIXED_POINT_SUPPORTED */
104 #endif /* PNG_cHRM_SUPPORTED */
106 #if defined(PNG_gAMA_SUPPORTED)
107 #ifdef PNG_FLOATING_POINT_SUPPORTED
109 png_set_gAMA(png_structp png_ptr
, png_infop info_ptr
, double file_gamma
)
112 png_debug1(1, "in %s storage function", "gAMA");
113 if (png_ptr
== NULL
|| info_ptr
== NULL
)
116 /* Check for overflow */
117 if (file_gamma
> 21474.83)
119 png_warning(png_ptr
, "Limiting gamma to 21474.83");
123 png_gamma
= file_gamma
;
124 info_ptr
->gamma
= (float)png_gamma
;
125 #ifdef PNG_FIXED_POINT_SUPPORTED
126 info_ptr
->int_gamma
= (int)(png_gamma
*100000.+.5);
128 info_ptr
->valid
|= PNG_INFO_gAMA
;
129 if (png_gamma
== 0.0)
130 png_warning(png_ptr
, "Setting gamma=0");
134 png_set_gAMA_fixed(png_structp png_ptr
, png_infop info_ptr
, png_fixed_point
137 png_fixed_point png_gamma
;
139 png_debug1(1, "in %s storage function", "gAMA");
140 if (png_ptr
== NULL
|| info_ptr
== NULL
)
143 if (int_gamma
> (png_fixed_point
)PNG_UINT_31_MAX
)
145 png_warning(png_ptr
, "Limiting gamma to 21474.83");
146 png_gamma
=PNG_UINT_31_MAX
;
152 png_warning(png_ptr
, "Setting negative gamma to zero");
156 png_gamma
= int_gamma
;
158 #ifdef PNG_FLOATING_POINT_SUPPORTED
159 info_ptr
->gamma
= (float)(png_gamma
/100000.);
161 #ifdef PNG_FIXED_POINT_SUPPORTED
162 info_ptr
->int_gamma
= png_gamma
;
164 info_ptr
->valid
|= PNG_INFO_gAMA
;
166 png_warning(png_ptr
, "Setting gamma=0");
170 #if defined(PNG_hIST_SUPPORTED)
172 png_set_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_16p hist
)
176 png_debug1(1, "in %s storage function", "hIST");
177 if (png_ptr
== NULL
|| info_ptr
== NULL
)
179 if (info_ptr
->num_palette
== 0 || info_ptr
->num_palette
180 > PNG_MAX_PALETTE_LENGTH
)
183 "Invalid palette size, hIST allocation skipped.");
187 #ifdef PNG_FREE_ME_SUPPORTED
188 png_free_data(png_ptr
, info_ptr
, PNG_FREE_HIST
, 0);
190 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
193 png_ptr
->hist
= (png_uint_16p
)png_malloc_warn(png_ptr
,
194 (png_uint_32
)(PNG_MAX_PALETTE_LENGTH
* png_sizeof(png_uint_16
)));
195 if (png_ptr
->hist
== NULL
)
197 png_warning(png_ptr
, "Insufficient memory for hIST chunk data.");
201 for (i
= 0; i
< info_ptr
->num_palette
; i
++)
202 png_ptr
->hist
[i
] = hist
[i
];
203 info_ptr
->hist
= png_ptr
->hist
;
204 info_ptr
->valid
|= PNG_INFO_hIST
;
206 #ifdef PNG_FREE_ME_SUPPORTED
207 info_ptr
->free_me
|= PNG_FREE_HIST
;
209 png_ptr
->flags
|= PNG_FLAG_FREE_HIST
;
215 png_set_IHDR(png_structp png_ptr
, png_infop info_ptr
,
216 png_uint_32 width
, png_uint_32 height
, int bit_depth
,
217 int color_type
, int interlace_type
, int compression_type
,
220 png_debug1(1, "in %s storage function", "IHDR");
221 if (png_ptr
== NULL
|| info_ptr
== NULL
)
224 /* Check for width and height valid values */
225 if (width
== 0 || height
== 0)
226 png_error(png_ptr
, "Image width or height is zero in IHDR");
227 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
228 if (width
> png_ptr
->user_width_max
|| height
> png_ptr
->user_height_max
)
229 png_error(png_ptr
, "image size exceeds user limits in IHDR");
231 if (width
> PNG_USER_WIDTH_MAX
|| height
> PNG_USER_HEIGHT_MAX
)
232 png_error(png_ptr
, "image size exceeds user limits in IHDR");
234 if (width
> PNG_UINT_31_MAX
|| height
> PNG_UINT_31_MAX
)
235 png_error(png_ptr
, "Invalid image size in IHDR");
236 if ( width
> (PNG_UINT_32_MAX
237 >> 3) /* 8-byte RGBA pixels */
238 - 64 /* bigrowbuf hack */
239 - 1 /* filter byte */
240 - 7*8 /* rounding of width to multiple of 8 pixels */
241 - 8) /* extra max_pixel_depth pad */
242 png_warning(png_ptr
, "Width is too large for libpng to process pixels");
244 /* Check other values */
245 if (bit_depth
!= 1 && bit_depth
!= 2 && bit_depth
!= 4 &&
246 bit_depth
!= 8 && bit_depth
!= 16)
247 png_error(png_ptr
, "Invalid bit depth in IHDR");
249 if (color_type
< 0 || color_type
== 1 ||
250 color_type
== 5 || color_type
> 6)
251 png_error(png_ptr
, "Invalid color type in IHDR");
253 if (((color_type
== PNG_COLOR_TYPE_PALETTE
) && bit_depth
> 8) ||
254 ((color_type
== PNG_COLOR_TYPE_RGB
||
255 color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
||
256 color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) && bit_depth
< 8))
257 png_error(png_ptr
, "Invalid color type/bit depth combination in IHDR");
259 if (interlace_type
>= PNG_INTERLACE_LAST
)
260 png_error(png_ptr
, "Unknown interlace method in IHDR");
262 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
263 png_error(png_ptr
, "Unknown compression method in IHDR");
265 #if defined(PNG_MNG_FEATURES_SUPPORTED)
266 /* Accept filter_method 64 (intrapixel differencing) only if
267 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
268 * 2. Libpng did not read a PNG signature (this filter_method is only
269 * used in PNG datastreams that are embedded in MNG datastreams) and
270 * 3. The application called png_permit_mng_features with a mask that
271 * included PNG_FLAG_MNG_FILTER_64 and
272 * 4. The filter_method is 64 and
273 * 5. The color_type is RGB or RGBA
275 if ((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
)&&png_ptr
->mng_features_permitted
)
276 png_warning(png_ptr
, "MNG features are not allowed in a PNG datastream");
277 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
279 if (!((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
280 (filter_type
== PNG_INTRAPIXEL_DIFFERENCING
) &&
281 ((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
) == 0) &&
282 (color_type
== PNG_COLOR_TYPE_RGB
||
283 color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)))
284 png_error(png_ptr
, "Unknown filter method in IHDR");
285 if (png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
)
286 png_warning(png_ptr
, "Invalid filter method in IHDR");
289 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
290 png_error(png_ptr
, "Unknown filter method in IHDR");
293 info_ptr
->width
= width
;
294 info_ptr
->height
= height
;
295 info_ptr
->bit_depth
= (png_byte
)bit_depth
;
296 info_ptr
->color_type
=(png_byte
) color_type
;
297 info_ptr
->compression_type
= (png_byte
)compression_type
;
298 info_ptr
->filter_type
= (png_byte
)filter_type
;
299 info_ptr
->interlace_type
= (png_byte
)interlace_type
;
300 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
301 info_ptr
->channels
= 1;
302 else if (info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
303 info_ptr
->channels
= 3;
305 info_ptr
->channels
= 1;
306 if (info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)
307 info_ptr
->channels
++;
308 info_ptr
->pixel_depth
= (png_byte
)(info_ptr
->channels
* info_ptr
->bit_depth
);
310 /* Check for potential overflow */
311 if (width
> (PNG_UINT_32_MAX
312 >> 3) /* 8-byte RGBA pixels */
313 - 64 /* bigrowbuf hack */
314 - 1 /* filter byte */
315 - 7*8 /* rounding of width to multiple of 8 pixels */
316 - 8) /* extra max_pixel_depth pad */
317 info_ptr
->rowbytes
= (png_size_t
)0;
319 info_ptr
->rowbytes
= PNG_ROWBYTES(info_ptr
->pixel_depth
, width
);
322 #if defined(PNG_oFFs_SUPPORTED)
324 png_set_oFFs(png_structp png_ptr
, png_infop info_ptr
,
325 png_int_32 offset_x
, png_int_32 offset_y
, int unit_type
)
327 png_debug1(1, "in %s storage function", "oFFs");
328 if (png_ptr
== NULL
|| info_ptr
== NULL
)
331 info_ptr
->x_offset
= offset_x
;
332 info_ptr
->y_offset
= offset_y
;
333 info_ptr
->offset_unit_type
= (png_byte
)unit_type
;
334 info_ptr
->valid
|= PNG_INFO_oFFs
;
338 #if defined(PNG_pCAL_SUPPORTED)
340 png_set_pCAL(png_structp png_ptr
, png_infop info_ptr
,
341 png_charp purpose
, png_int_32 X0
, png_int_32 X1
, int type
, int nparams
,
342 png_charp units
, png_charpp params
)
347 png_debug1(1, "in %s storage function", "pCAL");
348 if (png_ptr
== NULL
|| info_ptr
== NULL
)
351 length
= png_strlen(purpose
) + 1;
352 png_debug1(3, "allocating purpose for info (%lu bytes)",
353 (unsigned long)length
);
354 info_ptr
->pcal_purpose
= (png_charp
)png_malloc_warn(png_ptr
, length
);
355 if (info_ptr
->pcal_purpose
== NULL
)
357 png_warning(png_ptr
, "Insufficient memory for pCAL purpose.");
360 png_memcpy(info_ptr
->pcal_purpose
, purpose
, (png_size_t
)length
);
362 png_debug(3, "storing X0, X1, type, and nparams in info");
363 info_ptr
->pcal_X0
= X0
;
364 info_ptr
->pcal_X1
= X1
;
365 info_ptr
->pcal_type
= (png_byte
)type
;
366 info_ptr
->pcal_nparams
= (png_byte
)nparams
;
368 length
= png_strlen(units
) + 1;
369 png_debug1(3, "allocating units for info (%lu bytes)",
370 (unsigned long)length
);
371 info_ptr
->pcal_units
= (png_charp
)png_malloc_warn(png_ptr
, length
);
372 if (info_ptr
->pcal_units
== NULL
)
374 png_warning(png_ptr
, "Insufficient memory for pCAL units.");
377 png_memcpy(info_ptr
->pcal_units
, units
, (png_size_t
)length
);
379 info_ptr
->pcal_params
= (png_charpp
)png_malloc_warn(png_ptr
,
380 (png_uint_32
)((nparams
+ 1) * png_sizeof(png_charp
)));
381 if (info_ptr
->pcal_params
== NULL
)
383 png_warning(png_ptr
, "Insufficient memory for pCAL params.");
387 png_memset(info_ptr
->pcal_params
, 0, (nparams
+ 1) * png_sizeof(png_charp
));
389 for (i
= 0; i
< nparams
; i
++)
391 length
= png_strlen(params
[i
]) + 1;
392 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i
,
393 (unsigned long)length
);
394 info_ptr
->pcal_params
[i
] = (png_charp
)png_malloc_warn(png_ptr
, length
);
395 if (info_ptr
->pcal_params
[i
] == NULL
)
397 png_warning(png_ptr
, "Insufficient memory for pCAL parameter.");
400 png_memcpy(info_ptr
->pcal_params
[i
], params
[i
], (png_size_t
)length
);
403 info_ptr
->valid
|= PNG_INFO_pCAL
;
404 #ifdef PNG_FREE_ME_SUPPORTED
405 info_ptr
->free_me
|= PNG_FREE_PCAL
;
410 #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
411 #ifdef PNG_FLOATING_POINT_SUPPORTED
413 png_set_sCAL(png_structp png_ptr
, png_infop info_ptr
,
414 int unit
, double width
, double height
)
416 png_debug1(1, "in %s storage function", "sCAL");
417 if (png_ptr
== NULL
|| info_ptr
== NULL
)
420 info_ptr
->scal_unit
= (png_byte
)unit
;
421 info_ptr
->scal_pixel_width
= width
;
422 info_ptr
->scal_pixel_height
= height
;
424 info_ptr
->valid
|= PNG_INFO_sCAL
;
427 #ifdef PNG_FIXED_POINT_SUPPORTED
429 png_set_sCAL_s(png_structp png_ptr
, png_infop info_ptr
,
430 int unit
, png_charp swidth
, png_charp sheight
)
434 png_debug1(1, "in %s storage function", "sCAL");
435 if (png_ptr
== NULL
|| info_ptr
== NULL
)
438 info_ptr
->scal_unit
= (png_byte
)unit
;
440 length
= png_strlen(swidth
) + 1;
441 png_debug1(3, "allocating unit for info (%u bytes)",
442 (unsigned int)length
);
443 info_ptr
->scal_s_width
= (png_charp
)png_malloc_warn(png_ptr
, length
);
444 if (info_ptr
->scal_s_width
== NULL
)
447 "Memory allocation failed while processing sCAL.");
450 png_memcpy(info_ptr
->scal_s_width
, swidth
, (png_size_t
)length
);
452 length
= png_strlen(sheight
) + 1;
453 png_debug1(3, "allocating unit for info (%u bytes)",
454 (unsigned int)length
);
455 info_ptr
->scal_s_height
= (png_charp
)png_malloc_warn(png_ptr
, length
);
456 if (info_ptr
->scal_s_height
== NULL
)
458 png_free (png_ptr
, info_ptr
->scal_s_width
);
459 info_ptr
->scal_s_width
= NULL
;
461 "Memory allocation failed while processing sCAL.");
464 png_memcpy(info_ptr
->scal_s_height
, sheight
, (png_size_t
)length
);
465 info_ptr
->valid
|= PNG_INFO_sCAL
;
466 #ifdef PNG_FREE_ME_SUPPORTED
467 info_ptr
->free_me
|= PNG_FREE_SCAL
;
474 #if defined(PNG_pHYs_SUPPORTED)
476 png_set_pHYs(png_structp png_ptr
, png_infop info_ptr
,
477 png_uint_32 res_x
, png_uint_32 res_y
, int unit_type
)
479 png_debug1(1, "in %s storage function", "pHYs");
480 if (png_ptr
== NULL
|| info_ptr
== NULL
)
483 info_ptr
->x_pixels_per_unit
= res_x
;
484 info_ptr
->y_pixels_per_unit
= res_y
;
485 info_ptr
->phys_unit_type
= (png_byte
)unit_type
;
486 info_ptr
->valid
|= PNG_INFO_pHYs
;
491 png_set_PLTE(png_structp png_ptr
, png_infop info_ptr
,
492 png_colorp palette
, int num_palette
)
495 png_debug1(1, "in %s storage function", "PLTE");
496 if (png_ptr
== NULL
|| info_ptr
== NULL
)
499 if (num_palette
< 0 || num_palette
> PNG_MAX_PALETTE_LENGTH
)
501 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
502 png_error(png_ptr
, "Invalid palette length");
505 png_warning(png_ptr
, "Invalid palette length");
511 * It may not actually be necessary to set png_ptr->palette here;
512 * we do it for backward compatibility with the way the png_handle_tRNS
513 * function used to do the allocation.
515 #ifdef PNG_FREE_ME_SUPPORTED
516 png_free_data(png_ptr
, info_ptr
, PNG_FREE_PLTE
, 0);
519 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
520 * of num_palette entries, in case of an invalid PNG file that has
521 * too-large sample values.
523 png_ptr
->palette
= (png_colorp
)png_malloc(png_ptr
,
524 PNG_MAX_PALETTE_LENGTH
* png_sizeof(png_color
));
525 png_memset(png_ptr
->palette
, 0, PNG_MAX_PALETTE_LENGTH
*
526 png_sizeof(png_color
));
527 png_memcpy(png_ptr
->palette
, palette
, num_palette
* png_sizeof(png_color
));
528 info_ptr
->palette
= png_ptr
->palette
;
529 info_ptr
->num_palette
= png_ptr
->num_palette
= (png_uint_16
)num_palette
;
531 #ifdef PNG_FREE_ME_SUPPORTED
532 info_ptr
->free_me
|= PNG_FREE_PLTE
;
534 png_ptr
->flags
|= PNG_FLAG_FREE_PLTE
;
537 info_ptr
->valid
|= PNG_INFO_PLTE
;
540 #if defined(PNG_sBIT_SUPPORTED)
542 png_set_sBIT(png_structp png_ptr
, png_infop info_ptr
,
543 png_color_8p sig_bit
)
545 png_debug1(1, "in %s storage function", "sBIT");
546 if (png_ptr
== NULL
|| info_ptr
== NULL
)
549 png_memcpy(&(info_ptr
->sig_bit
), sig_bit
, png_sizeof(png_color_8
));
550 info_ptr
->valid
|= PNG_INFO_sBIT
;
554 #if defined(PNG_sRGB_SUPPORTED)
556 png_set_sRGB(png_structp png_ptr
, png_infop info_ptr
, int intent
)
558 png_debug1(1, "in %s storage function", "sRGB");
559 if (png_ptr
== NULL
|| info_ptr
== NULL
)
562 info_ptr
->srgb_intent
= (png_byte
)intent
;
563 info_ptr
->valid
|= PNG_INFO_sRGB
;
567 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr
, png_infop info_ptr
,
570 #if defined(PNG_gAMA_SUPPORTED)
571 #ifdef PNG_FLOATING_POINT_SUPPORTED
574 #ifdef PNG_FIXED_POINT_SUPPORTED
575 png_fixed_point int_file_gamma
;
578 #if defined(PNG_cHRM_SUPPORTED)
579 #ifdef PNG_FLOATING_POINT_SUPPORTED
580 float white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
;
582 png_fixed_point int_white_x
, int_white_y
, int_red_x
, int_red_y
, int_green_x
,
583 int_green_y
, int_blue_x
, int_blue_y
;
585 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
586 if (png_ptr
== NULL
|| info_ptr
== NULL
)
589 png_set_sRGB(png_ptr
, info_ptr
, intent
);
591 #if defined(PNG_gAMA_SUPPORTED)
592 #ifdef PNG_FLOATING_POINT_SUPPORTED
593 file_gamma
= (float).45455;
594 png_set_gAMA(png_ptr
, info_ptr
, file_gamma
);
596 #ifdef PNG_FIXED_POINT_SUPPORTED
597 int_file_gamma
= 45455L;
598 png_set_gAMA_fixed(png_ptr
, info_ptr
, int_file_gamma
);
602 #if defined(PNG_cHRM_SUPPORTED)
603 int_white_x
= 31270L;
604 int_white_y
= 32900L;
607 int_green_x
= 30000L;
608 int_green_y
= 60000L;
612 #ifdef PNG_FLOATING_POINT_SUPPORTED
613 white_x
= (float).3127;
614 white_y
= (float).3290;
617 green_x
= (float).30;
618 green_y
= (float).60;
623 #if !defined(PNG_NO_CHECK_cHRM)
624 if (png_check_cHRM_fixed(png_ptr
,
625 int_white_x
, int_white_y
, int_red_x
, int_red_y
, int_green_x
,
626 int_green_y
, int_blue_x
, int_blue_y
))
629 #ifdef PNG_FIXED_POINT_SUPPORTED
630 png_set_cHRM_fixed(png_ptr
, info_ptr
,
631 int_white_x
, int_white_y
, int_red_x
, int_red_y
, int_green_x
,
632 int_green_y
, int_blue_x
, int_blue_y
);
634 #ifdef PNG_FLOATING_POINT_SUPPORTED
635 png_set_cHRM(png_ptr
, info_ptr
,
636 white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
);
644 #if defined(PNG_iCCP_SUPPORTED)
646 png_set_iCCP(png_structp png_ptr
, png_infop info_ptr
,
647 png_charp name
, int compression_type
,
648 png_charp profile
, png_uint_32 proflen
)
650 png_charp new_iccp_name
;
651 png_charp new_iccp_profile
;
654 png_debug1(1, "in %s storage function", "iCCP");
655 if (png_ptr
== NULL
|| info_ptr
== NULL
|| name
== NULL
|| profile
== NULL
)
658 length
= png_strlen(name
)+1;
659 new_iccp_name
= (png_charp
)png_malloc_warn(png_ptr
, length
);
660 if (new_iccp_name
== NULL
)
662 png_warning(png_ptr
, "Insufficient memory to process iCCP chunk.");
665 png_memcpy(new_iccp_name
, name
, length
);
666 new_iccp_profile
= (png_charp
)png_malloc_warn(png_ptr
, proflen
);
667 if (new_iccp_profile
== NULL
)
669 png_free (png_ptr
, new_iccp_name
);
671 "Insufficient memory to process iCCP profile.");
674 png_memcpy(new_iccp_profile
, profile
, (png_size_t
)proflen
);
676 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ICCP
, 0);
678 info_ptr
->iccp_proflen
= proflen
;
679 info_ptr
->iccp_name
= new_iccp_name
;
680 info_ptr
->iccp_profile
= new_iccp_profile
;
681 /* Compression is always zero but is here so the API and info structure
682 * does not have to change if we introduce multiple compression types */
683 info_ptr
->iccp_compression
= (png_byte
)compression_type
;
684 #ifdef PNG_FREE_ME_SUPPORTED
685 info_ptr
->free_me
|= PNG_FREE_ICCP
;
687 info_ptr
->valid
|= PNG_INFO_iCCP
;
691 #if defined(PNG_TEXT_SUPPORTED)
693 png_set_text(png_structp png_ptr
, png_infop info_ptr
, png_textp text_ptr
,
697 ret
= png_set_text_2(png_ptr
, info_ptr
, text_ptr
, num_text
);
699 png_error(png_ptr
, "Insufficient memory to store text");
703 png_set_text_2(png_structp png_ptr
, png_infop info_ptr
, png_textp text_ptr
,
708 png_debug1(1, "in %s storage function", (png_ptr
->chunk_name
[0] == '\0' ?
709 "text" : (png_const_charp
)png_ptr
->chunk_name
));
711 if (png_ptr
== NULL
|| info_ptr
== NULL
|| num_text
== 0)
714 /* Make sure we have enough space in the "text" array in info_struct
715 * to hold all of the incoming text_ptr objects.
717 if (info_ptr
->num_text
+ num_text
> info_ptr
->max_text
)
719 if (info_ptr
->text
!= NULL
)
724 old_max
= info_ptr
->max_text
;
725 info_ptr
->max_text
= info_ptr
->num_text
+ num_text
+ 8;
726 old_text
= info_ptr
->text
;
727 info_ptr
->text
= (png_textp
)png_malloc_warn(png_ptr
,
728 (png_uint_32
)(info_ptr
->max_text
* png_sizeof(png_text
)));
729 if (info_ptr
->text
== NULL
)
731 png_free(png_ptr
, old_text
);
734 png_memcpy(info_ptr
->text
, old_text
, (png_size_t
)(old_max
*
735 png_sizeof(png_text
)));
736 png_free(png_ptr
, old_text
);
740 info_ptr
->max_text
= num_text
+ 8;
741 info_ptr
->num_text
= 0;
742 info_ptr
->text
= (png_textp
)png_malloc_warn(png_ptr
,
743 (png_uint_32
)(info_ptr
->max_text
* png_sizeof(png_text
)));
744 if (info_ptr
->text
== NULL
)
746 #ifdef PNG_FREE_ME_SUPPORTED
747 info_ptr
->free_me
|= PNG_FREE_TEXT
;
750 png_debug1(3, "allocated %d entries for info_ptr->text",
753 for (i
= 0; i
< num_text
; i
++)
755 png_size_t text_length
, key_len
;
756 png_size_t lang_len
, lang_key_len
;
757 png_textp textp
= &(info_ptr
->text
[info_ptr
->num_text
]);
759 if (text_ptr
[i
].key
== NULL
)
762 key_len
= png_strlen(text_ptr
[i
].key
);
764 if (text_ptr
[i
].compression
<= 0)
770 #ifdef PNG_iTXt_SUPPORTED
773 if (text_ptr
[i
].lang
!= NULL
)
774 lang_len
= png_strlen(text_ptr
[i
].lang
);
777 if (text_ptr
[i
].lang_key
!= NULL
)
778 lang_key_len
= png_strlen(text_ptr
[i
].lang_key
);
784 png_warning(png_ptr
, "iTXt chunk not supported.");
789 if (text_ptr
[i
].text
== NULL
|| text_ptr
[i
].text
[0] == '\0')
792 #ifdef PNG_iTXt_SUPPORTED
793 if (text_ptr
[i
].compression
> 0)
794 textp
->compression
= PNG_ITXT_COMPRESSION_NONE
;
797 textp
->compression
= PNG_TEXT_COMPRESSION_NONE
;
801 text_length
= png_strlen(text_ptr
[i
].text
);
802 textp
->compression
= text_ptr
[i
].compression
;
805 textp
->key
= (png_charp
)png_malloc_warn(png_ptr
,
807 (key_len
+ text_length
+ lang_len
+ lang_key_len
+ 4));
808 if (textp
->key
== NULL
)
810 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
812 (key_len
+ lang_len
+ lang_key_len
+ text_length
+ 4),
815 png_memcpy(textp
->key
, text_ptr
[i
].key
,(png_size_t
)(key_len
));
816 *(textp
->key
+ key_len
) = '\0';
817 #ifdef PNG_iTXt_SUPPORTED
818 if (text_ptr
[i
].compression
> 0)
820 textp
->lang
= textp
->key
+ key_len
+ 1;
821 png_memcpy(textp
->lang
, text_ptr
[i
].lang
, lang_len
);
822 *(textp
->lang
+ lang_len
) = '\0';
823 textp
->lang_key
= textp
->lang
+ lang_len
+ 1;
824 png_memcpy(textp
->lang_key
, text_ptr
[i
].lang_key
, lang_key_len
);
825 *(textp
->lang_key
+ lang_key_len
) = '\0';
826 textp
->text
= textp
->lang_key
+ lang_key_len
+ 1;
831 #ifdef PNG_iTXt_SUPPORTED
833 textp
->lang_key
=NULL
;
835 textp
->text
= textp
->key
+ key_len
+ 1;
838 png_memcpy(textp
->text
, text_ptr
[i
].text
,
839 (png_size_t
)(text_length
));
840 *(textp
->text
+ text_length
) = '\0';
842 #ifdef PNG_iTXt_SUPPORTED
843 if (textp
->compression
> 0)
845 textp
->text_length
= 0;
846 textp
->itxt_length
= text_length
;
851 textp
->text_length
= text_length
;
852 #ifdef PNG_iTXt_SUPPORTED
853 textp
->itxt_length
= 0;
856 info_ptr
->num_text
++;
857 png_debug1(3, "transferred text chunk %d", info_ptr
->num_text
);
863 #if defined(PNG_tIME_SUPPORTED)
865 png_set_tIME(png_structp png_ptr
, png_infop info_ptr
, png_timep mod_time
)
867 png_debug1(1, "in %s storage function", "tIME");
868 if (png_ptr
== NULL
|| info_ptr
== NULL
||
869 (png_ptr
->mode
& PNG_WROTE_tIME
))
872 png_memcpy(&(info_ptr
->mod_time
), mod_time
, png_sizeof(png_time
));
873 info_ptr
->valid
|= PNG_INFO_tIME
;
877 #if defined(PNG_tRNS_SUPPORTED)
879 png_set_tRNS(png_structp png_ptr
, png_infop info_ptr
,
880 png_bytep trans
, int num_trans
, png_color_16p trans_values
)
882 png_debug1(1, "in %s storage function", "tRNS");
883 if (png_ptr
== NULL
|| info_ptr
== NULL
)
889 * It may not actually be necessary to set png_ptr->trans here;
890 * we do it for backward compatibility with the way the png_handle_tRNS
891 * function used to do the allocation.
894 #ifdef PNG_FREE_ME_SUPPORTED
895 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TRNS
, 0);
898 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
899 png_ptr
->trans
= info_ptr
->trans
= (png_bytep
)png_malloc(png_ptr
,
900 (png_uint_32
)PNG_MAX_PALETTE_LENGTH
);
901 if (num_trans
> 0 && num_trans
<= PNG_MAX_PALETTE_LENGTH
)
902 png_memcpy(info_ptr
->trans
, trans
, (png_size_t
)num_trans
);
905 if (trans_values
!= NULL
)
907 int sample_max
= (1 << info_ptr
->bit_depth
);
908 if ((info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
&&
909 (int)trans_values
->gray
> sample_max
) ||
910 (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
&&
911 ((int)trans_values
->red
> sample_max
||
912 (int)trans_values
->green
> sample_max
||
913 (int)trans_values
->blue
> sample_max
)))
915 "tRNS chunk has out-of-range samples for bit_depth");
916 png_memcpy(&(info_ptr
->trans_values
), trans_values
,
917 png_sizeof(png_color_16
));
922 info_ptr
->num_trans
= (png_uint_16
)num_trans
;
925 info_ptr
->valid
|= PNG_INFO_tRNS
;
926 #ifdef PNG_FREE_ME_SUPPORTED
927 info_ptr
->free_me
|= PNG_FREE_TRNS
;
929 png_ptr
->flags
|= PNG_FLAG_FREE_TRNS
;
935 #if defined(PNG_sPLT_SUPPORTED)
937 png_set_sPLT(png_structp png_ptr
,
938 png_infop info_ptr
, png_sPLT_tp entries
, int nentries
)
940 * entries - array of png_sPLT_t structures
941 * to be added to the list of palettes
942 * in the info structure.
943 * nentries - number of palette structures to be
950 if (png_ptr
== NULL
|| info_ptr
== NULL
)
953 np
= (png_sPLT_tp
)png_malloc_warn(png_ptr
,
954 (info_ptr
->splt_palettes_num
+ nentries
) *
955 (png_uint_32
)png_sizeof(png_sPLT_t
));
958 png_warning(png_ptr
, "No memory for sPLT palettes.");
962 png_memcpy(np
, info_ptr
->splt_palettes
,
963 info_ptr
->splt_palettes_num
* png_sizeof(png_sPLT_t
));
964 png_free(png_ptr
, info_ptr
->splt_palettes
);
965 info_ptr
->splt_palettes
=NULL
;
967 for (i
= 0; i
< nentries
; i
++)
969 png_sPLT_tp to
= np
+ info_ptr
->splt_palettes_num
+ i
;
970 png_sPLT_tp from
= entries
+ i
;
973 length
= png_strlen(from
->name
) + 1;
974 to
->name
= (png_charp
)png_malloc_warn(png_ptr
, length
);
975 if (to
->name
== NULL
)
978 "Out of memory while processing sPLT chunk");
981 png_memcpy(to
->name
, from
->name
, length
);
982 to
->entries
= (png_sPLT_entryp
)png_malloc_warn(png_ptr
,
983 (png_uint_32
)(from
->nentries
* png_sizeof(png_sPLT_entry
)));
984 if (to
->entries
== NULL
)
987 "Out of memory while processing sPLT chunk");
988 png_free(png_ptr
, to
->name
);
992 png_memcpy(to
->entries
, from
->entries
,
993 from
->nentries
* png_sizeof(png_sPLT_entry
));
994 to
->nentries
= from
->nentries
;
995 to
->depth
= from
->depth
;
998 info_ptr
->splt_palettes
= np
;
999 info_ptr
->splt_palettes_num
+= nentries
;
1000 info_ptr
->valid
|= PNG_INFO_sPLT
;
1001 #ifdef PNG_FREE_ME_SUPPORTED
1002 info_ptr
->free_me
|= PNG_FREE_SPLT
;
1005 #endif /* PNG_sPLT_SUPPORTED */
1007 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1009 png_set_unknown_chunks(png_structp png_ptr
,
1010 png_infop info_ptr
, png_unknown_chunkp unknowns
, int num_unknowns
)
1012 png_unknown_chunkp np
;
1015 if (png_ptr
== NULL
|| info_ptr
== NULL
|| num_unknowns
== 0)
1018 np
= (png_unknown_chunkp
)png_malloc_warn(png_ptr
,
1019 (png_uint_32
)((info_ptr
->unknown_chunks_num
+ num_unknowns
) *
1020 png_sizeof(png_unknown_chunk
)));
1023 png_warning(png_ptr
,
1024 "Out of memory while processing unknown chunk.");
1028 png_memcpy(np
, info_ptr
->unknown_chunks
,
1029 info_ptr
->unknown_chunks_num
* png_sizeof(png_unknown_chunk
));
1030 png_free(png_ptr
, info_ptr
->unknown_chunks
);
1031 info_ptr
->unknown_chunks
=NULL
;
1033 for (i
= 0; i
< num_unknowns
; i
++)
1035 png_unknown_chunkp to
= np
+ info_ptr
->unknown_chunks_num
+ i
;
1036 png_unknown_chunkp from
= unknowns
+ i
;
1038 png_memcpy((png_charp
)to
->name
,
1039 (png_charp
)from
->name
,
1040 png_sizeof(from
->name
));
1041 to
->name
[png_sizeof(to
->name
)-1] = '\0';
1042 to
->size
= from
->size
;
1043 /* Note our location in the read or write sequence */
1044 to
->location
= (png_byte
)(png_ptr
->mode
& 0xff);
1046 if (from
->size
== 0)
1050 to
->data
= (png_bytep
)png_malloc_warn(png_ptr
,
1051 (png_uint_32
)from
->size
);
1052 if (to
->data
== NULL
)
1054 png_warning(png_ptr
,
1055 "Out of memory while processing unknown chunk.");
1059 png_memcpy(to
->data
, from
->data
, from
->size
);
1063 info_ptr
->unknown_chunks
= np
;
1064 info_ptr
->unknown_chunks_num
+= num_unknowns
;
1065 #ifdef PNG_FREE_ME_SUPPORTED
1066 info_ptr
->free_me
|= PNG_FREE_UNKN
;
1070 png_set_unknown_chunk_location(png_structp png_ptr
, png_infop info_ptr
,
1071 int chunk
, int location
)
1073 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& chunk
>= 0 && chunk
<
1074 (int)info_ptr
->unknown_chunks_num
)
1075 info_ptr
->unknown_chunks
[chunk
].location
= (png_byte
)location
;
1079 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
1080 #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
1081 defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
1083 png_permit_empty_plte (png_structp png_ptr
, int empty_plte_permitted
)
1085 /* This function is deprecated in favor of png_permit_mng_features()
1086 and will be removed from libpng-1.3.0 */
1087 png_debug(1, "in png_permit_empty_plte, DEPRECATED.");
1088 if (png_ptr
== NULL
)
1090 png_ptr
->mng_features_permitted
= (png_byte
)
1091 ((png_ptr
->mng_features_permitted
& (~PNG_FLAG_MNG_EMPTY_PLTE
)) |
1092 ((empty_plte_permitted
& PNG_FLAG_MNG_EMPTY_PLTE
)));
1097 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1099 png_permit_mng_features (png_structp png_ptr
, png_uint_32 mng_features
)
1101 png_debug(1, "in png_permit_mng_features");
1102 if (png_ptr
== NULL
)
1103 return (png_uint_32
)0;
1104 png_ptr
->mng_features_permitted
=
1105 (png_byte
)(mng_features
& PNG_ALL_MNG_FEATURES
);
1106 return (png_uint_32
)png_ptr
->mng_features_permitted
;
1110 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1112 png_set_keep_unknown_chunks(png_structp png_ptr
, int keep
, png_bytep
1113 chunk_list
, int num_chunks
)
1115 png_bytep new_list
, p
;
1116 int i
, old_num_chunks
;
1117 if (png_ptr
== NULL
)
1119 if (num_chunks
== 0)
1121 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
|| keep
== PNG_HANDLE_CHUNK_IF_SAFE
)
1122 png_ptr
->flags
|= PNG_FLAG_KEEP_UNKNOWN_CHUNKS
;
1124 png_ptr
->flags
&= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS
;
1126 if (keep
== PNG_HANDLE_CHUNK_ALWAYS
)
1127 png_ptr
->flags
|= PNG_FLAG_KEEP_UNSAFE_CHUNKS
;
1129 png_ptr
->flags
&= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS
;
1132 if (chunk_list
== NULL
)
1134 old_num_chunks
= png_ptr
->num_chunk_list
;
1135 new_list
=(png_bytep
)png_malloc(png_ptr
,
1137 (5*(num_chunks
+ old_num_chunks
)));
1138 if (png_ptr
->chunk_list
!= NULL
)
1140 png_memcpy(new_list
, png_ptr
->chunk_list
,
1141 (png_size_t
)(5*old_num_chunks
));
1142 png_free(png_ptr
, png_ptr
->chunk_list
);
1143 png_ptr
->chunk_list
=NULL
;
1145 png_memcpy(new_list
+ 5*old_num_chunks
, chunk_list
,
1146 (png_size_t
)(5*num_chunks
));
1147 for (p
= new_list
+ 5*old_num_chunks
+ 4, i
= 0; i
<num_chunks
; i
++, p
+= 5)
1149 png_ptr
->num_chunk_list
= old_num_chunks
+ num_chunks
;
1150 png_ptr
->chunk_list
= new_list
;
1151 #ifdef PNG_FREE_ME_SUPPORTED
1152 png_ptr
->free_me
|= PNG_FREE_LIST
;
1157 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1159 png_set_read_user_chunk_fn(png_structp png_ptr
, png_voidp user_chunk_ptr
,
1160 png_user_chunk_ptr read_user_chunk_fn
)
1162 png_debug(1, "in png_set_read_user_chunk_fn");
1163 if (png_ptr
== NULL
)
1165 png_ptr
->read_user_chunk_fn
= read_user_chunk_fn
;
1166 png_ptr
->user_chunk_ptr
= user_chunk_ptr
;
1170 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1172 png_set_rows(png_structp png_ptr
, png_infop info_ptr
, png_bytepp row_pointers
)
1174 png_debug1(1, "in %s storage function", "rows");
1176 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1179 if (info_ptr
->row_pointers
&& (info_ptr
->row_pointers
!= row_pointers
))
1180 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1181 info_ptr
->row_pointers
= row_pointers
;
1183 info_ptr
->valid
|= PNG_INFO_IDAT
;
1187 #ifdef PNG_WRITE_SUPPORTED
1189 png_set_compression_buffer_size(png_structp png_ptr
,
1192 if (png_ptr
== NULL
)
1194 png_free(png_ptr
, png_ptr
->zbuf
);
1195 png_ptr
->zbuf_size
= (png_size_t
)size
;
1196 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
, size
);
1197 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1198 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1203 png_set_invalid(png_structp png_ptr
, png_infop info_ptr
, int mask
)
1205 if (png_ptr
&& info_ptr
)
1206 info_ptr
->valid
&= ~mask
;
1211 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
1212 /* Function was added to libpng 1.2.0 and should always exist by default */
1214 png_set_asm_flags (png_structp png_ptr
, png_uint_32 asm_flags
)
1216 /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
1217 if (png_ptr
!= NULL
)
1218 png_ptr
->asm_flags
= 0;
1219 asm_flags
= asm_flags
; /* Quiet the compiler */
1222 /* This function was added to libpng 1.2.0 */
1224 png_set_mmx_thresholds (png_structp png_ptr
,
1225 png_byte mmx_bitdepth_threshold
,
1226 png_uint_32 mmx_rowbytes_threshold
)
1228 /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
1229 if (png_ptr
== NULL
)
1231 /* Quiet the compiler */
1232 mmx_bitdepth_threshold
= mmx_bitdepth_threshold
;
1233 mmx_rowbytes_threshold
= mmx_rowbytes_threshold
;
1235 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
1237 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
1238 /* This function was added to libpng 1.2.6 */
1240 png_set_user_limits (png_structp png_ptr
, png_uint_32 user_width_max
,
1241 png_uint_32 user_height_max
)
1243 /* Images with dimensions larger than these limits will be
1244 * rejected by png_set_IHDR(). To accept any PNG datastream
1245 * regardless of dimensions, set both limits to 0x7ffffffL.
1247 if (png_ptr
== NULL
)
1249 png_ptr
->user_width_max
= user_width_max
;
1250 png_ptr
->user_height_max
= user_height_max
;
1252 #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1254 #endif /* ?PNG_1_0_X */
1255 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */