blob: 5daebbf4845989578a9f5d1c34fe157e6b0bde93 [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-Pehrson4009a762010-07-31 06:34:36 -05004 * Last changed in libpng 1.5.0 [July 31, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * The functions here are used during reads to store data from the file
14 * into the info struct, and during writes to store application data
15 * into the info struct for writing into the file. This abstracts the
16 * info struct and allows us to change the structure in the future.
17 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050023#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050025png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
26{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050027 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050028
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060029 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050030 return;
31
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032 png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
Andreas Dilger47a0c421997-05-16 02:46:07 -050033 info_ptr->valid |= PNG_INFO_bKGD;
34}
35#endif
36
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050037#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050038void PNGFAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060039png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060040 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
41 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
42 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060043{
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060044 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050045
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060046 if (png_ptr == NULL || info_ptr == NULL)
47 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050048
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050049#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060050 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060051 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060052#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050053 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050054 info_ptr->x_white = white_x;
55 info_ptr->y_white = white_y;
56 info_ptr->x_red = red_x;
57 info_ptr->y_red = red_y;
58 info_ptr->x_green = green_x;
59 info_ptr->y_green = green_y;
60 info_ptr->x_blue = blue_x;
61 info_ptr->y_blue = blue_y;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050062 info_ptr->valid |= PNG_INFO_cHRM;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060063 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060064}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050065
66#ifdef PNG_FLOATING_POINT_SUPPORTED
67void PNGAPI
68png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
69 double white_x, double white_y, double red_x, double red_y,
70 double green_x, double green_y, double blue_x, double blue_y)
71{
72 png_set_cHRM_fixed(png_ptr, info_ptr,
73 png_fixed(png_ptr, white_x, "cHRM White X"),
74 png_fixed(png_ptr, white_y, "cHRM White Y"),
75 png_fixed(png_ptr, red_x, "cHRM Red X"),
76 png_fixed(png_ptr, red_y, "cHRM Red Y"),
77 png_fixed(png_ptr, green_x, "cHRM Green X"),
78 png_fixed(png_ptr, green_y, "cHRM Green Y"),
79 png_fixed(png_ptr, blue_x, "cHRM Blue X"),
80 png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
81}
82#endif /* PNG_FLOATING_POINT_SUPPORTED */
83
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060084#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060085
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050086#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050087void PNGFAPI
88png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
89 gamma)
90{
91 png_debug1(1, "in %s storage function", "gAMA");
92
93 if (png_ptr == NULL || info_ptr == NULL)
94 return;
95
96 /* Previously these values were limited, however they must be
97 * wrong, therfore storing them (and setting PNG_INFO_gAMA)
98 * must be wrong too.
99 */
100 if (gamma > (png_fixed_point)PNG_UINT_31_MAX)
101 png_warning(png_ptr, "Gamma too large, ignored");
102
103 else if (gamma <= 0)
104 png_warning(png_ptr, "Negative gamma ignored");
105
106 else
107 {
108 info_ptr->gamma = gamma;
109 info_ptr->valid |= PNG_INFO_gAMA;
110 }
111}
112
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600113#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500114void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
116{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500117 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
118 "png_set_gAMA"));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500120#endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500121#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500122
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500123#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500124void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500125png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
126{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500127 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600128
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500129 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500130
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600131 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500133
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500134 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600135 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500136 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500137 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600138 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500141 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600143 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500144 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
145 * version 1.2.1
146 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500147 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600148 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500149 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500150 {
151 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
152 return;
153 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600154
155 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500156 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500157
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600158 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600160
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600161 info_ptr->free_me |= PNG_FREE_HIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162}
163#endif
164
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500165void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
167 png_uint_32 width, png_uint_32 height, int bit_depth,
168 int color_type, int interlace_type, int compression_type,
169 int filter_type)
170{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500171 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500172
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600173 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 return;
175
176 info_ptr->width = width;
177 info_ptr->height = height;
178 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500179 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 info_ptr->compression_type = (png_byte)compression_type;
181 info_ptr->filter_type = (png_byte)filter_type;
182 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500183
184 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
185 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
186 info_ptr->compression_type, info_ptr->filter_type);
187
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500188 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
189 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500190
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500191 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500193
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 else
195 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500196
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
198 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500199
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600201
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500202 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500203 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600204 >> 3) /* 8-byte RRGGBBAA pixels */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500205 - 64 /* bigrowbuf hack */
206 - 1 /* filter byte */
207 - 7*8 /* rounding of width to multiple of 8 pixels */
208 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500209 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500210 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500211 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212}
213
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500214#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500215void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600217 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500219 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500220
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
224 info_ptr->x_offset = offset_x;
225 info_ptr->y_offset = offset_y;
226 info_ptr->offset_unit_type = (png_byte)unit_type;
227 info_ptr->valid |= PNG_INFO_oFFs;
228}
229#endif
230
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500231#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500232void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600234 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
235 png_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500237 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600238 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500240 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500241
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600242 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500243 return;
244
245 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500246 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600247 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500248
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500249 /* TODO: validate format of calibration name and unit name */
250
251 /* Check that the type matches the specification. */
252 if (type < 0 || type > 3)
253 png_error(png_ptr, "Invalid pCAL equation type");
254
255 /* Validate params[nparams] */
256 for (i=0; i<nparams; ++i)
257 if (!png_check_fp_string(params[i], png_strlen(params[i])))
258 png_error(png_ptr, "Invalid format for pCAL parameter");
259
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500260 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
261 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500262 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500263 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500264 return;
265 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500266
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500267 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500269 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500270 info_ptr->pcal_X0 = X0;
271 info_ptr->pcal_X1 = X1;
272 info_ptr->pcal_type = (png_byte)type;
273 info_ptr->pcal_nparams = (png_byte)nparams;
274
275 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500276 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500277 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500278
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500279 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
280 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500281 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500282 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500283 return;
284 }
285 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500287 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500288 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500289
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500290 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500291 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500292 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500293 return;
294 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500295
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600296 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297
298 for (i = 0; i < nparams; i++)
299 {
300 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500301 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500302 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500303
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500304 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500306 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500307 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500308 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
309 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500310 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500311
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500312 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 }
314
315 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500316 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317}
318#endif
319
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500320#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500321void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600323 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600324{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500325 png_size_t lengthw = 0, lengthh = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600326
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500327 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500328
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600329 if (png_ptr == NULL || info_ptr == NULL)
330 return;
331
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500332 /* Double check the unit (should never get here with an invalid
333 * unit unless this is an API call.)
334 */
335 if (unit != 1 && unit != 2)
336 png_error(png_ptr, "Invalid sCAL unit");
337
338 if (swidth == NULL || (lengthw = png_strlen(swidth)) <= 0 ||
339 swidth[0] == 45 /*'-'*/ || !png_check_fp_string(swidth, lengthw))
340 png_error(png_ptr, "Invalid sCAL width");
341
342 if (sheight == NULL || (lengthh = png_strlen(sheight)) <= 0 ||
343 sheight[0] == 45 /*'-'*/ || !png_check_fp_string(sheight, lengthh))
344 png_error(png_ptr, "Invalid sCAL height");
345
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600346 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500348 ++lengthw;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500349
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500350 png_debug1(3, "allocating unit for info (%u bytes)", lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500351
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500352 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500353
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500354 if (info_ptr->scal_s_width == NULL)
355 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500356 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500357 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500358 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500359
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500360 png_memcpy(info_ptr->scal_s_width, swidth, lengthw);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500362 ++lengthh;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500363
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500364 png_debug1(3, "allocating unit for info (%u bytes)", lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500365
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500366 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500367
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500368 if (info_ptr->scal_s_height == NULL)
369 {
370 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500371 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500372
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500373 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500374 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500375 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500376
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500377 png_memcpy(info_ptr->scal_s_height, sheight, lengthh);
378
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600379 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500380 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600381}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500382
383#ifdef PNG_FLOATING_POINT_SUPPORTED
384void PNGAPI
385png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width,
386 double height)
387{
388 png_debug1(1, "in %s storage function", "sCAL");
389
390 /* Check the arguments. */
391 if (width <= 0)
392 png_warning(png_ptr, "Invalid sCAL width ignored");
393 else if (height <= 0)
394 png_warning(png_ptr, "Invalid sCAL height ignored");
395 else
396 {
397 /* Convert 'width' and 'height' to ASCII. */
398 char swidth[PNG_sCAL_MAX_DIGITS+1];
399 char sheight[PNG_sCAL_MAX_DIGITS+1];
400
401 png_ascii_from_fp(png_ptr, swidth, sizeof swidth, width,
402 PNG_sCAL_PRECISION);
403 png_ascii_from_fp(png_ptr, sheight, sizeof sheight, height,
404 PNG_sCAL_PRECISION);
405
406 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
407 }
408}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600409#endif
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500410
411#ifdef PNG_FIXED_POINT_SUPPORTED
412void PNGAPI
413png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit,
414 png_fixed_point width, png_fixed_point height)
415{
416 png_debug1(1, "in %s storage function", "sCAL");
417
418 /* Check the arguments. */
419 if (width <= 0)
420 png_warning(png_ptr, "Invalid sCAL width ignored");
421 else if (height <= 0)
422 png_warning(png_ptr, "Invalid sCAL height ignored");
423 else
424 {
425 /* Convert 'width' and 'height' to ASCII. */
426 char swidth[PNG_sCAL_MAX_DIGITS+1];
427 char sheight[PNG_sCAL_MAX_DIGITS+1];
428
429 png_ascii_from_fixed(png_ptr, swidth, sizeof swidth, width);
430 png_ascii_from_fixed(png_ptr, sheight, sizeof sheight, height);
431
432 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
433 }
434}
435#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600436#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600437
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500438#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500439void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500440png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600441 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500443 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500444
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600445 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 return;
447
448 info_ptr->x_pixels_per_unit = res_x;
449 info_ptr->y_pixels_per_unit = res_y;
450 info_ptr->phys_unit_type = (png_byte)unit_type;
451 info_ptr->valid |= PNG_INFO_pHYs;
452}
453#endif
454
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500455void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600457 png_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600459
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500460 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500461
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600462 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500463 return;
464
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600465 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500466 {
467 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600468 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500469
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500470 else
471 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600472 png_warning(png_ptr, "Invalid palette length");
473 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500474 }
475 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600476
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600477 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600478 * we do it for backward compatibility with the way the png_handle_tRNS
479 * function used to do the allocation.
480 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600481 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500482
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600483 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500484 * of num_palette entries, in case of an invalid PNG file that has
485 * too-large sample values.
486 */
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600487 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600488 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500489
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500490 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600491 info_ptr->palette = png_ptr->palette;
492 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600493
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600494 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600495
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600496 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497}
498
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500499#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500500void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600502 png_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500504 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500505
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600506 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500507 return;
508
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500509 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 info_ptr->valid |= PNG_INFO_sBIT;
511}
512#endif
513
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500514#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500515void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600516png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600517{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500518 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500519
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600520 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600521 return;
522
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600523 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600524 info_ptr->valid |= PNG_INFO_sRGB;
525}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600526
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600528png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600529 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600530{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500531 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500532
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600533 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600534 return;
535
536 png_set_sRGB(png_ptr, info_ptr, intent);
537
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500538#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson82980f32010-06-26 12:31:58 -0500539 png_set_gAMA_fixed(png_ptr, info_ptr, 45455L);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600540#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600541
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500542#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson85b02372009-09-24 19:49:13 -0500543 png_set_cHRM_fixed(png_ptr, info_ptr,
Glenn Randers-Pehrson82980f32010-06-26 12:31:58 -0500544 /* color x y */
545 /* white */ 31270L, 32900L,
546 /* red */ 64000L, 33000L,
547 /* green */ 30000L, 60000L,
548 /* blue */ 15000L, 6000L
549 );
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600550#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600551}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600552#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600553
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600554
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500555#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500556void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600557png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600558 png_charp name, int compression_type,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500559 png_bytep profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600560{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500561 png_charp new_iccp_name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500562 png_bytep new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500563 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500564
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500565 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500566
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600567 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
568 return;
569
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500570 length = png_strlen(name)+1;
571 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500572 if (new_iccp_name == NULL)
573 {
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600574 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500575 return;
576 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500577 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500578 new_iccp_profile = (png_bytep)png_malloc_warn(png_ptr, proflen);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500579
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500580 if (new_iccp_profile == NULL)
581 {
582 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500583 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600584 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500585 return;
586 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500587
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500588 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
589
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500590 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500591
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600592 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500593 info_ptr->iccp_name = new_iccp_name;
594 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600595 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600596 * does not have to change if we introduce multiple compression types
597 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600598 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600599 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600600 info_ptr->valid |= PNG_INFO_iCCP;
601}
602#endif
603
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500604#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500605void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600607 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500609 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500610 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500611
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500612 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500614}
615
616int /* PRIVATE */
617png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600618 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500619{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500620 int i;
621
Glenn Randers-Pehrsone2a118f2009-07-27 22:08:25 -0500622 png_debug1(1, "in %s storage function", ((png_ptr == NULL ||
623 png_ptr->chunk_name[0] == '\0') ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600624 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600626 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500627 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500628
629 /* Make sure we have enough space in the "text" array in info_struct
630 * to hold all of the incoming text_ptr objects.
631 */
632 if (info_ptr->num_text + num_text > info_ptr->max_text)
633 {
634 if (info_ptr->text != NULL)
635 {
636 png_textp old_text;
637 int old_max;
638
639 old_max = info_ptr->max_text;
640 info_ptr->max_text = info_ptr->num_text + num_text + 8;
641 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500642 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500643 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500644
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500645 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500646 {
647 png_free(png_ptr, old_text);
648 return(1);
649 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500650
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600651 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600652 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500653 png_free(png_ptr, old_text);
654 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500655
Andreas Dilger47a0c421997-05-16 02:46:07 -0500656 else
657 {
658 info_ptr->max_text = num_text + 8;
659 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500660 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600661 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500662 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500663 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500664 info_ptr->free_me |= PNG_FREE_TEXT;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500665 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500666
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500667 png_debug1(3, "allocated %d entries for info_ptr->text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600668 info_ptr->max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 for (i = 0; i < num_text; i++)
671 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500672 png_size_t text_length, key_len;
673 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500674 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
675
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500676 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600677 continue;
678
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600679 key_len = png_strlen(text_ptr[i].key);
680
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500681 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500682 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500683 lang_len = 0;
684 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500685 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500686
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500687 else
Glenn Randers-Pehrson946c3f92010-03-30 14:50:07 -0500688#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600689 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500690 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600691
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500692 if (text_ptr[i].lang != NULL)
693 lang_len = png_strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500694
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500695 else
696 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500697
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500698 if (text_ptr[i].lang_key != NULL)
699 lang_key_len = png_strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500700
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500701 else
702 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600703 }
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600704#else /* PNG_iTXt_SUPPORTED */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500705 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500706 png_warning(png_ptr, "iTXt chunk not supported");
707 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500708 }
709#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500710
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500711 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600713 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500714#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500715 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600716 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500717
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600718 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500719#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600720 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500722
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 else
724 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 textp->compression = text_ptr[i].compression;
727 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500728
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500729 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600730 (png_size_t)
731 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500732
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500733 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500734 return(1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500735
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500736 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600737 (unsigned long)(png_uint_32)
738 (key_len + lang_len + lang_key_len + text_length + 4),
739 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500740
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500741 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500742 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500743
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600744 if (text_ptr[i].compression > 0)
745 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500746 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600747 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500748 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500749 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600750 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500751 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500752 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600753 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500754
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600755 else
756 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500757 textp->lang=NULL;
758 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500759 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600760 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500761
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500762 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500763 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600764 (png_size_t)(text_length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500765
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500766 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600767
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500768#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600770 {
771 textp->text_length = 0;
772 textp->itxt_length = text_length;
773 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500774
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600775 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500776#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600777 {
778 textp->text_length = text_length;
779 textp->itxt_length = 0;
780 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500783 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500785 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500786}
787#endif
788
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500789#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500790void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500791png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
792{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500793 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500794
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500795 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600796 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 return;
798
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500799 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800 info_ptr->valid |= PNG_INFO_tIME;
801}
802#endif
803
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500804#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500805void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500806png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600807 png_bytep trans_alpha, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500809 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500810
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600811 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500812 return;
813
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500814 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600815 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500816 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500817 * we do it for backward compatibility with the way the png_handle_tRNS
818 * function used to do the allocation.
819 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500820
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600821 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500822
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600823 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500824 png_ptr->trans_alpha = info_ptr->trans_alpha =
825 (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500826
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500828 png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600829 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500830
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500831 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500833 int sample_max = (1 << info_ptr->bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500834
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500835 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500836 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500837 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500838 ((int)trans_color->red > sample_max ||
839 (int)trans_color->green > sample_max ||
840 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500841 png_warning(png_ptr,
842 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500843
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500844 png_memcpy(&(info_ptr->trans_color), trans_color,
845 png_sizeof(png_color_16));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500846
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600847 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500848 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500850
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500852
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500853 if (num_trans != 0)
854 {
855 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500856 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500857 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858}
859#endif
860
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500861#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500862void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600863png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600864 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600865/*
866 * entries - array of png_sPLT_t structures
867 * to be added to the list of palettes
868 * in the info structure.
869 * nentries - number of palette structures to be
870 * added.
871 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600872{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500873 png_sPLT_tp np;
874 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600875
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500876 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500877 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500879 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
880 (info_ptr->splt_palettes_num + nentries) *
881 (png_size_t)png_sizeof(png_sPLT_t));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500882
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500883 if (np == NULL)
884 {
885 png_warning(png_ptr, "No memory for sPLT palettes");
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600886 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500887 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600888
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500889 png_memcpy(np, info_ptr->splt_palettes,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600890 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500891
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500892 png_free(png_ptr, info_ptr->splt_palettes);
893 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600894
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500895 for (i = 0; i < nentries; i++)
896 {
897 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
898 png_sPLT_tp from = entries + i;
899 png_uint_32 length;
900
901 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600902 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500903
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500904 if (to->name == NULL)
905 {
906 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600907 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500908 continue;
909 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500910
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500911 png_memcpy(to->name, from->name, length);
912 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600913 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500914
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500915 if (to->entries == NULL)
916 {
917 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600918 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500919 png_free(png_ptr, to->name);
920 to->name = NULL;
921 continue;
922 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500923
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500924 png_memcpy(to->entries, from->entries,
925 from->nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500926
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500927 to->nentries = from->nentries;
928 to->depth = from->depth;
929 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600930
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500931 info_ptr->splt_palettes = np;
932 info_ptr->splt_palettes_num += nentries;
933 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500934 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600935}
936#endif /* PNG_sPLT_SUPPORTED */
937
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -0600938#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500939void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600940png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600941 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600942{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500943 png_unknown_chunkp np;
944 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500946 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500947 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600948
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500949 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
950 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
951 png_sizeof(png_unknown_chunk)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500952
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500953 if (np == NULL)
954 {
955 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600956 "Out of memory while processing unknown chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500957 return;
958 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500960 png_memcpy(np, info_ptr->unknown_chunks,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600961 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500962
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500963 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600964 info_ptr->unknown_chunks = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600965
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500966 for (i = 0; i < num_unknowns; i++)
967 {
968 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
969 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600970
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600971 png_memcpy((png_charp)to->name, (png_charp)from->name,
972 png_sizeof(from->name));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500973 to->name[png_sizeof(to->name)-1] = '\0';
974 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500975 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500976 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600977
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500978 if (from->size == 0)
979 to->data=NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500980
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500981 else
982 {
983 to->data = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600984 (png_size_t)from->size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500985
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500986 if (to->data == NULL)
987 {
988 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600989 "Out of memory while processing unknown chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500990 to->size = 0;
991 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500992
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500993 else
994 png_memcpy(to->data, from->data, from->size);
995 }
996 }
997
998 info_ptr->unknown_chunks = np;
999 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001000 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001002void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001003png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001004 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001005{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001006 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001007 (int)info_ptr->unknown_chunks_num)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001008 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1009}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001010#endif
1011
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001012
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001013#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001014png_uint_32 PNGAPI
1015png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1016{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001017 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001018
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001019 if (png_ptr == NULL)
1020 return (png_uint_32)0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001021
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001022 png_ptr->mng_features_permitted =
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001023 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001024
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001025 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001026}
1027#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001028
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001029#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001030void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001031png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001032 chunk_list, int num_chunks)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001033{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001034 png_bytep new_list, p;
1035 int i, old_num_chunks;
1036 if (png_ptr == NULL)
1037 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039 if (num_chunks == 0)
1040 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001041 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001043
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001044 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001045 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001046
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001047 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001049
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001050 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001051 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001052
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001053 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001054 }
1055 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001056 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001057 old_num_chunks = png_ptr->num_chunk_list;
1058 new_list=(png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001059 (png_size_t)
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001060 (5*(num_chunks + old_num_chunks)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062 if (png_ptr->chunk_list != NULL)
1063 {
1064 png_memcpy(new_list, png_ptr->chunk_list,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001065 (png_size_t)(5*old_num_chunks));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001066 png_free(png_ptr, png_ptr->chunk_list);
1067 png_ptr->chunk_list=NULL;
1068 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001069
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001070 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001071 (png_size_t)(5*num_chunks));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001072
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1074 *p=(png_byte)keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001075
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001076 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1077 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001078 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001079}
1080#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001081
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001082#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001083void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001084png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001085 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001086{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001087 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001088
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001089 if (png_ptr == NULL)
1090 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001091
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1093 png_ptr->user_chunk_ptr = user_chunk_ptr;
1094}
1095#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001097#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001098void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001099png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1100{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001101 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001102
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001103 if (png_ptr == NULL || info_ptr == NULL)
1104 return;
1105
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001106 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001107 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001108
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001109 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001110
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001111 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001112 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001113}
1114#endif
1115
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001116void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001117png_set_compression_buffer_size(png_structp png_ptr,
1118 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001119{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001120 if (png_ptr == NULL)
1121 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001122
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001123 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001124 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001125 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1126 png_ptr->zstream.next_out = png_ptr->zbuf;
1127 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1128}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001129
1130void PNGAPI
1131png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1132{
1133 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001134 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001135}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001136
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001137
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001138
1139#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001140/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001141void PNGAPI
1142png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1143 png_uint_32 user_height_max)
1144{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001145 /* Images with dimensions larger than these limits will be
1146 * rejected by png_set_IHDR(). To accept any PNG datastream
1147 * regardless of dimensions, set both limits to 0x7ffffffL.
1148 */
1149 if (png_ptr == NULL)
1150 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001151
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001152 png_ptr->user_width_max = user_width_max;
1153 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001154}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001155
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001156/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001157void PNGAPI
1158png_set_chunk_cache_max (png_structp png_ptr,
1159 png_uint_32 user_chunk_cache_max)
1160{
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001161 if (png_ptr)
1162 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1163}
1164
1165/* This function was added to libpng 1.4.1 */
1166void PNGAPI
1167png_set_chunk_malloc_max (png_structp png_ptr,
1168 png_alloc_size_t user_chunk_malloc_max)
1169{
1170 if (png_ptr)
1171 png_ptr->user_chunk_malloc_max =
1172 (png_size_t)user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001173}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001174#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1175
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001177#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001178void PNGAPI
1179png_set_benign_errors(png_structp png_ptr, int allowed)
1180{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001181 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001182
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001183 if (allowed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001184 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001185
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001186 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001187 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001188}
1189#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001190#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */