tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

apng.patch (49281B)


      1 diff --git a/png.h b/png.h
      2 --- a/png.h
      3 +++ b/png.h
      4 @@ -328,6 +328,10 @@
      5 #   include "pnglibconf.h"
      6 #endif
      7 
      8 +#define PNG_APNG_SUPPORTED
      9 +#define PNG_READ_APNG_SUPPORTED
     10 +#define PNG_WRITE_APNG_SUPPORTED
     11 +
     12 #ifndef PNG_VERSION_INFO_ONLY
     13 /* Machine specific configuration. */
     14 #  include "pngconf.h"
     15 @@ -423,6 +427,17 @@ extern "C" {
     16  * See pngconf.h for base types that vary by machine/system
     17  */
     18 
     19 +#ifdef PNG_APNG_SUPPORTED
     20 +/* dispose_op flags from inside fcTL */
     21 +#define PNG_DISPOSE_OP_NONE        0x00
     22 +#define PNG_DISPOSE_OP_BACKGROUND  0x01
     23 +#define PNG_DISPOSE_OP_PREVIOUS    0x02
     24 +
     25 +/* blend_op flags from inside fcTL */
     26 +#define PNG_BLEND_OP_SOURCE        0x00
     27 +#define PNG_BLEND_OP_OVER          0x01
     28 +#endif /* APNG */
     29 +
     30 /* This triggers a compiler error in png.c, if png.c and png.h
     31  * do not agree upon the version number.
     32  */
     33 @@ -796,6 +811,10 @@ typedef PNG_CALLBACK(void, *png_write_st
     34 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
     35 typedef PNG_CALLBACK(void, *png_progressive_info_ptr, (png_structp, png_infop));
     36 typedef PNG_CALLBACK(void, *png_progressive_end_ptr, (png_structp, png_infop));
     37 +#ifdef PNG_APNG_SUPPORTED
     38 +typedef PNG_CALLBACK(void, *png_progressive_frame_ptr, (png_structp,
     39 +    png_uint_32));
     40 +#endif
     41 
     42 /* The following callback receives png_uint_32 row_number, int pass for the
     43  * png_bytep data of the row.  When transforming an interlaced image the
     44 @@ -3338,6 +3357,75 @@ PNG_EXPORT(244, int, png_set_option, (pn
     45  *  END OF HARDWARE AND SOFTWARE OPTIONS
     46  ******************************************************************************/
     47 
     48 +#ifdef PNG_APNG_SUPPORTED
     49 +PNG_EXPORT(260, png_uint_32, png_get_acTL, (png_structp png_ptr,
     50 +   png_infop info_ptr, png_uint_32 *num_frames, png_uint_32 *num_plays));
     51 +
     52 +PNG_EXPORT(261, png_uint_32, png_set_acTL, (png_structp png_ptr,
     53 +   png_infop info_ptr, png_uint_32 num_frames, png_uint_32 num_plays));
     54 +
     55 +PNG_EXPORT(262, png_uint_32, png_get_num_frames, (png_structp png_ptr,
     56 +   png_infop info_ptr));
     57 +
     58 +PNG_EXPORT(263, png_uint_32, png_get_num_plays, (png_structp png_ptr,
     59 +   png_infop info_ptr));
     60 +
     61 +PNG_EXPORT(264, png_uint_32, png_get_next_frame_fcTL,
     62 +   (png_structp png_ptr, png_infop info_ptr, png_uint_32 *width,
     63 +   png_uint_32 *height, png_uint_32 *x_offset, png_uint_32 *y_offset,
     64 +   png_uint_16 *delay_num, png_uint_16 *delay_den, png_byte *dispose_op,
     65 +   png_byte *blend_op));
     66 +
     67 +PNG_EXPORT(265, png_uint_32, png_set_next_frame_fcTL,
     68 +   (png_structp png_ptr, png_infop info_ptr, png_uint_32 width,
     69 +   png_uint_32 height, png_uint_32 x_offset, png_uint_32 y_offset,
     70 +   png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
     71 +   png_byte blend_op));
     72 +
     73 +PNG_EXPORT(266, png_uint_32, png_get_next_frame_width,
     74 +   (png_structp png_ptr, png_infop info_ptr));
     75 +PNG_EXPORT(267, png_uint_32, png_get_next_frame_height,
     76 +   (png_structp png_ptr, png_infop info_ptr));
     77 +PNG_EXPORT(268, png_uint_32, png_get_next_frame_x_offset,
     78 +   (png_structp png_ptr, png_infop info_ptr));
     79 +PNG_EXPORT(269, png_uint_32, png_get_next_frame_y_offset,
     80 +   (png_structp png_ptr, png_infop info_ptr));
     81 +PNG_EXPORT(270, png_uint_16, png_get_next_frame_delay_num,
     82 +   (png_structp png_ptr, png_infop info_ptr));
     83 +PNG_EXPORT(271, png_uint_16, png_get_next_frame_delay_den,
     84 +   (png_structp png_ptr, png_infop info_ptr));
     85 +PNG_EXPORT(272, png_byte, png_get_next_frame_dispose_op,
     86 +   (png_structp png_ptr, png_infop info_ptr));
     87 +PNG_EXPORT(273, png_byte, png_get_next_frame_blend_op,
     88 +   (png_structp png_ptr, png_infop info_ptr));
     89 +PNG_EXPORT(274, png_byte, png_get_first_frame_is_hidden,
     90 +   (png_structp png_ptr, png_infop info_ptr));
     91 +PNG_EXPORT(275, png_uint_32, png_set_first_frame_is_hidden,
     92 +   (png_structp png_ptr, png_infop info_ptr, png_byte is_hidden));
     93 +
     94 +#ifdef PNG_READ_APNG_SUPPORTED
     95 +PNG_EXPORT(276, void, png_read_frame_head, (png_structp png_ptr,
     96 +   png_infop info_ptr));
     97 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
     98 +PNG_EXPORT(277, void, png_set_progressive_frame_fn, (png_structp png_ptr,
     99 +   png_progressive_frame_ptr frame_info_fn,
    100 +   png_progressive_frame_ptr frame_end_fn));
    101 +#endif /* PROGRESSIVE_READ */
    102 +#endif /* READ_APNG */
    103 +
    104 +#ifdef PNG_WRITE_APNG_SUPPORTED
    105 +PNG_EXPORT(278, void, png_write_frame_head, (png_structp png_ptr,
    106 +   png_infop info_ptr, png_bytepp row_pointers,
    107 +   png_uint_32 width, png_uint_32 height,
    108 +   png_uint_32 x_offset, png_uint_32 y_offset,
    109 +   png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
    110 +   png_byte blend_op));
    111 +
    112 +PNG_EXPORT(279, void, png_write_frame_tail, (png_structp png_ptr,
    113 +   png_infop info_ptr));
    114 +#endif /* WRITE_APNG */
    115 +#endif /* APNG */
    116 +
    117 /* Maintainer: Put new public prototypes here ^, in libpng.3, in project
    118  * defs, and in scripts/symbols.def.
    119  */
    120 @@ -3346,7 +3434,11 @@ PNG_EXPORT(244, int, png_set_option, (pn
    121  * one to use is one more than this.)
    122  */
    123 #ifdef PNG_EXPORT_LAST_ORDINAL
    124 +#ifdef PNG_APNG_SUPPORTED
    125 +  PNG_EXPORT_LAST_ORDINAL(279);
    126 +#else
    127   PNG_EXPORT_LAST_ORDINAL(259);
    128 +#endif /* APNG */
    129 #endif
    130 
    131 #ifdef __cplusplus
    132 diff --git a/pngget.c b/pngget.c
    133 --- a/pngget.c
    134 +++ b/pngget.c
    135 @@ -1367,4 +1367,166 @@ png_get_palette_max(png_const_structp pn
    136 #  endif
    137 #endif
    138 
    139 +#ifdef PNG_APNG_SUPPORTED
    140 +png_uint_32 PNGAPI
    141 +png_get_acTL(png_structp png_ptr, png_infop info_ptr,
    142 +             png_uint_32 *num_frames, png_uint_32 *num_plays)
    143 +{
    144 +    png_debug1(1, "in %s retrieval function", "acTL");
    145 +
    146 +    if (png_ptr != NULL && info_ptr != NULL &&
    147 +        (info_ptr->valid & PNG_INFO_acTL) != 0 &&
    148 +        num_frames != NULL && num_plays != NULL)
    149 +    {
    150 +        *num_frames = info_ptr->num_frames;
    151 +        *num_plays = info_ptr->num_plays;
    152 +        return (1);
    153 +    }
    154 +
    155 +    return (0);
    156 +}
    157 +
    158 +png_uint_32 PNGAPI
    159 +png_get_num_frames(png_structp png_ptr, png_infop info_ptr)
    160 +{
    161 +    png_debug(1, "in png_get_num_frames()");
    162 +
    163 +    if (png_ptr != NULL && info_ptr != NULL)
    164 +        return (info_ptr->num_frames);
    165 +    return (0);
    166 +}
    167 +
    168 +png_uint_32 PNGAPI
    169 +png_get_num_plays(png_structp png_ptr, png_infop info_ptr)
    170 +{
    171 +    png_debug(1, "in png_get_num_plays()");
    172 +
    173 +    if (png_ptr != NULL && info_ptr != NULL)
    174 +        return (info_ptr->num_plays);
    175 +    return (0);
    176 +}
    177 +
    178 +png_uint_32 PNGAPI
    179 +png_get_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
    180 +             png_uint_32 *width, png_uint_32 *height,
    181 +             png_uint_32 *x_offset, png_uint_32 *y_offset,
    182 +             png_uint_16 *delay_num, png_uint_16 *delay_den,
    183 +             png_byte *dispose_op, png_byte *blend_op)
    184 +{
    185 +    png_debug1(1, "in %s retrieval function", "fcTL");
    186 +
    187 +    if (png_ptr != NULL && info_ptr != NULL &&
    188 +        (info_ptr->valid & PNG_INFO_fcTL) != 0 &&
    189 +        width != NULL && height != NULL &&
    190 +        x_offset != NULL && y_offset != NULL &&
    191 +        delay_num != NULL && delay_den != NULL &&
    192 +        dispose_op != NULL && blend_op != NULL)
    193 +    {
    194 +        *width = info_ptr->next_frame_width;
    195 +        *height = info_ptr->next_frame_height;
    196 +        *x_offset = info_ptr->next_frame_x_offset;
    197 +        *y_offset = info_ptr->next_frame_y_offset;
    198 +        *delay_num = info_ptr->next_frame_delay_num;
    199 +        *delay_den = info_ptr->next_frame_delay_den;
    200 +        *dispose_op = info_ptr->next_frame_dispose_op;
    201 +        *blend_op = info_ptr->next_frame_blend_op;
    202 +        return (1);
    203 +    }
    204 +
    205 +    return (0);
    206 +}
    207 +
    208 +png_uint_32 PNGAPI
    209 +png_get_next_frame_width(png_structp png_ptr, png_infop info_ptr)
    210 +{
    211 +    png_debug(1, "in png_get_next_frame_width()");
    212 +
    213 +    if (png_ptr != NULL && info_ptr != NULL)
    214 +        return (info_ptr->next_frame_width);
    215 +    return (0);
    216 +}
    217 +
    218 +png_uint_32 PNGAPI
    219 +png_get_next_frame_height(png_structp png_ptr, png_infop info_ptr)
    220 +{
    221 +    png_debug(1, "in png_get_next_frame_height()");
    222 +
    223 +    if (png_ptr != NULL && info_ptr != NULL)
    224 +        return (info_ptr->next_frame_height);
    225 +    return (0);
    226 +}
    227 +
    228 +png_uint_32 PNGAPI
    229 +png_get_next_frame_x_offset(png_structp png_ptr, png_infop info_ptr)
    230 +{
    231 +    png_debug(1, "in png_get_next_frame_x_offset()");
    232 +
    233 +    if (png_ptr != NULL && info_ptr != NULL)
    234 +        return (info_ptr->next_frame_x_offset);
    235 +    return (0);
    236 +}
    237 +
    238 +png_uint_32 PNGAPI
    239 +png_get_next_frame_y_offset(png_structp png_ptr, png_infop info_ptr)
    240 +{
    241 +    png_debug(1, "in png_get_next_frame_y_offset()");
    242 +
    243 +    if (png_ptr != NULL && info_ptr != NULL)
    244 +        return (info_ptr->next_frame_y_offset);
    245 +    return (0);
    246 +}
    247 +
    248 +png_uint_16 PNGAPI
    249 +png_get_next_frame_delay_num(png_structp png_ptr, png_infop info_ptr)
    250 +{
    251 +    png_debug(1, "in png_get_next_frame_delay_num()");
    252 +
    253 +    if (png_ptr != NULL && info_ptr != NULL)
    254 +        return (info_ptr->next_frame_delay_num);
    255 +    return (0);
    256 +}
    257 +
    258 +png_uint_16 PNGAPI
    259 +png_get_next_frame_delay_den(png_structp png_ptr, png_infop info_ptr)
    260 +{
    261 +    png_debug(1, "in png_get_next_frame_delay_den()");
    262 +
    263 +    if (png_ptr != NULL && info_ptr != NULL)
    264 +        return (info_ptr->next_frame_delay_den);
    265 +    return (0);
    266 +}
    267 +
    268 +png_byte PNGAPI
    269 +png_get_next_frame_dispose_op(png_structp png_ptr, png_infop info_ptr)
    270 +{
    271 +    png_debug(1, "in png_get_next_frame_dispose_op()");
    272 +
    273 +    if (png_ptr != NULL && info_ptr != NULL)
    274 +        return (info_ptr->next_frame_dispose_op);
    275 +    return (0);
    276 +}
    277 +
    278 +png_byte PNGAPI
    279 +png_get_next_frame_blend_op(png_structp png_ptr, png_infop info_ptr)
    280 +{
    281 +    png_debug(1, "in png_get_next_frame_blend_op()");
    282 +
    283 +    if (png_ptr != NULL && info_ptr != NULL)
    284 +        return (info_ptr->next_frame_blend_op);
    285 +    return (0);
    286 +}
    287 +
    288 +png_byte PNGAPI
    289 +png_get_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr)
    290 +{
    291 +    png_debug(1, "in png_first_frame_is_hidden()");
    292 +
    293 +    if (png_ptr != NULL)
    294 +       return (png_byte)(png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN);
    295 +
    296 +    PNG_UNUSED(info_ptr)
    297 +
    298 +    return 0;
    299 +}
    300 +#endif /* APNG */
    301 #endif /* READ || WRITE */
    302 diff --git a/pnginfo.h b/pnginfo.h
    303 --- a/pnginfo.h
    304 +++ b/pnginfo.h
    305 @@ -259,5 +259,18 @@ defined(PNG_READ_BACKGROUND_SUPPORTED)
    306 #ifdef PNG_sRGB_SUPPORTED
    307    int rendering_intent;
    308 #endif
    309 +#ifdef PNG_APNG_SUPPORTED
    310 +   png_uint_32 num_frames; /* including default image */
    311 +   png_uint_32 num_plays;
    312 +   png_uint_32 next_frame_width;
    313 +   png_uint_32 next_frame_height;
    314 +   png_uint_32 next_frame_x_offset;
    315 +   png_uint_32 next_frame_y_offset;
    316 +   png_uint_16 next_frame_delay_num;
    317 +   png_uint_16 next_frame_delay_den;
    318 +   png_byte next_frame_dispose_op;
    319 +   png_byte next_frame_blend_op;
    320 +#endif
    321 +
    322 };
    323 #endif /* PNGINFO_H */
    324 diff --git a/pngpread.c b/pngpread.c
    325 --- a/pngpread.c
    326 +++ b/pngpread.c
    327 @@ -200,6 +200,89 @@ png_push_read_chunk(png_structrp png_ptr
    328 
    329    chunk_name = png_ptr->chunk_name;
    330 
    331 +#ifdef PNG_READ_APNG_SUPPORTED
    332 +   if (png_ptr->num_frames_read > 0 &&
    333 +       png_ptr->num_frames_read < info_ptr->num_frames)
    334 +   {
    335 +      if (chunk_name == png_IDAT)
    336 +      {
    337 +         /* Discard trailing IDATs for the first frame */
    338 +         if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
    339 +             png_ptr->num_frames_read > 1)
    340 +            png_error(png_ptr, "out of place IDAT");
    341 +
    342 +         PNG_PUSH_SAVE_BUFFER_IF_FULL
    343 +         png_crc_finish(png_ptr, png_ptr->push_length);
    344 +         png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    345 +      }
    346 +
    347 +      else if (chunk_name == png_fdAT)
    348 +      {
    349 +         PNG_PUSH_SAVE_BUFFER_IF_LT(4)
    350 +         png_ensure_sequence_number(png_ptr, 4);
    351 +
    352 +         if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
    353 +         {
    354 +            /* Discard trailing fdATs for frames other than the first */
    355 +            if (png_ptr->num_frames_read < 2)
    356 +               png_error(png_ptr, "out of place fdAT");
    357 +
    358 +            PNG_PUSH_SAVE_BUFFER_IF_FULL
    359 +            png_crc_finish(png_ptr, png_ptr->push_length);
    360 +            png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    361 +         }
    362 +
    363 +         else
    364 +         {
    365 +            /* frame data follows */
    366 +            png_ptr->idat_size = png_ptr->push_length - 4;
    367 +            png_ptr->mode |= PNG_HAVE_IDAT;
    368 +            png_ptr->process_mode = PNG_READ_IDAT_MODE;
    369 +         }
    370 +      }
    371 +
    372 +      else if (chunk_name == png_fcTL)
    373 +      {
    374 +         PNG_PUSH_SAVE_BUFFER_IF_FULL
    375 +         png_read_reset(png_ptr);
    376 +         png_ptr->mode &= ~PNG_HAVE_fcTL;
    377 +
    378 +         png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
    379 +
    380 +         if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
    381 +            png_error(png_ptr, "missing required fcTL chunk");
    382 +
    383 +         png_read_reinit(png_ptr, info_ptr);
    384 +         png_progressive_read_reset(png_ptr);
    385 +
    386 +         if (png_ptr->frame_info_fn != NULL)
    387 +            (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
    388 +
    389 +         png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    390 +      }
    391 +
    392 +      else if (chunk_name == png_IEND)
    393 +      {
    394 +         PNG_PUSH_SAVE_BUFFER_IF_FULL
    395 +         png_warning(png_ptr, "Number of actual frames fewer than expected");
    396 +         png_crc_finish(png_ptr, png_ptr->push_length);
    397 +         png_ptr->process_mode = PNG_READ_DONE_MODE;
    398 +         png_push_have_end(png_ptr, info_ptr);
    399 +      }
    400 +
    401 +      else
    402 +      {
    403 +         PNG_PUSH_SAVE_BUFFER_IF_FULL
    404 +         png_warning(png_ptr, "Skipped (ignored) a chunk "
    405 +                              "between APNG chunks");
    406 +         png_crc_finish(png_ptr, png_ptr->push_length);
    407 +         png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    408 +      }
    409 +
    410 +      return;
    411 +   }
    412 +#endif /* READ_APNG */
    413 +
    414    if (chunk_name == png_IDAT)
    415    {
    416       if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
    417 @@ -260,6 +343,9 @@ png_push_read_chunk(png_structrp png_ptr
    418 
    419    else if (chunk_name == png_IDAT)
    420    {
    421 +#ifdef PNG_READ_APNG_SUPPORTED
    422 +      png_have_info(png_ptr, info_ptr);
    423 +#endif
    424       png_ptr->idat_size = png_ptr->push_length;
    425       png_ptr->process_mode = PNG_READ_IDAT_MODE;
    426       png_push_have_info(png_ptr, info_ptr);
    427 @@ -270,6 +356,20 @@ png_push_read_chunk(png_structrp png_ptr
    428       return;
    429    }
    430 
    431 +#ifdef PNG_READ_APNG_SUPPORTED
    432 +   else if (chunk_name == png_acTL)
    433 +   {
    434 +      PNG_PUSH_SAVE_BUFFER_IF_FULL
    435 +      png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
    436 +   }
    437 +
    438 +   else if (chunk_name == png_fcTL)
    439 +   {
    440 +      PNG_PUSH_SAVE_BUFFER_IF_FULL
    441 +      png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
    442 +   }
    443 +
    444 +#endif /* READ_APNG */
    445    else
    446    {
    447       PNG_PUSH_SAVE_BUFFER_IF_FULL
    448 @@ -401,7 +501,11 @@ png_push_read_IDAT(png_structrp png_ptr)
    449       png_byte chunk_tag[4];
    450 
    451       /* TODO: this code can be commoned up with the same code in push_read */
    452 +#ifdef PNG_READ_APNG_SUPPORTED
    453 +      PNG_PUSH_SAVE_BUFFER_IF_LT(12)
    454 +#else
    455       PNG_PUSH_SAVE_BUFFER_IF_LT(8)
    456 +#endif
    457       png_push_fill_buffer(png_ptr, chunk_length, 4);
    458       png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
    459       png_reset_crc(png_ptr);
    460 @@ -409,17 +513,60 @@ png_push_read_IDAT(png_structrp png_ptr)
    461       png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
    462       png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
    463 
    464 +#ifdef PNG_READ_APNG_SUPPORTED
    465 +      if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
    466 +      {
    467 +          if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) != 0)
    468 +          {
    469 +              png_ptr->process_mode = PNG_READ_CHUNK_MODE;
    470 +              if (png_ptr->frame_end_fn != NULL)
    471 +                 (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
    472 +              png_ptr->num_frames_read++;
    473 +              return;
    474 +          }
    475 +          else
    476 +          {
    477 +              if (png_ptr->chunk_name == png_IEND)
    478 +                  png_error(png_ptr, "Not enough image data");
    479 +              PNG_PUSH_SAVE_BUFFER_IF_FULL
    480 +              png_warning(png_ptr, "Skipping (ignoring) a chunk between "
    481 +                                   "APNG chunks");
    482 +              png_crc_finish(png_ptr, png_ptr->push_length);
    483 +              png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    484 +              return;
    485 +          }
    486 +      }
    487 +      else
    488 +#endif
    489 +#ifdef PNG_READ_APNG_SUPPORTED
    490 +      if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
    491 +#else
    492       if (png_ptr->chunk_name != png_IDAT)
    493 +#endif
    494       {
    495          png_ptr->process_mode = PNG_READ_CHUNK_MODE;
    496 
    497          if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
    498             png_error(png_ptr, "Not enough compressed data");
    499 
    500 +#ifdef PNG_READ_APNG_SUPPORTED
    501 +         if (png_ptr->frame_end_fn != NULL)
    502 +            (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
    503 +         png_ptr->num_frames_read++;
    504 +#endif
    505 +
    506          return;
    507       }
    508 
    509       png_ptr->idat_size = png_ptr->push_length;
    510 +
    511 +#ifdef PNG_READ_APNG_SUPPORTED
    512 +      if (png_ptr->num_frames_read > 0)
    513 +      {
    514 +         png_ensure_sequence_number(png_ptr, 4);
    515 +         png_ptr->idat_size -= 4;
    516 +      }
    517 +#endif
    518    }
    519 
    520    if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
    521 @@ -493,6 +640,16 @@ png_process_IDAT_data(png_structrp png_p
    522    if (!(buffer_length > 0) || buffer == NULL)
    523       png_error(png_ptr, "No IDAT data (internal error)");
    524 
    525 +#ifdef PNG_READ_APNG_SUPPORTED
    526 +   /* If the app is not APNG-aware, decode only the first frame */
    527 +   if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
    528 +      png_ptr->num_frames_read > 0)
    529 +   {
    530 +      png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
    531 +      return;
    532 +   }
    533 +#endif
    534 +
    535    /* This routine must process all the data it has been given
    536     * before returning, calling the row callback as required to
    537     * handle the uncompressed results.
    538 @@ -926,6 +1083,18 @@ png_set_progressive_read_fn(png_structrp
    539    png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
    540 }
    541 
    542 +#ifdef PNG_READ_APNG_SUPPORTED
    543 +void PNGAPI
    544 +png_set_progressive_frame_fn(png_structp png_ptr,
    545 +   png_progressive_frame_ptr frame_info_fn,
    546 +   png_progressive_frame_ptr frame_end_fn)
    547 +{
    548 +   png_ptr->frame_info_fn = frame_info_fn;
    549 +   png_ptr->frame_end_fn = frame_end_fn;
    550 +   png_ptr->apng_flags |= PNG_APNG_APP;
    551 +}
    552 +#endif
    553 +
    554 png_voidp PNGAPI
    555 png_get_progressive_ptr(png_const_structrp png_ptr)
    556 {
    557 diff --git a/pngpriv.h b/pngpriv.h
    558 --- a/pngpriv.h
    559 +++ b/pngpriv.h
    560 @@ -620,6 +620,10 @@
    561 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000U /* Have another chunk after IDAT */
    562 #define PNG_WROTE_eXIf            0x4000U
    563 #define PNG_IS_READ_STRUCT        0x8000U /* Else is a write struct */
    564 +#ifdef PNG_APNG_SUPPORTED
    565 +#define PNG_HAVE_acTL            0x10000U
    566 +#define PNG_HAVE_fcTL            0x20000U
    567 +#endif
    568 
    569 /* Flags for the transformations the PNG library does on the image data */
    570 #define PNG_BGR                 0x0001U
    571 @@ -884,6 +888,16 @@
    572 #define png_tRNS PNG_U32(116,  82,  78,  83)
    573 #define png_zTXt PNG_U32(122,  84,  88, 116)
    574 
    575 +#ifdef PNG_APNG_SUPPORTED
    576 +#define png_acTL PNG_U32( 97,  99,  84,  76)
    577 +#define png_fcTL PNG_U32(102,  99,  84,  76)
    578 +#define png_fdAT PNG_U32(102, 100,  65,  84)
    579 +
    580 +/* For png_struct.apng_flags: */
    581 +#define PNG_FIRST_FRAME_HIDDEN       0x0001U
    582 +#define PNG_APNG_APP                 0x0002U
    583 +#endif
    584 +
    585 /* The following will work on (signed char*) strings, whereas the get_uint_32
    586  * macro will fail on top-bit-set values because of the sign extension.
    587  */
    588 @@ -1671,6 +1685,49 @@ PNG_INTERNAL_FUNCTION(void,png_read_push
    589     PNG_EMPTY);
    590 #endif /* PROGRESSIVE_READ */
    591 
    592 +#ifdef PNG_APNG_SUPPORTED
    593 +PNG_INTERNAL_FUNCTION(void,png_ensure_fcTL_is_valid,(png_structp png_ptr,
    594 +   png_uint_32 width, png_uint_32 height,
    595 +   png_uint_32 x_offset, png_uint_32 y_offset,
    596 +   png_uint_16 delay_num, png_uint_16 delay_den,
    597 +   png_byte dispose_op, png_byte blend_op),PNG_EMPTY);
    598 +
    599 +#ifdef PNG_READ_APNG_SUPPORTED
    600 +PNG_INTERNAL_FUNCTION(void,png_handle_acTL,(png_structp png_ptr,
    601 +   png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
    602 +PNG_INTERNAL_FUNCTION(void,png_handle_fcTL,(png_structp png_ptr,
    603 +   png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
    604 +PNG_INTERNAL_FUNCTION(void,png_handle_fdAT,(png_structp png_ptr,
    605 +   png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
    606 +PNG_INTERNAL_FUNCTION(void,png_have_info,(png_structp png_ptr,
    607 +   png_infop info_ptr),PNG_EMPTY);
    608 +PNG_INTERNAL_FUNCTION(void,png_ensure_sequence_number,(png_structp png_ptr,
    609 +   png_uint_32 length),PNG_EMPTY);
    610 +PNG_INTERNAL_FUNCTION(void,png_read_reset,(png_structp png_ptr),PNG_EMPTY);
    611 +PNG_INTERNAL_FUNCTION(void,png_read_reinit,(png_structp png_ptr,
    612 +   png_infop info_ptr),PNG_EMPTY);
    613 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
    614 +PNG_INTERNAL_FUNCTION(void,png_progressive_read_reset,(png_structp png_ptr),
    615 +   PNG_EMPTY);
    616 +#endif /* PROGRESSIVE_READ */
    617 +#endif /* READ_APNG */
    618 +
    619 +#ifdef PNG_WRITE_APNG_SUPPORTED
    620 +PNG_INTERNAL_FUNCTION(void,png_write_acTL,(png_structp png_ptr,
    621 +   png_uint_32 num_frames, png_uint_32 num_plays),PNG_EMPTY);
    622 +PNG_INTERNAL_FUNCTION(void,png_write_fcTL,(png_structp png_ptr,
    623 +   png_uint_32 width, png_uint_32 height,
    624 +   png_uint_32 x_offset, png_uint_32 y_offset,
    625 +   png_uint_16 delay_num, png_uint_16 delay_den,
    626 +   png_byte dispose_op, png_byte blend_op),PNG_EMPTY);
    627 +PNG_INTERNAL_FUNCTION(void,png_write_fdAT,(png_structp png_ptr,
    628 +   png_const_bytep data, png_size_t length),PNG_EMPTY);
    629 +PNG_INTERNAL_FUNCTION(void,png_write_reset,(png_structp png_ptr),PNG_EMPTY);
    630 +PNG_INTERNAL_FUNCTION(void,png_write_reinit,(png_structp png_ptr,
    631 +   png_infop info_ptr, png_uint_32 width, png_uint_32 height),PNG_EMPTY);
    632 +#endif /* WRITE_APNG */
    633 +#endif /* APNG */
    634 +
    635 #ifdef PNG_iCCP_SUPPORTED
    636 /* Routines for checking parts of an ICC profile. */
    637 #ifdef PNG_READ_iCCP_SUPPORTED
    638 diff --git a/pngread.c b/pngread.c
    639 --- a/pngread.c
    640 +++ b/pngread.c
    641 @@ -155,16 +155,96 @@ png_read_info(png_structrp png_ptr, png_
    642 
    643       else if (chunk_name == png_IDAT)
    644       {
    645 +#ifdef PNG_READ_APNG_SUPPORTED
    646 +         png_have_info(png_ptr, info_ptr);
    647 +#endif
    648          png_ptr->idat_size = length;
    649          break;
    650       }
    651 
    652 +#ifdef PNG_READ_APNG_SUPPORTED
    653 +      else if (chunk_name == png_acTL)
    654 +         png_handle_acTL(png_ptr, info_ptr, length);
    655 +
    656 +      else if (chunk_name == png_fcTL)
    657 +         png_handle_fcTL(png_ptr, info_ptr, length);
    658 +
    659 +      else if (chunk_name == png_fdAT)
    660 +         png_handle_fdAT(png_ptr, info_ptr, length);
    661 +#endif
    662 +
    663       else
    664          png_handle_chunk(png_ptr, info_ptr, length);
    665    }
    666 }
    667 #endif /* SEQUENTIAL_READ */
    668 
    669 +#ifdef PNG_READ_APNG_SUPPORTED
    670 +void PNGAPI
    671 +png_read_frame_head(png_structp png_ptr, png_infop info_ptr)
    672 +{
    673 +    png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */
    674 +
    675 +    png_debug(0, "Reading frame head");
    676 +
    677 +    if ((png_ptr->mode & PNG_HAVE_acTL) == 0)
    678 +        png_error(png_ptr, "attempt to png_read_frame_head() but "
    679 +                           "no acTL present");
    680 +
    681 +    /* do nothing for the main IDAT */
    682 +    if (png_ptr->num_frames_read == 0)
    683 +        return;
    684 +
    685 +    png_read_reset(png_ptr);
    686 +    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
    687 +    png_ptr->mode &= ~PNG_HAVE_fcTL;
    688 +
    689 +    have_chunk_after_DAT = 0;
    690 +    for (;;)
    691 +    {
    692 +        png_uint_32 length = png_read_chunk_header(png_ptr);
    693 +
    694 +        if (png_ptr->chunk_name == png_IDAT)
    695 +        {
    696 +            /* discard trailing IDATs for the first frame */
    697 +            if (have_chunk_after_DAT != 0 || png_ptr->num_frames_read > 1)
    698 +                png_error(png_ptr, "png_read_frame_head(): out of place IDAT");
    699 +            png_crc_finish(png_ptr, length);
    700 +        }
    701 +
    702 +        else if (png_ptr->chunk_name == png_fcTL)
    703 +        {
    704 +            png_handle_fcTL(png_ptr, info_ptr, length);
    705 +            have_chunk_after_DAT = 1;
    706 +        }
    707 +
    708 +        else if (png_ptr->chunk_name == png_fdAT)
    709 +        {
    710 +            png_ensure_sequence_number(png_ptr, length);
    711 +
    712 +            /* discard trailing fdATs for frames other than the first */
    713 +            if (have_chunk_after_DAT == 0 && png_ptr->num_frames_read > 1)
    714 +                png_crc_finish(png_ptr, length - 4);
    715 +            else if (png_ptr->mode & PNG_HAVE_fcTL)
    716 +            {
    717 +                png_ptr->idat_size = length - 4;
    718 +                png_ptr->mode |= PNG_HAVE_IDAT;
    719 +
    720 +                break;
    721 +            }
    722 +            else
    723 +                png_error(png_ptr, "png_read_frame_head(): out of place fdAT");
    724 +        }
    725 +        else
    726 +        {
    727 +            png_warning(png_ptr, "Skipped (ignored) a chunk "
    728 +                                 "between APNG chunks");
    729 +            png_crc_finish(png_ptr, length);
    730 +        }
    731 +    }
    732 +}
    733 +#endif /* READ_APNG */
    734 +
    735 /* Optional call to update the users info_ptr structure */
    736 void PNGAPI
    737 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
    738 diff --git a/pngrutil.c b/pngrutil.c
    739 --- a/pngrutil.c
    740 +++ b/pngrutil.c
    741 @@ -922,6 +922,11 @@ png_handle_IHDR(png_structrp png_ptr, pn
    742    filter_type = buf[11];
    743    interlace_type = buf[12];
    744 
    745 +#ifdef PNG_READ_APNG_SUPPORTED
    746 +   png_ptr->first_frame_width = width;
    747 +   png_ptr->first_frame_height = height;
    748 +#endif
    749 +
    750    /* Set internal variables */
    751    png_ptr->width = width;
    752    png_ptr->height = height;
    753 @@ -2730,6 +2735,180 @@ png_handle_iTXt(png_structrp png_ptr, pn
    754 #  define png_handle_iTXt NULL
    755 #endif
    756 
    757 +#ifdef PNG_READ_APNG_SUPPORTED
    758 +void /* PRIVATE */
    759 +png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    760 +{
    761 +    png_byte data[8];
    762 +    png_uint_32 num_frames;
    763 +    png_uint_32 num_plays;
    764 +    png_uint_32 didSet;
    765 +
    766 +    png_debug(1, "in png_handle_acTL");
    767 +
    768 +    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
    769 +    {
    770 +        png_error(png_ptr, "Missing IHDR before acTL");
    771 +    }
    772 +    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
    773 +    {
    774 +        png_warning(png_ptr, "Invalid acTL after IDAT skipped");
    775 +        png_crc_finish(png_ptr, length);
    776 +        return;
    777 +    }
    778 +    else if ((png_ptr->mode & PNG_HAVE_acTL) != 0)
    779 +    {
    780 +        png_warning(png_ptr, "Duplicate acTL skipped");
    781 +        png_crc_finish(png_ptr, length);
    782 +        return;
    783 +    }
    784 +    else if (length != 8)
    785 +    {
    786 +        png_warning(png_ptr, "acTL with invalid length skipped");
    787 +        png_crc_finish(png_ptr, length);
    788 +        return;
    789 +    }
    790 +
    791 +    png_crc_read(png_ptr, data, 8);
    792 +    png_crc_finish(png_ptr, 0);
    793 +
    794 +    num_frames = png_get_uint_31(png_ptr, data);
    795 +    num_plays = png_get_uint_31(png_ptr, data + 4);
    796 +
    797 +    /* the set function will do error checking on num_frames */
    798 +    didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
    799 +    if (didSet != 0)
    800 +        png_ptr->mode |= PNG_HAVE_acTL;
    801 +}
    802 +
    803 +void /* PRIVATE */
    804 +png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    805 +{
    806 +    png_byte data[22];
    807 +    png_uint_32 width;
    808 +    png_uint_32 height;
    809 +    png_uint_32 x_offset;
    810 +    png_uint_32 y_offset;
    811 +    png_uint_16 delay_num;
    812 +    png_uint_16 delay_den;
    813 +    png_byte dispose_op;
    814 +    png_byte blend_op;
    815 +
    816 +    png_debug(1, "in png_handle_fcTL");
    817 +
    818 +    png_ensure_sequence_number(png_ptr, length);
    819 +
    820 +    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
    821 +    {
    822 +        png_error(png_ptr, "Missing IHDR before fcTL");
    823 +    }
    824 +    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
    825 +    {
    826 +        /* for any frames other then the first this message may be misleading,
    827 +        * but correct. PNG_HAVE_IDAT is unset before the frame head is read
    828 +        * i can't think of a better message */
    829 +        png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
    830 +        png_crc_finish(png_ptr, length-4);
    831 +        return;
    832 +    }
    833 +    else if ((png_ptr->mode & PNG_HAVE_fcTL) != 0)
    834 +    {
    835 +        png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
    836 +        png_crc_finish(png_ptr, length-4);
    837 +        return;
    838 +    }
    839 +    else if (length != 26)
    840 +    {
    841 +        png_warning(png_ptr, "fcTL with invalid length skipped");
    842 +        png_crc_finish(png_ptr, length-4);
    843 +        return;
    844 +    }
    845 +
    846 +    png_crc_read(png_ptr, data, 22);
    847 +    png_crc_finish(png_ptr, 0);
    848 +
    849 +    width = png_get_uint_31(png_ptr, data);
    850 +    height = png_get_uint_31(png_ptr, data + 4);
    851 +    x_offset = png_get_uint_31(png_ptr, data + 8);
    852 +    y_offset = png_get_uint_31(png_ptr, data + 12);
    853 +    delay_num = png_get_uint_16(data + 16);
    854 +    delay_den = png_get_uint_16(data + 18);
    855 +    dispose_op = data[20];
    856 +    blend_op = data[21];
    857 +
    858 +    if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
    859 +    {
    860 +        png_warning(png_ptr, "fcTL for the first frame must have zero offset");
    861 +        return;
    862 +    }
    863 +
    864 +    if (info_ptr != NULL)
    865 +    {
    866 +        if (png_ptr->num_frames_read == 0 &&
    867 +            (width != info_ptr->width || height != info_ptr->height))
    868 +        {
    869 +            png_warning(png_ptr, "size in first frame's fcTL must match "
    870 +                               "the size in IHDR");
    871 +            return;
    872 +        }
    873 +
    874 +        /* The set function will do more error checking */
    875 +        png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
    876 +                                x_offset, y_offset, delay_num, delay_den,
    877 +                                dispose_op, blend_op);
    878 +
    879 +        png_read_reinit(png_ptr, info_ptr);
    880 +
    881 +        png_ptr->mode |= PNG_HAVE_fcTL;
    882 +    }
    883 +}
    884 +
    885 +void /* PRIVATE */
    886 +png_have_info(png_structp png_ptr, png_infop info_ptr)
    887 +{
    888 +    if ((info_ptr->valid & PNG_INFO_acTL) != 0 &&
    889 +        (info_ptr->valid & PNG_INFO_fcTL) == 0)
    890 +    {
    891 +        png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
    892 +        info_ptr->num_frames++;
    893 +    }
    894 +}
    895 +
    896 +void /* PRIVATE */
    897 +png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
    898 +{
    899 +    png_ensure_sequence_number(png_ptr, length);
    900 +
    901 +    /* This function is only called from png_read_end(), png_read_info(),
    902 +    * and png_push_read_chunk() which means that:
    903 +    * - the user doesn't want to read this frame
    904 +    * - or this is an out-of-place fdAT
    905 +    * in either case it is safe to ignore the chunk with a warning */
    906 +    png_warning(png_ptr, "ignoring fdAT chunk");
    907 +    png_crc_finish(png_ptr, length - 4);
    908 +    PNG_UNUSED(info_ptr)
    909 +}
    910 +
    911 +void /* PRIVATE */
    912 +png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
    913 +{
    914 +    png_byte data[4];
    915 +    png_uint_32 sequence_number;
    916 +
    917 +    if (length < 4)
    918 +        png_error(png_ptr, "invalid fcTL or fdAT chunk found");
    919 +
    920 +    png_crc_read(png_ptr, data, 4);
    921 +    sequence_number = png_get_uint_31(png_ptr, data);
    922 +
    923 +    if (sequence_number != png_ptr->next_seq_num)
    924 +        png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
    925 +                           "number found");
    926 +
    927 +    png_ptr->next_seq_num++;
    928 +}
    929 +#endif /* READ_APNG */
    930 +
    931 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
    932 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
    933 static int
    934 @@ -4213,6 +4392,38 @@ png_read_IDAT_data(png_structrp png_ptr,
    935          uInt avail_in;
    936          png_bytep buffer;
    937 
    938 +#ifdef PNG_READ_APNG_SUPPORTED
    939 +         png_uint_32 bytes_to_skip = 0;
    940 +
    941 +         while (png_ptr->idat_size == 0 || bytes_to_skip != 0)
    942 +         {
    943 +            png_crc_finish(png_ptr, bytes_to_skip);
    944 +            bytes_to_skip = 0;
    945 +
    946 +            png_ptr->idat_size = png_read_chunk_header(png_ptr);
    947 +            if (png_ptr->num_frames_read == 0)
    948 +            {
    949 +               if (png_ptr->chunk_name != png_IDAT)
    950 +                  png_error(png_ptr, "Not enough image data");
    951 +            }
    952 +            else
    953 +            {
    954 +               if (png_ptr->chunk_name == png_IEND)
    955 +                  png_error(png_ptr, "Not enough image data");
    956 +               if (png_ptr->chunk_name != png_fdAT)
    957 +               {
    958 +                  png_warning(png_ptr, "Skipped (ignored) a chunk "
    959 +                                       "between APNG chunks");
    960 +                  bytes_to_skip = png_ptr->idat_size;
    961 +                  continue;
    962 +               }
    963 +
    964 +               png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
    965 +
    966 +               png_ptr->idat_size -= 4;
    967 +            }
    968 +         }
    969 +#else
    970          while (png_ptr->idat_size == 0)
    971          {
    972             png_crc_finish(png_ptr, 0);
    973 @@ -4224,6 +4435,7 @@ png_read_IDAT_data(png_structrp png_ptr,
    974             if (png_ptr->chunk_name != png_IDAT)
    975                png_error(png_ptr, "Not enough image data");
    976          }
    977 +#endif /* READ_APNG */
    978 
    979          avail_in = png_ptr->IDAT_read_size;
    980 
    981 @@ -4295,6 +4507,9 @@ png_read_IDAT_data(png_structrp png_ptr,
    982 
    983          png_ptr->mode |= PNG_AFTER_IDAT;
    984          png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
    985 +#ifdef PNG_READ_APNG_SUPPORTED
    986 +         png_ptr->num_frames_read++;
    987 +#endif
    988 
    989          if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
    990             png_chunk_benign_error(png_ptr, "Extra compressed data");
    991 @@ -4704,4 +4919,80 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED
    992 
    993    png_ptr->flags |= PNG_FLAG_ROW_INIT;
    994 }
    995 +
    996 +#ifdef PNG_READ_APNG_SUPPORTED
    997 +/* This function is to be called after the main IDAT set has been read and
    998 + * before a new IDAT is read. It resets some parts of png_ptr
    999 + * to make them usable by the read functions again */
   1000 +void /* PRIVATE */
   1001 +png_read_reset(png_structp png_ptr)
   1002 +{
   1003 +    png_ptr->mode &= ~PNG_HAVE_IDAT;
   1004 +    png_ptr->mode &= ~PNG_AFTER_IDAT;
   1005 +    png_ptr->row_number = 0;
   1006 +    png_ptr->pass = 0;
   1007 +}
   1008 +
   1009 +void /* PRIVATE */
   1010 +png_read_reinit(png_structp png_ptr, png_infop info_ptr)
   1011 +{
   1012 +    png_ptr->width = info_ptr->next_frame_width;
   1013 +    png_ptr->height = info_ptr->next_frame_height;
   1014 +    png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
   1015 +    png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,
   1016 +        png_ptr->width);
   1017 +    if (png_ptr->prev_row != NULL)
   1018 +        memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
   1019 +}
   1020 +
   1021 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   1022 +/* same as png_read_reset() but for the progressive reader */
   1023 +void /* PRIVATE */
   1024 +png_progressive_read_reset(png_structp png_ptr)
   1025 +{
   1026 +#ifdef PNG_READ_INTERLACING_SUPPORTED
   1027 +    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
   1028 +
   1029 +    /* Start of interlace block */
   1030 +    static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
   1031 +
   1032 +    /* Offset to next interlace block */
   1033 +    static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
   1034 +
   1035 +    /* Start of interlace block in the y direction */
   1036 +    static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
   1037 +
   1038 +    /* Offset to next interlace block in the y direction */
   1039 +    static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
   1040 +
   1041 +    if (png_ptr->interlaced != 0)
   1042 +    {
   1043 +        if ((png_ptr->transformations & PNG_INTERLACE) == 0)
   1044 +            png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
   1045 +                                png_pass_ystart[0]) / png_pass_yinc[0];
   1046 +        else
   1047 +            png_ptr->num_rows = png_ptr->height;
   1048 +
   1049 +        png_ptr->iwidth = (png_ptr->width +
   1050 +                           png_pass_inc[png_ptr->pass] - 1 -
   1051 +                           png_pass_start[png_ptr->pass]) /
   1052 +                           png_pass_inc[png_ptr->pass];
   1053 +    }
   1054 +    else
   1055 +#endif /* READ_INTERLACING */
   1056 +    {
   1057 +        png_ptr->num_rows = png_ptr->height;
   1058 +        png_ptr->iwidth = png_ptr->width;
   1059 +    }
   1060 +    png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED;
   1061 +    if (inflateReset(&(png_ptr->zstream)) != Z_OK)
   1062 +        png_error(png_ptr, "inflateReset failed");
   1063 +    png_ptr->zstream.avail_in = 0;
   1064 +    png_ptr->zstream.next_in = 0;
   1065 +    png_ptr->zstream.next_out = png_ptr->row_buf;
   1066 +    png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
   1067 +        png_ptr->iwidth) + 1;
   1068 +}
   1069 +#endif /* PROGRESSIVE_READ */
   1070 +#endif /* READ_APNG */
   1071 #endif /* READ */
   1072 diff --git a/pngset.c b/pngset.c
   1073 --- a/pngset.c
   1074 +++ b/pngset.c
   1075 @@ -463,6 +463,11 @@ png_set_IHDR(png_const_structrp png_ptr,
   1076    info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
   1077 
   1078    info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
   1079 +
   1080 +#ifdef PNG_APNG_SUPPORTED
   1081 +   /* for non-animated png. this may be overwritten from an acTL chunk later */
   1082 +   info_ptr->num_frames = 1;
   1083 +#endif
   1084 }
   1085 
   1086 #ifdef PNG_oFFs_SUPPORTED
   1087 @@ -1318,6 +1323,146 @@ png_set_sPLT(png_const_structrp png_ptr,
   1088 }
   1089 #endif /* sPLT */
   1090 
   1091 +#ifdef PNG_APNG_SUPPORTED
   1092 +png_uint_32 PNGAPI
   1093 +png_set_acTL(png_structp png_ptr, png_infop info_ptr,
   1094 +    png_uint_32 num_frames, png_uint_32 num_plays)
   1095 +{
   1096 +    png_debug1(1, "in %s storage function", "acTL");
   1097 +
   1098 +    if (png_ptr == NULL || info_ptr == NULL)
   1099 +    {
   1100 +        png_warning(png_ptr,
   1101 +                    "Call to png_set_acTL() with NULL png_ptr "
   1102 +                    "or info_ptr ignored");
   1103 +        return (0);
   1104 +    }
   1105 +    if (num_frames == 0)
   1106 +    {
   1107 +        png_warning(png_ptr,
   1108 +                    "Ignoring attempt to set acTL with num_frames zero");
   1109 +        return (0);
   1110 +    }
   1111 +    if (num_frames > PNG_UINT_31_MAX)
   1112 +    {
   1113 +        png_warning(png_ptr,
   1114 +                    "Ignoring attempt to set acTL with num_frames > 2^31-1");
   1115 +        return (0);
   1116 +    }
   1117 +    if (num_plays > PNG_UINT_31_MAX)
   1118 +    {
   1119 +        png_warning(png_ptr,
   1120 +                    "Ignoring attempt to set acTL with num_plays > 2^31-1");
   1121 +        return (0);
   1122 +    }
   1123 +
   1124 +    info_ptr->num_frames = num_frames;
   1125 +    info_ptr->num_plays = num_plays;
   1126 +
   1127 +    info_ptr->valid |= PNG_INFO_acTL;
   1128 +
   1129 +    return (1);
   1130 +}
   1131 +
   1132 +/* delay_num and delay_den can hold any 16-bit values including zero */
   1133 +png_uint_32 PNGAPI
   1134 +png_set_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
   1135 +    png_uint_32 width, png_uint_32 height,
   1136 +    png_uint_32 x_offset, png_uint_32 y_offset,
   1137 +    png_uint_16 delay_num, png_uint_16 delay_den,
   1138 +    png_byte dispose_op, png_byte blend_op)
   1139 +{
   1140 +    png_debug1(1, "in %s storage function", "fcTL");
   1141 +
   1142 +    if (png_ptr == NULL || info_ptr == NULL)
   1143 +    {
   1144 +        png_warning(png_ptr,
   1145 +                    "Call to png_set_fcTL() with NULL png_ptr or info_ptr "
   1146 +                    "ignored");
   1147 +        return (0);
   1148 +    }
   1149 +
   1150 +    png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
   1151 +                             delay_num, delay_den, dispose_op, blend_op);
   1152 +
   1153 +    if (blend_op == PNG_BLEND_OP_OVER)
   1154 +    {
   1155 +        if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0 &&
   1156 +            png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) == 0)
   1157 +        {
   1158 +          png_warning(png_ptr, "PNG_BLEND_OP_OVER is meaningless "
   1159 +                               "and wasteful for opaque images, ignored");
   1160 +          blend_op = PNG_BLEND_OP_SOURCE;
   1161 +        }
   1162 +    }
   1163 +
   1164 +    info_ptr->next_frame_width = width;
   1165 +    info_ptr->next_frame_height = height;
   1166 +    info_ptr->next_frame_x_offset = x_offset;
   1167 +    info_ptr->next_frame_y_offset = y_offset;
   1168 +    info_ptr->next_frame_delay_num = delay_num;
   1169 +    info_ptr->next_frame_delay_den = delay_den;
   1170 +    info_ptr->next_frame_dispose_op = dispose_op;
   1171 +    info_ptr->next_frame_blend_op = blend_op;
   1172 +
   1173 +    info_ptr->valid |= PNG_INFO_fcTL;
   1174 +
   1175 +    return (1);
   1176 +}
   1177 +
   1178 +void /* PRIVATE */
   1179 +png_ensure_fcTL_is_valid(png_structp png_ptr,
   1180 +    png_uint_32 width, png_uint_32 height,
   1181 +    png_uint_32 x_offset, png_uint_32 y_offset,
   1182 +    png_uint_16 delay_num, png_uint_16 delay_den,
   1183 +    png_byte dispose_op, png_byte blend_op)
   1184 +{
   1185 +    if (width == 0 || width > PNG_UINT_31_MAX)
   1186 +        png_error(png_ptr, "invalid width in fcTL (0 or > 2^31-1)");
   1187 +    if (height == 0 || height > PNG_UINT_31_MAX)
   1188 +        png_error(png_ptr, "invalid height in fcTL (0 or > 2^31-1)");
   1189 +    if (x_offset > PNG_UINT_31_MAX)
   1190 +        png_error(png_ptr, "invalid x_offset in fcTL (> 2^31-1)");
   1191 +    if (y_offset > PNG_UINT_31_MAX)
   1192 +        png_error(png_ptr, "invalid y_offset in fcTL (> 2^31-1)");
   1193 +    if (width + x_offset > png_ptr->first_frame_width ||
   1194 +        height + y_offset > png_ptr->first_frame_height)
   1195 +        png_error(png_ptr, "dimensions of a frame are greater than "
   1196 +                           "the ones in IHDR");
   1197 +
   1198 +    if (dispose_op != PNG_DISPOSE_OP_NONE &&
   1199 +        dispose_op != PNG_DISPOSE_OP_BACKGROUND &&
   1200 +        dispose_op != PNG_DISPOSE_OP_PREVIOUS)
   1201 +        png_error(png_ptr, "invalid dispose_op in fcTL");
   1202 +
   1203 +    if (blend_op != PNG_BLEND_OP_SOURCE &&
   1204 +        blend_op != PNG_BLEND_OP_OVER)
   1205 +        png_error(png_ptr, "invalid blend_op in fcTL");
   1206 +
   1207 +    PNG_UNUSED(delay_num)
   1208 +    PNG_UNUSED(delay_den)
   1209 +}
   1210 +
   1211 +png_uint_32 PNGAPI
   1212 +png_set_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr,
   1213 +                              png_byte is_hidden)
   1214 +{
   1215 +    png_debug(1, "in png_first_frame_is_hidden()");
   1216 +
   1217 +    if (png_ptr == NULL)
   1218 +        return 0;
   1219 +
   1220 +    if (is_hidden != 0)
   1221 +        png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
   1222 +    else
   1223 +        png_ptr->apng_flags &= ~PNG_FIRST_FRAME_HIDDEN;
   1224 +
   1225 +    PNG_UNUSED(info_ptr)
   1226 +
   1227 +    return 1;
   1228 +}
   1229 +#endif /* APNG */
   1230 +
   1231 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
   1232 static png_byte
   1233 check_location(png_const_structrp png_ptr, int location)
   1234 diff --git a/pngstruct.h b/pngstruct.h
   1235 --- a/pngstruct.h
   1236 +++ b/pngstruct.h
   1237 @@ -392,6 +392,27 @@ struct png_struct_def
   1238    png_byte filter_type;
   1239 #endif
   1240 
   1241 +#ifdef PNG_APNG_SUPPORTED
   1242 +   png_uint_32 apng_flags;
   1243 +   png_uint_32 next_seq_num;         /* next fcTL/fdAT chunk sequence number */
   1244 +   png_uint_32 first_frame_width;
   1245 +   png_uint_32 first_frame_height;
   1246 +
   1247 +#ifdef PNG_READ_APNG_SUPPORTED
   1248 +   png_uint_32 num_frames_read;      /* incremented after all image data of */
   1249 +                                     /* a frame is read */
   1250 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   1251 +   png_progressive_frame_ptr frame_info_fn; /* frame info read callback */
   1252 +   png_progressive_frame_ptr frame_end_fn;  /* frame data read callback */
   1253 +#endif
   1254 +#endif
   1255 +
   1256 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1257 +   png_uint_32 num_frames_to_write;
   1258 +   png_uint_32 num_frames_written;
   1259 +#endif
   1260 +#endif /* APNG */
   1261 +
   1262 /* New members added in libpng-1.2.0 */
   1263 
   1264 /* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
   1265 diff --git a/pngwrite.c b/pngwrite.c
   1266 --- a/pngwrite.c
   1267 +++ b/pngwrite.c
   1268 @@ -127,6 +127,10 @@ png_write_info_before_PLTE(png_structrp 
   1269        * the application continues writing the PNG.  So check the 'invalid'
   1270        * flag here too.
   1271        */
   1272 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1273 +   if ((info_ptr->valid & PNG_INFO_acTL) != 0)
   1274 +      png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
   1275 +#endif
   1276 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
   1277          /* Write unknown chunks first; PNG v3 establishes a precedence order
   1278           * for colourspace chunks.  It is certain therefore that new
   1279 @@ -399,6 +403,11 @@ png_write_end(png_structrp png_ptr, png_
   1280    if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
   1281       png_error(png_ptr, "No IDATs written into file");
   1282 
   1283 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1284 +   if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
   1285 +      png_error(png_ptr, "Not enough frames written");
   1286 +#endif
   1287 +
   1288 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
   1289    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
   1290        png_ptr->num_palette_max >= png_ptr->num_palette)
   1291 @@ -2446,4 +2455,42 @@ png_image_write_to_file(png_imagep image
   1292 }
   1293 #endif /* SIMPLIFIED_WRITE_STDIO */
   1294 #endif /* SIMPLIFIED_WRITE */
   1295 +
   1296 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1297 +void PNGAPI
   1298 +png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
   1299 +    png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
   1300 +    png_uint_32 x_offset, png_uint_32 y_offset,
   1301 +    png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
   1302 +    png_byte blend_op)
   1303 +{
   1304 +    png_debug(1, "in png_write_frame_head");
   1305 +
   1306 +    /* there is a chance this has been set after png_write_info was called,
   1307 +    * so it would be set but not written. is there a way to be sure? */
   1308 +    if ((info_ptr->valid & PNG_INFO_acTL) == 0)
   1309 +        png_error(png_ptr, "png_write_frame_head(): acTL not set");
   1310 +
   1311 +    png_write_reset(png_ptr);
   1312 +
   1313 +    png_write_reinit(png_ptr, info_ptr, width, height);
   1314 +
   1315 +    if ((png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) == 0 ||
   1316 +        png_ptr->num_frames_written != 0)
   1317 +        png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
   1318 +                       delay_num, delay_den, dispose_op, blend_op);
   1319 +
   1320 +    PNG_UNUSED(row_pointers)
   1321 +}
   1322 +
   1323 +void PNGAPI
   1324 +png_write_frame_tail(png_structp png_ptr, png_infop info_ptr)
   1325 +{
   1326 +    png_debug(1, "in png_write_frame_tail");
   1327 +
   1328 +    png_ptr->num_frames_written++;
   1329 +
   1330 +    PNG_UNUSED(info_ptr)
   1331 +}
   1332 +#endif /* WRITE_APNG */
   1333 #endif /* WRITE */
   1334 diff --git a/pngwutil.c b/pngwutil.c
   1335 --- a/pngwutil.c
   1336 +++ b/pngwutil.c
   1337 @@ -840,6 +840,11 @@ png_write_IHDR(png_structrp png_ptr, png
   1338    /* Write the chunk */
   1339    png_write_complete_chunk(png_ptr, png_IHDR, buf, 13);
   1340 
   1341 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1342 +   png_ptr->first_frame_width = width;
   1343 +   png_ptr->first_frame_height = height;
   1344 +#endif
   1345 +
   1346    if ((png_ptr->do_filter) == PNG_NO_FILTERS)
   1347    {
   1348 #ifdef PNG_WRITE_FILTER_SUPPORTED
   1349 @@ -1026,7 +1031,17 @@ png_compress_IDAT(png_structrp png_ptr, 
   1350 #endif
   1351 
   1352          if (size > 0)
   1353 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1354 +         {
   1355 +            if (png_ptr->num_frames_written == 0)
   1356 +#endif
   1357             png_write_complete_chunk(png_ptr, png_IDAT, data, size);
   1358 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1359 +            else
   1360 +               png_write_fdAT(png_ptr, data, size);
   1361 +         }
   1362 +#endif /* WRITE_APNG */
   1363 +
   1364          png_ptr->mode |= PNG_HAVE_IDAT;
   1365 
   1366          png_ptr->zstream.next_out = data;
   1367 @@ -1073,7 +1088,17 @@ png_compress_IDAT(png_structrp png_ptr, 
   1368 #endif
   1369 
   1370          if (size > 0)
   1371 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1372 +         {
   1373 +            if (png_ptr->num_frames_written == 0)
   1374 +#endif
   1375             png_write_complete_chunk(png_ptr, png_IDAT, data, size);
   1376 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1377 +            else
   1378 +               png_write_fdAT(png_ptr, data, size);
   1379 +         }
   1380 +#endif /* WRITE_APNG */
   1381 +
   1382          png_ptr->zstream.avail_out = 0;
   1383          png_ptr->zstream.next_out = NULL;
   1384          png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
   1385 @@ -1975,6 +2000,82 @@ png_write_tIME(png_structrp png_ptr, png
   1386 }
   1387 #endif
   1388 
   1389 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1390 +void /* PRIVATE */
   1391 +png_write_acTL(png_structp png_ptr,
   1392 +    png_uint_32 num_frames, png_uint_32 num_plays)
   1393 +{
   1394 +    png_byte buf[8];
   1395 +
   1396 +    png_debug(1, "in png_write_acTL");
   1397 +
   1398 +    png_ptr->num_frames_to_write = num_frames;
   1399 +
   1400 +    if ((png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) != 0)
   1401 +        num_frames--;
   1402 +
   1403 +    png_save_uint_32(buf, num_frames);
   1404 +    png_save_uint_32(buf + 4, num_plays);
   1405 +
   1406 +    png_write_complete_chunk(png_ptr, png_acTL, buf, (png_size_t)8);
   1407 +}
   1408 +
   1409 +void /* PRIVATE */
   1410 +png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
   1411 +    png_uint_32 x_offset, png_uint_32 y_offset,
   1412 +    png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
   1413 +    png_byte blend_op)
   1414 +{
   1415 +    png_byte buf[26];
   1416 +
   1417 +    png_debug(1, "in png_write_fcTL");
   1418 +
   1419 +    if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0))
   1420 +        png_error(png_ptr, "x and/or y offset for the first frame aren't 0");
   1421 +    if (png_ptr->num_frames_written == 0 &&
   1422 +        (width != png_ptr->first_frame_width ||
   1423 +         height != png_ptr->first_frame_height))
   1424 +        png_error(png_ptr, "width and/or height in the first frame's fcTL "
   1425 +                           "don't match the ones in IHDR");
   1426 +
   1427 +    /* more error checking */
   1428 +    png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
   1429 +                             delay_num, delay_den, dispose_op, blend_op);
   1430 +
   1431 +    png_save_uint_32(buf, png_ptr->next_seq_num);
   1432 +    png_save_uint_32(buf + 4, width);
   1433 +    png_save_uint_32(buf + 8, height);
   1434 +    png_save_uint_32(buf + 12, x_offset);
   1435 +    png_save_uint_32(buf + 16, y_offset);
   1436 +    png_save_uint_16(buf + 20, delay_num);
   1437 +    png_save_uint_16(buf + 22, delay_den);
   1438 +    buf[24] = dispose_op;
   1439 +    buf[25] = blend_op;
   1440 +
   1441 +    png_write_complete_chunk(png_ptr, png_fcTL, buf, (png_size_t)26);
   1442 +
   1443 +    png_ptr->next_seq_num++;
   1444 +}
   1445 +
   1446 +void /* PRIVATE */
   1447 +png_write_fdAT(png_structp png_ptr,
   1448 +    png_const_bytep data, png_size_t length)
   1449 +{
   1450 +    png_byte buf[4];
   1451 +
   1452 +    png_write_chunk_header(png_ptr, png_fdAT, (png_uint_32)(4 + length));
   1453 +
   1454 +    png_save_uint_32(buf, png_ptr->next_seq_num);
   1455 +    png_write_chunk_data(png_ptr, buf, 4);
   1456 +
   1457 +    png_write_chunk_data(png_ptr, data, length);
   1458 +
   1459 +    png_write_chunk_end(png_ptr);
   1460 +
   1461 +    png_ptr->next_seq_num++;
   1462 +}
   1463 +#endif /* WRITE_APNG */
   1464 +
   1465 /* Initializes the row writing capability of libpng */
   1466 void /* PRIVATE */
   1467 png_write_start_row(png_structrp png_ptr)
   1468 @@ -2828,4 +2929,39 @@ png_write_filtered_row(png_structrp png_
   1469    }
   1470 #endif /* WRITE_FLUSH */
   1471 }
   1472 +
   1473 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1474 +void /* PRIVATE */
   1475 +png_write_reset(png_structp png_ptr)
   1476 +{
   1477 +    png_ptr->row_number = 0;
   1478 +    png_ptr->pass = 0;
   1479 +    png_ptr->mode &= ~PNG_HAVE_IDAT;
   1480 +}
   1481 +
   1482 +void /* PRIVATE */
   1483 +png_write_reinit(png_structp png_ptr, png_infop info_ptr,
   1484 +                 png_uint_32 width, png_uint_32 height)
   1485 +{
   1486 +    if (png_ptr->num_frames_written == 0 &&
   1487 +        (width != png_ptr->first_frame_width ||
   1488 +         height != png_ptr->first_frame_height))
   1489 +        png_error(png_ptr, "width and/or height in the first frame's fcTL "
   1490 +                           "don't match the ones in IHDR");
   1491 +    if (width > png_ptr->first_frame_width ||
   1492 +        height > png_ptr->first_frame_height)
   1493 +        png_error(png_ptr, "width and/or height for a frame greater than "
   1494 +                           "the ones in IHDR");
   1495 +
   1496 +    png_set_IHDR(png_ptr, info_ptr, width, height,
   1497 +                 info_ptr->bit_depth, info_ptr->color_type,
   1498 +                 info_ptr->interlace_type, info_ptr->compression_type,
   1499 +                 info_ptr->filter_type);
   1500 +
   1501 +    png_ptr->width = width;
   1502 +    png_ptr->height = height;
   1503 +    png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
   1504 +    png_ptr->usr_width = png_ptr->width;
   1505 +}
   1506 +#endif /* WRITE_APNG */
   1507 #endif /* WRITE */