2 /* pngpread.c - read a png file in push mode
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, Glenn Randers-Pehrson
15 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
18 png_process_data(png_structp png_ptr
, png_infop info_ptr
,
19 png_bytep buffer
, png_size_t buffer_size
)
21 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
23 while (png_ptr
->buffer_size
)
25 png_process_some_data(png_ptr
, info_ptr
);
29 /* What we do with the incoming data depends on what we were previously
30 * doing before we ran out of data...
33 png_process_some_data(png_structp png_ptr
, png_infop info_ptr
)
35 switch (png_ptr
->process_mode
)
37 case PNG_READ_SIG_MODE
:
39 png_push_read_sig(png_ptr
, info_ptr
);
42 case PNG_READ_CHUNK_MODE
:
44 png_push_read_chunk(png_ptr
, info_ptr
);
47 case PNG_READ_IDAT_MODE
:
49 png_push_read_IDAT(png_ptr
);
52 #if defined(PNG_READ_tEXt_SUPPORTED)
53 case PNG_READ_tEXt_MODE
:
55 png_push_read_tEXt(png_ptr
, info_ptr
);
59 #if defined(PNG_READ_zTXt_SUPPORTED)
60 case PNG_READ_zTXt_MODE
:
62 png_push_read_zTXt(png_ptr
, info_ptr
);
68 png_push_crc_finish(png_ptr
);
73 png_ptr
->buffer_size
= 0;
79 /* Read any remaining signature bytes from the stream and compare them with
80 * the correct PNG signature. It is possible that this routine is called
81 * with bytes already read from the signature, whether because they have been
82 * checked by the calling application, or from multiple calls to this routine.
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 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
454 i
< png_ptr
->save_buffer_size
;
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)
784 png_ptr
->iwidth
= (png_ptr
->width
+
785 png_pass_inc
[png_ptr
->pass
] - 1 -
786 png_pass_start
[png_ptr
->pass
]) /
787 png_pass_inc
[png_ptr
->pass
];
789 png_ptr
->irowbytes
= ((png_ptr
->iwidth
*
790 png_ptr
->pixel_depth
+ 7) >> 3) + 1;
792 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
794 png_ptr
->num_rows
= (png_ptr
->height
+
795 png_pass_yinc
[png_ptr
->pass
] - 1 -
796 png_pass_ystart
[png_ptr
->pass
]) /
797 png_pass_yinc
[png_ptr
->pass
];
798 if (!(png_ptr
->num_rows
))
801 if (png_ptr
->transformations
& PNG_INTERLACE
)
803 } while (png_ptr
->iwidth
== 0);
807 #if defined(PNG_READ_tEXt_SUPPORTED)
809 png_push_handle_tEXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
811 if (png_ptr
->mode
== PNG_BEFORE_IHDR
|| png_ptr
->mode
& PNG_HAVE_IEND
)
813 png_error(png_ptr
, "Out of place tEXt");
814 /* to quiet some compiler warnings */
815 if(info_ptr
== NULL
) return;
818 #ifdef PNG_MAX_MALLOC_64K
819 png_ptr
->skip_length
= 0; /* This may not be necessary */
821 if (length
> (png_uint_32
)65535L) /* Can't hold the entire string in memory */
823 png_warning(png_ptr
, "tEXt chunk too large to fit in memory");
824 png_ptr
->skip_length
= length
- (png_uint_32
)65535L;
825 length
= (png_uint_32
)65535L;
829 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
830 (png_uint_32
)(length
+1));
831 png_ptr
->current_text
[length
] = '\0';
832 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
833 png_ptr
->current_text_size
= (png_size_t
)length
;
834 png_ptr
->current_text_left
= (png_size_t
)length
;
835 png_ptr
->process_mode
= PNG_READ_tEXt_MODE
;
839 png_push_read_tEXt(png_structp png_ptr
, png_infop info_ptr
)
841 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
843 png_size_t text_size
;
845 if (png_ptr
->buffer_size
< png_ptr
->current_text_left
)
846 text_size
= png_ptr
->buffer_size
;
848 text_size
= png_ptr
->current_text_left
;
849 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
850 png_ptr
->current_text_left
-= text_size
;
851 png_ptr
->current_text_ptr
+= text_size
;
853 if (!(png_ptr
->current_text_left
))
859 if (png_ptr
->buffer_size
< 4)
861 png_push_save_buffer(png_ptr
);
865 png_push_crc_finish(png_ptr
);
867 #if defined(PNG_MAX_MALLOC_64K)
868 if (png_ptr
->skip_length
)
872 key
= png_ptr
->current_text
;
873 png_ptr
->current_text
= 0;
875 for (text
= key
; *text
; text
++)
878 if (text
!= key
+ png_ptr
->current_text_size
)
881 text_ptr
= (png_textp
)png_malloc(png_ptr
, (png_uint_32
)sizeof(png_text
));
882 text_ptr
->compression
= PNG_TEXT_COMPRESSION_NONE
;
884 text_ptr
->text
= text
;
886 png_set_text(png_ptr
, info_ptr
, text_ptr
, 1);
888 png_free(png_ptr
, text_ptr
);
893 #if defined(PNG_READ_zTXt_SUPPORTED)
895 png_push_handle_zTXt(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
897 if (png_ptr
->mode
== PNG_BEFORE_IHDR
|| png_ptr
->mode
& PNG_HAVE_IEND
)
899 png_error(png_ptr
, "Out of place zTXt");
900 /* to quiet some compiler warnings */
901 if(info_ptr
== NULL
) return;
904 #ifdef PNG_MAX_MALLOC_64K
905 /* We can't handle zTXt chunks > 64K, since we don't have enough space
906 * to be able to store the uncompressed data. Actually, the threshold
907 * is probably around 32K, but it isn't as definite as 64K is.
909 if (length
> (png_uint_32
)65535L)
911 png_warning(png_ptr
, "zTXt chunk too large to fit in memory");
912 png_push_crc_skip(png_ptr
, length
);
917 png_ptr
->current_text
= (png_charp
)png_malloc(png_ptr
,
918 (png_uint_32
)(length
+1));
919 png_ptr
->current_text
[length
] = '\0';
920 png_ptr
->current_text_ptr
= png_ptr
->current_text
;
921 png_ptr
->current_text_size
= (png_size_t
)length
;
922 png_ptr
->current_text_left
= (png_size_t
)length
;
923 png_ptr
->process_mode
= PNG_READ_zTXt_MODE
;
927 png_push_read_zTXt(png_structp png_ptr
, png_infop info_ptr
)
929 if (png_ptr
->buffer_size
&& png_ptr
->current_text_left
)
931 png_size_t text_size
;
933 if (png_ptr
->buffer_size
< (png_uint_32
)png_ptr
->current_text_left
)
934 text_size
= png_ptr
->buffer_size
;
936 text_size
= png_ptr
->current_text_left
;
937 png_crc_read(png_ptr
, (png_bytep
)png_ptr
->current_text_ptr
, text_size
);
938 png_ptr
->current_text_left
-= text_size
;
939 png_ptr
->current_text_ptr
+= text_size
;
941 if (!(png_ptr
->current_text_left
))
947 png_size_t text_size
, key_size
;
949 if (png_ptr
->buffer_size
< 4)
951 png_push_save_buffer(png_ptr
);
955 png_push_crc_finish(png_ptr
);
957 key
= png_ptr
->current_text
;
958 png_ptr
->current_text
= 0;
960 for (text
= key
; *text
; text
++)
963 /* zTXt can't have zero text */
964 if (text
== key
+ png_ptr
->current_text_size
)
966 png_free(png_ptr
, key
);
972 if (*text
!= PNG_TEXT_COMPRESSION_zTXt
) /* check compression byte */
974 png_free(png_ptr
, key
);
980 png_ptr
->zstream
.next_in
= (png_bytep
)text
;
981 png_ptr
->zstream
.avail_in
= (uInt
)(png_ptr
->current_text_size
-
983 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
984 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
986 key_size
= text
- key
;
991 while (png_ptr
->zstream
.avail_in
)
993 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
994 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
996 inflateReset(&png_ptr
->zstream
);
997 png_ptr
->zstream
.avail_in
= 0;
998 png_free(png_ptr
, key
);
999 png_free(png_ptr
, text
);
1002 if (!(png_ptr
->zstream
.avail_out
) || ret
== Z_STREAM_END
)
1006 text
= (png_charp
)png_malloc(png_ptr
,
1007 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
+
1009 png_memcpy(text
+ key_size
, png_ptr
->zbuf
,
1010 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1011 png_memcpy(text
, key
, key_size
);
1012 text_size
= key_size
+ png_ptr
->zbuf_size
-
1013 png_ptr
->zstream
.avail_out
;
1014 *(text
+ text_size
) = '\0';
1021 text
= (png_charp
)png_malloc(png_ptr
, text_size
+
1022 (png_uint_32
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
1024 png_memcpy(text
, tmp
, text_size
);
1025 png_free(png_ptr
, tmp
);
1026 png_memcpy(text
+ text_size
, png_ptr
->zbuf
,
1027 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1028 text_size
+= png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
;
1029 *(text
+ text_size
) = '\0';
1031 if (ret
!= Z_STREAM_END
)
1033 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1034 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1042 if (ret
== Z_STREAM_END
)
1046 inflateReset(&png_ptr
->zstream
);
1047 png_ptr
->zstream
.avail_in
= 0;
1049 if (ret
!= Z_STREAM_END
)
1051 png_free(png_ptr
, key
);
1052 png_free(png_ptr
, text
);
1056 png_free(png_ptr
, key
);
1060 text_ptr
= (png_textp
)png_malloc(png_ptr
, (png_uint_32
)sizeof(png_text
));
1061 text_ptr
->compression
= PNG_TEXT_COMPRESSION_zTXt
;
1062 text_ptr
->key
= key
;
1063 text_ptr
->text
= text
;
1065 png_set_text(png_ptr
, info_ptr
, text_ptr
, 1);
1067 png_free(png_ptr
, text_ptr
);
1072 /* This function is called when we haven't found a handler for this
1073 * chunk. In the future we will have code here which can handle
1074 * user-defined callback functions for unknown chunks before they are
1075 * ignored or cause an error. If there isn't a problem with the
1076 * chunk itself (ie a bad chunk name or a critical chunk), the chunk
1077 * is (currently) silently ignored.
1080 png_push_handle_unknown(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 length
)
1082 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
1084 if (!(png_ptr
->chunk_name
[0] & 0x20))
1086 png_chunk_error(png_ptr
, "unknown critical chunk");
1087 /* to quiet some compiler warnings */
1088 if(info_ptr
== NULL
) return;
1091 png_push_crc_skip(png_ptr
, length
);
1095 png_push_have_info(png_structp png_ptr
, png_infop info_ptr
)
1097 if (png_ptr
->info_fn
!= NULL
)
1098 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1102 png_push_have_end(png_structp png_ptr
, png_infop info_ptr
)
1104 if (png_ptr
->end_fn
!= NULL
)
1105 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1109 png_push_have_row(png_structp png_ptr
, png_bytep row
)
1111 if (png_ptr
->row_fn
!= NULL
)
1112 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1113 (int)png_ptr
->pass
);
1117 png_progressive_combine_row (png_structp png_ptr
,
1118 png_bytep old_row
, png_bytep new_row
)
1120 if (new_row
!= NULL
)
1121 png_combine_row(png_ptr
, old_row
, png_pass_dsp_mask
[png_ptr
->pass
]);
1125 png_set_progressive_read_fn(png_structp png_ptr
, png_voidp progressive_ptr
,
1126 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1127 png_progressive_end_ptr end_fn
)
1129 png_ptr
->info_fn
= info_fn
;
1130 png_ptr
->row_fn
= row_fn
;
1131 png_ptr
->end_fn
= end_fn
;
1133 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1137 png_get_progressive_ptr(png_structp png_ptr
)
1139 return png_ptr
->io_ptr
;
1142 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */