istereo

annotate libs/libpng/pngread.c @ 43:73813c1176de

better hgignore
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 14 Aug 2015 04:33:42 +0300
parents
children
rev   line source
nuclear@26 1
nuclear@26 2 /* pngread.c - read a PNG file
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 * This file contains routines that an application calls directly to
nuclear@26 11 * read a PNG file or stream.
nuclear@26 12 */
nuclear@26 13
nuclear@26 14 #define PNG_INTERNAL
nuclear@26 15 #include "png.h"
nuclear@26 16 #if defined(PNG_READ_SUPPORTED)
nuclear@26 17
nuclear@26 18 /* Create a PNG structure for reading, and allocate any memory needed. */
nuclear@26 19 png_structp PNGAPI
nuclear@26 20 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
nuclear@26 21 png_error_ptr error_fn, png_error_ptr warn_fn)
nuclear@26 22 {
nuclear@26 23
nuclear@26 24 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 25 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
nuclear@26 26 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
nuclear@26 27 }
nuclear@26 28
nuclear@26 29 /* Alternate create PNG structure for reading, and allocate any memory needed. */
nuclear@26 30 png_structp PNGAPI
nuclear@26 31 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
nuclear@26 32 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
nuclear@26 33 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
nuclear@26 34 {
nuclear@26 35 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@26 36
nuclear@26 37 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 38 volatile
nuclear@26 39 #endif
nuclear@26 40 png_structp png_ptr;
nuclear@26 41
nuclear@26 42 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 43 #ifdef USE_FAR_KEYWORD
nuclear@26 44 jmp_buf jmpbuf;
nuclear@26 45 #endif
nuclear@26 46 #endif
nuclear@26 47
nuclear@26 48 int i;
nuclear@26 49
nuclear@26 50 png_debug(1, "in png_create_read_struct\n");
nuclear@26 51 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 52 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
nuclear@26 53 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
nuclear@26 54 #else
nuclear@26 55 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
nuclear@26 56 #endif
nuclear@26 57 if (png_ptr == NULL)
nuclear@26 58 return (NULL);
nuclear@26 59
nuclear@26 60 /* added at libpng-1.2.6 */
nuclear@26 61 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
nuclear@26 62 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
nuclear@26 63 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
nuclear@26 64 #endif
nuclear@26 65
nuclear@26 66 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 67 #ifdef USE_FAR_KEYWORD
nuclear@26 68 if (setjmp(jmpbuf))
nuclear@26 69 #else
nuclear@26 70 if (setjmp(png_ptr->jmpbuf))
nuclear@26 71 #endif
nuclear@26 72 {
nuclear@26 73 png_free(png_ptr, png_ptr->zbuf);
nuclear@26 74 png_ptr->zbuf = NULL;
nuclear@26 75 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 76 png_destroy_struct_2((png_voidp)png_ptr,
nuclear@26 77 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
nuclear@26 78 #else
nuclear@26 79 png_destroy_struct((png_voidp)png_ptr);
nuclear@26 80 #endif
nuclear@26 81 return (NULL);
nuclear@26 82 }
nuclear@26 83 #ifdef USE_FAR_KEYWORD
nuclear@26 84 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
nuclear@26 85 #endif
nuclear@26 86 #endif
nuclear@26 87
nuclear@26 88 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 89 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
nuclear@26 90 #endif
nuclear@26 91
nuclear@26 92 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
nuclear@26 93
nuclear@26 94 if (user_png_ver)
nuclear@26 95 {
nuclear@26 96 i = 0;
nuclear@26 97 do
nuclear@26 98 {
nuclear@26 99 if (user_png_ver[i] != png_libpng_ver[i])
nuclear@26 100 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
nuclear@26 101 } while (png_libpng_ver[i++]);
nuclear@26 102 }
nuclear@26 103 else
nuclear@26 104 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
nuclear@26 105
nuclear@26 106
nuclear@26 107 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
nuclear@26 108 {
nuclear@26 109 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
nuclear@26 110 * we must recompile any applications that use any older library version.
nuclear@26 111 * For versions after libpng 1.0, we will be compatible, so we need
nuclear@26 112 * only check the first digit.
nuclear@26 113 */
nuclear@26 114 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
nuclear@26 115 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
nuclear@26 116 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
nuclear@26 117 {
nuclear@26 118 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
nuclear@26 119 char msg[80];
nuclear@26 120 if (user_png_ver)
nuclear@26 121 {
nuclear@26 122 png_snprintf(msg, 80,
nuclear@26 123 "Application was compiled with png.h from libpng-%.20s",
nuclear@26 124 user_png_ver);
nuclear@26 125 png_warning(png_ptr, msg);
nuclear@26 126 }
nuclear@26 127 png_snprintf(msg, 80,
nuclear@26 128 "Application is running with png.c from libpng-%.20s",
nuclear@26 129 png_libpng_ver);
nuclear@26 130 png_warning(png_ptr, msg);
nuclear@26 131 #endif
nuclear@26 132 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
nuclear@26 133 png_ptr->flags = 0;
nuclear@26 134 #endif
nuclear@26 135 png_error(png_ptr,
nuclear@26 136 "Incompatible libpng version in application and library");
nuclear@26 137 }
nuclear@26 138 }
nuclear@26 139
nuclear@26 140 /* initialize zbuf - compression buffer */
nuclear@26 141 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
nuclear@26 142 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
nuclear@26 143 (png_uint_32)png_ptr->zbuf_size);
nuclear@26 144 png_ptr->zstream.zalloc = png_zalloc;
nuclear@26 145 png_ptr->zstream.zfree = png_zfree;
nuclear@26 146 png_ptr->zstream.opaque = (voidpf)png_ptr;
nuclear@26 147
nuclear@26 148 switch (inflateInit(&png_ptr->zstream))
nuclear@26 149 {
nuclear@26 150 case Z_OK: /* Do nothing */ break;
nuclear@26 151 case Z_MEM_ERROR:
nuclear@26 152 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
nuclear@26 153 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
nuclear@26 154 default: png_error(png_ptr, "Unknown zlib error");
nuclear@26 155 }
nuclear@26 156
nuclear@26 157 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@26 158 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@26 159
nuclear@26 160 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
nuclear@26 161
nuclear@26 162 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 163 /* Applications that neglect to set up their own setjmp() and then encounter
nuclear@26 164 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
nuclear@26 165 abort instead of returning. */
nuclear@26 166 #ifdef USE_FAR_KEYWORD
nuclear@26 167 if (setjmp(jmpbuf))
nuclear@26 168 PNG_ABORT();
nuclear@26 169 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
nuclear@26 170 #else
nuclear@26 171 if (setjmp(png_ptr->jmpbuf))
nuclear@26 172 PNG_ABORT();
nuclear@26 173 #endif
nuclear@26 174 #endif
nuclear@26 175 return (png_ptr);
nuclear@26 176 }
nuclear@26 177
nuclear@26 178 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
nuclear@26 179 /* Initialize PNG structure for reading, and allocate any memory needed.
nuclear@26 180 This interface is deprecated in favour of the png_create_read_struct(),
nuclear@26 181 and it will disappear as of libpng-1.3.0. */
nuclear@26 182 #undef png_read_init
nuclear@26 183 void PNGAPI
nuclear@26 184 png_read_init(png_structp png_ptr)
nuclear@26 185 {
nuclear@26 186 /* We only come here via pre-1.0.7-compiled applications */
nuclear@26 187 png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
nuclear@26 188 }
nuclear@26 189
nuclear@26 190 void PNGAPI
nuclear@26 191 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
nuclear@26 192 png_size_t png_struct_size, png_size_t png_info_size)
nuclear@26 193 {
nuclear@26 194 /* We only come here via pre-1.0.12-compiled applications */
nuclear@26 195 if (png_ptr == NULL) return;
nuclear@26 196 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
nuclear@26 197 if (png_sizeof(png_struct) > png_struct_size ||
nuclear@26 198 png_sizeof(png_info) > png_info_size)
nuclear@26 199 {
nuclear@26 200 char msg[80];
nuclear@26 201 png_ptr->warning_fn = NULL;
nuclear@26 202 if (user_png_ver)
nuclear@26 203 {
nuclear@26 204 png_snprintf(msg, 80,
nuclear@26 205 "Application was compiled with png.h from libpng-%.20s",
nuclear@26 206 user_png_ver);
nuclear@26 207 png_warning(png_ptr, msg);
nuclear@26 208 }
nuclear@26 209 png_snprintf(msg, 80,
nuclear@26 210 "Application is running with png.c from libpng-%.20s",
nuclear@26 211 png_libpng_ver);
nuclear@26 212 png_warning(png_ptr, msg);
nuclear@26 213 }
nuclear@26 214 #endif
nuclear@26 215 if (png_sizeof(png_struct) > png_struct_size)
nuclear@26 216 {
nuclear@26 217 png_ptr->error_fn = NULL;
nuclear@26 218 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
nuclear@26 219 png_ptr->flags = 0;
nuclear@26 220 #endif
nuclear@26 221 png_error(png_ptr,
nuclear@26 222 "The png struct allocated by the application for reading is too small.");
nuclear@26 223 }
nuclear@26 224 if (png_sizeof(png_info) > png_info_size)
nuclear@26 225 {
nuclear@26 226 png_ptr->error_fn = NULL;
nuclear@26 227 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
nuclear@26 228 png_ptr->flags = 0;
nuclear@26 229 #endif
nuclear@26 230 png_error(png_ptr,
nuclear@26 231 "The info struct allocated by application for reading is too small.");
nuclear@26 232 }
nuclear@26 233 png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
nuclear@26 234 }
nuclear@26 235 #endif /* PNG_1_0_X || PNG_1_2_X */
nuclear@26 236
nuclear@26 237 void PNGAPI
nuclear@26 238 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
nuclear@26 239 png_size_t png_struct_size)
nuclear@26 240 {
nuclear@26 241 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 242 jmp_buf tmp_jmp; /* to save current jump buffer */
nuclear@26 243 #endif
nuclear@26 244
nuclear@26 245 int i = 0;
nuclear@26 246
nuclear@26 247 png_structp png_ptr=*ptr_ptr;
nuclear@26 248
nuclear@26 249 if (png_ptr == NULL) return;
nuclear@26 250
nuclear@26 251 do
nuclear@26 252 {
nuclear@26 253 if (user_png_ver[i] != png_libpng_ver[i])
nuclear@26 254 {
nuclear@26 255 #ifdef PNG_LEGACY_SUPPORTED
nuclear@26 256 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
nuclear@26 257 #else
nuclear@26 258 png_ptr->warning_fn = NULL;
nuclear@26 259 png_warning(png_ptr,
nuclear@26 260 "Application uses deprecated png_read_init() and should be recompiled.");
nuclear@26 261 break;
nuclear@26 262 #endif
nuclear@26 263 }
nuclear@26 264 } while (png_libpng_ver[i++]);
nuclear@26 265
nuclear@26 266 png_debug(1, "in png_read_init_3\n");
nuclear@26 267
nuclear@26 268 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 269 /* save jump buffer and error functions */
nuclear@26 270 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
nuclear@26 271 #endif
nuclear@26 272
nuclear@26 273 if (png_sizeof(png_struct) > png_struct_size)
nuclear@26 274 {
nuclear@26 275 png_destroy_struct(png_ptr);
nuclear@26 276 *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
nuclear@26 277 png_ptr = *ptr_ptr;
nuclear@26 278 }
nuclear@26 279
nuclear@26 280 /* reset all variables to 0 */
nuclear@26 281 png_memset(png_ptr, 0, png_sizeof(png_struct));
nuclear@26 282
nuclear@26 283 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 284 /* restore jump buffer */
nuclear@26 285 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
nuclear@26 286 #endif
nuclear@26 287
nuclear@26 288 /* added at libpng-1.2.6 */
nuclear@26 289 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
nuclear@26 290 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
nuclear@26 291 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
nuclear@26 292 #endif
nuclear@26 293
nuclear@26 294 /* initialize zbuf - compression buffer */
nuclear@26 295 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
nuclear@26 296 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
nuclear@26 297 (png_uint_32)png_ptr->zbuf_size);
nuclear@26 298 png_ptr->zstream.zalloc = png_zalloc;
nuclear@26 299 png_ptr->zstream.zfree = png_zfree;
nuclear@26 300 png_ptr->zstream.opaque = (voidpf)png_ptr;
nuclear@26 301
nuclear@26 302 switch (inflateInit(&png_ptr->zstream))
nuclear@26 303 {
nuclear@26 304 case Z_OK: /* Do nothing */ break;
nuclear@26 305 case Z_MEM_ERROR:
nuclear@26 306 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
nuclear@26 307 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
nuclear@26 308 default: png_error(png_ptr, "Unknown zlib error");
nuclear@26 309 }
nuclear@26 310
nuclear@26 311 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@26 312 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@26 313
nuclear@26 314 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
nuclear@26 315 }
nuclear@26 316
nuclear@26 317 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 318 /* Read the information before the actual image data. This has been
nuclear@26 319 * changed in v0.90 to allow reading a file that already has the magic
nuclear@26 320 * bytes read from the stream. You can tell libpng how many bytes have
nuclear@26 321 * been read from the beginning of the stream (up to the maximum of 8)
nuclear@26 322 * via png_set_sig_bytes(), and we will only check the remaining bytes
nuclear@26 323 * here. The application can then have access to the signature bytes we
nuclear@26 324 * read if it is determined that this isn't a valid PNG file.
nuclear@26 325 */
nuclear@26 326 void PNGAPI
nuclear@26 327 png_read_info(png_structp png_ptr, png_infop info_ptr)
nuclear@26 328 {
nuclear@26 329 if (png_ptr == NULL || info_ptr == NULL) return;
nuclear@26 330 png_debug(1, "in png_read_info\n");
nuclear@26 331 /* If we haven't checked all of the PNG signature bytes, do so now. */
nuclear@26 332 if (png_ptr->sig_bytes < 8)
nuclear@26 333 {
nuclear@26 334 png_size_t num_checked = png_ptr->sig_bytes,
nuclear@26 335 num_to_check = 8 - num_checked;
nuclear@26 336
nuclear@26 337 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
nuclear@26 338 png_ptr->sig_bytes = 8;
nuclear@26 339
nuclear@26 340 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
nuclear@26 341 {
nuclear@26 342 if (num_checked < 4 &&
nuclear@26 343 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
nuclear@26 344 png_error(png_ptr, "Not a PNG file");
nuclear@26 345 else
nuclear@26 346 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
nuclear@26 347 }
nuclear@26 348 if (num_checked < 3)
nuclear@26 349 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
nuclear@26 350 }
nuclear@26 351
nuclear@26 352 for (;;)
nuclear@26 353 {
nuclear@26 354 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 355 PNG_CONST PNG_IHDR;
nuclear@26 356 PNG_CONST PNG_IDAT;
nuclear@26 357 PNG_CONST PNG_IEND;
nuclear@26 358 PNG_CONST PNG_PLTE;
nuclear@26 359 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 360 PNG_CONST PNG_bKGD;
nuclear@26 361 #endif
nuclear@26 362 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 363 PNG_CONST PNG_cHRM;
nuclear@26 364 #endif
nuclear@26 365 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 366 PNG_CONST PNG_gAMA;
nuclear@26 367 #endif
nuclear@26 368 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 369 PNG_CONST PNG_hIST;
nuclear@26 370 #endif
nuclear@26 371 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 372 PNG_CONST PNG_iCCP;
nuclear@26 373 #endif
nuclear@26 374 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 375 PNG_CONST PNG_iTXt;
nuclear@26 376 #endif
nuclear@26 377 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 378 PNG_CONST PNG_oFFs;
nuclear@26 379 #endif
nuclear@26 380 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 381 PNG_CONST PNG_pCAL;
nuclear@26 382 #endif
nuclear@26 383 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 384 PNG_CONST PNG_pHYs;
nuclear@26 385 #endif
nuclear@26 386 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 387 PNG_CONST PNG_sBIT;
nuclear@26 388 #endif
nuclear@26 389 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 390 PNG_CONST PNG_sCAL;
nuclear@26 391 #endif
nuclear@26 392 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 393 PNG_CONST PNG_sPLT;
nuclear@26 394 #endif
nuclear@26 395 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 396 PNG_CONST PNG_sRGB;
nuclear@26 397 #endif
nuclear@26 398 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 399 PNG_CONST PNG_tEXt;
nuclear@26 400 #endif
nuclear@26 401 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 402 PNG_CONST PNG_tIME;
nuclear@26 403 #endif
nuclear@26 404 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 405 PNG_CONST PNG_tRNS;
nuclear@26 406 #endif
nuclear@26 407 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 408 PNG_CONST PNG_zTXt;
nuclear@26 409 #endif
nuclear@26 410 #endif /* PNG_USE_LOCAL_ARRAYS */
nuclear@26 411 png_uint_32 length = png_read_chunk_header(png_ptr);
nuclear@26 412 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
nuclear@26 413
nuclear@26 414 /* This should be a binary subdivision search or a hash for
nuclear@26 415 * matching the chunk name rather than a linear search.
nuclear@26 416 */
nuclear@26 417 if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 418 if (png_ptr->mode & PNG_AFTER_IDAT)
nuclear@26 419 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
nuclear@26 420
nuclear@26 421 if (!png_memcmp(chunk_name, png_IHDR, 4))
nuclear@26 422 png_handle_IHDR(png_ptr, info_ptr, length);
nuclear@26 423 else if (!png_memcmp(chunk_name, png_IEND, 4))
nuclear@26 424 png_handle_IEND(png_ptr, info_ptr, length);
nuclear@26 425 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
nuclear@26 426 else if (png_handle_as_unknown(png_ptr, chunk_name))
nuclear@26 427 {
nuclear@26 428 if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 429 png_ptr->mode |= PNG_HAVE_IDAT;
nuclear@26 430 png_handle_unknown(png_ptr, info_ptr, length);
nuclear@26 431 if (!png_memcmp(chunk_name, png_PLTE, 4))
nuclear@26 432 png_ptr->mode |= PNG_HAVE_PLTE;
nuclear@26 433 else if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 434 {
nuclear@26 435 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@26 436 png_error(png_ptr, "Missing IHDR before IDAT");
nuclear@26 437 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
nuclear@26 438 !(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@26 439 png_error(png_ptr, "Missing PLTE before IDAT");
nuclear@26 440 break;
nuclear@26 441 }
nuclear@26 442 }
nuclear@26 443 #endif
nuclear@26 444 else if (!png_memcmp(chunk_name, png_PLTE, 4))
nuclear@26 445 png_handle_PLTE(png_ptr, info_ptr, length);
nuclear@26 446 else if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 447 {
nuclear@26 448 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@26 449 png_error(png_ptr, "Missing IHDR before IDAT");
nuclear@26 450 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
nuclear@26 451 !(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@26 452 png_error(png_ptr, "Missing PLTE before IDAT");
nuclear@26 453
nuclear@26 454 png_ptr->idat_size = length;
nuclear@26 455 png_ptr->mode |= PNG_HAVE_IDAT;
nuclear@26 456 break;
nuclear@26 457 }
nuclear@26 458 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 459 else if (!png_memcmp(chunk_name, png_bKGD, 4))
nuclear@26 460 png_handle_bKGD(png_ptr, info_ptr, length);
nuclear@26 461 #endif
nuclear@26 462 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 463 else if (!png_memcmp(chunk_name, png_cHRM, 4))
nuclear@26 464 png_handle_cHRM(png_ptr, info_ptr, length);
nuclear@26 465 #endif
nuclear@26 466 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 467 else if (!png_memcmp(chunk_name, png_gAMA, 4))
nuclear@26 468 png_handle_gAMA(png_ptr, info_ptr, length);
nuclear@26 469 #endif
nuclear@26 470 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 471 else if (!png_memcmp(chunk_name, png_hIST, 4))
nuclear@26 472 png_handle_hIST(png_ptr, info_ptr, length);
nuclear@26 473 #endif
nuclear@26 474 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 475 else if (!png_memcmp(chunk_name, png_oFFs, 4))
nuclear@26 476 png_handle_oFFs(png_ptr, info_ptr, length);
nuclear@26 477 #endif
nuclear@26 478 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 479 else if (!png_memcmp(chunk_name, png_pCAL, 4))
nuclear@26 480 png_handle_pCAL(png_ptr, info_ptr, length);
nuclear@26 481 #endif
nuclear@26 482 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 483 else if (!png_memcmp(chunk_name, png_sCAL, 4))
nuclear@26 484 png_handle_sCAL(png_ptr, info_ptr, length);
nuclear@26 485 #endif
nuclear@26 486 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 487 else if (!png_memcmp(chunk_name, png_pHYs, 4))
nuclear@26 488 png_handle_pHYs(png_ptr, info_ptr, length);
nuclear@26 489 #endif
nuclear@26 490 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 491 else if (!png_memcmp(chunk_name, png_sBIT, 4))
nuclear@26 492 png_handle_sBIT(png_ptr, info_ptr, length);
nuclear@26 493 #endif
nuclear@26 494 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 495 else if (!png_memcmp(chunk_name, png_sRGB, 4))
nuclear@26 496 png_handle_sRGB(png_ptr, info_ptr, length);
nuclear@26 497 #endif
nuclear@26 498 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 499 else if (!png_memcmp(chunk_name, png_iCCP, 4))
nuclear@26 500 png_handle_iCCP(png_ptr, info_ptr, length);
nuclear@26 501 #endif
nuclear@26 502 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 503 else if (!png_memcmp(chunk_name, png_sPLT, 4))
nuclear@26 504 png_handle_sPLT(png_ptr, info_ptr, length);
nuclear@26 505 #endif
nuclear@26 506 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 507 else if (!png_memcmp(chunk_name, png_tEXt, 4))
nuclear@26 508 png_handle_tEXt(png_ptr, info_ptr, length);
nuclear@26 509 #endif
nuclear@26 510 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 511 else if (!png_memcmp(chunk_name, png_tIME, 4))
nuclear@26 512 png_handle_tIME(png_ptr, info_ptr, length);
nuclear@26 513 #endif
nuclear@26 514 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 515 else if (!png_memcmp(chunk_name, png_tRNS, 4))
nuclear@26 516 png_handle_tRNS(png_ptr, info_ptr, length);
nuclear@26 517 #endif
nuclear@26 518 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 519 else if (!png_memcmp(chunk_name, png_zTXt, 4))
nuclear@26 520 png_handle_zTXt(png_ptr, info_ptr, length);
nuclear@26 521 #endif
nuclear@26 522 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 523 else if (!png_memcmp(chunk_name, png_iTXt, 4))
nuclear@26 524 png_handle_iTXt(png_ptr, info_ptr, length);
nuclear@26 525 #endif
nuclear@26 526 else
nuclear@26 527 png_handle_unknown(png_ptr, info_ptr, length);
nuclear@26 528 }
nuclear@26 529 }
nuclear@26 530 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 531
nuclear@26 532 /* optional call to update the users info_ptr structure */
nuclear@26 533 void PNGAPI
nuclear@26 534 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
nuclear@26 535 {
nuclear@26 536 png_debug(1, "in png_read_update_info\n");
nuclear@26 537 if (png_ptr == NULL) return;
nuclear@26 538 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
nuclear@26 539 png_read_start_row(png_ptr);
nuclear@26 540 else
nuclear@26 541 png_warning(png_ptr,
nuclear@26 542 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
nuclear@26 543 png_read_transform_info(png_ptr, info_ptr);
nuclear@26 544 }
nuclear@26 545
nuclear@26 546 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 547 /* Initialize palette, background, etc, after transformations
nuclear@26 548 * are set, but before any reading takes place. This allows
nuclear@26 549 * the user to obtain a gamma-corrected palette, for example.
nuclear@26 550 * If the user doesn't call this, we will do it ourselves.
nuclear@26 551 */
nuclear@26 552 void PNGAPI
nuclear@26 553 png_start_read_image(png_structp png_ptr)
nuclear@26 554 {
nuclear@26 555 png_debug(1, "in png_start_read_image\n");
nuclear@26 556 if (png_ptr == NULL) return;
nuclear@26 557 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
nuclear@26 558 png_read_start_row(png_ptr);
nuclear@26 559 }
nuclear@26 560 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 561
nuclear@26 562 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 563 void PNGAPI
nuclear@26 564 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
nuclear@26 565 {
nuclear@26 566 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 567 PNG_CONST PNG_IDAT;
nuclear@26 568 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
nuclear@26 569 0xff};
nuclear@26 570 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
nuclear@26 571 #endif
nuclear@26 572 int ret;
nuclear@26 573 if (png_ptr == NULL) return;
nuclear@26 574 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
nuclear@26 575 png_ptr->row_number, png_ptr->pass);
nuclear@26 576 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
nuclear@26 577 png_read_start_row(png_ptr);
nuclear@26 578 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
nuclear@26 579 {
nuclear@26 580 /* check for transforms that have been set but were defined out */
nuclear@26 581 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
nuclear@26 582 if (png_ptr->transformations & PNG_INVERT_MONO)
nuclear@26 583 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
nuclear@26 584 #endif
nuclear@26 585 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
nuclear@26 586 if (png_ptr->transformations & PNG_FILLER)
nuclear@26 587 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
nuclear@26 588 #endif
nuclear@26 589 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@26 590 if (png_ptr->transformations & PNG_PACKSWAP)
nuclear@26 591 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
nuclear@26 592 #endif
nuclear@26 593 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
nuclear@26 594 if (png_ptr->transformations & PNG_PACK)
nuclear@26 595 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
nuclear@26 596 #endif
nuclear@26 597 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
nuclear@26 598 if (png_ptr->transformations & PNG_SHIFT)
nuclear@26 599 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
nuclear@26 600 #endif
nuclear@26 601 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
nuclear@26 602 if (png_ptr->transformations & PNG_BGR)
nuclear@26 603 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
nuclear@26 604 #endif
nuclear@26 605 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
nuclear@26 606 if (png_ptr->transformations & PNG_SWAP_BYTES)
nuclear@26 607 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
nuclear@26 608 #endif
nuclear@26 609 }
nuclear@26 610
nuclear@26 611 #if defined(PNG_READ_INTERLACING_SUPPORTED)
nuclear@26 612 /* if interlaced and we do not need a new row, combine row and return */
nuclear@26 613 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
nuclear@26 614 {
nuclear@26 615 switch (png_ptr->pass)
nuclear@26 616 {
nuclear@26 617 case 0:
nuclear@26 618 if (png_ptr->row_number & 0x07)
nuclear@26 619 {
nuclear@26 620 if (dsp_row != NULL)
nuclear@26 621 png_combine_row(png_ptr, dsp_row,
nuclear@26 622 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 623 png_read_finish_row(png_ptr);
nuclear@26 624 return;
nuclear@26 625 }
nuclear@26 626 break;
nuclear@26 627 case 1:
nuclear@26 628 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
nuclear@26 629 {
nuclear@26 630 if (dsp_row != NULL)
nuclear@26 631 png_combine_row(png_ptr, dsp_row,
nuclear@26 632 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 633 png_read_finish_row(png_ptr);
nuclear@26 634 return;
nuclear@26 635 }
nuclear@26 636 break;
nuclear@26 637 case 2:
nuclear@26 638 if ((png_ptr->row_number & 0x07) != 4)
nuclear@26 639 {
nuclear@26 640 if (dsp_row != NULL && (png_ptr->row_number & 4))
nuclear@26 641 png_combine_row(png_ptr, dsp_row,
nuclear@26 642 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 643 png_read_finish_row(png_ptr);
nuclear@26 644 return;
nuclear@26 645 }
nuclear@26 646 break;
nuclear@26 647 case 3:
nuclear@26 648 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
nuclear@26 649 {
nuclear@26 650 if (dsp_row != NULL)
nuclear@26 651 png_combine_row(png_ptr, dsp_row,
nuclear@26 652 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 653 png_read_finish_row(png_ptr);
nuclear@26 654 return;
nuclear@26 655 }
nuclear@26 656 break;
nuclear@26 657 case 4:
nuclear@26 658 if ((png_ptr->row_number & 3) != 2)
nuclear@26 659 {
nuclear@26 660 if (dsp_row != NULL && (png_ptr->row_number & 2))
nuclear@26 661 png_combine_row(png_ptr, dsp_row,
nuclear@26 662 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 663 png_read_finish_row(png_ptr);
nuclear@26 664 return;
nuclear@26 665 }
nuclear@26 666 break;
nuclear@26 667 case 5:
nuclear@26 668 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
nuclear@26 669 {
nuclear@26 670 if (dsp_row != NULL)
nuclear@26 671 png_combine_row(png_ptr, dsp_row,
nuclear@26 672 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 673 png_read_finish_row(png_ptr);
nuclear@26 674 return;
nuclear@26 675 }
nuclear@26 676 break;
nuclear@26 677 case 6:
nuclear@26 678 if (!(png_ptr->row_number & 1))
nuclear@26 679 {
nuclear@26 680 png_read_finish_row(png_ptr);
nuclear@26 681 return;
nuclear@26 682 }
nuclear@26 683 break;
nuclear@26 684 }
nuclear@26 685 }
nuclear@26 686 #endif
nuclear@26 687
nuclear@26 688 if (!(png_ptr->mode & PNG_HAVE_IDAT))
nuclear@26 689 png_error(png_ptr, "Invalid attempt to read row data");
nuclear@26 690
nuclear@26 691 png_ptr->zstream.next_out = png_ptr->row_buf;
nuclear@26 692 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
nuclear@26 693 do
nuclear@26 694 {
nuclear@26 695 if (!(png_ptr->zstream.avail_in))
nuclear@26 696 {
nuclear@26 697 while (!png_ptr->idat_size)
nuclear@26 698 {
nuclear@26 699 png_crc_finish(png_ptr, 0);
nuclear@26 700
nuclear@26 701 png_ptr->idat_size = png_read_chunk_header(png_ptr);
nuclear@26 702 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 703 png_error(png_ptr, "Not enough image data");
nuclear@26 704 }
nuclear@26 705 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
nuclear@26 706 png_ptr->zstream.next_in = png_ptr->zbuf;
nuclear@26 707 if (png_ptr->zbuf_size > png_ptr->idat_size)
nuclear@26 708 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
nuclear@26 709 png_crc_read(png_ptr, png_ptr->zbuf,
nuclear@26 710 (png_size_t)png_ptr->zstream.avail_in);
nuclear@26 711 png_ptr->idat_size -= png_ptr->zstream.avail_in;
nuclear@26 712 }
nuclear@26 713 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
nuclear@26 714 if (ret == Z_STREAM_END)
nuclear@26 715 {
nuclear@26 716 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
nuclear@26 717 png_ptr->idat_size)
nuclear@26 718 png_error(png_ptr, "Extra compressed data");
nuclear@26 719 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@26 720 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
nuclear@26 721 break;
nuclear@26 722 }
nuclear@26 723 if (ret != Z_OK)
nuclear@26 724 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
nuclear@26 725 "Decompression error");
nuclear@26 726
nuclear@26 727 } while (png_ptr->zstream.avail_out);
nuclear@26 728
nuclear@26 729 png_ptr->row_info.color_type = png_ptr->color_type;
nuclear@26 730 png_ptr->row_info.width = png_ptr->iwidth;
nuclear@26 731 png_ptr->row_info.channels = png_ptr->channels;
nuclear@26 732 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
nuclear@26 733 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
nuclear@26 734 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
nuclear@26 735 png_ptr->row_info.width);
nuclear@26 736
nuclear@26 737 if (png_ptr->row_buf[0])
nuclear@26 738 png_read_filter_row(png_ptr, &(png_ptr->row_info),
nuclear@26 739 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
nuclear@26 740 (int)(png_ptr->row_buf[0]));
nuclear@26 741
nuclear@26 742 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
nuclear@26 743 png_ptr->rowbytes + 1);
nuclear@26 744
nuclear@26 745 #if defined(PNG_MNG_FEATURES_SUPPORTED)
nuclear@26 746 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
nuclear@26 747 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
nuclear@26 748 {
nuclear@26 749 /* Intrapixel differencing */
nuclear@26 750 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
nuclear@26 751 }
nuclear@26 752 #endif
nuclear@26 753
nuclear@26 754
nuclear@26 755 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
nuclear@26 756 png_do_read_transformations(png_ptr);
nuclear@26 757
nuclear@26 758 #if defined(PNG_READ_INTERLACING_SUPPORTED)
nuclear@26 759 /* blow up interlaced rows to full size */
nuclear@26 760 if (png_ptr->interlaced &&
nuclear@26 761 (png_ptr->transformations & PNG_INTERLACE))
nuclear@26 762 {
nuclear@26 763 if (png_ptr->pass < 6)
nuclear@26 764 /* old interface (pre-1.0.9):
nuclear@26 765 png_do_read_interlace(&(png_ptr->row_info),
nuclear@26 766 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
nuclear@26 767 */
nuclear@26 768 png_do_read_interlace(png_ptr);
nuclear@26 769
nuclear@26 770 if (dsp_row != NULL)
nuclear@26 771 png_combine_row(png_ptr, dsp_row,
nuclear@26 772 png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 773 if (row != NULL)
nuclear@26 774 png_combine_row(png_ptr, row,
nuclear@26 775 png_pass_mask[png_ptr->pass]);
nuclear@26 776 }
nuclear@26 777 else
nuclear@26 778 #endif
nuclear@26 779 {
nuclear@26 780 if (row != NULL)
nuclear@26 781 png_combine_row(png_ptr, row, 0xff);
nuclear@26 782 if (dsp_row != NULL)
nuclear@26 783 png_combine_row(png_ptr, dsp_row, 0xff);
nuclear@26 784 }
nuclear@26 785 png_read_finish_row(png_ptr);
nuclear@26 786
nuclear@26 787 if (png_ptr->read_row_fn != NULL)
nuclear@26 788 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
nuclear@26 789 }
nuclear@26 790 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 791
nuclear@26 792 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 793 /* Read one or more rows of image data. If the image is interlaced,
nuclear@26 794 * and png_set_interlace_handling() has been called, the rows need to
nuclear@26 795 * contain the contents of the rows from the previous pass. If the
nuclear@26 796 * image has alpha or transparency, and png_handle_alpha()[*] has been
nuclear@26 797 * called, the rows contents must be initialized to the contents of the
nuclear@26 798 * screen.
nuclear@26 799 *
nuclear@26 800 * "row" holds the actual image, and pixels are placed in it
nuclear@26 801 * as they arrive. If the image is displayed after each pass, it will
nuclear@26 802 * appear to "sparkle" in. "display_row" can be used to display a
nuclear@26 803 * "chunky" progressive image, with finer detail added as it becomes
nuclear@26 804 * available. If you do not want this "chunky" display, you may pass
nuclear@26 805 * NULL for display_row. If you do not want the sparkle display, and
nuclear@26 806 * you have not called png_handle_alpha(), you may pass NULL for rows.
nuclear@26 807 * If you have called png_handle_alpha(), and the image has either an
nuclear@26 808 * alpha channel or a transparency chunk, you must provide a buffer for
nuclear@26 809 * rows. In this case, you do not have to provide a display_row buffer
nuclear@26 810 * also, but you may. If the image is not interlaced, or if you have
nuclear@26 811 * not called png_set_interlace_handling(), the display_row buffer will
nuclear@26 812 * be ignored, so pass NULL to it.
nuclear@26 813 *
nuclear@26 814 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
nuclear@26 815 */
nuclear@26 816
nuclear@26 817 void PNGAPI
nuclear@26 818 png_read_rows(png_structp png_ptr, png_bytepp row,
nuclear@26 819 png_bytepp display_row, png_uint_32 num_rows)
nuclear@26 820 {
nuclear@26 821 png_uint_32 i;
nuclear@26 822 png_bytepp rp;
nuclear@26 823 png_bytepp dp;
nuclear@26 824
nuclear@26 825 png_debug(1, "in png_read_rows\n");
nuclear@26 826 if (png_ptr == NULL) return;
nuclear@26 827 rp = row;
nuclear@26 828 dp = display_row;
nuclear@26 829 if (rp != NULL && dp != NULL)
nuclear@26 830 for (i = 0; i < num_rows; i++)
nuclear@26 831 {
nuclear@26 832 png_bytep rptr = *rp++;
nuclear@26 833 png_bytep dptr = *dp++;
nuclear@26 834
nuclear@26 835 png_read_row(png_ptr, rptr, dptr);
nuclear@26 836 }
nuclear@26 837 else if (rp != NULL)
nuclear@26 838 for (i = 0; i < num_rows; i++)
nuclear@26 839 {
nuclear@26 840 png_bytep rptr = *rp;
nuclear@26 841 png_read_row(png_ptr, rptr, png_bytep_NULL);
nuclear@26 842 rp++;
nuclear@26 843 }
nuclear@26 844 else if (dp != NULL)
nuclear@26 845 for (i = 0; i < num_rows; i++)
nuclear@26 846 {
nuclear@26 847 png_bytep dptr = *dp;
nuclear@26 848 png_read_row(png_ptr, png_bytep_NULL, dptr);
nuclear@26 849 dp++;
nuclear@26 850 }
nuclear@26 851 }
nuclear@26 852 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 853
nuclear@26 854 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 855 /* Read the entire image. If the image has an alpha channel or a tRNS
nuclear@26 856 * chunk, and you have called png_handle_alpha()[*], you will need to
nuclear@26 857 * initialize the image to the current image that PNG will be overlaying.
nuclear@26 858 * We set the num_rows again here, in case it was incorrectly set in
nuclear@26 859 * png_read_start_row() by a call to png_read_update_info() or
nuclear@26 860 * png_start_read_image() if png_set_interlace_handling() wasn't called
nuclear@26 861 * prior to either of these functions like it should have been. You can
nuclear@26 862 * only call this function once. If you desire to have an image for
nuclear@26 863 * each pass of a interlaced image, use png_read_rows() instead.
nuclear@26 864 *
nuclear@26 865 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
nuclear@26 866 */
nuclear@26 867 void PNGAPI
nuclear@26 868 png_read_image(png_structp png_ptr, png_bytepp image)
nuclear@26 869 {
nuclear@26 870 png_uint_32 i, image_height;
nuclear@26 871 int pass, j;
nuclear@26 872 png_bytepp rp;
nuclear@26 873
nuclear@26 874 png_debug(1, "in png_read_image\n");
nuclear@26 875 if (png_ptr == NULL) return;
nuclear@26 876
nuclear@26 877 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@26 878 pass = png_set_interlace_handling(png_ptr);
nuclear@26 879 #else
nuclear@26 880 if (png_ptr->interlaced)
nuclear@26 881 png_error(png_ptr,
nuclear@26 882 "Cannot read interlaced image -- interlace handler disabled.");
nuclear@26 883 pass = 1;
nuclear@26 884 #endif
nuclear@26 885
nuclear@26 886
nuclear@26 887 image_height=png_ptr->height;
nuclear@26 888 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
nuclear@26 889
nuclear@26 890 for (j = 0; j < pass; j++)
nuclear@26 891 {
nuclear@26 892 rp = image;
nuclear@26 893 for (i = 0; i < image_height; i++)
nuclear@26 894 {
nuclear@26 895 png_read_row(png_ptr, *rp, png_bytep_NULL);
nuclear@26 896 rp++;
nuclear@26 897 }
nuclear@26 898 }
nuclear@26 899 }
nuclear@26 900 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 901
nuclear@26 902 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 903 /* Read the end of the PNG file. Will not read past the end of the
nuclear@26 904 * file, will verify the end is accurate, and will read any comments
nuclear@26 905 * or time information at the end of the file, if info is not NULL.
nuclear@26 906 */
nuclear@26 907 void PNGAPI
nuclear@26 908 png_read_end(png_structp png_ptr, png_infop info_ptr)
nuclear@26 909 {
nuclear@26 910 png_debug(1, "in png_read_end\n");
nuclear@26 911 if (png_ptr == NULL) return;
nuclear@26 912 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
nuclear@26 913
nuclear@26 914 do
nuclear@26 915 {
nuclear@26 916 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 917 PNG_CONST PNG_IHDR;
nuclear@26 918 PNG_CONST PNG_IDAT;
nuclear@26 919 PNG_CONST PNG_IEND;
nuclear@26 920 PNG_CONST PNG_PLTE;
nuclear@26 921 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 922 PNG_CONST PNG_bKGD;
nuclear@26 923 #endif
nuclear@26 924 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 925 PNG_CONST PNG_cHRM;
nuclear@26 926 #endif
nuclear@26 927 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 928 PNG_CONST PNG_gAMA;
nuclear@26 929 #endif
nuclear@26 930 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 931 PNG_CONST PNG_hIST;
nuclear@26 932 #endif
nuclear@26 933 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 934 PNG_CONST PNG_iCCP;
nuclear@26 935 #endif
nuclear@26 936 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 937 PNG_CONST PNG_iTXt;
nuclear@26 938 #endif
nuclear@26 939 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 940 PNG_CONST PNG_oFFs;
nuclear@26 941 #endif
nuclear@26 942 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 943 PNG_CONST PNG_pCAL;
nuclear@26 944 #endif
nuclear@26 945 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 946 PNG_CONST PNG_pHYs;
nuclear@26 947 #endif
nuclear@26 948 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 949 PNG_CONST PNG_sBIT;
nuclear@26 950 #endif
nuclear@26 951 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 952 PNG_CONST PNG_sCAL;
nuclear@26 953 #endif
nuclear@26 954 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 955 PNG_CONST PNG_sPLT;
nuclear@26 956 #endif
nuclear@26 957 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 958 PNG_CONST PNG_sRGB;
nuclear@26 959 #endif
nuclear@26 960 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 961 PNG_CONST PNG_tEXt;
nuclear@26 962 #endif
nuclear@26 963 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 964 PNG_CONST PNG_tIME;
nuclear@26 965 #endif
nuclear@26 966 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 967 PNG_CONST PNG_tRNS;
nuclear@26 968 #endif
nuclear@26 969 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 970 PNG_CONST PNG_zTXt;
nuclear@26 971 #endif
nuclear@26 972 #endif /* PNG_USE_LOCAL_ARRAYS */
nuclear@26 973 png_uint_32 length = png_read_chunk_header(png_ptr);
nuclear@26 974 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
nuclear@26 975
nuclear@26 976 if (!png_memcmp(chunk_name, png_IHDR, 4))
nuclear@26 977 png_handle_IHDR(png_ptr, info_ptr, length);
nuclear@26 978 else if (!png_memcmp(chunk_name, png_IEND, 4))
nuclear@26 979 png_handle_IEND(png_ptr, info_ptr, length);
nuclear@26 980 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
nuclear@26 981 else if (png_handle_as_unknown(png_ptr, chunk_name))
nuclear@26 982 {
nuclear@26 983 if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 984 {
nuclear@26 985 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
nuclear@26 986 png_error(png_ptr, "Too many IDAT's found");
nuclear@26 987 }
nuclear@26 988 png_handle_unknown(png_ptr, info_ptr, length);
nuclear@26 989 if (!png_memcmp(chunk_name, png_PLTE, 4))
nuclear@26 990 png_ptr->mode |= PNG_HAVE_PLTE;
nuclear@26 991 }
nuclear@26 992 #endif
nuclear@26 993 else if (!png_memcmp(chunk_name, png_IDAT, 4))
nuclear@26 994 {
nuclear@26 995 /* Zero length IDATs are legal after the last IDAT has been
nuclear@26 996 * read, but not after other chunks have been read.
nuclear@26 997 */
nuclear@26 998 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
nuclear@26 999 png_error(png_ptr, "Too many IDAT's found");
nuclear@26 1000 png_crc_finish(png_ptr, length);
nuclear@26 1001 }
nuclear@26 1002 else if (!png_memcmp(chunk_name, png_PLTE, 4))
nuclear@26 1003 png_handle_PLTE(png_ptr, info_ptr, length);
nuclear@26 1004 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 1005 else if (!png_memcmp(chunk_name, png_bKGD, 4))
nuclear@26 1006 png_handle_bKGD(png_ptr, info_ptr, length);
nuclear@26 1007 #endif
nuclear@26 1008 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 1009 else if (!png_memcmp(chunk_name, png_cHRM, 4))
nuclear@26 1010 png_handle_cHRM(png_ptr, info_ptr, length);
nuclear@26 1011 #endif
nuclear@26 1012 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 1013 else if (!png_memcmp(chunk_name, png_gAMA, 4))
nuclear@26 1014 png_handle_gAMA(png_ptr, info_ptr, length);
nuclear@26 1015 #endif
nuclear@26 1016 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 1017 else if (!png_memcmp(chunk_name, png_hIST, 4))
nuclear@26 1018 png_handle_hIST(png_ptr, info_ptr, length);
nuclear@26 1019 #endif
nuclear@26 1020 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 1021 else if (!png_memcmp(chunk_name, png_oFFs, 4))
nuclear@26 1022 png_handle_oFFs(png_ptr, info_ptr, length);
nuclear@26 1023 #endif
nuclear@26 1024 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 1025 else if (!png_memcmp(chunk_name, png_pCAL, 4))
nuclear@26 1026 png_handle_pCAL(png_ptr, info_ptr, length);
nuclear@26 1027 #endif
nuclear@26 1028 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 1029 else if (!png_memcmp(chunk_name, png_sCAL, 4))
nuclear@26 1030 png_handle_sCAL(png_ptr, info_ptr, length);
nuclear@26 1031 #endif
nuclear@26 1032 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 1033 else if (!png_memcmp(chunk_name, png_pHYs, 4))
nuclear@26 1034 png_handle_pHYs(png_ptr, info_ptr, length);
nuclear@26 1035 #endif
nuclear@26 1036 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 1037 else if (!png_memcmp(chunk_name, png_sBIT, 4))
nuclear@26 1038 png_handle_sBIT(png_ptr, info_ptr, length);
nuclear@26 1039 #endif
nuclear@26 1040 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 1041 else if (!png_memcmp(chunk_name, png_sRGB, 4))
nuclear@26 1042 png_handle_sRGB(png_ptr, info_ptr, length);
nuclear@26 1043 #endif
nuclear@26 1044 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 1045 else if (!png_memcmp(chunk_name, png_iCCP, 4))
nuclear@26 1046 png_handle_iCCP(png_ptr, info_ptr, length);
nuclear@26 1047 #endif
nuclear@26 1048 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 1049 else if (!png_memcmp(chunk_name, png_sPLT, 4))
nuclear@26 1050 png_handle_sPLT(png_ptr, info_ptr, length);
nuclear@26 1051 #endif
nuclear@26 1052 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 1053 else if (!png_memcmp(chunk_name, png_tEXt, 4))
nuclear@26 1054 png_handle_tEXt(png_ptr, info_ptr, length);
nuclear@26 1055 #endif
nuclear@26 1056 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 1057 else if (!png_memcmp(chunk_name, png_tIME, 4))
nuclear@26 1058 png_handle_tIME(png_ptr, info_ptr, length);
nuclear@26 1059 #endif
nuclear@26 1060 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 1061 else if (!png_memcmp(chunk_name, png_tRNS, 4))
nuclear@26 1062 png_handle_tRNS(png_ptr, info_ptr, length);
nuclear@26 1063 #endif
nuclear@26 1064 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 1065 else if (!png_memcmp(chunk_name, png_zTXt, 4))
nuclear@26 1066 png_handle_zTXt(png_ptr, info_ptr, length);
nuclear@26 1067 #endif
nuclear@26 1068 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 1069 else if (!png_memcmp(chunk_name, png_iTXt, 4))
nuclear@26 1070 png_handle_iTXt(png_ptr, info_ptr, length);
nuclear@26 1071 #endif
nuclear@26 1072 else
nuclear@26 1073 png_handle_unknown(png_ptr, info_ptr, length);
nuclear@26 1074 } while (!(png_ptr->mode & PNG_HAVE_IEND));
nuclear@26 1075 }
nuclear@26 1076 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 1077
nuclear@26 1078 /* free all memory used by the read */
nuclear@26 1079 void PNGAPI
nuclear@26 1080 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
nuclear@26 1081 png_infopp end_info_ptr_ptr)
nuclear@26 1082 {
nuclear@26 1083 png_structp png_ptr = NULL;
nuclear@26 1084 png_infop info_ptr = NULL, end_info_ptr = NULL;
nuclear@26 1085 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1086 png_free_ptr free_fn = NULL;
nuclear@26 1087 png_voidp mem_ptr = NULL;
nuclear@26 1088 #endif
nuclear@26 1089
nuclear@26 1090 png_debug(1, "in png_destroy_read_struct\n");
nuclear@26 1091 if (png_ptr_ptr != NULL)
nuclear@26 1092 png_ptr = *png_ptr_ptr;
nuclear@26 1093 if (png_ptr == NULL)
nuclear@26 1094 return;
nuclear@26 1095
nuclear@26 1096 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1097 free_fn = png_ptr->free_fn;
nuclear@26 1098 mem_ptr = png_ptr->mem_ptr;
nuclear@26 1099 #endif
nuclear@26 1100
nuclear@26 1101 if (info_ptr_ptr != NULL)
nuclear@26 1102 info_ptr = *info_ptr_ptr;
nuclear@26 1103
nuclear@26 1104 if (end_info_ptr_ptr != NULL)
nuclear@26 1105 end_info_ptr = *end_info_ptr_ptr;
nuclear@26 1106
nuclear@26 1107 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
nuclear@26 1108
nuclear@26 1109 if (info_ptr != NULL)
nuclear@26 1110 {
nuclear@26 1111 #if defined(PNG_TEXT_SUPPORTED)
nuclear@26 1112 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
nuclear@26 1113 #endif
nuclear@26 1114
nuclear@26 1115 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1116 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
nuclear@26 1117 (png_voidp)mem_ptr);
nuclear@26 1118 #else
nuclear@26 1119 png_destroy_struct((png_voidp)info_ptr);
nuclear@26 1120 #endif
nuclear@26 1121 *info_ptr_ptr = NULL;
nuclear@26 1122 }
nuclear@26 1123
nuclear@26 1124 if (end_info_ptr != NULL)
nuclear@26 1125 {
nuclear@26 1126 #if defined(PNG_READ_TEXT_SUPPORTED)
nuclear@26 1127 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
nuclear@26 1128 #endif
nuclear@26 1129 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1130 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
nuclear@26 1131 (png_voidp)mem_ptr);
nuclear@26 1132 #else
nuclear@26 1133 png_destroy_struct((png_voidp)end_info_ptr);
nuclear@26 1134 #endif
nuclear@26 1135 *end_info_ptr_ptr = NULL;
nuclear@26 1136 }
nuclear@26 1137
nuclear@26 1138 if (png_ptr != NULL)
nuclear@26 1139 {
nuclear@26 1140 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1141 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
nuclear@26 1142 (png_voidp)mem_ptr);
nuclear@26 1143 #else
nuclear@26 1144 png_destroy_struct((png_voidp)png_ptr);
nuclear@26 1145 #endif
nuclear@26 1146 *png_ptr_ptr = NULL;
nuclear@26 1147 }
nuclear@26 1148 }
nuclear@26 1149
nuclear@26 1150 /* free all memory used by the read (old method) */
nuclear@26 1151 void /* PRIVATE */
nuclear@26 1152 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
nuclear@26 1153 {
nuclear@26 1154 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 1155 jmp_buf tmp_jmp;
nuclear@26 1156 #endif
nuclear@26 1157 png_error_ptr error_fn;
nuclear@26 1158 png_error_ptr warning_fn;
nuclear@26 1159 png_voidp error_ptr;
nuclear@26 1160 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1161 png_free_ptr free_fn;
nuclear@26 1162 #endif
nuclear@26 1163
nuclear@26 1164 png_debug(1, "in png_read_destroy\n");
nuclear@26 1165 if (info_ptr != NULL)
nuclear@26 1166 png_info_destroy(png_ptr, info_ptr);
nuclear@26 1167
nuclear@26 1168 if (end_info_ptr != NULL)
nuclear@26 1169 png_info_destroy(png_ptr, end_info_ptr);
nuclear@26 1170
nuclear@26 1171 png_free(png_ptr, png_ptr->zbuf);
nuclear@26 1172 png_free(png_ptr, png_ptr->big_row_buf);
nuclear@26 1173 png_free(png_ptr, png_ptr->prev_row);
nuclear@26 1174 png_free(png_ptr, png_ptr->chunkdata);
nuclear@26 1175 #if defined(PNG_READ_DITHER_SUPPORTED)
nuclear@26 1176 png_free(png_ptr, png_ptr->palette_lookup);
nuclear@26 1177 png_free(png_ptr, png_ptr->dither_index);
nuclear@26 1178 #endif
nuclear@26 1179 #if defined(PNG_READ_GAMMA_SUPPORTED)
nuclear@26 1180 png_free(png_ptr, png_ptr->gamma_table);
nuclear@26 1181 #endif
nuclear@26 1182 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
nuclear@26 1183 png_free(png_ptr, png_ptr->gamma_from_1);
nuclear@26 1184 png_free(png_ptr, png_ptr->gamma_to_1);
nuclear@26 1185 #endif
nuclear@26 1186 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@26 1187 if (png_ptr->free_me & PNG_FREE_PLTE)
nuclear@26 1188 png_zfree(png_ptr, png_ptr->palette);
nuclear@26 1189 png_ptr->free_me &= ~PNG_FREE_PLTE;
nuclear@26 1190 #else
nuclear@26 1191 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
nuclear@26 1192 png_zfree(png_ptr, png_ptr->palette);
nuclear@26 1193 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
nuclear@26 1194 #endif
nuclear@26 1195 #if defined(PNG_tRNS_SUPPORTED) || \
nuclear@26 1196 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
nuclear@26 1197 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@26 1198 if (png_ptr->free_me & PNG_FREE_TRNS)
nuclear@26 1199 png_free(png_ptr, png_ptr->trans);
nuclear@26 1200 png_ptr->free_me &= ~PNG_FREE_TRNS;
nuclear@26 1201 #else
nuclear@26 1202 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
nuclear@26 1203 png_free(png_ptr, png_ptr->trans);
nuclear@26 1204 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
nuclear@26 1205 #endif
nuclear@26 1206 #endif
nuclear@26 1207 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 1208 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@26 1209 if (png_ptr->free_me & PNG_FREE_HIST)
nuclear@26 1210 png_free(png_ptr, png_ptr->hist);
nuclear@26 1211 png_ptr->free_me &= ~PNG_FREE_HIST;
nuclear@26 1212 #else
nuclear@26 1213 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
nuclear@26 1214 png_free(png_ptr, png_ptr->hist);
nuclear@26 1215 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
nuclear@26 1216 #endif
nuclear@26 1217 #endif
nuclear@26 1218 #if defined(PNG_READ_GAMMA_SUPPORTED)
nuclear@26 1219 if (png_ptr->gamma_16_table != NULL)
nuclear@26 1220 {
nuclear@26 1221 int i;
nuclear@26 1222 int istop = (1 << (8 - png_ptr->gamma_shift));
nuclear@26 1223 for (i = 0; i < istop; i++)
nuclear@26 1224 {
nuclear@26 1225 png_free(png_ptr, png_ptr->gamma_16_table[i]);
nuclear@26 1226 }
nuclear@26 1227 png_free(png_ptr, png_ptr->gamma_16_table);
nuclear@26 1228 }
nuclear@26 1229 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
nuclear@26 1230 if (png_ptr->gamma_16_from_1 != NULL)
nuclear@26 1231 {
nuclear@26 1232 int i;
nuclear@26 1233 int istop = (1 << (8 - png_ptr->gamma_shift));
nuclear@26 1234 for (i = 0; i < istop; i++)
nuclear@26 1235 {
nuclear@26 1236 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
nuclear@26 1237 }
nuclear@26 1238 png_free(png_ptr, png_ptr->gamma_16_from_1);
nuclear@26 1239 }
nuclear@26 1240 if (png_ptr->gamma_16_to_1 != NULL)
nuclear@26 1241 {
nuclear@26 1242 int i;
nuclear@26 1243 int istop = (1 << (8 - png_ptr->gamma_shift));
nuclear@26 1244 for (i = 0; i < istop; i++)
nuclear@26 1245 {
nuclear@26 1246 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
nuclear@26 1247 }
nuclear@26 1248 png_free(png_ptr, png_ptr->gamma_16_to_1);
nuclear@26 1249 }
nuclear@26 1250 #endif
nuclear@26 1251 #endif
nuclear@26 1252 #if defined(PNG_TIME_RFC1123_SUPPORTED)
nuclear@26 1253 png_free(png_ptr, png_ptr->time_buffer);
nuclear@26 1254 #endif
nuclear@26 1255
nuclear@26 1256 inflateEnd(&png_ptr->zstream);
nuclear@26 1257 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
nuclear@26 1258 png_free(png_ptr, png_ptr->save_buffer);
nuclear@26 1259 #endif
nuclear@26 1260
nuclear@26 1261 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
nuclear@26 1262 #ifdef PNG_TEXT_SUPPORTED
nuclear@26 1263 png_free(png_ptr, png_ptr->current_text);
nuclear@26 1264 #endif /* PNG_TEXT_SUPPORTED */
nuclear@26 1265 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
nuclear@26 1266
nuclear@26 1267 /* Save the important info out of the png_struct, in case it is
nuclear@26 1268 * being used again.
nuclear@26 1269 */
nuclear@26 1270 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 1271 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
nuclear@26 1272 #endif
nuclear@26 1273
nuclear@26 1274 error_fn = png_ptr->error_fn;
nuclear@26 1275 warning_fn = png_ptr->warning_fn;
nuclear@26 1276 error_ptr = png_ptr->error_ptr;
nuclear@26 1277 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1278 free_fn = png_ptr->free_fn;
nuclear@26 1279 #endif
nuclear@26 1280
nuclear@26 1281 png_memset(png_ptr, 0, png_sizeof(png_struct));
nuclear@26 1282
nuclear@26 1283 png_ptr->error_fn = error_fn;
nuclear@26 1284 png_ptr->warning_fn = warning_fn;
nuclear@26 1285 png_ptr->error_ptr = error_ptr;
nuclear@26 1286 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@26 1287 png_ptr->free_fn = free_fn;
nuclear@26 1288 #endif
nuclear@26 1289
nuclear@26 1290 #ifdef PNG_SETJMP_SUPPORTED
nuclear@26 1291 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
nuclear@26 1292 #endif
nuclear@26 1293
nuclear@26 1294 }
nuclear@26 1295
nuclear@26 1296 void PNGAPI
nuclear@26 1297 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
nuclear@26 1298 {
nuclear@26 1299 if (png_ptr == NULL) return;
nuclear@26 1300 png_ptr->read_row_fn = read_row_fn;
nuclear@26 1301 }
nuclear@26 1302
nuclear@26 1303
nuclear@26 1304 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
nuclear@26 1305 #if defined(PNG_INFO_IMAGE_SUPPORTED)
nuclear@26 1306 void PNGAPI
nuclear@26 1307 png_read_png(png_structp png_ptr, png_infop info_ptr,
nuclear@26 1308 int transforms,
nuclear@26 1309 voidp params)
nuclear@26 1310 {
nuclear@26 1311 int row;
nuclear@26 1312
nuclear@26 1313 if (png_ptr == NULL) return;
nuclear@26 1314 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
nuclear@26 1315 /* invert the alpha channel from opacity to transparency
nuclear@26 1316 */
nuclear@26 1317 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
nuclear@26 1318 png_set_invert_alpha(png_ptr);
nuclear@26 1319 #endif
nuclear@26 1320
nuclear@26 1321 /* png_read_info() gives us all of the information from the
nuclear@26 1322 * PNG file before the first IDAT (image data chunk).
nuclear@26 1323 */
nuclear@26 1324 png_read_info(png_ptr, info_ptr);
nuclear@26 1325 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
nuclear@26 1326 png_error(png_ptr, "Image is too high to process with png_read_png()");
nuclear@26 1327
nuclear@26 1328 /* -------------- image transformations start here ------------------- */
nuclear@26 1329
nuclear@26 1330 #if defined(PNG_READ_16_TO_8_SUPPORTED)
nuclear@26 1331 /* tell libpng to strip 16 bit/color files down to 8 bits per color
nuclear@26 1332 */
nuclear@26 1333 if (transforms & PNG_TRANSFORM_STRIP_16)
nuclear@26 1334 png_set_strip_16(png_ptr);
nuclear@26 1335 #endif
nuclear@26 1336
nuclear@26 1337 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
nuclear@26 1338 /* Strip alpha bytes from the input data without combining with
nuclear@26 1339 * the background (not recommended).
nuclear@26 1340 */
nuclear@26 1341 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
nuclear@26 1342 png_set_strip_alpha(png_ptr);
nuclear@26 1343 #endif
nuclear@26 1344
nuclear@26 1345 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
nuclear@26 1346 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
nuclear@26 1347 * byte into separate bytes (useful for paletted and grayscale images).
nuclear@26 1348 */
nuclear@26 1349 if (transforms & PNG_TRANSFORM_PACKING)
nuclear@26 1350 png_set_packing(png_ptr);
nuclear@26 1351 #endif
nuclear@26 1352
nuclear@26 1353 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@26 1354 /* Change the order of packed pixels to least significant bit first
nuclear@26 1355 * (not useful if you are using png_set_packing).
nuclear@26 1356 */
nuclear@26 1357 if (transforms & PNG_TRANSFORM_PACKSWAP)
nuclear@26 1358 png_set_packswap(png_ptr);
nuclear@26 1359 #endif
nuclear@26 1360
nuclear@26 1361 #if defined(PNG_READ_EXPAND_SUPPORTED)
nuclear@26 1362 /* Expand paletted colors into true RGB triplets
nuclear@26 1363 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
nuclear@26 1364 * Expand paletted or RGB images with transparency to full alpha
nuclear@26 1365 * channels so the data will be available as RGBA quartets.
nuclear@26 1366 */
nuclear@26 1367 if (transforms & PNG_TRANSFORM_EXPAND)
nuclear@26 1368 if ((png_ptr->bit_depth < 8) ||
nuclear@26 1369 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
nuclear@26 1370 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
nuclear@26 1371 png_set_expand(png_ptr);
nuclear@26 1372 #endif
nuclear@26 1373
nuclear@26 1374 /* We don't handle background color or gamma transformation or dithering.
nuclear@26 1375 */
nuclear@26 1376
nuclear@26 1377 #if defined(PNG_READ_INVERT_SUPPORTED)
nuclear@26 1378 /* invert monochrome files to have 0 as white and 1 as black
nuclear@26 1379 */
nuclear@26 1380 if (transforms & PNG_TRANSFORM_INVERT_MONO)
nuclear@26 1381 png_set_invert_mono(png_ptr);
nuclear@26 1382 #endif
nuclear@26 1383
nuclear@26 1384 #if defined(PNG_READ_SHIFT_SUPPORTED)
nuclear@26 1385 /* If you want to shift the pixel values from the range [0,255] or
nuclear@26 1386 * [0,65535] to the original [0,7] or [0,31], or whatever range the
nuclear@26 1387 * colors were originally in:
nuclear@26 1388 */
nuclear@26 1389 if ((transforms & PNG_TRANSFORM_SHIFT)
nuclear@26 1390 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
nuclear@26 1391 {
nuclear@26 1392 png_color_8p sig_bit;
nuclear@26 1393
nuclear@26 1394 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
nuclear@26 1395 png_set_shift(png_ptr, sig_bit);
nuclear@26 1396 }
nuclear@26 1397 #endif
nuclear@26 1398
nuclear@26 1399 #if defined(PNG_READ_BGR_SUPPORTED)
nuclear@26 1400 /* flip the RGB pixels to BGR (or RGBA to BGRA)
nuclear@26 1401 */
nuclear@26 1402 if (transforms & PNG_TRANSFORM_BGR)
nuclear@26 1403 png_set_bgr(png_ptr);
nuclear@26 1404 #endif
nuclear@26 1405
nuclear@26 1406 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
nuclear@26 1407 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
nuclear@26 1408 */
nuclear@26 1409 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
nuclear@26 1410 png_set_swap_alpha(png_ptr);
nuclear@26 1411 #endif
nuclear@26 1412
nuclear@26 1413 #if defined(PNG_READ_SWAP_SUPPORTED)
nuclear@26 1414 /* swap bytes of 16 bit files to least significant byte first
nuclear@26 1415 */
nuclear@26 1416 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
nuclear@26 1417 png_set_swap(png_ptr);
nuclear@26 1418 #endif
nuclear@26 1419
nuclear@26 1420 /* We don't handle adding filler bytes */
nuclear@26 1421
nuclear@26 1422 /* Optional call to gamma correct and add the background to the palette
nuclear@26 1423 * and update info structure. REQUIRED if you are expecting libpng to
nuclear@26 1424 * update the palette for you (i.e., you selected such a transform above).
nuclear@26 1425 */
nuclear@26 1426 png_read_update_info(png_ptr, info_ptr);
nuclear@26 1427
nuclear@26 1428 /* -------------- image transformations end here ------------------- */
nuclear@26 1429
nuclear@26 1430 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@26 1431 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
nuclear@26 1432 #endif
nuclear@26 1433 if (info_ptr->row_pointers == NULL)
nuclear@26 1434 {
nuclear@26 1435 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
nuclear@26 1436 info_ptr->height * png_sizeof(png_bytep));
nuclear@26 1437 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@26 1438 info_ptr->free_me |= PNG_FREE_ROWS;
nuclear@26 1439 #endif
nuclear@26 1440 for (row = 0; row < (int)info_ptr->height; row++)
nuclear@26 1441 {
nuclear@26 1442 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
nuclear@26 1443 png_get_rowbytes(png_ptr, info_ptr));
nuclear@26 1444 }
nuclear@26 1445 }
nuclear@26 1446
nuclear@26 1447 png_read_image(png_ptr, info_ptr->row_pointers);
nuclear@26 1448 info_ptr->valid |= PNG_INFO_IDAT;
nuclear@26 1449
nuclear@26 1450 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
nuclear@26 1451 png_read_end(png_ptr, info_ptr);
nuclear@26 1452
nuclear@26 1453 transforms = transforms; /* quiet compiler warnings */
nuclear@26 1454 params = params;
nuclear@26 1455
nuclear@26 1456 }
nuclear@26 1457 #endif /* PNG_INFO_IMAGE_SUPPORTED */
nuclear@26 1458 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
nuclear@26 1459 #endif /* PNG_READ_SUPPORTED */