The BIG graph update
[rrdtool.git] / libraries / libpng-1.2.0 / pngset.c
1
2 /* pngset.c - storage of image information into info struct
3  *
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.)
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  */
15
16 #define PNG_INTERNAL
17 #include "png.h"
18
19 #if defined(PNG_bKGD_SUPPORTED)
20 void PNGAPI
21 png_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 (png_ptr == NULL || 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_cHRM_SUPPORTED)
33 #ifdef PNG_FLOATING_POINT_SUPPORTED
34 void PNGAPI
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 #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);
60 #endif
61    info_ptr->valid |= PNG_INFO_cHRM;
62 }
63 #endif
64 #ifdef PNG_FIXED_POINT_SUPPORTED
65 void PNGAPI
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)
70 {
71    png_debug1(1, "in %s storage function\n", "cHRM");
72    if (png_ptr == NULL || info_ptr == NULL)
73       return;
74
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.);
92 #endif
93    info_ptr->valid |= PNG_INFO_cHRM;
94 }
95 #endif
96 #endif
97
98 #if defined(PNG_gAMA_SUPPORTED)
99 #ifdef PNG_FLOATING_POINT_SUPPORTED
100 void PNGAPI
101 png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
102 {
103    png_debug1(1, "in %s storage function\n", "gAMA");
104    if (png_ptr == NULL || info_ptr == NULL)
105       return;
106
107    info_ptr->gamma = (float)file_gamma;
108 #ifdef PNG_FIXED_POINT_SUPPORTED
109    info_ptr->int_gamma = (int)(file_gamma*100000.+.5);
110 #endif
111    info_ptr->valid |= PNG_INFO_gAMA;
112    if(file_gamma == 0.0)
113       png_warning(png_ptr, "Setting gamma=0");
114 }
115 #endif
116 void PNGAPI
117 png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
118    int_gamma)
119 {
120    png_debug1(1, "in %s storage function\n", "gAMA");
121    if (png_ptr == NULL || info_ptr == NULL)
122       return;
123
124 #ifdef PNG_FLOATING_POINT_SUPPORTED
125    info_ptr->gamma = (float)(int_gamma/100000.);
126 #endif
127 #ifdef PNG_FIXED_POINT_SUPPORTED
128    info_ptr->int_gamma = int_gamma;
129 #endif
130    info_ptr->valid |= PNG_INFO_gAMA;
131    if(int_gamma == 0)
132       png_warning(png_ptr, "Setting gamma=0");
133 }
134 #endif
135
136 #if defined(PNG_hIST_SUPPORTED)
137 void PNGAPI
138 png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
139 {
140    int  i;
141
142    png_debug1(1, "in %s storage function\n", "hIST");
143    if (png_ptr == NULL || info_ptr == NULL)
144       return;
145    if (info_ptr->num_palette == 0)
146    {
147        png_warning(png_ptr,
148           "Palette size 0, hIST allocation skipped.");
149        return;
150    }
151
152 #ifdef PNG_FREE_ME_SUPPORTED
153    png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
154 #endif
155    png_ptr->hist = (png_uint_16p)png_malloc(png_ptr,
156       (png_uint_32)(info_ptr->num_palette * sizeof (png_uint_16)));
157
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;
162
163 #ifdef PNG_FREE_ME_SUPPORTED
164    info_ptr->free_me |= PNG_FREE_HIST;
165 #else
166    png_ptr->flags |= PNG_FLAG_FREE_HIST;
167 #endif
168 }
169 #endif
170
171 void PNGAPI
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,
175    int filter_type)
176 {
177    int rowbytes_per_pixel;
178    png_debug1(1, "in %s storage function\n", "IHDR");
179    if (png_ptr == NULL || info_ptr == NULL)
180       return;
181
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");
187
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");
192
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");
196
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");
202
203    if (interlace_type >= PNG_INTERLACE_LAST)
204       png_error(png_ptr, "Unknown interlace method in IHDR");
205
206    if (compression_type != PNG_COMPRESSION_TYPE_BASE)
207       png_error(png_ptr, "Unknown compression method in IHDR");
208
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
218     */
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)
222    {
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");
231    }
232 #else
233    if(filter_type != PNG_FILTER_TYPE_BASE)
234       png_error(png_ptr, "Unknown filter method in IHDR");
235 #endif
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    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;
248    else
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);
253
254    /* check for overflow */
255    rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3;
256    if (( width > PNG_MAX_UINT/rowbytes_per_pixel))
257    {
258       png_warning(png_ptr,
259          "Width too large to process image data; rowbytes will overflow.");
260       info_ptr->rowbytes = (png_size_t)0;
261    }
262    else
263       info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3;
264 }
265
266 #if defined(PNG_oFFs_SUPPORTED)
267 void PNGAPI
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)
270 {
271    png_debug1(1, "in %s storage function\n", "oFFs");
272    if (png_ptr == NULL || info_ptr == NULL)
273       return;
274
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;
279 }
280 #endif
281
282 #if defined(PNG_pCAL_SUPPORTED)
283 void PNGAPI
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)
287 {
288    png_uint_32 length;
289    int i;
290
291    png_debug1(1, "in %s storage function\n", "pCAL");
292    if (png_ptr == NULL || info_ptr == NULL)
293       return;
294
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);
299
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;
305
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);
310
311    info_ptr->pcal_params = (png_charpp)png_malloc(png_ptr,
312       (png_uint_32)((nparams + 1) * sizeof(png_charp)));
313
314    info_ptr->pcal_params[nparams] = NULL;
315
316    for (i = 0; i < nparams; i++)
317    {
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);
322    }
323
324    info_ptr->valid |= PNG_INFO_pCAL;
325 #ifdef PNG_FREE_ME_SUPPORTED
326    info_ptr->free_me |= PNG_FREE_PCAL;
327 #endif
328 }
329 #endif
330
331 #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
332 #ifdef PNG_FLOATING_POINT_SUPPORTED
333 void PNGAPI
334 png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
335              int unit, double width, double height)
336 {
337    png_debug1(1, "in %s storage function\n", "sCAL");
338    if (png_ptr == NULL || info_ptr == NULL)
339       return;
340
341    info_ptr->scal_unit = (png_byte)unit;
342    info_ptr->scal_pixel_width = width;
343    info_ptr->scal_pixel_height = height;
344
345    info_ptr->valid |= PNG_INFO_sCAL;
346 }
347 #else
348 #ifdef PNG_FIXED_POINT_SUPPORTED
349 void PNGAPI
350 png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
351              int unit, png_charp swidth, png_charp sheight)
352 {
353    png_uint_32 length;
354
355    png_debug1(1, "in %s storage function\n", "sCAL");
356    if (png_ptr == NULL || info_ptr == NULL)
357       return;
358
359    info_ptr->scal_unit = (png_byte)unit;
360
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);
365
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);
370
371    info_ptr->valid |= PNG_INFO_sCAL;
372 #ifdef PNG_FREE_ME_SUPPORTED
373    info_ptr->free_me |= PNG_FREE_SCAL;
374 #endif
375 }
376 #endif
377 #endif
378 #endif
379
380 #if defined(PNG_pHYs_SUPPORTED)
381 void PNGAPI
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)
384 {
385    png_debug1(1, "in %s storage function\n", "pHYs");
386    if (png_ptr == NULL || info_ptr == NULL)
387       return;
388
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;
393 }
394 #endif
395
396 void PNGAPI
397 png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
398    png_colorp palette, int num_palette)
399 {
400
401    png_debug1(1, "in %s storage function\n", "PLTE");
402    if (png_ptr == NULL || info_ptr == NULL)
403       return;
404
405    /*
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.
409     */
410 #ifdef PNG_FREE_ME_SUPPORTED
411    png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
412 #endif
413    png_ptr->palette = (png_colorp)png_zalloc(png_ptr, (uInt)num_palette,
414       sizeof (png_color));
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;
418
419 #ifdef PNG_FREE_ME_SUPPORTED
420    info_ptr->free_me |= PNG_FREE_PLTE;
421 #else
422    png_ptr->flags |= PNG_FLAG_FREE_PLTE;
423 #endif
424
425    info_ptr->valid |= PNG_INFO_PLTE;
426 }
427
428 #if defined(PNG_sBIT_SUPPORTED)
429 void PNGAPI
430 png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
431    png_color_8p sig_bit)
432 {
433    png_debug1(1, "in %s storage function\n", "sBIT");
434    if (png_ptr == NULL || info_ptr == NULL)
435       return;
436
437    png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8));
438    info_ptr->valid |= PNG_INFO_sBIT;
439 }
440 #endif
441
442 #if defined(PNG_sRGB_SUPPORTED)
443 void PNGAPI
444 png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
445 {
446    png_debug1(1, "in %s storage function\n", "sRGB");
447    if (png_ptr == NULL || info_ptr == NULL)
448       return;
449
450    info_ptr->srgb_intent = (png_byte)intent;
451    info_ptr->valid |= PNG_INFO_sRGB;
452 }
453
454 void PNGAPI
455 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
456    int intent)
457 {
458 #if defined(PNG_gAMA_SUPPORTED)
459 #ifdef PNG_FLOATING_POINT_SUPPORTED
460    float file_gamma;
461 #endif
462 #ifdef PNG_FIXED_POINT_SUPPORTED
463    png_fixed_point int_file_gamma;
464 #endif
465 #endif
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;
469 #endif
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;
473 #endif
474 #endif
475    png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
476    if (png_ptr == NULL || info_ptr == NULL)
477       return;
478
479    png_set_sRGB(png_ptr, info_ptr, intent);
480
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);
485 #endif
486 #ifdef PNG_FIXED_POINT_SUPPORTED
487    int_file_gamma = 45455L;
488    png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
489 #endif
490 #endif
491
492 #if defined(PNG_cHRM_SUPPORTED)
493 #ifdef PNG_FIXED_POINT_SUPPORTED
494    int_white_x = 31270L;
495    int_white_y = 32900L;
496    int_red_x   = 64000L;
497    int_red_y   = 33000L;
498    int_green_x = 30000L;
499    int_green_y = 60000L;
500    int_blue_x  = 15000L;
501    int_blue_y  =  6000L;
502
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);
506 #endif
507 #ifdef PNG_FLOATING_POINT_SUPPORTED
508    white_x = (float).3127;
509    white_y = (float).3290;
510    red_x   = (float).64;
511    red_y   = (float).33;
512    green_x = (float).30;
513    green_y = (float).60;
514    blue_x  = (float).15;
515    blue_y  = (float).06;
516
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);
519 #endif
520 #endif
521 }
522 #endif
523
524
525 #if defined(PNG_iCCP_SUPPORTED)
526 void PNGAPI
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)
530 {
531    png_charp new_iccp_name;
532    png_charp new_iccp_profile;
533
534    png_debug1(1, "in %s storage function\n", "iCCP");
535    if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
536       return;
537
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);
542
543    png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
544
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;
553 #endif
554    info_ptr->valid |= PNG_INFO_iCCP;
555 }
556 #endif
557
558 #if defined(PNG_TEXT_SUPPORTED)
559 void PNGAPI
560 png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
561    int num_text)
562 {
563    int i;
564
565    png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
566       "text" : (png_const_charp)png_ptr->chunk_name));
567
568    if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
569       return;
570
571    /* Make sure we have enough space in the "text" array in info_struct
572     * to hold all of the incoming text_ptr objects.
573     */
574    if (info_ptr->num_text + num_text > info_ptr->max_text)
575    {
576       if (info_ptr->text != NULL)
577       {
578          png_textp old_text;
579          int old_max;
580
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 *
587             sizeof(png_text)));
588          png_free(png_ptr, old_text);
589       }
590       else
591       {
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;
598 #endif
599       }
600       png_debug1(3, "allocated %d entries for info_ptr->text\n",
601          info_ptr->max_text);
602    }
603    for (i = 0; i < num_text; i++)
604    {
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]);
608
609       if (text_ptr[i].key == NULL)
610           continue;
611
612       key_len = png_strlen(text_ptr[i].key);
613
614       if(text_ptr[i].compression <= 0)
615       {
616         lang_len = 0;
617         lang_key_len = 0;
618       }
619       else
620 #ifdef PNG_iTXt_SUPPORTED
621       {
622         /* set iTXt data */
623         if (text_ptr[i].key != NULL)
624           lang_len = png_strlen(text_ptr[i].lang);
625         else
626           lang_len = 0;
627         if (text_ptr[i].lang_key != NULL)
628           lang_key_len = png_strlen(text_ptr[i].lang_key);
629         else
630           lang_key_len = 0;
631       }
632 #else
633       {
634         png_warning(png_ptr, "iTXt chunk not supported.");
635         continue;
636       }
637 #endif
638
639       if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
640       {
641          text_length = 0;
642 #ifdef PNG_iTXt_SUPPORTED
643          if(text_ptr[i].compression > 0)
644             textp->compression = PNG_ITXT_COMPRESSION_NONE;
645          else
646 #endif
647             textp->compression = PNG_TEXT_COMPRESSION_NONE;
648       }
649       else
650       {
651          text_length = png_strlen(text_ptr[i].text);
652          textp->compression = text_ptr[i].compression;
653       }
654
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),
659          (int)textp->key);
660
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)
666       {
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;
674       }
675       else
676 #endif
677       {
678 #ifdef PNG_iTXt_SUPPORTED
679          textp->lang=(png_charp)NULL;
680          textp->lang_key=(png_charp)NULL;
681 #endif
682          textp->text=textp->key + key_len + 1;
683       }
684       if(text_length)
685          png_memcpy(textp->text, text_ptr[i].text,
686             (png_size_t)(text_length));
687       *(textp->text+text_length) = '\0';
688
689 #ifdef PNG_iTXt_SUPPORTED
690       if(textp->compression > 0)
691       {
692          textp->text_length = 0;
693          textp->itxt_length = text_length;
694       }
695       else
696 #endif
697       {
698          textp->text_length = text_length;
699 #ifdef PNG_iTXt_SUPPORTED
700          textp->itxt_length = 0;
701 #endif
702       }
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);
706    }
707 }
708 #endif
709
710 #if defined(PNG_tIME_SUPPORTED)
711 void PNGAPI
712 png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
713 {
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))
717       return;
718
719    png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time));
720    info_ptr->valid |= PNG_INFO_tIME;
721 }
722 #endif
723
724 #if defined(PNG_tRNS_SUPPORTED)
725 void PNGAPI
726 png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
727    png_bytep trans, int num_trans, png_color_16p trans_values)
728 {
729    png_debug1(1, "in %s storage function\n", "tRNS");
730    if (png_ptr == NULL || info_ptr == NULL)
731       return;
732
733    if (trans != NULL)
734    {
735        /*
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.
739         */
740 #ifdef PNG_FREE_ME_SUPPORTED
741        png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
742 #endif
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;
748 #else
749        png_ptr->flags |= PNG_FLAG_FREE_TRNS;
750 #endif
751    }
752
753    if (trans_values != NULL)
754    {
755       png_memcpy(&(info_ptr->trans_values), trans_values,
756          sizeof(png_color_16));
757       if (num_trans == 0)
758         num_trans = 1;
759    }
760    info_ptr->num_trans = (png_uint_16)num_trans;
761    info_ptr->valid |= PNG_INFO_tRNS;
762 }
763 #endif
764
765 #if defined(PNG_sPLT_SUPPORTED)
766 void PNGAPI
767 png_set_sPLT(png_structp png_ptr,
768              png_infop info_ptr, png_sPLT_tp entries, int nentries)
769 {
770     png_sPLT_tp np;
771     int i;
772
773     np = (png_sPLT_tp)png_malloc(png_ptr,
774         (info_ptr->splt_palettes_num + nentries) * sizeof(png_sPLT_t));
775
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;
780
781     for (i = 0; i < nentries; i++)
782     {
783         png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
784         png_sPLT_tp from = entries + i;
785
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;
795     }
796
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;
802 #endif
803 }
804 #endif /* PNG_sPLT_SUPPORTED */
805
806 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
807 void PNGAPI
808 png_set_unknown_chunks(png_structp png_ptr,
809    png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
810 {
811     png_unknown_chunkp np;
812     int i;
813
814     if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
815         return;
816
817     np = (png_unknown_chunkp)png_malloc(png_ptr,
818         (info_ptr->unknown_chunks_num + num_unknowns) *
819         sizeof(png_unknown_chunk));
820
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;
825
826     for (i = 0; i < num_unknowns; i++)
827     {
828         png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
829         png_unknown_chunkp from = unknowns + i;
830
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;
835
836         /* note our location in the read or write sequence */
837         to->location = (png_byte)(png_ptr->mode & 0xff);
838     }
839
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;
844 #endif
845 }
846 void PNGAPI
847 png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
848    int chunk, int location)
849 {
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;
853 }
854 #endif
855
856 #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
857     defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
858 void PNGAPI
859 png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
860 {
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");
864    if (png_ptr == NULL)
865       return;
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)));
869 }
870 #endif
871
872 #if defined(PNG_MNG_FEATURES_SUPPORTED)
873 png_uint_32 PNGAPI
874 png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
875 {
876    png_debug(1, "in png_permit_mng_features\n");
877    if (png_ptr == NULL)
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;
882 }
883 #endif
884
885 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
886 void PNGAPI
887 png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
888    chunk_list, int num_chunks)
889 {
890     png_bytep new_list, p;
891     int i, old_num_chunks;
892     if (num_chunks == 0)
893     {
894       if(keep == HANDLE_CHUNK_ALWAYS || keep == HANDLE_CHUNK_IF_SAFE)
895         png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
896       else
897         png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
898
899       if(keep == HANDLE_CHUNK_ALWAYS)
900         png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
901       else
902         png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
903       return;
904     }
905     if (chunk_list == NULL)
906       return;
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)
911     {
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;
916     }
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)
920        *p=(png_byte)keep;
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;
925 #endif
926 }
927 #endif
928
929 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
930 void PNGAPI
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)
933 {
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;
937 }
938 #endif
939
940 #if defined(PNG_INFO_IMAGE_SUPPORTED)
941 void PNGAPI
942 png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
943 {
944    png_debug1(1, "in %s storage function\n", "rows");
945
946    if (png_ptr == NULL || info_ptr == NULL)
947       return;
948
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;
952    if(row_pointers)
953       info_ptr->valid |= PNG_INFO_IDAT;
954 }
955 #endif
956
957 void PNGAPI
958 png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
959 {
960     if(png_ptr->zbuf)
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;
966 }
967
968 void PNGAPI
969 png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
970 {
971    if (png_ptr && info_ptr)
972       info_ptr->valid &= ~(mask);
973 }
974
975
976 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
977 /* this function was added to libpng 1.2.0 and should always exist by default */
978 void PNGAPI
979 png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
980 {
981     png_uint_32 settable_asm_flags;
982     png_uint_32 settable_mmx_flags;
983
984     settable_mmx_flags =
985 #ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
986                          PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  |
987 #endif
988 #ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE
989                          PNG_ASM_FLAG_MMX_READ_INTERLACE    |
990 #endif
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 |
996 #endif
997                          0;
998
999     /* could be some non-MMX ones in the future, but not currently: */
1000     settable_asm_flags = settable_mmx_flags;
1001
1002     if (!(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED) ||
1003         !(png_ptr->asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU))
1004     {
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;
1008     }
1009
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 */
1013
1014     png_ptr->asm_flags &= ~settable_asm_flags;                  /* zero them */
1015     png_ptr->asm_flags |= (asm_flags & settable_asm_flags);     /* set them */
1016 }
1017 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
1018
1019 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
1020 /* this function was added to libpng 1.2.0 */
1021 void PNGAPI
1022 png_set_mmx_thresholds (png_structp png_ptr,
1023                         png_byte mmx_bitdepth_threshold,
1024                         png_uint_32 mmx_rowbytes_threshold)
1025 {
1026     png_ptr->mmx_bitdepth_threshold = mmx_bitdepth_threshold;
1027     png_ptr->mmx_rowbytes_threshold = mmx_rowbytes_threshold;
1028 }
1029 #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */