+#ifdef PNG_READ_APNG_SUPPORTED
+ if (png_ptr->num_frames_read > 0 &&
+ png_ptr->num_frames_read < info_ptr->num_frames)
+ {
+ if (chunk_name == png_IDAT)
+ {
+ /* Discard trailing IDATs for the first frame */
+ if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
+ png_ptr->num_frames_read > 1)
+ png_error(png_ptr, "out of place IDAT");
+
+ PNG_PUSH_SAVE_BUFFER_IF_FULL
+ png_crc_finish(png_ptr, png_ptr->push_length);
+ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
+ }
+
+ else if (chunk_name == png_fdAT)
+ {
+ PNG_PUSH_SAVE_BUFFER_IF_LT(4)
+ png_ensure_sequence_number(png_ptr, 4);
+
+ if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
+ {
+ /* Discard trailing fdATs for frames other than the first */
+ if (png_ptr->num_frames_read < 2)
+ png_error(png_ptr, "out of place fdAT");
+
+ PNG_PUSH_SAVE_BUFFER_IF_FULL
+ png_crc_finish(png_ptr, png_ptr->push_length);
+ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
+ }
+
+ else
+ {
+ /* frame data follows */
+ png_ptr->idat_size = png_ptr->push_length - 4;
+ png_ptr->mode |= PNG_HAVE_IDAT;
+ png_ptr->process_mode = PNG_READ_IDAT_MODE;
+ }
+ }
+
+ else if (chunk_name == png_fcTL)
+ {
+ PNG_PUSH_SAVE_BUFFER_IF_FULL
+ png_read_reset(png_ptr);
+ png_ptr->mode &= ~PNG_HAVE_fcTL;
+
+ png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
+
+ if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
+ png_error(png_ptr, "missing required fcTL chunk");
+
+ png_read_reinit(png_ptr, info_ptr);
+ png_progressive_read_reset(png_ptr);
+
+ if (png_ptr->frame_info_fn != NULL)
+ (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
+
+ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
+ }
+
+ else if (chunk_name == png_IEND)
+ {
+ PNG_PUSH_SAVE_BUFFER_IF_FULL
+ png_warning(png_ptr, "Number of actual frames fewer than expected");
+ png_crc_finish(png_ptr, png_ptr->push_length);
+ png_ptr->process_mode = PNG_READ_DONE_MODE;
+ png_push_have_end(png_ptr, info_ptr);
+ }
+
+ else
+ {
+ PNG_PUSH_SAVE_BUFFER_IF_FULL
+ png_warning(png_ptr, "Skipped (ignored) a chunk "
+ "between APNG chunks");
+ png_crc_finish(png_ptr, png_ptr->push_length);
+ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
+ }
+
+ return;
+ }
+#endif /* PNG_READ_APNG_SUPPORTED */
+