blob: 686f6ca5474e504944c6e82fedd58e7c8405546c [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngset.c - storage of image information into info struct
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004 * Last changed in libpng 1.4.0 [May 20, 2009]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
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 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050015
Andreas Dilger47a0c421997-05-16 02:46:07 -050016#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060017#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050018#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060019
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060020#if defined(PNG_bKGD_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050021void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050022png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
23{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050024 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060025 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050026 return;
27
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050028 png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
Andreas Dilger47a0c421997-05-16 02:46:07 -050029 info_ptr->valid |= PNG_INFO_bKGD;
30}
31#endif
32
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060033#if defined(PNG_cHRM_SUPPORTED)
34#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050035void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050036png_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{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050040 png_debug1(1, "in %s storage function", "cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060041 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050042 return;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060043
Andreas Dilger47a0c421997-05-16 02:46:07 -050044 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;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060052#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);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060055 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);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060057 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);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060059 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);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060061#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050062 info_ptr->valid |= PNG_INFO_cHRM;
63}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060064#endif /* PNG_FLOATING_POINT_SUPPORTED */
65
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050067void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060069 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)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060072{
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060073 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060074 if (png_ptr == NULL || info_ptr == NULL)
75 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050076
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060077#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060078 if (png_check_cHRM_fixed(png_ptr,
79 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060080#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050081 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050082 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.);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060099#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500100 info_ptr->valid |= PNG_INFO_cHRM;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600101 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600102}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600103#endif /* PNG_FIXED_POINT_SUPPORTED */
104#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600105
106#if defined(PNG_gAMA_SUPPORTED)
107#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500108void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
110{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500111 double png_gamma;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500112 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600113 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 return;
115
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600116 /* Check for overflow */
117 if (file_gamma > 21474.83)
118 {
119 png_warning(png_ptr, "Limiting gamma to 21474.83");
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500120 png_gamma=21474.83;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600121 }
122 else
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500123 png_gamma = file_gamma;
124 info_ptr->gamma = (float)png_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600125#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500126 info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600127#endif
128 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500129 if (png_gamma == 0.0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500130 png_warning(png_ptr, "Setting gamma=0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600131}
132#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500133void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600134png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
135 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600136{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500137 png_fixed_point png_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600138
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500139 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140 if (png_ptr == NULL || info_ptr == NULL)
141 return;
142
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500143 if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600144 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 png_warning(png_ptr, "Limiting gamma to 21474.83");
146 png_gamma=PNG_UINT_31_MAX;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600147 }
148 else
149 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500150 if (int_gamma < 0)
151 {
152 png_warning(png_ptr, "Setting negative gamma to zero");
153 png_gamma = 0;
154 }
155 else
156 png_gamma = int_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600157 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500159 info_ptr->gamma = (float)(png_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600160#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500161#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500162 info_ptr->int_gamma = png_gamma;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500163#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500165 if (png_gamma == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500166 png_warning(png_ptr, "Setting gamma=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500168#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600170#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500171void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
173{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500174 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600175
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500176 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600177 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500179 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600180 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500181 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500182 png_warning(png_ptr,
183 "Invalid palette size, hIST allocation skipped");
184 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500185 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600187#ifdef PNG_FREE_ME_SUPPORTED
188 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
189#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500190 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
191 * version 1.2.1
192 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500193 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500194 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500195 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500196 {
197 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
198 return;
199 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600200
201 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500202 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600203 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600205
206#ifdef PNG_FREE_ME_SUPPORTED
207 info_ptr->free_me |= PNG_FREE_HIST;
208#else
209 png_ptr->flags |= PNG_FLAG_FREE_HIST;
210#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211}
212#endif
213
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500214void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500215png_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,
218 int filter_type)
219{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500220 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600221 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 return;
223
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500224 /* Check for width and height valid values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500225 if (width == 0 || height == 0)
226 png_error(png_ptr, "Image width or height is zero in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500227#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");
230#else
231 if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
232 png_error(png_ptr, "image size exceeds user limits in IHDR");
233#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500234 if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500235 png_error(png_ptr, "Invalid image size in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500236 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");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500243
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500244 /* Check other values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500245 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");
248
249 if (color_type < 0 || color_type == 1 ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500250 color_type == 5 || color_type > 6)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500251 png_error(png_ptr, "Invalid color type in IHDR");
252
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");
258
259 if (interlace_type >= PNG_INTERLACE_LAST)
260 png_error(png_ptr, "Unknown interlace method in IHDR");
261
262 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
263 png_error(png_ptr, "Unknown compression method in IHDR");
264
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
274 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500275 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)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500278 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500280 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
281 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
282 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500283 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
284 png_error(png_ptr, "Unknown filter method in IHDR");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285 if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500286 png_warning(png_ptr, "Invalid filter method in IHDR");
287 }
288#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500289 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500290 png_error(png_ptr, "Unknown filter method in IHDR");
291#endif
292
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 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;
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500300 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)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 info_ptr->channels = 3;
304 else
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);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600309
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500310 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500311 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500312 >> 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 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500317 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500318 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500319 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320}
321
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322#if defined(PNG_oFFs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500323void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600325 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500327 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600328 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329 return;
330
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;
335}
336#endif
337
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600338#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500339void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340png_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)
343{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500344 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600345 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500347 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600348 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 return;
350
351 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500352 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500353 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500354 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
355 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500356 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500357 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 return;
359 }
360 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500362 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 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;
367
368 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500369 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500370 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500371 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
372 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500374 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500375 return;
376 }
377 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500379 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500380 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500381 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500382 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500383 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500384 return;
385 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500386
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600387 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388
389 for (i = 0; i < nparams; i++)
390 {
391 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500392 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500393 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500394 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
395 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500397 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
398 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500399 }
400 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401 }
402
403 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500404#ifdef PNG_FREE_ME_SUPPORTED
405 info_ptr->free_me |= PNG_FREE_PCAL;
406#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407}
408#endif
409
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600410#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
411#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500412void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600414 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600415{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500416 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600417 if (png_ptr == NULL || info_ptr == NULL)
418 return;
419
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600420 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600421 info_ptr->scal_pixel_width = width;
422 info_ptr->scal_pixel_height = height;
423
424 info_ptr->valid |= PNG_INFO_sCAL;
425}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600426#else
427#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500428void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600429png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600430 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600431{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500432 png_size_t length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600433
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500434 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600435 if (png_ptr == NULL || info_ptr == NULL)
436 return;
437
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600438 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600439
440 length = png_strlen(swidth) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500441 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500442 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500443 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
444 if (info_ptr->scal_s_width == NULL)
445 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500446 png_warning(png_ptr,
447 "Memory allocation failed while processing sCAL");
448 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500449 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500450 png_memcpy(info_ptr->scal_s_width, swidth, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600451
452 length = png_strlen(sheight) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500453 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500454 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500455 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
456 if (info_ptr->scal_s_height == NULL)
457 {
458 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500459 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500460 png_warning(png_ptr,
461 "Memory allocation failed while processing sCAL");
462 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500463 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500464 png_memcpy(info_ptr->scal_s_height, sheight, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600465 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500466#ifdef PNG_FREE_ME_SUPPORTED
467 info_ptr->free_me |= PNG_FREE_SCAL;
468#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600469}
470#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600471#endif
472#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600473
474#if defined(PNG_pHYs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500475void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500476png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
477 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
478{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500479 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600480 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 return;
482
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;
487}
488#endif
489
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500490void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
492 png_colorp palette, int num_palette)
493{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600494
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500495 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600496 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497 return;
498
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600499 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500500 {
501 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600502 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500503 else
504 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600505 png_warning(png_ptr, "Invalid palette length");
506 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500507 }
508 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600509
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600510 /*
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.
514 */
515#ifdef PNG_FREE_ME_SUPPORTED
516 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
517#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500518
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600519 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500520 * of num_palette entries, in case of an invalid PNG file that has
521 * too-large sample values.
522 */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600523#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600524 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500525 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600526#else
527 png_ptr->palette = (png_colorp)png_malloc(png_ptr,
528 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
529 png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
530 png_sizeof(png_color));
531#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500532 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600533 info_ptr->palette = png_ptr->palette;
534 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600535
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600536#ifdef PNG_FREE_ME_SUPPORTED
537 info_ptr->free_me |= PNG_FREE_PLTE;
538#else
539 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
540#endif
541
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600542 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543}
544
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600545#if defined(PNG_sBIT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500546void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
548 png_color_8p sig_bit)
549{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500550 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600551 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500552 return;
553
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500554 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 info_ptr->valid |= PNG_INFO_sBIT;
556}
557#endif
558
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600559#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500560void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600561png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600562{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500563 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600564 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600565 return;
566
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600567 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600568 info_ptr->valid |= PNG_INFO_sRGB;
569}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600570
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600572png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600573 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600574{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600575#if defined(PNG_gAMA_SUPPORTED)
576#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600577 float file_gamma;
578#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600579#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600580 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600581#endif
582#endif
583#if defined(PNG_cHRM_SUPPORTED)
584#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
586#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600587 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600588 int_green_y, int_blue_x, int_blue_y;
589#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500590 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600591 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592 return;
593
594 png_set_sRGB(png_ptr, info_ptr, intent);
595
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600596#if defined(PNG_gAMA_SUPPORTED)
597#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500598 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600599 png_set_gAMA(png_ptr, info_ptr, file_gamma);
600#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600601#ifdef PNG_FIXED_POINT_SUPPORTED
602 int_file_gamma = 45455L;
603 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
604#endif
605#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600606
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600607#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600608 int_white_x = 31270L;
609 int_white_y = 32900L;
610 int_red_x = 64000L;
611 int_red_y = 33000L;
612 int_green_x = 30000L;
613 int_green_y = 60000L;
614 int_blue_x = 15000L;
615 int_blue_y = 6000L;
616
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600617#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600618 white_x = (float).3127;
619 white_y = (float).3290;
620 red_x = (float).64;
621 red_y = (float).33;
622 green_x = (float).30;
623 green_y = (float).60;
624 blue_x = (float).15;
625 blue_y = (float).06;
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600626#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600627
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600628#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600629 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500630 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
631 int_green_y, int_blue_x, int_blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600632#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600633 {
634#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500635 png_set_cHRM_fixed(png_ptr, info_ptr,
636 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
637 int_green_y, int_blue_x, int_blue_y);
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600638#endif
639#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600640 png_set_cHRM(png_ptr, info_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500641 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600642#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600643 }
644#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600645}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600646#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600647
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600648
649#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500650void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600651png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
652 png_charp name, int compression_type,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600653 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600654{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500655 png_charp new_iccp_name;
656 png_charp new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500657 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500658
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500659 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600660 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
661 return;
662
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500663 length = png_strlen(name)+1;
664 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500665 if (new_iccp_name == NULL)
666 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500667 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500668 return;
669 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500670 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500671 new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
672 if (new_iccp_profile == NULL)
673 {
674 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500675 png_warning(png_ptr,
676 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500677 return;
678 }
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500679 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
680
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500681 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500682
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600683 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500684 info_ptr->iccp_name = new_iccp_name;
685 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600686 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500687 * does not have to change if we introduce multiple compression types */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500689#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600690 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500691#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600692 info_ptr->valid |= PNG_INFO_iCCP;
693}
694#endif
695
696#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500697void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500698png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500699 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500701 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500702 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500703 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500705}
706
707int /* PRIVATE */
708png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500709 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500710{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 int i;
712
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500713 png_debug1(1, "in %s storage function", (png_ptr->chunk_name[0] == '\0' ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600714 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600716 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500717 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718
719 /* Make sure we have enough space in the "text" array in info_struct
720 * to hold all of the incoming text_ptr objects.
721 */
722 if (info_ptr->num_text + num_text > info_ptr->max_text)
723 {
724 if (info_ptr->text != NULL)
725 {
726 png_textp old_text;
727 int old_max;
728
729 old_max = info_ptr->max_text;
730 info_ptr->max_text = info_ptr->num_text + num_text + 8;
731 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500732 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500733 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500734 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500735 {
736 png_free(png_ptr, old_text);
737 return(1);
738 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600739 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500740 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741 png_free(png_ptr, old_text);
742 }
743 else
744 {
745 info_ptr->max_text = num_text + 8;
746 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500747 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500748 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500749 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500750 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500751#ifdef PNG_FREE_ME_SUPPORTED
752 info_ptr->free_me |= PNG_FREE_TEXT;
753#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 }
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500755 png_debug1(3, "allocated %d entries for info_ptr->text",
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 info_ptr->max_text);
757 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758 for (i = 0; i < num_text; i++)
759 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500760 png_size_t text_length, key_len;
761 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
763
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500764 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600765 continue;
766
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600767 key_len = png_strlen(text_ptr[i].key);
768
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500770 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500771 lang_len = 0;
772 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500773 }
774 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600776 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500777 /* Set iTXt data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500778 if (text_ptr[i].lang != NULL)
779 lang_len = png_strlen(text_ptr[i].lang);
780 else
781 lang_len = 0;
782 if (text_ptr[i].lang_key != NULL)
783 lang_key_len = png_strlen(text_ptr[i].lang_key);
784 else
785 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600786 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500787#else
788 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500789 png_warning(png_ptr, "iTXt chunk not supported");
790 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500791 }
792#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500793
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500794 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500795 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600796 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500797#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500798 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 textp->compression = PNG_ITXT_COMPRESSION_NONE;
800 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500801#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600802 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500803 }
804 else
805 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600806 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500807 textp->compression = text_ptr[i].compression;
808 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500809
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500810 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500811 (png_size_t)
812 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500813 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500814 return(1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500815 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500816 (unsigned long)(png_uint_32)
817 (key_len + lang_len + lang_key_len + text_length + 4),
818 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500819
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500820 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500822#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600823 if (text_ptr[i].compression > 0)
824 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500825 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600826 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500828 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500831 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600832 }
833 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500834#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600835 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500836#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500837 textp->lang=NULL;
838 textp->lang_key=NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500839#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500840 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500842 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500843 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600844 (png_size_t)(text_length));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500845 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600846
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500847#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500848 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600849 {
850 textp->text_length = 0;
851 textp->itxt_length = text_length;
852 }
853 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500854#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600855 {
856 textp->text_length = text_length;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500857#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600858 textp->itxt_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500859#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500861 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500862 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500864 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865}
866#endif
867
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600868#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500869void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500870png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
871{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500872 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500873 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600874 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500875 return;
876
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500877 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500878 info_ptr->valid |= PNG_INFO_tIME;
879}
880#endif
881
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600882#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500883void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500884png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500885 png_bytep trans, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500887 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600888 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889 return;
890
891 if (trans != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600892 {
893 /*
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500894 * It may not actually be necessary to set png_ptr->trans here;
895 * we do it for backward compatibility with the way the png_handle_tRNS
896 * function used to do the allocation.
897 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500898
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600899#ifdef PNG_FREE_ME_SUPPORTED
900 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
901#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500902
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600903 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500904 png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500905 (png_size_t)PNG_MAX_PALETTE_LENGTH);
906 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500907 png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600908 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500909
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500910 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500912 int sample_max = (1 << info_ptr->bit_depth);
913 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500914 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500915 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500916 ((int)trans_color->red > sample_max ||
917 (int)trans_color->green > sample_max ||
918 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500919 png_warning(png_ptr,
920 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500921 png_memcpy(&(info_ptr->trans_color), trans_color,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922 png_sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600923 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500924 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500925 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500926
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500928 if (num_trans != 0)
929 {
930 info_ptr->valid |= PNG_INFO_tRNS;
931#ifdef PNG_FREE_ME_SUPPORTED
932 info_ptr->free_me |= PNG_FREE_TRNS;
933#else
934 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
935#endif
936 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937}
938#endif
939
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600940#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500941void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600942png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600943 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600944/*
945 * entries - array of png_sPLT_t structures
946 * to be added to the list of palettes
947 * in the info structure.
948 * nentries - number of palette structures to be
949 * added.
950 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600951{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500952 png_sPLT_tp np;
953 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500955 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500956 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600957
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500958 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
959 (info_ptr->splt_palettes_num + nentries) *
960 (png_size_t)png_sizeof(png_sPLT_t));
961 if (np == NULL)
962 {
963 png_warning(png_ptr, "No memory for sPLT palettes");
964 return;
965 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600966
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500967 png_memcpy(np, info_ptr->splt_palettes,
968 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
969 png_free(png_ptr, info_ptr->splt_palettes);
970 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600971
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500972 for (i = 0; i < nentries; i++)
973 {
974 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
975 png_sPLT_tp from = entries + i;
976 png_uint_32 length;
977
978 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500979 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500980 if (to->name == NULL)
981 {
982 png_warning(png_ptr,
983 "Out of memory while processing sPLT chunk");
984 continue;
985 }
986 png_memcpy(to->name, from->name, length);
987 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500988 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500989 if (to->entries == NULL)
990 {
991 png_warning(png_ptr,
992 "Out of memory while processing sPLT chunk");
993 png_free(png_ptr, to->name);
994 to->name = NULL;
995 continue;
996 }
997 png_memcpy(to->entries, from->entries,
998 from->nentries * png_sizeof(png_sPLT_entry));
999 to->nentries = from->nentries;
1000 to->depth = from->depth;
1001 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001003 info_ptr->splt_palettes = np;
1004 info_ptr->splt_palettes_num += nentries;
1005 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001006#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001007 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001008#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009}
1010#endif /* PNG_sPLT_SUPPORTED */
1011
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001012#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001013void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001015 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001016{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001017 png_unknown_chunkp np;
1018 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001019
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001020 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001021 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001022
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001023 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
1024 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
1025 png_sizeof(png_unknown_chunk)));
1026 if (np == NULL)
1027 {
1028 png_warning(png_ptr,
1029 "Out of memory while processing unknown chunk");
1030 return;
1031 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001032
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001033 png_memcpy(np, info_ptr->unknown_chunks,
1034 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
1035 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001036
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037 for (i = 0; i < num_unknowns; i++)
1038 {
1039 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
1040 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001041
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042 png_memcpy((png_charp)to->name,
1043 (png_charp)from->name,
1044 png_sizeof(from->name));
1045 to->name[png_sizeof(to->name)-1] = '\0';
1046 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001047 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001049
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001050 if (from->size == 0)
1051 to->data=NULL;
1052 else
1053 {
1054 to->data = (png_bytep)png_malloc_warn(png_ptr,
1055 (png_size_t)from->size);
1056 if (to->data == NULL)
1057 {
1058 png_warning(png_ptr,
1059 "Out of memory while processing unknown chunk");
1060 to->size = 0;
1061 }
1062 else
1063 png_memcpy(to->data, from->data, from->size);
1064 }
1065 }
1066
1067 info_ptr->unknown_chunks = np;
1068 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001069#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001070 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001071#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001072}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001073void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001074png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
1075 int chunk, int location)
1076{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001077 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001078 (int)info_ptr->unknown_chunks_num)
1079 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1080}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001081#endif
1082
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001083
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001084#if defined(PNG_MNG_FEATURES_SUPPORTED)
1085png_uint_32 PNGAPI
1086png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1087{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001088 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001089 if (png_ptr == NULL)
1090 return (png_uint_32)0;
1091 png_ptr->mng_features_permitted =
1092 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
1093 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001094}
1095#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001097#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001098void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001099png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
1100 chunk_list, int num_chunks)
1101{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001102 png_bytep new_list, p;
1103 int i, old_num_chunks;
1104 if (png_ptr == NULL)
1105 return;
1106 if (num_chunks == 0)
1107 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001108 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001109 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001110 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001111 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001112
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001113 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001114 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001115 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001116 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001117 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 }
1119 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001120 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001121 old_num_chunks = png_ptr->num_chunk_list;
1122 new_list=(png_bytep)png_malloc(png_ptr,
1123 (png_size_t)
1124 (5*(num_chunks + old_num_chunks)));
1125 if (png_ptr->chunk_list != NULL)
1126 {
1127 png_memcpy(new_list, png_ptr->chunk_list,
1128 (png_size_t)(5*old_num_chunks));
1129 png_free(png_ptr, png_ptr->chunk_list);
1130 png_ptr->chunk_list=NULL;
1131 }
1132 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
1133 (png_size_t)(5*num_chunks));
1134 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1135 *p=(png_byte)keep;
1136 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1137 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001138#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001139 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001140#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001141}
1142#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001143
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001144#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001145void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
1147 png_user_chunk_ptr read_user_chunk_fn)
1148{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001149 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001150 if (png_ptr == NULL)
1151 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001152 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1153 png_ptr->user_chunk_ptr = user_chunk_ptr;
1154}
1155#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001156
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001157#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001158void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001159png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1160{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001161 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001162
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001163 if (png_ptr == NULL || info_ptr == NULL)
1164 return;
1165
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001166 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001167 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001168 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001169 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001170 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001171}
1172#endif
1173
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001174#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001175void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001176png_set_compression_buffer_size(png_structp png_ptr,
1177 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001178{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001179 if (png_ptr == NULL)
1180 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001181 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001182 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001183 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1184 png_ptr->zstream.next_out = png_ptr->zbuf;
1185 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1186}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001187#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001188
1189void PNGAPI
1190png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1191{
1192 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001193 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001194}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001195
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001196
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001197
1198#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001199/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001200void PNGAPI
1201png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1202 png_uint_32 user_height_max)
1203{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001204 /* Images with dimensions larger than these limits will be
1205 * rejected by png_set_IHDR(). To accept any PNG datastream
1206 * regardless of dimensions, set both limits to 0x7ffffffL.
1207 */
1208 if (png_ptr == NULL)
1209 return;
1210 png_ptr->user_width_max = user_width_max;
1211 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001212}
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001213/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001214void PNGAPI
1215png_set_chunk_cache_max (png_structp png_ptr,
1216 png_uint_32 user_chunk_cache_max)
1217{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001218 if (png_ptr == NULL)
1219 return;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001220 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1221 if (user_chunk_cache_max == 0x7fffffffL) /* Unlimited */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001222 png_ptr->user_chunk_cache_max = 0;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001223 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001224 png_ptr->user_chunk_cache_max = user_chunk_cache_max + 1;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001225}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001226#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1227
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001228
1229#if defined(PNG_BENIGN_ERRORS_SUPPORTED)
1230void PNGAPI
1231png_set_benign_errors(png_structp png_ptr, int allowed)
1232{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001233 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001234 if (allowed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001235 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001236 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001237 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001238}
1239#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001240#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */