2 /* pngpread.c - read a png file in push mode
4 * Last changed in libpng 1.6.17 [March 26, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
16 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
18 /* Push model modes */
19 #define PNG_READ_SIG_MODE 0
20 #define PNG_READ_CHUNK_MODE 1
21 #define PNG_READ_IDAT_MODE 2
22 #define PNG_SKIP_MODE 3
23 #define PNG_READ_tEXt_MODE 4
24 #define PNG_READ_zTXt_MODE 5
25 #define PNG_READ_DONE_MODE 6
26 #define PNG_READ_iTXt_MODE 7
27 #define PNG_ERROR_MODE 8
29 #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
30 if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
31 { png_push_save_buffer(png_ptr); return; }
32 #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
33 if (png_ptr->buffer_size < N) \
34 { png_push_save_buffer(png_ptr); return; }
37 png_process_data(png_structrp png_ptr
, png_inforp info_ptr
,
38 png_bytep buffer
, png_size_t buffer_size
)
40 if (png_ptr
== NULL
|| info_ptr
== NULL
)
43 png_push_restore_buffer(png_ptr
, buffer
, buffer_size
);
45 while (png_ptr
->buffer_size
)
47 png_process_some_data(png_ptr
, info_ptr
);
52 png_process_data_pause(png_structrp png_ptr
, int save
)
56 /* It's easiest for the caller if we do the save; then the caller doesn't
57 * have to supply the same data again:
60 png_push_save_buffer(png_ptr
);
63 /* This includes any pending saved bytes: */
64 png_size_t remaining
= png_ptr
->buffer_size
;
65 png_ptr
->buffer_size
= 0;
67 /* So subtract the saved buffer size, unless all the data
68 * is actually 'saved', in which case we just return 0
70 if (png_ptr
->save_buffer_size
< remaining
)
71 return remaining
- png_ptr
->save_buffer_size
;
79 png_process_data_skip(png_structrp png_ptr
)
81 png_uint_32 remaining
= 0;
83 if (png_ptr
!= NULL
&& png_ptr
->process_mode
== PNG_SKIP_MODE
&&
84 png_ptr
->skip_length
> 0)
86 /* At the end of png_process_data the buffer size must be 0 (see the loop
87 * above) so we can detect a broken call here:
89 if (png_ptr
->buffer_size
!= 0)
91 "png_process_data_skip called inside png_process_data");
93 /* If is impossible for there to be a saved buffer at this point -
94 * otherwise we could not be in SKIP mode. This will also happen if
95 * png_process_skip is called inside png_process_data (but only very
98 if (png_ptr
->save_buffer_size
!= 0)
99 png_error(png_ptr
, "png_process_data_skip called with saved data");
101 remaining
= png_ptr
->skip_length
;
102 png_ptr
->skip_length
= 0;
103 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
109 /* What we do with the incoming data depends on what we were previously
110 * doing before we ran out of data...
113 png_process_some_data(png_structrp png_ptr
, png_inforp info_ptr
)
118 switch (png_ptr
->process_mode
)
120 case PNG_READ_SIG_MODE
:
122 png_push_read_sig(png_ptr
, info_ptr
);
126 case PNG_READ_CHUNK_MODE
:
128 png_push_read_chunk(png_ptr
, info_ptr
);
132 case PNG_READ_IDAT_MODE
:
134 png_push_read_IDAT(png_ptr
);
140 png_push_crc_finish(png_ptr
);
146 png_ptr
->buffer_size
= 0;
152 /* Read any remaining signature bytes from the stream and compare them with
153 * the correct PNG signature. It is possible that this routine is called
154 * with bytes already read from the signature, either because they have been
155 * checked by the calling application, or because of multiple calls to this
159 png_push_read_sig(png_structrp png_ptr
, png_inforp info_ptr
)
161 png_size_t num_checked
= png_ptr
->sig_bytes
, /* SAFE, does not exceed 8 */
162 num_to_check
= 8 - num_checked
;
164 if (png_ptr
->buffer_size
< num_to_check
)
166 num_to_check
= png_ptr
->buffer_size
;
169 png_push_fill_buffer(png_ptr
, &(info_ptr
->signature
[num_checked
]),
171 png_ptr
->sig_bytes
= (png_byte
)(png_ptr
->sig_bytes
+ num_to_check
);
173 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
175 if (num_checked
< 4 &&
176 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
177 png_error(png_ptr
, "Not a PNG file");
180 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
184 if (png_ptr
->sig_bytes
>= 8)
186 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
192 png_push_read_chunk(png_structrp png_ptr
, png_inforp info_ptr
)
194 png_uint_32 chunk_name
;
195 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
196 int keep
; /* unknown handling method */
199 /* First we make sure we have enough data for the 4-byte chunk name
200 * and the 4-byte chunk length before proceeding with decoding the
201 * chunk data. To fully decode each of these chunks, we also make
202 * sure we have enough data in the buffer for the 4-byte CRC at the
203 * end of every chunk (except IDAT, which is handled separately).
205 if ((png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
) == 0)
207 png_byte chunk_length
[4];
208 png_byte chunk_tag
[4];
210 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
211 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
212 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
213 png_reset_crc(png_ptr
);
214 png_crc_read(png_ptr
, chunk_tag
, 4);
215 png_ptr
->chunk_name
= PNG_CHUNK_FROM_STRING(chunk_tag
);
216 png_check_chunk_name(png_ptr
, png_ptr
->chunk_name
);
217 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
220 chunk_name
= png_ptr
->chunk_name
;
222 if (chunk_name
== png_IDAT
)
224 if ((png_ptr
->mode
& PNG_AFTER_IDAT
) != 0)
225 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
227 /* If we reach an IDAT chunk, this means we have read all of the
228 * header chunks, and we can start reading the image (or if this
229 * is called after the image has been read - we have an error).
231 if ((png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
232 png_error(png_ptr
, "Missing IHDR before IDAT");
234 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
235 (png_ptr
->mode
& PNG_HAVE_PLTE
) == 0)
236 png_error(png_ptr
, "Missing PLTE before IDAT");
238 png_ptr
->mode
|= PNG_HAVE_IDAT
;
239 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
241 if ((png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
) == 0)
242 if (png_ptr
->push_length
== 0)
245 if ((png_ptr
->mode
& PNG_AFTER_IDAT
) != 0)
246 png_benign_error(png_ptr
, "Too many IDATs found");
249 if (chunk_name
== png_IHDR
)
251 if (png_ptr
->push_length
!= 13)
252 png_error(png_ptr
, "Invalid IHDR length");
254 PNG_PUSH_SAVE_BUFFER_IF_FULL
255 png_handle_IHDR(png_ptr
, info_ptr
, png_ptr
->push_length
);
258 else if (chunk_name
== png_IEND
)
260 PNG_PUSH_SAVE_BUFFER_IF_FULL
261 png_handle_IEND(png_ptr
, info_ptr
, png_ptr
->push_length
);
263 png_ptr
->process_mode
= PNG_READ_DONE_MODE
;
264 png_push_have_end(png_ptr
, info_ptr
);
267 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
268 else if ((keep
= png_chunk_unknown_handling(png_ptr
, chunk_name
)) != 0)
270 PNG_PUSH_SAVE_BUFFER_IF_FULL
271 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
, keep
);
273 if (chunk_name
== png_PLTE
)
274 png_ptr
->mode
|= PNG_HAVE_PLTE
;
278 else if (chunk_name
== png_PLTE
)
280 PNG_PUSH_SAVE_BUFFER_IF_FULL
281 png_handle_PLTE(png_ptr
, info_ptr
, png_ptr
->push_length
);
284 else if (chunk_name
== png_IDAT
)
286 png_ptr
->idat_size
= png_ptr
->push_length
;
287 png_ptr
->process_mode
= PNG_READ_IDAT_MODE
;
288 png_push_have_info(png_ptr
, info_ptr
);
289 png_ptr
->zstream
.avail_out
=
290 (uInt
) PNG_ROWBYTES(png_ptr
->pixel_depth
,
291 png_ptr
->iwidth
) + 1;
292 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
296 #ifdef PNG_READ_gAMA_SUPPORTED
297 else if (png_ptr
->chunk_name
== png_gAMA
)
299 PNG_PUSH_SAVE_BUFFER_IF_FULL
300 png_handle_gAMA(png_ptr
, info_ptr
, png_ptr
->push_length
);
304 #ifdef PNG_READ_sBIT_SUPPORTED
305 else if (png_ptr
->chunk_name
== png_sBIT
)
307 PNG_PUSH_SAVE_BUFFER_IF_FULL
308 png_handle_sBIT(png_ptr
, info_ptr
, png_ptr
->push_length
);
312 #ifdef PNG_READ_cHRM_SUPPORTED
313 else if (png_ptr
->chunk_name
== png_cHRM
)
315 PNG_PUSH_SAVE_BUFFER_IF_FULL
316 png_handle_cHRM(png_ptr
, info_ptr
, png_ptr
->push_length
);
320 #ifdef PNG_READ_sRGB_SUPPORTED
321 else if (chunk_name
== png_sRGB
)
323 PNG_PUSH_SAVE_BUFFER_IF_FULL
324 png_handle_sRGB(png_ptr
, info_ptr
, png_ptr
->push_length
);
328 #ifdef PNG_READ_iCCP_SUPPORTED
329 else if (png_ptr
->chunk_name
== png_iCCP
)
331 PNG_PUSH_SAVE_BUFFER_IF_FULL
332 png_handle_iCCP(png_ptr
, info_ptr
, png_ptr
->push_length
);
336 #ifdef PNG_READ_sPLT_SUPPORTED
337 else if (chunk_name
== png_sPLT
)
339 PNG_PUSH_SAVE_BUFFER_IF_FULL
340 png_handle_sPLT(png_ptr
, info_ptr
, png_ptr
->push_length
);
344 #ifdef PNG_READ_tRNS_SUPPORTED
345 else if (chunk_name
== png_tRNS
)
347 PNG_PUSH_SAVE_BUFFER_IF_FULL
348 png_handle_tRNS(png_ptr
, info_ptr
, png_ptr
->push_length
);
352 #ifdef PNG_READ_bKGD_SUPPORTED
353 else if (chunk_name
== png_bKGD
)
355 PNG_PUSH_SAVE_BUFFER_IF_FULL
356 png_handle_bKGD(png_ptr
, info_ptr
, png_ptr
->push_length
);
360 #ifdef PNG_READ_hIST_SUPPORTED
361 else if (chunk_name
== png_hIST
)
363 PNG_PUSH_SAVE_BUFFER_IF_FULL
364 png_handle_hIST(png_ptr
, info_ptr
, png_ptr
->push_length
);
368 #ifdef PNG_READ_pHYs_SUPPORTED
369 else if (chunk_name
== png_pHYs
)
371 PNG_PUSH_SAVE_BUFFER_IF_FULL
372 png_handle_pHYs(png_ptr
, info_ptr
, png_ptr
->push_length
);
376 #ifdef PNG_READ_oFFs_SUPPORTED
377 else if (chunk_name
== png_oFFs
)
379 PNG_PUSH_SAVE_BUFFER_IF_FULL
380 png_handle_oFFs(png_ptr
, info_ptr
, png_ptr
->push_length
);
384 #ifdef PNG_READ_pCAL_SUPPORTED
385 else if (chunk_name
== png_pCAL
)
387 PNG_PUSH_SAVE_BUFFER_IF_FULL
388 png_handle_pCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
392 #ifdef PNG_READ_sCAL_SUPPORTED
393 else if (chunk_name
== png_sCAL
)
395 PNG_PUSH_SAVE_BUFFER_IF_FULL
396 png_handle_sCAL(png_ptr
, info_ptr
, png_ptr
->push_length
);
400 #ifdef PNG_READ_tIME_SUPPORTED
401 else if (chunk_name
== png_tIME
)
403 PNG_PUSH_SAVE_BUFFER_IF_FULL
404 png_handle_tIME(png_ptr
, info_ptr
, png_ptr
->push_length
);
408 #ifdef PNG_READ_tEXt_SUPPORTED
409 else if (chunk_name
== png_tEXt
)
411 PNG_PUSH_SAVE_BUFFER_IF_FULL
412 png_handle_tEXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
416 #ifdef PNG_READ_zTXt_SUPPORTED
417 else if (chunk_name
== png_zTXt
)
419 PNG_PUSH_SAVE_BUFFER_IF_FULL
420 png_handle_zTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
424 #ifdef PNG_READ_iTXt_SUPPORTED
425 else if (chunk_name
== png_iTXt
)
427 PNG_PUSH_SAVE_BUFFER_IF_FULL
428 png_handle_iTXt(png_ptr
, info_ptr
, png_ptr
->push_length
);
434 PNG_PUSH_SAVE_BUFFER_IF_FULL
435 png_handle_unknown(png_ptr
, info_ptr
, png_ptr
->push_length
,
436 PNG_HANDLE_CHUNK_AS_DEFAULT
);
439 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
443 png_push_crc_skip(png_structrp png_ptr
, png_uint_32 skip
)
445 png_ptr
->process_mode
= PNG_SKIP_MODE
;
446 png_ptr
->skip_length
= skip
;
450 png_push_crc_finish(png_structrp png_ptr
)
452 if (png_ptr
->skip_length
!= 0 && png_ptr
->save_buffer_size
!= 0)
454 png_size_t save_size
= png_ptr
->save_buffer_size
;
455 png_uint_32 skip_length
= png_ptr
->skip_length
;
457 /* We want the smaller of 'skip_length' and 'save_buffer_size', but
458 * they are of different types and we don't know which variable has the
459 * fewest bits. Carefully select the smaller and cast it to the type of
460 * the larger - this cannot overflow. Do not cast in the following test
461 * - it will break on either 16 or 64 bit platforms.
463 if (skip_length
< save_size
)
464 save_size
= (png_size_t
)skip_length
;
467 skip_length
= (png_uint_32
)save_size
;
469 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
471 png_ptr
->skip_length
-= skip_length
;
472 png_ptr
->buffer_size
-= save_size
;
473 png_ptr
->save_buffer_size
-= save_size
;
474 png_ptr
->save_buffer_ptr
+= save_size
;
476 if (png_ptr
->skip_length
!= 0 && png_ptr
->current_buffer_size
!= 0)
478 png_size_t save_size
= png_ptr
->current_buffer_size
;
479 png_uint_32 skip_length
= png_ptr
->skip_length
;
481 /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
482 * the same problem exists as above and the same solution.
484 if (skip_length
< save_size
)
485 save_size
= (png_size_t
)skip_length
;
488 skip_length
= (png_uint_32
)save_size
;
490 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
492 png_ptr
->skip_length
-= skip_length
;
493 png_ptr
->buffer_size
-= save_size
;
494 png_ptr
->current_buffer_size
-= save_size
;
495 png_ptr
->current_buffer_ptr
+= save_size
;
497 if (png_ptr
->skip_length
== 0)
499 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
500 png_crc_finish(png_ptr
, 0);
501 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
506 png_push_fill_buffer(png_structp png_ptr
, png_bytep buffer
, png_size_t length
)
514 if (png_ptr
->save_buffer_size
!= 0)
516 png_size_t save_size
;
518 if (length
< png_ptr
->save_buffer_size
)
522 save_size
= png_ptr
->save_buffer_size
;
524 memcpy(ptr
, png_ptr
->save_buffer_ptr
, save_size
);
527 png_ptr
->buffer_size
-= save_size
;
528 png_ptr
->save_buffer_size
-= save_size
;
529 png_ptr
->save_buffer_ptr
+= save_size
;
531 if (length
!= 0 && png_ptr
->current_buffer_size
!= 0)
533 png_size_t save_size
;
535 if (length
< png_ptr
->current_buffer_size
)
539 save_size
= png_ptr
->current_buffer_size
;
541 memcpy(ptr
, png_ptr
->current_buffer_ptr
, save_size
);
542 png_ptr
->buffer_size
-= save_size
;
543 png_ptr
->current_buffer_size
-= save_size
;
544 png_ptr
->current_buffer_ptr
+= save_size
;
549 png_push_save_buffer(png_structrp png_ptr
)
551 if (png_ptr
->save_buffer_size
!= 0)
553 if (png_ptr
->save_buffer_ptr
!= png_ptr
->save_buffer
)
559 istop
= png_ptr
->save_buffer_size
;
560 for (i
= 0, sp
= png_ptr
->save_buffer_ptr
, dp
= png_ptr
->save_buffer
;
561 i
< istop
; i
++, sp
++, dp
++)
567 if (png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
>
568 png_ptr
->save_buffer_max
)
571 png_bytep old_buffer
;
573 if (png_ptr
->save_buffer_size
> PNG_SIZE_MAX
-
574 (png_ptr
->current_buffer_size
+ 256))
576 png_error(png_ptr
, "Potential overflow of save_buffer");
579 new_max
= png_ptr
->save_buffer_size
+ png_ptr
->current_buffer_size
+ 256;
580 old_buffer
= png_ptr
->save_buffer
;
581 png_ptr
->save_buffer
= (png_bytep
)png_malloc_warn(png_ptr
,
582 (png_size_t
)new_max
);
584 if (png_ptr
->save_buffer
== NULL
)
586 png_free(png_ptr
, old_buffer
);
588 png_error(png_ptr
, "Insufficient memory for save_buffer");
591 memcpy(png_ptr
->save_buffer
, old_buffer
, png_ptr
->save_buffer_size
);
592 png_free(png_ptr
, old_buffer
);
594 png_ptr
->save_buffer_max
= new_max
;
596 if (png_ptr
->current_buffer_size
)
598 memcpy(png_ptr
->save_buffer
+ png_ptr
->save_buffer_size
,
599 png_ptr
->current_buffer_ptr
, png_ptr
->current_buffer_size
);
600 png_ptr
->save_buffer_size
+= png_ptr
->current_buffer_size
;
601 png_ptr
->current_buffer_size
= 0;
603 png_ptr
->save_buffer_ptr
= png_ptr
->save_buffer
;
604 png_ptr
->buffer_size
= 0;
608 png_push_restore_buffer(png_structrp png_ptr
, png_bytep buffer
,
609 png_size_t buffer_length
)
611 png_ptr
->current_buffer
= buffer
;
612 png_ptr
->current_buffer_size
= buffer_length
;
613 png_ptr
->buffer_size
= buffer_length
+ png_ptr
->save_buffer_size
;
614 png_ptr
->current_buffer_ptr
= png_ptr
->current_buffer
;
618 png_push_read_IDAT(png_structrp png_ptr
)
620 if ((png_ptr
->mode
& PNG_HAVE_CHUNK_HEADER
) == 0)
622 png_byte chunk_length
[4];
623 png_byte chunk_tag
[4];
625 /* TODO: this code can be commoned up with the same code in push_read */
626 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
627 png_push_fill_buffer(png_ptr
, chunk_length
, 4);
628 png_ptr
->push_length
= png_get_uint_31(png_ptr
, chunk_length
);
629 png_reset_crc(png_ptr
);
630 png_crc_read(png_ptr
, chunk_tag
, 4);
631 png_ptr
->chunk_name
= PNG_CHUNK_FROM_STRING(chunk_tag
);
632 png_ptr
->mode
|= PNG_HAVE_CHUNK_HEADER
;
634 if (png_ptr
->chunk_name
!= png_IDAT
)
636 png_ptr
->process_mode
= PNG_READ_CHUNK_MODE
;
638 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
639 png_error(png_ptr
, "Not enough compressed data");
644 png_ptr
->idat_size
= png_ptr
->push_length
;
647 if (png_ptr
->idat_size
!= 0 && png_ptr
->save_buffer_size
!= 0)
649 png_size_t save_size
= png_ptr
->save_buffer_size
;
650 png_uint_32 idat_size
= png_ptr
->idat_size
;
652 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
653 * are of different types and we don't know which variable has the fewest
654 * bits. Carefully select the smaller and cast it to the type of the
655 * larger - this cannot overflow. Do not cast in the following test - it
656 * will break on either 16 or 64 bit platforms.
658 if (idat_size
< save_size
)
659 save_size
= (png_size_t
)idat_size
;
662 idat_size
= (png_uint_32
)save_size
;
664 png_calculate_crc(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
666 png_process_IDAT_data(png_ptr
, png_ptr
->save_buffer_ptr
, save_size
);
668 png_ptr
->idat_size
-= idat_size
;
669 png_ptr
->buffer_size
-= save_size
;
670 png_ptr
->save_buffer_size
-= save_size
;
671 png_ptr
->save_buffer_ptr
+= save_size
;
674 if (png_ptr
->idat_size
!= 0 && png_ptr
->current_buffer_size
!= 0)
676 png_size_t save_size
= png_ptr
->current_buffer_size
;
677 png_uint_32 idat_size
= png_ptr
->idat_size
;
679 /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
680 * are of different types and we don't know which variable has the fewest
681 * bits. Carefully select the smaller and cast it to the type of the
682 * larger - this cannot overflow.
684 if (idat_size
< save_size
)
685 save_size
= (png_size_t
)idat_size
;
688 idat_size
= (png_uint_32
)save_size
;
690 png_calculate_crc(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
692 png_process_IDAT_data(png_ptr
, png_ptr
->current_buffer_ptr
, save_size
);
694 png_ptr
->idat_size
-= idat_size
;
695 png_ptr
->buffer_size
-= save_size
;
696 png_ptr
->current_buffer_size
-= save_size
;
697 png_ptr
->current_buffer_ptr
+= save_size
;
699 if (png_ptr
->idat_size
== 0)
701 PNG_PUSH_SAVE_BUFFER_IF_LT(4)
702 png_crc_finish(png_ptr
, 0);
703 png_ptr
->mode
&= ~PNG_HAVE_CHUNK_HEADER
;
704 png_ptr
->mode
|= PNG_AFTER_IDAT
;
710 png_process_IDAT_data(png_structrp png_ptr
, png_bytep buffer
,
711 png_size_t buffer_length
)
713 /* The caller checks for a non-zero buffer length. */
714 if (!(buffer_length
> 0) || buffer
== NULL
)
715 png_error(png_ptr
, "No IDAT data (internal error)");
717 /* This routine must process all the data it has been given
718 * before returning, calling the row callback as required to
719 * handle the uncompressed results.
721 png_ptr
->zstream
.next_in
= buffer
;
722 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
723 png_ptr
->zstream
.avail_in
= (uInt
)buffer_length
;
725 /* Keep going until the decompressed data is all processed
726 * or the stream marked as finished.
728 while (png_ptr
->zstream
.avail_in
> 0 &&
729 (png_ptr
->flags
& PNG_FLAG_ZSTREAM_ENDED
) == 0)
733 /* We have data for zlib, but we must check that zlib
734 * has someplace to put the results. It doesn't matter
735 * if we don't expect any results -- it may be the input
736 * data is just the LZ end code.
738 if (!(png_ptr
->zstream
.avail_out
> 0))
740 /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
741 png_ptr
->zstream
.avail_out
= (uInt
)(PNG_ROWBYTES(png_ptr
->pixel_depth
,
742 png_ptr
->iwidth
) + 1);
744 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
747 /* Using Z_SYNC_FLUSH here means that an unterminated
748 * LZ stream (a stream with a missing end code) can still
749 * be handled, otherwise (Z_NO_FLUSH) a future zlib
750 * implementation might defer output and therefore
751 * change the current behavior (see comments in inflate.c
752 * for why this doesn't happen at present with zlib 1.2.5).
754 ret
= inflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
756 /* Check for any failure before proceeding. */
757 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
759 /* Terminate the decompression. */
760 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
763 /* This may be a truncated stream (missing or
764 * damaged end code). Treat that as a warning.
766 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
768 png_warning(png_ptr
, "Truncated compressed data in IDAT");
771 png_error(png_ptr
, "Decompression error in IDAT");
773 /* Skip the check on unprocessed input */
777 /* Did inflate output any data? */
778 if (png_ptr
->zstream
.next_out
!= png_ptr
->row_buf
)
780 /* Is this unexpected data after the last row?
781 * If it is, artificially terminate the LZ output
784 if (png_ptr
->row_number
>= png_ptr
->num_rows
||
788 png_warning(png_ptr
, "Extra compressed data in IDAT");
789 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
792 /* Do no more processing; skip the unprocessed
798 /* Do we have a complete row? */
799 if (png_ptr
->zstream
.avail_out
== 0)
800 png_push_process_row(png_ptr
);
803 /* And check for the end of the stream. */
804 if (ret
== Z_STREAM_END
)
805 png_ptr
->flags
|= PNG_FLAG_ZSTREAM_ENDED
;
808 /* All the data should have been processed, if anything
809 * is left at this point we have bytes of IDAT data
810 * after the zlib end code.
812 if (png_ptr
->zstream
.avail_in
> 0)
813 png_warning(png_ptr
, "Extra compression data in IDAT");
817 png_push_process_row(png_structrp png_ptr
)
819 /* 1.5.6: row_info moved out of png_struct to a local here. */
820 png_row_info row_info
;
822 row_info
.width
= png_ptr
->iwidth
; /* NOTE: width of current interlaced row */
823 row_info
.color_type
= png_ptr
->color_type
;
824 row_info
.bit_depth
= png_ptr
->bit_depth
;
825 row_info
.channels
= png_ptr
->channels
;
826 row_info
.pixel_depth
= png_ptr
->pixel_depth
;
827 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
829 if (png_ptr
->row_buf
[0] > PNG_FILTER_VALUE_NONE
)
831 if (png_ptr
->row_buf
[0] < PNG_FILTER_VALUE_LAST
)
832 png_read_filter_row(png_ptr
, &row_info
, png_ptr
->row_buf
+ 1,
833 png_ptr
->prev_row
+ 1, png_ptr
->row_buf
[0]);
835 png_error(png_ptr
, "bad adaptive filter value");
838 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
839 * 1.5.6, while the buffer really is this big in current versions of libpng
840 * it may not be in the future, so this was changed just to copy the
841 * interlaced row count:
843 memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, row_info
.rowbytes
+ 1);
845 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
846 if (png_ptr
->transformations
!= 0)
847 png_do_read_transformations(png_ptr
, &row_info
);
850 /* The transformed pixel depth should match the depth now in row_info. */
851 if (png_ptr
->transformed_pixel_depth
== 0)
853 png_ptr
->transformed_pixel_depth
= row_info
.pixel_depth
;
854 if (row_info
.pixel_depth
> png_ptr
->maximum_pixel_depth
)
855 png_error(png_ptr
, "progressive row overflow");
858 else if (png_ptr
->transformed_pixel_depth
!= row_info
.pixel_depth
)
859 png_error(png_ptr
, "internal progressive row size calculation error");
862 #ifdef PNG_READ_INTERLACING_SUPPORTED
863 /* Expand interlaced rows to full size */
864 if (png_ptr
->interlaced
!= 0 &&
865 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
867 if (png_ptr
->pass
< 6)
868 png_do_read_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
,
869 png_ptr
->transformations
);
871 switch (png_ptr
->pass
)
876 for (i
= 0; i
< 8 && png_ptr
->pass
== 0; i
++)
878 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
879 png_read_push_finish_row(png_ptr
); /* Updates png_ptr->pass */
882 if (png_ptr
->pass
== 2) /* Pass 1 might be empty */
884 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
886 png_push_have_row(png_ptr
, NULL
);
887 png_read_push_finish_row(png_ptr
);
891 if (png_ptr
->pass
== 4 && png_ptr
->height
<= 4)
893 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
895 png_push_have_row(png_ptr
, NULL
);
896 png_read_push_finish_row(png_ptr
);
900 if (png_ptr
->pass
== 6 && png_ptr
->height
<= 4)
902 png_push_have_row(png_ptr
, NULL
);
903 png_read_push_finish_row(png_ptr
);
912 for (i
= 0; i
< 8 && png_ptr
->pass
== 1; i
++)
914 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
915 png_read_push_finish_row(png_ptr
);
918 if (png_ptr
->pass
== 2) /* Skip top 4 generated rows */
920 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
922 png_push_have_row(png_ptr
, NULL
);
923 png_read_push_finish_row(png_ptr
);
934 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
936 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
937 png_read_push_finish_row(png_ptr
);
940 for (i
= 0; i
< 4 && png_ptr
->pass
== 2; i
++)
942 png_push_have_row(png_ptr
, NULL
);
943 png_read_push_finish_row(png_ptr
);
946 if (png_ptr
->pass
== 4) /* Pass 3 might be empty */
948 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
950 png_push_have_row(png_ptr
, NULL
);
951 png_read_push_finish_row(png_ptr
);
962 for (i
= 0; i
< 4 && png_ptr
->pass
== 3; i
++)
964 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
965 png_read_push_finish_row(png_ptr
);
968 if (png_ptr
->pass
== 4) /* Skip top two generated rows */
970 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
972 png_push_have_row(png_ptr
, NULL
);
973 png_read_push_finish_row(png_ptr
);
984 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
986 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
987 png_read_push_finish_row(png_ptr
);
990 for (i
= 0; i
< 2 && png_ptr
->pass
== 4; i
++)
992 png_push_have_row(png_ptr
, NULL
);
993 png_read_push_finish_row(png_ptr
);
996 if (png_ptr
->pass
== 6) /* Pass 5 might be empty */
998 png_push_have_row(png_ptr
, NULL
);
999 png_read_push_finish_row(png_ptr
);
1009 for (i
= 0; i
< 2 && png_ptr
->pass
== 5; i
++)
1011 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1012 png_read_push_finish_row(png_ptr
);
1015 if (png_ptr
->pass
== 6) /* Skip top generated row */
1017 png_push_have_row(png_ptr
, NULL
);
1018 png_read_push_finish_row(png_ptr
);
1027 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1028 png_read_push_finish_row(png_ptr
);
1030 if (png_ptr
->pass
!= 6)
1033 png_push_have_row(png_ptr
, NULL
);
1034 png_read_push_finish_row(png_ptr
);
1041 png_push_have_row(png_ptr
, png_ptr
->row_buf
+ 1);
1042 png_read_push_finish_row(png_ptr
);
1047 png_read_push_finish_row(png_structrp png_ptr
)
1049 #ifdef PNG_READ_INTERLACING_SUPPORTED
1050 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1052 /* Start of interlace block */
1053 static PNG_CONST png_byte png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
1055 /* Offset to next interlace block */
1056 static PNG_CONST png_byte png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
1058 /* Start of interlace block in the y direction */
1059 static PNG_CONST png_byte png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
1061 /* Offset to next interlace block in the y direction */
1062 static PNG_CONST png_byte png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
1064 /* Height of interlace block. This is not currently used - if you need
1065 * it, uncomment it here and in png.h
1066 static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1070 png_ptr
->row_number
++;
1071 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1074 #ifdef PNG_READ_INTERLACING_SUPPORTED
1075 if (png_ptr
->interlaced
!= 0)
1077 png_ptr
->row_number
= 0;
1078 memset(png_ptr
->prev_row
, 0, png_ptr
->rowbytes
+ 1);
1083 if ((png_ptr
->pass
== 1 && png_ptr
->width
< 5) ||
1084 (png_ptr
->pass
== 3 && png_ptr
->width
< 3) ||
1085 (png_ptr
->pass
== 5 && png_ptr
->width
< 2))
1088 if (png_ptr
->pass
> 7)
1091 if (png_ptr
->pass
>= 7)
1094 png_ptr
->iwidth
= (png_ptr
->width
+
1095 png_pass_inc
[png_ptr
->pass
] - 1 -
1096 png_pass_start
[png_ptr
->pass
]) /
1097 png_pass_inc
[png_ptr
->pass
];
1099 if ((png_ptr
->transformations
& PNG_INTERLACE
) != 0)
1102 png_ptr
->num_rows
= (png_ptr
->height
+
1103 png_pass_yinc
[png_ptr
->pass
] - 1 -
1104 png_pass_ystart
[png_ptr
->pass
]) /
1105 png_pass_yinc
[png_ptr
->pass
];
1107 } while (png_ptr
->iwidth
== 0 || png_ptr
->num_rows
== 0);
1109 #endif /* READ_INTERLACING */
1113 png_push_have_info(png_structrp png_ptr
, png_inforp info_ptr
)
1115 if (png_ptr
->info_fn
!= NULL
)
1116 (*(png_ptr
->info_fn
))(png_ptr
, info_ptr
);
1120 png_push_have_end(png_structrp png_ptr
, png_inforp info_ptr
)
1122 if (png_ptr
->end_fn
!= NULL
)
1123 (*(png_ptr
->end_fn
))(png_ptr
, info_ptr
);
1127 png_push_have_row(png_structrp png_ptr
, png_bytep row
)
1129 if (png_ptr
->row_fn
!= NULL
)
1130 (*(png_ptr
->row_fn
))(png_ptr
, row
, png_ptr
->row_number
,
1131 (int)png_ptr
->pass
);
1134 #ifdef PNG_READ_INTERLACING_SUPPORTED
1136 png_progressive_combine_row(png_const_structrp png_ptr
, png_bytep old_row
,
1137 png_const_bytep new_row
)
1139 if (png_ptr
== NULL
)
1142 /* new_row is a flag here - if it is NULL then the app callback was called
1143 * from an empty row (see the calls to png_struct::row_fn below), otherwise
1144 * it must be png_ptr->row_buf+1
1146 if (new_row
!= NULL
)
1147 png_combine_row(png_ptr
, old_row
, 1/*blocky display*/);
1149 #endif /* READ_INTERLACING */
1152 png_set_progressive_read_fn(png_structrp png_ptr
, png_voidp progressive_ptr
,
1153 png_progressive_info_ptr info_fn
, png_progressive_row_ptr row_fn
,
1154 png_progressive_end_ptr end_fn
)
1156 if (png_ptr
== NULL
)
1159 png_ptr
->info_fn
= info_fn
;
1160 png_ptr
->row_fn
= row_fn
;
1161 png_ptr
->end_fn
= end_fn
;
1163 png_set_read_fn(png_ptr
, progressive_ptr
, png_push_fill_buffer
);
1167 png_get_progressive_ptr(png_const_structrp png_ptr
)
1169 if (png_ptr
== NULL
)
1172 return png_ptr
->io_ptr
;
1174 #endif /* PROGRESSIVE_READ */