2 /* pngread.c - read a PNG file
4 * libpng 1.0.3 - January 14, 1999
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
17 /* Create a PNG structure for reading, and allocate any memory needed. */
19 png_create_read_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
20 png_error_ptr error_fn
, png_error_ptr warn_fn
)
23 #ifdef PNG_USER_MEM_SUPPORTED
24 return (png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
25 warn_fn
, NULL
, NULL
, NULL
));
28 /* Alternate create PNG structure for reading, and allocate any memory needed. */
30 png_create_read_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
31 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
32 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
34 #endif /* PNG_USER_MEM_SUPPORTED */
37 #ifdef USE_FAR_KEYWORD
40 png_debug(1, "in png_create_read_struct\n");
41 #ifdef PNG_USER_MEM_SUPPORTED
42 if ((png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
43 (png_malloc_ptr
)malloc_fn
)) == NULL
)
45 if ((png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
)) == NULL
)
48 return (png_structp
)NULL
;
50 #ifdef USE_FAR_KEYWORD
53 if (setjmp(png_ptr
->jmpbuf
))
56 png_free(png_ptr
, png_ptr
->zbuf
);
57 png_destroy_struct(png_ptr
);
58 return (png_structp
)NULL
;
60 #ifdef USE_FAR_KEYWORD
61 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
64 #ifdef PNG_USER_MEM_SUPPORTED
65 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
66 #endif /* PNG_USER_MEM_SUPPORTED */
68 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
70 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
71 * we must recompile any applications that use any older library version.
72 * For versions after libpng 1.0, we will be compatible, so we need
73 * only check the first digit.
75 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
76 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
79 "Incompatible libpng version in application and library");
82 /* initialize zbuf - compression buffer */
83 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
84 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
85 (png_uint_32
)png_ptr
->zbuf_size
);
86 png_ptr
->zstream
.zalloc
= png_zalloc
;
87 png_ptr
->zstream
.zfree
= png_zfree
;
88 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
90 switch (inflateInit(&png_ptr
->zstream
))
92 case Z_OK
: /* Do nothing */ break;
94 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory error"); break;
95 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version error"); break;
96 default: png_error(png_ptr
, "Unknown zlib error");
99 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
100 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
102 png_set_read_fn(png_ptr
, NULL
, NULL
);
107 /* Initialize PNG structure for reading, and allocate any memory needed.
108 This interface is deprecated in favour of the png_create_read_struct(),
109 and it will eventually disappear. */
111 png_read_init(png_structp png_ptr
)
113 jmp_buf tmp_jmp
; /* to save current jump buffer */
115 png_debug(1, "in png_read_init\n");
116 /* save jump buffer and error functions */
117 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
119 /* reset all variables to 0 */
120 png_memset(png_ptr
, 0, sizeof (png_struct
));
122 /* restore jump buffer */
123 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
125 /* initialize zbuf - compression buffer */
126 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
127 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
128 (png_uint_32
)png_ptr
->zbuf_size
);
129 png_ptr
->zstream
.zalloc
= png_zalloc
;
130 png_ptr
->zstream
.zfree
= png_zfree
;
131 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
133 switch (inflateInit(&png_ptr
->zstream
))
135 case Z_OK
: /* Do nothing */ break;
137 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory"); break;
138 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version"); break;
139 default: png_error(png_ptr
, "Unknown zlib error");
142 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
143 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
145 png_set_read_fn(png_ptr
, NULL
, NULL
);
148 /* Read the information before the actual image data. This has been
149 * changed in v0.90 to allow reading a file that already has the magic
150 * bytes read from the stream. You can tell libpng how many bytes have
151 * been read from the beginning of the stream (up to the maximum of 8)
152 * via png_set_sig_bytes(), and we will only check the remaining bytes
153 * here. The application can then have access to the signature bytes we
154 * read if it is determined that this isn't a valid PNG file.
157 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
159 png_debug(1, "in png_read_info\n");
160 /* save jump buffer and error functions */
161 /* If we haven't checked all of the PNG signature bytes, do so now. */
162 if (png_ptr
->sig_bytes
< 8)
164 png_size_t num_checked
= png_ptr
->sig_bytes
,
165 num_to_check
= 8 - num_checked
;
167 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
168 png_ptr
->sig_bytes
= 8;
170 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
172 if (num_checked
< 4 &&
173 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
174 png_error(png_ptr
, "Not a PNG file");
176 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
182 png_byte chunk_length
[4];
185 png_read_data(png_ptr
, chunk_length
, 4);
186 length
= png_get_uint_32(chunk_length
);
188 png_reset_crc(png_ptr
);
189 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
191 png_debug1(0, "Reading %s chunk.\n", png_ptr
->chunk_name
);
193 /* This should be a binary subdivision search or a hash for
194 * matching the chunk name rather than a linear search.
196 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
197 png_handle_IHDR(png_ptr
, info_ptr
, length
);
198 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
199 png_handle_PLTE(png_ptr
, info_ptr
, length
);
200 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
201 png_handle_IEND(png_ptr
, info_ptr
, length
);
202 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
204 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
205 png_error(png_ptr
, "Missing IHDR before IDAT");
206 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
207 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
208 png_error(png_ptr
, "Missing PLTE before IDAT");
210 png_ptr
->idat_size
= length
;
211 png_ptr
->mode
|= PNG_HAVE_IDAT
;
214 #if defined(PNG_READ_bKGD_SUPPORTED)
215 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
216 png_handle_bKGD(png_ptr
, info_ptr
, length
);
218 #if defined(PNG_READ_cHRM_SUPPORTED)
219 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
220 png_handle_cHRM(png_ptr
, info_ptr
, length
);
222 #if defined(PNG_READ_gAMA_SUPPORTED)
223 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
224 png_handle_gAMA(png_ptr
, info_ptr
, length
);
226 #if defined(PNG_READ_hIST_SUPPORTED)
227 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
228 png_handle_hIST(png_ptr
, info_ptr
, length
);
230 #if defined(PNG_READ_oFFs_SUPPORTED)
231 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
232 png_handle_oFFs(png_ptr
, info_ptr
, length
);
234 #if defined(PNG_READ_pCAL_SUPPORTED)
235 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
236 png_handle_pCAL(png_ptr
, info_ptr
, length
);
238 #if defined(PNG_READ_pHYs_SUPPORTED)
239 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
240 png_handle_pHYs(png_ptr
, info_ptr
, length
);
242 #if defined(PNG_READ_sBIT_SUPPORTED)
243 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
244 png_handle_sBIT(png_ptr
, info_ptr
, length
);
246 #if defined(PNG_READ_sRGB_SUPPORTED)
247 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
248 png_handle_sRGB(png_ptr
, info_ptr
, length
);
250 #if defined(PNG_READ_tEXt_SUPPORTED)
251 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
252 png_handle_tEXt(png_ptr
, info_ptr
, length
);
254 #if defined(PNG_READ_tIME_SUPPORTED)
255 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
256 png_handle_tIME(png_ptr
, info_ptr
, length
);
258 #if defined(PNG_READ_tRNS_SUPPORTED)
259 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
260 png_handle_tRNS(png_ptr
, info_ptr
, length
);
262 #if defined(PNG_READ_zTXt_SUPPORTED)
263 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
264 png_handle_zTXt(png_ptr
, info_ptr
, length
);
267 png_handle_unknown(png_ptr
, info_ptr
, length
);
271 /* optional call to update the users info_ptr structure */
273 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
275 png_debug(1, "in png_read_update_info\n");
276 /* save jump buffer and error functions */
277 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
278 png_read_start_row(png_ptr
);
279 png_read_transform_info(png_ptr
, info_ptr
);
282 /* Initialize palette, background, etc, after transformations
283 * are set, but before any reading takes place. This allows
284 * the user to obtain a gamma-corrected palette, for example.
285 * If the user doesn't call this, we will do it ourselves.
288 png_start_read_image(png_structp png_ptr
)
290 png_debug(1, "in png_start_read_image\n");
291 /* save jump buffer and error functions */
292 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
293 png_read_start_row(png_ptr
);
297 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
300 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
301 png_ptr
->row_number
, png_ptr
->pass
);
302 /* save jump buffer and error functions */
303 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
304 png_read_start_row(png_ptr
);
305 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
307 /* check for transforms that have been set but were defined out */
308 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
309 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
310 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined.");
312 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
313 if (png_ptr
->transformations
& PNG_FILLER
)
314 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined.");
316 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
317 if (png_ptr
->transformations
& PNG_PACKSWAP
)
318 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
320 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
321 if (png_ptr
->transformations
& PNG_PACK
)
322 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined.");
324 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
325 if (png_ptr
->transformations
& PNG_SHIFT
)
326 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined.");
328 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
329 if (png_ptr
->transformations
& PNG_BGR
)
330 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined.");
332 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
333 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
334 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined.");
338 #if defined(PNG_READ_INTERLACING_SUPPORTED)
339 /* if interlaced and we do not need a new row, combine row and return */
340 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
342 switch (png_ptr
->pass
)
345 if (png_ptr
->row_number
& 7)
348 png_combine_row(png_ptr
, dsp_row
,
349 png_pass_dsp_mask
[png_ptr
->pass
]);
350 png_read_finish_row(png_ptr
);
355 if ((png_ptr
->row_number
& 7) || png_ptr
->width
< 5)
358 png_combine_row(png_ptr
, dsp_row
,
359 png_pass_dsp_mask
[png_ptr
->pass
]);
360 png_read_finish_row(png_ptr
);
365 if ((png_ptr
->row_number
& 7) != 4)
367 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
368 png_combine_row(png_ptr
, dsp_row
,
369 png_pass_dsp_mask
[png_ptr
->pass
]);
370 png_read_finish_row(png_ptr
);
375 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
378 png_combine_row(png_ptr
, dsp_row
,
379 png_pass_dsp_mask
[png_ptr
->pass
]);
380 png_read_finish_row(png_ptr
);
385 if ((png_ptr
->row_number
& 3) != 2)
387 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
388 png_combine_row(png_ptr
, dsp_row
,
389 png_pass_dsp_mask
[png_ptr
->pass
]);
390 png_read_finish_row(png_ptr
);
395 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
398 png_combine_row(png_ptr
, dsp_row
,
399 png_pass_dsp_mask
[png_ptr
->pass
]);
400 png_read_finish_row(png_ptr
);
405 if (!(png_ptr
->row_number
& 1))
407 png_read_finish_row(png_ptr
);
415 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
416 png_error(png_ptr
, "Invalid attempt to read row data");
418 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
419 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
422 if (!(png_ptr
->zstream
.avail_in
))
424 while (!png_ptr
->idat_size
)
426 png_byte chunk_length
[4];
428 png_crc_finish(png_ptr
, 0);
430 png_read_data(png_ptr
, chunk_length
, 4);
431 png_ptr
->idat_size
= png_get_uint_32(chunk_length
);
433 png_reset_crc(png_ptr
);
434 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
435 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
436 png_error(png_ptr
, "Not enough image data");
438 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
439 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
440 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
441 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
442 png_crc_read(png_ptr
, png_ptr
->zbuf
,
443 (png_size_t
)png_ptr
->zstream
.avail_in
);
444 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
446 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
447 if (ret
== Z_STREAM_END
)
449 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
451 png_error(png_ptr
, "Extra compressed data");
452 png_ptr
->mode
|= PNG_AFTER_IDAT
;
453 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
457 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
458 "Decompression error");
460 } while (png_ptr
->zstream
.avail_out
);
462 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
463 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
464 png_ptr
->row_info
.channels
= png_ptr
->channels
;
465 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
466 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
468 png_ptr
->row_info
.rowbytes
= ((png_ptr
->row_info
.width
*
469 (png_uint_32
)png_ptr
->row_info
.pixel_depth
+ 7) >> 3);
472 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
473 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
474 (int)(png_ptr
->row_buf
[0]));
476 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
477 png_ptr
->rowbytes
+ 1);
479 if (png_ptr
->transformations
)
480 png_do_read_transformations(png_ptr
);
482 #if defined(PNG_READ_INTERLACING_SUPPORTED)
483 /* blow up interlaced rows to full size */
484 if (png_ptr
->interlaced
&&
485 (png_ptr
->transformations
& PNG_INTERLACE
))
487 if (png_ptr
->pass
< 6)
488 png_do_read_interlace(&(png_ptr
->row_info
),
489 png_ptr
->row_buf
+ 1, png_ptr
->pass
, png_ptr
->transformations
);
492 png_combine_row(png_ptr
, dsp_row
,
493 png_pass_dsp_mask
[png_ptr
->pass
]);
495 png_combine_row(png_ptr
, row
,
496 png_pass_mask
[png_ptr
->pass
]);
502 png_combine_row(png_ptr
, row
, 0xff);
504 png_combine_row(png_ptr
, dsp_row
, 0xff);
506 png_read_finish_row(png_ptr
);
508 if (png_ptr
->read_row_fn
!= NULL
)
509 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
512 /* Read one or more rows of image data. If the image is interlaced,
513 * and png_set_interlace_handling() has been called, the rows need to
514 * contain the contents of the rows from the previous pass. If the
515 * image has alpha or transparency, and png_handle_alpha()[*] has been
516 * called, the rows contents must be initialized to the contents of the
519 * "row" holds the actual image, and pixels are placed in it
520 * as they arrive. If the image is displayed after each pass, it will
521 * appear to "sparkle" in. "display_row" can be used to display a
522 * "chunky" progressive image, with finer detail added as it becomes
523 * available. If you do not want this "chunky" display, you may pass
524 * NULL for display_row. If you do not want the sparkle display, and
525 * you have not called png_handle_alpha(), you may pass NULL for rows.
526 * If you have called png_handle_alpha(), and the image has either an
527 * alpha channel or a transparency chunk, you must provide a buffer for
528 * rows. In this case, you do not have to provide a display_row buffer
529 * also, but you may. If the image is not interlaced, or if you have
530 * not called png_set_interlace_handling(), the display_row buffer will
531 * be ignored, so pass NULL to it.
533 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.3.
537 png_read_rows(png_structp png_ptr
, png_bytepp row
,
538 png_bytepp display_row
, png_uint_32 num_rows
)
544 png_debug(1, "in png_read_rows\n");
545 /* save jump buffer and error functions */
548 if (rp
!= NULL
&& dp
!= NULL
)
549 for (i
= 0; i
< num_rows
; i
++)
551 png_bytep rptr
= *rp
++;
552 png_bytep dptr
= *dp
++;
554 png_read_row(png_ptr
, rptr
, dptr
);
557 for (i
= 0; i
< num_rows
; i
++)
559 png_bytep rptr
= *rp
;
560 png_read_row(png_ptr
, rptr
, NULL
);
564 for (i
= 0; i
< num_rows
; i
++)
566 png_bytep dptr
= *dp
;
567 png_read_row(png_ptr
, NULL
, dptr
);
572 /* Read the entire image. If the image has an alpha channel or a tRNS
573 * chunk, and you have called png_handle_alpha()[*], you will need to
574 * initialize the image to the current image that PNG will be overlaying.
575 * We set the num_rows again here, in case it was incorrectly set in
576 * png_read_start_row() by a call to png_read_update_info() or
577 * png_start_read_image() if png_set_interlace_handling() wasn't called
578 * prior to either of these functions like it should have been. You can
579 * only call this function once. If you desire to have an image for
580 * each pass of a interlaced image, use png_read_rows() instead.
582 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.3.
585 png_read_image(png_structp png_ptr
, png_bytepp image
)
587 png_uint_32 i
,image_height
;
591 png_debug(1, "in png_read_image\n");
592 /* save jump buffer and error functions */
593 pass
= png_set_interlace_handling(png_ptr
);
595 image_height
=png_ptr
->height
;
596 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
598 for (j
= 0; j
< pass
; j
++)
601 for (i
= 0; i
< image_height
; i
++)
603 png_read_row(png_ptr
, *rp
, NULL
);
609 /* Read the end of the PNG file. Will not read past the end of the
610 * file, will verify the end is accurate, and will read any comments
611 * or time information at the end of the file, if info is not NULL.
614 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
616 png_byte chunk_length
[4];
619 png_debug(1, "in png_read_end\n");
620 /* save jump buffer and error functions */
621 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
625 png_read_data(png_ptr
, chunk_length
, 4);
626 length
= png_get_uint_32(chunk_length
);
628 png_reset_crc(png_ptr
);
629 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
631 png_debug1(0, "Reading %s chunk.\n", png_ptr
->chunk_name
);
633 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
634 png_handle_IHDR(png_ptr
, info_ptr
, length
);
635 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
637 /* Zero length IDATs are legal after the last IDAT has been
638 * read, but not after other chunks have been read.
640 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
641 png_error(png_ptr
, "Too many IDAT's found");
643 png_crc_finish(png_ptr
, 0);
645 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
646 png_handle_PLTE(png_ptr
, info_ptr
, length
);
647 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
648 png_handle_IEND(png_ptr
, info_ptr
, length
);
649 #if defined(PNG_READ_bKGD_SUPPORTED)
650 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
651 png_handle_bKGD(png_ptr
, info_ptr
, length
);
653 #if defined(PNG_READ_cHRM_SUPPORTED)
654 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
655 png_handle_cHRM(png_ptr
, info_ptr
, length
);
657 #if defined(PNG_READ_gAMA_SUPPORTED)
658 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
659 png_handle_gAMA(png_ptr
, info_ptr
, length
);
661 #if defined(PNG_READ_hIST_SUPPORTED)
662 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
663 png_handle_hIST(png_ptr
, info_ptr
, length
);
665 #if defined(PNG_READ_oFFs_SUPPORTED)
666 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
667 png_handle_oFFs(png_ptr
, info_ptr
, length
);
669 #if defined(PNG_READ_pCAL_SUPPORTED)
670 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
671 png_handle_pCAL(png_ptr
, info_ptr
, length
);
673 #if defined(PNG_READ_pHYs_SUPPORTED)
674 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
675 png_handle_pHYs(png_ptr
, info_ptr
, length
);
677 #if defined(PNG_READ_sBIT_SUPPORTED)
678 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
679 png_handle_sBIT(png_ptr
, info_ptr
, length
);
681 #if defined(PNG_READ_sRGB_SUPPORTED)
682 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
683 png_handle_sRGB(png_ptr
, info_ptr
, length
);
685 #if defined(PNG_READ_tEXt_SUPPORTED)
686 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
687 png_handle_tEXt(png_ptr
, info_ptr
, length
);
689 #if defined(PNG_READ_tIME_SUPPORTED)
690 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
691 png_handle_tIME(png_ptr
, info_ptr
, length
);
693 #if defined(PNG_READ_tRNS_SUPPORTED)
694 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
695 png_handle_tRNS(png_ptr
, info_ptr
, length
);
697 #if defined(PNG_READ_zTXt_SUPPORTED)
698 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
699 png_handle_zTXt(png_ptr
, info_ptr
, length
);
702 png_handle_unknown(png_ptr
, info_ptr
, length
);
703 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
706 /* free all memory used by the read */
708 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
709 png_infopp end_info_ptr_ptr
)
711 png_structp png_ptr
= NULL
;
712 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
713 #ifdef PNG_USER_MEM_SUPPORTED
714 png_free_ptr free_fn
= NULL
;
715 #endif /* PNG_USER_MEM_SUPPORTED */
717 png_debug(1, "in png_destroy_read_struct\n");
718 /* save jump buffer and error functions */
719 if (png_ptr_ptr
!= NULL
)
720 png_ptr
= *png_ptr_ptr
;
722 if (info_ptr_ptr
!= NULL
)
723 info_ptr
= *info_ptr_ptr
;
725 if (end_info_ptr_ptr
!= NULL
)
726 end_info_ptr
= *end_info_ptr_ptr
;
728 #ifdef PNG_USER_MEM_SUPPORTED
729 free_fn
= png_ptr
->free_fn
;
732 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
734 if (info_ptr
!= NULL
)
736 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
737 png_free(png_ptr
, info_ptr
->text
);
740 #ifdef PNG_USER_MEM_SUPPORTED
741 png_destroy_struct_2((png_voidp
)info_ptr
, free_fn
);
743 png_destroy_struct((png_voidp
)info_ptr
);
745 *info_ptr_ptr
= (png_infop
)NULL
;
748 if (end_info_ptr
!= NULL
)
750 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
751 png_free(png_ptr
, end_info_ptr
->text
);
753 #ifdef PNG_USER_MEM_SUPPORTED
754 png_destroy_struct_2((png_voidp
)end_info_ptr
, free_fn
);
756 png_destroy_struct((png_voidp
)end_info_ptr
);
758 *end_info_ptr_ptr
= (png_infop
)NULL
;
763 #ifdef PNG_USER_MEM_SUPPORTED
764 png_destroy_struct_2((png_voidp
)png_ptr
, free_fn
);
766 png_destroy_struct((png_voidp
)png_ptr
);
768 *png_ptr_ptr
= (png_structp
)NULL
;
772 /* free all memory used by the read (old method) */
774 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
, png_infop end_info_ptr
)
777 png_error_ptr error_fn
;
778 png_error_ptr warning_fn
;
780 #ifdef PNG_USER_MEM_SUPPORTED
781 png_free_ptr free_fn
;
784 png_debug(1, "in png_read_destroy\n");
785 /* save jump buffer and error functions */
786 if (info_ptr
!= NULL
)
787 png_info_destroy(png_ptr
, info_ptr
);
789 if (end_info_ptr
!= NULL
)
790 png_info_destroy(png_ptr
, end_info_ptr
);
792 png_free(png_ptr
, png_ptr
->zbuf
);
793 png_free(png_ptr
, png_ptr
->row_buf
);
794 png_free(png_ptr
, png_ptr
->prev_row
);
795 #if defined(PNG_READ_DITHER_SUPPORTED)
796 png_free(png_ptr
, png_ptr
->palette_lookup
);
797 png_free(png_ptr
, png_ptr
->dither_index
);
799 #if defined(PNG_READ_GAMMA_SUPPORTED)
800 png_free(png_ptr
, png_ptr
->gamma_table
);
802 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
803 png_free(png_ptr
, png_ptr
->gamma_from_1
);
804 png_free(png_ptr
, png_ptr
->gamma_to_1
);
806 if (png_ptr
->flags
& PNG_FLAG_FREE_PALETTE
)
807 png_zfree(png_ptr
, png_ptr
->palette
);
808 if (png_ptr
->flags
& PNG_FLAG_FREE_TRANS
)
809 png_free(png_ptr
, png_ptr
->trans
);
810 #if defined(PNG_READ_hIST_SUPPORTED)
811 if (png_ptr
->flags
& PNG_FLAG_FREE_HIST
)
812 png_free(png_ptr
, png_ptr
->hist
);
814 #if defined(PNG_READ_GAMMA_SUPPORTED)
815 if (png_ptr
->gamma_16_table
!= NULL
)
818 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
819 for (i
= 0; i
< istop
; i
++)
821 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
823 png_free(png_ptr
, png_ptr
->gamma_16_table
);
825 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
826 if (png_ptr
->gamma_16_from_1
!= NULL
)
829 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
830 for (i
= 0; i
< istop
; i
++)
832 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
834 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
836 if (png_ptr
->gamma_16_to_1
!= NULL
)
839 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
840 for (i
= 0; i
< istop
; i
++)
842 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
844 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
848 #if defined(PNG_TIME_RFC1123_SUPPORTED)
849 png_free(png_ptr
, png_ptr
->time_buffer
);
850 #endif /* PNG_TIME_RFC1123_SUPPORTED */
852 inflateEnd(&png_ptr
->zstream
);
853 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
854 png_free(png_ptr
, png_ptr
->save_buffer
);
857 /* Save the important info out of the png_struct, in case it is
860 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
862 error_fn
= png_ptr
->error_fn
;
863 warning_fn
= png_ptr
->warning_fn
;
864 error_ptr
= png_ptr
->error_ptr
;
865 #ifdef PNG_USER_MEM_SUPPORTED
866 free_fn
= png_ptr
->free_fn
;
869 png_memset(png_ptr
, 0, sizeof (png_struct
));
871 png_ptr
->error_fn
= error_fn
;
872 png_ptr
->warning_fn
= warning_fn
;
873 png_ptr
->error_ptr
= error_ptr
;
874 #ifdef PNG_USER_MEM_SUPPORTED
875 png_ptr
->free_fn
= free_fn
;
878 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
882 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
884 png_ptr
->read_row_fn
= read_row_fn
;