| 1 | |
| 2 | /* pngset.c - storage of image information into info struct |
| 3 | * |
| 4 | * libpng 1.0.3 - January 14, 1999 |
| 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, 1999 Glenn Randers-Pehrson |
| 9 | * |
| 10 | * The functions here are used during reads to store data from the file |
| 11 | * into the info struct, and during writes to store application data |
| 12 | * into the info struct for writing into the file. This abstracts the |
| 13 | * info struct and allows us to change the structure in the future. |
| 14 | */ |
| 15 | |
| 16 | #define PNG_INTERNAL |
| 17 | #include "png.h" |
| 18 | |
| 19 | #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED) |
| 20 | void |
| 21 | png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background) |
| 22 | { |
| 23 | png_debug1(1, "in %s storage function\n", "bKGD"); |
| 24 | if (png_ptr == NULL || info_ptr == NULL) |
| 25 | return; |
| 26 | |
| 27 | png_memcpy(&(info_ptr->background), background, sizeof(png_color_16)); |
| 28 | info_ptr->valid |= PNG_INFO_bKGD; |
| 29 | } |
| 30 | #endif |
| 31 | |
| 32 | #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED) |
| 33 | void |
| 34 | png_set_cHRM(png_structp png_ptr, png_infop info_ptr, |
| 35 | double white_x, double white_y, double red_x, double red_y, |
| 36 | double green_x, double green_y, double blue_x, double blue_y) |
| 37 | { |
| 38 | png_debug1(1, "in %s storage function\n", "cHRM"); |
| 39 | if (png_ptr == NULL || info_ptr == NULL) |
| 40 | return; |
| 41 | |
| 42 | info_ptr->x_white = (float)white_x; |
| 43 | info_ptr->y_white = (float)white_y; |
| 44 | info_ptr->x_red = (float)red_x; |
| 45 | info_ptr->y_red = (float)red_y; |
| 46 | info_ptr->x_green = (float)green_x; |
| 47 | info_ptr->y_green = (float)green_y; |
| 48 | info_ptr->x_blue = (float)blue_x; |
| 49 | info_ptr->y_blue = (float)blue_y; |
| 50 | info_ptr->valid |= PNG_INFO_cHRM; |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED) |
| 55 | void |
| 56 | png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma) |
| 57 | { |
| 58 | png_debug1(1, "in %s storage function\n", "gAMA"); |
| 59 | if (png_ptr == NULL || info_ptr == NULL) |
| 60 | return; |
| 61 | |
| 62 | info_ptr->gamma = (float)file_gamma; |
| 63 | info_ptr->valid |= PNG_INFO_gAMA; |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED) |
| 68 | void |
| 69 | png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist) |
| 70 | { |
| 71 | png_debug1(1, "in %s storage function\n", "hIST"); |
| 72 | if (png_ptr == NULL || info_ptr == NULL) |
| 73 | return; |
| 74 | |
| 75 | info_ptr->hist = hist; |
| 76 | info_ptr->valid |= PNG_INFO_hIST; |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | void |
| 81 | png_set_IHDR(png_structp png_ptr, png_infop info_ptr, |
| 82 | png_uint_32 width, png_uint_32 height, int bit_depth, |
| 83 | int color_type, int interlace_type, int compression_type, |
| 84 | int filter_type) |
| 85 | { |
| 86 | int rowbytes_per_pixel; |
| 87 | png_debug1(1, "in %s storage function\n", "IHDR"); |
| 88 | if (png_ptr == NULL || info_ptr == NULL) |
| 89 | return; |
| 90 | |
| 91 | info_ptr->width = width; |
| 92 | info_ptr->height = height; |
| 93 | info_ptr->bit_depth = (png_byte)bit_depth; |
| 94 | info_ptr->color_type =(png_byte) color_type; |
| 95 | info_ptr->compression_type = (png_byte)compression_type; |
| 96 | info_ptr->filter_type = (png_byte)filter_type; |
| 97 | info_ptr->interlace_type = (png_byte)interlace_type; |
| 98 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 99 | info_ptr->channels = 1; |
| 100 | else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 101 | info_ptr->channels = 3; |
| 102 | else |
| 103 | info_ptr->channels = 1; |
| 104 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 105 | info_ptr->channels++; |
| 106 | info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); |
| 107 | |
| 108 | /* check for overflow */ |
| 109 | rowbytes_per_pixel = (info_ptr->pixel_depth + 7) >> 3; |
| 110 | if (( width > (png_uint_32)2147483647L/rowbytes_per_pixel)) |
| 111 | { |
| 112 | png_warning(png_ptr, |
| 113 | "Width too large to process image data; rowbytes will overflow."); |
| 114 | info_ptr->rowbytes = (png_size_t)0; |
| 115 | } |
| 116 | else |
| 117 | info_ptr->rowbytes = (info_ptr->width * info_ptr->pixel_depth + 7) >> 3; |
| 118 | } |
| 119 | |
| 120 | #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED) |
| 121 | void |
| 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) |
| 124 | { |
| 125 | png_debug1(1, "in %s storage function\n", "oFFs"); |
| 126 | if (png_ptr == NULL || info_ptr == NULL) |
| 127 | return; |
| 128 | |
| 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; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) |
| 137 | void |
| 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) |
| 141 | { |
| 142 | png_uint_32 length; |
| 143 | int i; |
| 144 | |
| 145 | png_debug1(1, "in %s storage function\n", "pCAL"); |
| 146 | if (png_ptr == NULL || info_ptr == NULL) |
| 147 | return; |
| 148 | |
| 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); |
| 153 | |
| 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; |
| 159 | |
| 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); |
| 164 | |
| 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; |
| 168 | |
| 169 | for (i = 0; i < nparams; i++) |
| 170 | { |
| 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); |
| 175 | } |
| 176 | |
| 177 | info_ptr->valid |= PNG_INFO_pCAL; |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED) |
| 182 | void |
| 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) |
| 185 | { |
| 186 | png_debug1(1, "in %s storage function\n", "pHYs"); |
| 187 | if (png_ptr == NULL || info_ptr == NULL) |
| 188 | return; |
| 189 | |
| 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; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | void |
| 198 | png_set_PLTE(png_structp png_ptr, png_infop info_ptr, |
| 199 | png_colorp palette, int num_palette) |
| 200 | { |
| 201 | png_debug1(1, "in %s storage function\n", "PLTE"); |
| 202 | if (png_ptr == NULL || info_ptr == NULL) |
| 203 | return; |
| 204 | |
| 205 | info_ptr->palette = palette; |
| 206 | info_ptr->num_palette = (png_uint_16)num_palette; |
| 207 | info_ptr->valid |= PNG_INFO_PLTE; |
| 208 | } |
| 209 | |
| 210 | #if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED) |
| 211 | void |
| 212 | png_set_sBIT(png_structp png_ptr, png_infop info_ptr, |
| 213 | png_color_8p sig_bit) |
| 214 | { |
| 215 | png_debug1(1, "in %s storage function\n", "sBIT"); |
| 216 | if (png_ptr == NULL || info_ptr == NULL) |
| 217 | return; |
| 218 | |
| 219 | png_memcpy(&(info_ptr->sig_bit), sig_bit, sizeof (png_color_8)); |
| 220 | info_ptr->valid |= PNG_INFO_sBIT; |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED) |
| 225 | void |
| 226 | png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent) |
| 227 | { |
| 228 | png_debug1(1, "in %s storage function\n", "sRGB"); |
| 229 | if (png_ptr == NULL || info_ptr == NULL) |
| 230 | return; |
| 231 | |
| 232 | info_ptr->srgb_intent = (png_byte)intent; |
| 233 | info_ptr->valid |= PNG_INFO_sRGB; |
| 234 | } |
| 235 | void |
| 236 | png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr, |
| 237 | int intent) |
| 238 | { |
| 239 | #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED) |
| 240 | float file_gamma; |
| 241 | #endif |
| 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; |
| 244 | #endif |
| 245 | png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM"); |
| 246 | if (png_ptr == NULL || info_ptr == NULL) |
| 247 | return; |
| 248 | |
| 249 | png_set_sRGB(png_ptr, info_ptr, intent); |
| 250 | |
| 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); |
| 254 | #endif |
| 255 | |
| 256 | #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED) |
| 257 | white_x = (float).3127; |
| 258 | white_y = (float).3290; |
| 259 | red_x = (float).64; |
| 260 | red_y = (float).33; |
| 261 | green_x = (float).30; |
| 262 | green_y = (float).60; |
| 263 | blue_x = (float).15; |
| 264 | blue_y = (float).06; |
| 265 | |
| 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); |
| 268 | |
| 269 | #endif |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \ |
| 274 | defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) |
| 275 | void |
| 276 | png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, |
| 277 | int num_text) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ? |
| 282 | "text" : (png_const_charp)png_ptr->chunk_name)); |
| 283 | |
| 284 | if (png_ptr == NULL || info_ptr == NULL || num_text == 0) |
| 285 | return; |
| 286 | |
| 287 | /* Make sure we have enough space in the "text" array in info_struct |
| 288 | * to hold all of the incoming text_ptr objects. |
| 289 | */ |
| 290 | if (info_ptr->num_text + num_text > info_ptr->max_text) |
| 291 | { |
| 292 | if (info_ptr->text != NULL) |
| 293 | { |
| 294 | png_textp old_text; |
| 295 | int old_max; |
| 296 | |
| 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 * |
| 303 | sizeof(png_text))); |
| 304 | png_free(png_ptr, old_text); |
| 305 | } |
| 306 | else |
| 307 | { |
| 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))); |
| 312 | } |
| 313 | png_debug1(3, "allocated %d entries for info_ptr->text\n", |
| 314 | info_ptr->max_text); |
| 315 | } |
| 316 | |
| 317 | for (i = 0; i < num_text; i++) |
| 318 | { |
| 319 | png_textp textp = &(info_ptr->text[info_ptr->num_text]); |
| 320 | |
| 321 | if (text_ptr[i].text == NULL) |
| 322 | text_ptr[i].text = (png_charp)""; |
| 323 | |
| 324 | if (text_ptr[i].text[0] == '\0') |
| 325 | { |
| 326 | textp->text_length = 0; |
| 327 | textp->compression = PNG_TEXT_COMPRESSION_NONE; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | textp->text_length = png_strlen(text_ptr[i].text); |
| 332 | textp->compression = text_ptr[i].compression; |
| 333 | } |
| 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); |
| 338 | } |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | #if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED) |
| 343 | void |
| 344 | png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time) |
| 345 | { |
| 346 | png_debug1(1, "in %s storage function\n", "tIME"); |
| 347 | if (png_ptr == NULL || info_ptr == NULL) |
| 348 | return; |
| 349 | |
| 350 | png_memcpy(&(info_ptr->mod_time), mod_time, sizeof (png_time)); |
| 351 | info_ptr->valid |= PNG_INFO_tIME; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED) |
| 356 | void |
| 357 | png_set_tRNS(png_structp png_ptr, png_infop info_ptr, |
| 358 | png_bytep trans, int num_trans, png_color_16p trans_values) |
| 359 | { |
| 360 | png_debug1(1, "in %s storage function\n", "tRNS"); |
| 361 | if (png_ptr == NULL || info_ptr == NULL) |
| 362 | return; |
| 363 | |
| 364 | if (trans != NULL) |
| 365 | { |
| 366 | info_ptr->trans = trans; |
| 367 | } |
| 368 | |
| 369 | if (trans_values != NULL) |
| 370 | { |
| 371 | png_memcpy(&(info_ptr->trans_values), trans_values, |
| 372 | sizeof(png_color_16)); |
| 373 | if (num_trans == 0) |
| 374 | num_trans = 1; |
| 375 | } |
| 376 | info_ptr->num_trans = (png_uint_16)num_trans; |
| 377 | info_ptr->valid |= PNG_INFO_tRNS; |
| 378 | } |
| 379 | #endif |
| 380 | |