2 /* pngpread.c - read a png file in push mode
4 * libpng 1.2.5 - October 3, 2002
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
14 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
16 /* push model modes */
17 #define PNG_READ_SIG_MODE 0
18 #define PNG_READ_CHUNK_MODE 1
19 #define PNG_READ_IDAT_MODE 2
20 #define PNG_SKIP_MODE 3
21 #define PNG_READ_tEXt_MODE 4
22 #define PNG_READ_zTXt_MODE 5
23 #define PNG_READ_DONE_MODE 6
24 #define PNG_READ_iTXt_MODE 7
25 #define PNG_ERROR_MODE 8
28 png_process_data(png_structp png_ptr, png_infop info_ptr,
29 png_bytep buffer, png_size_t buffer_size)
31 png_push_restore_buffer(png_ptr, buffer, buffer_size);
33 while (png_ptr->buffer_size)
35 png_process_some_data(png_ptr, info_ptr);
39 /* What we do with the incoming data depends on what we were previously
40 * doing before we ran out of data...
43 png_process_some_data(png_structp png_ptr, png_infop info_ptr)
45 switch (png_ptr->process_mode)
47 case PNG_READ_SIG_MODE:
49 png_push_read_sig(png_ptr, info_ptr);
52 case PNG_READ_CHUNK_MODE:
54 png_push_read_chunk(png_ptr, info_ptr);
57 case PNG_READ_IDAT_MODE:
59 png_push_read_IDAT(png_ptr);
62 #if defined(PNG_READ_tEXt_SUPPORTED)
63 case PNG_READ_tEXt_MODE:
65 png_push_read_tEXt(png_ptr, info_ptr);
69 #if defined(PNG_READ_zTXt_SUPPORTED)
70 case PNG_READ_zTXt_MODE:
72 png_push_read_zTXt(png_ptr, info_ptr);
76 #if defined(PNG_READ_iTXt_SUPPORTED)
77 case PNG_READ_iTXt_MODE:
79 png_push_read_iTXt(png_ptr, info_ptr);
85 png_push_crc_finish(png_ptr);
90 png_ptr->buffer_size = 0;
96 /* Read any remaining signature bytes from the stream and compare them with
97 * the correct PNG signature. It is possible that this routine is called
98 * with bytes already read from the signature, either because they have been
99 * checked by the calling application, or because of multiple calls to this
103 png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
105 png_size_t num_checked = png_ptr->sig_bytes,
106 num_to_check = 8 - num_checked;
108 if (png_ptr->buffer_size < num_to_check)
110 num_to_check = png_ptr->buffer_size;
113 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
115 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check);
117 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
119 if (num_checked < 4 &&
120 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
121 png_error(png_ptr, "Not a PNG file");
123 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
127 if (png_ptr->sig_bytes >= 8)
129 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
135 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
137 #ifdef PNG_USE_LOCAL_ARRAYS
142 #if defined(PNG_READ_bKGD_SUPPORTED)
145 #if defined(PNG_READ_cHRM_SUPPORTED)
148 #if defined(PNG_READ_gAMA_SUPPORTED)
151 #if defined(PNG_READ_hIST_SUPPORTED)
154 #if defined(PNG_READ_iCCP_SUPPORTED)
157 #if defined(PNG_READ_iTXt_SUPPORTED)
160 #if defined(PNG_READ_oFFs_SUPPORTED)
163 #if defined(PNG_READ_pCAL_SUPPORTED)
166 #if defined(PNG_READ_pHYs_SUPPORTED)
169 #if defined(PNG_READ_sBIT_SUPPORTED)
172 #if defined(PNG_READ_sCAL_SUPPORTED)
175 #if defined(PNG_READ_sRGB_SUPPORTED)
178 #if defined(PNG_READ_sPLT_SUPPORTED)
181 #if defined(PNG_READ_tEXt_SUPPORTED)
184 #if defined(PNG_READ_tIME_SUPPORTED)
187 #if defined(PNG_READ_tRNS_SUPPORTED)
190 #if defined(PNG_READ_zTXt_SUPPORTED)
193 #endif /* PNG_USE_LOCAL_ARRAYS */
194 /* First we make sure we have enough data for the 4 byte chunk name
195 * and the 4 byte chunk length before proceeding with decoding the
196 * chunk data. To fully decode each of these chunks, we also make
197 * sure we have enough data in the buffer for the 4 byte CRC at the
198 * end of every chunk (except IDAT, which is handled separately).
200 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
202 png_byte chunk_length[4];
204 if (png_ptr->buffer_size < 8)
206 png_push_save_buffer(png_ptr);
210 png_push_fill_buffer(png_ptr, chunk_length, 4);
211 png_ptr->push_length = png_get_uint_32(chunk_length);
212 png_reset_crc(png_ptr);
213 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
214 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
217 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
219 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
221 png_push_save_buffer(png_ptr);
224 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
226 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
228 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
230 png_push_save_buffer(png_ptr);
233 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
235 else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
237 /* If we reach an IDAT chunk, this means we have read all of the
238 * header chunks, and we can start reading the image (or if this
239 * is called after the image has been read - we have an error).
241 if (!(png_ptr->mode & PNG_HAVE_IHDR))
242 png_error(png_ptr, "Missing IHDR before IDAT");
243 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
244 !(png_ptr->mode & PNG_HAVE_PLTE))
245 png_error(png_ptr, "Missing PLTE before IDAT");
247 if (png_ptr->mode & PNG_HAVE_IDAT)
249 if (png_ptr->push_length == 0)
252 if (png_ptr->mode & PNG_AFTER_IDAT)
253 png_error(png_ptr, "Too many IDAT's found");
256 png_ptr->idat_size = png_ptr->push_length;
257 png_ptr->mode |= PNG_HAVE_IDAT;
258 png_ptr->process_mode = PNG_READ_IDAT_MODE;
259 png_push_have_info(png_ptr, info_ptr);
260 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
261 png_ptr->zstream.next_out = png_ptr->row_buf;
264 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
266 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
268 png_push_save_buffer(png_ptr);
271 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
273 png_ptr->process_mode = PNG_READ_DONE_MODE;
274 png_push_have_end(png_ptr, info_ptr);
276 #if defined(PNG_READ_gAMA_SUPPORTED)
277 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
279 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
281 png_push_save_buffer(png_ptr);
284 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
287 #if defined(PNG_READ_sBIT_SUPPORTED)
288 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
290 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
292 png_push_save_buffer(png_ptr);
295 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
298 #if defined(PNG_READ_cHRM_SUPPORTED)
299 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
301 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
303 png_push_save_buffer(png_ptr);
306 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
309 #if defined(PNG_READ_sRGB_SUPPORTED)
310 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
312 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
314 png_push_save_buffer(png_ptr);
317 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
320 #if defined(PNG_READ_iCCP_SUPPORTED)
321 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
323 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
325 png_push_save_buffer(png_ptr);
328 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
331 #if defined(PNG_READ_sPLT_SUPPORTED)
332 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
334 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
336 png_push_save_buffer(png_ptr);
339 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
342 #if defined(PNG_READ_tRNS_SUPPORTED)
343 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
345 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
347 png_push_save_buffer(png_ptr);
350 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
353 #if defined(PNG_READ_bKGD_SUPPORTED)
354 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
356 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
358 png_push_save_buffer(png_ptr);
361 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
364 #if defined(PNG_READ_hIST_SUPPORTED)
365 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
367 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
369 png_push_save_buffer(png_ptr);
372 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
375 #if defined(PNG_READ_pHYs_SUPPORTED)
376 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
378 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
380 png_push_save_buffer(png_ptr);
383 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
386 #if defined(PNG_READ_oFFs_SUPPORTED)
387 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
389 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
391 png_push_save_buffer(png_ptr);
394 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
397 #if defined(PNG_READ_pCAL_SUPPORTED)
398 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
400 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
402 png_push_save_buffer(png_ptr);
405 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
408 #if defined(PNG_READ_sCAL_SUPPORTED)
409 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
411 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
413 png_push_save_buffer(png_ptr);
416 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
419 #if defined(PNG_READ_tIME_SUPPORTED)
420 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
422 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
424 png_push_save_buffer(png_ptr);
427 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
430 #if defined(PNG_READ_tEXt_SUPPORTED)
431 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
433 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
435 png_push_save_buffer(png_ptr);
438 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
441 #if defined(PNG_READ_zTXt_SUPPORTED)
442 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
444 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
446 png_push_save_buffer(png_ptr);
449 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
452 #if defined(PNG_READ_iTXt_SUPPORTED)
453 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
455 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
457 png_push_save_buffer(png_ptr);
460 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
465 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
467 png_push_save_buffer(png_ptr);
470 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
473 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
477 png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
479 png_ptr->process_mode = PNG_SKIP_MODE;
480 png_ptr->skip_length = skip;
484 png_push_crc_finish(png_structp png_ptr)
486 if (png_ptr->skip_length && png_ptr->save_buffer_size)
488 png_size_t save_size;
490 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
491 save_size = (png_size_t)png_ptr->skip_length;
493 save_size = png_ptr->save_buffer_size;
495 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
497 png_ptr->skip_length -= save_size;
498 png_ptr->buffer_size -= save_size;
499 png_ptr->save_buffer_size -= save_size;
500 png_ptr->save_buffer_ptr += save_size;
502 if (png_ptr->skip_length && png_ptr->current_buffer_size)
504 png_size_t save_size;
506 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
507 save_size = (png_size_t)png_ptr->skip_length;
509 save_size = png_ptr->current_buffer_size;
511 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
513 png_ptr->skip_length -= save_size;
514 png_ptr->buffer_size -= save_size;
515 png_ptr->current_buffer_size -= save_size;
516 png_ptr->current_buffer_ptr += save_size;
518 if (!png_ptr->skip_length)
520 if (png_ptr->buffer_size < 4)
522 png_push_save_buffer(png_ptr);
526 png_crc_finish(png_ptr, 0);
527 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
532 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
537 if (png_ptr->save_buffer_size)
539 png_size_t save_size;
541 if (length < png_ptr->save_buffer_size)
544 save_size = png_ptr->save_buffer_size;
546 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
549 png_ptr->buffer_size -= save_size;
550 png_ptr->save_buffer_size -= save_size;
551 png_ptr->save_buffer_ptr += save_size;
553 if (length && png_ptr->current_buffer_size)
555 png_size_t save_size;
557 if (length < png_ptr->current_buffer_size)
560 save_size = png_ptr->current_buffer_size;
562 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
563 png_ptr->buffer_size -= save_size;
564 png_ptr->current_buffer_size -= save_size;
565 png_ptr->current_buffer_ptr += save_size;
570 png_push_save_buffer(png_structp png_ptr)
572 if (png_ptr->save_buffer_size)
574 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
580 istop = png_ptr->save_buffer_size;
581 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
582 i < istop; i++, sp++, dp++)
588 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
589 png_ptr->save_buffer_max)
592 png_bytep old_buffer;
594 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
595 old_buffer = png_ptr->save_buffer;
596 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
597 (png_uint_32)new_max);
598 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
599 png_free(png_ptr, old_buffer);
600 png_ptr->save_buffer_max = new_max;
602 if (png_ptr->current_buffer_size)
604 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
605 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
606 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
607 png_ptr->current_buffer_size = 0;
609 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
610 png_ptr->buffer_size = 0;
614 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
615 png_size_t buffer_length)
617 png_ptr->current_buffer = buffer;
618 png_ptr->current_buffer_size = buffer_length;
619 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
620 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
624 png_push_read_IDAT(png_structp png_ptr)
626 #ifdef PNG_USE_LOCAL_ARRAYS
629 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
631 png_byte chunk_length[4];
633 if (png_ptr->buffer_size < 8)
635 png_push_save_buffer(png_ptr);
639 png_push_fill_buffer(png_ptr, chunk_length, 4);
640 png_ptr->push_length = png_get_uint_32(chunk_length);
642 png_reset_crc(png_ptr);
643 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
644 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
646 if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
648 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
649 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
650 png_error(png_ptr, "Not enough compressed data");
654 png_ptr->idat_size = png_ptr->push_length;
656 if (png_ptr->idat_size && png_ptr->save_buffer_size)
658 png_size_t save_size;
660 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
662 save_size = (png_size_t)png_ptr->idat_size;
663 /* check for overflow */
664 if((png_uint_32)save_size != png_ptr->idat_size)
665 png_error(png_ptr, "save_size overflowed in pngpread");
668 save_size = png_ptr->save_buffer_size;
670 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
671 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
672 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
673 png_ptr->idat_size -= save_size;
674 png_ptr->buffer_size -= save_size;
675 png_ptr->save_buffer_size -= save_size;
676 png_ptr->save_buffer_ptr += save_size;
678 if (png_ptr->idat_size && png_ptr->current_buffer_size)
680 png_size_t save_size;
682 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
684 save_size = (png_size_t)png_ptr->idat_size;
685 /* check for overflow */
686 if((png_uint_32)save_size != png_ptr->idat_size)
687 png_error(png_ptr, "save_size overflowed in pngpread");
690 save_size = png_ptr->current_buffer_size;
692 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
693 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
694 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
696 png_ptr->idat_size -= save_size;
697 png_ptr->buffer_size -= save_size;
698 png_ptr->current_buffer_size -= save_size;
699 png_ptr->current_buffer_ptr += save_size;
701 if (!png_ptr->idat_size)
703 if (png_ptr->buffer_size < 4)
705 png_push_save_buffer(png_ptr);
709 png_crc_finish(png_ptr, 0);
710 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
711 png_ptr->mode |= PNG_AFTER_IDAT;
716 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
717 png_size_t buffer_length)
721 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
722 png_error(png_ptr, "Extra compression data");
724 png_ptr->zstream.next_in = buffer;
725 png_ptr->zstream.avail_in = (uInt)buffer_length;
728 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
731 if (ret == Z_STREAM_END)
733 if (png_ptr->zstream.avail_in)
734 png_error(png_ptr, "Extra compressed data");
735 if (!(png_ptr->zstream.avail_out))
737 png_push_process_row(png_ptr);
740 png_ptr->mode |= PNG_AFTER_IDAT;
741 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
744 else if (ret == Z_BUF_ERROR)
747 png_error(png_ptr, "Decompression Error");
749 if (!(png_ptr->zstream.avail_out))
752 #if defined(PNG_READ_INTERLACING_SUPPORTED)
753 png_ptr->interlaced && png_ptr->pass > 6) ||
754 (!png_ptr->interlaced &&
756 png_ptr->row_number == png_ptr->num_rows-1))
758 if (png_ptr->zstream.avail_in)
759 png_warning(png_ptr, "Too much data in IDAT chunks");
760 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
763 png_push_process_row(png_ptr);
764 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
765 png_ptr->zstream.next_out = png_ptr->row_buf;
773 png_push_process_row(png_structp png_ptr)
775 png_ptr->row_info.color_type = png_ptr->color_type;
776 png_ptr->row_info.width = png_ptr->iwidth;
777 png_ptr->row_info.channels = png_ptr->channels;
778 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
779 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
781 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
782 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
784 png_read_filter_row(png_ptr, &(png_ptr->row_info),
785 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
786 (int)(png_ptr->row_buf[0]));
788 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
789 png_ptr->rowbytes + 1);
791 if (png_ptr->transformations)
792 png_do_read_transformations(png_ptr);
794 #if defined(PNG_READ_INTERLACING_SUPPORTED)
795 /* blow up interlaced rows to full size */
796 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
798 if (png_ptr->pass < 6)
799 /* old interface (pre-1.0.9):
800 png_do_read_interlace(&(png_ptr->row_info),
801 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
803 png_do_read_interlace(png_ptr);
805 switch (png_ptr->pass)
810 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
812 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
813 png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
815 if (png_ptr->pass == 2) /* pass 1 might be empty */
817 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
819 png_push_have_row(png_ptr, png_bytep_NULL);
820 png_read_push_finish_row(png_ptr);
823 if (png_ptr->pass == 4 && png_ptr->height <= 4)
825 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
827 png_push_have_row(png_ptr, png_bytep_NULL);
828 png_read_push_finish_row(png_ptr);
831 if (png_ptr->pass == 6 && png_ptr->height <= 4)
833 png_push_have_row(png_ptr, png_bytep_NULL);
834 png_read_push_finish_row(png_ptr);
841 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
843 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
844 png_read_push_finish_row(png_ptr);
846 if (png_ptr->pass == 2) /* skip top 4 generated rows */
848 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
850 png_push_have_row(png_ptr, png_bytep_NULL);
851 png_read_push_finish_row(png_ptr);
859 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
861 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
862 png_read_push_finish_row(png_ptr);
864 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
866 png_push_have_row(png_ptr, png_bytep_NULL);
867 png_read_push_finish_row(png_ptr);
869 if (png_ptr->pass == 4) /* pass 3 might be empty */
871 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
873 png_push_have_row(png_ptr, png_bytep_NULL);
874 png_read_push_finish_row(png_ptr);
882 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
884 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
885 png_read_push_finish_row(png_ptr);
887 if (png_ptr->pass == 4) /* skip top two generated rows */
889 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
891 png_push_have_row(png_ptr, png_bytep_NULL);
892 png_read_push_finish_row(png_ptr);
900 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
902 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
903 png_read_push_finish_row(png_ptr);
905 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
907 png_push_have_row(png_ptr, png_bytep_NULL);
908 png_read_push_finish_row(png_ptr);
910 if (png_ptr->pass == 6) /* pass 5 might be empty */
912 png_push_have_row(png_ptr, png_bytep_NULL);
913 png_read_push_finish_row(png_ptr);
920 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
922 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
923 png_read_push_finish_row(png_ptr);
925 if (png_ptr->pass == 6) /* skip top generated row */
927 png_push_have_row(png_ptr, png_bytep_NULL);
928 png_read_push_finish_row(png_ptr);
934 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
935 png_read_push_finish_row(png_ptr);
936 if (png_ptr->pass != 6)
938 png_push_have_row(png_ptr, png_bytep_NULL);
939 png_read_push_finish_row(png_ptr);
946 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
947 png_read_push_finish_row(png_ptr);
952 png_read_push_finish_row(png_structp png_ptr)
954 #ifdef PNG_USE_LOCAL_ARRAYS
955 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
957 /* start of interlace block */
958 const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
960 /* offset to next interlace block */
961 const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
963 /* start of interlace block in the y direction */
964 const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
966 /* offset to next interlace block in the y direction */
967 const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
969 /* Width of interlace block. This is not currently used - if you need
970 * it, uncomment it here and in png.h
971 const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
974 /* Height of interlace block. This is not currently used - if you need
975 * it, uncomment it here and in png.h
976 const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
980 png_ptr->row_number++;
981 if (png_ptr->row_number < png_ptr->num_rows)
984 if (png_ptr->interlaced)
986 png_ptr->row_number = 0;
987 png_memset_check(png_ptr, png_ptr->prev_row, 0,
988 png_ptr->rowbytes + 1);
992 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
993 (png_ptr->pass == 3 && png_ptr->width < 3) ||
994 (png_ptr->pass == 5 && png_ptr->width < 2))
997 if (png_ptr->pass > 7)
999 if (png_ptr->pass >= 7)
1002 png_ptr->iwidth = (png_ptr->width +
1003 png_pass_inc[png_ptr->pass] - 1 -
1004 png_pass_start[png_ptr->pass]) /
1005 png_pass_inc[png_ptr->pass];
1007 png_ptr->irowbytes = ((png_ptr->iwidth *
1008 png_ptr->pixel_depth + 7) >> 3) + 1;
1010 if (png_ptr->transformations & PNG_INTERLACE)
1013 png_ptr->num_rows = (png_ptr->height +
1014 png_pass_yinc[png_ptr->pass] - 1 -
1015 png_pass_ystart[png_ptr->pass]) /
1016 png_pass_yinc[png_ptr->pass];
1018 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1022 #if defined(PNG_READ_tEXt_SUPPORTED)
1024 png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1027 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1029 png_error(png_ptr, "Out of place tEXt");
1030 /* to quiet some compiler warnings */
1031 if(info_ptr == NULL) return;
1034 #ifdef PNG_MAX_MALLOC_64K
1035 png_ptr->skip_length = 0; /* This may not be necessary */
1037 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
1039 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
1040 png_ptr->skip_length = length - (png_uint_32)65535L;
1041 length = (png_uint_32)65535L;
1045 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1046 (png_uint_32)(length+1));
1047 png_ptr->current_text[length] = '\0';
1048 png_ptr->current_text_ptr = png_ptr->current_text;
1049 png_ptr->current_text_size = (png_size_t)length;
1050 png_ptr->current_text_left = (png_size_t)length;
1051 png_ptr->process_mode = PNG_READ_tEXt_MODE;
1055 png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
1057 if (png_ptr->buffer_size && png_ptr->current_text_left)
1059 png_size_t text_size;
1061 if (png_ptr->buffer_size < png_ptr->current_text_left)
1062 text_size = png_ptr->buffer_size;
1064 text_size = png_ptr->current_text_left;
1065 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1066 png_ptr->current_text_left -= text_size;
1067 png_ptr->current_text_ptr += text_size;
1069 if (!(png_ptr->current_text_left))
1076 if (png_ptr->buffer_size < 4)
1078 png_push_save_buffer(png_ptr);
1082 png_push_crc_finish(png_ptr);
1084 #if defined(PNG_MAX_MALLOC_64K)
1085 if (png_ptr->skip_length)
1089 key = png_ptr->current_text;
1091 for (text = key; *text; text++)
1094 if (text != key + png_ptr->current_text_size)
1097 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
1098 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1099 text_ptr->key = key;
1100 #ifdef PNG_iTXt_SUPPORTED
1101 text_ptr->lang = NULL;
1102 text_ptr->lang_key = NULL;
1104 text_ptr->text = text;
1106 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1108 png_free(png_ptr, key);
1109 png_free(png_ptr, text_ptr);
1110 png_ptr->current_text = NULL;
1113 png_warning(png_ptr, "Insufficient memory to store text chunk.");
1118 #if defined(PNG_READ_zTXt_SUPPORTED)
1120 png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1123 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1125 png_error(png_ptr, "Out of place zTXt");
1126 /* to quiet some compiler warnings */
1127 if(info_ptr == NULL) return;
1130 #ifdef PNG_MAX_MALLOC_64K
1131 /* We can't handle zTXt chunks > 64K, since we don't have enough space
1132 * to be able to store the uncompressed data. Actually, the threshold
1133 * is probably around 32K, but it isn't as definite as 64K is.
1135 if (length > (png_uint_32)65535L)
1137 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1138 png_push_crc_skip(png_ptr, length);
1143 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1144 (png_uint_32)(length+1));
1145 png_ptr->current_text[length] = '\0';
1146 png_ptr->current_text_ptr = png_ptr->current_text;
1147 png_ptr->current_text_size = (png_size_t)length;
1148 png_ptr->current_text_left = (png_size_t)length;
1149 png_ptr->process_mode = PNG_READ_zTXt_MODE;
1153 png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
1155 if (png_ptr->buffer_size && png_ptr->current_text_left)
1157 png_size_t text_size;
1159 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
1160 text_size = png_ptr->buffer_size;
1162 text_size = png_ptr->current_text_left;
1163 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1164 png_ptr->current_text_left -= text_size;
1165 png_ptr->current_text_ptr += text_size;
1167 if (!(png_ptr->current_text_left))
1173 png_size_t text_size, key_size;
1175 if (png_ptr->buffer_size < 4)
1177 png_push_save_buffer(png_ptr);
1181 png_push_crc_finish(png_ptr);
1183 key = png_ptr->current_text;
1185 for (text = key; *text; text++)
1188 /* zTXt can't have zero text */
1189 if (text == key + png_ptr->current_text_size)
1191 png_ptr->current_text = NULL;
1192 png_free(png_ptr, key);
1198 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
1200 png_ptr->current_text = NULL;
1201 png_free(png_ptr, key);
1207 png_ptr->zstream.next_in = (png_bytep )text;
1208 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
1210 png_ptr->zstream.next_out = png_ptr->zbuf;
1211 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1213 key_size = text - key;
1218 while (png_ptr->zstream.avail_in)
1220 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
1221 if (ret != Z_OK && ret != Z_STREAM_END)
1223 inflateReset(&png_ptr->zstream);
1224 png_ptr->zstream.avail_in = 0;
1225 png_ptr->current_text = NULL;
1226 png_free(png_ptr, key);
1227 png_free(png_ptr, text);
1230 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
1234 text = (png_charp)png_malloc(png_ptr,
1235 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1237 png_memcpy(text + key_size, png_ptr->zbuf,
1238 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1239 png_memcpy(text, key, key_size);
1240 text_size = key_size + png_ptr->zbuf_size -
1241 png_ptr->zstream.avail_out;
1242 *(text + text_size) = '\0';
1249 text = (png_charp)png_malloc(png_ptr, text_size +
1250 (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
1252 png_memcpy(text, tmp, text_size);
1253 png_free(png_ptr, tmp);
1254 png_memcpy(text + text_size, png_ptr->zbuf,
1255 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1256 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
1257 *(text + text_size) = '\0';
1259 if (ret != Z_STREAM_END)
1261 png_ptr->zstream.next_out = png_ptr->zbuf;
1262 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1270 if (ret == Z_STREAM_END)
1274 inflateReset(&png_ptr->zstream);
1275 png_ptr->zstream.avail_in = 0;
1277 if (ret != Z_STREAM_END)
1279 png_ptr->current_text = NULL;
1280 png_free(png_ptr, key);
1281 png_free(png_ptr, text);
1285 png_ptr->current_text = NULL;
1286 png_free(png_ptr, key);
1290 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
1291 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
1292 text_ptr->key = key;
1293 #ifdef PNG_iTXt_SUPPORTED
1294 text_ptr->lang = NULL;
1295 text_ptr->lang_key = NULL;
1297 text_ptr->text = text;
1299 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1301 png_free(png_ptr, key);
1302 png_free(png_ptr, text_ptr);
1305 png_warning(png_ptr, "Insufficient memory to store text chunk.");
1310 #if defined(PNG_READ_iTXt_SUPPORTED)
1312 png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1315 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1317 png_error(png_ptr, "Out of place iTXt");
1318 /* to quiet some compiler warnings */
1319 if(info_ptr == NULL) return;
1322 #ifdef PNG_MAX_MALLOC_64K
1323 png_ptr->skip_length = 0; /* This may not be necessary */
1325 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
1327 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
1328 png_ptr->skip_length = length - (png_uint_32)65535L;
1329 length = (png_uint_32)65535L;
1333 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1334 (png_uint_32)(length+1));
1335 png_ptr->current_text[length] = '\0';
1336 png_ptr->current_text_ptr = png_ptr->current_text;
1337 png_ptr->current_text_size = (png_size_t)length;
1338 png_ptr->current_text_left = (png_size_t)length;
1339 png_ptr->process_mode = PNG_READ_iTXt_MODE;
1343 png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
1346 if (png_ptr->buffer_size && png_ptr->current_text_left)
1348 png_size_t text_size;
1350 if (png_ptr->buffer_size < png_ptr->current_text_left)
1351 text_size = png_ptr->buffer_size;
1353 text_size = png_ptr->current_text_left;
1354 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
1355 png_ptr->current_text_left -= text_size;
1356 png_ptr->current_text_ptr += text_size;
1358 if (!(png_ptr->current_text_left))
1368 if (png_ptr->buffer_size < 4)
1370 png_push_save_buffer(png_ptr);
1374 png_push_crc_finish(png_ptr);
1376 #if defined(PNG_MAX_MALLOC_64K)
1377 if (png_ptr->skip_length)
1381 key = png_ptr->current_text;
1383 for (lang = key; *lang; lang++)
1386 if (lang != key + png_ptr->current_text_size)
1389 comp_flag = *lang++;
1390 lang++; /* skip comp_type, always zero */
1392 for (lang_key = lang; *lang_key; lang_key++)
1394 lang_key++; /* skip NUL separator */
1396 for (text = lang_key; *text; text++)
1399 if (text != key + png_ptr->current_text_size)
1402 text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
1403 text_ptr->compression = comp_flag + 2;
1404 text_ptr->key = key;
1405 text_ptr->lang = lang;
1406 text_ptr->lang_key = lang_key;
1407 text_ptr->text = text;
1408 text_ptr->text_length = 0;
1409 text_ptr->itxt_length = png_strlen(text);
1411 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1413 png_ptr->current_text = NULL;
1415 png_free(png_ptr, text_ptr);
1417 png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
1422 /* This function is called when we haven't found a handler for this
1423 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
1424 * name or a critical chunk), the chunk is (currently) silently ignored.
1427 png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1431 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
1433 if (!(png_ptr->chunk_name[0] & 0x20))
1435 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1436 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1438 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1439 && png_ptr->read_user_chunk_fn == NULL
1443 png_chunk_error(png_ptr, "unknown critical chunk");
1445 /* to quiet compiler warnings about unused info_ptr */
1446 if (info_ptr == NULL)
1450 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1451 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
1453 png_unknown_chunk chunk;
1455 #ifdef PNG_MAX_MALLOC_64K
1456 if (length > (png_uint_32)65535L)
1458 png_warning(png_ptr, "unknown chunk too large to fit in memory");
1459 skip = length - (png_uint_32)65535L;
1460 length = (png_uint_32)65535L;
1464 png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
1465 chunk.data = (png_bytep)png_malloc(png_ptr, length);
1466 png_crc_read(png_ptr, chunk.data, length);
1467 chunk.size = length;
1468 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
1469 if(png_ptr->read_user_chunk_fn != NULL)
1471 /* callback to user unknown chunk handler */
1472 if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
1474 if (!(png_ptr->chunk_name[0] & 0x20))
1475 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1476 HANDLE_CHUNK_ALWAYS)
1477 png_chunk_error(png_ptr, "unknown critical chunk");
1479 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
1483 png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
1484 png_free(png_ptr, chunk.data);
1489 png_push_crc_skip(png_ptr, skip);
1493 png_push_have_info(png_structp png_ptr, png_infop info_ptr)
1495 if (png_ptr->info_fn != NULL)
1496 (*(png_ptr->info_fn))(png_ptr, info_ptr);
1500 png_push_have_end(png_structp png_ptr, png_infop info_ptr)
1502 if (png_ptr->end_fn != NULL)
1503 (*(png_ptr->end_fn))(png_ptr, info_ptr);
1507 png_push_have_row(png_structp png_ptr, png_bytep row)
1509 if (png_ptr->row_fn != NULL)
1510 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1511 (int)png_ptr->pass);
1515 png_progressive_combine_row (png_structp png_ptr,
1516 png_bytep old_row, png_bytep new_row)
1518 #ifdef PNG_USE_LOCAL_ARRAYS
1519 const int FARDATA png_pass_dsp_mask[7] =
1520 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1522 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
1523 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
1527 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1528 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1529 png_progressive_end_ptr end_fn)
1531 png_ptr->info_fn = info_fn;
1532 png_ptr->row_fn = row_fn;
1533 png_ptr->end_fn = end_fn;
1535 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
1539 png_get_progressive_ptr(png_structp png_ptr)
1541 return png_ptr->io_ptr;
1543 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */