]> git.saurik.com Git - wxWidgets.git/blob - src/png/pngset.c
some clean up of the code
[wxWidgets.git] / src / png / pngset.c
1
2 /* pngset.c - storage of image information into info struct
3 *
4 * libpng 1.0.1
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 * Copyright (c) 1998, Glenn Randers-Pehrson
9 * March 15, 1998
10 *
11 * The functions here are used during reads to store data from the file
12 * into the info struct, and during writes to store application data
13 * into the info struct for writing into the file. This abstracts the
14 * info struct and allows us to change the structure in the future.
15 */
16
17 #define PNG_INTERNAL
18 #include "../png/png.h"
19
20 #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
21 void
22 png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
23 {
24 png_debug1(1, "in %s storage function\n", "bKGD");
25 if (png_ptr == NULL || info_ptr == NULL)
26 return;
27
28 png_memcpy(&(info_ptr->background), background, sizeof(png_color_16));
29 info_ptr->valid |= PNG_INFO_bKGD;
30 }
31 #endif
32
33 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
34 void
35 png_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");
40 if (png_ptr == NULL || info_ptr == NULL)
41 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
54
55 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
56 void
57 png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
58 {
59 png_debug1(1, "in %s storage function\n", "gAMA");
60 if (png_ptr == NULL || info_ptr == NULL)
61 return;
62
63 info_ptr->gamma = (float)file_gamma;
64 info_ptr->valid |= PNG_INFO_gAMA;
65 }
66 #endif
67
68 #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
69 void
70 png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
71 {
72 png_debug1(1, "in %s storage function\n", "hIST");
73 if (png_ptr == NULL || info_ptr == NULL)
74 return;
75
76 info_ptr->hist = hist;
77 info_ptr->valid |= PNG_INFO_hIST;
78 }
79 #endif
80
81 void
82 png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
83 png_uint_32 width, png_uint_32 height, int bit_depth,
84 int color_type, int interlace_type, int compression_type,
85 int filter_type)
86 {
87 int rowbytes_per_pixel;
88 png_debug1(1, "in %s storage function\n", "IHDR");
89 if (png_ptr == NULL || info_ptr == NULL)
90 return;
91
92 info_ptr->width = width;
93 info_ptr->height = height;
94 info_ptr->bit_depth = (png_byte)bit_depth;
95 info_ptr->color_type =(png_byte) color_type;
96 info_ptr->compression_type = (png_byte)compression_type;
97 info_ptr->filter_type = (png_byte)filter_type;
98 info_ptr->interlace_type = (png_byte)interlace_type;
99 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
100 info_ptr->channels = 1;
101 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
102 info_ptr->channels = 3;
103 else
104 info_ptr->channels = 1;
105 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
106 info_ptr->channels++;
107 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
108
109 /* check for overflow */
110 rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
111 info_ptr->rowbytes = info_ptr->width * rowbytes_per_pixel;
112 if (( width > (png_uint_32)2147483647L/rowbytes_per_pixel))
113 {
114 png_warning(png_ptr,
115 "Width too large to process image data; rowbytes will overflow.");
116 info_ptr->rowbytes = (png_size_t)0;
117 }
118 }
119
120 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
121 void
122 png_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");
126 if (png_ptr == NULL || info_ptr == NULL)
127 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)
137 void
138 png_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 {
142 png_uint_32 length;
143 int i;
144
145 png_debug1(1, "in %s storage function\n", "pCAL");
146 if (png_ptr == NULL || info_ptr == NULL)
147 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);
152 png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
153
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);
163 png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
164
165 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
166 (png_uint_32)((nparams + 1) * sizeof(png_charp)));
167 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);
174 png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
175 }
176
177 info_ptr->valid |= PNG_INFO_pCAL;
178 }
179 #endif
180
181 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
182 void
183 png_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");
187 if (png_ptr == NULL || info_ptr == NULL)
188 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
197 void
198 png_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");
202 if (png_ptr == NULL || info_ptr == NULL)
203 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)
211 void
212 png_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");
216 if (png_ptr == NULL || info_ptr == NULL)
217 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
224 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
225 void
226 png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
227 {
228 png_debug1(1, "in %s storage function\n", "sRGB");
229 if (png_ptr == NULL || info_ptr == NULL)
230 return;
231
232 info_ptr->srgb_intent = (png_byte)intent;
233 info_ptr->valid |= PNG_INFO_sRGB;
234 }
235 void
236 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
237 int intent)
238 {
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");
246 if (png_ptr == NULL || info_ptr == NULL)
247 return;
248
249 png_set_sRGB(png_ptr, info_ptr, intent);
250
251 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
252 file_gamma = (float).45;
253 png_set_gAMA(png_ptr, info_ptr, file_gamma);
254 #endif
255
256 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
257 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;
265
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)
275 void
276 png_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' ?
282 "text" : (png_const_charp)png_ptr->chunk_name));
283
284 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
285 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,
301 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
302 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
303 sizeof(png_text)));
304 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,
311 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
312 }
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)
322 text_ptr[i].text = (png_charp)"";
323
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)
343 void
344 png_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");
347 if (png_ptr == NULL || info_ptr == NULL)
348 return;
349
350 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
351 info_ptr->valid |= PNG_INFO_tIME;
352 }
353 #endif
354
355 #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
356 void
357 png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
358 png_bytep trans, int num_trans, png_color_16p trans_values)
359 {
360 png_debug1(1, "in %s storage function\n", "tRNS");
361 if (png_ptr == NULL || info_ptr == NULL)
362 return;
363
364 if (trans != NULL)
365 {
366 info_ptr->trans = trans;
367 }
368
369 if (trans_values != NULL)
370 {
371 png_memcpy(&(info_ptr->trans_values), trans_values,
372 sizeof(png_color_16));
373 if (num_trans == 0)
374 num_trans = 1;
375 }
376 info_ptr->num_trans = (png_uint_16)num_trans;
377 info_ptr->valid |= PNG_INFO_tRNS;
378 }
379 #endif
380