blob: 096d44c25e7190b1402d928bcc027ea4a915eb57 [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-Pehrson3097f612001-05-07 14:52:45 -05004 * libpng 1.2.0beta2 - May 7, 2001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -06006 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
10 * The functions here are used during reads to store data from the file
11 * into the info struct, and during writes to store application data
12 * into the info struct for writing into the file. This abstracts the
13 * info struct and allows us to change the structure in the future.
14 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050015
16#define PNG_INTERNAL
17#include "png.h"
18
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060019#if defined(PNG_bKGD_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050020void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050021png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
22{
23 png_debug1(1, "in %s storage function\n", "bKGD");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060024 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050025 return;
26
27 png_memcpy(&(info_ptr->background), background, sizeof(png_color_16));
28 info_ptr->valid |= PNG_INFO_bKGD;
29}
30#endif
31
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060032#if defined(PNG_cHRM_SUPPORTED)
33#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050034void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050035png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
36 double white_x, double white_y, double red_x, double red_y,
37 double green_x, double green_y, double blue_x, double blue_y)
38{
39 png_debug1(1, "in %s storage function\n", "cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060040 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050041 return;
42
43 info_ptr->x_white = (float)white_x;
44 info_ptr->y_white = (float)white_y;
45 info_ptr->x_red = (float)red_x;
46 info_ptr->y_red = (float)red_y;
47 info_ptr->x_green = (float)green_x;
48 info_ptr->y_green = (float)green_y;
49 info_ptr->x_blue = (float)blue_x;
50 info_ptr->y_blue = (float)blue_y;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060051#ifdef PNG_FIXED_POINT_SUPPORTED
52 info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
53 info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
54 info_ptr->int_x_red = (png_fixed_point)(red_x*100000.+0.5);
55 info_ptr->int_y_red = (png_fixed_point)(red_y*100000.+0.5);
56 info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
57 info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
58 info_ptr->int_x_blue = (png_fixed_point)(blue_x*100000.+0.5);
59 info_ptr->int_y_blue = (png_fixed_point)(blue_y*100000.+0.5);
60#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050061 info_ptr->valid |= PNG_INFO_cHRM;
62}
63#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060064#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050065void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060067 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
68 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
69 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060070{
71 png_debug1(1, "in %s storage function\n", "cHRM");
72 if (png_ptr == NULL || info_ptr == NULL)
73 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050074
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060075 info_ptr->int_x_white = white_x;
76 info_ptr->int_y_white = white_y;
77 info_ptr->int_x_red = red_x;
78 info_ptr->int_y_red = red_y;
79 info_ptr->int_x_green = green_x;
80 info_ptr->int_y_green = green_y;
81 info_ptr->int_x_blue = blue_x;
82 info_ptr->int_y_blue = blue_y;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060083#ifdef PNG_FLOATING_POINT_SUPPORTED
84 info_ptr->x_white = (float)(white_x/100000.);
85 info_ptr->y_white = (float)(white_y/100000.);
86 info_ptr->x_red = (float)(red_x/100000.);
87 info_ptr->y_red = (float)(red_y/100000.);
88 info_ptr->x_green = (float)(green_x/100000.);
89 info_ptr->y_green = (float)(green_y/100000.);
90 info_ptr->x_blue = (float)(blue_x/100000.);
91 info_ptr->y_blue = (float)(blue_y/100000.);
92#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060093 info_ptr->valid |= PNG_INFO_cHRM;
94}
95#endif
96#endif
97
98#if defined(PNG_gAMA_SUPPORTED)
99#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
102{
103 png_debug1(1, "in %s storage function\n", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600104 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105 return;
106
107 info_ptr->gamma = (float)file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600108#ifdef PNG_FIXED_POINT_SUPPORTED
109 info_ptr->int_gamma = (int)(file_gamma*100000.+.5);
110#endif
111 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500112 if(file_gamma == 0.0)
113 png_warning(png_ptr, "Setting gamma=0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600114}
115#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500116void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600117png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
118 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600119{
120 png_debug1(1, "in %s storage function\n", "gAMA");
121 if (png_ptr == NULL || info_ptr == NULL)
122 return;
123
124#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600125 info_ptr->gamma = (float)(int_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600126#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500127#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600128 info_ptr->int_gamma = int_gamma;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500129#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500131 if(int_gamma == 0)
132 png_warning(png_ptr, "Setting gamma=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500134#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600136#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500137void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500138png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
139{
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600140 int i;
141
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142 png_debug1(1, "in %s storage function\n", "hIST");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600143 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 return;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600145 if (info_ptr->num_palette == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500146 {
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600147 png_warning(png_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500148 "Palette size 0, hIST allocation skipped.");
149 return;
150 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600152#ifdef PNG_FREE_ME_SUPPORTED
153 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
154#endif
155 png_ptr->hist = (png_uint_16p)png_malloc(png_ptr,
156 (png_uint_32)(info_ptr->num_palette * sizeof (png_uint_16)));
157
158 for (i = 0; i < info_ptr->num_palette; i++)
159 png_ptr->hist[i] = hist[i];
160 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600162
163#ifdef PNG_FREE_ME_SUPPORTED
164 info_ptr->free_me |= PNG_FREE_HIST;
165#else
166 png_ptr->flags |= PNG_FLAG_FREE_HIST;
167#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168}
169#endif
170
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500171void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
173 png_uint_32 width, png_uint_32 height, int bit_depth,
174 int color_type, int interlace_type, int compression_type,
175 int filter_type)
176{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600177 int rowbytes_per_pixel;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_debug1(1, "in %s storage function\n", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600179 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 return;
181
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500182 /* check for width and height valid values */
183 if (width == 0 || height == 0)
184 png_error(png_ptr, "Image width or height is zero in IHDR");
185 if (width > PNG_MAX_UINT || height > PNG_MAX_UINT)
186 png_error(png_ptr, "Invalid image size in IHDR");
187
188 /* check other values */
189 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
190 bit_depth != 8 && bit_depth != 16)
191 png_error(png_ptr, "Invalid bit depth in IHDR");
192
193 if (color_type < 0 || color_type == 1 ||
194 color_type == 5 || color_type > 6)
195 png_error(png_ptr, "Invalid color type in IHDR");
196
197 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
198 ((color_type == PNG_COLOR_TYPE_RGB ||
199 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
200 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
201 png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
202
203 if (interlace_type >= PNG_INTERLACE_LAST)
204 png_error(png_ptr, "Unknown interlace method in IHDR");
205
206 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
207 png_error(png_ptr, "Unknown compression method in IHDR");
208
209#if defined(PNG_MNG_FEATURES_SUPPORTED)
210 /* Accept filter_method 64 (intrapixel differencing) only if
211 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
212 * 2. Libpng did not read a PNG signature (this filter_method is only
213 * used in PNG datastreams that are embedded in MNG datastreams) and
214 * 3. The application called png_permit_mng_features with a mask that
215 * included PNG_FLAG_MNG_FILTER_64 and
216 * 4. The filter_method is 64 and
217 * 5. The color_type is RGB or RGBA
218 */
219 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
220 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
221 if(filter_type != PNG_FILTER_TYPE_BASE)
222 {
223 if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
224 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
225 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
226 (color_type == PNG_COLOR_TYPE_RGB ||
227 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
228 png_error(png_ptr, "Unknown filter method in IHDR");
229 if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
230 png_warning(png_ptr, "Invalid filter method in IHDR");
231 }
232#else
233 if(filter_type != PNG_FILTER_TYPE_BASE)
234 png_error(png_ptr, "Unknown filter method in IHDR");
235#endif
236
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 info_ptr->width = width;
238 info_ptr->height = height;
239 info_ptr->bit_depth = (png_byte)bit_depth;
240 info_ptr->color_type =(png_byte) color_type;
241 info_ptr->compression_type = (png_byte)compression_type;
242 info_ptr->filter_type = (png_byte)filter_type;
243 info_ptr->interlace_type = (png_byte)interlace_type;
244 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
245 info_ptr->channels = 1;
246 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
247 info_ptr->channels = 3;
248 else
249 info_ptr->channels = 1;
250 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
251 info_ptr->channels++;
252 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600253
254 /* check for overflow */
255 rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500256 if (( width > PNG_MAX_UINT/rowbytes_per_pixel))
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600257 {
258 png_warning(png_ptr,
259 "Width too large to process image data; rowbytes will overflow.");
260 info_ptr->rowbytes = (png_size_t)0;
261 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500262 else
263 info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264}
265
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266#if defined(PNG_oFFs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500267void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500270{
271 png_debug1(1, "in %s storage function\n", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600272 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273 return;
274
275 info_ptr->x_offset = offset_x;
276 info_ptr->y_offset = offset_y;
277 info_ptr->offset_unit_type = (png_byte)unit_type;
278 info_ptr->valid |= PNG_INFO_oFFs;
279}
280#endif
281
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500283void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
285 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
286 png_charp units, png_charpp params)
287{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600288 png_uint_32 length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600289 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290
291 png_debug1(1, "in %s storage function\n", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600292 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 return;
294
295 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500296 png_debug1(3, "allocating purpose for info (%lu bytes)\n", length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 info_ptr->pcal_purpose = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600298 png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299
300 png_debug(3, "storing X0, X1, type, and nparams in info\n");
301 info_ptr->pcal_X0 = X0;
302 info_ptr->pcal_X1 = X1;
303 info_ptr->pcal_type = (png_byte)type;
304 info_ptr->pcal_nparams = (png_byte)nparams;
305
306 length = png_strlen(units) + 1;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500307 png_debug1(3, "allocating units for info (%lu bytes)\n", length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500308 info_ptr->pcal_units = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600309 png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310
311 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600312 (png_uint_32)((nparams + 1) * sizeof(png_charp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500313
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 info_ptr->pcal_params[nparams] = NULL;
315
316 for (i = 0; i < nparams; i++)
317 {
318 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500319 png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320 info_ptr->pcal_params[i] = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500321 png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 }
323
324 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500325#ifdef PNG_FREE_ME_SUPPORTED
326 info_ptr->free_me |= PNG_FREE_PCAL;
327#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328}
329#endif
330
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600331#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
332#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500333void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600334png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600335 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600336{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600337 png_debug1(1, "in %s storage function\n", "sCAL");
338 if (png_ptr == NULL || info_ptr == NULL)
339 return;
340
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600341 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600342 info_ptr->scal_pixel_width = width;
343 info_ptr->scal_pixel_height = height;
344
345 info_ptr->valid |= PNG_INFO_sCAL;
346}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347#else
348#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500349void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600350png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600351 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600352{
353 png_uint_32 length;
354
355 png_debug1(1, "in %s storage function\n", "sCAL");
356 if (png_ptr == NULL || info_ptr == NULL)
357 return;
358
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600359 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600360
361 length = png_strlen(swidth) + 1;
362 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
363 info_ptr->scal_s_width = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500364 png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365
366 length = png_strlen(sheight) + 1;
367 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500368 info_ptr->scal_s_height = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500369 png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600370
371 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500372#ifdef PNG_FREE_ME_SUPPORTED
373 info_ptr->free_me |= PNG_FREE_SCAL;
374#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375}
376#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600377#endif
378#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600379
380#if defined(PNG_pHYs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500381void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
383 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
384{
385 png_debug1(1, "in %s storage function\n", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600386 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 return;
388
389 info_ptr->x_pixels_per_unit = res_x;
390 info_ptr->y_pixels_per_unit = res_y;
391 info_ptr->phys_unit_type = (png_byte)unit_type;
392 info_ptr->valid |= PNG_INFO_pHYs;
393}
394#endif
395
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500396void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
398 png_colorp palette, int num_palette)
399{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600400
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401 png_debug1(1, "in %s storage function\n", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600402 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 return;
404
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600405 /*
406 * It may not actually be necessary to set png_ptr->palette here;
407 * we do it for backward compatibility with the way the png_handle_tRNS
408 * function used to do the allocation.
409 */
410#ifdef PNG_FREE_ME_SUPPORTED
411 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
412#endif
413 png_ptr->palette = (png_colorp)png_zalloc(png_ptr, (uInt)num_palette,
414 sizeof (png_color));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500415 png_memcpy(png_ptr->palette, palette, num_palette * sizeof (png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600416 info_ptr->palette = png_ptr->palette;
417 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600418
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600419#ifdef PNG_FREE_ME_SUPPORTED
420 info_ptr->free_me |= PNG_FREE_PLTE;
421#else
422 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
423#endif
424
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600425 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426}
427
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600428#if defined(PNG_sBIT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500429void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
431 png_color_8p sig_bit)
432{
433 png_debug1(1, "in %s storage function\n", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600434 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 return;
436
437 png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
438 info_ptr->valid |= PNG_INFO_sBIT;
439}
440#endif
441
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600442#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500443void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600444png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600445{
446 png_debug1(1, "in %s storage function\n", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600447 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600448 return;
449
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600450 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600451 info_ptr->valid |= PNG_INFO_sRGB;
452}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600453
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500454void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600455png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600456 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600457{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600458#if defined(PNG_gAMA_SUPPORTED)
459#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600460 float file_gamma;
461#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600462#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600463 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600464#endif
465#endif
466#if defined(PNG_cHRM_SUPPORTED)
467#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600468 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
469#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600470#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600471 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 -0600472 int_green_y, int_blue_x, int_blue_y;
473#endif
474#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600475 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600476 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600477 return;
478
479 png_set_sRGB(png_ptr, info_ptr, intent);
480
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600481#if defined(PNG_gAMA_SUPPORTED)
482#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500483 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600484 png_set_gAMA(png_ptr, info_ptr, file_gamma);
485#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600486#ifdef PNG_FIXED_POINT_SUPPORTED
487 int_file_gamma = 45455L;
488 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
489#endif
490#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600491
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600492#if defined(PNG_cHRM_SUPPORTED)
493#ifdef PNG_FIXED_POINT_SUPPORTED
494 int_white_x = 31270L;
495 int_white_y = 32900L;
496 int_red_x = 64000L;
497 int_red_y = 33000L;
498 int_green_x = 30000L;
499 int_green_y = 60000L;
500 int_blue_x = 15000L;
501 int_blue_y = 6000L;
502
503 png_set_cHRM_fixed(png_ptr, info_ptr,
504 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
505 int_blue_x, int_blue_y);
506#endif
507#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600508 white_x = (float).3127;
509 white_y = (float).3290;
510 red_x = (float).64;
511 red_y = (float).33;
512 green_x = (float).30;
513 green_y = (float).60;
514 blue_x = (float).15;
515 blue_y = (float).06;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600516
517 png_set_cHRM(png_ptr, info_ptr,
518 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600519#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600520#endif
521}
522#endif
523
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600524
525#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500526void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600527png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
528 png_charp name, int compression_type,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600529 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600530{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500531 png_charp new_iccp_name;
532 png_charp new_iccp_profile;
533
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600534 png_debug1(1, "in %s storage function\n", "iCCP");
535 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
536 return;
537
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500538 new_iccp_name = (png_charp)png_malloc(png_ptr, png_strlen(name)+1);
539 png_strcpy(new_iccp_name, name);
540 new_iccp_profile = (png_charp)png_malloc(png_ptr, proflen);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500541 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
542
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500543 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500544
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600545 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500546 info_ptr->iccp_name = new_iccp_name;
547 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600548 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500549 * does not have to change if we introduce multiple compression types */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600550 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500551#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600552 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500553#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600554 info_ptr->valid |= PNG_INFO_iCCP;
555}
556#endif
557
558#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500559void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500560png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
561 int num_text)
562{
563 int i;
564
565 png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600566 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600568 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 return;
570
571 /* Make sure we have enough space in the "text" array in info_struct
572 * to hold all of the incoming text_ptr objects.
573 */
574 if (info_ptr->num_text + num_text > info_ptr->max_text)
575 {
576 if (info_ptr->text != NULL)
577 {
578 png_textp old_text;
579 int old_max;
580
581 old_max = info_ptr->max_text;
582 info_ptr->max_text = info_ptr->num_text + num_text + 8;
583 old_text = info_ptr->text;
584 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600585 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600586 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
587 sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 png_free(png_ptr, old_text);
589 }
590 else
591 {
592 info_ptr->max_text = num_text + 8;
593 info_ptr->num_text = 0;
594 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600595 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500596#ifdef PNG_FREE_ME_SUPPORTED
597 info_ptr->free_me |= PNG_FREE_TEXT;
598#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 }
600 png_debug1(3, "allocated %d entries for info_ptr->text\n",
601 info_ptr->max_text);
602 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 for (i = 0; i < num_text; i++)
604 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500605 png_size_t text_length,key_len;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500606 png_size_t lang_len,lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500607 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
608
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500609 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600610 continue;
611
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600612 key_len = png_strlen(text_ptr[i].key);
613
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500614 if(text_ptr[i].compression <= 0)
615 {
616 lang_len = 0;
617 lang_key_len = 0;
618 }
619 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500620#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600621 {
622 /* set iTXt data */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500623 if (text_ptr[i].key != NULL)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500624 lang_len = png_strlen(text_ptr[i].lang);
625 else
626 lang_len = 0;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500627 if (text_ptr[i].lang_key != NULL)
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500628 lang_key_len = png_strlen(text_ptr[i].lang_key);
629 else
630 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600631 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500632#else
633 {
634 png_warning(png_ptr, "iTXt chunk not supported.");
635 continue;
636 }
637#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500638
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500639 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500640 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600641 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500642#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600643 if(text_ptr[i].compression > 0)
644 textp->compression = PNG_ITXT_COMPRESSION_NONE;
645 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500646#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600647 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648 }
649 else
650 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600651 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500652 textp->compression = text_ptr[i].compression;
653 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500654
655 textp->key = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500656 (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500657 png_debug2(2, "Allocated %d bytes at %x in png_set_text\n",
658 key_len + lang_len + lang_key_len + text_length + 4, (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500659
660 png_memcpy(textp->key, text_ptr[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600661 (png_size_t)(key_len));
662 *(textp->key+key_len) = '\0';
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500663#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600664 if (text_ptr[i].compression > 0)
665 {
666 textp->lang=textp->key + key_len + 1;
667 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
668 *(textp->lang+lang_len) = '\0';
669 textp->lang_key=textp->lang + lang_len + 1;
670 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
671 *(textp->lang_key+lang_key_len) = '\0';
672 textp->text=textp->lang_key + lang_key_len + 1;
673 }
674 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500675#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600676 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500677#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500678 textp->lang=(png_charp)NULL;
679 textp->lang_key=(png_charp)NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500680#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600681 textp->text=textp->key + key_len + 1;
682 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600683 if(text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500684 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600685 (png_size_t)(text_length));
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500686 *(textp->text+text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600687
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500688#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600689 if(textp->compression > 0)
690 {
691 textp->text_length = 0;
692 textp->itxt_length = text_length;
693 }
694 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500695#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 {
697 textp->text_length = text_length;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500698#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600699 textp->itxt_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500700#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600701 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500702 info_ptr->text[info_ptr->num_text]= *textp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 info_ptr->num_text++;
704 png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
705 }
706}
707#endif
708
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600709#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500710void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
712{
713 png_debug1(1, "in %s storage function\n", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500714 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600715 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 return;
717
718 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
719 info_ptr->valid |= PNG_INFO_tIME;
720}
721#endif
722
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600723#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500724void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
726 png_bytep trans, int num_trans, png_color_16p trans_values)
727{
728 png_debug1(1, "in %s storage function\n", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600729 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500730 return;
731
732 if (trans != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600733 {
734 /*
735 * It may not actually be necessary to set png_ptr->trans here;
736 * we do it for backward compatibility with the way the png_handle_tRNS
737 * function used to do the allocation.
738 */
739#ifdef PNG_FREE_ME_SUPPORTED
740 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
741#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500742 png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
743 num_trans);
744 png_memcpy(info_ptr->trans, trans, num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600745#ifdef PNG_FREE_ME_SUPPORTED
746 info_ptr->free_me |= PNG_FREE_TRNS;
747#else
748 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
749#endif
750 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751
752 if (trans_values != NULL)
753 {
754 png_memcpy(&(info_ptr->trans_values), trans_values,
755 sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600756 if (num_trans == 0)
757 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758 }
759 info_ptr->num_trans = (png_uint_16)num_trans;
760 info_ptr->valid |= PNG_INFO_tRNS;
761}
762#endif
763
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500765void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600766png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600767 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600768{
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600769 png_sPLT_tp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600770 int i;
771
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600772 np = (png_sPLT_tp)png_malloc(png_ptr,
773 (info_ptr->splt_palettes_num + nentries) * sizeof(png_sPLT_t));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600774
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600775 png_memcpy(np, info_ptr->splt_palettes,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600776 info_ptr->splt_palettes_num * sizeof(png_sPLT_t));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600777 png_free(png_ptr, info_ptr->splt_palettes);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500778 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600779
780 for (i = 0; i < nentries; i++)
781 {
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600782 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
783 png_sPLT_tp from = entries + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784
785 to->name = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500786 png_strlen(from->name) + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600787 png_strcpy(to->name, from->name);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600788 to->entries = (png_sPLT_entryp)png_malloc(png_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500789 from->nentries * sizeof(png_sPLT_t));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500790 png_memcpy(to->entries, from->entries,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500791 from->nentries * sizeof(png_sPLT_t));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500792 to->nentries = from->nentries;
793 to->depth = from->depth;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600794 }
795
796 info_ptr->splt_palettes = np;
797 info_ptr->splt_palettes_num += nentries;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600798 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500799#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600800 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500801#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802}
803#endif /* PNG_sPLT_SUPPORTED */
804
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500805#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500806void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600807png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600808 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600809{
810 png_unknown_chunkp np;
811 int i;
812
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600813 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
814 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600815
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600816 np = (png_unknown_chunkp)png_malloc(png_ptr,
817 (info_ptr->unknown_chunks_num + num_unknowns) *
818 sizeof(png_unknown_chunk));
819
820 png_memcpy(np, info_ptr->unknown_chunks,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600821 info_ptr->unknown_chunks_num * sizeof(png_unknown_chunk));
822 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500823 info_ptr->unknown_chunks=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600824
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 for (i = 0; i < num_unknowns; i++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600826 {
827 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
828 png_unknown_chunkp from = unknowns + i;
829
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600830 png_strcpy((png_charp)to->name, (png_charp)from->name);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600831 to->data = (png_bytep)png_malloc(png_ptr, from->size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500832 png_memcpy(to->data, from->data, from->size);
833 to->size = from->size;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500835 /* note our location in the read or write sequence */
836 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837 }
838
839 info_ptr->unknown_chunks = np;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600840 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500841#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600842 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500843#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600844}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500845void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500846png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
847 int chunk, int location)
848{
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600849 if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500850 (int)info_ptr->unknown_chunks_num)
851 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
852}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600853#endif
854
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600855#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
856 defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500857void PNGAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500858png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
859{
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600860 /* This function is deprecated in favor of png_permit_mng_features()
861 and will be removed from libpng-2.0.0 */
862 png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500863 if (png_ptr == NULL)
864 return;
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600865 png_ptr->mng_features_permitted = (png_byte)
866 ((png_ptr->mng_features_permitted & (~(PNG_FLAG_MNG_EMPTY_PLTE))) |
867 ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
868}
869#endif
870
871#if defined(PNG_MNG_FEATURES_SUPPORTED)
872png_uint_32 PNGAPI
873png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
874{
875 png_debug(1, "in png_permit_mng_features\n");
876 if (png_ptr == NULL)
877 return (png_uint_32)0;
878 png_ptr->mng_features_permitted =
879 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
880 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500881}
882#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600883
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600884#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500885void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600886png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
887 chunk_list, int num_chunks)
888{
889 png_bytep new_list, p;
890 int i, old_num_chunks;
891 if (num_chunks == 0)
892 {
893 if(keep == HANDLE_CHUNK_ALWAYS || keep == HANDLE_CHUNK_IF_SAFE)
894 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
895 else
896 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600897
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600898 if(keep == HANDLE_CHUNK_ALWAYS)
899 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
900 else
901 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
902 return;
903 }
904 if (chunk_list == NULL)
905 return;
906 old_num_chunks=png_ptr->num_chunk_list;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500907 new_list=(png_bytep)png_malloc(png_ptr,5*(num_chunks+old_num_chunks));
908 if(png_ptr->chunk_list != NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600909 {
910 png_memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600911 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500912 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600913 }
914 png_memcpy(new_list+5*old_num_chunks, chunk_list, 5*num_chunks);
915 for (p=new_list+5*old_num_chunks+4, i=0; i<num_chunks; i++, p+=5)
916 *p=(png_byte)keep;
917 png_ptr->num_chunk_list=old_num_chunks+num_chunks;
918 png_ptr->chunk_list=new_list;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500919#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600920 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500921#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600922}
923#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600924
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600925#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500926void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600927png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
928 png_user_chunk_ptr read_user_chunk_fn)
929{
930 png_debug(1, "in png_set_read_user_chunk_fn\n");
931 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
932 png_ptr->user_chunk_ptr = user_chunk_ptr;
933}
934#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600935
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600936#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500937void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600938png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
939{
940 png_debug1(1, "in %s storage function\n", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500941
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600942 if (png_ptr == NULL || info_ptr == NULL)
943 return;
944
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500945 if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500946 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500947 info_ptr->row_pointers = row_pointers;
948 if(row_pointers)
949 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600950}
951#endif
952
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500953void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500954png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
955{
956 if(png_ptr->zbuf)
957 png_free(png_ptr, png_ptr->zbuf);
958 png_ptr->zbuf_size = (png_size_t)size;
959 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
960 png_ptr->zstream.next_out = png_ptr->zbuf;
961 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
962}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500963
964void PNGAPI
965png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
966{
967 if (png_ptr && info_ptr)
968 info_ptr->valid &= ~(mask);
969}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600970
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500971
972#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
973/* this function was added to libpng 1.2.0 and should always exist by default */
974void PNGAPI
975png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
976{
977 png_uint_32 settable_asm_flags;
978 png_uint_32 settable_mmx_flags;
979
980 settable_mmx_flags =
981#ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
982 PNG_ASM_FLAG_MMX_READ_COMBINE_ROW |
983#endif
984#ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE
985 PNG_ASM_FLAG_MMX_READ_INTERLACE |
986#endif
987#ifdef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
988 PNG_ASM_FLAG_MMX_READ_FILTER_SUB |
989 PNG_ASM_FLAG_MMX_READ_FILTER_UP |
990 PNG_ASM_FLAG_MMX_READ_FILTER_AVG |
991 PNG_ASM_FLAG_MMX_READ_FILTER_PAETH |
992#endif
993 0;
994
995 /* could be some non-MMX ones in the future, but not currently: */
996 settable_asm_flags = settable_mmx_flags;
997
998 if (!(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED) ||
999 !(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU))
1000 {
1001 /* clear all MMX flags if MMX isn't supported */
1002 settable_asm_flags &= ~settable_mmx_flags;
1003 png_ptr->asm_flags &= ~settable_mmx_flags;
1004 }
1005
1006 /* we're replacing the settable bits with those passed in by the user,
1007 * so first zero them out of the master copy, then logical-OR in the
1008 * allowed subset that was requested */
1009
1010 png_ptr->asm_flags &= ~settable_asm_flags; /* zero them */
1011 png_ptr->asm_flags |= (asm_flags & settable_asm_flags); /* set them */
1012}
1013#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
1014
1015#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
1016/* this function was added to libpng 1.2.0 */
1017void PNGAPI
1018png_set_mmx_thresholds (png_structp png_ptr,
1019 png_byte mmx_bitdepth_threshold,
1020 png_uint_32 mmx_rowbytes_threshold)
1021{
1022 png_ptr->mmx_bitdepth_threshold = mmx_bitdepth_threshold;
1023 png_ptr->mmx_rowbytes_threshold = mmx_rowbytes_threshold;
1024}
1025#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */