2 ===================================================================
6 #ifdef PNG_READ_zTXt_SUPPORTED
9 +#ifdef PNG_READ_APNG_SUPPORTED
14 png_uint_32 length = png_read_chunk_header(png_ptr);
15 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
18 !(png_ptr->mode & PNG_HAVE_PLTE))
19 png_error(png_ptr, "Missing PLTE before IDAT");
21 +#ifdef PNG_READ_APNG_SUPPORTED
22 + png_have_info(png_ptr, info_ptr);
24 png_ptr->idat_size = length;
25 png_ptr->mode |= PNG_HAVE_IDAT;
28 else if (!png_memcmp(chunk_name, png_iTXt, 4))
29 png_handle_iTXt(png_ptr, info_ptr, length);
31 +#ifdef PNG_READ_APNG_SUPPORTED
32 + else if (!png_memcmp(chunk_name, png_acTL, 4))
33 + png_handle_acTL(png_ptr, info_ptr, length);
34 + else if (!png_memcmp(chunk_name, png_fcTL, 4))
35 + png_handle_fcTL(png_ptr, info_ptr, length);
36 + else if (!png_memcmp(chunk_name, png_fdAT, 4))
37 + png_handle_fdAT(png_ptr, info_ptr, length);
40 png_handle_unknown(png_ptr, info_ptr, length);
43 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
45 +#ifdef PNG_READ_APNG_SUPPORTED
47 +png_read_frame_head(png_structp png_ptr, png_infop info_ptr)
49 + png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */
51 + png_debug(0, "Reading frame head");
53 + if (!(png_ptr->mode & PNG_HAVE_acTL))
54 + png_error(png_ptr, "attempt to png_read_frame_head() but "
57 + /* do nothing for the main IDAT */
58 + if (png_ptr->num_frames_read == 0)
61 + png_crc_finish(png_ptr, 0); /* CRC from last IDAT or fdAT chunk */
63 + png_read_reset(png_ptr);
64 + png_ptr->mode &= ~PNG_HAVE_fcTL;
66 + have_chunk_after_DAT = 0;
72 + png_byte chunk_length[4];
75 + png_read_data(png_ptr, chunk_length, 4);
76 + length = png_get_uint_31(png_ptr, chunk_length);
78 + png_reset_crc(png_ptr);
79 + png_crc_read(png_ptr, png_ptr->chunk_name, 4);
81 + if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
83 + /* discard trailing IDATs for the first frame */
84 + if (have_chunk_after_DAT || png_ptr->num_frames_read > 1)
85 + png_error(png_ptr, "png_read_frame_head(): out of place IDAT");
86 + png_crc_finish(png_ptr, length);
88 + else if (!png_memcmp(png_ptr->chunk_name, png_fcTL, 4))
90 + png_handle_fcTL(png_ptr, info_ptr, length);
91 + have_chunk_after_DAT = 1;
93 + else if (!png_memcmp(png_ptr->chunk_name, png_fdAT, 4))
95 + png_ensure_sequence_number(png_ptr, length);
97 + /* discard trailing fdATs for frames other than the first */
98 + if (!have_chunk_after_DAT && png_ptr->num_frames_read > 1)
99 + png_crc_finish(png_ptr, length - 4);
100 + else if(png_ptr->mode & PNG_HAVE_fcTL)
102 + png_ptr->idat_size = length - 4;
103 + png_ptr->mode |= PNG_HAVE_IDAT;
108 + png_error(png_ptr, "png_read_frame_head(): out of place fdAT");
112 + png_warning(png_ptr, "Skipped (ignored) a chunk "
113 + "between APNG chunks");
114 + png_crc_finish(png_ptr, length);
118 +#endif /* PNG_READ_APNG_SUPPORTED */
120 /* Optional call to update the users info_ptr structure */
122 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
124 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
127 +#ifdef PNG_READ_APNG_SUPPORTED
131 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
133 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
134 @@ -596,13 +691,39 @@
136 if (!(png_ptr->zstream.avail_in))
138 - while (!png_ptr->idat_size)
139 + png_uint_32 bytes_to_skip = 0;
141 + while (!png_ptr->idat_size || bytes_to_skip != 0)
143 - png_crc_finish(png_ptr, 0);
144 + png_crc_finish(png_ptr, bytes_to_skip);
147 png_ptr->idat_size = png_read_chunk_header(png_ptr);
148 +#ifdef PNG_READ_APNG_SUPPORTED
149 + if (png_ptr->num_frames_read == 0)
152 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
153 png_error(png_ptr, "Not enough image data");
154 +#ifdef PNG_READ_APNG_SUPPORTED
158 + if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
159 + png_error(png_ptr, "Not enough image data");
160 + if (png_memcmp(png_ptr->chunk_name, png_fdAT, 4))
162 + png_warning(png_ptr, "Skipped (ignored) a chunk "
163 + "between APNG chunks");
164 + bytes_to_skip = png_ptr->idat_size;
168 + png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
170 + png_ptr->idat_size -= 4;
174 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
175 png_ptr->zstream.next_in = png_ptr->zbuf;
177 png_benign_error(png_ptr, "Extra compressed data");
178 png_ptr->mode |= PNG_AFTER_IDAT;
179 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
180 +#ifdef PNG_READ_APNG_SUPPORTED
181 + png_ptr->num_frames_read++;
187 #ifdef PNG_READ_zTXt_SUPPORTED
190 +#ifdef PNG_READ_APNG_SUPPORTED
195 png_uint_32 length = png_read_chunk_header(png_ptr);
196 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
198 @@ -974,6 +1103,14 @@
199 else if (!png_memcmp(chunk_name, png_iTXt, 4))
200 png_handle_iTXt(png_ptr, info_ptr, length);
202 +#ifdef PNG_READ_APNG_SUPPORTED
203 + else if (!png_memcmp(chunk_name, png_acTL, 4))
204 + png_handle_acTL(png_ptr, info_ptr, length);
205 + else if (!png_memcmp(chunk_name, png_fcTL, 4))
206 + png_handle_fcTL(png_ptr, info_ptr, length);
207 + else if (!png_memcmp(chunk_name, png_fdAT, 4))
208 + png_handle_fdAT(png_ptr, info_ptr, length);
211 png_handle_unknown(png_ptr, info_ptr, length);
212 } while (!(png_ptr->mode & PNG_HAVE_IEND));
214 ===================================================================
217 @@ -842,6 +842,167 @@
221 +#ifdef PNG_APNG_SUPPORTED
223 +png_get_acTL(png_structp png_ptr, png_infop info_ptr,
224 + png_uint_32 *num_frames, png_uint_32 *num_plays)
226 + png_debug1(1, "in %s retrieval function", "acTL");
228 + if (png_ptr != NULL && info_ptr != NULL &&
229 + (info_ptr->valid & PNG_INFO_acTL) &&
230 + num_frames != NULL && num_plays != NULL)
232 + *num_frames = info_ptr->num_frames;
233 + *num_plays = info_ptr->num_plays;
241 +png_get_num_frames(png_structp png_ptr, png_infop info_ptr)
243 + png_debug(1, "in png_get_num_frames()");
245 + if (png_ptr != NULL && info_ptr != NULL)
246 + return (info_ptr->num_frames);
251 +png_get_num_plays(png_structp png_ptr, png_infop info_ptr)
253 + png_debug(1, "in png_get_num_plays()");
255 + if (png_ptr != NULL && info_ptr != NULL)
256 + return (info_ptr->num_plays);
261 +png_get_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
262 + png_uint_32 *width, png_uint_32 *height,
263 + png_uint_32 *x_offset, png_uint_32 *y_offset,
264 + png_uint_16 *delay_num, png_uint_16 *delay_den,
265 + png_byte *dispose_op, png_byte *blend_op)
267 + png_debug1(1, "in %s retrieval function", "fcTL");
269 + if (png_ptr != NULL && info_ptr != NULL &&
270 + (info_ptr->valid & PNG_INFO_fcTL) &&
271 + width != NULL && height != NULL &&
272 + x_offset != NULL && x_offset != NULL &&
273 + delay_num != NULL && delay_den != NULL &&
274 + dispose_op != NULL && blend_op != NULL)
276 + *width = info_ptr->next_frame_width;
277 + *height = info_ptr->next_frame_height;
278 + *x_offset = info_ptr->next_frame_x_offset;
279 + *y_offset = info_ptr->next_frame_y_offset;
280 + *delay_num = info_ptr->next_frame_delay_num;
281 + *delay_den = info_ptr->next_frame_delay_den;
282 + *dispose_op = info_ptr->next_frame_dispose_op;
283 + *blend_op = info_ptr->next_frame_blend_op;
291 +png_get_next_frame_width(png_structp png_ptr, png_infop info_ptr)
293 + png_debug(1, "in png_get_next_frame_width()");
295 + if (png_ptr != NULL && info_ptr != NULL)
296 + return (info_ptr->next_frame_width);
301 +png_get_next_frame_height(png_structp png_ptr, png_infop info_ptr)
303 + png_debug(1, "in png_get_next_frame_height()");
305 + if (png_ptr != NULL && info_ptr != NULL)
306 + return (info_ptr->next_frame_height);
311 +png_get_next_frame_x_offset(png_structp png_ptr, png_infop info_ptr)
313 + png_debug(1, "in png_get_next_frame_x_offset()");
315 + if (png_ptr != NULL && info_ptr != NULL)
316 + return (info_ptr->next_frame_x_offset);
321 +png_get_next_frame_y_offset(png_structp png_ptr, png_infop info_ptr)
323 + png_debug(1, "in png_get_next_frame_y_offset()");
325 + if (png_ptr != NULL && info_ptr != NULL)
326 + return (info_ptr->next_frame_y_offset);
331 +png_get_next_frame_delay_num(png_structp png_ptr, png_infop info_ptr)
333 + png_debug(1, "in png_get_next_frame_delay_num()");
335 + if (png_ptr != NULL && info_ptr != NULL)
336 + return (info_ptr->next_frame_delay_num);
341 +png_get_next_frame_delay_den(png_structp png_ptr, png_infop info_ptr)
343 + png_debug(1, "in png_get_next_frame_delay_den()");
345 + if (png_ptr != NULL && info_ptr != NULL)
346 + return (info_ptr->next_frame_delay_den);
351 +png_get_next_frame_dispose_op(png_structp png_ptr, png_infop info_ptr)
353 + png_debug(1, "in png_get_next_frame_dispose_op()");
355 + if (png_ptr != NULL && info_ptr != NULL)
356 + return (info_ptr->next_frame_dispose_op);
361 +png_get_next_frame_blend_op(png_structp png_ptr, png_infop info_ptr)
363 + png_debug(1, "in png_get_next_frame_blend_op()");
365 + if (png_ptr != NULL && info_ptr != NULL)
366 + return (info_ptr->next_frame_blend_op);
371 +png_get_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr)
373 + png_debug(1, "in png_first_frame_is_hidden()");
375 + if (png_ptr != NULL)
376 + return (png_byte)(png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN);
380 +#endif /* PNG_APNG_SUPPORTED */
382 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
384 png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
386 ===================================================================
390 png_fixed_point int_y_blue PNG_DEPSTRUCT;
393 +#ifdef PNG_APNG_SUPPORTED
394 + png_uint_32 num_frames; /* including default image */
395 + png_uint_32 num_plays;
396 + png_uint_32 next_frame_width;
397 + png_uint_32 next_frame_height;
398 + png_uint_32 next_frame_x_offset;
399 + png_uint_32 next_frame_y_offset;
400 + png_uint_16 next_frame_delay_num;
401 + png_uint_16 next_frame_delay_den;
402 + png_byte next_frame_dispose_op;
403 + png_byte next_frame_blend_op;
408 typedef png_info FAR * png_infop;
410 #define PNG_INFO_sPLT 0x2000 /* ESR, 1.0.6 */
411 #define PNG_INFO_sCAL 0x4000 /* ESR, 1.0.6 */
412 #define PNG_INFO_IDAT 0x8000L /* ESR, 1.0.6 */
413 +#ifdef PNG_APNG_SUPPORTED
414 +#define PNG_INFO_acTL 0x10000L
415 +#define PNG_INFO_fcTL 0x20000L
418 /* This is used for the transformation routines, as some of them
419 * change these values for the row. It also should enable using
420 @@ -1026,6 +1043,10 @@
421 typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
422 typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
424 +#ifdef PNG_APNG_SUPPORTED
425 +typedef void (PNGAPI *png_progressive_frame_ptr) PNGARG((png_structp,
430 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
431 @@ -1445,6 +1466,39 @@
432 png_uint_32 user_chunk_cache_max PNG_DEPSTRUCT;
435 +#ifdef PNG_APNG_SUPPORTED
436 + png_uint_32 apng_flags;
437 + png_uint_32 next_seq_num; /* next fcTL/fdAT chunk sequence number */
438 + png_uint_32 first_frame_width;
439 + png_uint_32 first_frame_height;
441 +#ifdef PNG_READ_APNG_SUPPORTED
442 + png_uint_32 num_frames_read; /* incremented after all image data of */
443 + /* a frame is read */
444 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
445 + png_progressive_frame_ptr frame_info_fn; /* frame info read callback */
446 + png_progressive_frame_ptr frame_end_fn; /* frame data read callback */
450 +#ifdef PNG_WRITE_APNG_SUPPORTED
451 + png_uint_32 num_frames_to_write;
452 + png_uint_32 num_frames_written;
455 +/* For png_struct.apng_flags: */
456 +#define PNG_FIRST_FRAME_HIDDEN 0x0001
458 +/* dispose_op flags from inside fcTL */
459 +#define PNG_DISPOSE_OP_NONE 0x00
460 +#define PNG_DISPOSE_OP_BACKGROUND 0x01
461 +#define PNG_DISPOSE_OP_PREVIOUS 0x02
463 +/* blend_op flags from inside fcTL */
464 +#define PNG_BLEND_OP_SOURCE 0x00
465 +#define PNG_BLEND_OP_OVER 0x01
466 +#endif /* PNG_APNG_SUPPORTED */
468 /* New member added in libpng-1.0.25 and 1.2.17 */
469 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
470 /* Storage for unknown chunk that the library doesn't recognize. */
471 @@ -1773,6 +1827,18 @@
472 extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
475 +#ifdef PNG_WRITE_APNG_SUPPORTED
476 +extern PNG_EXPORT (void,png_write_frame_head) PNGARG((png_structp png_ptr,
477 + png_infop png_info, png_bytepp row_pointers,
478 + png_uint_32 width, png_uint_32 height,
479 + png_uint_32 x_offset, png_uint_32 y_offset,
480 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
481 + png_byte blend_op));
483 +extern PNG_EXPORT (void,png_write_frame_tail) PNGARG((png_structp png_ptr,
484 + png_infop png_info));
487 /* Write the end of the PNG file. */
488 extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
489 png_infop info_ptr));
490 @@ -2017,6 +2083,11 @@
491 png_voidp progressive_ptr,
492 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
493 png_progressive_end_ptr end_fn));
494 +#ifdef PNG_READ_APNG_SUPPORTED
495 +extern PNG_EXPORT(void,png_set_progressive_frame_fn) PNGARG((png_structp png_ptr,
496 + png_progressive_frame_ptr frame_info_fn,
497 + png_progressive_frame_ptr frame_end_fn));
500 /* Returns the user pointer associated with the push read functions */
501 extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
502 @@ -2443,6 +2514,53 @@
504 #endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */
506 +#ifdef PNG_APNG_SUPPORTED
507 +extern PNG_EXPORT(png_uint_32,png_get_acTL) PNGARG((png_structp png_ptr,
508 + png_infop info_ptr, png_uint_32 *num_frames, png_uint_32 *num_plays));
509 +extern PNG_EXPORT(png_uint_32,png_set_acTL) PNGARG((png_structp png_ptr,
510 + png_infop info_ptr, png_uint_32 num_frames, png_uint_32 num_plays));
511 +extern PNG_EXPORT(png_uint_32,png_get_num_frames) PNGARG((png_structp png_ptr,
512 + png_infop info_ptr));
513 +extern PNG_EXPORT(png_uint_32,png_get_num_plays)
514 + PNGARG((png_structp png_ptr, png_infop info_ptr));
516 +extern PNG_EXPORT(png_uint_32,png_get_next_frame_fcTL)
517 + PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 *width,
518 + png_uint_32 *height, png_uint_32 *x_offset, png_uint_32 *y_offset,
519 + png_uint_16 *delay_num, png_uint_16 *delay_den, png_byte *dispose_op,
520 + png_byte *blend_op));
521 +extern PNG_EXPORT(png_uint_32,png_set_next_frame_fcTL)
522 + PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 width,
523 + png_uint_32 height, png_uint_32 x_offset, png_uint_32 y_offset,
524 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
525 + png_byte blend_op));
526 +extern PNG_EXPORT(png_uint_32,png_get_next_frame_width)
527 + PNGARG((png_structp png_ptr, png_infop info_ptr));
528 +extern PNG_EXPORT(png_uint_32,png_get_next_frame_height)
529 + PNGARG((png_structp png_ptr, png_infop info_ptr));
530 +extern PNG_EXPORT(png_uint_32,png_get_next_frame_x_offset)
531 + PNGARG((png_structp png_ptr, png_infop info_ptr));
532 +extern PNG_EXPORT(png_uint_32,png_get_next_frame_y_offset)
533 + PNGARG((png_structp png_ptr, png_infop info_ptr));
534 +extern PNG_EXPORT(png_uint_16,png_get_next_frame_delay_num)
535 + PNGARG((png_structp png_ptr, png_infop info_ptr));
536 +extern PNG_EXPORT(png_uint_16,png_get_next_frame_delay_den)
537 + PNGARG((png_structp png_ptr, png_infop info_ptr));
538 +extern PNG_EXPORT(png_byte,png_get_next_frame_dispose_op)
539 + PNGARG((png_structp png_ptr, png_infop info_ptr));
540 +extern PNG_EXPORT(png_byte,png_get_next_frame_blend_op)
541 + PNGARG((png_structp png_ptr, png_infop info_ptr));
542 +extern PNG_EXPORT(png_byte,png_get_first_frame_is_hidden)
543 + PNGARG((png_structp png_ptr, png_infop info_ptr));
544 +extern PNG_EXPORT(png_uint_32,png_set_first_frame_is_hidden)
545 + PNGARG((png_structp png_ptr, png_infop info_ptr, png_byte is_hidden));
546 +#endif /* PNG_APNG_SUPPORTED */
548 +#ifdef PNG_READ_APNG_SUPPORTED
549 +extern PNG_EXPORT(void,png_read_frame_head) PNGARG((png_structp png_ptr,
550 + png_infop info_ptr));
553 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
554 /* Provide a list of chunks and how they are to be handled, if the built-in
555 handling or default unknown chunk handling is not desired. Any chunks not
557 ===================================================================
561 #define PNG_BACKGROUND_IS_GRAY 0x800
562 #define PNG_HAVE_PNG_SIGNATURE 0x1000
563 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */
564 +#ifdef PNG_APNG_SUPPORTED
565 +#define PNG_HAVE_acTL 0x4000
566 +#define PNG_HAVE_fcTL 0x8000L
569 /* Flags for the transformations the PNG library does on the image data */
570 #define PNG_BGR 0x0001
572 #define PNG_tIME PNG_CONST png_byte png_tIME[5] = {116, 73, 77, 69, '\0'}
573 #define PNG_tRNS PNG_CONST png_byte png_tRNS[5] = {116, 82, 78, 83, '\0'}
574 #define PNG_zTXt PNG_CONST png_byte png_zTXt[5] = {122, 84, 88, 116, '\0'}
575 +#ifdef PNG_APNG_SUPPORTED
576 +#define PNG_acTL PNG_CONST png_byte png_acTL[5] = { 97, 99, 84, 76, '\0'}
577 +#define PNG_fcTL PNG_CONST png_byte png_fcTL[5] = {102, 99, 84, 76, '\0'}
578 +#define PNG_fdAT PNG_CONST png_byte png_fdAT[5] = {102, 100, 65, 84, '\0'}
582 /* Inhibit C++ name-mangling for libpng functions but not for system calls. */
587 +#ifdef PNG_WRITE_APNG_SUPPORTED
588 +PNG_EXTERN void png_write_acTL PNGARG((png_structp png_ptr,
589 + png_uint_32 num_frames, png_uint_32 num_plays));
591 +PNG_EXTERN void png_write_fcTL PNGARG((png_structp png_ptr,
592 + png_uint_32 width, png_uint_32 height,
593 + png_uint_32 x_offset, png_uint_32 y_offset,
594 + png_uint_16 delay_num, png_uint_16 delay_den,
595 + png_byte dispose_op, png_byte blend_op));
598 /* Called when finished processing a row of data */
599 PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));
602 PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
603 png_infop info_ptr));
605 +#ifdef PNG_READ_APNG_SUPPORTED
606 +/* Private, reset some things to become ready for reading next frame */
607 +PNG_EXTERN void png_read_reset PNGARG((png_structp png_ptr));
608 +PNG_EXTERN void png_read_reinit PNGARG((png_structp png_ptr,
609 + png_infop info_ptr));
610 +PNG_EXTERN void png_progressive_read_reset PNGARG((png_structp png_ptr));
612 +#ifdef PNG_WRITE_APNG_SUPPORTED
613 +/* Private, reset some things to become ready for writing next frame */
614 +PNG_EXTERN void png_write_reset PNGARG((png_structp png_ptr));
615 +PNG_EXTERN void png_write_reinit PNGARG((png_structp png_ptr,
616 + png_infop info_ptr, png_uint_32 width, png_uint_32 height));
619 /* These are the functions that do the transformations */
620 #ifdef PNG_READ_FILLER_SUPPORTED
621 PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
623 png_uint_32 length));
626 +#ifdef PNG_READ_APNG_SUPPORTED
627 +PNG_EXTERN void png_handle_acTL PNGARG((png_structp png_ptr, png_infop info_ptr,
628 + png_uint_32 length));
629 +PNG_EXTERN void png_handle_fcTL PNGARG((png_structp png_ptr, png_infop info_ptr,
630 + png_uint_32 length));
631 +PNG_EXTERN void png_have_info PNGARG((png_structp png_ptr, png_infop info_ptr));
632 +PNG_EXTERN void png_handle_fdAT PNGARG((png_structp png_ptr, png_infop info_ptr,
633 + png_uint_32 length));
634 +PNG_EXTERN void png_ensure_sequence_number PNGARG((png_structp png_ptr,
635 + png_uint_32 length));
637 +#ifdef PNG_APNG_SUPPORTED
638 +PNG_EXTERN void png_ensure_fcTL_is_valid PNGARG((png_structp png_ptr,
639 + png_uint_32 width, png_uint_32 height,
640 + png_uint_32 x_offset, png_uint_32 y_offset,
641 + png_uint_16 delay_num, png_uint_16 delay_den,
642 + png_byte dispose_op, png_byte blend_op));
645 PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
646 png_infop info_ptr, png_uint_32 length));
649 ===================================================================
653 /* The rest of these check to see if the valid field has the appropriate
654 * flag set, and if it does, writes the chunk.
656 +#ifdef PNG_WRITE_APNG_SUPPORTED
657 + if (info_ptr->valid & PNG_INFO_acTL)
658 + png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
660 #ifdef PNG_WRITE_gAMA_SUPPORTED
661 if (info_ptr->valid & PNG_INFO_gAMA)
665 if (!(png_ptr->mode & PNG_HAVE_IDAT))
666 png_error(png_ptr, "No IDATs written into file");
667 +#ifdef PNG_WRITE_APNG_SUPPORTED
668 + if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
669 + png_error(png_ptr, "Not enough frames written");
672 /* See if user wants us to write information chunks */
673 if (info_ptr != NULL)
674 @@ -1454,4 +1462,39 @@
679 +#ifdef PNG_WRITE_APNG_SUPPORTED
681 +png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
682 + png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
683 + png_uint_32 x_offset, png_uint_32 y_offset,
684 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
687 + png_debug(1, "in png_write_frame_head");
689 + /* there is a chance this has been set after png_write_info was called,
690 + * so it would be set but not written. is there a way to be sure? */
691 + if (!(info_ptr->valid & PNG_INFO_acTL))
692 + png_error(png_ptr, "png_write_frame_head(): acTL not set");
694 + png_write_reset(png_ptr);
696 + png_write_reinit(png_ptr, info_ptr, width, height);
698 + if ( !(png_ptr->num_frames_written == 0 &&
699 + (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) ) )
700 + png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
701 + delay_num, delay_den, dispose_op, blend_op);
705 +png_write_frame_tail(png_structp png_ptr, png_infop png_info)
707 + png_debug(1, "in png_write_frame_tail");
709 + png_ptr->num_frames_written++;
711 +#endif /* PNG_WRITE_APNG_SUPPORTED */
713 #endif /* PNG_WRITE_SUPPORTED */
715 ===================================================================
719 # define PNG_READ_tRNS_SUPPORTED
720 # define PNG_tRNS_SUPPORTED
722 +#ifndef PNG_NO_READ_APNG
723 +# define PNG_READ_APNG_SUPPORTED
724 +# define PNG_APNG_SUPPORTED
726 #ifndef PNG_NO_READ_zTXt
727 # define PNG_READ_zTXt_SUPPORTED
728 # define PNG_zTXt_SUPPORTED
729 @@ -1028,6 +1032,14 @@
730 # define PNG_TEXT_SUPPORTED
733 +#ifndef PNG_NO_WRITE_APNG
734 +# ifndef PNG_WRITE_APNG_SUPPORTED
735 +# define PNG_WRITE_APNG_SUPPORTED
737 +# ifndef PNG_APNG_SUPPORTED
738 +# define PNG_APNG_SUPPORTED
742 #ifdef PNG_WRITE_tIME_SUPPORTED
743 # ifndef PNG_NO_CONVERT_tIME
745 ===================================================================
749 #ifdef PNG_READ_zTXt_SUPPORTED
752 +#ifdef PNG_READ_APNG_SUPPORTED
758 /* First we make sure we have enough data for the 4 byte chunk name
759 * and the 4 byte chunk length before proceeding with decoding the
760 @@ -230,6 +235,103 @@
761 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
764 +#ifdef PNG_READ_APNG_SUPPORTED
765 + if (png_ptr->num_frames_read > 0 &&
766 + png_ptr->num_frames_read < info_ptr->num_frames)
768 + if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
770 + /* Discard trailing IDATs for the first frame */
771 + if (png_ptr->mode & PNG_HAVE_fcTL || png_ptr->num_frames_read > 1)
772 + png_error(png_ptr, "out of place IDAT");
774 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
776 + png_push_save_buffer(png_ptr);
779 + png_push_crc_skip(png_ptr, png_ptr->push_length);
780 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
783 + else if (!png_memcmp(png_ptr->chunk_name, png_fdAT, 4))
785 + if (png_ptr->buffer_size < 4)
787 + png_push_save_buffer(png_ptr);
790 + png_ensure_sequence_number(png_ptr, 4);
792 + if (!(png_ptr->mode & PNG_HAVE_fcTL))
794 + /* Discard trailing fdATs for frames other than the first */
795 + if (png_ptr->num_frames_read < 2)
796 + png_error(png_ptr, "out of place fdAT");
798 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
800 + png_push_save_buffer(png_ptr);
803 + png_push_crc_skip(png_ptr, png_ptr->push_length);
804 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
809 + /* frame data follows */
810 + png_ptr->idat_size = png_ptr->push_length - 4;
811 + png_ptr->mode |= PNG_HAVE_IDAT;
812 + png_ptr->process_mode = PNG_READ_IDAT_MODE;
817 + else if(!png_memcmp(png_ptr->chunk_name, png_fcTL, 4))
819 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
821 + png_push_save_buffer(png_ptr);
825 + png_read_reset(png_ptr);
826 + png_ptr->mode &= ~PNG_HAVE_fcTL;
828 + png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
830 + if (!(png_ptr->mode & PNG_HAVE_fcTL))
831 + png_error(png_ptr, "missing required fcTL chunk");
833 + png_read_reinit(png_ptr, info_ptr);
834 + png_progressive_read_reset(png_ptr);
836 + if (png_ptr->frame_info_fn != NULL)
837 + (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
839 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
845 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
847 + png_push_save_buffer(png_ptr);
850 + png_warning(png_ptr, "Skipped (ignored) a chunk "
851 + "between APNG chunks");
852 + png_push_crc_skip(png_ptr, png_ptr->push_length);
853 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
859 +#endif /* PNG_READ_APNG_SUPPORTED */
861 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
862 if (png_ptr->mode & PNG_AFTER_IDAT)
863 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
865 png_benign_error(png_ptr, "Too many IDATs found");
868 +#ifdef PNG_READ_APNG_SUPPORTED
869 + png_have_info(png_ptr, info_ptr);
871 png_ptr->idat_size = png_ptr->push_length;
872 png_ptr->mode |= PNG_HAVE_IDAT;
873 png_ptr->process_mode = PNG_READ_IDAT_MODE;
878 +#ifdef PNG_READ_APNG_SUPPORTED
879 + else if (!png_memcmp(png_ptr->chunk_name, png_acTL, 4))
881 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
883 + png_push_save_buffer(png_ptr);
887 + png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
889 + else if (!png_memcmp(png_ptr->chunk_name, png_fcTL, 4))
891 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
893 + png_push_save_buffer(png_ptr);
897 + png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
899 + else if (!png_memcmp(png_ptr->chunk_name, png_fdAT, 4))
901 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
903 + png_push_save_buffer(png_ptr);
907 + png_handle_fdAT(png_ptr, info_ptr, png_ptr->push_length);
909 +#endif /* PNG_READ_APNG_SUPPORTED */
912 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
913 @@ -736,11 +873,15 @@
914 png_push_read_IDAT(png_structp png_ptr)
917 +#ifdef PNG_READ_APNG_SUPPORTED
921 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
923 png_byte chunk_length[4];
925 - if (png_ptr->buffer_size < 8)
926 + if (png_ptr->buffer_size < 12)
928 png_push_save_buffer(png_ptr);
930 @@ -752,15 +893,62 @@
931 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
932 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
934 - if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
935 +#ifdef PNG_READ_APNG_SUPPORTED
936 + if (png_memcmp(png_ptr->chunk_name, png_fdAT, 4)
937 + && png_ptr->num_frames_read > 0)
939 + if (png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)
941 + png_ptr->process_mode = PNG_READ_CHUNK_MODE;
942 + if (png_ptr->frame_end_fn != NULL)
943 + (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
944 + png_ptr->num_frames_read++;
949 + if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
950 + png_error(png_ptr, "Not enough image data");
951 + if (png_ptr->push_length + 4 > png_ptr->buffer_size)
953 + png_push_save_buffer(png_ptr);
956 + png_warning(png_ptr, "Skipping (ignoring) a chunk between "
958 + png_crc_finish(png_ptr, png_ptr->push_length);
959 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
965 + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)
966 +#ifdef PNG_READ_APNG_SUPPORTED
967 + && (png_ptr->num_frames_read == 0)
971 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
972 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
973 png_error(png_ptr, "Not enough compressed data");
974 +#ifdef PNG_READ_APNG_SUPPORTED
975 + if (png_ptr->frame_end_fn != NULL)
976 + (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
977 + png_ptr->num_frames_read++;
982 png_ptr->idat_size = png_ptr->push_length;
984 +#ifdef PNG_READ_APNG_SUPPORTED
985 + if (png_ptr->num_frames_read > 0)
987 + png_ensure_sequence_number(png_ptr, 4);
988 + png_ptr->idat_size -= 4;
992 if (png_ptr->idat_size && png_ptr->save_buffer_size)
994 @@ -1716,6 +1904,17 @@
995 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
998 +#ifdef PNG_READ_APNG_SUPPORTED
1000 +png_set_progressive_frame_fn(png_structp png_ptr,
1001 + png_progressive_frame_ptr frame_info_fn,
1002 + png_progressive_frame_ptr frame_end_fn)
1004 + png_ptr->frame_info_fn = frame_info_fn;
1005 + png_ptr->frame_end_fn = frame_end_fn;
1010 png_get_progressive_ptr(png_structp png_ptr)
1013 ===================================================================
1016 @@ -260,6 +260,11 @@
1017 info_ptr->rowbytes = 0;
1019 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
1021 +#ifdef PNG_APNG_SUPPORTED
1022 + /* for non-animated png. this may be overritten from an acTL chunk later */
1023 + info_ptr->num_frames = 1;
1027 #ifdef PNG_oFFs_SUPPORTED
1028 @@ -932,6 +937,142 @@
1030 #endif /* PNG_sPLT_SUPPORTED */
1032 +#ifdef PNG_APNG_SUPPORTED
1034 +png_set_acTL(png_structp png_ptr, png_infop info_ptr,
1035 + png_uint_32 num_frames, png_uint_32 num_plays)
1037 + png_debug1(1, "in %s storage function", "acTL");
1039 + if (png_ptr == NULL || info_ptr == NULL)
1041 + png_warning(png_ptr,
1042 + "Call to png_set_acTL() with NULL png_ptr "
1043 + "or info_ptr ignored");
1046 + if (num_frames == 0)
1048 + png_warning(png_ptr,
1049 + "Ignoring attempt to set acTL with num_frames zero");
1052 + if (num_frames > PNG_UINT_31_MAX)
1054 + png_warning(png_ptr,
1055 + "Ignoring attempt to set acTL with num_frames > 2^31-1");
1058 + if (num_plays > PNG_UINT_31_MAX)
1060 + png_warning(png_ptr,
1061 + "Ignoring attempt to set acTL with num_plays "
1066 + info_ptr->num_frames = num_frames;
1067 + info_ptr->num_plays = num_plays;
1069 + info_ptr->valid |= PNG_INFO_acTL;
1074 +/* delay_num and delay_den can hold any 16-bit values including zero */
1076 +png_set_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
1077 + png_uint_32 width, png_uint_32 height,
1078 + png_uint_32 x_offset, png_uint_32 y_offset,
1079 + png_uint_16 delay_num, png_uint_16 delay_den,
1080 + png_byte dispose_op, png_byte blend_op)
1082 + png_debug1(1, "in %s storage function", "fcTL");
1084 + if (png_ptr == NULL || info_ptr == NULL)
1086 + png_warning(png_ptr,
1087 + "Call to png_set_fcTL() with NULL png_ptr or info_ptr "
1092 + png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1093 + delay_num, delay_den, dispose_op, blend_op);
1095 + if (blend_op == PNG_BLEND_OP_OVER)
1097 + if (!(png_ptr->color_type & PNG_COLOR_MASK_ALPHA) &&
1098 + !(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1100 + png_warning(png_ptr, "PNG_BLEND_OP_OVER is meaningless "
1101 + "and wasteful for opaque images, ignored");
1102 + blend_op = PNG_BLEND_OP_SOURCE;
1106 + info_ptr->next_frame_width = width;
1107 + info_ptr->next_frame_height = height;
1108 + info_ptr->next_frame_x_offset = x_offset;
1109 + info_ptr->next_frame_y_offset = y_offset;
1110 + info_ptr->next_frame_delay_num = delay_num;
1111 + info_ptr->next_frame_delay_den = delay_den;
1112 + info_ptr->next_frame_dispose_op = dispose_op;
1113 + info_ptr->next_frame_blend_op = blend_op;
1115 + info_ptr->valid |= PNG_INFO_fcTL;
1121 +png_ensure_fcTL_is_valid(png_structp png_ptr,
1122 + png_uint_32 width, png_uint_32 height,
1123 + png_uint_32 x_offset, png_uint_32 y_offset,
1124 + png_uint_16 delay_num, png_uint_16 delay_den,
1125 + png_byte dispose_op, png_byte blend_op)
1127 + if (width + x_offset > png_ptr->first_frame_width ||
1128 + height + y_offset > png_ptr->first_frame_height)
1129 + png_error(png_ptr, "dimensions of a frame are greater than"
1130 + "the ones in IHDR");
1131 + if (width > PNG_UINT_31_MAX)
1132 + png_error(png_ptr, "invalid width in fcTL (> 2^31-1)");
1133 + if (height > PNG_UINT_31_MAX)
1134 + png_error(png_ptr, "invalid height in fcTL (> 2^31-1)");
1135 + if (x_offset > PNG_UINT_31_MAX)
1136 + png_error(png_ptr, "invalid x_offset in fcTL (> 2^31-1)");
1137 + if (y_offset > PNG_UINT_31_MAX)
1138 + png_error(png_ptr, "invalid y_offset in fcTL (> 2^31-1)");
1140 + if (dispose_op != PNG_DISPOSE_OP_NONE &&
1141 + dispose_op != PNG_DISPOSE_OP_BACKGROUND &&
1142 + dispose_op != PNG_DISPOSE_OP_PREVIOUS)
1143 + png_error(png_ptr, "invalid dispose_op in fcTL");
1145 + if (blend_op != PNG_BLEND_OP_SOURCE &&
1146 + blend_op != PNG_BLEND_OP_OVER)
1147 + png_error(png_ptr, "invalid blend_op in fcTL");
1151 +png_set_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr,
1152 + png_byte is_hidden)
1154 + png_debug(1, "in png_first_frame_is_hidden()");
1156 + if (png_ptr == NULL)
1160 + png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
1162 + png_ptr->apng_flags &= ~PNG_FIRST_FRAME_HIDDEN;
1166 +#endif /* PNG_APNG_SUPPORTED */
1168 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
1170 png_set_unknown_chunks(png_structp png_ptr,
1172 ===================================================================
1175 @@ -434,6 +434,11 @@
1176 filter_type = buf[11];
1177 interlace_type = buf[12];
1179 +#ifdef PNG_READ_APNG_SUPPORTED
1180 + png_ptr->first_frame_width = width;
1181 + png_ptr->first_frame_height = height;
1184 /* Set internal variables */
1185 png_ptr->width = width;
1186 png_ptr->height = height;
1187 @@ -2318,6 +2323,168 @@
1191 +#ifdef PNG_READ_APNG_SUPPORTED
1193 +png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1196 + png_uint_32 num_frames;
1197 + png_uint_32 num_plays;
1198 + png_uint_32 didSet;
1200 + png_debug(1, "in png_handle_acTL");
1202 + if (!(png_ptr->mode & PNG_HAVE_IHDR))
1204 + png_error(png_ptr, "Missing IHDR before acTL");
1206 + else if (png_ptr->mode & PNG_HAVE_IDAT)
1208 + png_warning(png_ptr, "Invalid acTL after IDAT skipped");
1209 + png_crc_finish(png_ptr, length);
1212 + else if (png_ptr->mode & PNG_HAVE_acTL)
1214 + png_warning(png_ptr, "Duplicate acTL skipped");
1215 + png_crc_finish(png_ptr, length);
1218 + else if (length != 8)
1220 + png_warning(png_ptr, "acTL with invalid length skipped");
1221 + png_crc_finish(png_ptr, length);
1225 + png_crc_read(png_ptr, data, 8);
1226 + png_crc_finish(png_ptr, 0);
1228 + num_frames = png_get_uint_31(png_ptr, data);
1229 + num_plays = png_get_uint_31(png_ptr, data + 4);
1231 + /* the set function will do error checking on num_frames */
1232 + didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
1234 + png_ptr->mode |= PNG_HAVE_acTL;
1238 +png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1240 + png_byte data[22];
1241 + png_uint_32 width;
1242 + png_uint_32 height;
1243 + png_uint_32 x_offset;
1244 + png_uint_32 y_offset;
1245 + png_uint_16 delay_num;
1246 + png_uint_16 delay_den;
1247 + png_byte dispose_op;
1248 + png_byte blend_op;
1250 + png_debug(1, "in png_handle_fcTL");
1252 + png_ensure_sequence_number(png_ptr, length);
1254 + if (!(png_ptr->mode & PNG_HAVE_IHDR))
1256 + png_error(png_ptr, "Missing IHDR before fcTL");
1258 + else if (png_ptr->mode & PNG_HAVE_IDAT)
1260 + /* for any frames other then the first this message may be misleading,
1261 + * but correct. PNG_HAVE_IDAT is unset before the frame head is read
1262 + * i can't think of a better message */
1263 + png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
1264 + png_crc_finish(png_ptr, length-4);
1267 + else if (png_ptr->mode & PNG_HAVE_fcTL)
1269 + png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
1270 + png_crc_finish(png_ptr, length-4);
1273 + else if (length != 26)
1275 + png_warning(png_ptr, "fcTL with invalid length skipped");
1276 + png_crc_finish(png_ptr, length-4);
1280 + png_crc_read(png_ptr, data, 22);
1281 + png_crc_finish(png_ptr, 0);
1283 + width = png_get_uint_31(png_ptr, data);
1284 + height = png_get_uint_31(png_ptr, data + 4);
1285 + x_offset = png_get_uint_31(png_ptr, data + 8);
1286 + y_offset = png_get_uint_31(png_ptr, data + 12);
1287 + delay_num = png_get_uint_16(data + 16);
1288 + delay_den = png_get_uint_16(data + 18);
1289 + dispose_op = data[20];
1290 + blend_op = data[21];
1292 + if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
1293 + png_error(png_ptr, "fcTL for the first frame must have zero offset");
1294 + if (png_ptr->num_frames_read == 0 &&
1295 + (width != info_ptr->width || height != info_ptr->height))
1296 + png_error(png_ptr, "size in first frame's fcTL must match "
1297 + "the size in IHDR");
1299 + /* the set function will do more error checking */
1300 + png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
1301 + x_offset, y_offset, delay_num, delay_den,
1302 + dispose_op, blend_op);
1304 + png_read_reinit(png_ptr, info_ptr);
1306 + png_ptr->mode |= PNG_HAVE_fcTL;
1310 +png_have_info(png_structp png_ptr, png_infop info_ptr)
1312 + if((info_ptr->valid & PNG_INFO_acTL) && !(info_ptr->valid & PNG_INFO_fcTL))
1314 + png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
1315 + info_ptr->num_frames++;
1320 +png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1322 + png_ensure_sequence_number(png_ptr, length);
1324 + /* This function is only called from png_read_end(), png_read_info(),
1325 + * and png_push_read_chunk() which means that:
1326 + * - the user doesn't want to read this frame
1327 + * - or this is an out-of-place fdAT
1328 + * in either case it is safe to ignore the chunk with a warning */
1329 + png_warning(png_ptr, "ignoring fdAT chunk");
1330 + png_crc_finish(png_ptr, length - 4);
1334 +png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
1337 + png_uint_32 sequence_number;
1340 + png_error(png_ptr, "invalid fcTL or fdAT chunk found");
1342 + png_crc_read(png_ptr, data, 4);
1343 + sequence_number = png_get_uint_31(png_ptr, data);
1345 + if (sequence_number != png_ptr->next_seq_num)
1346 + png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
1349 + png_ptr->next_seq_num++;
1351 +#endif /* PNG_READ_APNG_SUPPORTED */
1353 /* This function is called when we haven't found a handler for a
1354 chunk. If there isn't a problem with the chunk itself (ie bad
1355 chunk name, CRC, or a critical chunk), the chunk is silently ignored
1356 @@ -3353,4 +3520,73 @@
1358 png_ptr->flags |= PNG_FLAG_ROW_INIT;
1361 +#ifdef PNG_READ_APNG_SUPPORTED
1362 +/* This function is to be called after the main IDAT set has been read and
1363 + * before a new IDAT is read. It resets some parts of png_ptr
1364 + * to make them usable by the read functions again */
1366 +png_read_reset(png_structp png_ptr)
1368 + png_ptr->mode &= ~PNG_HAVE_IDAT;
1369 + png_ptr->mode &= ~PNG_AFTER_IDAT;
1370 + png_ptr->row_number = 0;
1371 + png_ptr->pass = 0;
1372 + png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
1376 +png_read_reinit(png_structp png_ptr, png_infop info_ptr)
1378 + png_ptr->width = info_ptr->next_frame_width;
1379 + png_ptr->height = info_ptr->next_frame_height;
1380 + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
1381 + if (png_ptr->prev_row)
1382 + png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1385 +/* same as png_read_reset() but for the progressive reader */
1387 +png_progressive_read_reset(png_structp png_ptr)
1389 + /* start of interlace block */
1390 + const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1392 + /* offset to next interlace block */
1393 + const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1395 + /* start of interlace block in the y direction */
1396 + const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1398 + /* offset to next interlace block in the y direction */
1399 + const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1401 + if (png_ptr->interlaced)
1403 + if (!(png_ptr->transformations & PNG_INTERLACE))
1404 + png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1405 + png_pass_ystart[0]) / png_pass_yinc[0];
1407 + png_ptr->num_rows = png_ptr->height;
1409 + png_ptr->iwidth = (png_ptr->width +
1410 + png_pass_inc[png_ptr->pass] - 1 -
1411 + png_pass_start[png_ptr->pass]) /
1412 + png_pass_inc[png_ptr->pass];
1416 + png_ptr->num_rows = png_ptr->height;
1417 + png_ptr->iwidth = png_ptr->width;
1419 + png_ptr->flags &= ~PNG_FLAG_ZLIB_FINISHED;
1420 + if (inflateReset(&(png_ptr->zstream)) != Z_OK)
1421 + png_error(png_ptr, "inflateReset failed");
1422 + png_ptr->zstream.avail_in = 0;
1423 + png_ptr->zstream.next_in = 0;
1424 + png_ptr->zstream.next_out = png_ptr->row_buf;
1425 + png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
1426 + png_ptr->iwidth) + 1;
1428 +#endif /* PNG_READ_APNG_SUPPORTED */
1429 #endif /* PNG_READ_SUPPORTED */
1431 ===================================================================
1434 @@ -539,6 +539,11 @@
1435 /* Write the chunk */
1436 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
1438 +#ifdef PNG_WRITE_APNG_SUPPORTED
1439 + png_ptr->first_frame_width = width;
1440 + png_ptr->first_frame_height = height;
1443 /* Initialize zlib with PNG info */
1444 png_ptr->zstream.zalloc = png_zalloc;
1445 png_ptr->zstream.zfree = png_zfree;
1447 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
1450 +#ifdef PNG_WRITE_APNG_SUPPORTED
1454 png_debug(1, "in png_write_IDAT");
1456 @@ -705,7 +713,28 @@
1457 "Invalid zlib compression method or flags in IDAT");
1460 +#ifdef PNG_WRITE_APNG_SUPPORTED
1461 + if(png_ptr->num_frames_written == 0)
1463 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
1464 +#ifdef PNG_WRITE_APNG_SUPPORTED
1469 + png_write_chunk_start(png_ptr, (png_bytep)png_fdAT, 4 + length);
1471 + png_save_uint_32(buf, png_ptr->next_seq_num);
1472 + png_write_chunk_data(png_ptr, buf, 4);
1474 + png_write_chunk_data(png_ptr, data, length);
1476 + png_write_chunk_end(png_ptr);
1478 + png_ptr->next_seq_num++;
1482 png_ptr->mode |= PNG_HAVE_IDAT;
1485 @@ -1712,6 +1741,66 @@
1489 +#ifdef PNG_WRITE_APNG_SUPPORTED
1491 +png_write_acTL(png_structp png_ptr,
1492 + png_uint_32 num_frames, png_uint_32 num_plays)
1495 + png_byte data[16];
1497 + png_debug(1, "in png_write_acTL");
1499 + png_ptr->num_frames_to_write = num_frames;
1501 + if (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN)
1504 + png_save_uint_32(data, num_frames);
1505 + png_save_uint_32(data + 4, num_plays);
1507 + png_write_chunk(png_ptr, (png_bytep)png_acTL, data, (png_size_t)8);
1511 +png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
1512 + png_uint_32 x_offset, png_uint_32 y_offset,
1513 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1514 + png_byte blend_op)
1517 + png_byte data[26];
1519 + png_debug(1, "in png_write_fcTL");
1521 + if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0))
1522 + png_error(png_ptr, "x and/or y offset for the first frame aren't 0");
1523 + if (png_ptr->num_frames_written == 0 &&
1524 + (width != png_ptr->first_frame_width ||
1525 + height != png_ptr->first_frame_height))
1526 + png_error(png_ptr, "width and/or height in the first frame's fcTL "
1527 + "don't match the ones in IHDR");
1529 + /* more error checking */
1530 + png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1531 + delay_num, delay_den, dispose_op, blend_op);
1533 + png_save_uint_32(data, png_ptr->next_seq_num);
1534 + png_save_uint_32(data + 4, width);
1535 + png_save_uint_32(data + 8, height);
1536 + png_save_uint_32(data + 12, x_offset);
1537 + png_save_uint_32(data + 16, y_offset);
1538 + png_save_uint_16(data + 20, delay_num);
1539 + png_save_uint_16(data + 22, delay_den);
1540 + data[24] = dispose_op;
1541 + data[25] = blend_op;
1543 + png_write_chunk(png_ptr, (png_bytep)png_fcTL, data, (png_size_t)26);
1545 + png_ptr->next_seq_num++;
1547 +#endif /* PNG_WRITE_APNG_SUPPORTED */
1549 /* Initializes the row writing capability of libpng */
1551 png_write_start_row(png_structp png_ptr)
1552 @@ -2113,8 +2202,8 @@
1553 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
1554 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
1556 - /* These will never be selected so we need not test them. */
1557 - filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
1558 + /* These use previous row */
1559 + filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_AVG | PNG_FILTER_PAETH);
1563 @@ -2783,4 +2872,39 @@
1568 +#ifdef PNG_WRITE_APNG_SUPPORTED
1570 +png_write_reset(png_structp png_ptr)
1572 + png_ptr->row_number = 0;
1573 + png_ptr->pass = 0;
1574 + png_ptr->mode &= ~PNG_HAVE_IDAT;
1578 +png_write_reinit(png_structp png_ptr, png_infop info_ptr,
1579 + png_uint_32 width, png_uint_32 height)
1581 + if (png_ptr->num_frames_written == 0 &&
1582 + (width != png_ptr->first_frame_width ||
1583 + height != png_ptr->first_frame_height))
1584 + png_error(png_ptr, "width and/or height in the first frame's fcTL "
1585 + "don't match the ones in IHDR");
1586 + if (width > png_ptr->first_frame_width ||
1587 + height > png_ptr->first_frame_height)
1588 + png_error(png_ptr, "width and/or height for a frame greater than"
1589 + "the ones in IHDR");
1591 + png_set_IHDR(png_ptr, info_ptr, width, height,
1592 + info_ptr->bit_depth, info_ptr->color_type,
1593 + info_ptr->interlace_type, info_ptr->compression_type,
1594 + info_ptr->filter_type);
1596 + png_ptr->width = width;
1597 + png_ptr->height = height;
1598 + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
1599 + png_ptr->usr_width = png_ptr->width;
1602 #endif /* PNG_WRITE_SUPPORTED */
1604 ===================================================================
1607 @@ -1325,7 +1325,7 @@
1608 * pixels. This check added to libpng-1.2.19
1610 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1611 - png_error(png_ptr, "Uninitialized row");
1612 + png_warning(png_ptr, "Uninitialized row");
1614 png_warning(png_ptr, "Uninitialized row");