2 /* pngread.c - read a PNG file
\r
4 * Last changed in libpng 1.2.30 [August 15, 2008]
\r
5 * For conditions of distribution and use, see copyright notice in png.h
\r
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
\r
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
\r
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
\r
10 * This file contains routines that an application calls directly to
\r
11 * read a PNG file or stream.
\r
14 #define PNG_INTERNAL
\r
16 #if defined(PNG_READ_SUPPORTED)
\r
18 /* Create a PNG structure for reading, and allocate any memory needed. */
\r
20 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
\r
21 png_error_ptr error_fn, png_error_ptr warn_fn)
\r
24 #ifdef PNG_USER_MEM_SUPPORTED
\r
25 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
\r
26 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
\r
29 /* Alternate create PNG structure for reading, and allocate any memory needed. */
\r
31 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
\r
32 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
\r
33 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
\r
35 #endif /* PNG_USER_MEM_SUPPORTED */
\r
37 #ifdef PNG_SETJMP_SUPPORTED
\r
40 png_structp png_ptr;
\r
42 #ifdef PNG_SETJMP_SUPPORTED
\r
43 #ifdef USE_FAR_KEYWORD
\r
50 png_debug(1, "in png_create_read_struct\n");
\r
51 #ifdef PNG_USER_MEM_SUPPORTED
\r
52 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
\r
53 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
\r
55 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
\r
57 if (png_ptr == NULL)
\r
60 /* added at libpng-1.2.6 */
\r
61 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
\r
62 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
\r
63 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
\r
66 #ifdef PNG_SETJMP_SUPPORTED
\r
67 #ifdef USE_FAR_KEYWORD
\r
70 if (setjmp(png_ptr->jmpbuf))
\r
73 png_free(png_ptr, png_ptr->zbuf);
\r
74 png_ptr->zbuf = NULL;
\r
75 #ifdef PNG_USER_MEM_SUPPORTED
\r
76 png_destroy_struct_2((png_voidp)png_ptr,
\r
77 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
\r
79 png_destroy_struct((png_voidp)png_ptr);
\r
83 #ifdef USE_FAR_KEYWORD
\r
84 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
\r
88 #ifdef PNG_USER_MEM_SUPPORTED
\r
89 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
\r
92 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
\r
99 if (user_png_ver[i] != png_libpng_ver[i])
\r
100 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
\r
101 } while (png_libpng_ver[i++]);
\r
104 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
\r
107 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
\r
109 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
\r
110 * we must recompile any applications that use any older library version.
\r
111 * For versions after libpng 1.0, we will be compatible, so we need
\r
112 * only check the first digit.
\r
114 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
\r
115 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
\r
116 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
\r
118 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
\r
122 png_snprintf(msg, 80,
\r
123 "Application was compiled with png.h from libpng-%.20s",
\r
125 png_warning(png_ptr, msg);
\r
127 png_snprintf(msg, 80,
\r
128 "Application is running with png.c from libpng-%.20s",
\r
130 png_warning(png_ptr, msg);
\r
132 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
\r
133 png_ptr->flags = 0;
\r
136 "Incompatible libpng version in application and library");
\r
140 /* initialize zbuf - compression buffer */
\r
141 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
\r
142 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
\r
143 (png_uint_32)png_ptr->zbuf_size);
\r
144 png_ptr->zstream.zalloc = png_zalloc;
\r
145 png_ptr->zstream.zfree = png_zfree;
\r
146 png_ptr->zstream.opaque = (voidpf)png_ptr;
\r
148 switch (inflateInit(&png_ptr->zstream))
\r
150 case Z_OK: /* Do nothing */ break;
\r
152 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
\r
153 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
\r
154 default: png_error(png_ptr, "Unknown zlib error");
\r
157 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
158 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
160 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
\r
162 #ifdef PNG_SETJMP_SUPPORTED
\r
163 /* Applications that neglect to set up their own setjmp() and then encounter
\r
164 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
\r
165 abort instead of returning. */
\r
166 #ifdef USE_FAR_KEYWORD
\r
167 if (setjmp(jmpbuf))
\r
169 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
\r
171 if (setjmp(png_ptr->jmpbuf))
\r
178 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
\r
179 /* Initialize PNG structure for reading, and allocate any memory needed.
\r
180 This interface is deprecated in favour of the png_create_read_struct(),
\r
181 and it will disappear as of libpng-1.3.0. */
\r
182 #undef png_read_init
\r
184 png_read_init(png_structp png_ptr)
\r
186 /* We only come here via pre-1.0.7-compiled applications */
\r
187 png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
\r
191 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
\r
192 png_size_t png_struct_size, png_size_t png_info_size)
\r
194 /* We only come here via pre-1.0.12-compiled applications */
\r
195 if (png_ptr == NULL) return;
\r
196 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
\r
197 if (png_sizeof(png_struct) > png_struct_size ||
\r
198 png_sizeof(png_info) > png_info_size)
\r
201 png_ptr->warning_fn = NULL;
\r
204 png_snprintf(msg, 80,
\r
205 "Application was compiled with png.h from libpng-%.20s",
\r
207 png_warning(png_ptr, msg);
\r
209 png_snprintf(msg, 80,
\r
210 "Application is running with png.c from libpng-%.20s",
\r
212 png_warning(png_ptr, msg);
\r
215 if (png_sizeof(png_struct) > png_struct_size)
\r
217 png_ptr->error_fn = NULL;
\r
218 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
\r
219 png_ptr->flags = 0;
\r
222 "The png struct allocated by the application for reading is too small.");
\r
224 if (png_sizeof(png_info) > png_info_size)
\r
226 png_ptr->error_fn = NULL;
\r
227 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
\r
228 png_ptr->flags = 0;
\r
231 "The info struct allocated by application for reading is too small.");
\r
233 png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
\r
235 #endif /* PNG_1_0_X || PNG_1_2_X */
\r
238 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
\r
239 png_size_t png_struct_size)
\r
241 #ifdef PNG_SETJMP_SUPPORTED
\r
242 jmp_buf tmp_jmp; /* to save current jump buffer */
\r
247 png_structp png_ptr=*ptr_ptr;
\r
249 if (png_ptr == NULL) return;
\r
253 if (user_png_ver[i] != png_libpng_ver[i])
\r
255 #ifdef PNG_LEGACY_SUPPORTED
\r
256 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
\r
258 png_ptr->warning_fn = NULL;
\r
259 png_warning(png_ptr,
\r
260 "Application uses deprecated png_read_init() and should be recompiled.");
\r
264 } while (png_libpng_ver[i++]);
\r
266 png_debug(1, "in png_read_init_3\n");
\r
268 #ifdef PNG_SETJMP_SUPPORTED
\r
269 /* save jump buffer and error functions */
\r
270 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
\r
273 if (png_sizeof(png_struct) > png_struct_size)
\r
275 png_destroy_struct(png_ptr);
\r
276 *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
\r
277 png_ptr = *ptr_ptr;
\r
280 /* reset all variables to 0 */
\r
281 png_memset(png_ptr, 0, png_sizeof(png_struct));
\r
283 #ifdef PNG_SETJMP_SUPPORTED
\r
284 /* restore jump buffer */
\r
285 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
\r
288 /* added at libpng-1.2.6 */
\r
289 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
\r
290 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
\r
291 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
\r
294 /* initialize zbuf - compression buffer */
\r
295 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
\r
296 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
\r
297 (png_uint_32)png_ptr->zbuf_size);
\r
298 png_ptr->zstream.zalloc = png_zalloc;
\r
299 png_ptr->zstream.zfree = png_zfree;
\r
300 png_ptr->zstream.opaque = (voidpf)png_ptr;
\r
302 switch (inflateInit(&png_ptr->zstream))
\r
304 case Z_OK: /* Do nothing */ break;
\r
306 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
\r
307 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
\r
308 default: png_error(png_ptr, "Unknown zlib error");
\r
311 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
312 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
314 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
\r
317 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
318 /* Read the information before the actual image data. This has been
\r
319 * changed in v0.90 to allow reading a file that already has the magic
\r
320 * bytes read from the stream. You can tell libpng how many bytes have
\r
321 * been read from the beginning of the stream (up to the maximum of 8)
\r
322 * via png_set_sig_bytes(), and we will only check the remaining bytes
\r
323 * here. The application can then have access to the signature bytes we
\r
324 * read if it is determined that this isn't a valid PNG file.
\r
327 png_read_info(png_structp png_ptr, png_infop info_ptr)
\r
329 if (png_ptr == NULL || info_ptr == NULL) return;
\r
330 png_debug(1, "in png_read_info\n");
\r
331 /* If we haven't checked all of the PNG signature bytes, do so now. */
\r
332 if (png_ptr->sig_bytes < 8)
\r
334 png_size_t num_checked = png_ptr->sig_bytes,
\r
335 num_to_check = 8 - num_checked;
\r
337 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
\r
338 png_ptr->sig_bytes = 8;
\r
340 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
\r
342 if (num_checked < 4 &&
\r
343 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
\r
344 png_error(png_ptr, "Not a PNG file");
\r
346 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
\r
348 if (num_checked < 3)
\r
349 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
\r
354 #ifdef PNG_USE_LOCAL_ARRAYS
\r
355 PNG_CONST PNG_IHDR;
\r
356 PNG_CONST PNG_IDAT;
\r
357 PNG_CONST PNG_IEND;
\r
358 PNG_CONST PNG_PLTE;
\r
359 #if defined(PNG_READ_bKGD_SUPPORTED)
\r
360 PNG_CONST PNG_bKGD;
\r
362 #if defined(PNG_READ_cHRM_SUPPORTED)
\r
363 PNG_CONST PNG_cHRM;
\r
365 #if defined(PNG_READ_gAMA_SUPPORTED)
\r
366 PNG_CONST PNG_gAMA;
\r
368 #if defined(PNG_READ_hIST_SUPPORTED)
\r
369 PNG_CONST PNG_hIST;
\r
371 #if defined(PNG_READ_iCCP_SUPPORTED)
\r
372 PNG_CONST PNG_iCCP;
\r
374 #if defined(PNG_READ_iTXt_SUPPORTED)
\r
375 PNG_CONST PNG_iTXt;
\r
377 #if defined(PNG_READ_oFFs_SUPPORTED)
\r
378 PNG_CONST PNG_oFFs;
\r
380 #if defined(PNG_READ_pCAL_SUPPORTED)
\r
381 PNG_CONST PNG_pCAL;
\r
383 #if defined(PNG_READ_pHYs_SUPPORTED)
\r
384 PNG_CONST PNG_pHYs;
\r
386 #if defined(PNG_READ_sBIT_SUPPORTED)
\r
387 PNG_CONST PNG_sBIT;
\r
389 #if defined(PNG_READ_sCAL_SUPPORTED)
\r
390 PNG_CONST PNG_sCAL;
\r
392 #if defined(PNG_READ_sPLT_SUPPORTED)
\r
393 PNG_CONST PNG_sPLT;
\r
395 #if defined(PNG_READ_sRGB_SUPPORTED)
\r
396 PNG_CONST PNG_sRGB;
\r
398 #if defined(PNG_READ_tEXt_SUPPORTED)
\r
399 PNG_CONST PNG_tEXt;
\r
401 #if defined(PNG_READ_tIME_SUPPORTED)
\r
402 PNG_CONST PNG_tIME;
\r
404 #if defined(PNG_READ_tRNS_SUPPORTED)
\r
405 PNG_CONST PNG_tRNS;
\r
407 #if defined(PNG_READ_zTXt_SUPPORTED)
\r
408 PNG_CONST PNG_zTXt;
\r
410 #endif /* PNG_USE_LOCAL_ARRAYS */
\r
411 png_uint_32 length = png_read_chunk_header(png_ptr);
\r
412 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
\r
414 /* This should be a binary subdivision search or a hash for
\r
415 * matching the chunk name rather than a linear search.
\r
417 if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
418 if (png_ptr->mode & PNG_AFTER_IDAT)
\r
419 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
\r
421 if (!png_memcmp(chunk_name, png_IHDR, 4))
\r
422 png_handle_IHDR(png_ptr, info_ptr, length);
\r
423 else if (!png_memcmp(chunk_name, png_IEND, 4))
\r
424 png_handle_IEND(png_ptr, info_ptr, length);
\r
425 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
\r
426 else if (png_handle_as_unknown(png_ptr, chunk_name))
\r
428 if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
429 png_ptr->mode |= PNG_HAVE_IDAT;
\r
430 png_handle_unknown(png_ptr, info_ptr, length);
\r
431 if (!png_memcmp(chunk_name, png_PLTE, 4))
\r
432 png_ptr->mode |= PNG_HAVE_PLTE;
\r
433 else if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
435 if (!(png_ptr->mode & PNG_HAVE_IHDR))
\r
436 png_error(png_ptr, "Missing IHDR before IDAT");
\r
437 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
\r
438 !(png_ptr->mode & PNG_HAVE_PLTE))
\r
439 png_error(png_ptr, "Missing PLTE before IDAT");
\r
444 else if (!png_memcmp(chunk_name, png_PLTE, 4))
\r
445 png_handle_PLTE(png_ptr, info_ptr, length);
\r
446 else if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
448 if (!(png_ptr->mode & PNG_HAVE_IHDR))
\r
449 png_error(png_ptr, "Missing IHDR before IDAT");
\r
450 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
\r
451 !(png_ptr->mode & PNG_HAVE_PLTE))
\r
452 png_error(png_ptr, "Missing PLTE before IDAT");
\r
454 png_ptr->idat_size = length;
\r
455 png_ptr->mode |= PNG_HAVE_IDAT;
\r
458 #if defined(PNG_READ_bKGD_SUPPORTED)
\r
459 else if (!png_memcmp(chunk_name, png_bKGD, 4))
\r
460 png_handle_bKGD(png_ptr, info_ptr, length);
\r
462 #if defined(PNG_READ_cHRM_SUPPORTED)
\r
463 else if (!png_memcmp(chunk_name, png_cHRM, 4))
\r
464 png_handle_cHRM(png_ptr, info_ptr, length);
\r
466 #if defined(PNG_READ_gAMA_SUPPORTED)
\r
467 else if (!png_memcmp(chunk_name, png_gAMA, 4))
\r
468 png_handle_gAMA(png_ptr, info_ptr, length);
\r
470 #if defined(PNG_READ_hIST_SUPPORTED)
\r
471 else if (!png_memcmp(chunk_name, png_hIST, 4))
\r
472 png_handle_hIST(png_ptr, info_ptr, length);
\r
474 #if defined(PNG_READ_oFFs_SUPPORTED)
\r
475 else if (!png_memcmp(chunk_name, png_oFFs, 4))
\r
476 png_handle_oFFs(png_ptr, info_ptr, length);
\r
478 #if defined(PNG_READ_pCAL_SUPPORTED)
\r
479 else if (!png_memcmp(chunk_name, png_pCAL, 4))
\r
480 png_handle_pCAL(png_ptr, info_ptr, length);
\r
482 #if defined(PNG_READ_sCAL_SUPPORTED)
\r
483 else if (!png_memcmp(chunk_name, png_sCAL, 4))
\r
484 png_handle_sCAL(png_ptr, info_ptr, length);
\r
486 #if defined(PNG_READ_pHYs_SUPPORTED)
\r
487 else if (!png_memcmp(chunk_name, png_pHYs, 4))
\r
488 png_handle_pHYs(png_ptr, info_ptr, length);
\r
490 #if defined(PNG_READ_sBIT_SUPPORTED)
\r
491 else if (!png_memcmp(chunk_name, png_sBIT, 4))
\r
492 png_handle_sBIT(png_ptr, info_ptr, length);
\r
494 #if defined(PNG_READ_sRGB_SUPPORTED)
\r
495 else if (!png_memcmp(chunk_name, png_sRGB, 4))
\r
496 png_handle_sRGB(png_ptr, info_ptr, length);
\r
498 #if defined(PNG_READ_iCCP_SUPPORTED)
\r
499 else if (!png_memcmp(chunk_name, png_iCCP, 4))
\r
500 png_handle_iCCP(png_ptr, info_ptr, length);
\r
502 #if defined(PNG_READ_sPLT_SUPPORTED)
\r
503 else if (!png_memcmp(chunk_name, png_sPLT, 4))
\r
504 png_handle_sPLT(png_ptr, info_ptr, length);
\r
506 #if defined(PNG_READ_tEXt_SUPPORTED)
\r
507 else if (!png_memcmp(chunk_name, png_tEXt, 4))
\r
508 png_handle_tEXt(png_ptr, info_ptr, length);
\r
510 #if defined(PNG_READ_tIME_SUPPORTED)
\r
511 else if (!png_memcmp(chunk_name, png_tIME, 4))
\r
512 png_handle_tIME(png_ptr, info_ptr, length);
\r
514 #if defined(PNG_READ_tRNS_SUPPORTED)
\r
515 else if (!png_memcmp(chunk_name, png_tRNS, 4))
\r
516 png_handle_tRNS(png_ptr, info_ptr, length);
\r
518 #if defined(PNG_READ_zTXt_SUPPORTED)
\r
519 else if (!png_memcmp(chunk_name, png_zTXt, 4))
\r
520 png_handle_zTXt(png_ptr, info_ptr, length);
\r
522 #if defined(PNG_READ_iTXt_SUPPORTED)
\r
523 else if (!png_memcmp(chunk_name, png_iTXt, 4))
\r
524 png_handle_iTXt(png_ptr, info_ptr, length);
\r
527 png_handle_unknown(png_ptr, info_ptr, length);
\r
530 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
532 /* optional call to update the users info_ptr structure */
\r
534 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
\r
536 png_debug(1, "in png_read_update_info\n");
\r
537 if (png_ptr == NULL) return;
\r
538 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
\r
539 png_read_start_row(png_ptr);
\r
541 png_warning(png_ptr,
\r
542 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
\r
543 png_read_transform_info(png_ptr, info_ptr);
\r
546 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
547 /* Initialize palette, background, etc, after transformations
\r
548 * are set, but before any reading takes place. This allows
\r
549 * the user to obtain a gamma-corrected palette, for example.
\r
550 * If the user doesn't call this, we will do it ourselves.
\r
553 png_start_read_image(png_structp png_ptr)
\r
555 png_debug(1, "in png_start_read_image\n");
\r
556 if (png_ptr == NULL) return;
\r
557 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
\r
558 png_read_start_row(png_ptr);
\r
560 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
562 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
564 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
\r
566 #ifdef PNG_USE_LOCAL_ARRAYS
\r
567 PNG_CONST PNG_IDAT;
\r
568 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
\r
570 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
\r
573 if (png_ptr == NULL) return;
\r
574 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
\r
575 png_ptr->row_number, png_ptr->pass);
\r
576 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
\r
577 png_read_start_row(png_ptr);
\r
578 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
\r
580 /* check for transforms that have been set but were defined out */
\r
581 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
\r
582 if (png_ptr->transformations & PNG_INVERT_MONO)
\r
583 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
\r
585 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
\r
586 if (png_ptr->transformations & PNG_FILLER)
\r
587 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
\r
589 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
\r
590 if (png_ptr->transformations & PNG_PACKSWAP)
\r
591 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
\r
593 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
\r
594 if (png_ptr->transformations & PNG_PACK)
\r
595 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
\r
597 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
\r
598 if (png_ptr->transformations & PNG_SHIFT)
\r
599 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
\r
601 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
\r
602 if (png_ptr->transformations & PNG_BGR)
\r
603 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
\r
605 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
\r
606 if (png_ptr->transformations & PNG_SWAP_BYTES)
\r
607 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
\r
611 #if defined(PNG_READ_INTERLACING_SUPPORTED)
\r
612 /* if interlaced and we do not need a new row, combine row and return */
\r
613 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
\r
615 switch (png_ptr->pass)
\r
618 if (png_ptr->row_number & 0x07)
\r
620 if (dsp_row != NULL)
\r
621 png_combine_row(png_ptr, dsp_row,
\r
622 png_pass_dsp_mask[png_ptr->pass]);
\r
623 png_read_finish_row(png_ptr);
\r
628 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
\r
630 if (dsp_row != NULL)
\r
631 png_combine_row(png_ptr, dsp_row,
\r
632 png_pass_dsp_mask[png_ptr->pass]);
\r
633 png_read_finish_row(png_ptr);
\r
638 if ((png_ptr->row_number & 0x07) != 4)
\r
640 if (dsp_row != NULL && (png_ptr->row_number & 4))
\r
641 png_combine_row(png_ptr, dsp_row,
\r
642 png_pass_dsp_mask[png_ptr->pass]);
\r
643 png_read_finish_row(png_ptr);
\r
648 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
\r
650 if (dsp_row != NULL)
\r
651 png_combine_row(png_ptr, dsp_row,
\r
652 png_pass_dsp_mask[png_ptr->pass]);
\r
653 png_read_finish_row(png_ptr);
\r
658 if ((png_ptr->row_number & 3) != 2)
\r
660 if (dsp_row != NULL && (png_ptr->row_number & 2))
\r
661 png_combine_row(png_ptr, dsp_row,
\r
662 png_pass_dsp_mask[png_ptr->pass]);
\r
663 png_read_finish_row(png_ptr);
\r
668 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
\r
670 if (dsp_row != NULL)
\r
671 png_combine_row(png_ptr, dsp_row,
\r
672 png_pass_dsp_mask[png_ptr->pass]);
\r
673 png_read_finish_row(png_ptr);
\r
678 if (!(png_ptr->row_number & 1))
\r
680 png_read_finish_row(png_ptr);
\r
688 if (!(png_ptr->mode & PNG_HAVE_IDAT))
\r
689 png_error(png_ptr, "Invalid attempt to read row data");
\r
691 png_ptr->zstream.next_out = png_ptr->row_buf;
\r
692 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
\r
695 if (!(png_ptr->zstream.avail_in))
\r
697 while (!png_ptr->idat_size)
\r
699 png_crc_finish(png_ptr, 0);
\r
701 png_ptr->idat_size = png_read_chunk_header(png_ptr);
\r
702 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
\r
703 png_error(png_ptr, "Not enough image data");
\r
705 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
\r
706 png_ptr->zstream.next_in = png_ptr->zbuf;
\r
707 if (png_ptr->zbuf_size > png_ptr->idat_size)
\r
708 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
\r
709 png_crc_read(png_ptr, png_ptr->zbuf,
\r
710 (png_size_t)png_ptr->zstream.avail_in);
\r
711 png_ptr->idat_size -= png_ptr->zstream.avail_in;
\r
713 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
\r
714 if (ret == Z_STREAM_END)
\r
716 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
\r
717 png_ptr->idat_size)
\r
718 png_error(png_ptr, "Extra compressed data");
\r
719 png_ptr->mode |= PNG_AFTER_IDAT;
\r
720 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
\r
724 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
\r
725 "Decompression error");
\r
727 } while (png_ptr->zstream.avail_out);
\r
729 png_ptr->row_info.color_type = png_ptr->color_type;
\r
730 png_ptr->row_info.width = png_ptr->iwidth;
\r
731 png_ptr->row_info.channels = png_ptr->channels;
\r
732 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
\r
733 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
\r
734 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
\r
735 png_ptr->row_info.width);
\r
737 if (png_ptr->row_buf[0])
\r
738 png_read_filter_row(png_ptr, &(png_ptr->row_info),
\r
739 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
\r
740 (int)(png_ptr->row_buf[0]));
\r
742 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
\r
743 png_ptr->rowbytes + 1);
\r
745 #if defined(PNG_MNG_FEATURES_SUPPORTED)
\r
746 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
\r
747 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
\r
749 /* Intrapixel differencing */
\r
750 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
\r
755 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
\r
756 png_do_read_transformations(png_ptr);
\r
758 #if defined(PNG_READ_INTERLACING_SUPPORTED)
\r
759 /* blow up interlaced rows to full size */
\r
760 if (png_ptr->interlaced &&
\r
761 (png_ptr->transformations & PNG_INTERLACE))
\r
763 if (png_ptr->pass < 6)
\r
764 /* old interface (pre-1.0.9):
\r
765 png_do_read_interlace(&(png_ptr->row_info),
\r
766 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
\r
768 png_do_read_interlace(png_ptr);
\r
770 if (dsp_row != NULL)
\r
771 png_combine_row(png_ptr, dsp_row,
\r
772 png_pass_dsp_mask[png_ptr->pass]);
\r
774 png_combine_row(png_ptr, row,
\r
775 png_pass_mask[png_ptr->pass]);
\r
781 png_combine_row(png_ptr, row, 0xff);
\r
782 if (dsp_row != NULL)
\r
783 png_combine_row(png_ptr, dsp_row, 0xff);
\r
785 png_read_finish_row(png_ptr);
\r
787 if (png_ptr->read_row_fn != NULL)
\r
788 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
\r
790 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
792 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
793 /* Read one or more rows of image data. If the image is interlaced,
\r
794 * and png_set_interlace_handling() has been called, the rows need to
\r
795 * contain the contents of the rows from the previous pass. If the
\r
796 * image has alpha or transparency, and png_handle_alpha()[*] has been
\r
797 * called, the rows contents must be initialized to the contents of the
\r
800 * "row" holds the actual image, and pixels are placed in it
\r
801 * as they arrive. If the image is displayed after each pass, it will
\r
802 * appear to "sparkle" in. "display_row" can be used to display a
\r
803 * "chunky" progressive image, with finer detail added as it becomes
\r
804 * available. If you do not want this "chunky" display, you may pass
\r
805 * NULL for display_row. If you do not want the sparkle display, and
\r
806 * you have not called png_handle_alpha(), you may pass NULL for rows.
\r
807 * If you have called png_handle_alpha(), and the image has either an
\r
808 * alpha channel or a transparency chunk, you must provide a buffer for
\r
809 * rows. In this case, you do not have to provide a display_row buffer
\r
810 * also, but you may. If the image is not interlaced, or if you have
\r
811 * not called png_set_interlace_handling(), the display_row buffer will
\r
812 * be ignored, so pass NULL to it.
\r
814 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
\r
818 png_read_rows(png_structp png_ptr, png_bytepp row,
\r
819 png_bytepp display_row, png_uint_32 num_rows)
\r
825 png_debug(1, "in png_read_rows\n");
\r
826 if (png_ptr == NULL) return;
\r
829 if (rp != NULL && dp != NULL)
\r
830 for (i = 0; i < num_rows; i++)
\r
832 png_bytep rptr = *rp++;
\r
833 png_bytep dptr = *dp++;
\r
835 png_read_row(png_ptr, rptr, dptr);
\r
837 else if (rp != NULL)
\r
838 for (i = 0; i < num_rows; i++)
\r
840 png_bytep rptr = *rp;
\r
841 png_read_row(png_ptr, rptr, png_bytep_NULL);
\r
844 else if (dp != NULL)
\r
845 for (i = 0; i < num_rows; i++)
\r
847 png_bytep dptr = *dp;
\r
848 png_read_row(png_ptr, png_bytep_NULL, dptr);
\r
852 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
854 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
855 /* Read the entire image. If the image has an alpha channel or a tRNS
\r
856 * chunk, and you have called png_handle_alpha()[*], you will need to
\r
857 * initialize the image to the current image that PNG will be overlaying.
\r
858 * We set the num_rows again here, in case it was incorrectly set in
\r
859 * png_read_start_row() by a call to png_read_update_info() or
\r
860 * png_start_read_image() if png_set_interlace_handling() wasn't called
\r
861 * prior to either of these functions like it should have been. You can
\r
862 * only call this function once. If you desire to have an image for
\r
863 * each pass of a interlaced image, use png_read_rows() instead.
\r
865 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
\r
868 png_read_image(png_structp png_ptr, png_bytepp image)
\r
870 png_uint_32 i, image_height;
\r
874 png_debug(1, "in png_read_image\n");
\r
875 if (png_ptr == NULL) return;
\r
877 #ifdef PNG_READ_INTERLACING_SUPPORTED
\r
878 pass = png_set_interlace_handling(png_ptr);
\r
880 if (png_ptr->interlaced)
\r
882 "Cannot read interlaced image -- interlace handler disabled.");
\r
887 image_height=png_ptr->height;
\r
888 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
\r
890 for (j = 0; j < pass; j++)
\r
893 for (i = 0; i < image_height; i++)
\r
895 png_read_row(png_ptr, *rp, png_bytep_NULL);
\r
900 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
902 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
903 /* Read the end of the PNG file. Will not read past the end of the
\r
904 * file, will verify the end is accurate, and will read any comments
\r
905 * or time information at the end of the file, if info is not NULL.
\r
908 png_read_end(png_structp png_ptr, png_infop info_ptr)
\r
910 png_debug(1, "in png_read_end\n");
\r
911 if (png_ptr == NULL) return;
\r
912 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
\r
916 #ifdef PNG_USE_LOCAL_ARRAYS
\r
917 PNG_CONST PNG_IHDR;
\r
918 PNG_CONST PNG_IDAT;
\r
919 PNG_CONST PNG_IEND;
\r
920 PNG_CONST PNG_PLTE;
\r
921 #if defined(PNG_READ_bKGD_SUPPORTED)
\r
922 PNG_CONST PNG_bKGD;
\r
924 #if defined(PNG_READ_cHRM_SUPPORTED)
\r
925 PNG_CONST PNG_cHRM;
\r
927 #if defined(PNG_READ_gAMA_SUPPORTED)
\r
928 PNG_CONST PNG_gAMA;
\r
930 #if defined(PNG_READ_hIST_SUPPORTED)
\r
931 PNG_CONST PNG_hIST;
\r
933 #if defined(PNG_READ_iCCP_SUPPORTED)
\r
934 PNG_CONST PNG_iCCP;
\r
936 #if defined(PNG_READ_iTXt_SUPPORTED)
\r
937 PNG_CONST PNG_iTXt;
\r
939 #if defined(PNG_READ_oFFs_SUPPORTED)
\r
940 PNG_CONST PNG_oFFs;
\r
942 #if defined(PNG_READ_pCAL_SUPPORTED)
\r
943 PNG_CONST PNG_pCAL;
\r
945 #if defined(PNG_READ_pHYs_SUPPORTED)
\r
946 PNG_CONST PNG_pHYs;
\r
948 #if defined(PNG_READ_sBIT_SUPPORTED)
\r
949 PNG_CONST PNG_sBIT;
\r
951 #if defined(PNG_READ_sCAL_SUPPORTED)
\r
952 PNG_CONST PNG_sCAL;
\r
954 #if defined(PNG_READ_sPLT_SUPPORTED)
\r
955 PNG_CONST PNG_sPLT;
\r
957 #if defined(PNG_READ_sRGB_SUPPORTED)
\r
958 PNG_CONST PNG_sRGB;
\r
960 #if defined(PNG_READ_tEXt_SUPPORTED)
\r
961 PNG_CONST PNG_tEXt;
\r
963 #if defined(PNG_READ_tIME_SUPPORTED)
\r
964 PNG_CONST PNG_tIME;
\r
966 #if defined(PNG_READ_tRNS_SUPPORTED)
\r
967 PNG_CONST PNG_tRNS;
\r
969 #if defined(PNG_READ_zTXt_SUPPORTED)
\r
970 PNG_CONST PNG_zTXt;
\r
972 #endif /* PNG_USE_LOCAL_ARRAYS */
\r
973 png_uint_32 length = png_read_chunk_header(png_ptr);
\r
974 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
\r
976 if (!png_memcmp(chunk_name, png_IHDR, 4))
\r
977 png_handle_IHDR(png_ptr, info_ptr, length);
\r
978 else if (!png_memcmp(chunk_name, png_IEND, 4))
\r
979 png_handle_IEND(png_ptr, info_ptr, length);
\r
980 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
\r
981 else if (png_handle_as_unknown(png_ptr, chunk_name))
\r
983 if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
985 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
\r
986 png_error(png_ptr, "Too many IDAT's found");
\r
988 png_handle_unknown(png_ptr, info_ptr, length);
\r
989 if (!png_memcmp(chunk_name, png_PLTE, 4))
\r
990 png_ptr->mode |= PNG_HAVE_PLTE;
\r
993 else if (!png_memcmp(chunk_name, png_IDAT, 4))
\r
995 /* Zero length IDATs are legal after the last IDAT has been
\r
996 * read, but not after other chunks have been read.
\r
998 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
\r
999 png_error(png_ptr, "Too many IDAT's found");
\r
1000 png_crc_finish(png_ptr, length);
\r
1002 else if (!png_memcmp(chunk_name, png_PLTE, 4))
\r
1003 png_handle_PLTE(png_ptr, info_ptr, length);
\r
1004 #if defined(PNG_READ_bKGD_SUPPORTED)
\r
1005 else if (!png_memcmp(chunk_name, png_bKGD, 4))
\r
1006 png_handle_bKGD(png_ptr, info_ptr, length);
\r
1008 #if defined(PNG_READ_cHRM_SUPPORTED)
\r
1009 else if (!png_memcmp(chunk_name, png_cHRM, 4))
\r
1010 png_handle_cHRM(png_ptr, info_ptr, length);
\r
1012 #if defined(PNG_READ_gAMA_SUPPORTED)
\r
1013 else if (!png_memcmp(chunk_name, png_gAMA, 4))
\r
1014 png_handle_gAMA(png_ptr, info_ptr, length);
\r
1016 #if defined(PNG_READ_hIST_SUPPORTED)
\r
1017 else if (!png_memcmp(chunk_name, png_hIST, 4))
\r
1018 png_handle_hIST(png_ptr, info_ptr, length);
\r
1020 #if defined(PNG_READ_oFFs_SUPPORTED)
\r
1021 else if (!png_memcmp(chunk_name, png_oFFs, 4))
\r
1022 png_handle_oFFs(png_ptr, info_ptr, length);
\r
1024 #if defined(PNG_READ_pCAL_SUPPORTED)
\r
1025 else if (!png_memcmp(chunk_name, png_pCAL, 4))
\r
1026 png_handle_pCAL(png_ptr, info_ptr, length);
\r
1028 #if defined(PNG_READ_sCAL_SUPPORTED)
\r
1029 else if (!png_memcmp(chunk_name, png_sCAL, 4))
\r
1030 png_handle_sCAL(png_ptr, info_ptr, length);
\r
1032 #if defined(PNG_READ_pHYs_SUPPORTED)
\r
1033 else if (!png_memcmp(chunk_name, png_pHYs, 4))
\r
1034 png_handle_pHYs(png_ptr, info_ptr, length);
\r
1036 #if defined(PNG_READ_sBIT_SUPPORTED)
\r
1037 else if (!png_memcmp(chunk_name, png_sBIT, 4))
\r
1038 png_handle_sBIT(png_ptr, info_ptr, length);
\r
1040 #if defined(PNG_READ_sRGB_SUPPORTED)
\r
1041 else if (!png_memcmp(chunk_name, png_sRGB, 4))
\r
1042 png_handle_sRGB(png_ptr, info_ptr, length);
\r
1044 #if defined(PNG_READ_iCCP_SUPPORTED)
\r
1045 else if (!png_memcmp(chunk_name, png_iCCP, 4))
\r
1046 png_handle_iCCP(png_ptr, info_ptr, length);
\r
1048 #if defined(PNG_READ_sPLT_SUPPORTED)
\r
1049 else if (!png_memcmp(chunk_name, png_sPLT, 4))
\r
1050 png_handle_sPLT(png_ptr, info_ptr, length);
\r
1052 #if defined(PNG_READ_tEXt_SUPPORTED)
\r
1053 else if (!png_memcmp(chunk_name, png_tEXt, 4))
\r
1054 png_handle_tEXt(png_ptr, info_ptr, length);
\r
1056 #if defined(PNG_READ_tIME_SUPPORTED)
\r
1057 else if (!png_memcmp(chunk_name, png_tIME, 4))
\r
1058 png_handle_tIME(png_ptr, info_ptr, length);
\r
1060 #if defined(PNG_READ_tRNS_SUPPORTED)
\r
1061 else if (!png_memcmp(chunk_name, png_tRNS, 4))
\r
1062 png_handle_tRNS(png_ptr, info_ptr, length);
\r
1064 #if defined(PNG_READ_zTXt_SUPPORTED)
\r
1065 else if (!png_memcmp(chunk_name, png_zTXt, 4))
\r
1066 png_handle_zTXt(png_ptr, info_ptr, length);
\r
1068 #if defined(PNG_READ_iTXt_SUPPORTED)
\r
1069 else if (!png_memcmp(chunk_name, png_iTXt, 4))
\r
1070 png_handle_iTXt(png_ptr, info_ptr, length);
\r
1073 png_handle_unknown(png_ptr, info_ptr, length);
\r
1074 } while (!(png_ptr->mode & PNG_HAVE_IEND));
\r
1076 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
1078 /* free all memory used by the read */
\r
1080 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
\r
1081 png_infopp end_info_ptr_ptr)
\r
1083 png_structp png_ptr = NULL;
\r
1084 png_infop info_ptr = NULL, end_info_ptr = NULL;
\r
1085 #ifdef PNG_USER_MEM_SUPPORTED
\r
1086 png_free_ptr free_fn = NULL;
\r
1087 png_voidp mem_ptr = NULL;
\r
1090 png_debug(1, "in png_destroy_read_struct\n");
\r
1091 if (png_ptr_ptr != NULL)
\r
1092 png_ptr = *png_ptr_ptr;
\r
1093 if (png_ptr == NULL)
\r
1096 #ifdef PNG_USER_MEM_SUPPORTED
\r
1097 free_fn = png_ptr->free_fn;
\r
1098 mem_ptr = png_ptr->mem_ptr;
\r
1101 if (info_ptr_ptr != NULL)
\r
1102 info_ptr = *info_ptr_ptr;
\r
1104 if (end_info_ptr_ptr != NULL)
\r
1105 end_info_ptr = *end_info_ptr_ptr;
\r
1107 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
\r
1109 if (info_ptr != NULL)
\r
1111 #if defined(PNG_TEXT_SUPPORTED)
\r
1112 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
\r
1115 #ifdef PNG_USER_MEM_SUPPORTED
\r
1116 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
\r
1117 (png_voidp)mem_ptr);
\r
1119 png_destroy_struct((png_voidp)info_ptr);
\r
1121 *info_ptr_ptr = NULL;
\r
1124 if (end_info_ptr != NULL)
\r
1126 #if defined(PNG_READ_TEXT_SUPPORTED)
\r
1127 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
\r
1129 #ifdef PNG_USER_MEM_SUPPORTED
\r
1130 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
\r
1131 (png_voidp)mem_ptr);
\r
1133 png_destroy_struct((png_voidp)end_info_ptr);
\r
1135 *end_info_ptr_ptr = NULL;
\r
1138 if (png_ptr != NULL)
\r
1140 #ifdef PNG_USER_MEM_SUPPORTED
\r
1141 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
\r
1142 (png_voidp)mem_ptr);
\r
1144 png_destroy_struct((png_voidp)png_ptr);
\r
1146 *png_ptr_ptr = NULL;
\r
1150 /* free all memory used by the read (old method) */
\r
1151 void /* PRIVATE */
\r
1152 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
\r
1154 #ifdef PNG_SETJMP_SUPPORTED
\r
1157 png_error_ptr error_fn;
\r
1158 png_error_ptr warning_fn;
\r
1159 png_voidp error_ptr;
\r
1160 #ifdef PNG_USER_MEM_SUPPORTED
\r
1161 png_free_ptr free_fn;
\r
1164 png_debug(1, "in png_read_destroy\n");
\r
1165 if (info_ptr != NULL)
\r
1166 png_info_destroy(png_ptr, info_ptr);
\r
1168 if (end_info_ptr != NULL)
\r
1169 png_info_destroy(png_ptr, end_info_ptr);
\r
1171 png_free(png_ptr, png_ptr->zbuf);
\r
1172 png_free(png_ptr, png_ptr->big_row_buf);
\r
1173 png_free(png_ptr, png_ptr->prev_row);
\r
1174 png_free(png_ptr, png_ptr->chunkdata);
\r
1175 #if defined(PNG_READ_DITHER_SUPPORTED)
\r
1176 png_free(png_ptr, png_ptr->palette_lookup);
\r
1177 png_free(png_ptr, png_ptr->dither_index);
\r
1179 #if defined(PNG_READ_GAMMA_SUPPORTED)
\r
1180 png_free(png_ptr, png_ptr->gamma_table);
\r
1182 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
\r
1183 png_free(png_ptr, png_ptr->gamma_from_1);
\r
1184 png_free(png_ptr, png_ptr->gamma_to_1);
\r
1186 #ifdef PNG_FREE_ME_SUPPORTED
\r
1187 if (png_ptr->free_me & PNG_FREE_PLTE)
\r
1188 png_zfree(png_ptr, png_ptr->palette);
\r
1189 png_ptr->free_me &= ~PNG_FREE_PLTE;
\r
1191 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
\r
1192 png_zfree(png_ptr, png_ptr->palette);
\r
1193 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
\r
1195 #if defined(PNG_tRNS_SUPPORTED) || \
\r
1196 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
\r
1197 #ifdef PNG_FREE_ME_SUPPORTED
\r
1198 if (png_ptr->free_me & PNG_FREE_TRNS)
\r
1199 png_free(png_ptr, png_ptr->trans);
\r
1200 png_ptr->free_me &= ~PNG_FREE_TRNS;
\r
1202 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
\r
1203 png_free(png_ptr, png_ptr->trans);
\r
1204 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
\r
1207 #if defined(PNG_READ_hIST_SUPPORTED)
\r
1208 #ifdef PNG_FREE_ME_SUPPORTED
\r
1209 if (png_ptr->free_me & PNG_FREE_HIST)
\r
1210 png_free(png_ptr, png_ptr->hist);
\r
1211 png_ptr->free_me &= ~PNG_FREE_HIST;
\r
1213 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
\r
1214 png_free(png_ptr, png_ptr->hist);
\r
1215 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
\r
1218 #if defined(PNG_READ_GAMMA_SUPPORTED)
\r
1219 if (png_ptr->gamma_16_table != NULL)
\r
1222 int istop = (1 << (8 - png_ptr->gamma_shift));
\r
1223 for (i = 0; i < istop; i++)
\r
1225 png_free(png_ptr, png_ptr->gamma_16_table[i]);
\r
1227 png_free(png_ptr, png_ptr->gamma_16_table);
\r
1229 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
\r
1230 if (png_ptr->gamma_16_from_1 != NULL)
\r
1233 int istop = (1 << (8 - png_ptr->gamma_shift));
\r
1234 for (i = 0; i < istop; i++)
\r
1236 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
\r
1238 png_free(png_ptr, png_ptr->gamma_16_from_1);
\r
1240 if (png_ptr->gamma_16_to_1 != NULL)
\r
1243 int istop = (1 << (8 - png_ptr->gamma_shift));
\r
1244 for (i = 0; i < istop; i++)
\r
1246 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
\r
1248 png_free(png_ptr, png_ptr->gamma_16_to_1);
\r
1252 #if defined(PNG_TIME_RFC1123_SUPPORTED)
\r
1253 png_free(png_ptr, png_ptr->time_buffer);
\r
1256 inflateEnd(&png_ptr->zstream);
\r
1257 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
\r
1258 png_free(png_ptr, png_ptr->save_buffer);
\r
1261 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
\r
1262 #ifdef PNG_TEXT_SUPPORTED
\r
1263 png_free(png_ptr, png_ptr->current_text);
\r
1264 #endif /* PNG_TEXT_SUPPORTED */
\r
1265 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
\r
1267 /* Save the important info out of the png_struct, in case it is
\r
1268 * being used again.
\r
1270 #ifdef PNG_SETJMP_SUPPORTED
\r
1271 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
\r
1274 error_fn = png_ptr->error_fn;
\r
1275 warning_fn = png_ptr->warning_fn;
\r
1276 error_ptr = png_ptr->error_ptr;
\r
1277 #ifdef PNG_USER_MEM_SUPPORTED
\r
1278 free_fn = png_ptr->free_fn;
\r
1281 png_memset(png_ptr, 0, png_sizeof(png_struct));
\r
1283 png_ptr->error_fn = error_fn;
\r
1284 png_ptr->warning_fn = warning_fn;
\r
1285 png_ptr->error_ptr = error_ptr;
\r
1286 #ifdef PNG_USER_MEM_SUPPORTED
\r
1287 png_ptr->free_fn = free_fn;
\r
1290 #ifdef PNG_SETJMP_SUPPORTED
\r
1291 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
\r
1297 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
\r
1299 if (png_ptr == NULL) return;
\r
1300 png_ptr->read_row_fn = read_row_fn;
\r
1304 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
\r
1305 #if defined(PNG_INFO_IMAGE_SUPPORTED)
\r
1307 png_read_png(png_structp png_ptr, png_infop info_ptr,
\r
1313 if (png_ptr == NULL) return;
\r
1314 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
\r
1315 /* invert the alpha channel from opacity to transparency
\r
1317 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
\r
1318 png_set_invert_alpha(png_ptr);
\r
1321 /* png_read_info() gives us all of the information from the
\r
1322 * PNG file before the first IDAT (image data chunk).
\r
1324 png_read_info(png_ptr, info_ptr);
\r
1325 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
\r
1326 png_error(png_ptr, "Image is too high to process with png_read_png()");
\r
1328 /* -------------- image transformations start here ------------------- */
\r
1330 #if defined(PNG_READ_16_TO_8_SUPPORTED)
\r
1331 /* tell libpng to strip 16 bit/color files down to 8 bits per color
\r
1333 if (transforms & PNG_TRANSFORM_STRIP_16)
\r
1334 png_set_strip_16(png_ptr);
\r
1337 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
\r
1338 /* Strip alpha bytes from the input data without combining with
\r
1339 * the background (not recommended).
\r
1341 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
\r
1342 png_set_strip_alpha(png_ptr);
\r
1345 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
\r
1346 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
\r
1347 * byte into separate bytes (useful for paletted and grayscale images).
\r
1349 if (transforms & PNG_TRANSFORM_PACKING)
\r
1350 png_set_packing(png_ptr);
\r
1353 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
\r
1354 /* Change the order of packed pixels to least significant bit first
\r
1355 * (not useful if you are using png_set_packing).
\r
1357 if (transforms & PNG_TRANSFORM_PACKSWAP)
\r
1358 png_set_packswap(png_ptr);
\r
1361 #if defined(PNG_READ_EXPAND_SUPPORTED)
\r
1362 /* Expand paletted colors into true RGB triplets
\r
1363 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
\r
1364 * Expand paletted or RGB images with transparency to full alpha
\r
1365 * channels so the data will be available as RGBA quartets.
\r
1367 if (transforms & PNG_TRANSFORM_EXPAND)
\r
1368 if ((png_ptr->bit_depth < 8) ||
\r
1369 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
\r
1370 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
\r
1371 png_set_expand(png_ptr);
\r
1374 /* We don't handle background color or gamma transformation or dithering.
\r
1377 #if defined(PNG_READ_INVERT_SUPPORTED)
\r
1378 /* invert monochrome files to have 0 as white and 1 as black
\r
1380 if (transforms & PNG_TRANSFORM_INVERT_MONO)
\r
1381 png_set_invert_mono(png_ptr);
\r
1384 #if defined(PNG_READ_SHIFT_SUPPORTED)
\r
1385 /* If you want to shift the pixel values from the range [0,255] or
\r
1386 * [0,65535] to the original [0,7] or [0,31], or whatever range the
\r
1387 * colors were originally in:
\r
1389 if ((transforms & PNG_TRANSFORM_SHIFT)
\r
1390 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
\r
1392 png_color_8p sig_bit;
\r
1394 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
\r
1395 png_set_shift(png_ptr, sig_bit);
\r
1399 #if defined(PNG_READ_BGR_SUPPORTED)
\r
1400 /* flip the RGB pixels to BGR (or RGBA to BGRA)
\r
1402 if (transforms & PNG_TRANSFORM_BGR)
\r
1403 png_set_bgr(png_ptr);
\r
1406 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
\r
1407 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
\r
1409 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
\r
1410 png_set_swap_alpha(png_ptr);
\r
1413 #if defined(PNG_READ_SWAP_SUPPORTED)
\r
1414 /* swap bytes of 16 bit files to least significant byte first
\r
1416 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
\r
1417 png_set_swap(png_ptr);
\r
1420 /* We don't handle adding filler bytes */
\r
1422 /* Optional call to gamma correct and add the background to the palette
\r
1423 * and update info structure. REQUIRED if you are expecting libpng to
\r
1424 * update the palette for you (i.e., you selected such a transform above).
\r
1426 png_read_update_info(png_ptr, info_ptr);
\r
1428 /* -------------- image transformations end here ------------------- */
\r
1430 #ifdef PNG_FREE_ME_SUPPORTED
\r
1431 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
\r
1433 if (info_ptr->row_pointers == NULL)
\r
1435 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
\r
1436 info_ptr->height * png_sizeof(png_bytep));
\r
1437 #ifdef PNG_FREE_ME_SUPPORTED
\r
1438 info_ptr->free_me |= PNG_FREE_ROWS;
\r
1440 for (row = 0; row < (int)info_ptr->height; row++)
\r
1442 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
\r
1443 png_get_rowbytes(png_ptr, info_ptr));
\r
1447 png_read_image(png_ptr, info_ptr->row_pointers);
\r
1448 info_ptr->valid |= PNG_INFO_IDAT;
\r
1450 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
\r
1451 png_read_end(png_ptr, info_ptr);
\r
1453 transforms = transforms; /* quiet compiler warnings */
\r
1457 #endif /* PNG_INFO_IMAGE_SUPPORTED */
\r
1458 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
\r
1459 #endif /* PNG_READ_SUPPORTED */
\r