blob: 85c4bc5fcc46c2234550b09ab68da5012405f584 [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-Pehrson4db34422010-06-25 20:13:41 -05004 * Last changed in libpng 1.5.0 [June 26, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050023#ifdef 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-Pehrsone26c0952009-09-23 11:22:08 -050037#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060038#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,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060041 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)
Andreas Dilger47a0c421997-05-16 02:46:07 -050043{
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
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060058 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);
60 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);
62 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);
64 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-Pehrsonda301352010-03-06 06:42:48 -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-Pehrsone26c0952009-09-23 11:22:08 -050083#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -060084 if (png_check_cHRM_fixed(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060085 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
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500112#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600113#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-Pehrsonda301352010-03-06 06:42:48 -0600128 png_gamma = 21474.83;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600129 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500130
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600131 else
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500132 png_gamma = file_gamma;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500133
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500134 info_ptr->gamma = (float)png_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600135#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500136 info_ptr->int_gamma = (int)(png_gamma*100000.+.5);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600137#endif
138 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500140 if (png_gamma == 0.0)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600141 png_warning(png_ptr, "Setting gamma = 0");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600142}
143#endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500144#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500145void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600146png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600147 int_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600148{
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500149 png_fixed_point png_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600150
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500151 png_debug1(1, "in %s storage function", "gAMA");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500152
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600153 if (png_ptr == NULL || info_ptr == NULL)
154 return;
155
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500156 if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600157 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500158 png_warning(png_ptr, "Limiting gamma to 21474.83");
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600159 png_gamma = PNG_UINT_31_MAX;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600160 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500161
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600162 else
163 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500164 if (int_gamma < 0)
165 {
166 png_warning(png_ptr, "Setting negative gamma to zero");
167 png_gamma = 0;
168 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500169
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500170 else
171 png_gamma = int_gamma;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600172 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600173#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson4db4aff2009-04-20 11:49:24 -0500174 info_ptr->gamma = (float)(png_gamma/100000.);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600175#endif
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500176 info_ptr->int_gamma = png_gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 info_ptr->valid |= PNG_INFO_gAMA;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Glenn Randers-Pehrson8764c252009-04-19 07:52:22 -0500179 if (png_gamma == 0)
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600180 png_warning(png_ptr, "Setting gamma = 0");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500182#endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500183#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500185#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500186void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
188{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500189 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600190
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500191 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500192
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600193 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500195
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500196 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600197 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500198 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600200 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500201
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500202 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500203 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600205 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500206 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
207 * version 1.2.1
208 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500209 png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600210 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500211 if (png_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500212 {
213 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
214 return;
215 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600216
217 for (i = 0; i < info_ptr->num_palette; i++)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500218 png_ptr->hist[i] = hist[i];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500219
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600220 info_ptr->hist = png_ptr->hist;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 info_ptr->valid |= PNG_INFO_hIST;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600222
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600223 info_ptr->free_me |= PNG_FREE_HIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500224}
225#endif
226
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500227void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
229 png_uint_32 width, png_uint_32 height, int bit_depth,
230 int color_type, int interlace_type, int compression_type,
231 int filter_type)
232{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500233 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500234
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600235 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 return;
237
238 info_ptr->width = width;
239 info_ptr->height = height;
240 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500241 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 info_ptr->compression_type = (png_byte)compression_type;
243 info_ptr->filter_type = (png_byte)filter_type;
244 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500245
246 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
247 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
248 info_ptr->compression_type, info_ptr->filter_type);
249
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500250 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
251 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500252
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500253 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500255
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 else
257 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500258
Andreas Dilger47a0c421997-05-16 02:46:07 -0500259 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
260 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500261
Andreas Dilger47a0c421997-05-16 02:46:07 -0500262 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600263
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500264 /* Check for potential overflow */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 if (width > (PNG_UINT_32_MAX
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600266 >> 3) /* 8-byte RRGGBBAA pixels */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500267 - 64 /* bigrowbuf hack */
268 - 1 /* filter byte */
269 - 7*8 /* rounding of width to multiple of 8 pixels */
270 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500271 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500272 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500273 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274}
275
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500276#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500277void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600279 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500281 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500282
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600283 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284 return;
285
286 info_ptr->x_offset = offset_x;
287 info_ptr->y_offset = offset_y;
288 info_ptr->offset_unit_type = (png_byte)unit_type;
289 info_ptr->valid |= PNG_INFO_oFFs;
290}
291#endif
292
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500293#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500294void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600296 png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
297 png_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500299 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600300 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500302 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500303
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600304 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 return;
306
307 length = png_strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500308 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600309 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500310
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500311 info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
312 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500313 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500314 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500315 return;
316 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500317
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500318 png_memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500319
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500320 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 info_ptr->pcal_X0 = X0;
322 info_ptr->pcal_X1 = X1;
323 info_ptr->pcal_type = (png_byte)type;
324 info_ptr->pcal_nparams = (png_byte)nparams;
325
326 length = png_strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500327 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500328 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500329
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500330 info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
331 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500332 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500333 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500334 return;
335 }
336 png_memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500338 info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500339 (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500340
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500341 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500342 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500343 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500344 return;
345 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500346
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600347 png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348
349 for (i = 0; i < nparams; i++)
350 {
351 length = png_strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500352 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500353 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500354
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500355 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500356
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500357 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500359 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
360 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500361 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500362
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500363 png_memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 }
365
366 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500367 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368}
369#endif
370
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500371#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600372#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500373void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600375 int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600376{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500377 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500378
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600379 if (png_ptr == NULL || info_ptr == NULL)
380 return;
381
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600382 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600383 info_ptr->scal_pixel_width = width;
384 info_ptr->scal_pixel_height = height;
385
386 info_ptr->valid |= PNG_INFO_sCAL;
387}
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500388#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600389#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500390void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600391png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600392 int unit, png_charp swidth, png_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600393{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500394 png_size_t length;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500396 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500397
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398 if (png_ptr == NULL || info_ptr == NULL)
399 return;
400
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600401 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600402
403 length = png_strlen(swidth) + 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500404
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500405 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600406 (unsigned int)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500407
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500408 info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500409
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500410 if (info_ptr->scal_s_width == NULL)
411 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500412 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600413 "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500414 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500415 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500416
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500417 png_memcpy(info_ptr->scal_s_width, swidth, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600418
419 length = png_strlen(sheight) + 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500420
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500421 png_debug1(3, "allocating unit for info (%u bytes)",
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500422 (unsigned int)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500423
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500424 info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500425
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500426 if (info_ptr->scal_s_height == NULL)
427 {
428 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500429 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500431 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600432 "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500433
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500434 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500435 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500436
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500437 png_memcpy(info_ptr->scal_s_height, sheight, length);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600438 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500439 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600440}
441#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600442#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600443
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500444#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500445void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600447 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500449 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500450
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600451 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 return;
453
454 info_ptr->x_pixels_per_unit = res_x;
455 info_ptr->y_pixels_per_unit = res_y;
456 info_ptr->phys_unit_type = (png_byte)unit_type;
457 info_ptr->valid |= PNG_INFO_pHYs;
458}
459#endif
460
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500461void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500462png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600463 png_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500464{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600465
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500466 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500467
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600468 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 return;
470
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600471 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500472 {
473 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600474 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500475
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500476 else
477 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600478 png_warning(png_ptr, "Invalid palette length");
479 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500480 }
481 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600482
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600483 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600484 * we do it for backward compatibility with the way the png_handle_tRNS
485 * function used to do the allocation.
486 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600487 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500488
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600489 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500490 * of num_palette entries, in case of an invalid PNG file that has
491 * too-large sample values.
492 */
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600493 png_ptr->palette = (png_colorp)png_calloc(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600494 PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500495
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500496 png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600497 info_ptr->palette = png_ptr->palette;
498 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600499
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600500 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600501
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600502 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503}
504
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500505#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500506void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500507png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600508 png_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500510 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500511
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600512 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 return;
514
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500515 png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500516 info_ptr->valid |= PNG_INFO_sBIT;
517}
518#endif
519
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500520#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500521void PNGAPI
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600522png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600523{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500524 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500525
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600526 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600527 return;
528
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600529 info_ptr->srgb_intent = (png_byte)intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600530 info_ptr->valid |= PNG_INFO_sRGB;
531}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600532
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500533void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600534png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600535 int intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600536{
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500537#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600538#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600539 float file_gamma;
540#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600541#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600542 png_fixed_point int_file_gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600543#endif
544#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500545#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600546#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600547 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
548#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600549 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600550 int_green_y, int_blue_x, int_blue_y;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600551#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500552 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500553
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600554 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600555 return;
556
557 png_set_sRGB(png_ptr, info_ptr, intent);
558
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500559#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600560#ifdef PNG_FIXED_POINT_SUPPORTED
561 int_file_gamma = 45455L;
562 png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500563#else
564 /* Floating point must be set! */
565 file_gamma = (float).45455;
566 png_set_gAMA(png_ptr, info_ptr, file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600567#endif
568#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600569
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500570#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600571 int_white_x = 31270L;
572 int_white_y = 32900L;
573 int_red_x = 64000L;
574 int_red_y = 33000L;
575 int_green_x = 30000L;
576 int_green_y = 60000L;
577 int_blue_x = 15000L;
578 int_blue_y = 6000L;
579
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600580#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600581 white_x = (float).3127;
582 white_y = (float).3290;
583 red_x = (float).64;
584 red_y = (float).33;
585 green_x = (float).30;
586 green_y = (float).60;
587 blue_x = (float).15;
588 blue_y = (float).06;
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600589#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600590
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600591#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson85b02372009-09-24 19:49:13 -0500592 png_set_cHRM_fixed(png_ptr, info_ptr,
593 int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
594 int_green_y, int_blue_x, int_blue_y);
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600595#endif
596#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson85b02372009-09-24 19:49:13 -0500597 png_set_cHRM(png_ptr, info_ptr,
598 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600599#endif
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600600#endif /* cHRM */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600601}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600602#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600604
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500605#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500606void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600607png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600608 png_charp name, int compression_type,
609 png_charp profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600610{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500611 png_charp new_iccp_name;
612 png_charp new_iccp_profile;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500613 png_uint_32 length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500614
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500615 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500616
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600617 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
618 return;
619
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500620 length = png_strlen(name)+1;
621 new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500622 if (new_iccp_name == NULL)
623 {
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600624 png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500625 return;
626 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500627 png_memcpy(new_iccp_name, name, length);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500628 new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500629
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500630 if (new_iccp_profile == NULL)
631 {
632 png_free (png_ptr, new_iccp_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500633 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600634 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500635 return;
636 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500637
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500638 png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
639
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500640 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500641
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600642 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500643 info_ptr->iccp_name = new_iccp_name;
644 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645 /* Compression is always zero but is here so the API and info structure
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600646 * does not have to change if we introduce multiple compression types
647 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600648 info_ptr->iccp_compression = (png_byte)compression_type;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600649 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600650 info_ptr->valid |= PNG_INFO_iCCP;
651}
652#endif
653
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500654#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500655void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500656png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600657 int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500659 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500660 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500661
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500662 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500663 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500664}
665
666int /* PRIVATE */
667png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600668 int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500669{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 int i;
671
Glenn Randers-Pehrsone2a118f2009-07-27 22:08:25 -0500672 png_debug1(1, "in %s storage function", ((png_ptr == NULL ||
673 png_ptr->chunk_name[0] == '\0') ?
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600674 "text" : (png_const_charp)png_ptr->chunk_name));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500675
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600676 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500677 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678
679 /* Make sure we have enough space in the "text" array in info_struct
680 * to hold all of the incoming text_ptr objects.
681 */
682 if (info_ptr->num_text + num_text > info_ptr->max_text)
683 {
684 if (info_ptr->text != NULL)
685 {
686 png_textp old_text;
687 int old_max;
688
689 old_max = info_ptr->max_text;
690 info_ptr->max_text = info_ptr->num_text + num_text + 8;
691 old_text = info_ptr->text;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500692 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500694
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500695 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500696 {
697 png_free(png_ptr, old_text);
698 return(1);
699 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500700
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600701 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600702 png_sizeof(png_text)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 png_free(png_ptr, old_text);
704 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500705
Andreas Dilger47a0c421997-05-16 02:46:07 -0500706 else
707 {
708 info_ptr->max_text = num_text + 8;
709 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500710 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600711 (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500712 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500713 return(1);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500714 info_ptr->free_me |= PNG_FREE_TEXT;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500716
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500717 png_debug1(3, "allocated %d entries for info_ptr->text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600718 info_ptr->max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 for (i = 0; i < num_text; i++)
721 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500722 png_size_t text_length, key_len;
723 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
725
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500726 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600727 continue;
728
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 key_len = png_strlen(text_ptr[i].key);
730
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500731 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500732 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500733 lang_len = 0;
734 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500735 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500736
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500737 else
Glenn Randers-Pehrson946c3f92010-03-30 14:50:07 -0500738#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600739 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500740 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600741
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500742 if (text_ptr[i].lang != NULL)
743 lang_len = png_strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500744
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500745 else
746 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500747
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500748 if (text_ptr[i].lang_key != NULL)
749 lang_key_len = png_strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500750
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500751 else
752 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600753 }
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600754#else /* PNG_iTXt_SUPPORTED */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500755 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500756 png_warning(png_ptr, "iTXt chunk not supported");
757 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500758 }
759#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500760
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500761 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600763 text_length = 0;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500764#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500765 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600766 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500767
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600768 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500769#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600770 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500771 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500772
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773 else
774 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600775 text_length = png_strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500776 textp->compression = text_ptr[i].compression;
777 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500778
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500779 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600780 (png_size_t)
781 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500782
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500783 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500784 return(1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500785
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500786 png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600787 (unsigned long)(png_uint_32)
788 (key_len + lang_len + lang_key_len + text_length + 4),
789 (int)textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500790
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500791 png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600794 if (text_ptr[i].compression > 0)
795 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500796 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600797 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500798 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500799 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600800 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500801 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500802 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600803 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500804
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600805 else
806 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500807 textp->lang=NULL;
808 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500809 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600810 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500811
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500812 if (text_length)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500813 png_memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600814 (png_size_t)(text_length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500815
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500816 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600817
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500818#ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500819 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600820 {
821 textp->text_length = 0;
822 textp->itxt_length = text_length;
823 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500824
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 else
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500826#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600827 {
828 textp->text_length = text_length;
829 textp->itxt_length = 0;
830 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500831
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500833 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500835 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500836}
837#endif
838
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500839#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500840void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500841png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
842{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500843 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500844
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500845 if (png_ptr == NULL || info_ptr == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600846 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 return;
848
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500850 info_ptr->valid |= PNG_INFO_tIME;
851}
852#endif
853
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500854#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500855void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600857 png_bytep trans_alpha, int num_trans, png_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500859 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500860
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600861 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500862 return;
863
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500864 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600865 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500866 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500867 * we do it for backward compatibility with the way the png_handle_tRNS
868 * function used to do the allocation.
869 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500870
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600871 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500872
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600873 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500874 png_ptr->trans_alpha = info_ptr->trans_alpha = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500875 (png_size_t)PNG_MAX_PALETTE_LENGTH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500876
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500877 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500878 png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600879 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500881 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500882 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500883 int sample_max = (1 << info_ptr->bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500884
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500885 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500886 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500887 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500888 ((int)trans_color->red > sample_max ||
889 (int)trans_color->green > sample_max ||
890 (int)trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500891 png_warning(png_ptr,
892 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500893
894 png_memcpy(&(info_ptr->trans_color), trans_color, png_sizeof(png_color_16));
895
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600896 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500897 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500898 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500899
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500901
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500902 if (num_trans != 0)
903 {
904 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500905 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500906 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907}
908#endif
909
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500910#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500911void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600912png_set_sPLT(png_structp png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600913 png_infop info_ptr, png_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600914/*
915 * entries - array of png_sPLT_t structures
916 * to be added to the list of palettes
917 * in the info structure.
918 * nentries - number of palette structures to be
919 * added.
920 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600921{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500922 png_sPLT_tp np;
923 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600924
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500925 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500926 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600927
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500928 np = (png_sPLT_tp)png_malloc_warn(png_ptr,
929 (info_ptr->splt_palettes_num + nentries) *
930 (png_size_t)png_sizeof(png_sPLT_t));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500931
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500932 if (np == NULL)
933 {
934 png_warning(png_ptr, "No memory for sPLT palettes");
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600935 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500936 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500938 png_memcpy(np, info_ptr->splt_palettes,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600939 info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500940
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500941 png_free(png_ptr, info_ptr->splt_palettes);
942 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600943
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500944 for (i = 0; i < nentries; i++)
945 {
946 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
947 png_sPLT_tp from = entries + i;
948 png_uint_32 length;
949
950 length = png_strlen(from->name) + 1;
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600951 to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500952
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500953 if (to->name == NULL)
954 {
955 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600956 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500957 continue;
958 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500959
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500960 png_memcpy(to->name, from->name, length);
961 to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600962 (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500963
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500964 if (to->entries == NULL)
965 {
966 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600967 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500968 png_free(png_ptr, to->name);
969 to->name = NULL;
970 continue;
971 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500972
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500973 png_memcpy(to->entries, from->entries,
974 from->nentries * png_sizeof(png_sPLT_entry));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500975
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500976 to->nentries = from->nentries;
977 to->depth = from->depth;
978 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600979
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500980 info_ptr->splt_palettes = np;
981 info_ptr->splt_palettes_num += nentries;
982 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500983 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600984}
985#endif /* PNG_sPLT_SUPPORTED */
986
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -0600987#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500988void PNGAPI
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989png_set_unknown_chunks(png_structp png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600990 png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600991{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500992 png_unknown_chunkp np;
993 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500995 if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500996 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500998 np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
999 (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *
1000 png_sizeof(png_unknown_chunk)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001001
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001002 if (np == NULL)
1003 {
1004 png_warning(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001005 "Out of memory while processing unknown chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001006 return;
1007 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001009 png_memcpy(np, info_ptr->unknown_chunks,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001010 info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001011
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001012 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001013 info_ptr->unknown_chunks = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001015 for (i = 0; i < num_unknowns; i++)
1016 {
1017 png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
1018 png_unknown_chunkp from = unknowns + i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001019
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001020 png_memcpy((png_charp)to->name, (png_charp)from->name,
1021 png_sizeof(from->name));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001022 to->name[png_sizeof(to->name)-1] = '\0';
1023 to->size = from->size;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001024 /* Note our location in the read or write sequence */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001025 to->location = (png_byte)(png_ptr->mode & 0xff);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001026
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 if (from->size == 0)
1028 to->data=NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001029
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001030 else
1031 {
1032 to->data = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001033 (png_size_t)from->size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001034
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001035 if (to->data == NULL)
1036 {
1037 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001038 "Out of memory while processing unknown chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039 to->size = 0;
1040 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001041
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042 else
1043 png_memcpy(to->data, from->data, from->size);
1044 }
1045 }
1046
1047 info_ptr->unknown_chunks = np;
1048 info_ptr->unknown_chunks_num += num_unknowns;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001049 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001050}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001051void PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001052png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001053 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001054{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001055 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001056 (int)info_ptr->unknown_chunks_num)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001057 info_ptr->unknown_chunks[chunk].location = (png_byte)location;
1058}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001059#endif
1060
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001061
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001062#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001063png_uint_32 PNGAPI
1064png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
1065{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001066 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001067
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001068 if (png_ptr == NULL)
1069 return (png_uint_32)0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001070
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001071 png_ptr->mng_features_permitted =
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001072 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001073
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001074 return (png_uint_32)png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001075}
1076#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001077
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001078#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001079void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001080png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001081 chunk_list, int num_chunks)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001082{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001083 png_bytep new_list, p;
1084 int i, old_num_chunks;
1085 if (png_ptr == NULL)
1086 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001087
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001088 if (num_chunks == 0)
1089 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091 png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001092
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001093 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001094 png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001095
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001096 if (keep == PNG_HANDLE_CHUNK_ALWAYS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001097 png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001098
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001099 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100 png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001101
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001102 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001103 }
1104 if (chunk_list == NULL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001105 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001106 old_num_chunks = png_ptr->num_chunk_list;
1107 new_list=(png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001108 (png_size_t)
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001109 (5*(num_chunks + old_num_chunks)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001110
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001111 if (png_ptr->chunk_list != NULL)
1112 {
1113 png_memcpy(new_list, png_ptr->chunk_list,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001114 (png_size_t)(5*old_num_chunks));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 png_free(png_ptr, png_ptr->chunk_list);
1116 png_ptr->chunk_list=NULL;
1117 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001118
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001119 png_memcpy(new_list + 5*old_num_chunks, chunk_list,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001120 (png_size_t)(5*num_chunks));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001121
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001122 for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
1123 *p=(png_byte)keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001124
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001125 png_ptr->num_chunk_list = old_num_chunks + num_chunks;
1126 png_ptr->chunk_list = new_list;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001127 png_ptr->free_me |= PNG_FREE_LIST;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001128}
1129#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001130
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001131#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001132void PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001133png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001134 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001135{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001136 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001137
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001138 if (png_ptr == NULL)
1139 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001140
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001141 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1142 png_ptr->user_chunk_ptr = user_chunk_ptr;
1143}
1144#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001145
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001146#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001147void PNGAPI
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001148png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
1149{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001150 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001151
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001152 if (png_ptr == NULL || info_ptr == NULL)
1153 return;
1154
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001155 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001156 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001157
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001158 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001159
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001160 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001161 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001162}
1163#endif
1164
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001165void PNGAPI
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001166png_set_compression_buffer_size(png_structp png_ptr,
1167 png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001168{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001169 if (png_ptr == NULL)
1170 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001171
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001172 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001173 png_ptr->zbuf_size = size;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001174 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
1175 png_ptr->zstream.next_out = png_ptr->zbuf;
1176 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1177}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001178
1179void PNGAPI
1180png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
1181{
1182 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001183 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001184}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001185
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001186
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001187
1188#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001189/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001190void PNGAPI
1191png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
1192 png_uint_32 user_height_max)
1193{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001194 /* Images with dimensions larger than these limits will be
1195 * rejected by png_set_IHDR(). To accept any PNG datastream
1196 * regardless of dimensions, set both limits to 0x7ffffffL.
1197 */
1198 if (png_ptr == NULL)
1199 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001200
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001201 png_ptr->user_width_max = user_width_max;
1202 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001203}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001204
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001205/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001206void PNGAPI
1207png_set_chunk_cache_max (png_structp png_ptr,
1208 png_uint_32 user_chunk_cache_max)
1209{
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001210 if (png_ptr)
1211 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1212}
1213
1214/* This function was added to libpng 1.4.1 */
1215void PNGAPI
1216png_set_chunk_malloc_max (png_structp png_ptr,
1217 png_alloc_size_t user_chunk_malloc_max)
1218{
1219 if (png_ptr)
1220 png_ptr->user_chunk_malloc_max =
1221 (png_size_t)user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001222}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001223#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1224
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001225
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001226#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001227void PNGAPI
1228png_set_benign_errors(png_structp png_ptr, int allowed)
1229{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001230 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001231
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001232 if (allowed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001233 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001234
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001235 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001236 png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001237}
1238#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001239#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */