istereo

annotate libs/libpng/png.c @ 28:c0ae8e668447

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