blob: 2855207b210b57b5f8285e736d5518f577714682 [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 *
4 * libpng 1.00.97
5 * 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
8 * May 28, 1997
9 *
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");
24 if (info_ptr == NULL)
25 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");
39 if (info_ptr == NULL)
40 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");
59 if (info_ptr == NULL)
60 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");
72 if (info_ptr == NULL)
73 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{
86 png_debug1(1, "in %s storage function\n", "IHDR");
87 if (info_ptr == NULL)
88 return;
89
90 info_ptr->width = width;
91 info_ptr->height = height;
92 info_ptr->bit_depth = (png_byte)bit_depth;
93 info_ptr->color_type =(png_byte) color_type;
94 info_ptr->compression_type = (png_byte)compression_type;
95 info_ptr->filter_type = (png_byte)filter_type;
96 info_ptr->interlace_type = (png_byte)interlace_type;
97 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
98 info_ptr->channels = 1;
99 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
100 info_ptr->channels = 3;
101 else
102 info_ptr->channels = 1;
103 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
104 info_ptr->channels++;
105 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
106 info_ptr->rowbytes = ((info_ptr->width * info_ptr->pixel_depth + 7) >> 3);
107}
108
109#if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
110void
111png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
112 png_uint_32 offset_x, png_uint_32 offset_y, int unit_type)
113{
114 png_debug1(1, "in %s storage function\n", "oFFs");
115 if (info_ptr == NULL)
116 return;
117
118 info_ptr->x_offset = offset_x;
119 info_ptr->y_offset = offset_y;
120 info_ptr->offset_unit_type = (png_byte)unit_type;
121 info_ptr->valid |= PNG_INFO_oFFs;
122}
123#endif
124
125#if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED)
126void
127png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
128 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
129 png_charp units, png_charpp params)
130{
131 png_size_t length;
132 int i;
133
134 png_debug1(1, "in %s storage function\n", "pCAL");
135 if (info_ptr == NULL)
136 return;
137
138 length = png_strlen(purpose) + 1;
139 png_debug1(3, "allocating purpose for info (%d bytes)\n", length);
140 info_ptr->pcal_purpose = (png_charp)png_malloc(png_ptr, length);
141 png_memcpy(info_ptr->pcal_purpose, purpose, length);
142
143 png_debug(3, "storing X0, X1, type, and nparams in info\n");
144 info_ptr->pcal_X0 = X0;
145 info_ptr->pcal_X1 = X1;
146 info_ptr->pcal_type = (png_byte)type;
147 info_ptr->pcal_nparams = (png_byte)nparams;
148
149 length = png_strlen(units) + 1;
150 png_debug1(3, "allocating units for info (%d bytes)\n", length);
151 info_ptr->pcal_units = (png_charp)png_malloc(png_ptr, length);
152 png_memcpy(info_ptr->pcal_units, units, length);
153
154 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
155 (nparams + 1) * sizeof(png_charp));
156 info_ptr->pcal_params[nparams] = NULL;
157
158 for (i = 0; i < nparams; i++)
159 {
160 length = png_strlen(params[i]) + 1;
161 png_debug2(3, "allocating parameter %d for info (%d bytes)\n", i, length);
162 info_ptr->pcal_params[i] = (png_charp)png_malloc(png_ptr, length);
163 png_memcpy(info_ptr->pcal_params[i], params[i], length);
164 }
165
166 info_ptr->valid |= PNG_INFO_pCAL;
167}
168#endif
169
170#if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
171void
172png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
173 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
174{
175 png_debug1(1, "in %s storage function\n", "pHYs");
176 if (info_ptr == NULL)
177 return;
178
179 info_ptr->x_pixels_per_unit = res_x;
180 info_ptr->y_pixels_per_unit = res_y;
181 info_ptr->phys_unit_type = (png_byte)unit_type;
182 info_ptr->valid |= PNG_INFO_pHYs;
183}
184#endif
185
186void
187png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
188 png_colorp palette, int num_palette)
189{
190 png_debug1(1, "in %s storage function\n", "PLTE");
191 if (info_ptr == NULL)
192 return;
193
194 info_ptr->palette = palette;
195 info_ptr->num_palette = (png_uint_16)num_palette;
196 info_ptr->valid |= PNG_INFO_PLTE;
197}
198
199#if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
200void
201png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
202 png_color_8p sig_bit)
203{
204 png_debug1(1, "in %s storage function\n", "sBIT");
205 if (info_ptr == NULL)
206 return;
207
208 png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
209 info_ptr->valid |= PNG_INFO_sBIT;
210}
211#endif
212
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600213#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
214void
215png_set_sRGB(png_structp png_ptr, png_infop info_ptr, png_byte intent)
216{
217 png_debug1(1, "in %s storage function\n", "sRGB");
218 if (info_ptr == NULL)
219 return;
220
221 info_ptr->srgb_intent = intent;
222 info_ptr->valid |= PNG_INFO_sRGB;
223}
224void
225png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
226 png_byte intent)
227{
228#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
229 float file_gamma;
230#endif
231#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
232 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
233#endif
234 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
235 if (info_ptr == NULL)
236 return;
237
238 png_set_sRGB(png_ptr, info_ptr, intent);
239
240#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
241 file_gamma = 0.45;
242 png_set_gAMA(png_ptr, info_ptr, file_gamma);
243#endif
244
245#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
246 white_x = 0.3127;
247 white_y = 0.3290;
248 red_x = 0.6400;
249 red_y = 0.3300;
250 green_x = 0.3000;
251 green_y = 0.6000;
252 blue_x = 0.1500;
253 blue_y = 0.0600;
254
255 png_set_cHRM(png_ptr, info_ptr,
256 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
257
258#endif
259}
260#endif
261
262#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
263 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264void
265png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
266 int num_text)
267{
268 int i;
269
270 png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
271 "text" : png_ptr->chunk_name));
272
273 if (info_ptr == NULL || num_text == 0)
274 return;
275
276 /* Make sure we have enough space in the "text" array in info_struct
277 * to hold all of the incoming text_ptr objects.
278 */
279 if (info_ptr->num_text + num_text > info_ptr->max_text)
280 {
281 if (info_ptr->text != NULL)
282 {
283 png_textp old_text;
284 int old_max;
285
286 old_max = info_ptr->max_text;
287 info_ptr->max_text = info_ptr->num_text + num_text + 8;
288 old_text = info_ptr->text;
289 info_ptr->text = (png_textp)png_malloc(png_ptr,
290 info_ptr->max_text * sizeof (png_text));
291 png_memcpy(info_ptr->text, old_text, old_max * sizeof(png_text));
292 png_free(png_ptr, old_text);
293 }
294 else
295 {
296 info_ptr->max_text = num_text + 8;
297 info_ptr->num_text = 0;
298 info_ptr->text = (png_textp)png_malloc(png_ptr,
299 info_ptr->max_text * sizeof (png_text));
300 }
301 png_debug1(3, "allocated %d entries for info_ptr->text\n",
302 info_ptr->max_text);
303 }
304
305 for (i = 0; i < num_text; i++)
306 {
307 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
308
309 if (text_ptr[i].text == NULL)
310 text_ptr[i].text = "";
311
312 if (text_ptr[i].text[0] == '\0')
313 {
314 textp->text_length = 0;
315 textp->compression = PNG_TEXT_COMPRESSION_NONE;
316 }
317 else
318 {
319 textp->text_length = png_strlen(text_ptr[i].text);
320 textp->compression = text_ptr[i].compression;
321 }
322 textp->text = text_ptr[i].text;
323 textp->key = text_ptr[i].key;
324 info_ptr->num_text++;
325 png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
326 }
327}
328#endif
329
330#if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
331void
332png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
333{
334 png_debug1(1, "in %s storage function\n", "tIME");
335 if (info_ptr == NULL)
336 return;
337
338 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
339 info_ptr->valid |= PNG_INFO_tIME;
340}
341#endif
342
343#if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
344void
345png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
346 png_bytep trans, int num_trans, png_color_16p trans_values)
347{
348 png_debug1(1, "in %s storage function\n", "tRNS");
349 if (info_ptr == NULL)
350 return;
351
352 if (trans != NULL)
353 {
354 info_ptr->trans = trans;
355 }
356
357 if (trans_values != NULL)
358 {
359 png_memcpy(&(info_ptr->trans_values), trans_values,
360 sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600361 if (num_trans == 0)
362 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 }
364 info_ptr->num_trans = (png_uint_16)num_trans;
365 info_ptr->valid |= PNG_INFO_tRNS;
366}
367#endif
368