blob: ec31c44a61df5f9c93528e30f01f9e71014b8bff [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-Pehrson866b62a2009-08-08 16:33:14 -05004 * Last changed in libpng 1.4.0 [August 8, 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-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
Andreas Dilger47a0c421997-05-16 02:46:07 -050019#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050021#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060023#if defined(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-Pehrsona357b991998-02-08 20:56:40 -060028 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029 return;
30
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050031 png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
Andreas Dilger47a0c421997-05-16 02:46:07 -050032 info_ptr->valid |= PNG_INFO_bKGD;
33}
34#endif
35
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060036#if defined(PNG_cHRM_SUPPORTED)
37#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050038void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050039png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
40 double white_x, double white_y, double red_x, double red_y,
41 double green_x, double green_y, double blue_x, double blue_y)
42{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050043 png_debug1(1, "in %s storage function", "cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060044 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050045 return;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060046
Andreas Dilger47a0c421997-05-16 02:46:07 -050047 info_ptr->x_white = (float)white_x;
48 info_ptr->y_white = (float)white_y;
49 info_ptr->x_red = (float)red_x;
50 info_ptr->y_red = (float)red_y;
51 info_ptr->x_green = (float)green_x;
52 info_ptr->y_green = (float)green_y;
53 info_ptr->x_blue = (float)blue_x;
54 info_ptr->y_blue = (float)blue_y;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060055#ifdef PNG_FIXED_POINT_SUPPORTED
56 info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
57 info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060058 info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5);
59 info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060060 info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
61 info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060062 info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5);
63 info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060064#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 info_ptr->valid |= PNG_INFO_cHRM;
66}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060067#endif /* PNG_FLOATING_POINT_SUPPORTED */
68
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060069#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050070void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060071png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060072 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
73 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
74 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060075{
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060076 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060077 if (png_ptr == NULL || info_ptr == NULL)
78 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050079
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060080#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060081 if (png_check_cHRM_fixed(png_ptr,
82 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060083#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050084 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050085 info_ptr->int_x_white = white_x;
86 info_ptr->int_y_white = white_y;
87 info_ptr->int_x_red = red_x;
88 info_ptr->int_y_red = red_y;
89 info_ptr->int_x_green = green_x;
90 info_ptr->int_y_green = green_y;
91 info_ptr->int_x_blue = blue_x;
92 info_ptr->int_y_blue = blue_y;
93#ifdef PNG_FLOATING_POINT_SUPPORTED
94 info_ptr->x_white = (float)(white_x/100000.);
95 info_ptr->y_white = (float)(white_y/100000.);
96 info_ptr->x_red = (float)( red_x/100000.);
97 info_ptr->y_red = (float)( red_y/100000.);
98 info_ptr->x_green = (float)(green_x/100000.);
99 info_ptr->y_green = (float)(green_y/100000.);
100 info_ptr->x_blue = (float)( blue_x/100000.);
101 info_ptr->y_blue = (float)( blue_y/100000.);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600102#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500103 info_ptr->valid |= PNG_INFO_cHRM;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600104 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600105}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600106#endif /* PNG_FIXED_POINT_SUPPORTED */
107#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600108
109#if defined(PNG_gAMA_SUPPORTED)
110#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
113{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500114 double png_gamma;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500115 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600116 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 return;
118
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600119 /* Check for overflow */
120 if (file_gamma > 21474.83)
121 {
122 png_warning(png_ptr, "Limiting gamma to 21474.83");
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500123 png_gamma=21474.83;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600124 }
125 else
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500126 png_gamma = file_gamma;
127 info_ptr->gamma = (float)png_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600128#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500129 info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600130#endif
131 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500132 if (png_gamma == 0.0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500133 png_warning(png_ptr, "Setting gamma=0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600134}
135#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500136void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600137png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
138 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600139{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500140 png_fixed_point png_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600141
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500142 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600143 if (png_ptr == NULL || info_ptr == NULL)
144 return;
145
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500146 if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600147 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 png_warning(png_ptr, "Limiting gamma to 21474.83");
149 png_gamma=PNG_UINT_31_MAX;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600150 }
151 else
152 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153 if (int_gamma < 0)
154 {
155 png_warning(png_ptr, "Setting negative gamma to zero");
156 png_gamma = 0;
157 }
158 else
159 png_gamma = int_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600160 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600161#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500162 info_ptr->gamma = (float)(png_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600163#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500164#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500165 info_ptr->int_gamma = png_gamma;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500166#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500168 if (png_gamma == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500169 png_warning(png_ptr, "Setting gamma=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500171#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600173#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500174void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
176{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500177 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600178
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500179 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600180 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500182 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600183 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500184 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500185 png_warning(png_ptr,
186 "Invalid palette size, hIST allocation skipped");
187 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500188 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600190#ifdef PNG_FREE_ME_SUPPORTED
191 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
192#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500193 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
194 * version 1.2.1
195 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500196 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500197 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500198 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199 {
200 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
201 return;
202 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600203
204 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500205 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600206 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600208
209#ifdef PNG_FREE_ME_SUPPORTED
210 info_ptr->free_me |= PNG_FREE_HIST;
211#else
212 png_ptr->flags |= PNG_FLAG_FREE_HIST;
213#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214}
215#endif
216
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500217void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
219 png_uint_32 width, png_uint_32 height, int bit_depth,
220 int color_type, int interlace_type, int compression_type,
221 int filter_type)
222{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500223 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600224 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500225 return;
226
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500227 /* Check for width and height valid values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500228 if (width == 0 || height == 0)
229 png_error(png_ptr, "Image width or height is zero in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500230#ifdef PNG_SET_USER_LIMITS_SUPPORTED
231 if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
232 png_error(png_ptr, "image size exceeds user limits in IHDR");
233#else
234 if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
235 png_error(png_ptr, "image size exceeds user limits in IHDR");
236#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500237 if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500238 png_error(png_ptr, "Invalid image size in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500239 if ( width > (PNG_UINT_32_MAX
240 >> 3) /* 8-byte RGBA pixels */
241 - 64 /* bigrowbuf hack */
242 - 1 /* filter byte */
243 - 7*8 /* rounding of width to multiple of 8 pixels */
244 - 8) /* extra max_pixel_depth pad */
245 png_warning(png_ptr, "Width is too large for libpng to process pixels");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500246
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500247 /* Check other values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500248 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500249 bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500250 png_error(png_ptr, "Invalid bit depth in IHDR");
251
252 if (color_type < 0 || color_type == 1 ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500253 color_type == 5 || color_type > 6)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500254 png_error(png_ptr, "Invalid color type in IHDR");
255
256 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
257 ((color_type == PNG_COLOR_TYPE_RGB ||
258 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
259 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
260 png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
261
262 if (interlace_type >= PNG_INTERLACE_LAST)
263 png_error(png_ptr, "Unknown interlace method in IHDR");
264
265 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
266 png_error(png_ptr, "Unknown compression method in IHDR");
267
268#if defined(PNG_MNG_FEATURES_SUPPORTED)
269 /* Accept filter_method 64 (intrapixel differencing) only if
270 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
271 * 2. Libpng did not read a PNG signature (this filter_method is only
272 * used in PNG datastreams that are embedded in MNG datastreams) and
273 * 3. The application called png_permit_mng_features with a mask that
274 * included PNG_FLAG_MNG_FILTER_64 and
275 * 4. The filter_method is 64 and
276 * 5. The color_type is RGB or RGBA
277 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500278 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
279 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
280 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500281 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500282 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500283 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
284 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
285 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500286 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
287 png_error(png_ptr, "Unknown filter method in IHDR");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500288 if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500289 png_warning(png_ptr, "Invalid filter method in IHDR");
290 }
291#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500292 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500293 png_error(png_ptr, "Unknown filter method in IHDR");
294#endif
295
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 info_ptr->width = width;
297 info_ptr->height = height;
298 info_ptr->bit_depth = (png_byte)bit_depth;
299 info_ptr->color_type =(png_byte) color_type;
300 info_ptr->compression_type = (png_byte)compression_type;
301 info_ptr->filter_type = (png_byte)filter_type;
302 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500303 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
304 info_ptr->channels = 1;
305 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 info_ptr->channels = 3;
307 else
308 info_ptr->channels = 1;
309 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
310 info_ptr->channels++;
311 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600312
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500313 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500314 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500315 >> 3) /* 8-byte RGBA pixels */
316 - 64 /* bigrowbuf hack */
317 - 1 /* filter byte */
318 - 7*8 /* rounding of width to multiple of 8 pixels */
319 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500320 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500321 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500322 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323}
324
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600325#if defined(PNG_oFFs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500326void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500327png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500330 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600331 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 return;
333
334 info_ptr->x_offset = offset_x;
335 info_ptr->y_offset = offset_y;
336 info_ptr->offset_unit_type = (png_byte)unit_type;
337 info_ptr->valid |= PNG_INFO_oFFs;
338}
339#endif
340
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600341#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500342void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
344 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
345 png_charp units, png_charpp params)
346{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500347 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600348 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500350 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600351 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 return;
353
354 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500355 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500356 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500357 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
358 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500360 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500361 return;
362 }
363 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500365 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 info_ptr->pcal_X0 = X0;
367 info_ptr->pcal_X1 = X1;
368 info_ptr->pcal_type = (png_byte)type;
369 info_ptr->pcal_nparams = (png_byte)nparams;
370
371 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500372 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500373 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500374 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
375 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500376 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500377 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500378 return;
379 }
380 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500382 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500383 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500384 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500385 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500386 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 return;
388 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500389
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600390 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391
392 for (i = 0; i < nparams; i++)
393 {
394 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500395 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500396 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500397 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
398 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500399 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500400 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
401 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500402 }
403 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 }
405
406 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500407#ifdef PNG_FREE_ME_SUPPORTED
408 info_ptr->free_me |= PNG_FREE_PCAL;
409#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410}
411#endif
412
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
414#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500415void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600416png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600417 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600418{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500419 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600420 if (png_ptr == NULL || info_ptr == NULL)
421 return;
422
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600423 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424 info_ptr->scal_pixel_width = width;
425 info_ptr->scal_pixel_height = height;
426
427 info_ptr->valid |= PNG_INFO_sCAL;
428}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600429#else
430#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500431void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600432png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600433 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500435 png_size_t length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500437 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600438 if (png_ptr == NULL || info_ptr == NULL)
439 return;
440
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600441 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600442
443 length = png_strlen(swidth) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500444 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500445 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500446 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
447 if (info_ptr->scal_s_width == NULL)
448 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500449 png_warning(png_ptr,
450 "Memory allocation failed while processing sCAL");
451 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500452 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500453 png_memcpy(info_ptr->scal_s_width, swidth, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600454
455 length = png_strlen(sheight) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500456 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500457 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500458 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
459 if (info_ptr->scal_s_height == NULL)
460 {
461 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500462 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500463 png_warning(png_ptr,
464 "Memory allocation failed while processing sCAL");
465 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500466 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500467 png_memcpy(info_ptr->scal_s_height, sheight, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600468 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500469#ifdef PNG_FREE_ME_SUPPORTED
470 info_ptr->free_me |= PNG_FREE_SCAL;
471#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600472}
473#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600474#endif
475#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600476
477#if defined(PNG_pHYs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500478void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500479png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
480 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
481{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500482 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600483 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500484 return;
485
486 info_ptr->x_pixels_per_unit = res_x;
487 info_ptr->y_pixels_per_unit = res_y;
488 info_ptr->phys_unit_type = (png_byte)unit_type;
489 info_ptr->valid |= PNG_INFO_pHYs;
490}
491#endif
492
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500493void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
495 png_colorp palette, int num_palette)
496{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600497
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500498 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600499 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 return;
501
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600502 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500503 {
504 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600505 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500506 else
507 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600508 png_warning(png_ptr, "Invalid palette length");
509 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 }
511 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600512
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600513 /*
514 * It may not actually be necessary to set png_ptr->palette here;
515 * we do it for backward compatibility with the way the png_handle_tRNS
516 * function used to do the allocation.
517 */
518#ifdef PNG_FREE_ME_SUPPORTED
519 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
520#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500521
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600522 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500523 * of num_palette entries, in case of an invalid PNG file that has
524 * too-large sample values.
525 */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600526#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600527 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500528 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600529#else
530 png_ptr->palette = (png_colorp)png_malloc(png_ptr,
531 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
532 png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
533 png_sizeof(png_color));
534#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500535 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600536 info_ptr->palette = png_ptr->palette;
537 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600538
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600539#ifdef PNG_FREE_ME_SUPPORTED
540 info_ptr->free_me |= PNG_FREE_PLTE;
541#else
542 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
543#endif
544
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600545 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546}
547
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600548#if defined(PNG_sBIT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500549void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
551 png_color_8p sig_bit)
552{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500553 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600554 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 return;
556
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500557 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 info_ptr->valid |= PNG_INFO_sBIT;
559}
560#endif
561
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600562#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500563void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600564png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600565{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500566 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600567 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600568 return;
569
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600570 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600571 info_ptr->valid |= PNG_INFO_sRGB;
572}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600573
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500574void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600575png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600576 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600577{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600578#if defined(PNG_gAMA_SUPPORTED)
579#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600580 float file_gamma;
581#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600582#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600583 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600584#endif
585#endif
586#if defined(PNG_cHRM_SUPPORTED)
587#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
589#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600590 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 -0600591 int_green_y, int_blue_x, int_blue_y;
592#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500593 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600594 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600595 return;
596
597 png_set_sRGB(png_ptr, info_ptr, intent);
598
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600599#if defined(PNG_gAMA_SUPPORTED)
600#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500601 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600602 png_set_gAMA(png_ptr, info_ptr, file_gamma);
603#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600604#ifdef PNG_FIXED_POINT_SUPPORTED
605 int_file_gamma = 45455L;
606 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
607#endif
608#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600609
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600610#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600611 int_white_x = 31270L;
612 int_white_y = 32900L;
613 int_red_x = 64000L;
614 int_red_y = 33000L;
615 int_green_x = 30000L;
616 int_green_y = 60000L;
617 int_blue_x = 15000L;
618 int_blue_y = 6000L;
619
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600620#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600621 white_x = (float).3127;
622 white_y = (float).3290;
623 red_x = (float).64;
624 red_y = (float).33;
625 green_x = (float).30;
626 green_y = (float).60;
627 blue_x = (float).15;
628 blue_y = (float).06;
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600629#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600630
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600631#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600632 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500633 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
634 int_green_y, int_blue_x, int_blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600635#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600636 {
637#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500638 png_set_cHRM_fixed(png_ptr, info_ptr,
639 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
640 int_green_y, int_blue_x, int_blue_y);
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600641#endif
642#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600643 png_set_cHRM(png_ptr, info_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500644 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600646 }
647#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600648}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600649#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600650
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600651
652#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500653void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600654png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
655 png_charp name, int compression_type,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600656 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600657{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500658 png_charp new_iccp_name;
659 png_charp new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500660 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500661
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500662 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600663 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
664 return;
665
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500666 length = png_strlen(name)+1;
667 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500668 if (new_iccp_name == NULL)
669 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500670 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500671 return;
672 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500673 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500674 new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
675 if (new_iccp_profile == NULL)
676 {
677 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500678 png_warning(png_ptr,
679 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500680 return;
681 }
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500682 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
683
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500684 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500685
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600686 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500687 info_ptr->iccp_name = new_iccp_name;
688 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600689 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500690 * does not have to change if we introduce multiple compression types */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600691 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500692#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600693 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500694#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 info_ptr->valid |= PNG_INFO_iCCP;
696}
697#endif
698
699#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500700void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500701png_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 -0500702 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500704 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500705 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500706 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500707 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500708}
709
710int /* PRIVATE */
711png_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 -0500712 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500713{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500714 int i;
715
Glenn Randers-Pehrsone2a118f2009-07-27 22:08:25 -0500716 png_debug1(1, "in %s storage function", ((png_ptr == NULL ||
717 png_ptr->chunk_name[0] == '\0') ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600718 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600720 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500721 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722
723 /* Make sure we have enough space in the "text" array in info_struct
724 * to hold all of the incoming text_ptr objects.
725 */
726 if (info_ptr->num_text + num_text > info_ptr->max_text)
727 {
728 if (info_ptr->text != NULL)
729 {
730 png_textp old_text;
731 int old_max;
732
733 old_max = info_ptr->max_text;
734 info_ptr->max_text = info_ptr->num_text + num_text + 8;
735 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500736 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500737 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500738 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500739 {
740 png_free(png_ptr, old_text);
741 return(1);
742 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600743 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500744 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500745 png_free(png_ptr, old_text);
746 }
747 else
748 {
749 info_ptr->max_text = num_text + 8;
750 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500751 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500752 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500753 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500754 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500755#ifdef PNG_FREE_ME_SUPPORTED
756 info_ptr->free_me |= PNG_FREE_TEXT;
757#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758 }
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500759 png_debug1(3, "allocated %d entries for info_ptr->text",
Andreas Dilger47a0c421997-05-16 02:46:07 -0500760 info_ptr->max_text);
761 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 for (i = 0; i < num_text; i++)
763 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500764 png_size_t text_length, key_len;
765 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500766 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
767
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500768 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600769 continue;
770
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600771 key_len = png_strlen(text_ptr[i].key);
772
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500773 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500774 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500775 lang_len = 0;
776 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500777 }
778 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500779#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600780 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500781 /* Set iTXt data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500782 if (text_ptr[i].lang != NULL)
783 lang_len = png_strlen(text_ptr[i].lang);
784 else
785 lang_len = 0;
786 if (text_ptr[i].lang_key != NULL)
787 lang_key_len = png_strlen(text_ptr[i].lang_key);
788 else
789 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600790 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500791#else
792 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793 png_warning(png_ptr, "iTXt chunk not supported");
794 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500795 }
796#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500798 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500799 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600800 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500801#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500802 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600803 textp->compression = PNG_ITXT_COMPRESSION_NONE;
804 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500805#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600806 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500807 }
808 else
809 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600810 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500811 textp->compression = text_ptr[i].compression;
812 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500813
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500814 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 (png_size_t)
816 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500817 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500818 return(1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500819 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500820 (unsigned long)(png_uint_32)
821 (key_len + lang_len + lang_key_len + text_length + 4),
822 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500823
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500825 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500826#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600827 if (text_ptr[i].compression > 0)
828 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500829 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600830 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500831 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500832 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600833 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500834 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500835 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600836 }
837 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500838#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600839 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500840#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500841 textp->lang=NULL;
842 textp->lang_key=NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500843#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500844 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600845 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500846 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500847 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600848 (png_size_t)(text_length));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600850
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500851#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500852 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600853 {
854 textp->text_length = 0;
855 textp->itxt_length = text_length;
856 }
857 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500858#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600859 {
860 textp->text_length = text_length;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500861#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600862 textp->itxt_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500863#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600864 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500866 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500868 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869}
870#endif
871
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600872#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500873void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500874png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
875{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500876 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500877 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600878 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879 return;
880
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500881 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500882 info_ptr->valid |= PNG_INFO_tIME;
883}
884#endif
885
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600886#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500887void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500889 png_bytep trans_alpha, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500890{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500891 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600892 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500893 return;
894
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500895 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600896 {
897 /*
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500898 * It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500899 * we do it for backward compatibility with the way the png_handle_tRNS
900 * function used to do the allocation.
901 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500902
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600903#ifdef PNG_FREE_ME_SUPPORTED
904 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
905#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500906
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600907 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500908 png_ptr->trans_alpha = info_ptr->trans_alpha = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500909 (png_size_t)PNG_MAX_PALETTE_LENGTH);
910 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500911 png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600912 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500914 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500916 int sample_max = (1 << info_ptr->bit_depth);
917 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500918 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500919 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500920 ((int)trans_color->red > sample_max ||
921 (int)trans_color->green > sample_max ||
922 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500923 png_warning(png_ptr,
924 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500925 png_memcpy(&(info_ptr->trans_color), trans_color,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500926 png_sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600927 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500928 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500930
Andreas Dilger47a0c421997-05-16 02:46:07 -0500931 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500932 if (num_trans != 0)
933 {
934 info_ptr->valid |= PNG_INFO_tRNS;
935#ifdef PNG_FREE_ME_SUPPORTED
936 info_ptr->free_me |= PNG_FREE_TRNS;
937#else
938 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
939#endif
940 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941}
942#endif
943
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600944#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500945void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600946png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600947 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600948/*
949 * entries - array of png_sPLT_t structures
950 * to be added to the list of palettes
951 * in the info structure.
952 * nentries - number of palette structures to be
953 * added.
954 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600955{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500956 png_sPLT_tp np;
957 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600958
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500959 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500960 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600961
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500962 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
963 (info_ptr->splt_palettes_num + nentries) *
964 (png_size_t)png_sizeof(png_sPLT_t));
965 if (np == NULL)
966 {
967 png_warning(png_ptr, "No memory for sPLT palettes");
968 return;
969 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600970
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500971 png_memcpy(np, info_ptr->splt_palettes,
972 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
973 png_free(png_ptr, info_ptr->splt_palettes);
974 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600975
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500976 for (i = 0; i < nentries; i++)
977 {
978 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
979 png_sPLT_tp from = entries + i;
980 png_uint_32 length;
981
982 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500983 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500984 if (to->name == NULL)
985 {
986 png_warning(png_ptr,
987 "Out of memory while processing sPLT chunk");
988 continue;
989 }
990 png_memcpy(to->name, from->name, length);
991 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500992 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500993 if (to->entries == NULL)
994 {
995 png_warning(png_ptr,
996 "Out of memory while processing sPLT chunk");
997 png_free(png_ptr, to->name);
998 to->name = NULL;
999 continue;
1000 }
1001 png_memcpy(to->entries, from->entries,
1002 from->nentries * png_sizeof(png_sPLT_entry));
1003 to->nentries = from->nentries;
1004 to->depth = from->depth;
1005 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001007 info_ptr->splt_palettes = np;
1008 info_ptr->splt_palettes_num += nentries;
1009 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001010#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001011 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001012#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013}
1014#endif /* PNG_sPLT_SUPPORTED */
1015
Glenn Randers-Pehrson59c39b82009-06-16 17:23:08 -05001016#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001017void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001018png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001019 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001020{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001021 png_unknown_chunkp np;
1022 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001023
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001025 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001026
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
1028 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
1029 png_sizeof(png_unknown_chunk)));
1030 if (np == NULL)
1031 {
1032 png_warning(png_ptr,
1033 "Out of memory while processing unknown chunk");
1034 return;
1035 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001036
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037 png_memcpy(np, info_ptr->unknown_chunks,
1038 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
1039 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001040
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001041 for (i = 0; i < num_unknowns; i++)
1042 {
1043 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
1044 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001045
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001046 png_memcpy((png_charp)to->name,
1047 (png_charp)from->name,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048 png_sizeof(from->name));
1049 to->name[png_sizeof(to->name)-1] = '\0';
1050 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001051 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001052 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001053
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001054 if (from->size == 0)
1055 to->data=NULL;
1056 else
1057 {
1058 to->data = (png_bytep)png_malloc_warn(png_ptr,
1059 (png_size_t)from->size);
1060 if (to->data == NULL)
1061 {
1062 png_warning(png_ptr,
1063 "Out of memory while processing unknown chunk");
1064 to->size = 0;
1065 }
1066 else
1067 png_memcpy(to->data, from->data, from->size);
1068 }
1069 }
1070
1071 info_ptr->unknown_chunks = np;
1072 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001073#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001075#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001077void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001078png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
1079 int chunk, int location)
1080{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001081 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001082 (int)info_ptr->unknown_chunks_num)
1083 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1084}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001085#endif
1086
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001087
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001088#if defined(PNG_MNG_FEATURES_SUPPORTED)
1089png_uint_32 PNGAPI
1090png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1091{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001092 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001093 if (png_ptr == NULL)
1094 return (png_uint_32)0;
1095 png_ptr->mng_features_permitted =
1096 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
1097 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001098}
1099#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001100
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001101#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001102void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001103png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
1104 chunk_list, int num_chunks)
1105{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001106 png_bytep new_list, p;
1107 int i, old_num_chunks;
1108 if (png_ptr == NULL)
1109 return;
1110 if (num_chunks == 0)
1111 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001112 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001113 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001114 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001116
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001117 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
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 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001120 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001121 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001122 }
1123 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001124 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001125 old_num_chunks = png_ptr->num_chunk_list;
1126 new_list=(png_bytep)png_malloc(png_ptr,
1127 (png_size_t)
1128 (5*(num_chunks + old_num_chunks)));
1129 if (png_ptr->chunk_list != NULL)
1130 {
1131 png_memcpy(new_list, png_ptr->chunk_list,
1132 (png_size_t)(5*old_num_chunks));
1133 png_free(png_ptr, png_ptr->chunk_list);
1134 png_ptr->chunk_list=NULL;
1135 }
1136 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
1137 (png_size_t)(5*num_chunks));
1138 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1139 *p=(png_byte)keep;
1140 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1141 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001142#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001143 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001144#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001145}
1146#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001147
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001148#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001149void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001150png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
1151 png_user_chunk_ptr read_user_chunk_fn)
1152{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001153 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001154 if (png_ptr == NULL)
1155 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001156 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1157 png_ptr->user_chunk_ptr = user_chunk_ptr;
1158}
1159#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001160
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001161#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001162void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001163png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1164{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001165 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001166
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001167 if (png_ptr == NULL || info_ptr == NULL)
1168 return;
1169
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001170 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001171 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001172 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001173 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001174 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001175}
1176#endif
1177
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001178#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001180png_set_compression_buffer_size(png_structp png_ptr,
1181 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001182{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001183 if (png_ptr == NULL)
1184 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001185 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001186 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001187 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1188 png_ptr->zstream.next_out = png_ptr->zbuf;
1189 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1190}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001191#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001192
1193void PNGAPI
1194png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1195{
1196 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001197 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001198}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001199
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001200
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001201
1202#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001203/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001204void PNGAPI
1205png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1206 png_uint_32 user_height_max)
1207{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001208 /* Images with dimensions larger than these limits will be
1209 * rejected by png_set_IHDR(). To accept any PNG datastream
1210 * regardless of dimensions, set both limits to 0x7ffffffL.
1211 */
1212 if (png_ptr == NULL)
1213 return;
1214 png_ptr->user_width_max = user_width_max;
1215 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001216}
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001217/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001218void PNGAPI
1219png_set_chunk_cache_max (png_structp png_ptr,
1220 png_uint_32 user_chunk_cache_max)
1221{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001222 if (png_ptr == NULL)
1223 return;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001224 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1225 if (user_chunk_cache_max == 0x7fffffffL) /* Unlimited */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001226 png_ptr->user_chunk_cache_max = 0;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001227 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001228 png_ptr->user_chunk_cache_max = user_chunk_cache_max + 1;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001229}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001230#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1231
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001232
1233#if defined(PNG_BENIGN_ERRORS_SUPPORTED)
1234void PNGAPI
1235png_set_benign_errors(png_structp png_ptr, int allowed)
1236{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001237 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001238 if (allowed)
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 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001241 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001242}
1243#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001244#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */