blob: 6e7420a40dada7445881034846b94e380c92f48b [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-Pehrson166c5a31999-12-10 09:43:02 -06004 * libpng 1.0.5h - December 10, 1999
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
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)
Andreas Dilger47a0c421997-05-16 02:46:07 -050020void
21png_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
Andreas Dilger47a0c421997-05-16 02:46:07 -050034void
35png_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;
51 info_ptr->valid |= PNG_INFO_cHRM;
52}
53#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060054#ifdef PNG_FIXED_POINT_SUPPORTED
55void
56png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
57 png_uint_32 white_x, png_uint_32 white_y, png_uint_32 red_x,
58 png_uint_32 red_y, png_uint_32 green_x, png_uint_32 green_y,
59 png_uint_32 blue_x, png_uint_32 blue_y)
60{
61 png_debug1(1, "in %s storage function\n", "cHRM");
62 if (png_ptr == NULL || info_ptr == NULL)
63 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050064
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060065 info_ptr->int_x_white = white_x;
66 info_ptr->int_y_white = white_y;
67 info_ptr->int_x_red = red_x;
68 info_ptr->int_y_red = red_y;
69 info_ptr->int_x_green = green_x;
70 info_ptr->int_y_green = green_y;
71 info_ptr->int_x_blue = blue_x;
72 info_ptr->int_y_blue = blue_y;
73 info_ptr->valid |= PNG_INFO_cHRM;
74}
75#endif
76#endif
77
78#if defined(PNG_gAMA_SUPPORTED)
79#ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050080void
81png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
82{
83 png_debug1(1, "in %s storage function\n", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060084 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050085 return;
86
87 info_ptr->gamma = (float)file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060088#ifdef PNG_FIXED_POINT_SUPPORTED
89 info_ptr->int_gamma = (int)(file_gamma*100000.+.5);
90#endif
91 info_ptr->valid |= PNG_INFO_gAMA;
92}
93#endif
94#endif
95#ifdef PNG_FIXED_POINT_SUPPORTED
96void
97png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_uint_32 int_gamma)
98{
99 png_debug1(1, "in %s storage function\n", "gAMA");
100 if (png_ptr == NULL || info_ptr == NULL)
101 return;
102
103#ifdef PNG_FLOATING_POINT_SUPPORTED
104 info_ptr->gamma = (float)int_gamma/100000.;
105#endif
106 info_ptr->int_gamma = int_gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500107 info_ptr->valid |= PNG_INFO_gAMA;
108}
109#endif
110
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600111#if defined(PNG_hIST_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112void
113png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
114{
115 png_debug1(1, "in %s storage function\n", "hIST");
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
119 info_ptr->hist = hist;
120 info_ptr->valid |= PNG_INFO_hIST;
121}
122#endif
123
124void
125png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
126 png_uint_32 width, png_uint_32 height, int bit_depth,
127 int color_type, int interlace_type, int compression_type,
128 int filter_type)
129{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600130 int rowbytes_per_pixel;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500131 png_debug1(1, "in %s storage function\n", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600132 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 return;
134
135 info_ptr->width = width;
136 info_ptr->height = height;
137 info_ptr->bit_depth = (png_byte)bit_depth;
138 info_ptr->color_type =(png_byte) color_type;
139 info_ptr->compression_type = (png_byte)compression_type;
140 info_ptr->filter_type = (png_byte)filter_type;
141 info_ptr->interlace_type = (png_byte)interlace_type;
142 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
143 info_ptr->channels = 1;
144 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
145 info_ptr->channels = 3;
146 else
147 info_ptr->channels = 1;
148 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
149 info_ptr->channels++;
150 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600151
152 /* check for overflow */
153 rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500154 if (( width > PNG_MAX_UINT/rowbytes_per_pixel))
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600155 {
156 png_warning(png_ptr,
157 "Width too large to process image data; rowbytes will overflow.");
158 info_ptr->rowbytes = (png_size_t)0;
159 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500160 else
161 info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162}
163
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600164#if defined(PNG_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500165void
166png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600167 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168{
169 png_debug1(1, "in %s storage function\n", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600170 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 return;
172
173 info_ptr->x_offset = offset_x;
174 info_ptr->y_offset = offset_y;
175 info_ptr->offset_unit_type = (png_byte)unit_type;
176 info_ptr->valid |= PNG_INFO_oFFs;
177}
178#endif
179
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600180#if defined(PNG_pCAL_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181void
182png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
183 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
184 png_charp units, png_charpp params)
185{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600186 png_uint_32 length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600187 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188
189 png_debug1(1, "in %s storage function\n", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600190 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 return;
192
193 length = png_strlen(purpose) + 1;
194 png_debug1(3, "allocating purpose for info (%d bytes)\n", length);
195 info_ptr->pcal_purpose = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600196 png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197
198 png_debug(3, "storing X0, X1, type, and nparams in info\n");
199 info_ptr->pcal_X0 = X0;
200 info_ptr->pcal_X1 = X1;
201 info_ptr->pcal_type = (png_byte)type;
202 info_ptr->pcal_nparams = (png_byte)nparams;
203
204 length = png_strlen(units) + 1;
205 png_debug1(3, "allocating units for info (%d bytes)\n", length);
206 info_ptr->pcal_units = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600207 png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208
209 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600210 (png_uint_32)((nparams + 1) * sizeof(png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 info_ptr->pcal_params[nparams] = NULL;
212
213 for (i = 0; i < nparams; i++)
214 {
215 length = png_strlen(params[i]) + 1;
216 png_debug2(3, "allocating parameter %d for info (%d bytes)\n", i, length);
217 info_ptr->pcal_params[i] = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600218 png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500219 }
220
221 info_ptr->valid |= PNG_INFO_pCAL;
222}
223#endif
224
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
226#ifdef PNG_FLOATING_POINT_SUPPORTED
227void
228png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
229 png_charp unit, double width, double height)
230{
231 png_uint_32 length;
232
233 png_debug1(1, "in %s storage function\n", "sCAL");
234 if (png_ptr == NULL || info_ptr == NULL)
235 return;
236
237 length = png_strlen(unit) + 1;
238 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
239 info_ptr->scal_unit = (png_charp)png_malloc(png_ptr, length);
240 png_memcpy(info_ptr->scal_unit, unit, (png_size_t)length);
241 info_ptr->scal_pixel_width = width;
242 info_ptr->scal_pixel_height = height;
243
244 info_ptr->valid |= PNG_INFO_sCAL;
245}
246#endif
247void
248png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
249 png_charp unit, png_charp swidth, png_charp sheight)
250{
251 png_uint_32 length;
252
253 png_debug1(1, "in %s storage function\n", "sCAL");
254 if (png_ptr == NULL || info_ptr == NULL)
255 return;
256
257 length = png_strlen(unit) + 1;
258 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
259 info_ptr->scal_unit = (png_charp)png_malloc(png_ptr, length);
260 png_memcpy(info_ptr->scal_unit, unit, (png_size_t)length);
261
262 length = png_strlen(swidth) + 1;
263 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
264 info_ptr->scal_s_width = (png_charp)png_malloc(png_ptr, length);
265 png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
266
267 length = png_strlen(sheight) + 1;
268 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
269 info_ptr->scal_s_width = (png_charp)png_malloc(png_ptr, length);
270 png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
271
272 info_ptr->valid |= PNG_INFO_sCAL;
273}
274#endif
275
276#if defined(PNG_pHYs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277void
278png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
279 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
280{
281 png_debug1(1, "in %s storage function\n", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600282 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283 return;
284
285 info_ptr->x_pixels_per_unit = res_x;
286 info_ptr->y_pixels_per_unit = res_y;
287 info_ptr->phys_unit_type = (png_byte)unit_type;
288 info_ptr->valid |= PNG_INFO_pHYs;
289}
290#endif
291
292void
293png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
294 png_colorp palette, int num_palette)
295{
296 png_debug1(1, "in %s storage function\n", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600297 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298 return;
299
300 info_ptr->palette = palette;
301 info_ptr->num_palette = (png_uint_16)num_palette;
302 info_ptr->valid |= PNG_INFO_PLTE;
303}
304
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600305#if defined(PNG_sBIT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306void
307png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
308 png_color_8p sig_bit)
309{
310 png_debug1(1, "in %s storage function\n", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600311 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 return;
313
314 png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
315 info_ptr->valid |= PNG_INFO_sBIT;
316}
317#endif
318
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600319#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600320void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600321png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600322{
323 png_debug1(1, "in %s storage function\n", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600324 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600325 return;
326
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600327 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600328 info_ptr->valid |= PNG_INFO_sRGB;
329}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600330
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600331void
332png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600333 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600334{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600335#if defined(PNG_gAMA_SUPPORTED)
336#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600337 float file_gamma;
338#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600339#ifdef PNG_FIXED_POINT_SUPPORTED
340 png_uint_32 int_file_gamma;
341#endif
342#endif
343#if defined(PNG_cHRM_SUPPORTED)
344#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600345 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
346#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347#ifdef PNG_FIXED_POINT_SUPPORTED
348 png_uint_32 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
349 int_green_y, int_blue_x, int_blue_y;
350#endif
351#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600352 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600353 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600354 return;
355
356 png_set_sRGB(png_ptr, info_ptr, intent);
357
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600358#if defined(PNG_gAMA_SUPPORTED)
359#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500360 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600361 png_set_gAMA(png_ptr, info_ptr, file_gamma);
362#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363#ifdef PNG_FIXED_POINT_SUPPORTED
364 int_file_gamma = 45455L;
365 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
366#endif
367#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600368
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369#if defined(PNG_cHRM_SUPPORTED)
370#ifdef PNG_FIXED_POINT_SUPPORTED
371 int_white_x = 31270L;
372 int_white_y = 32900L;
373 int_red_x = 64000L;
374 int_red_y = 33000L;
375 int_green_x = 30000L;
376 int_green_y = 60000L;
377 int_blue_x = 15000L;
378 int_blue_y = 6000L;
379
380 png_set_cHRM_fixed(png_ptr, info_ptr,
381 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
382 int_blue_x, int_blue_y);
383#endif
384#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600385 white_x = (float).3127;
386 white_y = (float).3290;
387 red_x = (float).64;
388 red_y = (float).33;
389 green_x = (float).30;
390 green_y = (float).60;
391 blue_x = (float).15;
392 blue_y = (float).06;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600393
394 png_set_cHRM(png_ptr, info_ptr,
395 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600397#endif
398}
399#endif
400
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401
402#if defined(PNG_iCCP_SUPPORTED)
403void
404png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
405 png_charp name, int compression_type,
406 png_charp profile, int proflen)
407{
408 png_debug1(1, "in %s storage function\n", "iCCP");
409 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
410 return;
411
412 info_ptr->iccp_name = png_malloc(png_ptr, png_strlen(name)+1);
413 strcpy(info_ptr->iccp_name, name);
414 info_ptr->iccp_profile = png_malloc(png_ptr, proflen);
415 memcpy(info_ptr->iccp_profile, profile, proflen);
416 info_ptr->iccp_proflen = (png_uint_32)proflen;
417 /* Compression is always zero but is here so the API and info structure
418 * does not have to change * if we introduce multiple compression types */
419 info_ptr->iccp_compression = (png_byte)compression_type;
420 info_ptr->valid |= PNG_INFO_iCCP;
421}
422#endif
423
424#if defined(PNG_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425void
426png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
427 int num_text)
428{
429 int i;
430
431 png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600432 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600434 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 return;
436
437 /* Make sure we have enough space in the "text" array in info_struct
438 * to hold all of the incoming text_ptr objects.
439 */
440 if (info_ptr->num_text + num_text > info_ptr->max_text)
441 {
442 if (info_ptr->text != NULL)
443 {
444 png_textp old_text;
445 int old_max;
446
447 old_max = info_ptr->max_text;
448 info_ptr->max_text = info_ptr->num_text + num_text + 8;
449 old_text = info_ptr->text;
450 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600451 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600452 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
453 sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 png_free(png_ptr, old_text);
455 }
456 else
457 {
458 info_ptr->max_text = num_text + 8;
459 info_ptr->num_text = 0;
460 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600461 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500462 }
463 png_debug1(3, "allocated %d entries for info_ptr->text\n",
464 info_ptr->max_text);
465 }
466
467 for (i = 0; i < num_text; i++)
468 {
469 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500470 png_charp key,text;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500472 if (text_ptr[i].key == (png_charp)NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600473 continue;
474
475#ifdef PNG_iTXt_SUPPORTED
476 textp->lang = text_ptr[i].lang;
477 textp->translated_key = text_ptr[i].translated_key;
478#else
479 textp->lang = NULL;
480 textp->translated_key = NULL;
481#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500482
483 if (text_ptr[i].text[0] == '\0')
484 {
485 textp->text_length = 0;
486 textp->compression = PNG_TEXT_COMPRESSION_NONE;
487 }
488 else
489 {
490 textp->text_length = png_strlen(text_ptr[i].text);
491 textp->compression = text_ptr[i].compression;
492 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500493 key=text_ptr[i].key;
494 for (text = key; *text++;)
495 /* empty loop to find the byte after the zero byte after the
496 end of key */ ;
497
498 textp->key = (png_charp)png_malloc(png_ptr,
499 (png_uint_32)(text+textp->text_length - key)+1);
500 /* Caution: the calling program, not libpng, is responsible for
501 freeing this, if libpng wasn't the caller. */
502 png_debug2(2, "Allocated %d bytes at %x in png_set_text\n",
503 text+textp->text_length-key+1, textp->key);
504
505 png_memcpy(textp->key, text_ptr[i].key,
506 (png_size_t)(text - key)); /* includes the zero-byte separator */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600507
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500508 textp->text = textp->key + (text-key);
509 if(textp->text_length)
510 {
511 png_memcpy(textp->text, text_ptr[i].text,
512 (png_size_t)(textp->text_length));
513 *(textp->text+textp->text_length) = '\0';
514 }
515 else
516 textp->text--;
517 info_ptr->text[info_ptr->num_text]= *textp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 info_ptr->num_text++;
519 png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
520 }
521}
522#endif
523
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600524#if defined(PNG_tIME_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525void
526png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
527{
528 png_debug1(1, "in %s storage function\n", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500529 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600530 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531 return;
532
533 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
534 info_ptr->valid |= PNG_INFO_tIME;
535}
536#endif
537
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600538#if defined(PNG_tRNS_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539void
540png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
541 png_bytep trans, int num_trans, png_color_16p trans_values)
542{
543 png_debug1(1, "in %s storage function\n", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600544 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545 return;
546
547 if (trans != NULL)
548 {
549 info_ptr->trans = trans;
550 }
551
552 if (trans_values != NULL)
553 {
554 png_memcpy(&(info_ptr->trans_values), trans_values,
555 sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600556 if (num_trans == 0)
557 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 }
559 info_ptr->num_trans = (png_uint_16)num_trans;
560 info_ptr->valid |= PNG_INFO_tRNS;
561}
562#endif
563
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600564#if defined(PNG_sPLT_SUPPORTED)
565void
566png_set_spalettes(png_structp png_ptr,
567 png_infop info_ptr, png_spalette_p entries, int nentries)
568{
569 png_spalette_p np;
570 int i;
571
572 np = (png_spalette_p)png_malloc(png_ptr,
573 (info_ptr->splt_palettes_num + nentries) * sizeof(png_spalette));
574
575 memcpy(np, info_ptr->splt_palettes,
576 info_ptr->splt_palettes_num * sizeof(png_spalette));
577 png_free(png_ptr, info_ptr->splt_palettes);
578
579 for (i = 0; i < nentries; i++)
580 {
581 png_spalette_p to = np + info_ptr->splt_palettes_num + i;
582 png_spalette_p from = entries + i;
583
584 to->name = (png_charp)png_malloc(png_ptr,
585 png_strlen(from->name) + 1);
586 png_strcpy(to->name, from->name);
587 to->entries = (png_spalette_entryp)png_malloc(png_ptr,
588 from->nentries * sizeof(png_spalette));
589 memcpy(to->entries, from->entries,
590 from->nentries * sizeof(png_spalette));
591 }
592
593 info_ptr->splt_palettes = np;
594 info_ptr->splt_palettes_num += nentries;
595}
596#endif /* PNG_sPLT_SUPPORTED */
597
598#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
599void
600png_set_unknown_chunks(png_structp png_ptr,
601 png_infop info_ptr, png_unknown_chunkp unknowns, int nunknowns)
602{
603 png_unknown_chunkp np;
604 int i;
605
606 np = (png_unknown_chunkp)png_malloc(png_ptr,
607 (info_ptr->unknown_chunks_num + nunknowns) * sizeof(png_unknown_chunk));
608
609 memcpy(np, info_ptr->unknown_chunks,
610 info_ptr->unknown_chunks_num * sizeof(png_unknown_chunk));
611 png_free(png_ptr, info_ptr->unknown_chunks);
612
613 for (i = 0; i < nunknowns; i++)
614 {
615 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
616 png_unknown_chunkp from = unknowns + i;
617
618 png_strcpy(to->name, from->name);
619 to->data = (png_bytep)png_malloc(png_ptr, from->size);
620 memcpy(to->data, from->data, from->size);
621 to->size = from->size;
622
623 /* note our location in the read or write sequence */
624 to->location = png_ptr->mode;
625 }
626
627 info_ptr->unknown_chunks = np;
628 info_ptr->unknown_chunks_num += nunknowns;
629}
630#endif
631
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500632#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED)
633void
634png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
635{
636 png_debug1(1, "in png_permit_empty_plte\n", "");
637 if (png_ptr == NULL)
638 return;
639 png_ptr->empty_plte_permitted=(png_byte)empty_plte_permitted;
640}
641#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600642
643
644
645
646
647
648