blob: 07fc581fc268004cf4cc9b67ae7135b9b8183b46 [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-Pehrson4393a9a1999-09-17 12:27:26 -05004 * libpng 1.0.4 - September 17, 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
19#if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
20void
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
32#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
33void
34png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
35 double white_x, double white_y, double red_x, double red_y,
36 double green_x, double green_y, double blue_x, double blue_y)
37{
38 png_debug1(1, "in %s storage function\n", "cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060039 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050040 return;
41
42 info_ptr->x_white = (float)white_x;
43 info_ptr->y_white = (float)white_y;
44 info_ptr->x_red = (float)red_x;
45 info_ptr->y_red = (float)red_y;
46 info_ptr->x_green = (float)green_x;
47 info_ptr->y_green = (float)green_y;
48 info_ptr->x_blue = (float)blue_x;
49 info_ptr->y_blue = (float)blue_y;
50 info_ptr->valid |= PNG_INFO_cHRM;
51}
52#endif
53
54#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
55void
56png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
57{
58 png_debug1(1, "in %s storage function\n", "gAMA");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060059 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050060 return;
61
62 info_ptr->gamma = (float)file_gamma;
63 info_ptr->valid |= PNG_INFO_gAMA;
64}
65#endif
66
67#if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
68void
69png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
70{
71 png_debug1(1, "in %s storage function\n", "hIST");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060072 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050073 return;
74
75 info_ptr->hist = hist;
76 info_ptr->valid |= PNG_INFO_hIST;
77}
78#endif
79
80void
81png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
82 png_uint_32 width, png_uint_32 height, int bit_depth,
83 int color_type, int interlace_type, int compression_type,
84 int filter_type)
85{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060086 int rowbytes_per_pixel;
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_debug1(1, "in %s storage function\n", "IHDR");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060088 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050089 return;
90
91 info_ptr->width = width;
92 info_ptr->height = height;
93 info_ptr->bit_depth = (png_byte)bit_depth;
94 info_ptr->color_type =(png_byte) color_type;
95 info_ptr->compression_type = (png_byte)compression_type;
96 info_ptr->filter_type = (png_byte)filter_type;
97 info_ptr->interlace_type = (png_byte)interlace_type;
98 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
99 info_ptr->channels = 1;
100 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
101 info_ptr->channels = 3;
102 else
103 info_ptr->channels = 1;
104 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
105 info_ptr->channels++;
106 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600107
108 /* check for overflow */
109 rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500110 if (( width > PNG_MAX_UINT/rowbytes_per_pixel))
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600111 {
112 png_warning(png_ptr,
113 "Width too large to process image data; rowbytes will overflow.");
114 info_ptr->rowbytes = (png_size_t)0;
115 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500116 else
117 info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118}
119
120#if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
121void
122png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
123 png_uint_32 offset_x, png_uint_32 offset_y, int unit_type)
124{
125 png_debug1(1, "in %s storage function\n", "oFFs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600126 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500127 return;
128
129 info_ptr->x_offset = offset_x;
130 info_ptr->y_offset = offset_y;
131 info_ptr->offset_unit_type = (png_byte)unit_type;
132 info_ptr->valid |= PNG_INFO_oFFs;
133}
134#endif
135
136#if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED)
137void
138png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
139 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
140 png_charp units, png_charpp params)
141{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600142 png_uint_32 length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600143 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144
145 png_debug1(1, "in %s storage function\n", "pCAL");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600146 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 return;
148
149 length = png_strlen(purpose) + 1;
150 png_debug1(3, "allocating purpose for info (%d bytes)\n", length);
151 info_ptr->pcal_purpose = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600152 png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500153
154 png_debug(3, "storing X0, X1, type, and nparams in info\n");
155 info_ptr->pcal_X0 = X0;
156 info_ptr->pcal_X1 = X1;
157 info_ptr->pcal_type = (png_byte)type;
158 info_ptr->pcal_nparams = (png_byte)nparams;
159
160 length = png_strlen(units) + 1;
161 png_debug1(3, "allocating units for info (%d bytes)\n", length);
162 info_ptr->pcal_units = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600163 png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164
165 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600166 (png_uint_32)((nparams + 1) * sizeof(png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 info_ptr->pcal_params[nparams] = NULL;
168
169 for (i = 0; i < nparams; i++)
170 {
171 length = png_strlen(params[i]) + 1;
172 png_debug2(3, "allocating parameter %d for info (%d bytes)\n", i, length);
173 info_ptr->pcal_params[i] = (png_charp)png_malloc(png_ptr, length);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600174 png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 }
176
177 info_ptr->valid |= PNG_INFO_pCAL;
178}
179#endif
180
181#if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
182void
183png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
184 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
185{
186 png_debug1(1, "in %s storage function\n", "pHYs");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600187 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 return;
189
190 info_ptr->x_pixels_per_unit = res_x;
191 info_ptr->y_pixels_per_unit = res_y;
192 info_ptr->phys_unit_type = (png_byte)unit_type;
193 info_ptr->valid |= PNG_INFO_pHYs;
194}
195#endif
196
197void
198png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
199 png_colorp palette, int num_palette)
200{
201 png_debug1(1, "in %s storage function\n", "PLTE");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600202 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500203 return;
204
205 info_ptr->palette = palette;
206 info_ptr->num_palette = (png_uint_16)num_palette;
207 info_ptr->valid |= PNG_INFO_PLTE;
208}
209
210#if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
211void
212png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
213 png_color_8p sig_bit)
214{
215 png_debug1(1, "in %s storage function\n", "sBIT");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600216 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 return;
218
219 png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
220 info_ptr->valid |= PNG_INFO_sBIT;
221}
222#endif
223
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600224#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
225void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600226png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600227{
228 png_debug1(1, "in %s storage function\n", "sRGB");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600229 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600230 return;
231
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600232 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600233 info_ptr->valid |= PNG_INFO_sRGB;
234}
235void
236png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600237 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600238{
239#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
240 float file_gamma;
241#endif
242#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
243 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
244#endif
245 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600246 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600247 return;
248
249 png_set_sRGB(png_ptr, info_ptr, intent);
250
251#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600252 file_gamma = (float).45;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600253 png_set_gAMA(png_ptr, info_ptr, file_gamma);
254#endif
255
256#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600257 white_x = (float).3127;
258 white_y = (float).3290;
259 red_x = (float).64;
260 red_y = (float).33;
261 green_x = (float).30;
262 green_y = (float).60;
263 blue_x = (float).15;
264 blue_y = (float).06;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600265
266 png_set_cHRM(png_ptr, info_ptr,
267 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
268
269#endif
270}
271#endif
272
273#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
274 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275void
276png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
277 int num_text)
278{
279 int i;
280
281 png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600282 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600284 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500285 return;
286
287 /* Make sure we have enough space in the "text" array in info_struct
288 * to hold all of the incoming text_ptr objects.
289 */
290 if (info_ptr->num_text + num_text > info_ptr->max_text)
291 {
292 if (info_ptr->text != NULL)
293 {
294 png_textp old_text;
295 int old_max;
296
297 old_max = info_ptr->max_text;
298 info_ptr->max_text = info_ptr->num_text + num_text + 8;
299 old_text = info_ptr->text;
300 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600301 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600302 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
303 sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 png_free(png_ptr, old_text);
305 }
306 else
307 {
308 info_ptr->max_text = num_text + 8;
309 info_ptr->num_text = 0;
310 info_ptr->text = (png_textp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600311 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 }
313 png_debug1(3, "allocated %d entries for info_ptr->text\n",
314 info_ptr->max_text);
315 }
316
317 for (i = 0; i < num_text; i++)
318 {
319 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
320
321 if (text_ptr[i].text == NULL)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600322 text_ptr[i].text = (png_charp)"";
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323
324 if (text_ptr[i].text[0] == '\0')
325 {
326 textp->text_length = 0;
327 textp->compression = PNG_TEXT_COMPRESSION_NONE;
328 }
329 else
330 {
331 textp->text_length = png_strlen(text_ptr[i].text);
332 textp->compression = text_ptr[i].compression;
333 }
334 textp->text = text_ptr[i].text;
335 textp->key = text_ptr[i].key;
336 info_ptr->num_text++;
337 png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
338 }
339}
340#endif
341
342#if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
343void
344png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
345{
346 png_debug1(1, "in %s storage function\n", "tIME");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500347 if (png_ptr == NULL || info_ptr == NULL ||
348 (png_ptr->flags & PNG_FLAG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 return;
350
351 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
352 info_ptr->valid |= PNG_INFO_tIME;
353}
354#endif
355
356#if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
357void
358png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
359 png_bytep trans, int num_trans, png_color_16p trans_values)
360{
361 png_debug1(1, "in %s storage function\n", "tRNS");
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600362 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 return;
364
365 if (trans != NULL)
366 {
367 info_ptr->trans = trans;
368 }
369
370 if (trans_values != NULL)
371 {
372 png_memcpy(&(info_ptr->trans_values), trans_values,
373 sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600374 if (num_trans == 0)
375 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 }
377 info_ptr->num_trans = (png_uint_16)num_trans;
378 info_ptr->valid |= PNG_INFO_tRNS;
379}
380#endif
381
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500382#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED)
383void
384png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
385{
386 png_debug1(1, "in png_permit_empty_plte\n", "");
387 if (png_ptr == NULL)
388 return;
389 png_ptr->empty_plte_permitted=(png_byte)empty_plte_permitted;
390}
391#endif