2 /* pngpread.c - read a png file in push mode
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
14 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
17 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
18 png_bytep buffer
, png_size_t buffer_size
)
20 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
22 while (png_ptr
->buffer_size
)
24 png_process_some_data(png_ptr
, info_ptr
);
28 /* What we do with the incoming data depends on what we were previously
29 * doing before we ran out of data...
32 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
34 switch (png_ptr
->process_mode
)
36 case PNG_READ_SIG_MODE
:
38 png_push_read_sig(png_ptr
, info_ptr
);
41 case PNG_READ_CHUNK_MODE
:
43 png_push_read_chunk(png_ptr
, info_ptr
);
46 case PNG_READ_IDAT_MODE
:
48 png_push_read_IDAT(png_ptr
);
51 #if defined(PNG_READ_tEXt_SUPPORTED)
52 case PNG_READ_tEXt_MODE
:
54 png_push_read_tEXt(png_ptr
, info_ptr
);
58 #if defined(PNG_READ_zTXt_SUPPORTED)
59 case PNG_READ_zTXt_MODE
:
61 png_push_read_zTXt(png_ptr
, info_ptr
);
67 png_push_crc_finish(png_ptr
);
72 png_ptr
->buffer_size
= 0;
78 /* Read any remaining signature bytes from the stream and compare them with
79 * the correct PNG signature. It is possible that this routine is called
80 * with bytes already read from the signature, either because they have been
81 * checked by the calling application, or because of multiple calls to this
85 png_push_read_sig(png_structp png_ptr
, png_infop info_ptr
)
87 png_size_t num_checked
= png_ptr
->sig_bytes
,
88 num_to_check
= 8 - num_checked
;
90 if (png_ptr
->buffer_size
< num_to_check
)
92 num_to_check
= png_ptr
->buffer_size
;
95 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
97 png_ptr
->sig_bytes
+= num_to_check
;
99 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
101 if (num_checked
< 4 &&
102 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
103 png_error(png_ptr
, "Not a PNG file");
105 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
109 if (png_ptr
->sig_bytes
>= 8)
111 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
117 png_push_read_chunk(png_structp png_ptr
, png_infop info_ptr
)
119 /* First we make sure we have enough data for the 4 byte chunk name
120 * and the 4 byte chunk length before proceeding with decoding the
121 * chunk data. To fully decode each of these chunks, we also make
122 * sure we have enough data in the buffer for the 4 byte CRC at the
123 * end of every chunk (except IDAT, which is handled separately).
125 if (!(png_ptr
->flags
& PNG_FLAG_HAVE_CHUNK_HEADER
))
127 png_byte chunk_length
[4];
129 if (png_ptr
->buffer_size
< 8)
131 png_push_save_buffer(png_ptr
);
135 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
136 png_ptr
->push_length
= png_get_uint_32(chunk_length
);
137 png_reset_crc(png_ptr
);
138 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
139 png_ptr
->flags
|= PNG_FLAG_HAVE_CHUNK_HEADER
;
142 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
144 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
146 png_push_save_buffer(png_ptr
);
150 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
152 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
154 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
156 png_push_save_buffer(png_ptr
);
160 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
162 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
164 /* If we reach an IDAT chunk, this means we have read all of the
165 * header chunks, and we can start reading the image (or if this
166 * is called after the image has been read - we have an error).
168 if (png_ptr
->mode
& PNG_HAVE_IDAT
)
170 if (png_ptr
->push_length
== 0)
173 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
174 png_error(png_ptr
, "Too many IDAT's found");
177 png_ptr
->idat_size
= png_ptr
->push_length
;
178 png_ptr
->mode
|= PNG_HAVE_IDAT
;
179 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
180 png_push_have_info(png_ptr
, info_ptr
);
181 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
182 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
185 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
187 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
189 png_push_save_buffer(png_ptr
);
193 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
194 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
195 png_push_have_end(png_ptr
, info_ptr
);
197 #if defined(PNG_READ_gAMA_SUPPORTED)
198 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
200 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
202 png_push_save_buffer(png_ptr
);
206 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
209 #if defined(PNG_READ_sBIT_SUPPORTED)
210 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
212 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
214 png_push_save_buffer(png_ptr
);
218 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
221 #if defined(PNG_READ_cHRM_SUPPORTED)
222 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
224 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
226 png_push_save_buffer(png_ptr
);
230 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
233 #if defined(PNG_READ_sRGB_SUPPORTED)
234 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
236 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
238 png_push_save_buffer(png_ptr
);
242 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
245 #if defined(PNG_READ_tRNS_SUPPORTED)
246 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
248 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
250 png_push_save_buffer(png_ptr
);
254 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
257 #if defined(PNG_READ_bKGD_SUPPORTED)
258 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
260 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
262 png_push_save_buffer(png_ptr
);
266 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
269 #if defined(PNG_READ_hIST_SUPPORTED)
270 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
272 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
274 png_push_save_buffer(png_ptr
);
278 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
281 #if defined(PNG_READ_pHYs_SUPPORTED)
282 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
284 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
286 png_push_save_buffer(png_ptr
);
290 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
293 #if defined(PNG_READ_oFFs_SUPPORTED)
294 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
296 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
298 png_push_save_buffer(png_ptr
);
302 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
305 #if defined(PNG_READ_pCAL_SUPPORTED)
306 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
308 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
310 png_push_save_buffer(png_ptr
);
314 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
317 #if defined(PNG_READ_tIME_SUPPORTED)
318 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
320 if (png_ptr
->push_length
+ 4 > png_ptr
->buffer_size
)
322 png_push_save_buffer(png_ptr
);
326 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
329 #if defined(PNG_READ_tEXt_SUPPORTED)
330 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
332 png_push_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
335 #if defined(PNG_READ_zTXt_SUPPORTED)
336 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
338 png_push_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
343 png_push_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
);
346 png_ptr
->flags
&= ~PNG_FLAG_HAVE_CHUNK_HEADER
;
350 png_push_crc_skip(png_structp png_ptr
, png_uint_32 skip
)
352 png_ptr
->process_mode
= PNG_SKIP_MODE
;
353 png_ptr
->skip_length
= skip
;
357 png_push_crc_finish(png_structp png_ptr
)
359 if (png_ptr
->skip_length
&& png_ptr
->save_buffer_size
)
361 png_size_t save_size
;
363 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->save_buffer_size
)
364 save_size
= (png_size_t
)png_ptr
->skip_length
;
366 save_size
= png_ptr
->save_buffer_size
;
368 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
370 png_ptr
->skip_length
-= save_size
;
371 png_ptr
->buffer_size
-= save_size
;
372 png_ptr
->save_buffer_size
-= save_size
;
373 png_ptr
->save_buffer_ptr
+= save_size
;
375 if (png_ptr
->skip_length
&& png_ptr
->current_buffer_size
)
377 png_size_t save_size
;
379 if (png_ptr
->skip_length
< (png_uint_32
)png_ptr
->current_buffer_size
)
380 save_size
= (png_size_t
)png_ptr
->skip_length
;
382 save_size
= png_ptr
->current_buffer_size
;
384 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
386 png_ptr
->skip_length
-= save_size
;
387 png_ptr
->buffer_size
-= save_size
;
388 png_ptr
->current_buffer_size
-= save_size
;
389 png_ptr
->current_buffer_ptr
+= save_size
;
391 if (!png_ptr
->skip_length
)
393 if (png_ptr
->buffer_size
< 4)
395 png_push_save_buffer(png_ptr
);
399 png_crc_finish(png_ptr
, 0);
400 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
405 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
410 if (png_ptr
->save_buffer_size
)
412 png_size_t save_size
;
414 if (length
< png_ptr
->save_buffer_size
)
417 save_size
= png_ptr
->save_buffer_size
;
419 png_memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
422 png_ptr
->buffer_size
-= save_size
;
423 png_ptr
->save_buffer_size
-= save_size
;
424 png_ptr
->save_buffer_ptr
+= save_size
;
426 if (length
&& png_ptr
->current_buffer_size
)
428 png_size_t save_size
;
430 if (length
< png_ptr
->current_buffer_size
)
433 save_size
= png_ptr
->current_buffer_size
;
435 png_memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
436 png_ptr
->buffer_size
-= save_size
;
437 png_ptr
->current_buffer_size
-= save_size
;
438 png_ptr
->current_buffer_ptr
+= save_size
;
443 png_push_save_buffer(png_structp png_ptr
)
445 if (png_ptr
->save_buffer_size
)
447 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
453 istop
= png_ptr
->save_buffer_size
;
454 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
455 i
< istop
; i
++, sp
++, dp
++)
461 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
462 png_ptr
->save_buffer_max
)
465 png_bytep old_buffer
;
467 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
468 old_buffer
= png_ptr
->save_buffer
;
469 png_ptr
->save_buffer
= (png_bytep
)png_malloc(png_ptr
,
470 (png_uint_32
)new_max
);
471 png_memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
472 png_free(png_ptr
, old_buffer
);
473 png_ptr
->save_buffer_max
= new_max
;
475 if (png_ptr
->current_buffer_size
)
477 png_memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
478 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
479 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
480 png_ptr
->current_buffer_size
= 0;
482 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
483 png_ptr
->buffer_size
= 0;
487 png_push_restore_buffer(png_structp png_ptr
, png_bytep buffer
,
488 png_size_t buffer_length
)
490 png_ptr
->current_buffer
= buffer
;
491 png_ptr
->current_buffer_size
= buffer_length
;
492 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
493 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
497 png_push_read_IDAT(png_structp png_ptr
)
499 if (!(png_ptr
->flags
& PNG_FLAG_HAVE_CHUNK_HEADER
))
501 png_byte chunk_length
[4];
503 if (png_ptr
->buffer_size
< 8)
505 png_push_save_buffer(png_ptr
);
509 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
510 png_ptr
->push_length
= png_get_uint_32(chunk_length
);
512 png_reset_crc(png_ptr
);
513 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
514 png_ptr
->flags
|= PNG_FLAG_HAVE_CHUNK_HEADER
;
516 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
518 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
519 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
))
520 png_error(png_ptr
, "Not enough compressed data");
524 png_ptr
->idat_size
= png_ptr
->push_length
;
526 if (png_ptr
->idat_size
&& png_ptr
->save_buffer_size
)
528 png_size_t save_size
;
530 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->save_buffer_size
)
532 save_size
= (png_size_t
)png_ptr
->idat_size
;
533 /* check for overflow */
534 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
535 png_error(png_ptr
, "save_size overflowed in pngpread");
538 save_size
= png_ptr
->save_buffer_size
;
540 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
541 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
543 png_ptr
->idat_size
-= save_size
;
544 png_ptr
->buffer_size
-= save_size
;
545 png_ptr
->save_buffer_size
-= save_size
;
546 png_ptr
->save_buffer_ptr
+= save_size
;
548 if (png_ptr
->idat_size
&& png_ptr
->current_buffer_size
)
550 png_size_t save_size
;
552 if (png_ptr
->idat_size
< (png_uint_32
)png_ptr
->current_buffer_size
)
554 save_size
= (png_size_t
)png_ptr
->idat_size
;
555 /* check for overflow */
556 if((png_uint_32
)save_size
!= png_ptr
->idat_size
)
557 png_error(png_ptr
, "save_size overflowed in pngpread");
560 save_size
= png_ptr
->current_buffer_size
;
562 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
563 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
565 png_ptr
->idat_size
-= save_size
;
566 png_ptr
->buffer_size
-= save_size
;
567 png_ptr
->current_buffer_size
-= save_size
;
568 png_ptr
->current_buffer_ptr
+= save_size
;
570 if (!png_ptr
->idat_size
)
572 if (png_ptr
->buffer_size
< 4)
574 png_push_save_buffer(png_ptr
);
578 png_crc_finish(png_ptr
, 0);
579 png_ptr
->flags
&= ~PNG_FLAG_HAVE_CHUNK_HEADER
;
584 png_process_IDAT_data(png_structp png_ptr
, png_bytep buffer
,
585 png_size_t buffer_length
)
589 if ((png_ptr
->flags
& PNG_FLAG_ZLIB_FINISHED
) && buffer_length
)
590 png_error(png_ptr
, "Extra compression data");
592 png_ptr
->zstream
.next_in
= buffer
;
593 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
596 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
597 if (ret
== Z_STREAM_END
)
599 if (png_ptr
->zstream
.avail_in
)
600 png_error(png_ptr
, "Extra compressed data");
601 if (!(png_ptr
->zstream
.avail_out
))
603 png_push_process_row(png_ptr
);
606 png_ptr
->mode
|= PNG_AFTER_IDAT
;
607 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
610 else if (ret
== Z_BUF_ERROR
)
612 else if (ret
!= Z_OK
)
613 png_error(png_ptr
, "Decompression Error");
614 if (!(png_ptr
->zstream
.avail_out
))
616 png_push_process_row(png_ptr
);
617 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
618 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
626 png_push_process_row(png_structp png_ptr
)
628 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
629 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
630 png_ptr
->row_info
.channels
= png_ptr
->channels
;
631 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
632 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
634 png_ptr
->row_info
.rowbytes
= ((png_ptr
->row_info
.width
*
635 (png_uint_32
)png_ptr
->row_info
.pixel_depth
+ 7) >> 3);
637 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
638 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
639 (int)(png_ptr
->row_buf
[0]));
641 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
642 png_ptr
->rowbytes
+ 1);
644 if (png_ptr
->transformations
)
645 png_do_read_transformations(png_ptr
);
647 #if defined(PNG_READ_INTERLACING_SUPPORTED)
648 /* blow up interlaced rows to full size */
649 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
651 if (png_ptr
->pass
< 6)
652 png_do_read_interlace(&(png_ptr
->row_info
),
653 png_ptr
->row_buf
+ 1, png_ptr
->pass
, png_ptr
->transformations
);
655 switch (png_ptr
->pass
)
660 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
662 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
663 png_read_push_finish_row(png_ptr
);
670 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
672 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
673 png_read_push_finish_row(png_ptr
);
675 if (png_ptr
->pass
== 2)
677 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
679 png_push_have_row(png_ptr
, NULL
);
680 png_read_push_finish_row(png_ptr
);
688 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
690 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
691 png_read_push_finish_row(png_ptr
);
693 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
695 png_push_have_row(png_ptr
, NULL
);
696 png_read_push_finish_row(png_ptr
);
703 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
705 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
706 png_read_push_finish_row(png_ptr
);
708 if (png_ptr
->pass
== 4)
710 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
712 png_push_have_row(png_ptr
, NULL
);
713 png_read_push_finish_row(png_ptr
);
721 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
723 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
724 png_read_push_finish_row(png_ptr
);
726 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
728 png_push_have_row(png_ptr
, NULL
);
729 png_read_push_finish_row(png_ptr
);
736 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
738 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
739 png_read_push_finish_row(png_ptr
);
741 if (png_ptr
->pass
== 6)
743 png_push_have_row(png_ptr
, NULL
);
744 png_read_push_finish_row(png_ptr
);
750 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
751 png_read_push_finish_row(png_ptr
);
752 if (png_ptr
->pass
!= 6)
754 png_push_have_row(png_ptr
, NULL
);
755 png_read_push_finish_row(png_ptr
);
762 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
763 png_read_push_finish_row(png_ptr
);
768 png_read_push_finish_row(png_structp png_ptr
)
770 png_ptr
->row_number
++;
771 if (png_ptr
->row_number
< png_ptr
->num_rows
)
774 if (png_ptr
->interlaced
)
776 png_ptr
->row_number
= 0;
777 png_memset_check(png_ptr
, png_ptr
->prev_row
, 0,
778 png_ptr
->rowbytes
+ 1);
782 if (png_ptr
->pass
>= 7)
785 png_ptr
->iwidth
= (png_ptr
->width
+
786 png_pass_inc
[png_ptr
->pass
] - 1 -
787 png_pass_start
[png_ptr
->pass
]) /
788 png_pass_inc
[png_ptr
->pass
];
790 png_ptr
->irowbytes
= ((png_ptr
->iwidth
*
791 png_ptr
->pixel_depth
+ 7) >> 3) + 1;
793 if (png_ptr
->transformations
& PNG_INTERLACE
)
796 png_ptr
->num_rows
= (png_ptr
->height
+
797 png_pass_yinc
[png_ptr
->pass
] - 1 -
798 png_pass_ystart
[png_ptr
->pass
]) /
799 png_pass_yinc
[png_ptr
->pass
];
801 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
805 #if defined(PNG_READ_tEXt_SUPPORTED)
807 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
809 if (png_ptr
->mode
== PNG_BEFORE_IHDR
|| png_ptr
->mode
& PNG_HAVE_IEND
)
811 png_error(png_ptr
, "Out of place tEXt");
812 /* to quiet some compiler warnings */
813 if(info_ptr
== NULL
) return;
816 #ifdef PNG_MAX_MALLOC_64K
817 png_ptr
->skip_length
= 0; /* This may not be necessary */
819 if (length
> (png_uint_32
)65535L) /* Can't hold the entire string in memory */
821 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
822 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
823 length
= (png_uint_32
)65535L;
827 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
828 (png_uint_32
)(length
+1));
829 png_ptr
->current_text
[length
] = '\0';
830 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
831 png_ptr
->current_text_size
= (png_size_t
)length
;
832 png_ptr
->current_text_left
= (png_size_t
)length
;
833 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
837 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
839 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
841 png_size_t text_size
;
843 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
844 text_size
= png_ptr
->buffer_size
;
846 text_size
= png_ptr
->current_text_left
;
847 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
848 png_ptr
->current_text_left
-= text_size
;
849 png_ptr
->current_text_ptr
+= text_size
;
851 if (!(png_ptr
->current_text_left
))
857 if (png_ptr
->buffer_size
< 4)
859 png_push_save_buffer(png_ptr
);
863 png_push_crc_finish(png_ptr
);
865 #if defined(PNG_MAX_MALLOC_64K)
866 if (png_ptr
->skip_length
)
870 key
= png_ptr
->current_text
;
871 png_ptr
->current_text
= 0;
873 for (text
= key
; *text
; text
++)
876 if (text
!= key
+ png_ptr
->current_text_size
)
879 text_ptr
= (png_textp
)png_malloc(png_ptr
, (png_uint_32
)sizeof(png_text
));
880 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
882 text_ptr
->text
= text
;
884 png_set_text(png_ptr
, info_ptr
, text_ptr
, 1);
886 png_free(png_ptr
, text_ptr
);
891 #if defined(PNG_READ_zTXt_SUPPORTED)
893 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
895 if (png_ptr
->mode
== PNG_BEFORE_IHDR
|| png_ptr
->mode
& PNG_HAVE_IEND
)
897 png_error(png_ptr
, "Out of place zTXt");
898 /* to quiet some compiler warnings */
899 if(info_ptr
== NULL
) return;
902 #ifdef PNG_MAX_MALLOC_64K
903 /* We can't handle zTXt chunks > 64K, since we don't have enough space
904 * to be able to store the uncompressed data. Actually, the threshold
905 * is probably around 32K, but it isn't as definite as 64K is.
907 if (length
> (png_uint_32
)65535L)
909 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
910 png_push_crc_skip(png_ptr
, length
);
915 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
916 (png_uint_32
)(length
+1));
917 png_ptr
->current_text
[length
] = '\0';
918 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
919 png_ptr
->current_text_size
= (png_size_t
)length
;
920 png_ptr
->current_text_left
= (png_size_t
)length
;
921 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
925 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
927 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
929 png_size_t text_size
;
931 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
932 text_size
= png_ptr
->buffer_size
;
934 text_size
= png_ptr
->current_text_left
;
935 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
936 png_ptr
->current_text_left
-= text_size
;
937 png_ptr
->current_text_ptr
+= text_size
;
939 if (!(png_ptr
->current_text_left
))
945 png_size_t text_size
, key_size
;
947 if (png_ptr
->buffer_size
< 4)
949 png_push_save_buffer(png_ptr
);
953 png_push_crc_finish(png_ptr
);
955 key
= png_ptr
->current_text
;
956 png_ptr
->current_text
= 0;
958 for (text
= key
; *text
; text
++)
961 /* zTXt can't have zero text */
962 if (text
== key
+ png_ptr
->current_text_size
)
964 png_free(png_ptr
, key
);
970 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* check compression byte */
972 png_free(png_ptr
, key
);
978 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
979 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
981 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
982 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
984 key_size
= text
- key
;
989 while (png_ptr
->zstream
.avail_in
)
991 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
992 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
994 inflateReset(&png_ptr
->zstream
);
995 png_ptr
->zstream
.avail_in
= 0;
996 png_free(png_ptr
, key
);
997 png_free(png_ptr
, text
);
1000 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1004 text
= (png_charp
)png_malloc(png_ptr
,
1005 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
+
1007 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1008 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1009 png_memcpy(text
, key
, key_size
);
1010 text_size
= key_size
+ png_ptr
->zbuf_size
-
1011 png_ptr
->zstream
.avail_out
;
1012 *(text
+ text_size
) = '\0';
1019 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1020 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1022 png_memcpy(text
, tmp
, text_size
);
1023 png_free(png_ptr
, tmp
);
1024 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1025 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1026 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1027 *(text
+ text_size
) = '\0';
1029 if (ret
!= Z_STREAM_END
)
1031 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1032 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1040 if (ret
== Z_STREAM_END
)
1044 inflateReset(&png_ptr
->zstream
);
1045 png_ptr
->zstream
.avail_in
= 0;
1047 if (ret
!= Z_STREAM_END
)
1049 png_free(png_ptr
, key
);
1050 png_free(png_ptr
, text
);
1054 png_free(png_ptr
, key
);
1058 text_ptr
= (png_textp
)png_malloc(png_ptr
, (png_uint_32
)sizeof(png_text
));
1059 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1060 text_ptr
->key
= key
;
1061 text_ptr
->text
= text
;
1063 png_set_text(png_ptr
, info_ptr
, text_ptr
, 1);
1065 png_free(png_ptr
, text_ptr
);
1070 /* This function is called when we haven't found a handler for this
1071 * chunk. In the future we will have code here that can handle
1072 * user-defined callback functions for unknown chunks before they are
1073 * ignored or cause an error. If there isn't a problem with the
1074 * chunk itself (ie a bad chunk name or a critical chunk), the chunk
1075 * is (currently) silently ignored.
1078 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1080 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
1082 if (!(png_ptr
->chunk_name
[0] & 0x20))
1084 png_chunk_error(png_ptr
, "unknown critical chunk");
1085 /* to quiet some compiler warnings */
1086 if(info_ptr
== NULL
) return;
1089 png_push_crc_skip(png_ptr
, length
);
1093 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1095 if (png_ptr
->info_fn
!= NULL
)
1096 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1100 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1102 if (png_ptr
->end_fn
!= NULL
)
1103 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1107 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1109 if (png_ptr
->row_fn
!= NULL
)
1110 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1111 (int)png_ptr
->pass
);
1115 png_progressive_combine_row (png_structp png_ptr
,
1116 png_bytep old_row
, png_bytep new_row
)
1118 if (new_row
!= NULL
) /* new_row must == png_ptr->row_buf here. */
1119 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1123 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1124 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1125 png_progressive_end_ptr end_fn
)
1127 png_ptr
->info_fn
= info_fn
;
1128 png_ptr
->row_fn
= row_fn
;
1129 png_ptr
->end_fn
= end_fn
;
1131 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1135 png_get_progressive_ptr(png_structp png_ptr
)
1137 return png_ptr
->io_ptr
;
1140 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */