| 1 | /* |
|---|
| 2 | * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/PNG/lpng102/pngread.c#1 $ |
|---|
| 3 | * $Revision: #1 $ |
|---|
| 4 | * $DateTime: 2006/02/24 17:51:46 $ |
|---|
| 5 | * $Change: 42566 $ |
|---|
| 6 | * $Author: pryush.sharma $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /* pngread.c - read a PNG file |
|---|
| 11 | * |
|---|
| 12 | * libpng 1.0.2 - June 14, 1998 |
|---|
| 13 | * For conditions of distribution and use, see copyright notice in png.h |
|---|
| 14 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
|---|
| 15 | * Copyright (c) 1996, 1997 Andreas Dilger |
|---|
| 16 | * Copyright (c) 1998, Glenn Randers-Pehrson |
|---|
| 17 | * |
|---|
| 18 | * This file contains routines that an application calls directly to |
|---|
| 19 | * read a PNG file or stream. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #define PNG_INTERNAL |
|---|
| 23 | #include "png.h" |
|---|
| 24 | |
|---|
| 25 | /* Create a PNG structure for reading, and allocate any memory needed. */ |
|---|
| 26 | png_structp |
|---|
| 27 | png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, |
|---|
| 28 | png_error_ptr error_fn, png_error_ptr warn_fn) |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 32 | return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
|---|
| 33 | warn_fn, NULL, NULL, NULL)); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /* Alternate create PNG structure for reading, and allocate any memory needed. */ |
|---|
| 37 | png_structp |
|---|
| 38 | png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, |
|---|
| 39 | png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
|---|
| 40 | png_malloc_ptr malloc_fn, png_free_ptr free_fn) |
|---|
| 41 | { |
|---|
| 42 | #endif /* PNG_USER_MEM_SUPPORTED */ |
|---|
| 43 | |
|---|
| 44 | png_structp png_ptr; |
|---|
| 45 | #ifdef USE_FAR_KEYWORD |
|---|
| 46 | jmp_buf jmpbuf; |
|---|
| 47 | #endif |
|---|
| 48 | png_debug(1, "in png_create_read_struct\n"); |
|---|
| 49 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 50 | if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, |
|---|
| 51 | (png_malloc_ptr)malloc_fn)) == NULL) |
|---|
| 52 | #else |
|---|
| 53 | if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL) |
|---|
| 54 | #endif |
|---|
| 55 | { |
|---|
| 56 | return (png_structp)NULL; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | #ifdef USE_FAR_KEYWORD |
|---|
| 60 | if (setjmp(jmpbuf)) |
|---|
| 61 | #else |
|---|
| 62 | if (setjmp(&png_ptr->jmpbuf)) |
|---|
| 63 | #endif |
|---|
| 64 | { |
|---|
| 65 | png_free(png_ptr, png_ptr->zbuf); |
|---|
| 66 | png_destroy_struct(png_ptr); |
|---|
| 67 | return (png_structp)NULL; |
|---|
| 68 | } |
|---|
| 69 | #ifdef USE_FAR_KEYWORD |
|---|
| 70 | png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
|---|
| 71 | #endif |
|---|
| 72 | |
|---|
| 73 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 74 | png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); |
|---|
| 75 | #endif /* PNG_USER_MEM_SUPPORTED */ |
|---|
| 76 | |
|---|
| 77 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
|---|
| 78 | |
|---|
| 79 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
|---|
| 80 | * we must recompile any applications that use any older library version. |
|---|
| 81 | * For versions after libpng 1.0, we will be compatible, so we need |
|---|
| 82 | * only check the first digit. |
|---|
| 83 | */ |
|---|
| 84 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
|---|
| 85 | (user_png_ver[0] == '0' && user_png_ver[2] < '9')) |
|---|
| 86 | { |
|---|
| 87 | png_error(png_ptr, |
|---|
| 88 | "Incompatible libpng version in application and library"); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /* initialize zbuf - compression buffer */ |
|---|
| 92 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
|---|
| 93 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
|---|
| 94 | (png_uint_32)png_ptr->zbuf_size); |
|---|
| 95 | png_ptr->zstream.zalloc = png_zalloc; |
|---|
| 96 | png_ptr->zstream.zfree = png_zfree; |
|---|
| 97 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
|---|
| 98 | |
|---|
| 99 | switch (png_inflateInit(&png_ptr->zstream)) |
|---|
| 100 | { |
|---|
| 101 | case Z_OK: /* Do nothing */ break; |
|---|
| 102 | case Z_MEM_ERROR: |
|---|
| 103 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; |
|---|
| 104 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; |
|---|
| 105 | default: png_error(png_ptr, "Unknown zlib error"); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | png_ptr->zstream.next_out = png_ptr->zbuf; |
|---|
| 109 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
|---|
| 110 | |
|---|
| 111 | png_set_read_fn(png_ptr, NULL, NULL); |
|---|
| 112 | |
|---|
| 113 | return (png_ptr); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /* Initialize PNG structure for reading, and allocate any memory needed. |
|---|
| 117 | This interface is deprecated in favour of the png_create_read_struct(), |
|---|
| 118 | and it will eventually disappear. */ |
|---|
| 119 | void |
|---|
| 120 | png_read_init(png_structp png_ptr) |
|---|
| 121 | { |
|---|
| 122 | jmp_buf tmp_jmp; /* to save current jump buffer */ |
|---|
| 123 | |
|---|
| 124 | png_debug(1, "in png_read_init\n"); |
|---|
| 125 | /* save jump buffer and error functions */ |
|---|
| 126 | png_memcpy(&tmp_jmp, &png_ptr->jmpbuf, sizeof (jmp_buf)); |
|---|
| 127 | |
|---|
| 128 | /* reset all variables to 0 */ |
|---|
| 129 | png_memset(png_ptr, 0, sizeof (png_struct)); |
|---|
| 130 | |
|---|
| 131 | /* restore jump buffer */ |
|---|
| 132 | png_memcpy(&png_ptr->jmpbuf, &tmp_jmp, sizeof (jmp_buf)); |
|---|
| 133 | |
|---|
| 134 | /* initialize zbuf - compression buffer */ |
|---|
| 135 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
|---|
| 136 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
|---|
| 137 | (png_uint_32)png_ptr->zbuf_size); |
|---|
| 138 | png_ptr->zstream.zalloc = png_zalloc; |
|---|
| 139 | png_ptr->zstream.zfree = png_zfree; |
|---|
| 140 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
|---|
| 141 | |
|---|
| 142 | switch (png_inflateInit(&png_ptr->zstream)) |
|---|
| 143 | { |
|---|
| 144 | case Z_OK: /* Do nothing */ break; |
|---|
| 145 | case Z_MEM_ERROR: |
|---|
| 146 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break; |
|---|
| 147 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break; |
|---|
| 148 | default: png_error(png_ptr, "Unknown zlib error"); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | png_ptr->zstream.next_out = png_ptr->zbuf; |
|---|
| 152 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
|---|
| 153 | |
|---|
| 154 | png_set_read_fn(png_ptr, NULL, NULL); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /* Read the information before the actual image data. This has been |
|---|
| 158 | * changed in v0.90 to allow reading a file that already has the magic |
|---|
| 159 | * bytes read from the stream. You can tell libpng how many bytes have |
|---|
| 160 | * been read from the beginning of the stream (up to the maximum of 8) |
|---|
| 161 | * via png_set_sig_bytes(), and we will only check the remaining bytes |
|---|
| 162 | * here. The application can then have access to the signature bytes we |
|---|
| 163 | * read if it is determined that this isn't a valid PNG file. |
|---|
| 164 | */ |
|---|
| 165 | void |
|---|
| 166 | png_read_info(png_structp png_ptr, png_infop info_ptr) |
|---|
| 167 | { |
|---|
| 168 | png_debug(1, "in png_read_info\n"); |
|---|
| 169 | /* save jump buffer and error functions */ |
|---|
| 170 | /* If we haven't checked all of the PNG signature bytes, do so now. */ |
|---|
| 171 | if (png_ptr->sig_bytes < 8) |
|---|
| 172 | { |
|---|
| 173 | png_size_t num_checked = png_ptr->sig_bytes, |
|---|
| 174 | num_to_check = 8 - num_checked; |
|---|
| 175 | |
|---|
| 176 | png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); |
|---|
| 177 | png_ptr->sig_bytes = 8; |
|---|
| 178 | |
|---|
| 179 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) |
|---|
| 180 | { |
|---|
| 181 | if (num_checked < 4 && |
|---|
| 182 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) |
|---|
| 183 | png_error(png_ptr, "Not a PNG file"); |
|---|
| 184 | else |
|---|
| 185 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | for(;;) |
|---|
| 190 | { |
|---|
| 191 | png_byte chunk_length[4]; |
|---|
| 192 | png_uint_32 length; |
|---|
| 193 | |
|---|
| 194 | png_read_data(png_ptr, chunk_length, 4); |
|---|
| 195 | length = png_get_uint_32(chunk_length); |
|---|
| 196 | |
|---|
| 197 | png_reset_crc(png_ptr); |
|---|
| 198 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
|---|
| 199 | |
|---|
| 200 | png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name); |
|---|
| 201 | |
|---|
| 202 | /* This should be a binary subdivision search or a hash for |
|---|
| 203 | * matching the chunk name rather than a linear search. |
|---|
| 204 | */ |
|---|
| 205 | if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) |
|---|
| 206 | png_handle_IHDR(png_ptr, info_ptr, length); |
|---|
| 207 | else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) |
|---|
| 208 | png_handle_PLTE(png_ptr, info_ptr, length); |
|---|
| 209 | else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) |
|---|
| 210 | png_handle_IEND(png_ptr, info_ptr, length); |
|---|
| 211 | else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
|---|
| 212 | { |
|---|
| 213 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
|---|
| 214 | png_error(png_ptr, "Missing IHDR before IDAT"); |
|---|
| 215 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
|---|
| 216 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
|---|
| 217 | png_error(png_ptr, "Missing PLTE before IDAT"); |
|---|
| 218 | |
|---|
| 219 | png_ptr->idat_size = length; |
|---|
| 220 | png_ptr->mode |= PNG_HAVE_IDAT; |
|---|
| 221 | break; |
|---|
| 222 | } |
|---|
| 223 | #if defined(PNG_READ_bKGD_SUPPORTED) |
|---|
| 224 | else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) |
|---|
| 225 | png_handle_bKGD(png_ptr, info_ptr, length); |
|---|
| 226 | #endif |
|---|
| 227 | #if defined(PNG_READ_cHRM_SUPPORTED) |
|---|
| 228 | else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) |
|---|
| 229 | png_handle_cHRM(png_ptr, info_ptr, length); |
|---|
| 230 | #endif |
|---|
| 231 | #if defined(PNG_READ_gAMA_SUPPORTED) |
|---|
| 232 | else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) |
|---|
| 233 | png_handle_gAMA(png_ptr, info_ptr, length); |
|---|
| 234 | #endif |
|---|
| 235 | #if defined(PNG_READ_hIST_SUPPORTED) |
|---|
| 236 | else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) |
|---|
| 237 | png_handle_hIST(png_ptr, info_ptr, length); |
|---|
| 238 | #endif |
|---|
| 239 | #if defined(PNG_READ_oFFs_SUPPORTED) |
|---|
| 240 | else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) |
|---|
| 241 | png_handle_oFFs(png_ptr, info_ptr, length); |
|---|
| 242 | #endif |
|---|
| 243 | #if defined(PNG_READ_pCAL_SUPPORTED) |
|---|
| 244 | else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) |
|---|
| 245 | png_handle_pCAL(png_ptr, info_ptr, length); |
|---|
| 246 | #endif |
|---|
| 247 | #if defined(PNG_READ_pHYs_SUPPORTED) |
|---|
| 248 | else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) |
|---|
| 249 | png_handle_pHYs(png_ptr, info_ptr, length); |
|---|
| 250 | #endif |
|---|
| 251 | #if defined(PNG_READ_sBIT_SUPPORTED) |
|---|
| 252 | else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) |
|---|
| 253 | png_handle_sBIT(png_ptr, info_ptr, length); |
|---|
| 254 | #endif |
|---|
| 255 | #if defined(PNG_READ_sRGB_SUPPORTED) |
|---|
| 256 | else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) |
|---|
| 257 | png_handle_sRGB(png_ptr, info_ptr, length); |
|---|
| 258 | #endif |
|---|
| 259 | #if defined(PNG_READ_tEXt_SUPPORTED) |
|---|
| 260 | else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) |
|---|
| 261 | png_handle_tEXt(png_ptr, info_ptr, length); |
|---|
| 262 | #endif |
|---|
| 263 | #if defined(PNG_READ_tIME_SUPPORTED) |
|---|
| 264 | else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) |
|---|
| 265 | png_handle_tIME(png_ptr, info_ptr, length); |
|---|
| 266 | #endif |
|---|
| 267 | #if defined(PNG_READ_tRNS_SUPPORTED) |
|---|
| 268 | else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) |
|---|
| 269 | png_handle_tRNS(png_ptr, info_ptr, length); |
|---|
| 270 | #endif |
|---|
| 271 | #if defined(PNG_READ_zTXt_SUPPORTED) |
|---|
| 272 | else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) |
|---|
| 273 | png_handle_zTXt(png_ptr, info_ptr, length); |
|---|
| 274 | #endif |
|---|
| 275 | else |
|---|
| 276 | png_handle_unknown(png_ptr, info_ptr, length); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /* optional call to update the users info_ptr structure */ |
|---|
| 281 | void |
|---|
| 282 | png_read_update_info(png_structp png_ptr, png_infop info_ptr) |
|---|
| 283 | { |
|---|
| 284 | png_debug(1, "in png_read_update_info\n"); |
|---|
| 285 | /* save jump buffer and error functions */ |
|---|
| 286 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
|---|
| 287 | png_read_start_row(png_ptr); |
|---|
| 288 | png_read_transform_info(png_ptr, info_ptr); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | /* Initialize palette, background, etc, after transformations |
|---|
| 292 | * are set, but before any reading takes place. This allows |
|---|
| 293 | * the user to obtain a gamma-corrected palette, for example. |
|---|
| 294 | * If the user doesn't call this, we will do it ourselves. |
|---|
| 295 | */ |
|---|
| 296 | void |
|---|
| 297 | png_start_read_image(png_structp png_ptr) |
|---|
| 298 | { |
|---|
| 299 | png_debug(1, "in png_start_read_image\n"); |
|---|
| 300 | /* save jump buffer and error functions */ |
|---|
| 301 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
|---|
| 302 | png_read_start_row(png_ptr); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | void |
|---|
| 306 | png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) |
|---|
| 307 | { |
|---|
| 308 | int ret; |
|---|
| 309 | png_debug2(1, "in png_read_row (row %d, pass %d)\n", |
|---|
| 310 | png_ptr->row_number, png_ptr->pass); |
|---|
| 311 | /* save jump buffer and error functions */ |
|---|
| 312 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
|---|
| 313 | png_read_start_row(png_ptr); |
|---|
| 314 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
|---|
| 315 | { |
|---|
| 316 | /* check for transforms that have been set but were defined out */ |
|---|
| 317 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
|---|
| 318 | if (png_ptr->transformations & PNG_INVERT_MONO) |
|---|
| 319 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined."); |
|---|
| 320 | #endif |
|---|
| 321 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
|---|
| 322 | if (png_ptr->transformations & PNG_FILLER) |
|---|
| 323 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined."); |
|---|
| 324 | #endif |
|---|
| 325 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED) |
|---|
| 326 | if (png_ptr->transformations & PNG_PACKSWAP) |
|---|
| 327 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined."); |
|---|
| 328 | #endif |
|---|
| 329 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
|---|
| 330 | if (png_ptr->transformations & PNG_PACK) |
|---|
| 331 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined."); |
|---|
| 332 | #endif |
|---|
| 333 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
|---|
| 334 | if (png_ptr->transformations & PNG_SHIFT) |
|---|
| 335 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined."); |
|---|
| 336 | #endif |
|---|
| 337 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
|---|
| 338 | if (png_ptr->transformations & PNG_BGR) |
|---|
| 339 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined."); |
|---|
| 340 | #endif |
|---|
| 341 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
|---|
| 342 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
|---|
| 343 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined."); |
|---|
| 344 | #endif |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
|---|
| 348 | /* if interlaced and we do not need a new row, combine row and return */ |
|---|
| 349 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
|---|
| 350 | { |
|---|
| 351 | switch (png_ptr->pass) |
|---|
| 352 | { |
|---|
| 353 | case 0: |
|---|
| 354 | if (png_ptr->row_number & 7) |
|---|
| 355 | { |
|---|
| 356 | if (dsp_row != NULL) |
|---|
| 357 | png_combine_row(png_ptr, dsp_row, |
|---|
| 358 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 359 | png_read_finish_row(png_ptr); |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | break; |
|---|
| 363 | case 1: |
|---|
| 364 | if ((png_ptr->row_number & 7) || png_ptr->width < 5) |
|---|
| 365 | { |
|---|
| 366 | if (dsp_row != NULL) |
|---|
| 367 | png_combine_row(png_ptr, dsp_row, |
|---|
| 368 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 369 | png_read_finish_row(png_ptr); |
|---|
| 370 | return; |
|---|
| 371 | } |
|---|
| 372 | break; |
|---|
| 373 | case 2: |
|---|
| 374 | if ((png_ptr->row_number & 7) != 4) |
|---|
| 375 | { |
|---|
| 376 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
|---|
| 377 | png_combine_row(png_ptr, dsp_row, |
|---|
| 378 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 379 | png_read_finish_row(png_ptr); |
|---|
| 380 | return; |
|---|
| 381 | } |
|---|
| 382 | break; |
|---|
| 383 | case 3: |
|---|
| 384 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
|---|
| 385 | { |
|---|
| 386 | if (dsp_row != NULL) |
|---|
| 387 | png_combine_row(png_ptr, dsp_row, |
|---|
| 388 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 389 | png_read_finish_row(png_ptr); |
|---|
| 390 | return; |
|---|
| 391 | } |
|---|
| 392 | break; |
|---|
| 393 | case 4: |
|---|
| 394 | if ((png_ptr->row_number & 3) != 2) |
|---|
| 395 | { |
|---|
| 396 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
|---|
| 397 | png_combine_row(png_ptr, dsp_row, |
|---|
| 398 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 399 | png_read_finish_row(png_ptr); |
|---|
| 400 | return; |
|---|
| 401 | } |
|---|
| 402 | break; |
|---|
| 403 | case 5: |
|---|
| 404 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
|---|
| 405 | { |
|---|
| 406 | if (dsp_row != NULL) |
|---|
| 407 | png_combine_row(png_ptr, dsp_row, |
|---|
| 408 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 409 | png_read_finish_row(png_ptr); |
|---|
| 410 | return; |
|---|
| 411 | } |
|---|
| 412 | break; |
|---|
| 413 | case 6: |
|---|
| 414 | if (!(png_ptr->row_number & 1)) |
|---|
| 415 | { |
|---|
| 416 | png_read_finish_row(png_ptr); |
|---|
| 417 | return; |
|---|
| 418 | } |
|---|
| 419 | break; |
|---|
| 420 | } |
|---|
| 421 | } |
|---|
| 422 | #endif |
|---|
| 423 | |
|---|
| 424 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
|---|
| 425 | png_error(png_ptr, "Invalid attempt to read row data"); |
|---|
| 426 | |
|---|
| 427 | png_ptr->zstream.next_out = png_ptr->row_buf; |
|---|
| 428 | png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; |
|---|
| 429 | do |
|---|
| 430 | { |
|---|
| 431 | if (!(png_ptr->zstream.avail_in)) |
|---|
| 432 | { |
|---|
| 433 | while (!png_ptr->idat_size) |
|---|
| 434 | { |
|---|
| 435 | png_byte chunk_length[4]; |
|---|
| 436 | |
|---|
| 437 | png_crc_finish(png_ptr, 0); |
|---|
| 438 | |
|---|
| 439 | png_read_data(png_ptr, chunk_length, 4); |
|---|
| 440 | png_ptr->idat_size = png_get_uint_32(chunk_length); |
|---|
| 441 | |
|---|
| 442 | png_reset_crc(png_ptr); |
|---|
| 443 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
|---|
| 444 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
|---|
| 445 | png_error(png_ptr, "Not enough image data"); |
|---|
| 446 | } |
|---|
| 447 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
|---|
| 448 | png_ptr->zstream.next_in = png_ptr->zbuf; |
|---|
| 449 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
|---|
| 450 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
|---|
| 451 | png_crc_read(png_ptr, png_ptr->zbuf, |
|---|
| 452 | (png_size_t)png_ptr->zstream.avail_in); |
|---|
| 453 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
|---|
| 454 | } |
|---|
| 455 | ret = png_inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
|---|
| 456 | if (ret == Z_STREAM_END) |
|---|
| 457 | { |
|---|
| 458 | if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || |
|---|
| 459 | png_ptr->idat_size) |
|---|
| 460 | png_error(png_ptr, "Extra compressed data"); |
|---|
| 461 | png_ptr->mode |= PNG_AFTER_IDAT; |
|---|
| 462 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
|---|
| 463 | break; |
|---|
| 464 | } |
|---|
| 465 | if (ret != Z_OK) |
|---|
| 466 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
|---|
| 467 | "Decompression error"); |
|---|
| 468 | |
|---|
| 469 | } while (png_ptr->zstream.avail_out); |
|---|
| 470 | |
|---|
| 471 | png_ptr->row_info.color_type = png_ptr->color_type; |
|---|
| 472 | png_ptr->row_info.width = png_ptr->iwidth; |
|---|
| 473 | png_ptr->row_info.channels = png_ptr->channels; |
|---|
| 474 | png_ptr->row_info.bit_depth = png_ptr->bit_depth; |
|---|
| 475 | png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; |
|---|
| 476 | { |
|---|
| 477 | png_ptr->row_info.rowbytes = ((png_ptr->row_info.width * |
|---|
| 478 | (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | png_read_filter_row(png_ptr, &(png_ptr->row_info), |
|---|
| 482 | png_ptr->row_buf + 1, png_ptr->prev_row + 1, |
|---|
| 483 | (int)(png_ptr->row_buf[0])); |
|---|
| 484 | |
|---|
| 485 | png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, |
|---|
| 486 | png_ptr->rowbytes + 1); |
|---|
| 487 | |
|---|
| 488 | if (png_ptr->transformations) |
|---|
| 489 | png_do_read_transformations(png_ptr); |
|---|
| 490 | |
|---|
| 491 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
|---|
| 492 | /* blow up interlaced rows to full size */ |
|---|
| 493 | if (png_ptr->interlaced && |
|---|
| 494 | (png_ptr->transformations & PNG_INTERLACE)) |
|---|
| 495 | { |
|---|
| 496 | if (png_ptr->pass < 6) |
|---|
| 497 | png_do_read_interlace(&(png_ptr->row_info), |
|---|
| 498 | png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); |
|---|
| 499 | |
|---|
| 500 | if (dsp_row != NULL) |
|---|
| 501 | png_combine_row(png_ptr, dsp_row, |
|---|
| 502 | png_pass_dsp_mask[png_ptr->pass]); |
|---|
| 503 | if (row != NULL) |
|---|
| 504 | png_combine_row(png_ptr, row, |
|---|
| 505 | png_pass_mask[png_ptr->pass]); |
|---|
| 506 | } |
|---|
| 507 | else |
|---|
| 508 | #endif |
|---|
| 509 | { |
|---|
| 510 | if (row != NULL) |
|---|
| 511 | png_combine_row(png_ptr, row, 0xff); |
|---|
| 512 | if (dsp_row != NULL) |
|---|
| 513 | png_combine_row(png_ptr, dsp_row, 0xff); |
|---|
| 514 | } |
|---|
| 515 | png_read_finish_row(png_ptr); |
|---|
| 516 | |
|---|
| 517 | if (png_ptr->read_row_fn != NULL) |
|---|
| 518 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | /* Read one or more rows of image data. If the image is interlaced, |
|---|
| 522 | * and png_set_interlace_handling() has been called, the rows need to |
|---|
| 523 | * contain the contents of the rows from the previous pass. If the |
|---|
| 524 | * image has alpha or transparency, and png_handle_alpha()[*] has been |
|---|
| 525 | * called, the rows contents must be initialized to the contents of the |
|---|
| 526 | * screen. |
|---|
| 527 | * |
|---|
| 528 | * "row" holds the actual image, and pixels are placed in it |
|---|
| 529 | * as they arrive. If the image is displayed after each pass, it will |
|---|
| 530 | * appear to "sparkle" in. "display_row" can be used to display a |
|---|
| 531 | * "chunky" progressive image, with finer detail added as it becomes |
|---|
| 532 | * available. If you do not want this "chunky" display, you may pass |
|---|
| 533 | * NULL for display_row. If you do not want the sparkle display, and |
|---|
| 534 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
|---|
| 535 | * If you have called png_handle_alpha(), and the image has either an |
|---|
| 536 | * alpha channel or a transparency chunk, you must provide a buffer for |
|---|
| 537 | * rows. In this case, you do not have to provide a display_row buffer |
|---|
| 538 | * also, but you may. If the image is not interlaced, or if you have |
|---|
| 539 | * not called png_set_interlace_handling(), the display_row buffer will |
|---|
| 540 | * be ignored, so pass NULL to it. |
|---|
| 541 | * |
|---|
| 542 | * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2. |
|---|
| 543 | */ |
|---|
| 544 | |
|---|
| 545 | void |
|---|
| 546 | png_read_rows(png_structp png_ptr, png_bytepp row, |
|---|
| 547 | png_bytepp display_row, png_uint_32 num_rows) |
|---|
| 548 | { |
|---|
| 549 | png_uint_32 i; |
|---|
| 550 | png_bytepp rp; |
|---|
| 551 | png_bytepp dp; |
|---|
| 552 | |
|---|
| 553 | png_debug(1, "in png_read_rows\n"); |
|---|
| 554 | /* save jump buffer and error functions */ |
|---|
| 555 | rp = row; |
|---|
| 556 | dp = display_row; |
|---|
| 557 | if (rp != NULL && dp != NULL) |
|---|
| 558 | for (i = 0; i < num_rows; i++) |
|---|
| 559 | { |
|---|
| 560 | png_bytep rptr = *rp++; |
|---|
| 561 | png_bytep dptr = *dp++; |
|---|
| 562 | |
|---|
| 563 | png_read_row(png_ptr, rptr, dptr); |
|---|
| 564 | } |
|---|
| 565 | else if(rp != NULL) |
|---|
| 566 | for (i = 0; i < num_rows; i++) |
|---|
| 567 | { |
|---|
| 568 | png_bytep rptr = *rp; |
|---|
| 569 | png_read_row(png_ptr, rptr, NULL); |
|---|
| 570 | rp++; |
|---|
| 571 | } |
|---|
| 572 | else if(dp != NULL) |
|---|
| 573 | for (i = 0; i < num_rows; i++) |
|---|
| 574 | { |
|---|
| 575 | png_bytep dptr = *dp; |
|---|
| 576 | png_read_row(png_ptr, NULL, dptr); |
|---|
| 577 | dp++; |
|---|
| 578 | } |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | /* Read the entire image. If the image has an alpha channel or a tRNS |
|---|
| 582 | * chunk, and you have called png_handle_alpha()[*], you will need to |
|---|
| 583 | * initialize the image to the current image that PNG will be overlaying. |
|---|
| 584 | * We set the num_rows again here, in case it was incorrectly set in |
|---|
| 585 | * png_read_start_row() by a call to png_read_update_info() or |
|---|
| 586 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
|---|
| 587 | * prior to either of these functions like it should have been. You can |
|---|
| 588 | * only call this function once. If you desire to have an image for |
|---|
| 589 | * each pass of a interlaced image, use png_read_rows() instead. |
|---|
| 590 | * |
|---|
| 591 | * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2. |
|---|
| 592 | */ |
|---|
| 593 | void |
|---|
| 594 | png_read_image(png_structp png_ptr, png_bytepp image) |
|---|
| 595 | { |
|---|
| 596 | png_uint_32 i,image_height; |
|---|
| 597 | int pass, j; |
|---|
| 598 | png_bytepp rp; |
|---|
| 599 | |
|---|
| 600 | png_debug(1, "in png_read_image\n"); |
|---|
| 601 | /* save jump buffer and error functions */ |
|---|
| 602 | pass = png_set_interlace_handling(png_ptr); |
|---|
| 603 | |
|---|
| 604 | image_height=png_ptr->height; |
|---|
| 605 | png_ptr->num_rows = image_height; /* Make sure this is set correctly */ |
|---|
| 606 | |
|---|
| 607 | for (j = 0; j < pass; j++) |
|---|
| 608 | { |
|---|
| 609 | rp = image; |
|---|
| 610 | for (i = 0; i < image_height; i++) |
|---|
| 611 | { |
|---|
| 612 | png_read_row(png_ptr, *rp, NULL); |
|---|
| 613 | rp++; |
|---|
| 614 | } |
|---|
| 615 | } |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | /* Read the end of the PNG file. Will not read past the end of the |
|---|
| 619 | * file, will verify the end is accurate, and will read any comments |
|---|
| 620 | * or time information at the end of the file, if info is not NULL. |
|---|
| 621 | */ |
|---|
| 622 | void |
|---|
| 623 | png_read_end(png_structp png_ptr, png_infop info_ptr) |
|---|
| 624 | { |
|---|
| 625 | png_byte chunk_length[4]; |
|---|
| 626 | png_uint_32 length; |
|---|
| 627 | |
|---|
| 628 | png_debug(1, "in png_read_end\n"); |
|---|
| 629 | /* save jump buffer and error functions */ |
|---|
| 630 | png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ |
|---|
| 631 | |
|---|
| 632 | do |
|---|
| 633 | { |
|---|
| 634 | png_read_data(png_ptr, chunk_length, 4); |
|---|
| 635 | length = png_get_uint_32(chunk_length); |
|---|
| 636 | |
|---|
| 637 | png_reset_crc(png_ptr); |
|---|
| 638 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
|---|
| 639 | |
|---|
| 640 | png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name); |
|---|
| 641 | |
|---|
| 642 | if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) |
|---|
| 643 | png_handle_IHDR(png_ptr, info_ptr, length); |
|---|
| 644 | else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
|---|
| 645 | { |
|---|
| 646 | /* Zero length IDATs are legal after the last IDAT has been |
|---|
| 647 | * read, but not after other chunks have been read. |
|---|
| 648 | */ |
|---|
| 649 | if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT) |
|---|
| 650 | png_error(png_ptr, "Too many IDAT's found"); |
|---|
| 651 | else |
|---|
| 652 | png_crc_finish(png_ptr, 0); |
|---|
| 653 | } |
|---|
| 654 | else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) |
|---|
| 655 | png_handle_PLTE(png_ptr, info_ptr, length); |
|---|
| 656 | else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) |
|---|
| 657 | png_handle_IEND(png_ptr, info_ptr, length); |
|---|
| 658 | #if defined(PNG_READ_bKGD_SUPPORTED) |
|---|
| 659 | else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) |
|---|
| 660 | png_handle_bKGD(png_ptr, info_ptr, length); |
|---|
| 661 | #endif |
|---|
| 662 | #if defined(PNG_READ_cHRM_SUPPORTED) |
|---|
| 663 | else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) |
|---|
| 664 | png_handle_cHRM(png_ptr, info_ptr, length); |
|---|
| 665 | #endif |
|---|
| 666 | #if defined(PNG_READ_gAMA_SUPPORTED) |
|---|
| 667 | else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) |
|---|
| 668 | png_handle_gAMA(png_ptr, info_ptr, length); |
|---|
| 669 | #endif |
|---|
| 670 | #if defined(PNG_READ_hIST_SUPPORTED) |
|---|
| 671 | else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) |
|---|
| 672 | png_handle_hIST(png_ptr, info_ptr, length); |
|---|
| 673 | #endif |
|---|
| 674 | #if defined(PNG_READ_oFFs_SUPPORTED) |
|---|
| 675 | else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) |
|---|
| 676 | png_handle_oFFs(png_ptr, info_ptr, length); |
|---|
| 677 | #endif |
|---|
| 678 | #if defined(PNG_READ_pCAL_SUPPORTED) |
|---|
| 679 | else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) |
|---|
| 680 | png_handle_pCAL(png_ptr, info_ptr, length); |
|---|
| 681 | #endif |
|---|
| 682 | #if defined(PNG_READ_pHYs_SUPPORTED) |
|---|
| 683 | else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) |
|---|
| 684 | png_handle_pHYs(png_ptr, info_ptr, length); |
|---|
| 685 | #endif |
|---|
| 686 | #if defined(PNG_READ_sBIT_SUPPORTED) |
|---|
| 687 | else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) |
|---|
| 688 | png_handle_sBIT(png_ptr, info_ptr, length); |
|---|
| 689 | #endif |
|---|
| 690 | #if defined(PNG_READ_sRGB_SUPPORTED) |
|---|
| 691 | else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) |
|---|
| 692 | png_handle_sRGB(png_ptr, info_ptr, length); |
|---|
| 693 | #endif |
|---|
| 694 | #if defined(PNG_READ_tEXt_SUPPORTED) |
|---|
| 695 | else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) |
|---|
| 696 | png_handle_tEXt(png_ptr, info_ptr, length); |
|---|
| 697 | #endif |
|---|
| 698 | #if defined(PNG_READ_tIME_SUPPORTED) |
|---|
| 699 | else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) |
|---|
| 700 | png_handle_tIME(png_ptr, info_ptr, length); |
|---|
| 701 | #endif |
|---|
| 702 | #if defined(PNG_READ_tRNS_SUPPORTED) |
|---|
| 703 | else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) |
|---|
| 704 | png_handle_tRNS(png_ptr, info_ptr, length); |
|---|
| 705 | #endif |
|---|
| 706 | #if defined(PNG_READ_zTXt_SUPPORTED) |
|---|
| 707 | else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) |
|---|
| 708 | png_handle_zTXt(png_ptr, info_ptr, length); |
|---|
| 709 | #endif |
|---|
| 710 | else |
|---|
| 711 | png_handle_unknown(png_ptr, info_ptr, length); |
|---|
| 712 | } while (!(png_ptr->mode & PNG_HAVE_IEND)); |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | /* free all memory used by the read */ |
|---|
| 716 | void |
|---|
| 717 | png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
|---|
| 718 | png_infopp end_info_ptr_ptr) |
|---|
| 719 | { |
|---|
| 720 | png_structp png_ptr = NULL; |
|---|
| 721 | png_infop info_ptr = NULL, end_info_ptr = NULL; |
|---|
| 722 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 723 | png_free_ptr free_fn = NULL; |
|---|
| 724 | #endif /* PNG_USER_MEM_SUPPORTED */ |
|---|
| 725 | |
|---|
| 726 | png_debug(1, "in png_destroy_read_struct\n"); |
|---|
| 727 | /* save jump buffer and error functions */ |
|---|
| 728 | if (png_ptr_ptr != NULL) |
|---|
| 729 | png_ptr = *png_ptr_ptr; |
|---|
| 730 | |
|---|
| 731 | if (info_ptr_ptr != NULL) |
|---|
| 732 | info_ptr = *info_ptr_ptr; |
|---|
| 733 | |
|---|
| 734 | if (end_info_ptr_ptr != NULL) |
|---|
| 735 | end_info_ptr = *end_info_ptr_ptr; |
|---|
| 736 | |
|---|
| 737 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 738 | free_fn = png_ptr->free_fn; |
|---|
| 739 | #endif |
|---|
| 740 | |
|---|
| 741 | png_read_destroy(png_ptr, info_ptr, end_info_ptr); |
|---|
| 742 | |
|---|
| 743 | if (info_ptr != NULL) |
|---|
| 744 | { |
|---|
| 745 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
|---|
| 746 | png_free(png_ptr, info_ptr->text); |
|---|
| 747 | #endif |
|---|
| 748 | |
|---|
| 749 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 750 | png_destroy_struct_2((png_voidp)info_ptr, free_fn); |
|---|
| 751 | #else |
|---|
| 752 | png_destroy_struct((png_voidp)info_ptr); |
|---|
| 753 | #endif |
|---|
| 754 | *info_ptr_ptr = (png_infop)NULL; |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | if (end_info_ptr != NULL) |
|---|
| 758 | { |
|---|
| 759 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
|---|
| 760 | png_free(png_ptr, end_info_ptr->text); |
|---|
| 761 | #endif |
|---|
| 762 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 763 | png_destroy_struct_2((png_voidp)end_info_ptr, free_fn); |
|---|
| 764 | #else |
|---|
| 765 | png_destroy_struct((png_voidp)end_info_ptr); |
|---|
| 766 | #endif |
|---|
| 767 | *end_info_ptr_ptr = (png_infop)NULL; |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | if (png_ptr != NULL) |
|---|
| 771 | { |
|---|
| 772 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 773 | png_destroy_struct_2((png_voidp)png_ptr, free_fn); |
|---|
| 774 | #else |
|---|
| 775 | png_destroy_struct((png_voidp)png_ptr); |
|---|
| 776 | #endif |
|---|
| 777 | *png_ptr_ptr = (png_structp)NULL; |
|---|
| 778 | } |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | /* free all memory used by the read (old method) */ |
|---|
| 782 | void |
|---|
| 783 | png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr) |
|---|
| 784 | { |
|---|
| 785 | jmp_buf tmp_jmp; |
|---|
| 786 | png_error_ptr error_fn; |
|---|
| 787 | png_error_ptr warning_fn; |
|---|
| 788 | png_voidp error_ptr; |
|---|
| 789 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 790 | png_free_ptr free_fn; |
|---|
| 791 | #endif |
|---|
| 792 | |
|---|
| 793 | png_debug(1, "in png_read_destroy\n"); |
|---|
| 794 | /* save jump buffer and error functions */ |
|---|
| 795 | if (info_ptr != NULL) |
|---|
| 796 | png_info_destroy(png_ptr, info_ptr); |
|---|
| 797 | |
|---|
| 798 | if (end_info_ptr != NULL) |
|---|
| 799 | png_info_destroy(png_ptr, end_info_ptr); |
|---|
| 800 | |
|---|
| 801 | png_free(png_ptr, png_ptr->zbuf); |
|---|
| 802 | png_free(png_ptr, png_ptr->row_buf); |
|---|
| 803 | png_free(png_ptr, png_ptr->prev_row); |
|---|
| 804 | #if defined(PNG_READ_DITHER_SUPPORTED) |
|---|
| 805 | png_free(png_ptr, png_ptr->palette_lookup); |
|---|
| 806 | png_free(png_ptr, png_ptr->dither_index); |
|---|
| 807 | #endif |
|---|
| 808 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
|---|
| 809 | png_free(png_ptr, png_ptr->gamma_table); |
|---|
| 810 | #endif |
|---|
| 811 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
|---|
| 812 | png_free(png_ptr, png_ptr->gamma_from_1); |
|---|
| 813 | png_free(png_ptr, png_ptr->gamma_to_1); |
|---|
| 814 | #endif |
|---|
| 815 | if (png_ptr->flags & PNG_FLAG_FREE_PALETTE) |
|---|
| 816 | png_zfree(png_ptr, png_ptr->palette); |
|---|
| 817 | if (png_ptr->flags & PNG_FLAG_FREE_TRANS) |
|---|
| 818 | png_free(png_ptr, png_ptr->trans); |
|---|
| 819 | #if defined(PNG_READ_hIST_SUPPORTED) |
|---|
| 820 | if (png_ptr->flags & PNG_FLAG_FREE_HIST) |
|---|
| 821 | png_free(png_ptr, png_ptr->hist); |
|---|
| 822 | #endif |
|---|
| 823 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
|---|
| 824 | if (png_ptr->gamma_16_table != NULL) |
|---|
| 825 | { |
|---|
| 826 | int i; |
|---|
| 827 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
|---|
| 828 | for (i = 0; i < istop; i++) |
|---|
| 829 | { |
|---|
| 830 | png_free(png_ptr, png_ptr->gamma_16_table[i]); |
|---|
| 831 | } |
|---|
| 832 | } |
|---|
| 833 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
|---|
| 834 | png_free(png_ptr, png_ptr->gamma_16_table); |
|---|
| 835 | if (png_ptr->gamma_16_from_1 != NULL) |
|---|
| 836 | { |
|---|
| 837 | int i; |
|---|
| 838 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
|---|
| 839 | for (i = 0; i < istop; i++) |
|---|
| 840 | { |
|---|
| 841 | png_free(png_ptr, png_ptr->gamma_16_from_1[i]); |
|---|
| 842 | } |
|---|
| 843 | } |
|---|
| 844 | png_free(png_ptr, png_ptr->gamma_16_from_1); |
|---|
| 845 | if (png_ptr->gamma_16_to_1 != NULL) |
|---|
| 846 | { |
|---|
| 847 | int i; |
|---|
| 848 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
|---|
| 849 | for (i = 0; i < istop; i++) |
|---|
| 850 | { |
|---|
| 851 | png_free(png_ptr, png_ptr->gamma_16_to_1[i]); |
|---|
| 852 | } |
|---|
| 853 | } |
|---|
| 854 | png_free(png_ptr, png_ptr->gamma_16_to_1); |
|---|
| 855 | #endif |
|---|
| 856 | #endif |
|---|
| 857 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
|---|
| 858 | png_free(png_ptr, png_ptr->time_buffer); |
|---|
| 859 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
|---|
| 860 | |
|---|
| 861 | png_inflateEnd(&png_ptr->zstream); |
|---|
| 862 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
|---|
| 863 | png_free(png_ptr, png_ptr->save_buffer); |
|---|
| 864 | #endif |
|---|
| 865 | |
|---|
| 866 | /* Save the important info out of the png_struct, in case it is |
|---|
| 867 | * being used again. |
|---|
| 868 | */ |
|---|
| 869 | png_memcpy(&tmp_jmp, &png_ptr->jmpbuf, sizeof (jmp_buf)); |
|---|
| 870 | |
|---|
| 871 | error_fn = png_ptr->error_fn; |
|---|
| 872 | warning_fn = png_ptr->warning_fn; |
|---|
| 873 | error_ptr = png_ptr->error_ptr; |
|---|
| 874 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 875 | free_fn = png_ptr->free_fn; |
|---|
| 876 | #endif |
|---|
| 877 | |
|---|
| 878 | png_memset(png_ptr, 0, sizeof (png_struct)); |
|---|
| 879 | |
|---|
| 880 | png_ptr->error_fn = error_fn; |
|---|
| 881 | png_ptr->warning_fn = warning_fn; |
|---|
| 882 | png_ptr->error_ptr = error_ptr; |
|---|
| 883 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 884 | png_ptr->free_fn = free_fn; |
|---|
| 885 | #endif |
|---|
| 886 | |
|---|
| 887 | png_memcpy(&png_ptr->jmpbuf, &tmp_jmp, sizeof (jmp_buf)); |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | void |
|---|
| 891 | png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) |
|---|
| 892 | { |
|---|
| 893 | png_ptr->read_row_fn = read_row_fn; |
|---|
| 894 | } |
|---|