2 /* pngset.c - storage of image information into info struct
4 * libpng 1.2.0 - September 1, 2001
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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.
19 #if defined(PNG_bKGD_SUPPORTED)
21 png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
23 png_debug1(1, "in %s storage function\n", "bKGD");
24 if (png_ptr == NULL || info_ptr == NULL)
27 png_memcpy(&(info_ptr->background), background, sizeof(png_color_16));
28 info_ptr->valid |= PNG_INFO_bKGD;
32 #if defined(PNG_cHRM_SUPPORTED)
33 #ifdef PNG_FLOATING_POINT_SUPPORTED
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)
39 png_debug1(1, "in %s storage function\n", "cHRM");
40 if (png_ptr == NULL || info_ptr == NULL)
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 #ifdef PNG_FIXED_POINT_SUPPORTED
52 info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
53 info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
54 info_ptr->int_x_red = (png_fixed_point)(red_x*100000.+0.5);
55 info_ptr->int_y_red = (png_fixed_point)(red_y*100000.+0.5);
56 info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
57 info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
58 info_ptr->int_x_blue = (png_fixed_point)(blue_x*100000.+0.5);
59 info_ptr->int_y_blue = (png_fixed_point)(blue_y*100000.+0.5);
61 info_ptr->valid |= PNG_INFO_cHRM;
64 #ifdef PNG_FIXED_POINT_SUPPORTED
66 png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
67 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
68 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
69 png_fixed_point blue_x, png_fixed_point blue_y)
71 png_debug1(1, "in %s storage function\n", "cHRM");
72 if (png_ptr == NULL || info_ptr == NULL)
75 info_ptr->int_x_white = white_x;
76 info_ptr->int_y_white = white_y;
77 info_ptr->int_x_red = red_x;
78 info_ptr->int_y_red = red_y;
79 info_ptr->int_x_green = green_x;
80 info_ptr->int_y_green = green_y;
81 info_ptr->int_x_blue = blue_x;
82 info_ptr->int_y_blue = blue_y;
83 #ifdef PNG_FLOATING_POINT_SUPPORTED
84 info_ptr->x_white = (float)(white_x/100000.);
85 info_ptr->y_white = (float)(white_y/100000.);
86 info_ptr->x_red = (float)(red_x/100000.);
87 info_ptr->y_red = (float)(red_y/100000.);
88 info_ptr->x_green = (float)(green_x/100000.);
89 info_ptr->y_green = (float)(green_y/100000.);
90 info_ptr->x_blue = (float)(blue_x/100000.);
91 info_ptr->y_blue = (float)(blue_y/100000.);
93 info_ptr->valid |= PNG_INFO_cHRM;
98 #if defined(PNG_gAMA_SUPPORTED)
99 #ifdef PNG_FLOATING_POINT_SUPPORTED
101 png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
103 png_debug1(1, "in %s storage function\n", "gAMA");
104 if (png_ptr == NULL || info_ptr == NULL)
107 info_ptr->gamma = (float)file_gamma;
108 #ifdef PNG_FIXED_POINT_SUPPORTED
109 info_ptr->int_gamma = (int)(file_gamma*100000.+.5);
111 info_ptr->valid |= PNG_INFO_gAMA;
112 if(file_gamma == 0.0)
113 png_warning(png_ptr, "Setting gamma=0");
117 png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
120 png_debug1(1, "in %s storage function\n", "gAMA");
121 if (png_ptr == NULL || info_ptr == NULL)
124 #ifdef PNG_FLOATING_POINT_SUPPORTED
125 info_ptr->gamma = (float)(int_gamma/100000.);
127 #ifdef PNG_FIXED_POINT_SUPPORTED
128 info_ptr->int_gamma = int_gamma;
130 info_ptr->valid |= PNG_INFO_gAMA;
132 png_warning(png_ptr, "Setting gamma=0");
136 #if defined(PNG_hIST_SUPPORTED)
138 png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
142 png_debug1(1, "in %s storage function\n", "hIST");
143 if (png_ptr == NULL || info_ptr == NULL)
145 if (info_ptr->num_palette == 0)
148 "Palette size 0, hIST allocation skipped.");
152 #ifdef PNG_FREE_ME_SUPPORTED
153 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
155 png_ptr->hist = (png_uint_16p)png_malloc(png_ptr,
156 (png_uint_32)(info_ptr->num_palette * sizeof (png_uint_16)));
158 for (i = 0; i < info_ptr->num_palette; i++)
159 png_ptr->hist[i] = hist[i];
160 info_ptr->hist = png_ptr->hist;
161 info_ptr->valid |= PNG_INFO_hIST;
163 #ifdef PNG_FREE_ME_SUPPORTED
164 info_ptr->free_me |= PNG_FREE_HIST;
166 png_ptr->flags |= PNG_FLAG_FREE_HIST;
172 png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
173 png_uint_32 width, png_uint_32 height, int bit_depth,
174 int color_type, int interlace_type, int compression_type,
177 int rowbytes_per_pixel;
178 png_debug1(1, "in %s storage function\n", "IHDR");
179 if (png_ptr == NULL || info_ptr == NULL)
182 /* check for width and height valid values */
183 if (width == 0 || height == 0)
184 png_error(png_ptr, "Image width or height is zero in IHDR");
185 if (width > PNG_MAX_UINT || height > PNG_MAX_UINT)
186 png_error(png_ptr, "Invalid image size in IHDR");
188 /* check other values */
189 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
190 bit_depth != 8 && bit_depth != 16)
191 png_error(png_ptr, "Invalid bit depth in IHDR");
193 if (color_type < 0 || color_type == 1 ||
194 color_type == 5 || color_type > 6)
195 png_error(png_ptr, "Invalid color type in IHDR");
197 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
198 ((color_type == PNG_COLOR_TYPE_RGB ||
199 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
200 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
201 png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
203 if (interlace_type >= PNG_INTERLACE_LAST)
204 png_error(png_ptr, "Unknown interlace method in IHDR");
206 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
207 png_error(png_ptr, "Unknown compression method in IHDR");
209 #if defined(PNG_MNG_FEATURES_SUPPORTED)
210 /* Accept filter_method 64 (intrapixel differencing) only if
211 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
212 * 2. Libpng did not read a PNG signature (this filter_method is only
213 * used in PNG datastreams that are embedded in MNG datastreams) and
214 * 3. The application called png_permit_mng_features with a mask that
215 * included PNG_FLAG_MNG_FILTER_64 and
216 * 4. The filter_method is 64 and
217 * 5. The color_type is RGB or RGBA
219 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
220 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
221 if(filter_type != PNG_FILTER_TYPE_BASE)
223 if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
224 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
225 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
226 (color_type == PNG_COLOR_TYPE_RGB ||
227 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
228 png_error(png_ptr, "Unknown filter method in IHDR");
229 if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
230 png_warning(png_ptr, "Invalid filter method in IHDR");
233 if(filter_type != PNG_FILTER_TYPE_BASE)
234 png_error(png_ptr, "Unknown filter method in IHDR");
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 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
245 info_ptr->channels = 1;
246 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
247 info_ptr->channels = 3;
249 info_ptr->channels = 1;
250 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
251 info_ptr->channels++;
252 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
254 /* check for overflow */
255 rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
256 if (( width > PNG_MAX_UINT/rowbytes_per_pixel))
259 "Width too large to process image data; rowbytes will overflow.");
260 info_ptr->rowbytes = (png_size_t)0;
263 info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3;
266 #if defined(PNG_oFFs_SUPPORTED)
268 png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
269 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
271 png_debug1(1, "in %s storage function\n", "oFFs");
272 if (png_ptr == NULL || info_ptr == NULL)
275 info_ptr->x_offset = offset_x;
276 info_ptr->y_offset = offset_y;
277 info_ptr->offset_unit_type = (png_byte)unit_type;
278 info_ptr->valid |= PNG_INFO_oFFs;
282 #if defined(PNG_pCAL_SUPPORTED)
284 png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
285 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
286 png_charp units, png_charpp params)
291 png_debug1(1, "in %s storage function\n", "pCAL");
292 if (png_ptr == NULL || info_ptr == NULL)
295 length = png_strlen(purpose) + 1;
296 png_debug1(3, "allocating purpose for info (%lu bytes)\n", length);
297 info_ptr->pcal_purpose = (png_charp)png_malloc(png_ptr, length);
298 png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
300 png_debug(3, "storing X0, X1, type, and nparams in info\n");
301 info_ptr->pcal_X0 = X0;
302 info_ptr->pcal_X1 = X1;
303 info_ptr->pcal_type = (png_byte)type;
304 info_ptr->pcal_nparams = (png_byte)nparams;
306 length = png_strlen(units) + 1;
307 png_debug1(3, "allocating units for info (%lu bytes)\n", length);
308 info_ptr->pcal_units = (png_charp)png_malloc(png_ptr, length);
309 png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
311 info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
312 (png_uint_32)((nparams + 1) * sizeof(png_charp)));
314 info_ptr->pcal_params[nparams] = NULL;
316 for (i = 0; i < nparams; i++)
318 length = png_strlen(params[i]) + 1;
319 png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length);
320 info_ptr->pcal_params[i] = (png_charp)png_malloc(png_ptr, length);
321 png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
324 info_ptr->valid |= PNG_INFO_pCAL;
325 #ifdef PNG_FREE_ME_SUPPORTED
326 info_ptr->free_me |= PNG_FREE_PCAL;
331 #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
332 #ifdef PNG_FLOATING_POINT_SUPPORTED
334 png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
335 int unit, double width, double height)
337 png_debug1(1, "in %s storage function\n", "sCAL");
338 if (png_ptr == NULL || info_ptr == NULL)
341 info_ptr->scal_unit = (png_byte)unit;
342 info_ptr->scal_pixel_width = width;
343 info_ptr->scal_pixel_height = height;
345 info_ptr->valid |= PNG_INFO_sCAL;
348 #ifdef PNG_FIXED_POINT_SUPPORTED
350 png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
351 int unit, png_charp swidth, png_charp sheight)
355 png_debug1(1, "in %s storage function\n", "sCAL");
356 if (png_ptr == NULL || info_ptr == NULL)
359 info_ptr->scal_unit = (png_byte)unit;
361 length = png_strlen(swidth) + 1;
362 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
363 info_ptr->scal_s_width = (png_charp)png_malloc(png_ptr, length);
364 png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
366 length = png_strlen(sheight) + 1;
367 png_debug1(3, "allocating unit for info (%d bytes)\n", length);
368 info_ptr->scal_s_height = (png_charp)png_malloc(png_ptr, length);
369 png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
371 info_ptr->valid |= PNG_INFO_sCAL;
372 #ifdef PNG_FREE_ME_SUPPORTED
373 info_ptr->free_me |= PNG_FREE_SCAL;
380 #if defined(PNG_pHYs_SUPPORTED)
382 png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
383 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
385 png_debug1(1, "in %s storage function\n", "pHYs");
386 if (png_ptr == NULL || info_ptr == NULL)
389 info_ptr->x_pixels_per_unit = res_x;
390 info_ptr->y_pixels_per_unit = res_y;
391 info_ptr->phys_unit_type = (png_byte)unit_type;
392 info_ptr->valid |= PNG_INFO_pHYs;
397 png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
398 png_colorp palette, int num_palette)
401 png_debug1(1, "in %s storage function\n", "PLTE");
402 if (png_ptr == NULL || info_ptr == NULL)
406 * It may not actually be necessary to set png_ptr->palette here;
407 * we do it for backward compatibility with the way the png_handle_tRNS
408 * function used to do the allocation.
410 #ifdef PNG_FREE_ME_SUPPORTED
411 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
413 png_ptr->palette = (png_colorp)png_zalloc(png_ptr, (uInt)num_palette,
415 png_memcpy(png_ptr->palette, palette, num_palette * sizeof (png_color));
416 info_ptr->palette = png_ptr->palette;
417 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
419 #ifdef PNG_FREE_ME_SUPPORTED
420 info_ptr->free_me |= PNG_FREE_PLTE;
422 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
425 info_ptr->valid |= PNG_INFO_PLTE;
428 #if defined(PNG_sBIT_SUPPORTED)
430 png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
431 png_color_8p sig_bit)
433 png_debug1(1, "in %s storage function\n", "sBIT");
434 if (png_ptr == NULL || info_ptr == NULL)
437 png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
438 info_ptr->valid |= PNG_INFO_sBIT;
442 #if defined(PNG_sRGB_SUPPORTED)
444 png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
446 png_debug1(1, "in %s storage function\n", "sRGB");
447 if (png_ptr == NULL || info_ptr == NULL)
450 info_ptr->srgb_intent = (png_byte)intent;
451 info_ptr->valid |= PNG_INFO_sRGB;
455 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
458 #if defined(PNG_gAMA_SUPPORTED)
459 #ifdef PNG_FLOATING_POINT_SUPPORTED
462 #ifdef PNG_FIXED_POINT_SUPPORTED
463 png_fixed_point int_file_gamma;
466 #if defined(PNG_cHRM_SUPPORTED)
467 #ifdef PNG_FLOATING_POINT_SUPPORTED
468 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
470 #ifdef PNG_FIXED_POINT_SUPPORTED
471 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
472 int_green_y, int_blue_x, int_blue_y;
475 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
476 if (png_ptr == NULL || info_ptr == NULL)
479 png_set_sRGB(png_ptr, info_ptr, intent);
481 #if defined(PNG_gAMA_SUPPORTED)
482 #ifdef PNG_FLOATING_POINT_SUPPORTED
483 file_gamma = (float).45455;
484 png_set_gAMA(png_ptr, info_ptr, file_gamma);
486 #ifdef PNG_FIXED_POINT_SUPPORTED
487 int_file_gamma = 45455L;
488 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
492 #if defined(PNG_cHRM_SUPPORTED)
493 #ifdef PNG_FIXED_POINT_SUPPORTED
494 int_white_x = 31270L;
495 int_white_y = 32900L;
498 int_green_x = 30000L;
499 int_green_y = 60000L;
503 png_set_cHRM_fixed(png_ptr, info_ptr,
504 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
505 int_blue_x, int_blue_y);
507 #ifdef PNG_FLOATING_POINT_SUPPORTED
508 white_x = (float).3127;
509 white_y = (float).3290;
512 green_x = (float).30;
513 green_y = (float).60;
517 png_set_cHRM(png_ptr, info_ptr,
518 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
525 #if defined(PNG_iCCP_SUPPORTED)
527 png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
528 png_charp name, int compression_type,
529 png_charp profile, png_uint_32 proflen)
531 png_charp new_iccp_name;
532 png_charp new_iccp_profile;
534 png_debug1(1, "in %s storage function\n", "iCCP");
535 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
538 new_iccp_name = (png_charp)png_malloc(png_ptr, png_strlen(name)+1);
539 png_strcpy(new_iccp_name, name);
540 new_iccp_profile = (png_charp)png_malloc(png_ptr, proflen);
541 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
543 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
545 info_ptr->iccp_proflen = proflen;
546 info_ptr->iccp_name = new_iccp_name;
547 info_ptr->iccp_profile = new_iccp_profile;
548 /* Compression is always zero but is here so the API and info structure
549 * does not have to change if we introduce multiple compression types */
550 info_ptr->iccp_compression = (png_byte)compression_type;
551 #ifdef PNG_FREE_ME_SUPPORTED
552 info_ptr->free_me |= PNG_FREE_ICCP;
554 info_ptr->valid |= PNG_INFO_iCCP;
558 #if defined(PNG_TEXT_SUPPORTED)
560 png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
565 png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
566 "text" : (png_const_charp)png_ptr->chunk_name));
568 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
571 /* Make sure we have enough space in the "text" array in info_struct
572 * to hold all of the incoming text_ptr objects.
574 if (info_ptr->num_text + num_text > info_ptr->max_text)
576 if (info_ptr->text != NULL)
581 old_max = info_ptr->max_text;
582 info_ptr->max_text = info_ptr->num_text + num_text + 8;
583 old_text = info_ptr->text;
584 info_ptr->text = (png_textp)png_malloc(png_ptr,
585 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
586 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
588 png_free(png_ptr, old_text);
592 info_ptr->max_text = num_text + 8;
593 info_ptr->num_text = 0;
594 info_ptr->text = (png_textp)png_malloc(png_ptr,
595 (png_uint_32)(info_ptr->max_text * sizeof (png_text)));
596 #ifdef PNG_FREE_ME_SUPPORTED
597 info_ptr->free_me |= PNG_FREE_TEXT;
600 png_debug1(3, "allocated %d entries for info_ptr->text\n",
603 for (i = 0; i < num_text; i++)
605 png_size_t text_length,key_len;
606 png_size_t lang_len,lang_key_len;
607 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
609 if (text_ptr[i].key == NULL)
612 key_len = png_strlen(text_ptr[i].key);
614 if(text_ptr[i].compression <= 0)
620 #ifdef PNG_iTXt_SUPPORTED
623 if (text_ptr[i].key != NULL)
624 lang_len = png_strlen(text_ptr[i].lang);
627 if (text_ptr[i].lang_key != NULL)
628 lang_key_len = png_strlen(text_ptr[i].lang_key);
634 png_warning(png_ptr, "iTXt chunk not supported.");
639 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
642 #ifdef PNG_iTXt_SUPPORTED
643 if(text_ptr[i].compression > 0)
644 textp->compression = PNG_ITXT_COMPRESSION_NONE;
647 textp->compression = PNG_TEXT_COMPRESSION_NONE;
651 text_length = png_strlen(text_ptr[i].text);
652 textp->compression = text_ptr[i].compression;
655 textp->key = (png_charp)png_malloc(png_ptr,
656 (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4));
657 png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
658 (png_uint_32)(key_len + lang_len + lang_key_len + text_length + 4),
661 png_memcpy(textp->key, text_ptr[i].key,
662 (png_size_t)(key_len));
663 *(textp->key+key_len) = '\0';
664 #ifdef PNG_iTXt_SUPPORTED
665 if (text_ptr[i].compression > 0)
667 textp->lang=textp->key + key_len + 1;
668 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
669 *(textp->lang+lang_len) = '\0';
670 textp->lang_key=textp->lang + lang_len + 1;
671 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
672 *(textp->lang_key+lang_key_len) = '\0';
673 textp->text=textp->lang_key + lang_key_len + 1;
678 #ifdef PNG_iTXt_SUPPORTED
679 textp->lang=(png_charp)NULL;
680 textp->lang_key=(png_charp)NULL;
682 textp->text=textp->key + key_len + 1;
685 png_memcpy(textp->text, text_ptr[i].text,
686 (png_size_t)(text_length));
687 *(textp->text+text_length) = '\0';
689 #ifdef PNG_iTXt_SUPPORTED
690 if(textp->compression > 0)
692 textp->text_length = 0;
693 textp->itxt_length = text_length;
698 textp->text_length = text_length;
699 #ifdef PNG_iTXt_SUPPORTED
700 textp->itxt_length = 0;
703 info_ptr->text[info_ptr->num_text]= *textp;
704 info_ptr->num_text++;
705 png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
710 #if defined(PNG_tIME_SUPPORTED)
712 png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
714 png_debug1(1, "in %s storage function\n", "tIME");
715 if (png_ptr == NULL || info_ptr == NULL ||
716 (png_ptr->mode & PNG_WROTE_tIME))
719 png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
720 info_ptr->valid |= PNG_INFO_tIME;
724 #if defined(PNG_tRNS_SUPPORTED)
726 png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
727 png_bytep trans, int num_trans, png_color_16p trans_values)
729 png_debug1(1, "in %s storage function\n", "tRNS");
730 if (png_ptr == NULL || info_ptr == NULL)
736 * It may not actually be necessary to set png_ptr->trans here;
737 * we do it for backward compatibility with the way the png_handle_tRNS
738 * function used to do the allocation.
740 #ifdef PNG_FREE_ME_SUPPORTED
741 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
743 png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
744 (png_uint_32)num_trans);
745 png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
746 #ifdef PNG_FREE_ME_SUPPORTED
747 info_ptr->free_me |= PNG_FREE_TRNS;
749 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
753 if (trans_values != NULL)
755 png_memcpy(&(info_ptr->trans_values), trans_values,
756 sizeof(png_color_16));
760 info_ptr->num_trans = (png_uint_16)num_trans;
761 info_ptr->valid |= PNG_INFO_tRNS;
765 #if defined(PNG_sPLT_SUPPORTED)
767 png_set_sPLT(png_structp png_ptr,
768 png_infop info_ptr, png_sPLT_tp entries, int nentries)
773 np = (png_sPLT_tp)png_malloc(png_ptr,
774 (info_ptr->splt_palettes_num + nentries) * sizeof(png_sPLT_t));
776 png_memcpy(np, info_ptr->splt_palettes,
777 info_ptr->splt_palettes_num * sizeof(png_sPLT_t));
778 png_free(png_ptr, info_ptr->splt_palettes);
779 info_ptr->splt_palettes=NULL;
781 for (i = 0; i < nentries; i++)
783 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
784 png_sPLT_tp from = entries + i;
786 to->name = (png_charp)png_malloc(png_ptr,
787 png_strlen(from->name) + 1);
788 png_strcpy(to->name, from->name);
789 to->entries = (png_sPLT_entryp)png_malloc(png_ptr,
790 from->nentries * sizeof(png_sPLT_t));
791 png_memcpy(to->entries, from->entries,
792 from->nentries * sizeof(png_sPLT_t));
793 to->nentries = from->nentries;
794 to->depth = from->depth;
797 info_ptr->splt_palettes = np;
798 info_ptr->splt_palettes_num += nentries;
799 info_ptr->valid |= PNG_INFO_sPLT;
800 #ifdef PNG_FREE_ME_SUPPORTED
801 info_ptr->free_me |= PNG_FREE_SPLT;
804 #endif /* PNG_sPLT_SUPPORTED */
806 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
808 png_set_unknown_chunks(png_structp png_ptr,
809 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
811 png_unknown_chunkp np;
814 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
817 np = (png_unknown_chunkp)png_malloc(png_ptr,
818 (info_ptr->unknown_chunks_num + num_unknowns) *
819 sizeof(png_unknown_chunk));
821 png_memcpy(np, info_ptr->unknown_chunks,
822 info_ptr->unknown_chunks_num * sizeof(png_unknown_chunk));
823 png_free(png_ptr, info_ptr->unknown_chunks);
824 info_ptr->unknown_chunks=NULL;
826 for (i = 0; i < num_unknowns; i++)
828 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
829 png_unknown_chunkp from = unknowns + i;
831 png_strcpy((png_charp)to->name, (png_charp)from->name);
832 to->data = (png_bytep)png_malloc(png_ptr, from->size);
833 png_memcpy(to->data, from->data, from->size);
834 to->size = from->size;
836 /* note our location in the read or write sequence */
837 to->location = (png_byte)(png_ptr->mode & 0xff);
840 info_ptr->unknown_chunks = np;
841 info_ptr->unknown_chunks_num += num_unknowns;
842 #ifdef PNG_FREE_ME_SUPPORTED
843 info_ptr->free_me |= PNG_FREE_UNKN;
847 png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
848 int chunk, int location)
850 if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
851 (int)info_ptr->unknown_chunks_num)
852 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
856 #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
857 defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
859 png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
861 /* This function is deprecated in favor of png_permit_mng_features()
862 and will be removed from libpng-2.0.0 */
863 png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
866 png_ptr->mng_features_permitted = (png_byte)
867 ((png_ptr->mng_features_permitted & (~(PNG_FLAG_MNG_EMPTY_PLTE))) |
868 ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
872 #if defined(PNG_MNG_FEATURES_SUPPORTED)
874 png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
876 png_debug(1, "in png_permit_mng_features\n");
878 return (png_uint_32)0;
879 png_ptr->mng_features_permitted =
880 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
881 return (png_uint_32)png_ptr->mng_features_permitted;
885 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
887 png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
888 chunk_list, int num_chunks)
890 png_bytep new_list, p;
891 int i, old_num_chunks;
894 if(keep == HANDLE_CHUNK_ALWAYS || keep == HANDLE_CHUNK_IF_SAFE)
895 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
897 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
899 if(keep == HANDLE_CHUNK_ALWAYS)
900 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
902 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
905 if (chunk_list == NULL)
907 old_num_chunks=png_ptr->num_chunk_list;
908 new_list=(png_bytep)png_malloc(png_ptr,
909 (png_uint_32)(5*(num_chunks+old_num_chunks)));
910 if(png_ptr->chunk_list != NULL)
912 png_memcpy(new_list, png_ptr->chunk_list,
913 (png_size_t)(5*old_num_chunks));
914 png_free(png_ptr, png_ptr->chunk_list);
915 png_ptr->chunk_list=NULL;
917 png_memcpy(new_list+5*old_num_chunks, chunk_list,
918 (png_size_t)(5*num_chunks));
919 for (p=new_list+5*old_num_chunks+4, i=0; i<num_chunks; i++, p+=5)
921 png_ptr->num_chunk_list=old_num_chunks+num_chunks;
922 png_ptr->chunk_list=new_list;
923 #ifdef PNG_FREE_ME_SUPPORTED
924 png_ptr->free_me |= PNG_FREE_LIST;
929 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
931 png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
932 png_user_chunk_ptr read_user_chunk_fn)
934 png_debug(1, "in png_set_read_user_chunk_fn\n");
935 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
936 png_ptr->user_chunk_ptr = user_chunk_ptr;
940 #if defined(PNG_INFO_IMAGE_SUPPORTED)
942 png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
944 png_debug1(1, "in %s storage function\n", "rows");
946 if (png_ptr == NULL || info_ptr == NULL)
949 if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
950 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
951 info_ptr->row_pointers = row_pointers;
953 info_ptr->valid |= PNG_INFO_IDAT;
958 png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
961 png_free(png_ptr, png_ptr->zbuf);
962 png_ptr->zbuf_size = (png_size_t)size;
963 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
964 png_ptr->zstream.next_out = png_ptr->zbuf;
965 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
969 png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
971 if (png_ptr && info_ptr)
972 info_ptr->valid &= ~(mask);
976 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
977 /* this function was added to libpng 1.2.0 and should always exist by default */
979 png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
981 png_uint_32 settable_asm_flags;
982 png_uint_32 settable_mmx_flags;
985 #ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
986 PNG_ASM_FLAG_MMX_READ_COMBINE_ROW |
988 #ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE
989 PNG_ASM_FLAG_MMX_READ_INTERLACE |
991 #ifdef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
992 PNG_ASM_FLAG_MMX_READ_FILTER_SUB |
993 PNG_ASM_FLAG_MMX_READ_FILTER_UP |
994 PNG_ASM_FLAG_MMX_READ_FILTER_AVG |
995 PNG_ASM_FLAG_MMX_READ_FILTER_PAETH |
999 /* could be some non-MMX ones in the future, but not currently: */
1000 settable_asm_flags = settable_mmx_flags;
1002 if (!(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED) ||
1003 !(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU))
1005 /* clear all MMX flags if MMX isn't supported */
1006 settable_asm_flags &= ~settable_mmx_flags;
1007 png_ptr->asm_flags &= ~settable_mmx_flags;
1010 /* we're replacing the settable bits with those passed in by the user,
1011 * so first zero them out of the master copy, then logical-OR in the
1012 * allowed subset that was requested */
1014 png_ptr->asm_flags &= ~settable_asm_flags; /* zero them */
1015 png_ptr->asm_flags |= (asm_flags & settable_asm_flags); /* set them */
1017 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
1019 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
1020 /* this function was added to libpng 1.2.0 */
1022 png_set_mmx_thresholds (png_structp png_ptr,
1023 png_byte mmx_bitdepth_threshold,
1024 png_uint_32 mmx_rowbytes_threshold)
1026 png_ptr->mmx_bitdepth_threshold = mmx_bitdepth_threshold;
1027 png_ptr->mmx_rowbytes_threshold = mmx_rowbytes_threshold;
1029 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */