1 |
|
---|
2 | /* pngset.c - storage of image information into info struct
|
---|
3 | *
|
---|
4 | * Last changed in libpng 1.2.53 [February 26, 2015]
|
---|
5 | * Copyright (c) 1998-2015 Glenn Randers-Pehrson
|
---|
6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
---|
7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
---|
8 | *
|
---|
9 | * This code is released under the libpng license.
|
---|
10 | * For conditions of distribution and use, see the disclaimer
|
---|
11 | * and license in png.h
|
---|
12 | *
|
---|
13 | * The functions here are used during reads to store data from the file
|
---|
14 | * into the info struct, and during writes to store application data
|
---|
15 | * into the info struct for writing into the file. This abstracts the
|
---|
16 | * info struct and allows us to change the structure in the future.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #define PNG_INTERNAL
|
---|
20 | #define PNG_NO_PEDANTIC_WARNINGS
|
---|
21 | #include "png.h"
|
---|
22 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
|
---|
23 |
|
---|
24 | #ifdef PNG_bKGD_SUPPORTED
|
---|
25 | void PNGAPI
|
---|
26 | png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
|
---|
27 | {
|
---|
28 | png_debug1(1, "in %s storage function", "bKGD");
|
---|
29 |
|
---|
30 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
31 | return;
|
---|
32 |
|
---|
33 | png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
|
---|
34 | info_ptr->valid |= PNG_INFO_bKGD;
|
---|
35 | }
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #ifdef PNG_cHRM_SUPPORTED
|
---|
39 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
40 | void PNGAPI
|
---|
41 | png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
|
---|
42 | double white_x, double white_y, double red_x, double red_y,
|
---|
43 | double green_x, double green_y, double blue_x, double blue_y)
|
---|
44 | {
|
---|
45 | png_debug1(1, "in %s storage function", "cHRM");
|
---|
46 |
|
---|
47 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
48 | return;
|
---|
49 |
|
---|
50 | info_ptr->x_white = (float)white_x;
|
---|
51 | info_ptr->y_white = (float)white_y;
|
---|
52 | info_ptr->x_red = (float)red_x;
|
---|
53 | info_ptr->y_red = (float)red_y;
|
---|
54 | info_ptr->x_green = (float)green_x;
|
---|
55 | info_ptr->y_green = (float)green_y;
|
---|
56 | info_ptr->x_blue = (float)blue_x;
|
---|
57 | info_ptr->y_blue = (float)blue_y;
|
---|
58 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
59 | info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
|
---|
60 | info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
|
---|
61 | info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5);
|
---|
62 | info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5);
|
---|
63 | info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
|
---|
64 | info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
|
---|
65 | info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5);
|
---|
66 | info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5);
|
---|
67 | #endif
|
---|
68 | info_ptr->valid |= PNG_INFO_cHRM;
|
---|
69 | }
|
---|
70 | #endif /* PNG_FLOATING_POINT_SUPPORTED */
|
---|
71 |
|
---|
72 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
73 | void PNGAPI
|
---|
74 | png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
|
---|
75 | png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
|
---|
76 | png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
|
---|
77 | png_fixed_point blue_x, png_fixed_point blue_y)
|
---|
78 | {
|
---|
79 | png_debug1(1, "in %s storage function", "cHRM fixed");
|
---|
80 |
|
---|
81 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
82 | return;
|
---|
83 |
|
---|
84 | #ifdef PNG_CHECK_cHRM_SUPPORTED
|
---|
85 | if (png_check_cHRM_fixed(png_ptr,
|
---|
86 | white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
|
---|
87 | #endif
|
---|
88 | {
|
---|
89 | info_ptr->int_x_white = white_x;
|
---|
90 | info_ptr->int_y_white = white_y;
|
---|
91 | info_ptr->int_x_red = red_x;
|
---|
92 | info_ptr->int_y_red = red_y;
|
---|
93 | info_ptr->int_x_green = green_x;
|
---|
94 | info_ptr->int_y_green = green_y;
|
---|
95 | info_ptr->int_x_blue = blue_x;
|
---|
96 | info_ptr->int_y_blue = blue_y;
|
---|
97 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
98 | info_ptr->x_white = (float)(white_x/100000.);
|
---|
99 | info_ptr->y_white = (float)(white_y/100000.);
|
---|
100 | info_ptr->x_red = (float)( red_x/100000.);
|
---|
101 | info_ptr->y_red = (float)( red_y/100000.);
|
---|
102 | info_ptr->x_green = (float)(green_x/100000.);
|
---|
103 | info_ptr->y_green = (float)(green_y/100000.);
|
---|
104 | info_ptr->x_blue = (float)( blue_x/100000.);
|
---|
105 | info_ptr->y_blue = (float)( blue_y/100000.);
|
---|
106 | #endif
|
---|
107 | info_ptr->valid |= PNG_INFO_cHRM;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | #endif /* PNG_FIXED_POINT_SUPPORTED */
|
---|
111 | #endif /* PNG_cHRM_SUPPORTED */
|
---|
112 |
|
---|
113 | #ifdef PNG_gAMA_SUPPORTED
|
---|
114 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
115 | void PNGAPI
|
---|
116 | png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
|
---|
117 | {
|
---|
118 | double png_gamma;
|
---|
119 |
|
---|
120 | png_debug1(1, "in %s storage function", "gAMA");
|
---|
121 |
|
---|
122 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
123 | return;
|
---|
124 |
|
---|
125 | /* Check for overflow */
|
---|
126 | if (file_gamma > 21474.83)
|
---|
127 | {
|
---|
128 | png_warning(png_ptr, "Limiting gamma to 21474.83");
|
---|
129 | png_gamma=21474.83;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | png_gamma = file_gamma;
|
---|
133 | info_ptr->gamma = (float)png_gamma;
|
---|
134 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
135 | info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
|
---|
136 | #endif
|
---|
137 | info_ptr->valid |= PNG_INFO_gAMA;
|
---|
138 | if (png_gamma == 0.0)
|
---|
139 | png_warning(png_ptr, "Setting gamma=0");
|
---|
140 | }
|
---|
141 | #endif
|
---|
142 | void PNGAPI
|
---|
143 | png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
|
---|
144 | int_gamma)
|
---|
145 | {
|
---|
146 | png_fixed_point png_gamma;
|
---|
147 |
|
---|
148 | png_debug1(1, "in %s storage function", "gAMA");
|
---|
149 |
|
---|
150 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
151 | return;
|
---|
152 |
|
---|
153 | if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
|
---|
154 | {
|
---|
155 | png_warning(png_ptr, "Limiting gamma to 21474.83");
|
---|
156 | png_gamma=PNG_UINT_31_MAX;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | if (int_gamma < 0)
|
---|
161 | {
|
---|
162 | png_warning(png_ptr, "Setting negative gamma to zero");
|
---|
163 | png_gamma = 0;
|
---|
164 | }
|
---|
165 | else
|
---|
166 | png_gamma = int_gamma;
|
---|
167 | }
|
---|
168 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
169 | info_ptr->gamma = (float)(png_gamma/100000.);
|
---|
170 | #endif
|
---|
171 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
172 | info_ptr->int_gamma = png_gamma;
|
---|
173 | #endif
|
---|
174 | info_ptr->valid |= PNG_INFO_gAMA;
|
---|
175 | if (png_gamma == 0)
|
---|
176 | png_warning(png_ptr, "Setting gamma=0");
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | #ifdef PNG_hIST_SUPPORTED
|
---|
181 | void PNGAPI
|
---|
182 | png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
|
---|
183 | {
|
---|
184 | int i;
|
---|
185 |
|
---|
186 | png_debug1(1, "in %s storage function", "hIST");
|
---|
187 |
|
---|
188 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
189 | return;
|
---|
190 |
|
---|
191 | if (info_ptr->num_palette == 0 || info_ptr->num_palette
|
---|
192 | > PNG_MAX_PALETTE_LENGTH)
|
---|
193 | {
|
---|
194 | png_warning(png_ptr,
|
---|
195 | "Invalid palette size, hIST allocation skipped.");
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
200 | png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
|
---|
201 | #endif
|
---|
202 | /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
|
---|
203 | * version 1.2.1
|
---|
204 | */
|
---|
205 | png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
|
---|
206 | (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)));
|
---|
207 | if (png_ptr->hist == NULL)
|
---|
208 | {
|
---|
209 | png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | for (i = 0; i < info_ptr->num_palette; i++)
|
---|
214 | png_ptr->hist[i] = hist[i];
|
---|
215 | info_ptr->hist = png_ptr->hist;
|
---|
216 | info_ptr->valid |= PNG_INFO_hIST;
|
---|
217 |
|
---|
218 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
219 | info_ptr->free_me |= PNG_FREE_HIST;
|
---|
220 | #else
|
---|
221 | png_ptr->flags |= PNG_FLAG_FREE_HIST;
|
---|
222 | #endif
|
---|
223 | }
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | void PNGAPI
|
---|
227 | png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
|
---|
228 | png_uint_32 width, png_uint_32 height, int bit_depth,
|
---|
229 | int color_type, int interlace_type, int compression_type,
|
---|
230 | int filter_type)
|
---|
231 | {
|
---|
232 | png_debug1(1, "in %s storage function", "IHDR");
|
---|
233 |
|
---|
234 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
235 | return;
|
---|
236 |
|
---|
237 | 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 |
|
---|
245 | png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
|
---|
246 | info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
|
---|
247 | info_ptr->compression_type, info_ptr->filter_type);
|
---|
248 |
|
---|
249 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
---|
250 | info_ptr->channels = 1;
|
---|
251 | else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
|
---|
252 | info_ptr->channels = 3;
|
---|
253 | else
|
---|
254 | info_ptr->channels = 1;
|
---|
255 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
|
---|
256 | info_ptr->channels++;
|
---|
257 | info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
|
---|
258 |
|
---|
259 | /* Check for potential overflow */
|
---|
260 | if (width > (PNG_UINT_32_MAX
|
---|
261 | >> 3) /* 8-byte RGBA pixels */
|
---|
262 | - 64 /* bigrowbuf hack */
|
---|
263 | - 1 /* filter byte */
|
---|
264 | - 7*8 /* rounding of width to multiple of 8 pixels */
|
---|
265 | - 8) /* extra max_pixel_depth pad */
|
---|
266 | {
|
---|
267 | info_ptr->rowbytes = (png_size_t)0;
|
---|
268 | png_error(png_ptr, "Image width is too large for this architecture");
|
---|
269 | }
|
---|
270 | else
|
---|
271 | info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
|
---|
272 | }
|
---|
273 |
|
---|
274 | #ifdef PNG_oFFs_SUPPORTED
|
---|
275 | void PNGAPI
|
---|
276 | png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
|
---|
277 | png_int_32 offset_x, png_int_32 offset_y, int unit_type)
|
---|
278 | {
|
---|
279 | png_debug1(1, "in %s storage function", "oFFs");
|
---|
280 |
|
---|
281 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
282 | return;
|
---|
283 |
|
---|
284 | info_ptr->x_offset = offset_x;
|
---|
285 | info_ptr->y_offset = offset_y;
|
---|
286 | info_ptr->offset_unit_type = (png_byte)unit_type;
|
---|
287 | info_ptr->valid |= PNG_INFO_oFFs;
|
---|
288 | }
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | #ifdef PNG_pCAL_SUPPORTED
|
---|
292 | void PNGAPI
|
---|
293 | png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
|
---|
294 | png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
|
---|
295 | png_charp units, png_charpp params)
|
---|
296 | {
|
---|
297 | png_uint_32 length;
|
---|
298 | int i;
|
---|
299 |
|
---|
300 | png_debug1(1, "in %s storage function", "pCAL");
|
---|
301 |
|
---|
302 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
303 | return;
|
---|
304 |
|
---|
305 | length = png_strlen(purpose) + 1;
|
---|
306 | png_debug1(3, "allocating purpose for info (%lu bytes)",
|
---|
307 | (unsigned long)length);
|
---|
308 | info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
309 | if (info_ptr->pcal_purpose == NULL)
|
---|
310 | {
|
---|
311 | png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
|
---|
312 | return;
|
---|
313 | }
|
---|
314 | png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
|
---|
315 |
|
---|
316 | png_debug(3, "storing X0, X1, type, and nparams in info");
|
---|
317 | info_ptr->pcal_X0 = X0;
|
---|
318 | info_ptr->pcal_X1 = X1;
|
---|
319 | info_ptr->pcal_type = (png_byte)type;
|
---|
320 | info_ptr->pcal_nparams = (png_byte)nparams;
|
---|
321 |
|
---|
322 | length = png_strlen(units) + 1;
|
---|
323 | png_debug1(3, "allocating units for info (%lu bytes)",
|
---|
324 | (unsigned long)length);
|
---|
325 | info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
326 | if (info_ptr->pcal_units == NULL)
|
---|
327 | {
|
---|
328 | png_warning(png_ptr, "Insufficient memory for pCAL units.");
|
---|
329 | return;
|
---|
330 | }
|
---|
331 | png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
|
---|
332 |
|
---|
333 | info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
|
---|
334 | (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
|
---|
335 | if (info_ptr->pcal_params == NULL)
|
---|
336 | {
|
---|
337 | png_warning(png_ptr, "Insufficient memory for pCAL params.");
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
|
---|
342 |
|
---|
343 | for (i = 0; i < nparams; i++)
|
---|
344 | {
|
---|
345 | length = png_strlen(params[i]) + 1;
|
---|
346 | png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
|
---|
347 | (unsigned long)length);
|
---|
348 | info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
349 | if (info_ptr->pcal_params[i] == NULL)
|
---|
350 | {
|
---|
351 | png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
|
---|
352 | return;
|
---|
353 | }
|
---|
354 | png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
|
---|
355 | }
|
---|
356 |
|
---|
357 | info_ptr->valid |= PNG_INFO_pCAL;
|
---|
358 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
359 | info_ptr->free_me |= PNG_FREE_PCAL;
|
---|
360 | #endif
|
---|
361 | }
|
---|
362 | #endif
|
---|
363 |
|
---|
364 | #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
|
---|
365 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
366 | void PNGAPI
|
---|
367 | png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
|
---|
368 | int unit, double width, double height)
|
---|
369 | {
|
---|
370 | png_debug1(1, "in %s storage function", "sCAL");
|
---|
371 |
|
---|
372 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
373 | return;
|
---|
374 |
|
---|
375 | info_ptr->scal_unit = (png_byte)unit;
|
---|
376 | info_ptr->scal_pixel_width = width;
|
---|
377 | info_ptr->scal_pixel_height = height;
|
---|
378 |
|
---|
379 | info_ptr->valid |= PNG_INFO_sCAL;
|
---|
380 | }
|
---|
381 | #else
|
---|
382 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
383 | void PNGAPI
|
---|
384 | png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
|
---|
385 | int unit, png_charp swidth, png_charp sheight)
|
---|
386 | {
|
---|
387 | png_uint_32 length;
|
---|
388 |
|
---|
389 | png_debug1(1, "in %s storage function", "sCAL");
|
---|
390 |
|
---|
391 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
392 | return;
|
---|
393 |
|
---|
394 | info_ptr->scal_unit = (png_byte)unit;
|
---|
395 |
|
---|
396 | length = png_strlen(swidth) + 1;
|
---|
397 | png_debug1(3, "allocating unit for info (%u bytes)",
|
---|
398 | (unsigned int)length);
|
---|
399 | info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
400 | if (info_ptr->scal_s_width == NULL)
|
---|
401 | {
|
---|
402 | png_warning(png_ptr,
|
---|
403 | "Memory allocation failed while processing sCAL.");
|
---|
404 | return;
|
---|
405 | }
|
---|
406 | png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
|
---|
407 |
|
---|
408 | length = png_strlen(sheight) + 1;
|
---|
409 | png_debug1(3, "allocating unit for info (%u bytes)",
|
---|
410 | (unsigned int)length);
|
---|
411 | info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
412 | if (info_ptr->scal_s_height == NULL)
|
---|
413 | {
|
---|
414 | png_free (png_ptr, info_ptr->scal_s_width);
|
---|
415 | info_ptr->scal_s_width = NULL;
|
---|
416 | png_warning(png_ptr,
|
---|
417 | "Memory allocation failed while processing sCAL.");
|
---|
418 | return;
|
---|
419 | }
|
---|
420 | png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
|
---|
421 | info_ptr->valid |= PNG_INFO_sCAL;
|
---|
422 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
423 | info_ptr->free_me |= PNG_FREE_SCAL;
|
---|
424 | #endif
|
---|
425 | }
|
---|
426 | #endif
|
---|
427 | #endif
|
---|
428 | #endif
|
---|
429 |
|
---|
430 | #ifdef PNG_pHYs_SUPPORTED
|
---|
431 | void PNGAPI
|
---|
432 | png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
|
---|
433 | png_uint_32 res_x, png_uint_32 res_y, int unit_type)
|
---|
434 | {
|
---|
435 | png_debug1(1, "in %s storage function", "pHYs");
|
---|
436 |
|
---|
437 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
438 | return;
|
---|
439 |
|
---|
440 | info_ptr->x_pixels_per_unit = res_x;
|
---|
441 | info_ptr->y_pixels_per_unit = res_y;
|
---|
442 | info_ptr->phys_unit_type = (png_byte)unit_type;
|
---|
443 | info_ptr->valid |= PNG_INFO_pHYs;
|
---|
444 | }
|
---|
445 | #endif
|
---|
446 |
|
---|
447 | void PNGAPI
|
---|
448 | png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
|
---|
449 | png_colorp palette, int num_palette)
|
---|
450 | {
|
---|
451 |
|
---|
452 | png_debug1(1, "in %s storage function", "PLTE");
|
---|
453 |
|
---|
454 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
455 | return;
|
---|
456 |
|
---|
457 | if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
|
---|
458 | {
|
---|
459 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
---|
460 | png_error(png_ptr, "Invalid palette length");
|
---|
461 | else
|
---|
462 | {
|
---|
463 | png_warning(png_ptr, "Invalid palette length");
|
---|
464 | return;
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | /* It may not actually be necessary to set png_ptr->palette here;
|
---|
469 | * we do it for backward compatibility with the way the png_handle_tRNS
|
---|
470 | * function used to do the allocation.
|
---|
471 | */
|
---|
472 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
473 | png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
|
---|
474 | #endif
|
---|
475 |
|
---|
476 | /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
|
---|
477 | * of num_palette entries, in case of an invalid PNG file that has
|
---|
478 | * too-large sample values.
|
---|
479 | */
|
---|
480 | png_ptr->palette = (png_colorp)png_calloc(png_ptr,
|
---|
481 | PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
|
---|
482 | png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
|
---|
483 | info_ptr->palette = png_ptr->palette;
|
---|
484 | info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
|
---|
485 |
|
---|
486 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
487 | info_ptr->free_me |= PNG_FREE_PLTE;
|
---|
488 | #else
|
---|
489 | png_ptr->flags |= PNG_FLAG_FREE_PLTE;
|
---|
490 | #endif
|
---|
491 |
|
---|
492 | info_ptr->valid |= PNG_INFO_PLTE;
|
---|
493 | }
|
---|
494 |
|
---|
495 | #ifdef PNG_sBIT_SUPPORTED
|
---|
496 | void PNGAPI
|
---|
497 | png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
|
---|
498 | png_color_8p sig_bit)
|
---|
499 | {
|
---|
500 | png_debug1(1, "in %s storage function", "sBIT");
|
---|
501 |
|
---|
502 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
503 | return;
|
---|
504 |
|
---|
505 | png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
|
---|
506 | info_ptr->valid |= PNG_INFO_sBIT;
|
---|
507 | }
|
---|
508 | #endif
|
---|
509 |
|
---|
510 | #ifdef PNG_sRGB_SUPPORTED
|
---|
511 | void PNGAPI
|
---|
512 | png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
|
---|
513 | {
|
---|
514 | png_debug1(1, "in %s storage function", "sRGB");
|
---|
515 |
|
---|
516 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
517 | return;
|
---|
518 |
|
---|
519 | info_ptr->srgb_intent = (png_byte)intent;
|
---|
520 | info_ptr->valid |= PNG_INFO_sRGB;
|
---|
521 | }
|
---|
522 |
|
---|
523 | void PNGAPI
|
---|
524 | png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
|
---|
525 | int intent)
|
---|
526 | {
|
---|
527 | #ifdef PNG_gAMA_SUPPORTED
|
---|
528 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
529 | float file_gamma;
|
---|
530 | #endif
|
---|
531 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
532 | png_fixed_point int_file_gamma;
|
---|
533 | #endif
|
---|
534 | #endif
|
---|
535 | #ifdef PNG_cHRM_SUPPORTED
|
---|
536 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
537 | float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
|
---|
538 | #endif
|
---|
539 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
540 | png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
|
---|
541 | int_green_y, int_blue_x, int_blue_y;
|
---|
542 | #endif
|
---|
543 | #endif
|
---|
544 | png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
|
---|
545 |
|
---|
546 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
547 | return;
|
---|
548 |
|
---|
549 | png_set_sRGB(png_ptr, info_ptr, intent);
|
---|
550 |
|
---|
551 | #ifdef PNG_gAMA_SUPPORTED
|
---|
552 | #ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
553 | file_gamma = (float).45455;
|
---|
554 | png_set_gAMA(png_ptr, info_ptr, file_gamma);
|
---|
555 | #endif
|
---|
556 | #ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
557 | int_file_gamma = 45455L;
|
---|
558 | png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
|
---|
559 | #endif
|
---|
560 | #endif
|
---|
561 |
|
---|
562 | #ifdef PNG_cHRM_SUPPORTED
|
---|
563 | # ifdef PNG_FIXED_POINT_SUPPORTED
|
---|
564 | int_white_x = 31270L;
|
---|
565 | int_white_y = 32900L;
|
---|
566 | int_red_x = 64000L;
|
---|
567 | int_red_y = 33000L;
|
---|
568 | int_green_x = 30000L;
|
---|
569 | int_green_y = 60000L;
|
---|
570 | int_blue_x = 15000L;
|
---|
571 | int_blue_y = 6000L;
|
---|
572 | png_set_cHRM_fixed(png_ptr, info_ptr,
|
---|
573 | int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
|
---|
574 | int_green_y, int_blue_x, int_blue_y);
|
---|
575 | # endif
|
---|
576 |
|
---|
577 | # ifdef PNG_FLOATING_POINT_SUPPORTED
|
---|
578 | white_x = (float).3127;
|
---|
579 | white_y = (float).3290;
|
---|
580 | red_x = (float).64;
|
---|
581 | red_y = (float).33;
|
---|
582 | green_x = (float).30;
|
---|
583 | green_y = (float).60;
|
---|
584 | blue_x = (float).15;
|
---|
585 | blue_y = (float).06;
|
---|
586 | png_set_cHRM(png_ptr, info_ptr,
|
---|
587 | white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
|
---|
588 | # endif
|
---|
589 | #endif /* cHRM */
|
---|
590 | }
|
---|
591 | #endif /* sRGB */
|
---|
592 |
|
---|
593 |
|
---|
594 | #ifdef PNG_iCCP_SUPPORTED
|
---|
595 | void PNGAPI
|
---|
596 | png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
|
---|
597 | png_charp name, int compression_type,
|
---|
598 | png_charp profile, png_uint_32 proflen)
|
---|
599 | {
|
---|
600 | png_charp new_iccp_name;
|
---|
601 | png_charp new_iccp_profile;
|
---|
602 | png_uint_32 length;
|
---|
603 |
|
---|
604 | png_debug1(1, "in %s storage function", "iCCP");
|
---|
605 |
|
---|
606 | if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
|
---|
607 | return;
|
---|
608 |
|
---|
609 | length = png_strlen(name)+1;
|
---|
610 | new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
611 | if (new_iccp_name == NULL)
|
---|
612 | {
|
---|
613 | png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
|
---|
614 | return;
|
---|
615 | }
|
---|
616 | png_memcpy(new_iccp_name, name, length);
|
---|
617 | new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
|
---|
618 | if (new_iccp_profile == NULL)
|
---|
619 | {
|
---|
620 | png_free (png_ptr, new_iccp_name);
|
---|
621 | png_warning(png_ptr,
|
---|
622 | "Insufficient memory to process iCCP profile.");
|
---|
623 | return;
|
---|
624 | }
|
---|
625 | png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
|
---|
626 |
|
---|
627 | png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
|
---|
628 |
|
---|
629 | info_ptr->iccp_proflen = proflen;
|
---|
630 | info_ptr->iccp_name = new_iccp_name;
|
---|
631 | info_ptr->iccp_profile = new_iccp_profile;
|
---|
632 | /* Compression is always zero but is here so the API and info structure
|
---|
633 | * does not have to change if we introduce multiple compression types
|
---|
634 | */
|
---|
635 | info_ptr->iccp_compression = (png_byte)compression_type;
|
---|
636 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
637 | info_ptr->free_me |= PNG_FREE_ICCP;
|
---|
638 | #endif
|
---|
639 | info_ptr->valid |= PNG_INFO_iCCP;
|
---|
640 | }
|
---|
641 | #endif
|
---|
642 |
|
---|
643 | #ifdef PNG_TEXT_SUPPORTED
|
---|
644 | void PNGAPI
|
---|
645 | png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
|
---|
646 | int num_text)
|
---|
647 | {
|
---|
648 | int ret;
|
---|
649 | ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
|
---|
650 | if (ret)
|
---|
651 | png_error(png_ptr, "Insufficient memory to store text");
|
---|
652 | }
|
---|
653 |
|
---|
654 | int /* PRIVATE */
|
---|
655 | png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
|
---|
656 | int num_text)
|
---|
657 | {
|
---|
658 | int i;
|
---|
659 |
|
---|
660 | png_debug1(1, "in %s storage function", ((png_ptr == NULL ||
|
---|
661 | png_ptr->chunk_name[0] == '\0') ?
|
---|
662 | "text" : (png_const_charp)png_ptr->chunk_name));
|
---|
663 |
|
---|
664 | if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
|
---|
665 | return(0);
|
---|
666 |
|
---|
667 | /* Make sure we have enough space in the "text" array in info_struct
|
---|
668 | * to hold all of the incoming text_ptr objects.
|
---|
669 | */
|
---|
670 | if (info_ptr->num_text + num_text > info_ptr->max_text)
|
---|
671 | {
|
---|
672 | int old_max_text = info_ptr->max_text;
|
---|
673 | int old_num_text = info_ptr->num_text;
|
---|
674 |
|
---|
675 | if (info_ptr->text != NULL)
|
---|
676 | {
|
---|
677 | png_textp old_text;
|
---|
678 |
|
---|
679 | info_ptr->max_text = info_ptr->num_text + num_text + 8;
|
---|
680 | old_text = info_ptr->text;
|
---|
681 |
|
---|
682 | info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
|
---|
683 | (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
|
---|
684 | if (info_ptr->text == NULL)
|
---|
685 | {
|
---|
686 | /* Restore to previous condition */
|
---|
687 | info_ptr->max_text = old_max_text;
|
---|
688 | info_ptr->text = old_text;
|
---|
689 | return(1);
|
---|
690 | }
|
---|
691 | png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
|
---|
692 | png_sizeof(png_text)));
|
---|
693 | png_free(png_ptr, old_text);
|
---|
694 | }
|
---|
695 | else
|
---|
696 | {
|
---|
697 | info_ptr->max_text = num_text + 8;
|
---|
698 | info_ptr->num_text = 0;
|
---|
699 | info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
|
---|
700 | (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
|
---|
701 | if (info_ptr->text == NULL)
|
---|
702 | {
|
---|
703 | /* Restore to previous condition */
|
---|
704 | info_ptr->num_text = old_num_text;
|
---|
705 | info_ptr->max_text = old_max_text;
|
---|
706 | return(1);
|
---|
707 | }
|
---|
708 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
709 | info_ptr->free_me |= PNG_FREE_TEXT;
|
---|
710 | #endif
|
---|
711 | }
|
---|
712 | png_debug1(3, "allocated %d entries for info_ptr->text",
|
---|
713 | info_ptr->max_text);
|
---|
714 | }
|
---|
715 |
|
---|
716 | for (i = 0; i < num_text; i++)
|
---|
717 | {
|
---|
718 | png_size_t text_length, key_len;
|
---|
719 | png_size_t lang_len, lang_key_len;
|
---|
720 | png_textp textp = &(info_ptr->text[info_ptr->num_text]);
|
---|
721 |
|
---|
722 | if (text_ptr[i].key == NULL)
|
---|
723 | continue;
|
---|
724 |
|
---|
725 | key_len = png_strlen(text_ptr[i].key);
|
---|
726 |
|
---|
727 | if (text_ptr[i].compression <= 0)
|
---|
728 | {
|
---|
729 | lang_len = 0;
|
---|
730 | lang_key_len = 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | else
|
---|
734 | #ifdef PNG_iTXt_SUPPORTED
|
---|
735 | {
|
---|
736 | /* Set iTXt data */
|
---|
737 |
|
---|
738 | if (text_ptr[i].lang != NULL)
|
---|
739 | lang_len = png_strlen(text_ptr[i].lang);
|
---|
740 | else
|
---|
741 | lang_len = 0;
|
---|
742 | if (text_ptr[i].lang_key != NULL)
|
---|
743 | lang_key_len = png_strlen(text_ptr[i].lang_key);
|
---|
744 | else
|
---|
745 | lang_key_len = 0;
|
---|
746 | }
|
---|
747 | #else /* PNG_iTXt_SUPPORTED */
|
---|
748 | {
|
---|
749 | png_warning(png_ptr, "iTXt chunk not supported.");
|
---|
750 | continue;
|
---|
751 | }
|
---|
752 | #endif
|
---|
753 |
|
---|
754 | if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
|
---|
755 | {
|
---|
756 | text_length = 0;
|
---|
757 | #ifdef PNG_iTXt_SUPPORTED
|
---|
758 | if (text_ptr[i].compression > 0)
|
---|
759 | textp->compression = PNG_ITXT_COMPRESSION_NONE;
|
---|
760 | else
|
---|
761 | #endif
|
---|
762 | textp->compression = PNG_TEXT_COMPRESSION_NONE;
|
---|
763 | }
|
---|
764 |
|
---|
765 | else
|
---|
766 | {
|
---|
767 | text_length = png_strlen(text_ptr[i].text);
|
---|
768 | textp->compression = text_ptr[i].compression;
|
---|
769 | }
|
---|
770 |
|
---|
771 | textp->key = (png_charp)png_malloc_warn(png_ptr,
|
---|
772 | (png_uint_32)
|
---|
773 | (key_len + text_length + lang_len + lang_key_len + 4));
|
---|
774 | if (textp->key == NULL)
|
---|
775 | return(1);
|
---|
776 | png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
|
---|
777 | (png_uint_32)
|
---|
778 | (key_len + lang_len + lang_key_len + text_length + 4),
|
---|
779 | (int)textp->key);
|
---|
780 |
|
---|
781 | png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
|
---|
782 | *(textp->key + key_len) = '\0';
|
---|
783 | #ifdef PNG_iTXt_SUPPORTED
|
---|
784 | if (text_ptr[i].compression > 0)
|
---|
785 | {
|
---|
786 | textp->lang = textp->key + key_len + 1;
|
---|
787 | png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
|
---|
788 | *(textp->lang + lang_len) = '\0';
|
---|
789 | textp->lang_key = textp->lang + lang_len + 1;
|
---|
790 | png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
|
---|
791 | *(textp->lang_key + lang_key_len) = '\0';
|
---|
792 | textp->text = textp->lang_key + lang_key_len + 1;
|
---|
793 | }
|
---|
794 | else
|
---|
795 | #endif
|
---|
796 | {
|
---|
797 | #ifdef PNG_iTXt_SUPPORTED
|
---|
798 | textp->lang=NULL;
|
---|
799 | textp->lang_key=NULL;
|
---|
800 | #endif
|
---|
801 | textp->text = textp->key + key_len + 1;
|
---|
802 | }
|
---|
803 | if (text_length)
|
---|
804 | png_memcpy(textp->text, text_ptr[i].text,
|
---|
805 | (png_size_t)(text_length));
|
---|
806 | *(textp->text + text_length) = '\0';
|
---|
807 |
|
---|
808 | #ifdef PNG_iTXt_SUPPORTED
|
---|
809 | if (textp->compression > 0)
|
---|
810 | {
|
---|
811 | textp->text_length = 0;
|
---|
812 | textp->itxt_length = text_length;
|
---|
813 | }
|
---|
814 | else
|
---|
815 | #endif
|
---|
816 |
|
---|
817 | {
|
---|
818 | textp->text_length = text_length;
|
---|
819 | #ifdef PNG_iTXt_SUPPORTED
|
---|
820 | textp->itxt_length = 0;
|
---|
821 | #endif
|
---|
822 | }
|
---|
823 | info_ptr->num_text++;
|
---|
824 | png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
|
---|
825 | }
|
---|
826 | return(0);
|
---|
827 | }
|
---|
828 | #endif
|
---|
829 |
|
---|
830 | #ifdef PNG_tIME_SUPPORTED
|
---|
831 | void PNGAPI
|
---|
832 | png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
|
---|
833 | {
|
---|
834 | png_debug1(1, "in %s storage function", "tIME");
|
---|
835 |
|
---|
836 | if (png_ptr == NULL || info_ptr == NULL ||
|
---|
837 | (png_ptr->mode & PNG_WROTE_tIME))
|
---|
838 | return;
|
---|
839 |
|
---|
840 | png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
|
---|
841 | info_ptr->valid |= PNG_INFO_tIME;
|
---|
842 | }
|
---|
843 | #endif
|
---|
844 |
|
---|
845 | #ifdef PNG_tRNS_SUPPORTED
|
---|
846 | void PNGAPI
|
---|
847 | png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
|
---|
848 | png_bytep trans, int num_trans, png_color_16p trans_values)
|
---|
849 | {
|
---|
850 | png_debug1(1, "in %s storage function", "tRNS");
|
---|
851 |
|
---|
852 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
853 | return;
|
---|
854 |
|
---|
855 | if (num_trans < 0 || num_trans > PNG_MAX_PALETTE_LENGTH)
|
---|
856 | {
|
---|
857 | png_warning(png_ptr, "Ignoring invalid num_trans value");
|
---|
858 | return;
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (trans != NULL)
|
---|
862 | {
|
---|
863 | /* It may not actually be necessary to set png_ptr->trans here;
|
---|
864 | * we do it for backward compatibility with the way the png_handle_tRNS
|
---|
865 | * function used to do the allocation.
|
---|
866 | */
|
---|
867 |
|
---|
868 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
869 | png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
|
---|
870 | #endif
|
---|
871 |
|
---|
872 | /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
|
---|
873 | png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
|
---|
874 | (png_uint_32)PNG_MAX_PALETTE_LENGTH);
|
---|
875 | if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
|
---|
876 | png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
|
---|
877 | }
|
---|
878 |
|
---|
879 | if (trans_values != NULL)
|
---|
880 | {
|
---|
881 | int sample_max = (1 << info_ptr->bit_depth);
|
---|
882 | if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
|
---|
883 | (int)trans_values->gray > sample_max) ||
|
---|
884 | (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
|
---|
885 | ((int)trans_values->red > sample_max ||
|
---|
886 | (int)trans_values->green > sample_max ||
|
---|
887 | (int)trans_values->blue > sample_max)))
|
---|
888 | png_warning(png_ptr,
|
---|
889 | "tRNS chunk has out-of-range samples for bit_depth");
|
---|
890 | png_memcpy(&(info_ptr->trans_values), trans_values,
|
---|
891 | png_sizeof(png_color_16));
|
---|
892 | if (num_trans == 0)
|
---|
893 | num_trans = 1;
|
---|
894 | }
|
---|
895 |
|
---|
896 | info_ptr->num_trans = (png_uint_16)num_trans;
|
---|
897 | if (num_trans != 0)
|
---|
898 | {
|
---|
899 | info_ptr->valid |= PNG_INFO_tRNS;
|
---|
900 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
901 | info_ptr->free_me |= PNG_FREE_TRNS;
|
---|
902 | #else
|
---|
903 | png_ptr->flags |= PNG_FLAG_FREE_TRNS;
|
---|
904 | #endif
|
---|
905 | }
|
---|
906 | }
|
---|
907 | #endif
|
---|
908 |
|
---|
909 | #ifdef PNG_sPLT_SUPPORTED
|
---|
910 | void PNGAPI
|
---|
911 | png_set_sPLT(png_structp png_ptr,
|
---|
912 | png_infop info_ptr, png_sPLT_tp entries, int nentries)
|
---|
913 | /*
|
---|
914 | * entries - array of png_sPLT_t structures
|
---|
915 | * to be added to the list of palettes
|
---|
916 | * in the info structure.
|
---|
917 | * nentries - number of palette structures to be
|
---|
918 | * added.
|
---|
919 | */
|
---|
920 | {
|
---|
921 | png_sPLT_tp np;
|
---|
922 | int i;
|
---|
923 |
|
---|
924 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
925 | return;
|
---|
926 |
|
---|
927 | np = (png_sPLT_tp)png_malloc_warn(png_ptr,
|
---|
928 | (info_ptr->splt_palettes_num + nentries) *
|
---|
929 | (png_uint_32)png_sizeof(png_sPLT_t));
|
---|
930 | if (np == NULL)
|
---|
931 | {
|
---|
932 | png_warning(png_ptr, "No memory for sPLT palettes.");
|
---|
933 | return;
|
---|
934 | }
|
---|
935 |
|
---|
936 | png_memcpy(np, info_ptr->splt_palettes,
|
---|
937 | info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
|
---|
938 | png_free(png_ptr, info_ptr->splt_palettes);
|
---|
939 | info_ptr->splt_palettes=NULL;
|
---|
940 |
|
---|
941 | for (i = 0; i < nentries; i++)
|
---|
942 | {
|
---|
943 | png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
|
---|
944 | png_sPLT_tp from = entries + i;
|
---|
945 | png_uint_32 length;
|
---|
946 |
|
---|
947 | length = png_strlen(from->name) + 1;
|
---|
948 | to->name = (png_charp)png_malloc_warn(png_ptr, length);
|
---|
949 | if (to->name == NULL)
|
---|
950 | {
|
---|
951 | png_warning(png_ptr,
|
---|
952 | "Out of memory while processing sPLT chunk");
|
---|
953 | continue;
|
---|
954 | }
|
---|
955 | png_memcpy(to->name, from->name, length);
|
---|
956 | to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
|
---|
957 | (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry)));
|
---|
958 | if (to->entries == NULL)
|
---|
959 | {
|
---|
960 | png_warning(png_ptr,
|
---|
961 | "Out of memory while processing sPLT chunk");
|
---|
962 | png_free(png_ptr, to->name);
|
---|
963 | to->name = NULL;
|
---|
964 | continue;
|
---|
965 | }
|
---|
966 | png_memcpy(to->entries, from->entries,
|
---|
967 | from->nentries * png_sizeof(png_sPLT_entry));
|
---|
968 | to->nentries = from->nentries;
|
---|
969 | to->depth = from->depth;
|
---|
970 | }
|
---|
971 |
|
---|
972 | info_ptr->splt_palettes = np;
|
---|
973 | info_ptr->splt_palettes_num += nentries;
|
---|
974 | info_ptr->valid |= PNG_INFO_sPLT;
|
---|
975 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
976 | info_ptr->free_me |= PNG_FREE_SPLT;
|
---|
977 | #endif
|
---|
978 | }
|
---|
979 | #endif /* PNG_sPLT_SUPPORTED */
|
---|
980 |
|
---|
981 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
|
---|
982 | void PNGAPI
|
---|
983 | png_set_unknown_chunks(png_structp png_ptr,
|
---|
984 | png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
|
---|
985 | {
|
---|
986 | png_unknown_chunkp np;
|
---|
987 | int i;
|
---|
988 |
|
---|
989 | if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
|
---|
990 | return;
|
---|
991 |
|
---|
992 | np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
|
---|
993 | (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) *
|
---|
994 | png_sizeof(png_unknown_chunk)));
|
---|
995 | if (np == NULL)
|
---|
996 | {
|
---|
997 | png_warning(png_ptr,
|
---|
998 | "Out of memory while processing unknown chunk.");
|
---|
999 | return;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | png_memcpy(np, info_ptr->unknown_chunks,
|
---|
1003 | info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
|
---|
1004 | png_free(png_ptr, info_ptr->unknown_chunks);
|
---|
1005 | info_ptr->unknown_chunks = NULL;
|
---|
1006 |
|
---|
1007 | for (i = 0; i < num_unknowns; i++)
|
---|
1008 | {
|
---|
1009 | png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
|
---|
1010 | png_unknown_chunkp from = unknowns + i;
|
---|
1011 |
|
---|
1012 | png_memcpy((png_charp)to->name, (png_charp)from->name,
|
---|
1013 | png_sizeof(from->name));
|
---|
1014 | to->name[png_sizeof(to->name)-1] = '\0';
|
---|
1015 | to->size = from->size;
|
---|
1016 | /* Note our location in the read or write sequence */
|
---|
1017 | to->location = (png_byte)(png_ptr->mode & 0xff);
|
---|
1018 |
|
---|
1019 | if (from->size == 0)
|
---|
1020 | to->data=NULL;
|
---|
1021 | else
|
---|
1022 | {
|
---|
1023 | to->data = (png_bytep)png_malloc_warn(png_ptr,
|
---|
1024 | (png_uint_32)from->size);
|
---|
1025 | if (to->data == NULL)
|
---|
1026 | {
|
---|
1027 | png_warning(png_ptr,
|
---|
1028 | "Out of memory while processing unknown chunk.");
|
---|
1029 | to->size = 0;
|
---|
1030 | }
|
---|
1031 | else
|
---|
1032 | png_memcpy(to->data, from->data, from->size);
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | info_ptr->unknown_chunks = np;
|
---|
1037 | info_ptr->unknown_chunks_num += num_unknowns;
|
---|
1038 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
1039 | info_ptr->free_me |= PNG_FREE_UNKN;
|
---|
1040 | #endif
|
---|
1041 | }
|
---|
1042 | void PNGAPI
|
---|
1043 | png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
|
---|
1044 | int chunk, int location)
|
---|
1045 | {
|
---|
1046 | if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
|
---|
1047 | (int)info_ptr->unknown_chunks_num)
|
---|
1048 | info_ptr->unknown_chunks[chunk].location = (png_byte)location;
|
---|
1049 | }
|
---|
1050 | #endif
|
---|
1051 |
|
---|
1052 | #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
|
---|
1053 | #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
|
---|
1054 | defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
|
---|
1055 | void PNGAPI
|
---|
1056 | png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
|
---|
1057 | {
|
---|
1058 | /* This function is deprecated in favor of png_permit_mng_features()
|
---|
1059 | and will be removed from libpng-1.3.0 */
|
---|
1060 |
|
---|
1061 | png_debug(1, "in png_permit_empty_plte, DEPRECATED.");
|
---|
1062 |
|
---|
1063 | if (png_ptr == NULL)
|
---|
1064 | return;
|
---|
1065 | png_ptr->mng_features_permitted = (png_byte)
|
---|
1066 | ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
|
---|
1067 | ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
|
---|
1068 | }
|
---|
1069 | #endif
|
---|
1070 | #endif
|
---|
1071 |
|
---|
1072 | #ifdef PNG_MNG_FEATURES_SUPPORTED
|
---|
1073 | png_uint_32 PNGAPI
|
---|
1074 | png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
|
---|
1075 | {
|
---|
1076 | png_debug(1, "in png_permit_mng_features");
|
---|
1077 |
|
---|
1078 | if (png_ptr == NULL)
|
---|
1079 | return (png_uint_32)0;
|
---|
1080 | png_ptr->mng_features_permitted =
|
---|
1081 | (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
|
---|
1082 | return (png_uint_32)png_ptr->mng_features_permitted;
|
---|
1083 | }
|
---|
1084 | #endif
|
---|
1085 |
|
---|
1086 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
---|
1087 | void PNGAPI
|
---|
1088 | png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
|
---|
1089 | chunk_list, int num_chunks)
|
---|
1090 | {
|
---|
1091 | png_bytep new_list, p;
|
---|
1092 | int i, old_num_chunks;
|
---|
1093 | if (png_ptr == NULL)
|
---|
1094 | return;
|
---|
1095 | if (num_chunks == 0)
|
---|
1096 | {
|
---|
1097 | if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
|
---|
1098 | png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
|
---|
1099 | else
|
---|
1100 | png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
|
---|
1101 |
|
---|
1102 | if (keep == PNG_HANDLE_CHUNK_ALWAYS)
|
---|
1103 | png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
|
---|
1104 | else
|
---|
1105 | png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
|
---|
1106 | return;
|
---|
1107 | }
|
---|
1108 | if (chunk_list == NULL)
|
---|
1109 | return;
|
---|
1110 | old_num_chunks = png_ptr->num_chunk_list;
|
---|
1111 | new_list=(png_bytep)png_malloc(png_ptr,
|
---|
1112 | (png_uint_32)
|
---|
1113 | (5*(num_chunks + old_num_chunks)));
|
---|
1114 | if (png_ptr->chunk_list != NULL)
|
---|
1115 | {
|
---|
1116 | png_memcpy(new_list, png_ptr->chunk_list,
|
---|
1117 | (png_size_t)(5*old_num_chunks));
|
---|
1118 | png_free(png_ptr, png_ptr->chunk_list);
|
---|
1119 | png_ptr->chunk_list=NULL;
|
---|
1120 | }
|
---|
1121 | png_memcpy(new_list + 5*old_num_chunks, chunk_list,
|
---|
1122 | (png_size_t)(5*num_chunks));
|
---|
1123 | for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
|
---|
1124 | *p=(png_byte)keep;
|
---|
1125 | png_ptr->num_chunk_list = old_num_chunks + num_chunks;
|
---|
1126 | png_ptr->chunk_list = new_list;
|
---|
1127 | #ifdef PNG_FREE_ME_SUPPORTED
|
---|
1128 | png_ptr->free_me |= PNG_FREE_LIST;
|
---|
1129 | #endif
|
---|
1130 | }
|
---|
1131 | #endif
|
---|
1132 |
|
---|
1133 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
1134 | void PNGAPI
|
---|
1135 | png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
|
---|
1136 | png_user_chunk_ptr read_user_chunk_fn)
|
---|
1137 | {
|
---|
1138 | png_debug(1, "in png_set_read_user_chunk_fn");
|
---|
1139 |
|
---|
1140 | if (png_ptr == NULL)
|
---|
1141 | return;
|
---|
1142 |
|
---|
1143 | png_ptr->read_user_chunk_fn = read_user_chunk_fn;
|
---|
1144 | png_ptr->user_chunk_ptr = user_chunk_ptr;
|
---|
1145 | }
|
---|
1146 | #endif
|
---|
1147 |
|
---|
1148 | #ifdef PNG_INFO_IMAGE_SUPPORTED
|
---|
1149 | void PNGAPI
|
---|
1150 | png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
|
---|
1151 | {
|
---|
1152 | png_debug1(1, "in %s storage function", "rows");
|
---|
1153 |
|
---|
1154 | if (png_ptr == NULL || info_ptr == NULL)
|
---|
1155 | return;
|
---|
1156 |
|
---|
1157 | if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
|
---|
1158 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
|
---|
1159 | info_ptr->row_pointers = row_pointers;
|
---|
1160 | if (row_pointers)
|
---|
1161 | info_ptr->valid |= PNG_INFO_IDAT;
|
---|
1162 | }
|
---|
1163 | #endif
|
---|
1164 |
|
---|
1165 | void PNGAPI
|
---|
1166 | png_set_compression_buffer_size(png_structp png_ptr,
|
---|
1167 | png_uint_32 size)
|
---|
1168 | {
|
---|
1169 | if (png_ptr == NULL)
|
---|
1170 | return;
|
---|
1171 | png_free(png_ptr, png_ptr->zbuf);
|
---|
1172 | png_ptr->zbuf_size = (png_size_t)size;
|
---|
1173 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
|
---|
1174 | png_ptr->zstream.next_out = png_ptr->zbuf;
|
---|
1175 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | void PNGAPI
|
---|
1179 | png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
|
---|
1180 | {
|
---|
1181 | if (png_ptr && info_ptr)
|
---|
1182 | info_ptr->valid &= ~mask;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 |
|
---|
1186 | #ifndef PNG_1_0_X
|
---|
1187 | #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
|
---|
1188 | /* Function was added to libpng 1.2.0 and should always exist by default */
|
---|
1189 | void PNGAPI
|
---|
1190 | png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
|
---|
1191 | {
|
---|
1192 | /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
|
---|
1193 | if (png_ptr != NULL)
|
---|
1194 | png_ptr->asm_flags = 0;
|
---|
1195 | PNG_UNUSED(asm_flags) /* Quiet the compiler */
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /* This function was added to libpng 1.2.0 */
|
---|
1199 | void PNGAPI
|
---|
1200 | png_set_mmx_thresholds (png_structp png_ptr,
|
---|
1201 | png_byte mmx_bitdepth_threshold,
|
---|
1202 | png_uint_32 mmx_rowbytes_threshold)
|
---|
1203 | {
|
---|
1204 | /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
|
---|
1205 | if (png_ptr == NULL)
|
---|
1206 | return;
|
---|
1207 | /* Quiet the compiler */
|
---|
1208 | PNG_UNUSED(mmx_bitdepth_threshold)
|
---|
1209 | PNG_UNUSED(mmx_rowbytes_threshold)
|
---|
1210 | }
|
---|
1211 | #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
|
---|
1212 |
|
---|
1213 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
---|
1214 | /* This function was added to libpng 1.2.6 */
|
---|
1215 | void PNGAPI
|
---|
1216 | png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
|
---|
1217 | png_uint_32 user_height_max)
|
---|
1218 | {
|
---|
1219 | /* Images with dimensions larger than these limits will be
|
---|
1220 | * rejected by png_set_IHDR(). To accept any PNG datastream
|
---|
1221 | * regardless of dimensions, set both limits to 0x7ffffffL.
|
---|
1222 | */
|
---|
1223 | if (png_ptr == NULL)
|
---|
1224 | return;
|
---|
1225 | png_ptr->user_width_max = user_width_max;
|
---|
1226 | png_ptr->user_height_max = user_height_max;
|
---|
1227 | }
|
---|
1228 | #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
|
---|
1229 |
|
---|
1230 |
|
---|
1231 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED
|
---|
1232 | void PNGAPI
|
---|
1233 | png_set_benign_errors(png_structp png_ptr, int allowed)
|
---|
1234 | {
|
---|
1235 | png_debug(1, "in png_set_benign_errors");
|
---|
1236 |
|
---|
1237 | if (allowed)
|
---|
1238 | png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
|
---|
1239 | else
|
---|
1240 | png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
|
---|
1241 | }
|
---|
1242 | #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
|
---|
1243 | #endif /* ?PNG_1_0_X */
|
---|
1244 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
|
---|