3dphotoshoot

annotate libs/libpng/png.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
rev   line source
nuclear@14 1
nuclear@14 2 /* png.c - location for general purpose libpng functions
nuclear@14 3 *
nuclear@14 4 * Last changed in libpng 1.2.30 [August 15, 2008]
nuclear@14 5 * For conditions of distribution and use, see copyright notice in png.h
nuclear@14 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
nuclear@14 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
nuclear@14 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
nuclear@14 9 */
nuclear@14 10
nuclear@14 11 #define PNG_INTERNAL
nuclear@14 12 #define PNG_NO_EXTERN
nuclear@14 13 #include "png.h"
nuclear@14 14
nuclear@14 15 /* Generate a compiler error if there is an old png.h in the search path. */
nuclear@14 16 typedef version_1_2_33 Your_png_h_is_not_version_1_2_33;
nuclear@14 17
nuclear@14 18 /* Version information for C files. This had better match the version
nuclear@14 19 * string defined in png.h. */
nuclear@14 20
nuclear@14 21 #ifdef PNG_USE_GLOBAL_ARRAYS
nuclear@14 22 /* png_libpng_ver was changed to a function in version 1.0.5c */
nuclear@14 23 PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
nuclear@14 24
nuclear@14 25 #ifdef PNG_READ_SUPPORTED
nuclear@14 26
nuclear@14 27 /* png_sig was changed to a function in version 1.0.5c */
nuclear@14 28 /* Place to hold the signature string for a PNG file. */
nuclear@14 29 PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
nuclear@14 30 #endif /* PNG_READ_SUPPORTED */
nuclear@14 31
nuclear@14 32 /* Invoke global declarations for constant strings for known chunk types */
nuclear@14 33 PNG_IHDR;
nuclear@14 34 PNG_IDAT;
nuclear@14 35 PNG_IEND;
nuclear@14 36 PNG_PLTE;
nuclear@14 37 PNG_bKGD;
nuclear@14 38 PNG_cHRM;
nuclear@14 39 PNG_gAMA;
nuclear@14 40 PNG_hIST;
nuclear@14 41 PNG_iCCP;
nuclear@14 42 PNG_iTXt;
nuclear@14 43 PNG_oFFs;
nuclear@14 44 PNG_pCAL;
nuclear@14 45 PNG_sCAL;
nuclear@14 46 PNG_pHYs;
nuclear@14 47 PNG_sBIT;
nuclear@14 48 PNG_sPLT;
nuclear@14 49 PNG_sRGB;
nuclear@14 50 PNG_tEXt;
nuclear@14 51 PNG_tIME;
nuclear@14 52 PNG_tRNS;
nuclear@14 53 PNG_zTXt;
nuclear@14 54
nuclear@14 55 #ifdef PNG_READ_SUPPORTED
nuclear@14 56 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@14 57
nuclear@14 58 /* start of interlace block */
nuclear@14 59 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
nuclear@14 60
nuclear@14 61 /* offset to next interlace block */
nuclear@14 62 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@14 63
nuclear@14 64 /* start of interlace block in the y direction */
nuclear@14 65 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
nuclear@14 66
nuclear@14 67 /* offset to next interlace block in the y direction */
nuclear@14 68 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
nuclear@14 69
nuclear@14 70 /* Height of interlace block. This is not currently used - if you need
nuclear@14 71 * it, uncomment it here and in png.h
nuclear@14 72 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@14 73 */
nuclear@14 74
nuclear@14 75 /* Mask to determine which pixels are valid in a pass */
nuclear@14 76 PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
nuclear@14 77
nuclear@14 78 /* Mask to determine which pixels to overwrite while displaying */
nuclear@14 79 PNG_CONST int FARDATA png_pass_dsp_mask[]
nuclear@14 80 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
nuclear@14 81
nuclear@14 82 #endif /* PNG_READ_SUPPORTED */
nuclear@14 83 #endif /* PNG_USE_GLOBAL_ARRAYS */
nuclear@14 84
nuclear@14 85 /* Tells libpng that we have already handled the first "num_bytes" bytes
nuclear@14 86 * of the PNG file signature. If the PNG data is embedded into another
nuclear@14 87 * stream we can set num_bytes = 8 so that libpng will not attempt to read
nuclear@14 88 * or write any of the magic bytes before it starts on the IHDR.
nuclear@14 89 */
nuclear@14 90
nuclear@14 91 #ifdef PNG_READ_SUPPORTED
nuclear@14 92 void PNGAPI
nuclear@14 93 png_set_sig_bytes(png_structp png_ptr, int num_bytes)
nuclear@14 94 {
nuclear@14 95 if (png_ptr == NULL) return;
nuclear@14 96 png_debug(1, "in png_set_sig_bytes\n");
nuclear@14 97 if (num_bytes > 8)
nuclear@14 98 png_error(png_ptr, "Too many bytes for PNG signature.");
nuclear@14 99
nuclear@14 100 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
nuclear@14 101 }
nuclear@14 102
nuclear@14 103 /* Checks whether the supplied bytes match the PNG signature. We allow
nuclear@14 104 * checking less than the full 8-byte signature so that those apps that
nuclear@14 105 * already read the first few bytes of a file to determine the file type
nuclear@14 106 * can simply check the remaining bytes for extra assurance. Returns
nuclear@14 107 * an integer less than, equal to, or greater than zero if sig is found,
nuclear@14 108 * respectively, to be less than, to match, or be greater than the correct
nuclear@14 109 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
nuclear@14 110 */
nuclear@14 111 int PNGAPI
nuclear@14 112 png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
nuclear@14 113 {
nuclear@14 114 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
nuclear@14 115 if (num_to_check > 8)
nuclear@14 116 num_to_check = 8;
nuclear@14 117 else if (num_to_check < 1)
nuclear@14 118 return (-1);
nuclear@14 119
nuclear@14 120 if (start > 7)
nuclear@14 121 return (-1);
nuclear@14 122
nuclear@14 123 if (start + num_to_check > 8)
nuclear@14 124 num_to_check = 8 - start;
nuclear@14 125
nuclear@14 126 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
nuclear@14 127 }
nuclear@14 128
nuclear@14 129 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
nuclear@14 130 /* (Obsolete) function to check signature bytes. It does not allow one
nuclear@14 131 * to check a partial signature. This function might be removed in the
nuclear@14 132 * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
nuclear@14 133 */
nuclear@14 134 int PNGAPI
nuclear@14 135 png_check_sig(png_bytep sig, int num)
nuclear@14 136 {
nuclear@14 137 return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
nuclear@14 138 }
nuclear@14 139 #endif
nuclear@14 140 #endif /* PNG_READ_SUPPORTED */
nuclear@14 141
nuclear@14 142 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@14 143 /* Function to allocate memory for zlib and clear it to 0. */
nuclear@14 144 #ifdef PNG_1_0_X
nuclear@14 145 voidpf PNGAPI
nuclear@14 146 #else
nuclear@14 147 voidpf /* private */
nuclear@14 148 #endif
nuclear@14 149 png_zalloc(voidpf png_ptr, uInt items, uInt size)
nuclear@14 150 {
nuclear@14 151 png_voidp ptr;
nuclear@14 152 png_structp p=(png_structp)png_ptr;
nuclear@14 153 png_uint_32 save_flags=p->flags;
nuclear@14 154 png_uint_32 num_bytes;
nuclear@14 155
nuclear@14 156 if (png_ptr == NULL) return (NULL);
nuclear@14 157 if (items > PNG_UINT_32_MAX/size)
nuclear@14 158 {
nuclear@14 159 png_warning (p, "Potential overflow in png_zalloc()");
nuclear@14 160 return (NULL);
nuclear@14 161 }
nuclear@14 162 num_bytes = (png_uint_32)items * size;
nuclear@14 163
nuclear@14 164 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
nuclear@14 165 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
nuclear@14 166 p->flags=save_flags;
nuclear@14 167
nuclear@14 168 #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
nuclear@14 169 if (ptr == NULL)
nuclear@14 170 return ((voidpf)ptr);
nuclear@14 171
nuclear@14 172 if (num_bytes > (png_uint_32)0x8000L)
nuclear@14 173 {
nuclear@14 174 png_memset(ptr, 0, (png_size_t)0x8000L);
nuclear@14 175 png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
nuclear@14 176 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
nuclear@14 177 }
nuclear@14 178 else
nuclear@14 179 {
nuclear@14 180 png_memset(ptr, 0, (png_size_t)num_bytes);
nuclear@14 181 }
nuclear@14 182 #endif
nuclear@14 183 return ((voidpf)ptr);
nuclear@14 184 }
nuclear@14 185
nuclear@14 186 /* function to free memory for zlib */
nuclear@14 187 #ifdef PNG_1_0_X
nuclear@14 188 void PNGAPI
nuclear@14 189 #else
nuclear@14 190 void /* private */
nuclear@14 191 #endif
nuclear@14 192 png_zfree(voidpf png_ptr, voidpf ptr)
nuclear@14 193 {
nuclear@14 194 png_free((png_structp)png_ptr, (png_voidp)ptr);
nuclear@14 195 }
nuclear@14 196
nuclear@14 197 /* Reset the CRC variable to 32 bits of 1's. Care must be taken
nuclear@14 198 * in case CRC is > 32 bits to leave the top bits 0.
nuclear@14 199 */
nuclear@14 200 void /* PRIVATE */
nuclear@14 201 png_reset_crc(png_structp png_ptr)
nuclear@14 202 {
nuclear@14 203 png_ptr->crc = crc32(0, Z_NULL, 0);
nuclear@14 204 }
nuclear@14 205
nuclear@14 206 /* Calculate the CRC over a section of data. We can only pass as
nuclear@14 207 * much data to this routine as the largest single buffer size. We
nuclear@14 208 * also check that this data will actually be used before going to the
nuclear@14 209 * trouble of calculating it.
nuclear@14 210 */
nuclear@14 211 void /* PRIVATE */
nuclear@14 212 png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
nuclear@14 213 {
nuclear@14 214 int need_crc = 1;
nuclear@14 215
nuclear@14 216 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
nuclear@14 217 {
nuclear@14 218 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
nuclear@14 219 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
nuclear@14 220 need_crc = 0;
nuclear@14 221 }
nuclear@14 222 else /* critical */
nuclear@14 223 {
nuclear@14 224 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
nuclear@14 225 need_crc = 0;
nuclear@14 226 }
nuclear@14 227
nuclear@14 228 if (need_crc)
nuclear@14 229 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
nuclear@14 230 }
nuclear@14 231
nuclear@14 232 /* Allocate the memory for an info_struct for the application. We don't
nuclear@14 233 * really need the png_ptr, but it could potentially be useful in the
nuclear@14 234 * future. This should be used in favour of malloc(png_sizeof(png_info))
nuclear@14 235 * and png_info_init() so that applications that want to use a shared
nuclear@14 236 * libpng don't have to be recompiled if png_info changes size.
nuclear@14 237 */
nuclear@14 238 png_infop PNGAPI
nuclear@14 239 png_create_info_struct(png_structp png_ptr)
nuclear@14 240 {
nuclear@14 241 png_infop info_ptr;
nuclear@14 242
nuclear@14 243 png_debug(1, "in png_create_info_struct\n");
nuclear@14 244 if (png_ptr == NULL) return (NULL);
nuclear@14 245 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@14 246 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
nuclear@14 247 png_ptr->malloc_fn, png_ptr->mem_ptr);
nuclear@14 248 #else
nuclear@14 249 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
nuclear@14 250 #endif
nuclear@14 251 if (info_ptr != NULL)
nuclear@14 252 png_info_init_3(&info_ptr, png_sizeof(png_info));
nuclear@14 253
nuclear@14 254 return (info_ptr);
nuclear@14 255 }
nuclear@14 256
nuclear@14 257 /* This function frees the memory associated with a single info struct.
nuclear@14 258 * Normally, one would use either png_destroy_read_struct() or
nuclear@14 259 * png_destroy_write_struct() to free an info struct, but this may be
nuclear@14 260 * useful for some applications.
nuclear@14 261 */
nuclear@14 262 void PNGAPI
nuclear@14 263 png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
nuclear@14 264 {
nuclear@14 265 png_infop info_ptr = NULL;
nuclear@14 266 if (png_ptr == NULL) return;
nuclear@14 267
nuclear@14 268 png_debug(1, "in png_destroy_info_struct\n");
nuclear@14 269 if (info_ptr_ptr != NULL)
nuclear@14 270 info_ptr = *info_ptr_ptr;
nuclear@14 271
nuclear@14 272 if (info_ptr != NULL)
nuclear@14 273 {
nuclear@14 274 png_info_destroy(png_ptr, info_ptr);
nuclear@14 275
nuclear@14 276 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@14 277 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
nuclear@14 278 png_ptr->mem_ptr);
nuclear@14 279 #else
nuclear@14 280 png_destroy_struct((png_voidp)info_ptr);
nuclear@14 281 #endif
nuclear@14 282 *info_ptr_ptr = NULL;
nuclear@14 283 }
nuclear@14 284 }
nuclear@14 285
nuclear@14 286 /* Initialize the info structure. This is now an internal function (0.89)
nuclear@14 287 * and applications using it are urged to use png_create_info_struct()
nuclear@14 288 * instead.
nuclear@14 289 */
nuclear@14 290 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
nuclear@14 291 #undef png_info_init
nuclear@14 292 void PNGAPI
nuclear@14 293 png_info_init(png_infop info_ptr)
nuclear@14 294 {
nuclear@14 295 /* We only come here via pre-1.0.12-compiled applications */
nuclear@14 296 png_info_init_3(&info_ptr, 0);
nuclear@14 297 }
nuclear@14 298 #endif
nuclear@14 299
nuclear@14 300 void PNGAPI
nuclear@14 301 png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
nuclear@14 302 {
nuclear@14 303 png_infop info_ptr = *ptr_ptr;
nuclear@14 304
nuclear@14 305 if (info_ptr == NULL) return;
nuclear@14 306
nuclear@14 307 png_debug(1, "in png_info_init_3\n");
nuclear@14 308
nuclear@14 309 if (png_sizeof(png_info) > png_info_struct_size)
nuclear@14 310 {
nuclear@14 311 png_destroy_struct(info_ptr);
nuclear@14 312 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
nuclear@14 313 *ptr_ptr = info_ptr;
nuclear@14 314 }
nuclear@14 315
nuclear@14 316 /* set everything to 0 */
nuclear@14 317 png_memset(info_ptr, 0, png_sizeof(png_info));
nuclear@14 318 }
nuclear@14 319
nuclear@14 320 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 321 void PNGAPI
nuclear@14 322 png_data_freer(png_structp png_ptr, png_infop info_ptr,
nuclear@14 323 int freer, png_uint_32 mask)
nuclear@14 324 {
nuclear@14 325 png_debug(1, "in png_data_freer\n");
nuclear@14 326 if (png_ptr == NULL || info_ptr == NULL)
nuclear@14 327 return;
nuclear@14 328 if (freer == PNG_DESTROY_WILL_FREE_DATA)
nuclear@14 329 info_ptr->free_me |= mask;
nuclear@14 330 else if (freer == PNG_USER_WILL_FREE_DATA)
nuclear@14 331 info_ptr->free_me &= ~mask;
nuclear@14 332 else
nuclear@14 333 png_warning(png_ptr,
nuclear@14 334 "Unknown freer parameter in png_data_freer.");
nuclear@14 335 }
nuclear@14 336 #endif
nuclear@14 337
nuclear@14 338 void PNGAPI
nuclear@14 339 png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
nuclear@14 340 int num)
nuclear@14 341 {
nuclear@14 342 png_debug(1, "in png_free_data\n");
nuclear@14 343 if (png_ptr == NULL || info_ptr == NULL)
nuclear@14 344 return;
nuclear@14 345
nuclear@14 346 #if defined(PNG_TEXT_SUPPORTED)
nuclear@14 347 /* free text item num or (if num == -1) all text items */
nuclear@14 348 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 349 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
nuclear@14 350 #else
nuclear@14 351 if (mask & PNG_FREE_TEXT)
nuclear@14 352 #endif
nuclear@14 353 {
nuclear@14 354 if (num != -1)
nuclear@14 355 {
nuclear@14 356 if (info_ptr->text && info_ptr->text[num].key)
nuclear@14 357 {
nuclear@14 358 png_free(png_ptr, info_ptr->text[num].key);
nuclear@14 359 info_ptr->text[num].key = NULL;
nuclear@14 360 }
nuclear@14 361 }
nuclear@14 362 else
nuclear@14 363 {
nuclear@14 364 int i;
nuclear@14 365 for (i = 0; i < info_ptr->num_text; i++)
nuclear@14 366 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
nuclear@14 367 png_free(png_ptr, info_ptr->text);
nuclear@14 368 info_ptr->text = NULL;
nuclear@14 369 info_ptr->num_text=0;
nuclear@14 370 }
nuclear@14 371 }
nuclear@14 372 #endif
nuclear@14 373
nuclear@14 374 #if defined(PNG_tRNS_SUPPORTED)
nuclear@14 375 /* free any tRNS entry */
nuclear@14 376 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 377 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
nuclear@14 378 #else
nuclear@14 379 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
nuclear@14 380 #endif
nuclear@14 381 {
nuclear@14 382 png_free(png_ptr, info_ptr->trans);
nuclear@14 383 info_ptr->trans = NULL;
nuclear@14 384 info_ptr->valid &= ~PNG_INFO_tRNS;
nuclear@14 385 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@14 386 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
nuclear@14 387 #endif
nuclear@14 388 }
nuclear@14 389 #endif
nuclear@14 390
nuclear@14 391 #if defined(PNG_sCAL_SUPPORTED)
nuclear@14 392 /* free any sCAL entry */
nuclear@14 393 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 394 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
nuclear@14 395 #else
nuclear@14 396 if (mask & PNG_FREE_SCAL)
nuclear@14 397 #endif
nuclear@14 398 {
nuclear@14 399 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
nuclear@14 400 png_free(png_ptr, info_ptr->scal_s_width);
nuclear@14 401 png_free(png_ptr, info_ptr->scal_s_height);
nuclear@14 402 info_ptr->scal_s_width = NULL;
nuclear@14 403 info_ptr->scal_s_height = NULL;
nuclear@14 404 #endif
nuclear@14 405 info_ptr->valid &= ~PNG_INFO_sCAL;
nuclear@14 406 }
nuclear@14 407 #endif
nuclear@14 408
nuclear@14 409 #if defined(PNG_pCAL_SUPPORTED)
nuclear@14 410 /* free any pCAL entry */
nuclear@14 411 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 412 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
nuclear@14 413 #else
nuclear@14 414 if (mask & PNG_FREE_PCAL)
nuclear@14 415 #endif
nuclear@14 416 {
nuclear@14 417 png_free(png_ptr, info_ptr->pcal_purpose);
nuclear@14 418 png_free(png_ptr, info_ptr->pcal_units);
nuclear@14 419 info_ptr->pcal_purpose = NULL;
nuclear@14 420 info_ptr->pcal_units = NULL;
nuclear@14 421 if (info_ptr->pcal_params != NULL)
nuclear@14 422 {
nuclear@14 423 int i;
nuclear@14 424 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
nuclear@14 425 {
nuclear@14 426 png_free(png_ptr, info_ptr->pcal_params[i]);
nuclear@14 427 info_ptr->pcal_params[i]=NULL;
nuclear@14 428 }
nuclear@14 429 png_free(png_ptr, info_ptr->pcal_params);
nuclear@14 430 info_ptr->pcal_params = NULL;
nuclear@14 431 }
nuclear@14 432 info_ptr->valid &= ~PNG_INFO_pCAL;
nuclear@14 433 }
nuclear@14 434 #endif
nuclear@14 435
nuclear@14 436 #if defined(PNG_iCCP_SUPPORTED)
nuclear@14 437 /* free any iCCP entry */
nuclear@14 438 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 439 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
nuclear@14 440 #else
nuclear@14 441 if (mask & PNG_FREE_ICCP)
nuclear@14 442 #endif
nuclear@14 443 {
nuclear@14 444 png_free(png_ptr, info_ptr->iccp_name);
nuclear@14 445 png_free(png_ptr, info_ptr->iccp_profile);
nuclear@14 446 info_ptr->iccp_name = NULL;
nuclear@14 447 info_ptr->iccp_profile = NULL;
nuclear@14 448 info_ptr->valid &= ~PNG_INFO_iCCP;
nuclear@14 449 }
nuclear@14 450 #endif
nuclear@14 451
nuclear@14 452 #if defined(PNG_sPLT_SUPPORTED)
nuclear@14 453 /* free a given sPLT entry, or (if num == -1) all sPLT entries */
nuclear@14 454 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 455 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
nuclear@14 456 #else
nuclear@14 457 if (mask & PNG_FREE_SPLT)
nuclear@14 458 #endif
nuclear@14 459 {
nuclear@14 460 if (num != -1)
nuclear@14 461 {
nuclear@14 462 if (info_ptr->splt_palettes)
nuclear@14 463 {
nuclear@14 464 png_free(png_ptr, info_ptr->splt_palettes[num].name);
nuclear@14 465 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
nuclear@14 466 info_ptr->splt_palettes[num].name = NULL;
nuclear@14 467 info_ptr->splt_palettes[num].entries = NULL;
nuclear@14 468 }
nuclear@14 469 }
nuclear@14 470 else
nuclear@14 471 {
nuclear@14 472 if (info_ptr->splt_palettes_num)
nuclear@14 473 {
nuclear@14 474 int i;
nuclear@14 475 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
nuclear@14 476 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
nuclear@14 477
nuclear@14 478 png_free(png_ptr, info_ptr->splt_palettes);
nuclear@14 479 info_ptr->splt_palettes = NULL;
nuclear@14 480 info_ptr->splt_palettes_num = 0;
nuclear@14 481 }
nuclear@14 482 info_ptr->valid &= ~PNG_INFO_sPLT;
nuclear@14 483 }
nuclear@14 484 }
nuclear@14 485 #endif
nuclear@14 486
nuclear@14 487 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@14 488 if (png_ptr->unknown_chunk.data)
nuclear@14 489 {
nuclear@14 490 png_free(png_ptr, png_ptr->unknown_chunk.data);
nuclear@14 491 png_ptr->unknown_chunk.data = NULL;
nuclear@14 492 }
nuclear@14 493
nuclear@14 494 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 495 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
nuclear@14 496 #else
nuclear@14 497 if (mask & PNG_FREE_UNKN)
nuclear@14 498 #endif
nuclear@14 499 {
nuclear@14 500 if (num != -1)
nuclear@14 501 {
nuclear@14 502 if (info_ptr->unknown_chunks)
nuclear@14 503 {
nuclear@14 504 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
nuclear@14 505 info_ptr->unknown_chunks[num].data = NULL;
nuclear@14 506 }
nuclear@14 507 }
nuclear@14 508 else
nuclear@14 509 {
nuclear@14 510 int i;
nuclear@14 511
nuclear@14 512 if (info_ptr->unknown_chunks_num)
nuclear@14 513 {
nuclear@14 514 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
nuclear@14 515 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
nuclear@14 516
nuclear@14 517 png_free(png_ptr, info_ptr->unknown_chunks);
nuclear@14 518 info_ptr->unknown_chunks = NULL;
nuclear@14 519 info_ptr->unknown_chunks_num = 0;
nuclear@14 520 }
nuclear@14 521 }
nuclear@14 522 }
nuclear@14 523 #endif
nuclear@14 524
nuclear@14 525 #if defined(PNG_hIST_SUPPORTED)
nuclear@14 526 /* free any hIST entry */
nuclear@14 527 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 528 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
nuclear@14 529 #else
nuclear@14 530 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
nuclear@14 531 #endif
nuclear@14 532 {
nuclear@14 533 png_free(png_ptr, info_ptr->hist);
nuclear@14 534 info_ptr->hist = NULL;
nuclear@14 535 info_ptr->valid &= ~PNG_INFO_hIST;
nuclear@14 536 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@14 537 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
nuclear@14 538 #endif
nuclear@14 539 }
nuclear@14 540 #endif
nuclear@14 541
nuclear@14 542 /* free any PLTE entry that was internally allocated */
nuclear@14 543 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 544 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
nuclear@14 545 #else
nuclear@14 546 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
nuclear@14 547 #endif
nuclear@14 548 {
nuclear@14 549 png_zfree(png_ptr, info_ptr->palette);
nuclear@14 550 info_ptr->palette = NULL;
nuclear@14 551 info_ptr->valid &= ~PNG_INFO_PLTE;
nuclear@14 552 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@14 553 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
nuclear@14 554 #endif
nuclear@14 555 info_ptr->num_palette = 0;
nuclear@14 556 }
nuclear@14 557
nuclear@14 558 #if defined(PNG_INFO_IMAGE_SUPPORTED)
nuclear@14 559 /* free any image bits attached to the info structure */
nuclear@14 560 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 561 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
nuclear@14 562 #else
nuclear@14 563 if (mask & PNG_FREE_ROWS)
nuclear@14 564 #endif
nuclear@14 565 {
nuclear@14 566 if (info_ptr->row_pointers)
nuclear@14 567 {
nuclear@14 568 int row;
nuclear@14 569 for (row = 0; row < (int)info_ptr->height; row++)
nuclear@14 570 {
nuclear@14 571 png_free(png_ptr, info_ptr->row_pointers[row]);
nuclear@14 572 info_ptr->row_pointers[row]=NULL;
nuclear@14 573 }
nuclear@14 574 png_free(png_ptr, info_ptr->row_pointers);
nuclear@14 575 info_ptr->row_pointers=NULL;
nuclear@14 576 }
nuclear@14 577 info_ptr->valid &= ~PNG_INFO_IDAT;
nuclear@14 578 }
nuclear@14 579 #endif
nuclear@14 580
nuclear@14 581 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@14 582 if (num == -1)
nuclear@14 583 info_ptr->free_me &= ~mask;
nuclear@14 584 else
nuclear@14 585 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
nuclear@14 586 #endif
nuclear@14 587 }
nuclear@14 588
nuclear@14 589 /* This is an internal routine to free any memory that the info struct is
nuclear@14 590 * pointing to before re-using it or freeing the struct itself. Recall
nuclear@14 591 * that png_free() checks for NULL pointers for us.
nuclear@14 592 */
nuclear@14 593 void /* PRIVATE */
nuclear@14 594 png_info_destroy(png_structp png_ptr, png_infop info_ptr)
nuclear@14 595 {
nuclear@14 596 png_debug(1, "in png_info_destroy\n");
nuclear@14 597
nuclear@14 598 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
nuclear@14 599
nuclear@14 600 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@14 601 if (png_ptr->num_chunk_list)
nuclear@14 602 {
nuclear@14 603 png_free(png_ptr, png_ptr->chunk_list);
nuclear@14 604 png_ptr->chunk_list=NULL;
nuclear@14 605 png_ptr->num_chunk_list = 0;
nuclear@14 606 }
nuclear@14 607 #endif
nuclear@14 608
nuclear@14 609 png_info_init_3(&info_ptr, png_sizeof(png_info));
nuclear@14 610 }
nuclear@14 611 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@14 612
nuclear@14 613 /* This function returns a pointer to the io_ptr associated with the user
nuclear@14 614 * functions. The application should free any memory associated with this
nuclear@14 615 * pointer before png_write_destroy() or png_read_destroy() are called.
nuclear@14 616 */
nuclear@14 617 png_voidp PNGAPI
nuclear@14 618 png_get_io_ptr(png_structp png_ptr)
nuclear@14 619 {
nuclear@14 620 if (png_ptr == NULL) return (NULL);
nuclear@14 621 return (png_ptr->io_ptr);
nuclear@14 622 }
nuclear@14 623
nuclear@14 624 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@14 625 #if !defined(PNG_NO_STDIO)
nuclear@14 626 /* Initialize the default input/output functions for the PNG file. If you
nuclear@14 627 * use your own read or write routines, you can call either png_set_read_fn()
nuclear@14 628 * or png_set_write_fn() instead of png_init_io(). If you have defined
nuclear@14 629 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
nuclear@14 630 * necessarily available.
nuclear@14 631 */
nuclear@14 632 void PNGAPI
nuclear@14 633 png_init_io(png_structp png_ptr, png_FILE_p fp)
nuclear@14 634 {
nuclear@14 635 png_debug(1, "in png_init_io\n");
nuclear@14 636 if (png_ptr == NULL) return;
nuclear@14 637 png_ptr->io_ptr = (png_voidp)fp;
nuclear@14 638 }
nuclear@14 639 #endif
nuclear@14 640
nuclear@14 641 #if defined(PNG_TIME_RFC1123_SUPPORTED)
nuclear@14 642 /* Convert the supplied time into an RFC 1123 string suitable for use in
nuclear@14 643 * a "Creation Time" or other text-based time string.
nuclear@14 644 */
nuclear@14 645 png_charp PNGAPI
nuclear@14 646 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
nuclear@14 647 {
nuclear@14 648 static PNG_CONST char short_months[12][4] =
nuclear@14 649 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
nuclear@14 650 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
nuclear@14 651
nuclear@14 652 if (png_ptr == NULL) return (NULL);
nuclear@14 653 if (png_ptr->time_buffer == NULL)
nuclear@14 654 {
nuclear@14 655 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
nuclear@14 656 png_sizeof(char)));
nuclear@14 657 }
nuclear@14 658
nuclear@14 659 #if defined(_WIN32_WCE)
nuclear@14 660 {
nuclear@14 661 wchar_t time_buf[29];
nuclear@14 662 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
nuclear@14 663 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@14 664 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@14 665 ptime->second % 61);
nuclear@14 666 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
nuclear@14 667 NULL, NULL);
nuclear@14 668 }
nuclear@14 669 #else
nuclear@14 670 #ifdef USE_FAR_KEYWORD
nuclear@14 671 {
nuclear@14 672 char near_time_buf[29];
nuclear@14 673 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
nuclear@14 674 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@14 675 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@14 676 ptime->second % 61);
nuclear@14 677 png_memcpy(png_ptr->time_buffer, near_time_buf,
nuclear@14 678 29*png_sizeof(char));
nuclear@14 679 }
nuclear@14 680 #else
nuclear@14 681 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
nuclear@14 682 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@14 683 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@14 684 ptime->second % 61);
nuclear@14 685 #endif
nuclear@14 686 #endif /* _WIN32_WCE */
nuclear@14 687 return ((png_charp)png_ptr->time_buffer);
nuclear@14 688 }
nuclear@14 689 #endif /* PNG_TIME_RFC1123_SUPPORTED */
nuclear@14 690
nuclear@14 691 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@14 692
nuclear@14 693 png_charp PNGAPI
nuclear@14 694 png_get_copyright(png_structp png_ptr)
nuclear@14 695 {
nuclear@14 696 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@14 697 return ((png_charp) "\n libpng version 1.2.33 - October 31, 2008\n\
nuclear@14 698 Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
nuclear@14 699 Copyright (c) 1996-1997 Andreas Dilger\n\
nuclear@14 700 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
nuclear@14 701 }
nuclear@14 702
nuclear@14 703 /* The following return the library version as a short string in the
nuclear@14 704 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
nuclear@14 705 * used with your application, print out PNG_LIBPNG_VER_STRING, which
nuclear@14 706 * is defined in png.h.
nuclear@14 707 * Note: now there is no difference between png_get_libpng_ver() and
nuclear@14 708 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
nuclear@14 709 * it is guaranteed that png.c uses the correct version of png.h.
nuclear@14 710 */
nuclear@14 711 png_charp PNGAPI
nuclear@14 712 png_get_libpng_ver(png_structp png_ptr)
nuclear@14 713 {
nuclear@14 714 /* Version of *.c files used when building libpng */
nuclear@14 715 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@14 716 return ((png_charp) PNG_LIBPNG_VER_STRING);
nuclear@14 717 }
nuclear@14 718
nuclear@14 719 png_charp PNGAPI
nuclear@14 720 png_get_header_ver(png_structp png_ptr)
nuclear@14 721 {
nuclear@14 722 /* Version of *.h files used when building libpng */
nuclear@14 723 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@14 724 return ((png_charp) PNG_LIBPNG_VER_STRING);
nuclear@14 725 }
nuclear@14 726
nuclear@14 727 png_charp PNGAPI
nuclear@14 728 png_get_header_version(png_structp png_ptr)
nuclear@14 729 {
nuclear@14 730 /* Returns longer string containing both version and date */
nuclear@14 731 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@14 732 return ((png_charp) PNG_HEADER_VERSION_STRING
nuclear@14 733 #ifndef PNG_READ_SUPPORTED
nuclear@14 734 " (NO READ SUPPORT)"
nuclear@14 735 #endif
nuclear@14 736 "\n");
nuclear@14 737 }
nuclear@14 738
nuclear@14 739 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@14 740 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
nuclear@14 741 int PNGAPI
nuclear@14 742 png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
nuclear@14 743 {
nuclear@14 744 /* check chunk_name and return "keep" value if it's on the list, else 0 */
nuclear@14 745 int i;
nuclear@14 746 png_bytep p;
nuclear@14 747 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
nuclear@14 748 return 0;
nuclear@14 749 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
nuclear@14 750 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
nuclear@14 751 if (!png_memcmp(chunk_name, p, 4))
nuclear@14 752 return ((int)*(p + 4));
nuclear@14 753 return 0;
nuclear@14 754 }
nuclear@14 755 #endif
nuclear@14 756
nuclear@14 757 /* This function, added to libpng-1.0.6g, is untested. */
nuclear@14 758 int PNGAPI
nuclear@14 759 png_reset_zstream(png_structp png_ptr)
nuclear@14 760 {
nuclear@14 761 if (png_ptr == NULL) return Z_STREAM_ERROR;
nuclear@14 762 return (inflateReset(&png_ptr->zstream));
nuclear@14 763 }
nuclear@14 764 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@14 765
nuclear@14 766 /* This function was added to libpng-1.0.7 */
nuclear@14 767 png_uint_32 PNGAPI
nuclear@14 768 png_access_version_number(void)
nuclear@14 769 {
nuclear@14 770 /* Version of *.c files used when building libpng */
nuclear@14 771 return((png_uint_32) PNG_LIBPNG_VER);
nuclear@14 772 }
nuclear@14 773
nuclear@14 774
nuclear@14 775 #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
nuclear@14 776 #if !defined(PNG_1_0_X)
nuclear@14 777 /* this function was added to libpng 1.2.0 */
nuclear@14 778 int PNGAPI
nuclear@14 779 png_mmx_support(void)
nuclear@14 780 {
nuclear@14 781 /* obsolete, to be removed from libpng-1.4.0 */
nuclear@14 782 return -1;
nuclear@14 783 }
nuclear@14 784 #endif /* PNG_1_0_X */
nuclear@14 785 #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
nuclear@14 786
nuclear@14 787 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@14 788 #ifdef PNG_SIZE_T
nuclear@14 789 /* Added at libpng version 1.2.6 */
nuclear@14 790 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
nuclear@14 791 png_size_t PNGAPI
nuclear@14 792 png_convert_size(size_t size)
nuclear@14 793 {
nuclear@14 794 if (size > (png_size_t)-1)
nuclear@14 795 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
nuclear@14 796 return ((png_size_t)size);
nuclear@14 797 }
nuclear@14 798 #endif /* PNG_SIZE_T */
nuclear@14 799 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */