blob: 071b026122764f2f02446673407c0c1e175e8d4d [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05009 * This code is released under the zlib-libpng license.
10 * For conditions of distribution and use, see copyright notice in png.h
11 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 * The functions here are used during reads to store data from the file
13 * into the info struct, and during writes to store application data
14 * into the info struct for writing into the file. This abstracts the
15 * info struct and allows us to change the structure in the future.
16 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050017
Andreas Dilger47a0c421997-05-16 02:46:07 -050018#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060019#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050020#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060022#if defined(PNG_bKGD_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050023void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050024png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
25{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050026 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060027 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050028 return;
29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050030 png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
Andreas Dilger47a0c421997-05-16 02:46:07 -050031 info_ptr->valid |= PNG_INFO_bKGD;
32}
33#endif
34
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060035#if defined(PNG_cHRM_SUPPORTED)
36#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050037void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050038png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
39 double white_x, double white_y, double red_x, double red_y,
40 double green_x, double green_y, double blue_x, double blue_y)
41{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050042 png_debug1(1, "in %s storage function", "cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060043 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050044 return;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060045
Andreas Dilger47a0c421997-05-16 02:46:07 -050046 info_ptr->x_white = (float)white_x;
47 info_ptr->y_white = (float)white_y;
48 info_ptr->x_red = (float)red_x;
49 info_ptr->y_red = (float)red_y;
50 info_ptr->x_green = (float)green_x;
51 info_ptr->y_green = (float)green_y;
52 info_ptr->x_blue = (float)blue_x;
53 info_ptr->y_blue = (float)blue_y;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060054#ifdef PNG_FIXED_POINT_SUPPORTED
55 info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
56 info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060057 info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5);
58 info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060059 info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
60 info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060061 info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5);
62 info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060063#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 info_ptr->valid |= PNG_INFO_cHRM;
65}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060066#endif /* PNG_FLOATING_POINT_SUPPORTED */
67
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050069void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060070png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060071 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
72 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
73 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060074{
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060075 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060076 if (png_ptr == NULL || info_ptr == NULL)
77 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050078
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060079#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060080 if (png_check_cHRM_fixed(png_ptr,
81 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060082#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050083 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050084 info_ptr->int_x_white = white_x;
85 info_ptr->int_y_white = white_y;
86 info_ptr->int_x_red = red_x;
87 info_ptr->int_y_red = red_y;
88 info_ptr->int_x_green = green_x;
89 info_ptr->int_y_green = green_y;
90 info_ptr->int_x_blue = blue_x;
91 info_ptr->int_y_blue = blue_y;
92#ifdef PNG_FLOATING_POINT_SUPPORTED
93 info_ptr->x_white = (float)(white_x/100000.);
94 info_ptr->y_white = (float)(white_y/100000.);
95 info_ptr->x_red = (float)( red_x/100000.);
96 info_ptr->y_red = (float)( red_y/100000.);
97 info_ptr->x_green = (float)(green_x/100000.);
98 info_ptr->y_green = (float)(green_y/100000.);
99 info_ptr->x_blue = (float)( blue_x/100000.);
100 info_ptr->y_blue = (float)( blue_y/100000.);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600101#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500102 info_ptr->valid |= PNG_INFO_cHRM;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600103 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600104}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600105#endif /* PNG_FIXED_POINT_SUPPORTED */
106#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600107
108#if defined(PNG_gAMA_SUPPORTED)
109#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
112{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500113 double png_gamma;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500114 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600115 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 return;
117
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600118 /* Check for overflow */
119 if (file_gamma > 21474.83)
120 {
121 png_warning(png_ptr, "Limiting gamma to 21474.83");
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500122 png_gamma=21474.83;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600123 }
124 else
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500125 png_gamma = file_gamma;
126 info_ptr->gamma = (float)png_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600127#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500128 info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600129#endif
130 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500131 if (png_gamma == 0.0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500132 png_warning(png_ptr, "Setting gamma=0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600133}
134#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500135void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600136png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
137 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600138{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500139 png_fixed_point png_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600140
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500141 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600142 if (png_ptr == NULL || info_ptr == NULL)
143 return;
144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600146 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500147 png_warning(png_ptr, "Limiting gamma to 21474.83");
148 png_gamma=PNG_UINT_31_MAX;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600149 }
150 else
151 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 if (int_gamma < 0)
153 {
154 png_warning(png_ptr, "Setting negative gamma to zero");
155 png_gamma = 0;
156 }
157 else
158 png_gamma = int_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600159 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600160#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500161 info_ptr->gamma = (float)(png_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600162#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500163#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500164 info_ptr->int_gamma = png_gamma;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500165#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500167 if (png_gamma == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500168 png_warning(png_ptr, "Setting gamma=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500170#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600172#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500173void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
175{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500176 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600177
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500178 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600179 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500181 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600182 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500183 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500184 png_warning(png_ptr,
185 "Invalid palette size, hIST allocation skipped");
186 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500187 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600189#ifdef PNG_FREE_ME_SUPPORTED
190 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
191#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500192 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
193 * version 1.2.1
194 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500195 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500196 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500197 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500198 {
199 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
200 return;
201 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600202
203 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600205 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600207
208#ifdef PNG_FREE_ME_SUPPORTED
209 info_ptr->free_me |= PNG_FREE_HIST;
210#else
211 png_ptr->flags |= PNG_FLAG_FREE_HIST;
212#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500213}
214#endif
215
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500216void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
218 png_uint_32 width, png_uint_32 height, int bit_depth,
219 int color_type, int interlace_type, int compression_type,
220 int filter_type)
221{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500222 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600223 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500224 return;
225
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500226 /* Check for width and height valid values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500227 if (width == 0 || height == 0)
228 png_error(png_ptr, "Image width or height is zero in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500229#ifdef PNG_SET_USER_LIMITS_SUPPORTED
230 if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
231 png_error(png_ptr, "image size exceeds user limits in IHDR");
232#else
233 if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
234 png_error(png_ptr, "image size exceeds user limits in IHDR");
235#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500236 if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500237 png_error(png_ptr, "Invalid image size in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500238 if ( width > (PNG_UINT_32_MAX
239 >> 3) /* 8-byte RGBA pixels */
240 - 64 /* bigrowbuf hack */
241 - 1 /* filter byte */
242 - 7*8 /* rounding of width to multiple of 8 pixels */
243 - 8) /* extra max_pixel_depth pad */
244 png_warning(png_ptr, "Width is too large for libpng to process pixels");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500245
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500246 /* Check other values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500247 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500248 bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500249 png_error(png_ptr, "Invalid bit depth in IHDR");
250
251 if (color_type < 0 || color_type == 1 ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500252 color_type == 5 || color_type > 6)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500253 png_error(png_ptr, "Invalid color type in IHDR");
254
255 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
256 ((color_type == PNG_COLOR_TYPE_RGB ||
257 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
258 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
259 png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
260
261 if (interlace_type >= PNG_INTERLACE_LAST)
262 png_error(png_ptr, "Unknown interlace method in IHDR");
263
264 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
265 png_error(png_ptr, "Unknown compression method in IHDR");
266
267#if defined(PNG_MNG_FEATURES_SUPPORTED)
268 /* Accept filter_method 64 (intrapixel differencing) only if
269 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
270 * 2. Libpng did not read a PNG signature (this filter_method is only
271 * used in PNG datastreams that are embedded in MNG datastreams) and
272 * 3. The application called png_permit_mng_features with a mask that
273 * included PNG_FLAG_MNG_FILTER_64 and
274 * 4. The filter_method is 64 and
275 * 5. The color_type is RGB or RGBA
276 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500277 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
278 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
279 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500280 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500281 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500282 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
283 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
284 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500285 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
286 png_error(png_ptr, "Unknown filter method in IHDR");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500287 if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500288 png_warning(png_ptr, "Invalid filter method in IHDR");
289 }
290#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500291 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500292 png_error(png_ptr, "Unknown filter method in IHDR");
293#endif
294
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295 info_ptr->width = width;
296 info_ptr->height = height;
297 info_ptr->bit_depth = (png_byte)bit_depth;
298 info_ptr->color_type =(png_byte) color_type;
299 info_ptr->compression_type = (png_byte)compression_type;
300 info_ptr->filter_type = (png_byte)filter_type;
301 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500302 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
303 info_ptr->channels = 1;
304 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 info_ptr->channels = 3;
306 else
307 info_ptr->channels = 1;
308 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
309 info_ptr->channels++;
310 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600311
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500312 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500313 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500314 >> 3) /* 8-byte RGBA pixels */
315 - 64 /* bigrowbuf hack */
316 - 1 /* filter byte */
317 - 7*8 /* rounding of width to multiple of 8 pixels */
318 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500319 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500320 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500321 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322}
323
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600324#if defined(PNG_oFFs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500325void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600327 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500329 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600330 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331 return;
332
333 info_ptr->x_offset = offset_x;
334 info_ptr->y_offset = offset_y;
335 info_ptr->offset_unit_type = (png_byte)unit_type;
336 info_ptr->valid |= PNG_INFO_oFFs;
337}
338#endif
339
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600340#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500341void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
343 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
344 png_charp units, png_charpp params)
345{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600347 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500349 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600350 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 return;
352
353 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500354 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500355 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500356 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
357 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500359 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500360 return;
361 }
362 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500364 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 info_ptr->pcal_X0 = X0;
366 info_ptr->pcal_X1 = X1;
367 info_ptr->pcal_type = (png_byte)type;
368 info_ptr->pcal_nparams = (png_byte)nparams;
369
370 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500371 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500372 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500373 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
374 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500375 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500376 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500377 return;
378 }
379 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500381 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500382 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500383 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500384 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500385 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500386 return;
387 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500388
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600389 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390
391 for (i = 0; i < nparams; i++)
392 {
393 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500394 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500395 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500396 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
397 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500398 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500399 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
400 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500401 }
402 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 }
404
405 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500406#ifdef PNG_FREE_ME_SUPPORTED
407 info_ptr->free_me |= PNG_FREE_PCAL;
408#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409}
410#endif
411
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600412#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
413#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500414void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600415png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600416 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600417{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500418 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600419 if (png_ptr == NULL || info_ptr == NULL)
420 return;
421
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600422 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600423 info_ptr->scal_pixel_width = width;
424 info_ptr->scal_pixel_height = height;
425
426 info_ptr->valid |= PNG_INFO_sCAL;
427}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600428#else
429#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500430void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600431png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600432 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600433{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500434 png_size_t length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600435
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500436 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600437 if (png_ptr == NULL || info_ptr == NULL)
438 return;
439
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600440 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600441
442 length = png_strlen(swidth) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500443 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500444 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500445 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
446 if (info_ptr->scal_s_width == NULL)
447 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500448 png_warning(png_ptr,
449 "Memory allocation failed while processing sCAL");
450 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500451 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500452 png_memcpy(info_ptr->scal_s_width, swidth, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600453
454 length = png_strlen(sheight) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500455 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500456 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500457 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
458 if (info_ptr->scal_s_height == NULL)
459 {
460 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500461 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500462 png_warning(png_ptr,
463 "Memory allocation failed while processing sCAL");
464 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500465 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500466 png_memcpy(info_ptr->scal_s_height, sheight, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600467 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500468#ifdef PNG_FREE_ME_SUPPORTED
469 info_ptr->free_me |= PNG_FREE_SCAL;
470#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600471}
472#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600473#endif
474#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600475
476#if defined(PNG_pHYs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500477void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500478png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
479 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
480{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500481 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600482 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483 return;
484
485 info_ptr->x_pixels_per_unit = res_x;
486 info_ptr->y_pixels_per_unit = res_y;
487 info_ptr->phys_unit_type = (png_byte)unit_type;
488 info_ptr->valid |= PNG_INFO_pHYs;
489}
490#endif
491
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500492void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
494 png_colorp palette, int num_palette)
495{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600496
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500497 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600498 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 return;
500
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600501 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500502 {
503 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600504 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500505 else
506 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600507 png_warning(png_ptr, "Invalid palette length");
508 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500509 }
510 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600511
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600512 /*
513 * It may not actually be necessary to set png_ptr->palette here;
514 * we do it for backward compatibility with the way the png_handle_tRNS
515 * function used to do the allocation.
516 */
517#ifdef PNG_FREE_ME_SUPPORTED
518 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
519#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500520
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600521 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500522 * of num_palette entries, in case of an invalid PNG file that has
523 * too-large sample values.
524 */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600525#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600526 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500527 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600528#else
529 png_ptr->palette = (png_colorp)png_malloc(png_ptr,
530 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
531 png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
532 png_sizeof(png_color));
533#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500534 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600535 info_ptr->palette = png_ptr->palette;
536 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600537
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600538#ifdef PNG_FREE_ME_SUPPORTED
539 info_ptr->free_me |= PNG_FREE_PLTE;
540#else
541 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
542#endif
543
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600544 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545}
546
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600547#if defined(PNG_sBIT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500548void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
550 png_color_8p sig_bit)
551{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500552 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600553 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 return;
555
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500556 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500557 info_ptr->valid |= PNG_INFO_sBIT;
558}
559#endif
560
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600561#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500562void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600563png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600564{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500565 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600566 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600567 return;
568
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600569 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600570 info_ptr->valid |= PNG_INFO_sRGB;
571}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600572
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500573void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600574png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600575 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600576{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600577#if defined(PNG_gAMA_SUPPORTED)
578#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600579 float file_gamma;
580#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600581#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600582 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600583#endif
584#endif
585#if defined(PNG_cHRM_SUPPORTED)
586#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600587 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
588#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600589 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 -0600590 int_green_y, int_blue_x, int_blue_y;
591#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500592 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600593 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600594 return;
595
596 png_set_sRGB(png_ptr, info_ptr, intent);
597
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600598#if defined(PNG_gAMA_SUPPORTED)
599#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500600 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600601 png_set_gAMA(png_ptr, info_ptr, file_gamma);
602#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600603#ifdef PNG_FIXED_POINT_SUPPORTED
604 int_file_gamma = 45455L;
605 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
606#endif
607#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600608
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600609#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600610 int_white_x = 31270L;
611 int_white_y = 32900L;
612 int_red_x = 64000L;
613 int_red_y = 33000L;
614 int_green_x = 30000L;
615 int_green_y = 60000L;
616 int_blue_x = 15000L;
617 int_blue_y = 6000L;
618
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600619#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600620 white_x = (float).3127;
621 white_y = (float).3290;
622 red_x = (float).64;
623 red_y = (float).33;
624 green_x = (float).30;
625 green_y = (float).60;
626 blue_x = (float).15;
627 blue_y = (float).06;
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600628#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600629
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600630#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600631 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500632 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
633 int_green_y, int_blue_x, int_blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600634#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600635 {
636#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500637 png_set_cHRM_fixed(png_ptr, info_ptr,
638 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
639 int_green_y, int_blue_x, int_blue_y);
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600640#endif
641#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600642 png_set_cHRM(png_ptr, info_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500643 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600644#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600645 }
646#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600647}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600648#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600649
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600650
651#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500652void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600653png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
654 png_charp name, int compression_type,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600655 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600656{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500657 png_charp new_iccp_name;
658 png_charp new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500659 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500660
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500661 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600662 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
663 return;
664
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500665 length = png_strlen(name)+1;
666 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500667 if (new_iccp_name == NULL)
668 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500669 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500670 return;
671 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500672 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500673 new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
674 if (new_iccp_profile == NULL)
675 {
676 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500677 png_warning(png_ptr,
678 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500679 return;
680 }
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500681 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
682
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500683 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500684
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600685 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500686 info_ptr->iccp_name = new_iccp_name;
687 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500689 * does not have to change if we introduce multiple compression types */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600690 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500691#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600692 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500693#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694 info_ptr->valid |= PNG_INFO_iCCP;
695}
696#endif
697
698#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500699void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700png_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 -0500701 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500702{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500703 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500704 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500705 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500706 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500707}
708
709int /* PRIVATE */
710png_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 -0500711 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500712{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500713 int i;
714
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500715 png_debug1(1, "in %s storage function", (png_ptr->chunk_name[0] == '\0' ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600716 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500717
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600718 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500719 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720
721 /* Make sure we have enough space in the "text" array in info_struct
722 * to hold all of the incoming text_ptr objects.
723 */
724 if (info_ptr->num_text + num_text > info_ptr->max_text)
725 {
726 if (info_ptr->text != NULL)
727 {
728 png_textp old_text;
729 int old_max;
730
731 old_max = info_ptr->max_text;
732 info_ptr->max_text = info_ptr->num_text + num_text + 8;
733 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500734 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500735 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500736 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500737 {
738 png_free(png_ptr, old_text);
739 return(1);
740 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600741 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500742 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 png_free(png_ptr, old_text);
744 }
745 else
746 {
747 info_ptr->max_text = num_text + 8;
748 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500749 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500751 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500752 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500753#ifdef PNG_FREE_ME_SUPPORTED
754 info_ptr->free_me |= PNG_FREE_TEXT;
755#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 }
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500757 png_debug1(3, "allocated %d entries for info_ptr->text",
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758 info_ptr->max_text);
759 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500760 for (i = 0; i < num_text; i++)
761 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500762 png_size_t text_length, key_len;
763 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
765
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500766 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600767 continue;
768
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600769 key_len = png_strlen(text_ptr[i].key);
770
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500771 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500772 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500773 lang_len = 0;
774 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500775 }
776 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500777#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600778 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500779 /* Set iTXt data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500780 if (text_ptr[i].lang != NULL)
781 lang_len = png_strlen(text_ptr[i].lang);
782 else
783 lang_len = 0;
784 if (text_ptr[i].lang_key != NULL)
785 lang_key_len = png_strlen(text_ptr[i].lang_key);
786 else
787 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600788 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500789#else
790 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500791 png_warning(png_ptr, "iTXt chunk not supported");
792 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500793 }
794#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500795
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500796 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600798 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500799#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500800 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600801 textp->compression = PNG_ITXT_COMPRESSION_NONE;
802 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500803#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600804 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500805 }
806 else
807 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600808 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500809 textp->compression = text_ptr[i].compression;
810 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500811
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500812 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500813 (png_size_t)
814 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500815 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500816 return(1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500817 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500818 (unsigned long)(png_uint_32)
819 (key_len + lang_len + lang_key_len + text_length + 4),
820 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500821
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500822 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500824#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 if (text_ptr[i].compression > 0)
826 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500827 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600828 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500829 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500830 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600831 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500832 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500833 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600834 }
835 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500836#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600837 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500838#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500839 textp->lang=NULL;
840 textp->lang_key=NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500841#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500842 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600843 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500844 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500845 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600846 (png_size_t)(text_length));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500847 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600848
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500849#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500850 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600851 {
852 textp->text_length = 0;
853 textp->itxt_length = text_length;
854 }
855 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500856#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600857 {
858 textp->text_length = text_length;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500859#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 textp->itxt_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500861#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600862 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500864 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500866 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867}
868#endif
869
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600870#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500871void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500872png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
873{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500874 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500875 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600876 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877 return;
878
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500879 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 info_ptr->valid |= PNG_INFO_tIME;
881}
882#endif
883
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600884#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500885void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500887 png_bytep trans, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500889 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600890 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 return;
892
893 if (trans != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600894 {
895 /*
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500896 * It may not actually be necessary to set png_ptr->trans here;
897 * we do it for backward compatibility with the way the png_handle_tRNS
898 * function used to do the allocation.
899 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500900
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600901#ifdef PNG_FREE_ME_SUPPORTED
902 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
903#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500904
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600905 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500906 png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500907 (png_size_t)PNG_MAX_PALETTE_LENGTH);
908 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500909 png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600910 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500912 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500914 int sample_max = (1 << info_ptr->bit_depth);
915 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500916 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500917 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500918 ((int)trans_color->red > sample_max ||
919 (int)trans_color->green > sample_max ||
920 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500921 png_warning(png_ptr,
922 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500923 png_memcpy(&(info_ptr->trans_color), trans_color,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500924 png_sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600925 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500926 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500928
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500930 if (num_trans != 0)
931 {
932 info_ptr->valid |= PNG_INFO_tRNS;
933#ifdef PNG_FREE_ME_SUPPORTED
934 info_ptr->free_me |= PNG_FREE_TRNS;
935#else
936 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
937#endif
938 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939}
940#endif
941
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600942#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500943void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600944png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600945 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600946/*
947 * entries - array of png_sPLT_t structures
948 * to be added to the list of palettes
949 * in the info structure.
950 * nentries - number of palette structures to be
951 * added.
952 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600953{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500954 png_sPLT_tp np;
955 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600956
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500957 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500958 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500960 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
961 (info_ptr->splt_palettes_num + nentries) *
962 (png_size_t)png_sizeof(png_sPLT_t));
963 if (np == NULL)
964 {
965 png_warning(png_ptr, "No memory for sPLT palettes");
966 return;
967 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600968
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500969 png_memcpy(np, info_ptr->splt_palettes,
970 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
971 png_free(png_ptr, info_ptr->splt_palettes);
972 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600973
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500974 for (i = 0; i < nentries; i++)
975 {
976 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
977 png_sPLT_tp from = entries + i;
978 png_uint_32 length;
979
980 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500981 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500982 if (to->name == NULL)
983 {
984 png_warning(png_ptr,
985 "Out of memory while processing sPLT chunk");
986 continue;
987 }
988 png_memcpy(to->name, from->name, length);
989 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500990 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500991 if (to->entries == NULL)
992 {
993 png_warning(png_ptr,
994 "Out of memory while processing sPLT chunk");
995 png_free(png_ptr, to->name);
996 to->name = NULL;
997 continue;
998 }
999 png_memcpy(to->entries, from->entries,
1000 from->nentries * png_sizeof(png_sPLT_entry));
1001 to->nentries = from->nentries;
1002 to->depth = from->depth;
1003 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001004
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001005 info_ptr->splt_palettes = np;
1006 info_ptr->splt_palettes_num += nentries;
1007 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001008#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001009 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001010#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011}
1012#endif /* PNG_sPLT_SUPPORTED */
1013
Glenn Randers-Pehrson59c39b82009-06-16 17:23:08 -05001014#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001015void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001016png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001018{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001019 png_unknown_chunkp np;
1020 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001021
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001022 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001023 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001024
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001025 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
1026 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
1027 png_sizeof(png_unknown_chunk)));
1028 if (np == NULL)
1029 {
1030 png_warning(png_ptr,
1031 "Out of memory while processing unknown chunk");
1032 return;
1033 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001034
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001035 png_memcpy(np, info_ptr->unknown_chunks,
1036 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
1037 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001038
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039 for (i = 0; i < num_unknowns; i++)
1040 {
1041 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
1042 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001044 png_memcpy((png_charp)to->name,
1045 (png_charp)from->name,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001046 png_sizeof(from->name));
1047 to->name[png_sizeof(to->name)-1] = '\0';
1048 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001049 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001050 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001052 if (from->size == 0)
1053 to->data=NULL;
1054 else
1055 {
1056 to->data = (png_bytep)png_malloc_warn(png_ptr,
1057 (png_size_t)from->size);
1058 if (to->data == NULL)
1059 {
1060 png_warning(png_ptr,
1061 "Out of memory while processing unknown chunk");
1062 to->size = 0;
1063 }
1064 else
1065 png_memcpy(to->data, from->data, from->size);
1066 }
1067 }
1068
1069 info_ptr->unknown_chunks = np;
1070 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001071#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001072 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001073#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001074}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001075void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001076png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
1077 int chunk, int location)
1078{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001079 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001080 (int)info_ptr->unknown_chunks_num)
1081 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1082}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001083#endif
1084
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001085
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001086#if defined(PNG_MNG_FEATURES_SUPPORTED)
1087png_uint_32 PNGAPI
1088png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1089{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001090 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001091 if (png_ptr == NULL)
1092 return (png_uint_32)0;
1093 png_ptr->mng_features_permitted =
1094 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
1095 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001096}
1097#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001098
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001099#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001100void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001101png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
1102 chunk_list, int num_chunks)
1103{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001104 png_bytep new_list, p;
1105 int i, old_num_chunks;
1106 if (png_ptr == NULL)
1107 return;
1108 if (num_chunks == 0)
1109 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001110 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001111 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001112 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001113 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001114
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001115 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
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 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001119 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001120 }
1121 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001122 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001123 old_num_chunks = png_ptr->num_chunk_list;
1124 new_list=(png_bytep)png_malloc(png_ptr,
1125 (png_size_t)
1126 (5*(num_chunks + old_num_chunks)));
1127 if (png_ptr->chunk_list != NULL)
1128 {
1129 png_memcpy(new_list, png_ptr->chunk_list,
1130 (png_size_t)(5*old_num_chunks));
1131 png_free(png_ptr, png_ptr->chunk_list);
1132 png_ptr->chunk_list=NULL;
1133 }
1134 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
1135 (png_size_t)(5*num_chunks));
1136 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1137 *p=(png_byte)keep;
1138 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1139 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001140#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001141 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001142#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001143}
1144#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001145
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001147void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001148png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
1149 png_user_chunk_ptr read_user_chunk_fn)
1150{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001151 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001152 if (png_ptr == NULL)
1153 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001154 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1155 png_ptr->user_chunk_ptr = user_chunk_ptr;
1156}
1157#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001158
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001159#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001160void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001161png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1162{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001163 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001164
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001165 if (png_ptr == NULL || info_ptr == NULL)
1166 return;
1167
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001168 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001169 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001170 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001171 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001172 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001173}
1174#endif
1175
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001176#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001177void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001178png_set_compression_buffer_size(png_structp png_ptr,
1179 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001180{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001181 if (png_ptr == NULL)
1182 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001183 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001184 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001185 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1186 png_ptr->zstream.next_out = png_ptr->zbuf;
1187 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1188}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001189#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001190
1191void PNGAPI
1192png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1193{
1194 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001195 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001196}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001197
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001198
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001199
1200#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001201/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001202void PNGAPI
1203png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1204 png_uint_32 user_height_max)
1205{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001206 /* Images with dimensions larger than these limits will be
1207 * rejected by png_set_IHDR(). To accept any PNG datastream
1208 * regardless of dimensions, set both limits to 0x7ffffffL.
1209 */
1210 if (png_ptr == NULL)
1211 return;
1212 png_ptr->user_width_max = user_width_max;
1213 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001214}
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001215/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001216void PNGAPI
1217png_set_chunk_cache_max (png_structp png_ptr,
1218 png_uint_32 user_chunk_cache_max)
1219{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001220 if (png_ptr == NULL)
1221 return;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001222 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1223 if (user_chunk_cache_max == 0x7fffffffL) /* Unlimited */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001224 png_ptr->user_chunk_cache_max = 0;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001225 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001226 png_ptr->user_chunk_cache_max = user_chunk_cache_max + 1;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001227}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001228#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1229
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001230
1231#if defined(PNG_BENIGN_ERRORS_SUPPORTED)
1232void PNGAPI
1233png_set_benign_errors(png_structp png_ptr, int allowed)
1234{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001235 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001236 if (allowed)
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 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001239 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001240}
1241#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001242#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */