blob: 6f05c171141f8759311c4b4c2fe6ed8b64e37f9d [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngset.c - storage of image information into info struct
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05004 * Last changed in libpng 1.4.0 [August 16, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * 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 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050018
Andreas Dilger47a0c421997-05-16 02:46:07 -050019#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050021#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060023#if defined(PNG_bKGD_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050025png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
26{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050027 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050028
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060029 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050030 return;
31
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032 png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
Andreas Dilger47a0c421997-05-16 02:46:07 -050033 info_ptr->valid |= PNG_INFO_bKGD;
34}
35#endif
36
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060037#if defined(PNG_cHRM_SUPPORTED)
38#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050039void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050040png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
41 double white_x, double white_y, double red_x, double red_y,
42 double green_x, double green_y, double blue_x, double blue_y)
43{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050044 png_debug1(1, "in %s storage function", "cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050045
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -060046 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050047 return;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060048
Andreas Dilger47a0c421997-05-16 02:46:07 -050049 info_ptr->x_white = (float)white_x;
50 info_ptr->y_white = (float)white_y;
51 info_ptr->x_red = (float)red_x;
52 info_ptr->y_red = (float)red_y;
53 info_ptr->x_green = (float)green_x;
54 info_ptr->y_green = (float)green_y;
55 info_ptr->x_blue = (float)blue_x;
56 info_ptr->y_blue = (float)blue_y;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060057#ifdef PNG_FIXED_POINT_SUPPORTED
58 info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
59 info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060060 info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5);
61 info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060062 info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
63 info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060064 info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5);
65 info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060066#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 info_ptr->valid |= PNG_INFO_cHRM;
68}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060069#endif /* PNG_FLOATING_POINT_SUPPORTED */
70
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060071#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050072void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060073png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060074 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
75 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
76 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060077{
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060078 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050079
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060080 if (png_ptr == NULL || info_ptr == NULL)
81 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060083#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060084 if (png_check_cHRM_fixed(png_ptr,
85 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -060086#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050087 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050088 info_ptr->int_x_white = white_x;
89 info_ptr->int_y_white = white_y;
90 info_ptr->int_x_red = red_x;
91 info_ptr->int_y_red = red_y;
92 info_ptr->int_x_green = green_x;
93 info_ptr->int_y_green = green_y;
94 info_ptr->int_x_blue = blue_x;
95 info_ptr->int_y_blue = blue_y;
96#ifdef PNG_FLOATING_POINT_SUPPORTED
97 info_ptr->x_white = (float)(white_x/100000.);
98 info_ptr->y_white = (float)(white_y/100000.);
99 info_ptr->x_red = (float)( red_x/100000.);
100 info_ptr->y_red = (float)( red_y/100000.);
101 info_ptr->x_green = (float)(green_x/100000.);
102 info_ptr->y_green = (float)(green_y/100000.);
103 info_ptr->x_blue = (float)( blue_x/100000.);
104 info_ptr->y_blue = (float)( blue_y/100000.);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600105#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500106 info_ptr->valid |= PNG_INFO_cHRM;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600107 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600108}
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600109#endif /* PNG_FIXED_POINT_SUPPORTED */
110#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600111
112#if defined(PNG_gAMA_SUPPORTED)
113#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500114void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
116{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500117 double png_gamma;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500118
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500119 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500120
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600121 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500122 return;
123
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600124 /* Check for overflow */
125 if (file_gamma > 21474.83)
126 {
127 png_warning(png_ptr, "Limiting gamma to 21474.83");
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500128 png_gamma=21474.83;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600129 }
130 else
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500131 png_gamma = file_gamma;
132 info_ptr->gamma = (float)png_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600133#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500134 info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600135#endif
136 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500137 if (png_gamma == 0.0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500138 png_warning(png_ptr, "Setting gamma=0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600139}
140#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500141void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600142png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
143 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600144{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500145 png_fixed_point png_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600146
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500147 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500148
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600149 if (png_ptr == NULL || info_ptr == NULL)
150 return;
151
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600153 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 png_warning(png_ptr, "Limiting gamma to 21474.83");
155 png_gamma=PNG_UINT_31_MAX;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600156 }
157 else
158 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500159 if (int_gamma < 0)
160 {
161 png_warning(png_ptr, "Setting negative gamma to zero");
162 png_gamma = 0;
163 }
164 else
165 png_gamma = int_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600166 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600167#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500168 info_ptr->gamma = (float)(png_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600169#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500170#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500171 info_ptr->int_gamma = png_gamma;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500172#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500174 if (png_gamma == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500175 png_warning(png_ptr, "Setting gamma=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500177#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600179#if defined(PNG_hIST_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500180void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
182{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500183 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600184
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500185 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500186
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600187 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500189
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500190 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600191 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500192 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500193 png_warning(png_ptr,
194 "Invalid palette size, hIST allocation skipped");
195 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600198#ifdef PNG_FREE_ME_SUPPORTED
199 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
200#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500201 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
202 * version 1.2.1
203 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500204 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500205 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500206 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 {
208 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
209 return;
210 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600211
212 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500213 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600214 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500215 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600216
217#ifdef PNG_FREE_ME_SUPPORTED
218 info_ptr->free_me |= PNG_FREE_HIST;
219#else
220 png_ptr->flags |= PNG_FLAG_FREE_HIST;
221#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222}
223#endif
224
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500225void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
227 png_uint_32 width, png_uint_32 height, int bit_depth,
228 int color_type, int interlace_type, int compression_type,
229 int filter_type)
230{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500231 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500232
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600233 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500234 return;
235
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500236 /* Check for width and height valid values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500237 if (width == 0 || height == 0)
238 png_error(png_ptr, "Image width or height is zero in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500239#ifdef PNG_SET_USER_LIMITS_SUPPORTED
240 if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
241 png_error(png_ptr, "image size exceeds user limits in IHDR");
242#else
243 if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
244 png_error(png_ptr, "image size exceeds user limits in IHDR");
245#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500246 if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500247 png_error(png_ptr, "Invalid image size in IHDR");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500248 if ( width > (PNG_UINT_32_MAX
249 >> 3) /* 8-byte RGBA pixels */
250 - 64 /* bigrowbuf hack */
251 - 1 /* filter byte */
252 - 7*8 /* rounding of width to multiple of 8 pixels */
253 - 8) /* extra max_pixel_depth pad */
254 png_warning(png_ptr, "Width is too large for libpng to process pixels");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500255
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500256 /* Check other values */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500257 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500258 bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500259 png_error(png_ptr, "Invalid bit depth in IHDR");
260
261 if (color_type < 0 || color_type == 1 ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500262 color_type == 5 || color_type > 6)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500263 png_error(png_ptr, "Invalid color type in IHDR");
264
265 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
266 ((color_type == PNG_COLOR_TYPE_RGB ||
267 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
268 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
269 png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
270
271 if (interlace_type >= PNG_INTERLACE_LAST)
272 png_error(png_ptr, "Unknown interlace method in IHDR");
273
274 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
275 png_error(png_ptr, "Unknown compression method in IHDR");
276
277#if defined(PNG_MNG_FEATURES_SUPPORTED)
278 /* Accept filter_method 64 (intrapixel differencing) only if
279 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
280 * 2. Libpng did not read a PNG signature (this filter_method is only
281 * used in PNG datastreams that are embedded in MNG datastreams) and
282 * 3. The application called png_permit_mng_features with a mask that
283 * included PNG_FLAG_MNG_FILTER_64 and
284 * 4. The filter_method is 64 and
285 * 5. The color_type is RGB or RGBA
286 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500287 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
288 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
289 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500290 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500291 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500292 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
293 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
294 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500295 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
296 png_error(png_ptr, "Unknown filter method in IHDR");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500297 if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500298 png_warning(png_ptr, "Invalid filter method in IHDR");
299 }
300#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500301 if (filter_type != PNG_FILTER_TYPE_BASE)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500302 png_error(png_ptr, "Unknown filter method in IHDR");
303#endif
304
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 info_ptr->width = width;
306 info_ptr->height = height;
307 info_ptr->bit_depth = (png_byte)bit_depth;
308 info_ptr->color_type =(png_byte) color_type;
309 info_ptr->compression_type = (png_byte)compression_type;
310 info_ptr->filter_type = (png_byte)filter_type;
311 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500312 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
313 info_ptr->channels = 1;
314 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 info_ptr->channels = 3;
316 else
317 info_ptr->channels = 1;
318 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
319 info_ptr->channels++;
320 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600321
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500322 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500324 >> 3) /* 8-byte RGBA pixels */
325 - 64 /* bigrowbuf hack */
326 - 1 /* filter byte */
327 - 7*8 /* rounding of width to multiple of 8 pixels */
328 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500329 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500330 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500331 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332}
333
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600334#if defined(PNG_oFFs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500335void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600337 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500339 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500340
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600341 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 return;
343
344 info_ptr->x_offset = offset_x;
345 info_ptr->y_offset = offset_y;
346 info_ptr->offset_unit_type = (png_byte)unit_type;
347 info_ptr->valid |= PNG_INFO_oFFs;
348}
349#endif
350
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351#if defined(PNG_pCAL_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500352void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
354 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
355 png_charp units, png_charpp params)
356{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500357 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600358 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500360 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500361
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600362 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 return;
364
365 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500366 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500367 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500368 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
369 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500370 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500371 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500372 return;
373 }
374 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500375
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500376 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 info_ptr->pcal_X0 = X0;
378 info_ptr->pcal_X1 = X1;
379 info_ptr->pcal_type = (png_byte)type;
380 info_ptr->pcal_nparams = (png_byte)nparams;
381
382 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500383 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500384 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500385 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
386 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500388 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500389 return;
390 }
391 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500393 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500394 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500395 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500397 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500398 return;
399 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500400
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600401 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402
403 for (i = 0; i < nparams; i++)
404 {
405 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500406 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500407 (unsigned long)length);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500408 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
409 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500410 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500411 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
412 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500413 }
414 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 }
416
417 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500418#ifdef PNG_FREE_ME_SUPPORTED
419 info_ptr->free_me |= PNG_FREE_PCAL;
420#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421}
422#endif
423
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
425#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500426void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600427png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600428 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600429{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500430 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500431
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600432 if (png_ptr == NULL || info_ptr == NULL)
433 return;
434
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600435 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436 info_ptr->scal_pixel_width = width;
437 info_ptr->scal_pixel_height = height;
438
439 info_ptr->valid |= PNG_INFO_sCAL;
440}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600441#else
442#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500443void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600444png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600445 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600446{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500447 png_size_t length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600448
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500449 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500450
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600451 if (png_ptr == NULL || info_ptr == NULL)
452 return;
453
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600454 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600455
456 length = png_strlen(swidth) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500457 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500458 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500459 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
460 if (info_ptr->scal_s_width == NULL)
461 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500462 png_warning(png_ptr,
463 "Memory allocation failed while processing sCAL");
464 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500465 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500466 png_memcpy(info_ptr->scal_s_width, swidth, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600467
468 length = png_strlen(sheight) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500469 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500470 (unsigned int)length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500471 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
472 if (info_ptr->scal_s_height == NULL)
473 {
474 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500475 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500476 png_warning(png_ptr,
477 "Memory allocation failed while processing sCAL");
478 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500479 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500480 png_memcpy(info_ptr->scal_s_height, sheight, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600481 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500482#ifdef PNG_FREE_ME_SUPPORTED
483 info_ptr->free_me |= PNG_FREE_SCAL;
484#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600485}
486#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600487#endif
488#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600489
490#if defined(PNG_pHYs_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500491void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500492png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
493 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
494{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500495 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500496
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600497 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500498 return;
499
500 info_ptr->x_pixels_per_unit = res_x;
501 info_ptr->y_pixels_per_unit = res_y;
502 info_ptr->phys_unit_type = (png_byte)unit_type;
503 info_ptr->valid |= PNG_INFO_pHYs;
504}
505#endif
506
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500507void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
509 png_colorp palette, int num_palette)
510{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600511
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500512 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500513
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600514 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500515 return;
516
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600517 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500518 {
519 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600520 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500521 else
522 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600523 png_warning(png_ptr, "Invalid palette length");
524 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525 }
526 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600527
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600528 /*
529 * It may not actually be necessary to set png_ptr->palette here;
530 * we do it for backward compatibility with the way the png_handle_tRNS
531 * function used to do the allocation.
532 */
533#ifdef PNG_FREE_ME_SUPPORTED
534 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
535#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500536
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600537 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500538 * of num_palette entries, in case of an invalid PNG file that has
539 * too-large sample values.
540 */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600541#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600542 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500543 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600544#else
545 png_ptr->palette = (png_colorp)png_malloc(png_ptr,
546 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
547 png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
548 png_sizeof(png_color));
549#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500550 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600551 info_ptr->palette = png_ptr->palette;
552 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600553
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600554#ifdef PNG_FREE_ME_SUPPORTED
555 info_ptr->free_me |= PNG_FREE_PLTE;
556#else
557 png_ptr->flags |= PNG_FLAG_FREE_PLTE;
558#endif
559
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600560 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561}
562
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600563#if defined(PNG_sBIT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500564void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
566 png_color_8p sig_bit)
567{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500568 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500569
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600570 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 return;
572
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500573 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 info_ptr->valid |= PNG_INFO_sBIT;
575}
576#endif
577
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600578#if defined(PNG_sRGB_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600580png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600581{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500582 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500583
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600584 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585 return;
586
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600587 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 info_ptr->valid |= PNG_INFO_sRGB;
589}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600590
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500591void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600593 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600594{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600595#if defined(PNG_gAMA_SUPPORTED)
596#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600597 float file_gamma;
598#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600599#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600600 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600601#endif
602#endif
603#if defined(PNG_cHRM_SUPPORTED)
604#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600605 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
606#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600607 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600608 int_green_y, int_blue_x, int_blue_y;
609#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500610 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500611
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600612 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600613 return;
614
615 png_set_sRGB(png_ptr, info_ptr, intent);
616
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600617#if defined(PNG_gAMA_SUPPORTED)
618#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonab1e5831999-10-06 04:57:42 -0500619 file_gamma = (float).45455;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600620 png_set_gAMA(png_ptr, info_ptr, file_gamma);
621#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600622#ifdef PNG_FIXED_POINT_SUPPORTED
623 int_file_gamma = 45455L;
624 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
625#endif
626#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600627
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600628#if defined(PNG_cHRM_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600629 int_white_x = 31270L;
630 int_white_y = 32900L;
631 int_red_x = 64000L;
632 int_red_y = 33000L;
633 int_green_x = 30000L;
634 int_green_y = 60000L;
635 int_blue_x = 15000L;
636 int_blue_y = 6000L;
637
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600638#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600639 white_x = (float).3127;
640 white_y = (float).3290;
641 red_x = (float).64;
642 red_y = (float).33;
643 green_x = (float).30;
644 green_y = (float).60;
645 blue_x = (float).15;
646 blue_y = (float).06;
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600647#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600648
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600649#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600650 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500651 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
652 int_green_y, int_blue_x, int_blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600653#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600654 {
655#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500656 png_set_cHRM_fixed(png_ptr, info_ptr,
657 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
658 int_green_y, int_blue_x, int_blue_y);
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600659#endif
660#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600661 png_set_cHRM(png_ptr, info_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500662 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600663#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600664 }
665#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600666}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600667#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600668
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600669
670#if defined(PNG_iCCP_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500671void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600672png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
673 png_charp name, int compression_type,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600674 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600675{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500676 png_charp new_iccp_name;
677 png_charp new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500678 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500679
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500680 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500681
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600682 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
683 return;
684
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500685 length = png_strlen(name)+1;
686 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500687 if (new_iccp_name == NULL)
688 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500689 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500690 return;
691 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500692 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500693 new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
694 if (new_iccp_profile == NULL)
695 {
696 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500697 png_warning(png_ptr,
698 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500699 return;
700 }
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500701 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
702
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500703 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500704
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600705 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500706 info_ptr->iccp_name = new_iccp_name;
707 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600708 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500709 * does not have to change if we introduce multiple compression types */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600710 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500711#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600712 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500713#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600714 info_ptr->valid |= PNG_INFO_iCCP;
715}
716#endif
717
718#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500719void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500721 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500723 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500724 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500725 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500726 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500727}
728
729int /* PRIVATE */
730png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500731 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500732{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500733 int i;
734
Glenn Randers-Pehrsone2a118f2009-07-27 22:08:25 -0500735 png_debug1(1, "in %s storage function", ((png_ptr == NULL ||
736 png_ptr->chunk_name[0] == '\0') ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600737 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600739 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500740 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741
742 /* Make sure we have enough space in the "text" array in info_struct
743 * to hold all of the incoming text_ptr objects.
744 */
745 if (info_ptr->num_text + num_text > info_ptr->max_text)
746 {
747 if (info_ptr->text != NULL)
748 {
749 png_textp old_text;
750 int old_max;
751
752 old_max = info_ptr->max_text;
753 info_ptr->max_text = info_ptr->num_text + num_text + 8;
754 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500755 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500756 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500757 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500758 {
759 png_free(png_ptr, old_text);
760 return(1);
761 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600762 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500763 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_free(png_ptr, old_text);
765 }
766 else
767 {
768 info_ptr->max_text = num_text + 8;
769 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500770 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500771 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500772 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500773 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500774#ifdef PNG_FREE_ME_SUPPORTED
775 info_ptr->free_me |= PNG_FREE_TEXT;
776#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777 }
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500778 png_debug1(3, "allocated %d entries for info_ptr->text",
Andreas Dilger47a0c421997-05-16 02:46:07 -0500779 info_ptr->max_text);
780 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500781 for (i = 0; i < num_text; i++)
782 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500783 png_size_t text_length, key_len;
784 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500785 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
786
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500787 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600788 continue;
789
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600790 key_len = png_strlen(text_ptr[i].key);
791
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500793 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500794 lang_len = 0;
795 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500796 }
797 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500798#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500800 /* Set iTXt data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500801 if (text_ptr[i].lang != NULL)
802 lang_len = png_strlen(text_ptr[i].lang);
803 else
804 lang_len = 0;
805 if (text_ptr[i].lang_key != NULL)
806 lang_key_len = png_strlen(text_ptr[i].lang_key);
807 else
808 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600809 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500810#else
811 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500812 png_warning(png_ptr, "iTXt chunk not supported");
813 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500814 }
815#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500816
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500817 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500818 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600819 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500820#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600822 textp->compression = PNG_ITXT_COMPRESSION_NONE;
823 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500824#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 }
827 else
828 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500830 textp->compression = text_ptr[i].compression;
831 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500832
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500833 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500834 (png_size_t)
835 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500836 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500837 return(1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500838 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500839 (unsigned long)(png_uint_32)
840 (key_len + lang_len + lang_key_len + text_length + 4),
841 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500842
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500843 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500844 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500845#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600846 if (text_ptr[i].compression > 0)
847 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500848 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600849 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500850 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500851 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600852 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500853 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500854 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600855 }
856 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500857#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600858 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500859#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500860 textp->lang=NULL;
861 textp->lang_key=NULL;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500862#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500863 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600864 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500865 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500866 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600867 (png_size_t)(text_length));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500868 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500870#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500871 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600872 {
873 textp->text_length = 0;
874 textp->itxt_length = text_length;
875 }
876 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500877#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600878 {
879 textp->text_length = text_length;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500880#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600881 textp->itxt_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500882#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600883 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500884 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500885 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500887 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888}
889#endif
890
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600891#if defined(PNG_tIME_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500892void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500893png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
894{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500895 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500896
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500897 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600898 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 return;
900
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500901 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500902 info_ptr->valid |= PNG_INFO_tIME;
903}
904#endif
905
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600906#if defined(PNG_tRNS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500907void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500909 png_bytep trans_alpha, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500911 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500912
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600913 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914 return;
915
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500916 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600917 {
918 /*
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500919 * It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500920 * we do it for backward compatibility with the way the png_handle_tRNS
921 * function used to do the allocation.
922 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500923
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600924#ifdef PNG_FREE_ME_SUPPORTED
925 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
926#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500927
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600928 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500929 png_ptr->trans_alpha = info_ptr->trans_alpha = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500930 (png_size_t)PNG_MAX_PALETTE_LENGTH);
931 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500932 png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600933 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500934
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500935 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500937 int sample_max = (1 << info_ptr->bit_depth);
938 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500939 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500940 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500941 ((int)trans_color->red > sample_max ||
942 (int)trans_color->green > sample_max ||
943 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500944 png_warning(png_ptr,
945 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500946 png_memcpy(&(info_ptr->trans_color), trans_color,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500947 png_sizeof(png_color_16));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600948 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500949 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500950 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500951
Andreas Dilger47a0c421997-05-16 02:46:07 -0500952 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500953 if (num_trans != 0)
954 {
955 info_ptr->valid |= PNG_INFO_tRNS;
956#ifdef PNG_FREE_ME_SUPPORTED
957 info_ptr->free_me |= PNG_FREE_TRNS;
958#else
959 png_ptr->flags |= PNG_FLAG_FREE_TRNS;
960#endif
961 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500962}
963#endif
964
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600965#if defined(PNG_sPLT_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500966void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600967png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600968 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600969/*
970 * entries - array of png_sPLT_t structures
971 * to be added to the list of palettes
972 * in the info structure.
973 * nentries - number of palette structures to be
974 * added.
975 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600976{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500977 png_sPLT_tp np;
978 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600979
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500980 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500981 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600982
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500983 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
984 (info_ptr->splt_palettes_num + nentries) *
985 (png_size_t)png_sizeof(png_sPLT_t));
986 if (np == NULL)
987 {
988 png_warning(png_ptr, "No memory for sPLT palettes");
989 return;
990 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600991
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500992 png_memcpy(np, info_ptr->splt_palettes,
993 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
994 png_free(png_ptr, info_ptr->splt_palettes);
995 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600996
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500997 for (i = 0; i < nentries; i++)
998 {
999 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
1000 png_sPLT_tp from = entries + i;
1001 png_uint_32 length;
1002
1003 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001004 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001005 if (to->name == NULL)
1006 {
1007 png_warning(png_ptr,
1008 "Out of memory while processing sPLT chunk");
1009 continue;
1010 }
1011 png_memcpy(to->name, from->name, length);
1012 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001013 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001014 if (to->entries == NULL)
1015 {
1016 png_warning(png_ptr,
1017 "Out of memory while processing sPLT chunk");
1018 png_free(png_ptr, to->name);
1019 to->name = NULL;
1020 continue;
1021 }
1022 png_memcpy(to->entries, from->entries,
1023 from->nentries * png_sizeof(png_sPLT_entry));
1024 to->nentries = from->nentries;
1025 to->depth = from->depth;
1026 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001027
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001028 info_ptr->splt_palettes = np;
1029 info_ptr->splt_palettes_num += nentries;
1030 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001031#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001032 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001033#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001034}
1035#endif /* PNG_sPLT_SUPPORTED */
1036
Glenn Randers-Pehrson59c39b82009-06-16 17:23:08 -05001037#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001038void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001039png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001041{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042 png_unknown_chunkp np;
1043 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001044
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001045 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001046 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001047
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
1049 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
1050 png_sizeof(png_unknown_chunk)));
1051 if (np == NULL)
1052 {
1053 png_warning(png_ptr,
1054 "Out of memory while processing unknown chunk");
1055 return;
1056 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001058 png_memcpy(np, info_ptr->unknown_chunks,
1059 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
1060 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062 for (i = 0; i < num_unknowns; i++)
1063 {
1064 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
1065 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001066
Glenn Randers-Pehrson7824a702009-06-13 10:05:05 -05001067 png_memcpy((png_charp)to->name,
1068 (png_charp)from->name,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001069 png_sizeof(from->name));
1070 to->name[png_sizeof(to->name)-1] = '\0';
1071 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001072 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001074
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001075 if (from->size == 0)
1076 to->data=NULL;
1077 else
1078 {
1079 to->data = (png_bytep)png_malloc_warn(png_ptr,
1080 (png_size_t)from->size);
1081 if (to->data == NULL)
1082 {
1083 png_warning(png_ptr,
1084 "Out of memory while processing unknown chunk");
1085 to->size = 0;
1086 }
1087 else
1088 png_memcpy(to->data, from->data, from->size);
1089 }
1090 }
1091
1092 info_ptr->unknown_chunks = np;
1093 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001094#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001095 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001096#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001097}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001098void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001099png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
1100 int chunk, int location)
1101{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001102 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001103 (int)info_ptr->unknown_chunks_num)
1104 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1105}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001106#endif
1107
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001108
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001109#if defined(PNG_MNG_FEATURES_SUPPORTED)
1110png_uint_32 PNGAPI
1111png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1112{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001113 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001114
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001115 if (png_ptr == NULL)
1116 return (png_uint_32)0;
1117 png_ptr->mng_features_permitted =
1118 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
1119 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001120}
1121#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001122
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001123#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001124void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001125png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
1126 chunk_list, int num_chunks)
1127{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128 png_bytep new_list, p;
1129 int i, old_num_chunks;
1130 if (png_ptr == NULL)
1131 return;
1132 if (num_chunks == 0)
1133 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001134 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001135 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001136 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001137 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001138
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001139 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001140 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001141 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001142 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001143 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001144 }
1145 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001147 old_num_chunks = png_ptr->num_chunk_list;
1148 new_list=(png_bytep)png_malloc(png_ptr,
1149 (png_size_t)
1150 (5*(num_chunks + old_num_chunks)));
1151 if (png_ptr->chunk_list != NULL)
1152 {
1153 png_memcpy(new_list, png_ptr->chunk_list,
1154 (png_size_t)(5*old_num_chunks));
1155 png_free(png_ptr, png_ptr->chunk_list);
1156 png_ptr->chunk_list=NULL;
1157 }
1158 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
1159 (png_size_t)(5*num_chunks));
1160 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1161 *p=(png_byte)keep;
1162 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1163 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001164#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001165 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001166#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001167}
1168#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001169
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001170#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001171void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001172png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
1173 png_user_chunk_ptr read_user_chunk_fn)
1174{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001175 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001176
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001177 if (png_ptr == NULL)
1178 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001179
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001180 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1181 png_ptr->user_chunk_ptr = user_chunk_ptr;
1182}
1183#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001184
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001185#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001186void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001187png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1188{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001189 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001190
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001191 if (png_ptr == NULL || info_ptr == NULL)
1192 return;
1193
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001194 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001195 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001196 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001197 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001198 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001199}
1200#endif
1201
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001202#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001203void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001204png_set_compression_buffer_size(png_structp png_ptr,
1205 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001206{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001207 if (png_ptr == NULL)
1208 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001209 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001210 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001211 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1212 png_ptr->zstream.next_out = png_ptr->zbuf;
1213 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1214}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001215#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001216
1217void PNGAPI
1218png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1219{
1220 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001221 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001222}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001223
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001224
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001225
1226#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001227/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001228void PNGAPI
1229png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1230 png_uint_32 user_height_max)
1231{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001232 /* Images with dimensions larger than these limits will be
1233 * rejected by png_set_IHDR(). To accept any PNG datastream
1234 * regardless of dimensions, set both limits to 0x7ffffffL.
1235 */
1236 if (png_ptr == NULL)
1237 return;
1238 png_ptr->user_width_max = user_width_max;
1239 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001240}
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001241/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001242void PNGAPI
1243png_set_chunk_cache_max (png_structp png_ptr,
1244 png_uint_32 user_chunk_cache_max)
1245{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001246 if (png_ptr == NULL)
1247 return;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001248 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1249 if (user_chunk_cache_max == 0x7fffffffL) /* Unlimited */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001250 png_ptr->user_chunk_cache_max = 0;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001251 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001252 png_ptr->user_chunk_cache_max = user_chunk_cache_max + 1;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001253}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001254#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1255
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001256
1257#if defined(PNG_BENIGN_ERRORS_SUPPORTED)
1258void PNGAPI
1259png_set_benign_errors(png_structp png_ptr, int allowed)
1260{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001261 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001262
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001263 if (allowed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001264 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001265 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001266 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001267}
1268#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001269#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */