2 /* pngset.c - storage of image information into info struct
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, Glenn Randers-Pehrson
11 * The functions here are used during reads to store data from the file
12 * into the info struct, and during writes to store application data
13 * into the info struct for writing into the file. This abstracts the
14 * info struct and allows us to change the structure in the future.
18 #include "../png/png.h"
20 #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
22 png_set_bKGD(png_structp png_ptr
, png_infop info_ptr
, png_color_16p background
)
24 png_debug1(1, "in %s storage function\n", "bKGD");
25 if (png_ptr
== NULL
|| info_ptr
== NULL
)
28 png_memcpy(&(info_ptr
->background
), background
, sizeof(png_color_16
));
29 info_ptr
->valid
|= PNG_INFO_bKGD
;
33 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
35 png_set_cHRM(png_structp png_ptr
, png_infop info_ptr
,
36 double white_x
, double white_y
, double red_x
, double red_y
,
37 double green_x
, double green_y
, double blue_x
, double blue_y
)
39 png_debug1(1, "in %s storage function\n", "cHRM");
40 if (png_ptr
== NULL
|| info_ptr
== NULL
)
43 info_ptr
->x_white
= (float)white_x
;
44 info_ptr
->y_white
= (float)white_y
;
45 info_ptr
->x_red
= (float)red_x
;
46 info_ptr
->y_red
= (float)red_y
;
47 info_ptr
->x_green
= (float)green_x
;
48 info_ptr
->y_green
= (float)green_y
;
49 info_ptr
->x_blue
= (float)blue_x
;
50 info_ptr
->y_blue
= (float)blue_y
;
51 info_ptr
->valid
|= PNG_INFO_cHRM
;
55 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
57 png_set_gAMA(png_structp png_ptr
, png_infop info_ptr
, double file_gamma
)
59 png_debug1(1, "in %s storage function\n", "gAMA");
60 if (png_ptr
== NULL
|| info_ptr
== NULL
)
63 info_ptr
->gamma
= (float)file_gamma
;
64 info_ptr
->valid
|= PNG_INFO_gAMA
;
68 #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
70 png_set_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_16p hist
)
72 png_debug1(1, "in %s storage function\n", "hIST");
73 if (png_ptr
== NULL
|| info_ptr
== NULL
)
76 info_ptr
->hist
= hist
;
77 info_ptr
->valid
|= PNG_INFO_hIST
;
82 png_set_IHDR(png_structp png_ptr
, png_infop info_ptr
,
83 png_uint_32 width
, png_uint_32 height
, int bit_depth
,
84 int color_type
, int interlace_type
, int compression_type
,
87 int rowbytes_per_pixel
;
88 png_debug1(1, "in %s storage function\n", "IHDR");
89 if (png_ptr
== NULL
|| info_ptr
== NULL
)
92 info_ptr
->width
= width
;
93 info_ptr
->height
= height
;
94 info_ptr
->bit_depth
= (png_byte
)bit_depth
;
95 info_ptr
->color_type
=(png_byte
) color_type
;
96 info_ptr
->compression_type
= (png_byte
)compression_type
;
97 info_ptr
->filter_type
= (png_byte
)filter_type
;
98 info_ptr
->interlace_type
= (png_byte
)interlace_type
;
99 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
100 info_ptr
->channels
= 1;
101 else if (info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
102 info_ptr
->channels
= 3;
104 info_ptr
->channels
= 1;
105 if (info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)
106 info_ptr
->channels
++;
107 info_ptr
->pixel_depth
= (png_byte
)(info_ptr
->channels
* info_ptr
->bit_depth
);
109 /* check for overflow */
110 rowbytes_per_pixel
= (info_ptr
->pixel_depth
+ 7) >> 3;
111 info_ptr
->rowbytes
= info_ptr
->width
* rowbytes_per_pixel
;
112 if (( width
> (png_uint_32
)2147483647L/rowbytes_per_pixel
))
115 "Width too large to process image data; rowbytes will overflow.");
116 info_ptr
->rowbytes
= (png_size_t
)0;
120 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
122 png_set_oFFs(png_structp png_ptr
, png_infop info_ptr
,
123 png_uint_32 offset_x
, png_uint_32 offset_y
, int unit_type
)
125 png_debug1(1, "in %s storage function\n", "oFFs");
126 if (png_ptr
== NULL
|| info_ptr
== NULL
)
129 info_ptr
->x_offset
= offset_x
;
130 info_ptr
->y_offset
= offset_y
;
131 info_ptr
->offset_unit_type
= (png_byte
)unit_type
;
132 info_ptr
->valid
|= PNG_INFO_oFFs
;
136 #if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED)
138 png_set_pCAL(png_structp png_ptr
, png_infop info_ptr
,
139 png_charp purpose
, png_int_32 X0
, png_int_32 X1
, int type
, int nparams
,
140 png_charp units
, png_charpp params
)
145 png_debug1(1, "in %s storage function\n", "pCAL");
146 if (png_ptr
== NULL
|| info_ptr
== NULL
)
149 length
= png_strlen(purpose
) + 1;
150 png_debug1(3, "allocating purpose for info (%d bytes)\n", length
);
151 info_ptr
->pcal_purpose
= (png_charp
)png_malloc(png_ptr
, length
);
152 png_memcpy(info_ptr
->pcal_purpose
, purpose
, (png_size_t
)length
);
154 png_debug(3, "storing X0, X1, type, and nparams in info\n");
155 info_ptr
->pcal_X0
= X0
;
156 info_ptr
->pcal_X1
= X1
;
157 info_ptr
->pcal_type
= (png_byte
)type
;
158 info_ptr
->pcal_nparams
= (png_byte
)nparams
;
160 length
= png_strlen(units
) + 1;
161 png_debug1(3, "allocating units for info (%d bytes)\n", length
);
162 info_ptr
->pcal_units
= (png_charp
)png_malloc(png_ptr
, length
);
163 png_memcpy(info_ptr
->pcal_units
, units
, (png_size_t
)length
);
165 info_ptr
->pcal_params
= (png_charpp
)png_malloc(png_ptr
,
166 (png_uint_32
)((nparams
+ 1) * sizeof(png_charp
)));
167 info_ptr
->pcal_params
[nparams
] = NULL
;
169 for (i
= 0; i
< nparams
; i
++)
171 length
= png_strlen(params
[i
]) + 1;
172 png_debug2(3, "allocating parameter %d for info (%d bytes)\n", i
, length
);
173 info_ptr
->pcal_params
[i
] = (png_charp
)png_malloc(png_ptr
, length
);
174 png_memcpy(info_ptr
->pcal_params
[i
], params
[i
], (png_size_t
)length
);
177 info_ptr
->valid
|= PNG_INFO_pCAL
;
181 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
183 png_set_pHYs(png_structp png_ptr
, png_infop info_ptr
,
184 png_uint_32 res_x
, png_uint_32 res_y
, int unit_type
)
186 png_debug1(1, "in %s storage function\n", "pHYs");
187 if (png_ptr
== NULL
|| info_ptr
== NULL
)
190 info_ptr
->x_pixels_per_unit
= res_x
;
191 info_ptr
->y_pixels_per_unit
= res_y
;
192 info_ptr
->phys_unit_type
= (png_byte
)unit_type
;
193 info_ptr
->valid
|= PNG_INFO_pHYs
;
198 png_set_PLTE(png_structp png_ptr
, png_infop info_ptr
,
199 png_colorp palette
, int num_palette
)
201 png_debug1(1, "in %s storage function\n", "PLTE");
202 if (png_ptr
== NULL
|| info_ptr
== NULL
)
205 info_ptr
->palette
= palette
;
206 info_ptr
->num_palette
= (png_uint_16
)num_palette
;
207 info_ptr
->valid
|= PNG_INFO_PLTE
;
210 #if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
212 png_set_sBIT(png_structp png_ptr
, png_infop info_ptr
,
213 png_color_8p sig_bit
)
215 png_debug1(1, "in %s storage function\n", "sBIT");
216 if (png_ptr
== NULL
|| info_ptr
== NULL
)
219 png_memcpy(&(info_ptr
->sig_bit
), sig_bit
, sizeof (png_color_8
));
220 info_ptr
->valid
|= PNG_INFO_sBIT
;
224 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
226 png_set_sRGB(png_structp png_ptr
, png_infop info_ptr
, int intent
)
228 png_debug1(1, "in %s storage function\n", "sRGB");
229 if (png_ptr
== NULL
|| info_ptr
== NULL
)
232 info_ptr
->srgb_intent
= (png_byte
)intent
;
233 info_ptr
->valid
|= PNG_INFO_sRGB
;
236 png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr
, png_infop info_ptr
,
239 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
242 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
243 float white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
;
245 png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
246 if (png_ptr
== NULL
|| info_ptr
== NULL
)
249 png_set_sRGB(png_ptr
, info_ptr
, intent
);
251 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
252 file_gamma
= (float).45;
253 png_set_gAMA(png_ptr
, info_ptr
, file_gamma
);
256 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
257 white_x
= (float).3127;
258 white_y
= (float).3290;
261 green_x
= (float).30;
262 green_y
= (float).60;
266 png_set_cHRM(png_ptr
, info_ptr
,
267 white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
, blue_y
);
273 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
274 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
276 png_set_text(png_structp png_ptr
, png_infop info_ptr
, png_textp text_ptr
,
281 png_debug1(1, "in %s storage function\n", (png_ptr
->chunk_name
[0] == '\0' ?
282 "text" : (png_const_charp
)png_ptr
->chunk_name
));
284 if (png_ptr
== NULL
|| info_ptr
== NULL
|| num_text
== 0)
287 /* Make sure we have enough space in the "text" array in info_struct
288 * to hold all of the incoming text_ptr objects.
290 if (info_ptr
->num_text
+ num_text
> info_ptr
->max_text
)
292 if (info_ptr
->text
!= NULL
)
297 old_max
= info_ptr
->max_text
;
298 info_ptr
->max_text
= info_ptr
->num_text
+ num_text
+ 8;
299 old_text
= info_ptr
->text
;
300 info_ptr
->text
= (png_textp
)png_malloc(png_ptr
,
301 (png_uint_32
)(info_ptr
->max_text
* sizeof (png_text
)));
302 png_memcpy(info_ptr
->text
, old_text
, (png_size_t
)(old_max
*
304 png_free(png_ptr
, old_text
);
308 info_ptr
->max_text
= num_text
+ 8;
309 info_ptr
->num_text
= 0;
310 info_ptr
->text
= (png_textp
)png_malloc(png_ptr
,
311 (png_uint_32
)(info_ptr
->max_text
* sizeof (png_text
)));
313 png_debug1(3, "allocated %d entries for info_ptr->text\n",
317 for (i
= 0; i
< num_text
; i
++)
319 png_textp textp
= &(info_ptr
->text
[info_ptr
->num_text
]);
321 if (text_ptr
[i
].text
== NULL
)
322 text_ptr
[i
].text
= (png_charp
)"";
324 if (text_ptr
[i
].text
[0] == '\0')
326 textp
->text_length
= 0;
327 textp
->compression
= PNG_TEXT_COMPRESSION_NONE
;
331 textp
->text_length
= png_strlen(text_ptr
[i
].text
);
332 textp
->compression
= text_ptr
[i
].compression
;
334 textp
->text
= text_ptr
[i
].text
;
335 textp
->key
= text_ptr
[i
].key
;
336 info_ptr
->num_text
++;
337 png_debug1(3, "transferred text chunk %d\n", info_ptr
->num_text
);
342 #if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
344 png_set_tIME(png_structp png_ptr
, png_infop info_ptr
, png_timep mod_time
)
346 png_debug1(1, "in %s storage function\n", "tIME");
347 if (png_ptr
== NULL
|| info_ptr
== NULL
)
350 png_memcpy(&(info_ptr
->mod_time
), mod_time
, sizeof (png_time
));
351 info_ptr
->valid
|= PNG_INFO_tIME
;
355 #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
357 png_set_tRNS(png_structp png_ptr
, png_infop info_ptr
,
358 png_bytep trans
, int num_trans
, png_color_16p trans_values
)
360 png_debug1(1, "in %s storage function\n", "tRNS");
361 if (png_ptr
== NULL
|| info_ptr
== NULL
)
366 info_ptr
->trans
= trans
;
369 if (trans_values
!= NULL
)
371 png_memcpy(&(info_ptr
->trans_values
), trans_values
,
372 sizeof(png_color_16
));
376 info_ptr
->num_trans
= (png_uint_16
)num_trans
;
377 info_ptr
->valid
|= PNG_INFO_tRNS
;