Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / media / libpng / apng.patch
blob1cb3d350194bf99f264e8cf245345618c713a53f
1 diff --git a/png.h b/png.h
2 --- a/png.h
3 +++ b/png.h
4 @@ -329,6 +329,10 @@
5 # include "pnglibconf.h"
6 #endif
8 +#define PNG_APNG_SUPPORTED
9 +#define PNG_READ_APNG_SUPPORTED
10 +#define PNG_WRITE_APNG_SUPPORTED
12 #ifndef PNG_VERSION_INFO_ONLY
13 /* Machine specific configuration. */
14 # include "pngconf.h"
15 @@ -424,6 +428,17 @@ extern "C" {
16 * See pngconf.h for base types that vary by machine/system
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
25 +/* blend_op flags from inside fcTL */
26 +#define PNG_BLEND_OP_SOURCE 0x00
27 +#define PNG_BLEND_OP_OVER 0x01
28 +#endif /* APNG */
30 /* This triggers a compiler error in png.c, if png.c and png.h
31 * do not agree upon the version number.
33 @@ -745,6 +760,10 @@ typedef png_unknown_chunk * * png_unknow
34 #define PNG_INFO_sCAL 0x4000U /* ESR, 1.0.6 */
35 #define PNG_INFO_IDAT 0x8000U /* ESR, 1.0.6 */
36 #define PNG_INFO_eXIf 0x10000U /* GR-P, 1.6.31 */
37 +#ifdef PNG_APNG_SUPPORTED
38 +#define PNG_INFO_acTL 0x20000U
39 +#define PNG_INFO_fcTL 0x40000U
40 +#endif
42 /* This is used for the transformation routines, as some of them
43 * change these values for the row. It also should enable using
44 @@ -782,6 +801,10 @@ typedef PNG_CALLBACK(void, *png_write_st
45 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
46 typedef PNG_CALLBACK(void, *png_progressive_info_ptr, (png_structp, png_infop));
47 typedef PNG_CALLBACK(void, *png_progressive_end_ptr, (png_structp, png_infop));
48 +#ifdef PNG_APNG_SUPPORTED
49 +typedef PNG_CALLBACK(void, *png_progressive_frame_ptr, (png_structp,
50 + png_uint_32));
51 +#endif
53 /* The following callback receives png_uint_32 row_number, int pass for the
54 * png_bytep data of the row. When transforming an interlaced image the
55 @@ -3230,6 +3253,75 @@ PNG_EXPORT(244, int, png_set_option, (pn
56 * END OF HARDWARE AND SOFTWARE OPTIONS
57 ******************************************************************************/
59 +#ifdef PNG_APNG_SUPPORTED
60 +PNG_EXPORT(248, png_uint_32, png_get_acTL, (png_structp png_ptr,
61 + png_infop info_ptr, png_uint_32 *num_frames, png_uint_32 *num_plays));
63 +PNG_EXPORT(249, png_uint_32, png_set_acTL, (png_structp png_ptr,
64 + png_infop info_ptr, png_uint_32 num_frames, png_uint_32 num_plays));
66 +PNG_EXPORT(250, png_uint_32, png_get_num_frames, (png_structp png_ptr,
67 + png_infop info_ptr));
69 +PNG_EXPORT(251, png_uint_32, png_get_num_plays, (png_structp png_ptr,
70 + png_infop info_ptr));
72 +PNG_EXPORT(252, png_uint_32, png_get_next_frame_fcTL,
73 + (png_structp png_ptr, png_infop info_ptr, png_uint_32 *width,
74 + png_uint_32 *height, png_uint_32 *x_offset, png_uint_32 *y_offset,
75 + png_uint_16 *delay_num, png_uint_16 *delay_den, png_byte *dispose_op,
76 + png_byte *blend_op));
78 +PNG_EXPORT(253, png_uint_32, png_set_next_frame_fcTL,
79 + (png_structp png_ptr, png_infop info_ptr, png_uint_32 width,
80 + png_uint_32 height, png_uint_32 x_offset, png_uint_32 y_offset,
81 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
82 + png_byte blend_op));
84 +PNG_EXPORT(254, png_uint_32, png_get_next_frame_width,
85 + (png_structp png_ptr, png_infop info_ptr));
86 +PNG_EXPORT(255, png_uint_32, png_get_next_frame_height,
87 + (png_structp png_ptr, png_infop info_ptr));
88 +PNG_EXPORT(256, png_uint_32, png_get_next_frame_x_offset,
89 + (png_structp png_ptr, png_infop info_ptr));
90 +PNG_EXPORT(257, png_uint_32, png_get_next_frame_y_offset,
91 + (png_structp png_ptr, png_infop info_ptr));
92 +PNG_EXPORT(258, png_uint_16, png_get_next_frame_delay_num,
93 + (png_structp png_ptr, png_infop info_ptr));
94 +PNG_EXPORT(259, png_uint_16, png_get_next_frame_delay_den,
95 + (png_structp png_ptr, png_infop info_ptr));
96 +PNG_EXPORT(260, png_byte, png_get_next_frame_dispose_op,
97 + (png_structp png_ptr, png_infop info_ptr));
98 +PNG_EXPORT(261, png_byte, png_get_next_frame_blend_op,
99 + (png_structp png_ptr, png_infop info_ptr));
100 +PNG_EXPORT(262, png_byte, png_get_first_frame_is_hidden,
101 + (png_structp png_ptr, png_infop info_ptr));
102 +PNG_EXPORT(263, png_uint_32, png_set_first_frame_is_hidden,
103 + (png_structp png_ptr, png_infop info_ptr, png_byte is_hidden));
105 +#ifdef PNG_READ_APNG_SUPPORTED
106 +PNG_EXPORT(264, void, png_read_frame_head, (png_structp png_ptr,
107 + png_infop info_ptr));
108 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
109 +PNG_EXPORT(265, void, png_set_progressive_frame_fn, (png_structp png_ptr,
110 + png_progressive_frame_ptr frame_info_fn,
111 + png_progressive_frame_ptr frame_end_fn));
112 +#endif /* PROGRESSIVE_READ */
113 +#endif /* READ_APNG */
115 +#ifdef PNG_WRITE_APNG_SUPPORTED
116 +PNG_EXPORT(266, void, png_write_frame_head, (png_structp png_ptr,
117 + png_infop info_ptr, png_bytepp row_pointers,
118 + png_uint_32 width, png_uint_32 height,
119 + png_uint_32 x_offset, png_uint_32 y_offset,
120 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
121 + png_byte blend_op));
123 +PNG_EXPORT(267, void, png_write_frame_tail, (png_structp png_ptr,
124 + png_infop info_ptr));
125 +#endif /* WRITE_APNG */
126 +#endif /* APNG */
128 /* Maintainer: Put new public prototypes here ^, in libpng.3, in project
129 * defs, and in scripts/symbols.def.
131 @@ -3238,7 +3330,11 @@ PNG_EXPORT(244, int, png_set_option, (pn
132 * one to use is one more than this.)
134 #ifdef PNG_EXPORT_LAST_ORDINAL
135 +#ifdef PNG_APNG_SUPPORTED
136 + PNG_EXPORT_LAST_ORDINAL(269);
137 +#else
138 PNG_EXPORT_LAST_ORDINAL(249);
139 +#endif /* APNG */
140 #endif
142 #ifdef __cplusplus
143 diff --git a/pngget.c b/pngget.c
144 --- a/pngget.c
145 +++ b/pngget.c
146 @@ -1264,4 +1264,166 @@ png_get_palette_max(png_const_structp pn
147 # endif
148 #endif
150 +#ifdef PNG_APNG_SUPPORTED
151 +png_uint_32 PNGAPI
152 +png_get_acTL(png_structp png_ptr, png_infop info_ptr,
153 + png_uint_32 *num_frames, png_uint_32 *num_plays)
155 + png_debug1(1, "in %s retrieval function", "acTL");
157 + if (png_ptr != NULL && info_ptr != NULL &&
158 + (info_ptr->valid & PNG_INFO_acTL) != 0 &&
159 + num_frames != NULL && num_plays != NULL)
161 + *num_frames = info_ptr->num_frames;
162 + *num_plays = info_ptr->num_plays;
163 + return (1);
166 + return (0);
169 +png_uint_32 PNGAPI
170 +png_get_num_frames(png_structp png_ptr, png_infop info_ptr)
172 + png_debug(1, "in png_get_num_frames()");
174 + if (png_ptr != NULL && info_ptr != NULL)
175 + return (info_ptr->num_frames);
176 + return (0);
179 +png_uint_32 PNGAPI
180 +png_get_num_plays(png_structp png_ptr, png_infop info_ptr)
182 + png_debug(1, "in png_get_num_plays()");
184 + if (png_ptr != NULL && info_ptr != NULL)
185 + return (info_ptr->num_plays);
186 + return (0);
189 +png_uint_32 PNGAPI
190 +png_get_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
191 + png_uint_32 *width, png_uint_32 *height,
192 + png_uint_32 *x_offset, png_uint_32 *y_offset,
193 + png_uint_16 *delay_num, png_uint_16 *delay_den,
194 + png_byte *dispose_op, png_byte *blend_op)
196 + png_debug1(1, "in %s retrieval function", "fcTL");
198 + if (png_ptr != NULL && info_ptr != NULL &&
199 + (info_ptr->valid & PNG_INFO_fcTL) != 0 &&
200 + width != NULL && height != NULL &&
201 + x_offset != NULL && y_offset != NULL &&
202 + delay_num != NULL && delay_den != NULL &&
203 + dispose_op != NULL && blend_op != NULL)
205 + *width = info_ptr->next_frame_width;
206 + *height = info_ptr->next_frame_height;
207 + *x_offset = info_ptr->next_frame_x_offset;
208 + *y_offset = info_ptr->next_frame_y_offset;
209 + *delay_num = info_ptr->next_frame_delay_num;
210 + *delay_den = info_ptr->next_frame_delay_den;
211 + *dispose_op = info_ptr->next_frame_dispose_op;
212 + *blend_op = info_ptr->next_frame_blend_op;
213 + return (1);
216 + return (0);
219 +png_uint_32 PNGAPI
220 +png_get_next_frame_width(png_structp png_ptr, png_infop info_ptr)
222 + png_debug(1, "in png_get_next_frame_width()");
224 + if (png_ptr != NULL && info_ptr != NULL)
225 + return (info_ptr->next_frame_width);
226 + return (0);
229 +png_uint_32 PNGAPI
230 +png_get_next_frame_height(png_structp png_ptr, png_infop info_ptr)
232 + png_debug(1, "in png_get_next_frame_height()");
234 + if (png_ptr != NULL && info_ptr != NULL)
235 + return (info_ptr->next_frame_height);
236 + return (0);
239 +png_uint_32 PNGAPI
240 +png_get_next_frame_x_offset(png_structp png_ptr, png_infop info_ptr)
242 + png_debug(1, "in png_get_next_frame_x_offset()");
244 + if (png_ptr != NULL && info_ptr != NULL)
245 + return (info_ptr->next_frame_x_offset);
246 + return (0);
249 +png_uint_32 PNGAPI
250 +png_get_next_frame_y_offset(png_structp png_ptr, png_infop info_ptr)
252 + png_debug(1, "in png_get_next_frame_y_offset()");
254 + if (png_ptr != NULL && info_ptr != NULL)
255 + return (info_ptr->next_frame_y_offset);
256 + return (0);
259 +png_uint_16 PNGAPI
260 +png_get_next_frame_delay_num(png_structp png_ptr, png_infop info_ptr)
262 + png_debug(1, "in png_get_next_frame_delay_num()");
264 + if (png_ptr != NULL && info_ptr != NULL)
265 + return (info_ptr->next_frame_delay_num);
266 + return (0);
269 +png_uint_16 PNGAPI
270 +png_get_next_frame_delay_den(png_structp png_ptr, png_infop info_ptr)
272 + png_debug(1, "in png_get_next_frame_delay_den()");
274 + if (png_ptr != NULL && info_ptr != NULL)
275 + return (info_ptr->next_frame_delay_den);
276 + return (0);
279 +png_byte PNGAPI
280 +png_get_next_frame_dispose_op(png_structp png_ptr, png_infop info_ptr)
282 + png_debug(1, "in png_get_next_frame_dispose_op()");
284 + if (png_ptr != NULL && info_ptr != NULL)
285 + return (info_ptr->next_frame_dispose_op);
286 + return (0);
289 +png_byte PNGAPI
290 +png_get_next_frame_blend_op(png_structp png_ptr, png_infop info_ptr)
292 + png_debug(1, "in png_get_next_frame_blend_op()");
294 + if (png_ptr != NULL && info_ptr != NULL)
295 + return (info_ptr->next_frame_blend_op);
296 + return (0);
299 +png_byte PNGAPI
300 +png_get_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr)
302 + png_debug(1, "in png_first_frame_is_hidden()");
304 + if (png_ptr != NULL)
305 + return (png_byte)(png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN);
307 + PNG_UNUSED(info_ptr)
309 + return 0;
311 +#endif /* APNG */
312 #endif /* READ || WRITE */
313 diff --git a/pnginfo.h b/pnginfo.h
314 --- a/pnginfo.h
315 +++ b/pnginfo.h
316 @@ -263,5 +263,18 @@ defined(PNG_READ_BACKGROUND_SUPPORTED)
317 png_bytepp row_pointers; /* the image bits */
318 #endif
320 +#ifdef PNG_APNG_SUPPORTED
321 + png_uint_32 num_frames; /* including default image */
322 + png_uint_32 num_plays;
323 + png_uint_32 next_frame_width;
324 + png_uint_32 next_frame_height;
325 + png_uint_32 next_frame_x_offset;
326 + png_uint_32 next_frame_y_offset;
327 + png_uint_16 next_frame_delay_num;
328 + png_uint_16 next_frame_delay_den;
329 + png_byte next_frame_dispose_op;
330 + png_byte next_frame_blend_op;
331 +#endif
334 #endif /* PNGINFO_H */
335 diff --git a/pngpread.c b/pngpread.c
336 --- a/pngpread.c
337 +++ b/pngpread.c
338 @@ -195,6 +195,89 @@ png_push_read_chunk(png_structrp png_ptr
340 chunk_name = png_ptr->chunk_name;
342 +#ifdef PNG_READ_APNG_SUPPORTED
343 + if (png_ptr->num_frames_read > 0 &&
344 + png_ptr->num_frames_read < info_ptr->num_frames)
346 + if (chunk_name == png_IDAT)
348 + /* Discard trailing IDATs for the first frame */
349 + if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
350 + png_ptr->num_frames_read > 1)
351 + png_error(png_ptr, "out of place IDAT");
353 + PNG_PUSH_SAVE_BUFFER_IF_FULL
354 + png_crc_finish(png_ptr, png_ptr->push_length);
355 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
358 + else if (chunk_name == png_fdAT)
360 + PNG_PUSH_SAVE_BUFFER_IF_LT(4)
361 + png_ensure_sequence_number(png_ptr, 4);
363 + if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
365 + /* Discard trailing fdATs for frames other than the first */
366 + if (png_ptr->num_frames_read < 2)
367 + png_error(png_ptr, "out of place fdAT");
369 + PNG_PUSH_SAVE_BUFFER_IF_FULL
370 + png_crc_finish(png_ptr, png_ptr->push_length);
371 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
374 + else
376 + /* frame data follows */
377 + png_ptr->idat_size = png_ptr->push_length - 4;
378 + png_ptr->mode |= PNG_HAVE_IDAT;
379 + png_ptr->process_mode = PNG_READ_IDAT_MODE;
383 + else if (chunk_name == png_fcTL)
385 + PNG_PUSH_SAVE_BUFFER_IF_FULL
386 + png_read_reset(png_ptr);
387 + png_ptr->mode &= ~PNG_HAVE_fcTL;
389 + png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
391 + if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
392 + png_error(png_ptr, "missing required fcTL chunk");
394 + png_read_reinit(png_ptr, info_ptr);
395 + png_progressive_read_reset(png_ptr);
397 + if (png_ptr->frame_info_fn != NULL)
398 + (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
400 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
403 + else if (chunk_name == png_IEND)
405 + PNG_PUSH_SAVE_BUFFER_IF_FULL
406 + png_warning(png_ptr, "Number of actual frames fewer than expected");
407 + png_crc_finish(png_ptr, png_ptr->push_length);
408 + png_ptr->process_mode = PNG_READ_DONE_MODE;
409 + png_push_have_end(png_ptr, info_ptr);
412 + else
414 + PNG_PUSH_SAVE_BUFFER_IF_FULL
415 + png_warning(png_ptr, "Skipped (ignored) a chunk "
416 + "between APNG chunks");
417 + png_crc_finish(png_ptr, png_ptr->push_length);
418 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
421 + return;
423 +#endif /* READ_APNG */
425 if (chunk_name == png_IDAT)
427 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
428 @@ -261,6 +344,9 @@ png_push_read_chunk(png_structrp png_ptr
430 else if (chunk_name == png_IDAT)
432 +#ifdef PNG_READ_APNG_SUPPORTED
433 + png_have_info(png_ptr, info_ptr);
434 +#endif
435 png_ptr->idat_size = png_ptr->push_length;
436 png_ptr->process_mode = PNG_READ_IDAT_MODE;
437 png_push_have_info(png_ptr, info_ptr);
438 @@ -415,6 +501,20 @@ png_push_read_chunk(png_structrp png_ptr
440 #endif
442 +#ifdef PNG_READ_APNG_SUPPORTED
443 + else if (chunk_name == png_acTL)
445 + PNG_PUSH_SAVE_BUFFER_IF_FULL
446 + png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
449 + else if (chunk_name == png_fcTL)
451 + PNG_PUSH_SAVE_BUFFER_IF_FULL
452 + png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
455 +#endif /* READ_APNG */
456 else
458 PNG_PUSH_SAVE_BUFFER_IF_FULL
459 @@ -547,7 +647,11 @@ png_push_read_IDAT(png_structrp png_ptr)
460 png_byte chunk_tag[4];
462 /* TODO: this code can be commoned up with the same code in push_read */
463 +#ifdef PNG_READ_APNG_SUPPORTED
464 + PNG_PUSH_SAVE_BUFFER_IF_LT(12)
465 +#else
466 PNG_PUSH_SAVE_BUFFER_IF_LT(8)
467 +#endif
468 png_push_fill_buffer(png_ptr, chunk_length, 4);
469 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
470 png_reset_crc(png_ptr);
471 @@ -555,17 +659,60 @@ png_push_read_IDAT(png_structrp png_ptr)
472 png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
473 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
475 +#ifdef PNG_READ_APNG_SUPPORTED
476 + if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
478 + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) != 0)
480 + png_ptr->process_mode = PNG_READ_CHUNK_MODE;
481 + if (png_ptr->frame_end_fn != NULL)
482 + (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
483 + png_ptr->num_frames_read++;
484 + return;
486 + else
488 + if (png_ptr->chunk_name == png_IEND)
489 + png_error(png_ptr, "Not enough image data");
490 + PNG_PUSH_SAVE_BUFFER_IF_FULL
491 + png_warning(png_ptr, "Skipping (ignoring) a chunk between "
492 + "APNG chunks");
493 + png_crc_finish(png_ptr, png_ptr->push_length);
494 + png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
495 + return;
498 + else
499 +#endif
500 +#ifdef PNG_READ_APNG_SUPPORTED
501 + if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
502 +#else
503 if (png_ptr->chunk_name != png_IDAT)
504 +#endif
506 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
508 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
509 png_error(png_ptr, "Not enough compressed data");
511 +#ifdef PNG_READ_APNG_SUPPORTED
512 + if (png_ptr->frame_end_fn != NULL)
513 + (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
514 + png_ptr->num_frames_read++;
515 +#endif
517 return;
520 png_ptr->idat_size = png_ptr->push_length;
522 +#ifdef PNG_READ_APNG_SUPPORTED
523 + if (png_ptr->num_frames_read > 0)
525 + png_ensure_sequence_number(png_ptr, 4);
526 + png_ptr->idat_size -= 4;
528 +#endif
531 if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
532 @@ -639,6 +786,16 @@ png_process_IDAT_data(png_structrp png_p
533 if (!(buffer_length > 0) || buffer == NULL)
534 png_error(png_ptr, "No IDAT data (internal error)");
536 +#ifdef PNG_READ_APNG_SUPPORTED
537 + /* If the app is not APNG-aware, decode only the first frame */
538 + if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
539 + png_ptr->num_frames_read > 0)
541 + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
542 + return;
544 +#endif
546 /* This routine must process all the data it has been given
547 * before returning, calling the row callback as required to
548 * handle the uncompressed results.
549 @@ -1093,6 +1250,18 @@ png_set_progressive_read_fn(png_structrp
550 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
553 +#ifdef PNG_READ_APNG_SUPPORTED
554 +void PNGAPI
555 +png_set_progressive_frame_fn(png_structp png_ptr,
556 + png_progressive_frame_ptr frame_info_fn,
557 + png_progressive_frame_ptr frame_end_fn)
559 + png_ptr->frame_info_fn = frame_info_fn;
560 + png_ptr->frame_end_fn = frame_end_fn;
561 + png_ptr->apng_flags |= PNG_APNG_APP;
563 +#endif
565 png_voidp PNGAPI
566 png_get_progressive_ptr(png_const_structrp png_ptr)
568 diff --git a/pngpriv.h b/pngpriv.h
569 --- a/pngpriv.h
570 +++ b/pngpriv.h
571 @@ -662,6 +662,10 @@
572 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000U /* Have another chunk after IDAT */
573 #define PNG_WROTE_eXIf 0x4000U
574 #define PNG_IS_READ_STRUCT 0x8000U /* Else is a write struct */
575 +#ifdef PNG_APNG_SUPPORTED
576 +#define PNG_HAVE_acTL 0x10000U
577 +#define PNG_HAVE_fcTL 0x20000U
578 +#endif
580 /* Flags for the transformations the PNG library does on the image data */
581 #define PNG_BGR 0x0001U
582 @@ -898,6 +902,16 @@
583 #define png_tRNS PNG_U32(116, 82, 78, 83)
584 #define png_zTXt PNG_U32(122, 84, 88, 116)
586 +#ifdef PNG_APNG_SUPPORTED
587 +#define png_acTL PNG_U32( 97, 99, 84, 76)
588 +#define png_fcTL PNG_U32(102, 99, 84, 76)
589 +#define png_fdAT PNG_U32(102, 100, 65, 84)
591 +/* For png_struct.apng_flags: */
592 +#define PNG_FIRST_FRAME_HIDDEN 0x0001U
593 +#define PNG_APNG_APP 0x0002U
594 +#endif
596 /* The following will work on (signed char*) strings, whereas the get_uint_32
597 * macro will fail on top-bit-set values because of the sign extension.
599 @@ -1683,6 +1697,49 @@ PNG_INTERNAL_FUNCTION(void,png_push_read
601 #endif /* PROGRESSIVE_READ */
603 +#ifdef PNG_APNG_SUPPORTED
604 +PNG_INTERNAL_FUNCTION(void,png_ensure_fcTL_is_valid,(png_structp png_ptr,
605 + png_uint_32 width, png_uint_32 height,
606 + png_uint_32 x_offset, png_uint_32 y_offset,
607 + png_uint_16 delay_num, png_uint_16 delay_den,
608 + png_byte dispose_op, png_byte blend_op),PNG_EMPTY);
610 +#ifdef PNG_READ_APNG_SUPPORTED
611 +PNG_INTERNAL_FUNCTION(void,png_handle_acTL,(png_structp png_ptr,
612 + png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
613 +PNG_INTERNAL_FUNCTION(void,png_handle_fcTL,(png_structp png_ptr,
614 + png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
615 +PNG_INTERNAL_FUNCTION(void,png_handle_fdAT,(png_structp png_ptr,
616 + png_infop info_ptr, png_uint_32 length),PNG_EMPTY);
617 +PNG_INTERNAL_FUNCTION(void,png_have_info,(png_structp png_ptr,
618 + png_infop info_ptr),PNG_EMPTY);
619 +PNG_INTERNAL_FUNCTION(void,png_ensure_sequence_number,(png_structp png_ptr,
620 + png_uint_32 length),PNG_EMPTY);
621 +PNG_INTERNAL_FUNCTION(void,png_read_reset,(png_structp png_ptr),PNG_EMPTY);
622 +PNG_INTERNAL_FUNCTION(void,png_read_reinit,(png_structp png_ptr,
623 + png_infop info_ptr),PNG_EMPTY);
624 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
625 +PNG_INTERNAL_FUNCTION(void,png_progressive_read_reset,(png_structp png_ptr),
626 + PNG_EMPTY);
627 +#endif /* PROGRESSIVE_READ */
628 +#endif /* READ_APNG */
630 +#ifdef PNG_WRITE_APNG_SUPPORTED
631 +PNG_INTERNAL_FUNCTION(void,png_write_acTL,(png_structp png_ptr,
632 + png_uint_32 num_frames, png_uint_32 num_plays),PNG_EMPTY);
633 +PNG_INTERNAL_FUNCTION(void,png_write_fcTL,(png_structp png_ptr,
634 + png_uint_32 width, png_uint_32 height,
635 + png_uint_32 x_offset, png_uint_32 y_offset,
636 + png_uint_16 delay_num, png_uint_16 delay_den,
637 + png_byte dispose_op, png_byte blend_op),PNG_EMPTY);
638 +PNG_INTERNAL_FUNCTION(void,png_write_fdAT,(png_structp png_ptr,
639 + png_const_bytep data, png_size_t length),PNG_EMPTY);
640 +PNG_INTERNAL_FUNCTION(void,png_write_reset,(png_structp png_ptr),PNG_EMPTY);
641 +PNG_INTERNAL_FUNCTION(void,png_write_reinit,(png_structp png_ptr,
642 + png_infop info_ptr, png_uint_32 width, png_uint_32 height),PNG_EMPTY);
643 +#endif /* WRITE_APNG */
644 +#endif /* APNG */
646 /* Added at libpng version 1.6.0 */
647 #ifdef PNG_GAMMA_SUPPORTED
648 PNG_INTERNAL_FUNCTION(void,png_colorspace_set_gamma,(png_const_structrp png_ptr,
649 diff --git a/pngread.c b/pngread.c
650 --- a/pngread.c
651 +++ b/pngread.c
652 @@ -161,6 +161,9 @@ png_read_info(png_structrp png_ptr, png_
654 else if (chunk_name == png_IDAT)
656 +#ifdef PNG_READ_APNG_SUPPORTED
657 + png_have_info(png_ptr, info_ptr);
658 +#endif
659 png_ptr->idat_size = length;
660 break;
662 @@ -255,6 +258,17 @@ png_read_info(png_structrp png_ptr, png_
663 png_handle_iTXt(png_ptr, info_ptr, length);
664 #endif
666 +#ifdef PNG_READ_APNG_SUPPORTED
667 + else if (chunk_name == png_acTL)
668 + png_handle_acTL(png_ptr, info_ptr, length);
670 + else if (chunk_name == png_fcTL)
671 + png_handle_fcTL(png_ptr, info_ptr, length);
673 + else if (chunk_name == png_fdAT)
674 + png_handle_fdAT(png_ptr, info_ptr, length);
675 +#endif
677 else
678 png_handle_unknown(png_ptr, info_ptr, length,
679 PNG_HANDLE_CHUNK_AS_DEFAULT);
680 @@ -262,6 +276,72 @@ png_read_info(png_structrp png_ptr, png_
682 #endif /* SEQUENTIAL_READ */
684 +#ifdef PNG_READ_APNG_SUPPORTED
685 +void PNGAPI
686 +png_read_frame_head(png_structp png_ptr, png_infop info_ptr)
688 + png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */
690 + png_debug(0, "Reading frame head");
692 + if ((png_ptr->mode & PNG_HAVE_acTL) == 0)
693 + png_error(png_ptr, "attempt to png_read_frame_head() but "
694 + "no acTL present");
696 + /* do nothing for the main IDAT */
697 + if (png_ptr->num_frames_read == 0)
698 + return;
700 + png_read_reset(png_ptr);
701 + png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
702 + png_ptr->mode &= ~PNG_HAVE_fcTL;
704 + have_chunk_after_DAT = 0;
705 + for (;;)
707 + png_uint_32 length = png_read_chunk_header(png_ptr);
709 + if (png_ptr->chunk_name == png_IDAT)
711 + /* discard trailing IDATs for the first frame */
712 + if (have_chunk_after_DAT != 0 || png_ptr->num_frames_read > 1)
713 + png_error(png_ptr, "png_read_frame_head(): out of place IDAT");
714 + png_crc_finish(png_ptr, length);
717 + else if (png_ptr->chunk_name == png_fcTL)
719 + png_handle_fcTL(png_ptr, info_ptr, length);
720 + have_chunk_after_DAT = 1;
723 + else if (png_ptr->chunk_name == png_fdAT)
725 + png_ensure_sequence_number(png_ptr, length);
727 + /* discard trailing fdATs for frames other than the first */
728 + if (have_chunk_after_DAT == 0 && png_ptr->num_frames_read > 1)
729 + png_crc_finish(png_ptr, length - 4);
730 + else if (png_ptr->mode & PNG_HAVE_fcTL)
732 + png_ptr->idat_size = length - 4;
733 + png_ptr->mode |= PNG_HAVE_IDAT;
735 + break;
737 + else
738 + png_error(png_ptr, "png_read_frame_head(): out of place fdAT");
740 + else
742 + png_warning(png_ptr, "Skipped (ignored) a chunk "
743 + "between APNG chunks");
744 + png_crc_finish(png_ptr, length);
748 +#endif /* READ_APNG */
750 /* Optional call to update the users info_ptr structure */
751 void PNGAPI
752 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
753 diff --git a/pngrutil.c b/pngrutil.c
754 --- a/pngrutil.c
755 +++ b/pngrutil.c
756 @@ -863,6 +863,11 @@ png_handle_IHDR(png_structrp png_ptr, pn
757 filter_type = buf[11];
758 interlace_type = buf[12];
760 +#ifdef PNG_READ_APNG_SUPPORTED
761 + png_ptr->first_frame_width = width;
762 + png_ptr->first_frame_height = height;
763 +#endif
765 /* Set internal variables */
766 png_ptr->width = width;
767 png_ptr->height = height;
768 @@ -2857,6 +2862,180 @@ png_handle_iTXt(png_structrp png_ptr, pn
770 #endif
772 +#ifdef PNG_READ_APNG_SUPPORTED
773 +void /* PRIVATE */
774 +png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
776 + png_byte data[8];
777 + png_uint_32 num_frames;
778 + png_uint_32 num_plays;
779 + png_uint_32 didSet;
781 + png_debug(1, "in png_handle_acTL");
783 + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
785 + png_error(png_ptr, "Missing IHDR before acTL");
787 + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
789 + png_warning(png_ptr, "Invalid acTL after IDAT skipped");
790 + png_crc_finish(png_ptr, length);
791 + return;
793 + else if ((png_ptr->mode & PNG_HAVE_acTL) != 0)
795 + png_warning(png_ptr, "Duplicate acTL skipped");
796 + png_crc_finish(png_ptr, length);
797 + return;
799 + else if (length != 8)
801 + png_warning(png_ptr, "acTL with invalid length skipped");
802 + png_crc_finish(png_ptr, length);
803 + return;
806 + png_crc_read(png_ptr, data, 8);
807 + png_crc_finish(png_ptr, 0);
809 + num_frames = png_get_uint_31(png_ptr, data);
810 + num_plays = png_get_uint_31(png_ptr, data + 4);
812 + /* the set function will do error checking on num_frames */
813 + didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
814 + if (didSet != 0)
815 + png_ptr->mode |= PNG_HAVE_acTL;
818 +void /* PRIVATE */
819 +png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
821 + png_byte data[22];
822 + png_uint_32 width;
823 + png_uint_32 height;
824 + png_uint_32 x_offset;
825 + png_uint_32 y_offset;
826 + png_uint_16 delay_num;
827 + png_uint_16 delay_den;
828 + png_byte dispose_op;
829 + png_byte blend_op;
831 + png_debug(1, "in png_handle_fcTL");
833 + png_ensure_sequence_number(png_ptr, length);
835 + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
837 + png_error(png_ptr, "Missing IHDR before fcTL");
839 + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
841 + /* for any frames other then the first this message may be misleading,
842 + * but correct. PNG_HAVE_IDAT is unset before the frame head is read
843 + * i can't think of a better message */
844 + png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
845 + png_crc_finish(png_ptr, length-4);
846 + return;
848 + else if ((png_ptr->mode & PNG_HAVE_fcTL) != 0)
850 + png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
851 + png_crc_finish(png_ptr, length-4);
852 + return;
854 + else if (length != 26)
856 + png_warning(png_ptr, "fcTL with invalid length skipped");
857 + png_crc_finish(png_ptr, length-4);
858 + return;
861 + png_crc_read(png_ptr, data, 22);
862 + png_crc_finish(png_ptr, 0);
864 + width = png_get_uint_31(png_ptr, data);
865 + height = png_get_uint_31(png_ptr, data + 4);
866 + x_offset = png_get_uint_31(png_ptr, data + 8);
867 + y_offset = png_get_uint_31(png_ptr, data + 12);
868 + delay_num = png_get_uint_16(data + 16);
869 + delay_den = png_get_uint_16(data + 18);
870 + dispose_op = data[20];
871 + blend_op = data[21];
873 + if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
875 + png_warning(png_ptr, "fcTL for the first frame must have zero offset");
876 + return;
879 + if (info_ptr != NULL)
881 + if (png_ptr->num_frames_read == 0 &&
882 + (width != info_ptr->width || height != info_ptr->height))
884 + png_warning(png_ptr, "size in first frame's fcTL must match "
885 + "the size in IHDR");
886 + return;
889 + /* The set function will do more error checking */
890 + png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
891 + x_offset, y_offset, delay_num, delay_den,
892 + dispose_op, blend_op);
894 + png_read_reinit(png_ptr, info_ptr);
896 + png_ptr->mode |= PNG_HAVE_fcTL;
900 +void /* PRIVATE */
901 +png_have_info(png_structp png_ptr, png_infop info_ptr)
903 + if ((info_ptr->valid & PNG_INFO_acTL) != 0 &&
904 + (info_ptr->valid & PNG_INFO_fcTL) == 0)
906 + png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
907 + info_ptr->num_frames++;
911 +void /* PRIVATE */
912 +png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
914 + png_ensure_sequence_number(png_ptr, length);
916 + /* This function is only called from png_read_end(), png_read_info(),
917 + * and png_push_read_chunk() which means that:
918 + * - the user doesn't want to read this frame
919 + * - or this is an out-of-place fdAT
920 + * in either case it is safe to ignore the chunk with a warning */
921 + png_warning(png_ptr, "ignoring fdAT chunk");
922 + png_crc_finish(png_ptr, length - 4);
923 + PNG_UNUSED(info_ptr)
926 +void /* PRIVATE */
927 +png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
929 + png_byte data[4];
930 + png_uint_32 sequence_number;
932 + if (length < 4)
933 + png_error(png_ptr, "invalid fcTL or fdAT chunk found");
935 + png_crc_read(png_ptr, data, 4);
936 + sequence_number = png_get_uint_31(png_ptr, data);
938 + if (sequence_number != png_ptr->next_seq_num)
939 + png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
940 + "number found");
942 + png_ptr->next_seq_num++;
944 +#endif /* READ_APNG */
946 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
947 /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
948 static int
949 @@ -3162,7 +3341,11 @@ png_check_chunk_length(png_const_structr
950 if (PNG_USER_CHUNK_MALLOC_MAX < limit)
951 limit = PNG_USER_CHUNK_MALLOC_MAX;
952 # endif
953 +#ifdef PNG_READ_APNG_SUPPORTED
954 + if (png_ptr->chunk_name == png_IDAT || png_ptr->chunk_name == png_fdAT)
955 +#else
956 if (png_ptr->chunk_name == png_IDAT)
957 +#endif
959 png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
960 size_t row_factor =
961 @@ -4166,6 +4349,38 @@ png_read_IDAT_data(png_structrp png_ptr,
962 uInt avail_in;
963 png_bytep buffer;
965 +#ifdef PNG_READ_APNG_SUPPORTED
966 + png_uint_32 bytes_to_skip = 0;
968 + while (png_ptr->idat_size == 0 || bytes_to_skip != 0)
970 + png_crc_finish(png_ptr, bytes_to_skip);
971 + bytes_to_skip = 0;
973 + png_ptr->idat_size = png_read_chunk_header(png_ptr);
974 + if (png_ptr->num_frames_read == 0)
976 + if (png_ptr->chunk_name != png_IDAT)
977 + png_error(png_ptr, "Not enough image data");
979 + else
981 + if (png_ptr->chunk_name == png_IEND)
982 + png_error(png_ptr, "Not enough image data");
983 + if (png_ptr->chunk_name != png_fdAT)
985 + png_warning(png_ptr, "Skipped (ignored) a chunk "
986 + "between APNG chunks");
987 + bytes_to_skip = png_ptr->idat_size;
988 + continue;
991 + png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
993 + png_ptr->idat_size -= 4;
996 +#else
997 while (png_ptr->idat_size == 0)
999 png_crc_finish(png_ptr, 0);
1000 @@ -4177,6 +4392,7 @@ png_read_IDAT_data(png_structrp png_ptr,
1001 if (png_ptr->chunk_name != png_IDAT)
1002 png_error(png_ptr, "Not enough image data");
1004 +#endif /* READ_APNG */
1006 avail_in = png_ptr->IDAT_read_size;
1008 @@ -4240,6 +4456,9 @@ png_read_IDAT_data(png_structrp png_ptr,
1010 png_ptr->mode |= PNG_AFTER_IDAT;
1011 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
1012 +#ifdef PNG_READ_APNG_SUPPORTED
1013 + png_ptr->num_frames_read++;
1014 +#endif
1016 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
1017 png_chunk_benign_error(png_ptr, "Extra compressed data");
1018 @@ -4677,4 +4896,80 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED
1020 png_ptr->flags |= PNG_FLAG_ROW_INIT;
1023 +#ifdef PNG_READ_APNG_SUPPORTED
1024 +/* This function is to be called after the main IDAT set has been read and
1025 + * before a new IDAT is read. It resets some parts of png_ptr
1026 + * to make them usable by the read functions again */
1027 +void /* PRIVATE */
1028 +png_read_reset(png_structp png_ptr)
1030 + png_ptr->mode &= ~PNG_HAVE_IDAT;
1031 + png_ptr->mode &= ~PNG_AFTER_IDAT;
1032 + png_ptr->row_number = 0;
1033 + png_ptr->pass = 0;
1036 +void /* PRIVATE */
1037 +png_read_reinit(png_structp png_ptr, png_infop info_ptr)
1039 + png_ptr->width = info_ptr->next_frame_width;
1040 + png_ptr->height = info_ptr->next_frame_height;
1041 + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
1042 + png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,
1043 + png_ptr->width);
1044 + if (png_ptr->prev_row != NULL)
1045 + memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1048 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1049 +/* same as png_read_reset() but for the progressive reader */
1050 +void /* PRIVATE */
1051 +png_progressive_read_reset(png_structp png_ptr)
1053 +#ifdef PNG_READ_INTERLACING_SUPPORTED
1054 + /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1056 + /* Start of interlace block */
1057 + static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1059 + /* Offset to next interlace block */
1060 + static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1062 + /* Start of interlace block in the y direction */
1063 + static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1065 + /* Offset to next interlace block in the y direction */
1066 + static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1068 + if (png_ptr->interlaced != 0)
1070 + if ((png_ptr->transformations & PNG_INTERLACE) == 0)
1071 + png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1072 + png_pass_ystart[0]) / png_pass_yinc[0];
1073 + else
1074 + png_ptr->num_rows = png_ptr->height;
1076 + png_ptr->iwidth = (png_ptr->width +
1077 + png_pass_inc[png_ptr->pass] - 1 -
1078 + png_pass_start[png_ptr->pass]) /
1079 + png_pass_inc[png_ptr->pass];
1081 + else
1082 +#endif /* READ_INTERLACING */
1084 + png_ptr->num_rows = png_ptr->height;
1085 + png_ptr->iwidth = png_ptr->width;
1087 + png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED;
1088 + if (inflateReset(&(png_ptr->zstream)) != Z_OK)
1089 + png_error(png_ptr, "inflateReset failed");
1090 + png_ptr->zstream.avail_in = 0;
1091 + png_ptr->zstream.next_in = 0;
1092 + png_ptr->zstream.next_out = png_ptr->row_buf;
1093 + png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
1094 + png_ptr->iwidth) + 1;
1096 +#endif /* PROGRESSIVE_READ */
1097 +#endif /* READ_APNG */
1098 #endif /* READ */
1099 diff --git a/pngset.c b/pngset.c
1100 --- a/pngset.c
1101 +++ b/pngset.c
1102 @@ -280,6 +280,11 @@ png_set_IHDR(png_const_structrp png_ptr,
1103 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
1105 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
1107 +#ifdef PNG_APNG_SUPPORTED
1108 + /* for non-animated png. this may be overwritten from an acTL chunk later */
1109 + info_ptr->num_frames = 1;
1110 +#endif
1113 #ifdef PNG_oFFs_SUPPORTED
1114 @@ -1151,6 +1156,146 @@ png_set_sPLT(png_const_structrp png_ptr,
1116 #endif /* sPLT */
1118 +#ifdef PNG_APNG_SUPPORTED
1119 +png_uint_32 PNGAPI
1120 +png_set_acTL(png_structp png_ptr, png_infop info_ptr,
1121 + png_uint_32 num_frames, png_uint_32 num_plays)
1123 + png_debug1(1, "in %s storage function", "acTL");
1125 + if (png_ptr == NULL || info_ptr == NULL)
1127 + png_warning(png_ptr,
1128 + "Call to png_set_acTL() with NULL png_ptr "
1129 + "or info_ptr ignored");
1130 + return (0);
1132 + if (num_frames == 0)
1134 + png_warning(png_ptr,
1135 + "Ignoring attempt to set acTL with num_frames zero");
1136 + return (0);
1138 + if (num_frames > PNG_UINT_31_MAX)
1140 + png_warning(png_ptr,
1141 + "Ignoring attempt to set acTL with num_frames > 2^31-1");
1142 + return (0);
1144 + if (num_plays > PNG_UINT_31_MAX)
1146 + png_warning(png_ptr,
1147 + "Ignoring attempt to set acTL with num_plays > 2^31-1");
1148 + return (0);
1151 + info_ptr->num_frames = num_frames;
1152 + info_ptr->num_plays = num_plays;
1154 + info_ptr->valid |= PNG_INFO_acTL;
1156 + return (1);
1159 +/* delay_num and delay_den can hold any 16-bit values including zero */
1160 +png_uint_32 PNGAPI
1161 +png_set_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
1162 + png_uint_32 width, png_uint_32 height,
1163 + png_uint_32 x_offset, png_uint_32 y_offset,
1164 + png_uint_16 delay_num, png_uint_16 delay_den,
1165 + png_byte dispose_op, png_byte blend_op)
1167 + png_debug1(1, "in %s storage function", "fcTL");
1169 + if (png_ptr == NULL || info_ptr == NULL)
1171 + png_warning(png_ptr,
1172 + "Call to png_set_fcTL() with NULL png_ptr or info_ptr "
1173 + "ignored");
1174 + return (0);
1177 + png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1178 + delay_num, delay_den, dispose_op, blend_op);
1180 + if (blend_op == PNG_BLEND_OP_OVER)
1182 + if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0 &&
1183 + png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) == 0)
1185 + png_warning(png_ptr, "PNG_BLEND_OP_OVER is meaningless "
1186 + "and wasteful for opaque images, ignored");
1187 + blend_op = PNG_BLEND_OP_SOURCE;
1191 + info_ptr->next_frame_width = width;
1192 + info_ptr->next_frame_height = height;
1193 + info_ptr->next_frame_x_offset = x_offset;
1194 + info_ptr->next_frame_y_offset = y_offset;
1195 + info_ptr->next_frame_delay_num = delay_num;
1196 + info_ptr->next_frame_delay_den = delay_den;
1197 + info_ptr->next_frame_dispose_op = dispose_op;
1198 + info_ptr->next_frame_blend_op = blend_op;
1200 + info_ptr->valid |= PNG_INFO_fcTL;
1202 + return (1);
1205 +void /* PRIVATE */
1206 +png_ensure_fcTL_is_valid(png_structp png_ptr,
1207 + png_uint_32 width, png_uint_32 height,
1208 + png_uint_32 x_offset, png_uint_32 y_offset,
1209 + png_uint_16 delay_num, png_uint_16 delay_den,
1210 + png_byte dispose_op, png_byte blend_op)
1212 + if (width == 0 || width > PNG_UINT_31_MAX)
1213 + png_error(png_ptr, "invalid width in fcTL (0 or > 2^31-1)");
1214 + if (height == 0 || height > PNG_UINT_31_MAX)
1215 + png_error(png_ptr, "invalid height in fcTL (0 or > 2^31-1)");
1216 + if (x_offset > PNG_UINT_31_MAX)
1217 + png_error(png_ptr, "invalid x_offset in fcTL (> 2^31-1)");
1218 + if (y_offset > PNG_UINT_31_MAX)
1219 + png_error(png_ptr, "invalid y_offset in fcTL (> 2^31-1)");
1220 + if (width + x_offset > png_ptr->first_frame_width ||
1221 + height + y_offset > png_ptr->first_frame_height)
1222 + png_error(png_ptr, "dimensions of a frame are greater than "
1223 + "the ones in IHDR");
1225 + if (dispose_op != PNG_DISPOSE_OP_NONE &&
1226 + dispose_op != PNG_DISPOSE_OP_BACKGROUND &&
1227 + dispose_op != PNG_DISPOSE_OP_PREVIOUS)
1228 + png_error(png_ptr, "invalid dispose_op in fcTL");
1230 + if (blend_op != PNG_BLEND_OP_SOURCE &&
1231 + blend_op != PNG_BLEND_OP_OVER)
1232 + png_error(png_ptr, "invalid blend_op in fcTL");
1234 + PNG_UNUSED(delay_num)
1235 + PNG_UNUSED(delay_den)
1238 +png_uint_32 PNGAPI
1239 +png_set_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr,
1240 + png_byte is_hidden)
1242 + png_debug(1, "in png_first_frame_is_hidden()");
1244 + if (png_ptr == NULL)
1245 + return 0;
1247 + if (is_hidden != 0)
1248 + png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
1249 + else
1250 + png_ptr->apng_flags &= ~PNG_FIRST_FRAME_HIDDEN;
1252 + PNG_UNUSED(info_ptr)
1254 + return 1;
1256 +#endif /* APNG */
1258 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1259 static png_byte
1260 check_location(png_const_structrp png_ptr, int location)
1261 diff --git a/pngstruct.h b/pngstruct.h
1262 --- a/pngstruct.h
1263 +++ b/pngstruct.h
1264 @@ -399,6 +399,27 @@ struct png_struct_def
1265 png_byte filter_type;
1266 #endif
1268 +#ifdef PNG_APNG_SUPPORTED
1269 + png_uint_32 apng_flags;
1270 + png_uint_32 next_seq_num; /* next fcTL/fdAT chunk sequence number */
1271 + png_uint_32 first_frame_width;
1272 + png_uint_32 first_frame_height;
1274 +#ifdef PNG_READ_APNG_SUPPORTED
1275 + png_uint_32 num_frames_read; /* incremented after all image data of */
1276 + /* a frame is read */
1277 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1278 + png_progressive_frame_ptr frame_info_fn; /* frame info read callback */
1279 + png_progressive_frame_ptr frame_end_fn; /* frame data read callback */
1280 +#endif
1281 +#endif
1283 +#ifdef PNG_WRITE_APNG_SUPPORTED
1284 + png_uint_32 num_frames_to_write;
1285 + png_uint_32 num_frames_written;
1286 +#endif
1287 +#endif /* APNG */
1289 /* New members added in libpng-1.2.0 */
1291 /* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
1292 diff --git a/pngwrite.c b/pngwrite.c
1293 --- a/pngwrite.c
1294 +++ b/pngwrite.c
1295 @@ -128,6 +128,10 @@ png_write_info_before_PLTE(png_structrp
1296 * the application continues writing the PNG. So check the 'invalid'
1297 * flag here too.
1299 +#ifdef PNG_WRITE_APNG_SUPPORTED
1300 + if ((info_ptr->valid & PNG_INFO_acTL) != 0)
1301 + png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
1302 +#endif
1303 #ifdef PNG_GAMMA_SUPPORTED
1304 # ifdef PNG_WRITE_gAMA_SUPPORTED
1305 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
1306 @@ -368,6 +372,11 @@ png_write_end(png_structrp png_ptr, png_
1307 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
1308 png_error(png_ptr, "No IDATs written into file");
1310 +#ifdef PNG_WRITE_APNG_SUPPORTED
1311 + if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
1312 + png_error(png_ptr, "Not enough frames written");
1313 +#endif
1315 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
1316 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1317 png_ptr->num_palette_max >= png_ptr->num_palette)
1318 @@ -2415,4 +2424,42 @@ png_image_write_to_file(png_imagep image
1320 #endif /* SIMPLIFIED_WRITE_STDIO */
1321 #endif /* SIMPLIFIED_WRITE */
1323 +#ifdef PNG_WRITE_APNG_SUPPORTED
1324 +void PNGAPI
1325 +png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
1326 + png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
1327 + png_uint_32 x_offset, png_uint_32 y_offset,
1328 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1329 + png_byte blend_op)
1331 + png_debug(1, "in png_write_frame_head");
1333 + /* there is a chance this has been set after png_write_info was called,
1334 + * so it would be set but not written. is there a way to be sure? */
1335 + if ((info_ptr->valid & PNG_INFO_acTL) == 0)
1336 + png_error(png_ptr, "png_write_frame_head(): acTL not set");
1338 + png_write_reset(png_ptr);
1340 + png_write_reinit(png_ptr, info_ptr, width, height);
1342 + if ((png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) == 0 ||
1343 + png_ptr->num_frames_written != 0)
1344 + png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
1345 + delay_num, delay_den, dispose_op, blend_op);
1347 + PNG_UNUSED(row_pointers)
1350 +void PNGAPI
1351 +png_write_frame_tail(png_structp png_ptr, png_infop info_ptr)
1353 + png_debug(1, "in png_write_frame_tail");
1355 + png_ptr->num_frames_written++;
1357 + PNG_UNUSED(info_ptr)
1359 +#endif /* WRITE_APNG */
1360 #endif /* WRITE */
1361 diff --git a/pngwutil.c b/pngwutil.c
1362 --- a/pngwutil.c
1363 +++ b/pngwutil.c
1364 @@ -823,6 +823,11 @@ png_write_IHDR(png_structrp png_ptr, png
1365 /* Write the chunk */
1366 png_write_complete_chunk(png_ptr, png_IHDR, buf, 13);
1368 +#ifdef PNG_WRITE_APNG_SUPPORTED
1369 + png_ptr->first_frame_width = width;
1370 + png_ptr->first_frame_height = height;
1371 +#endif
1373 if ((png_ptr->do_filter) == PNG_NO_FILTERS)
1375 #ifdef PNG_WRITE_FILTER_SUPPORTED
1376 @@ -1009,7 +1014,17 @@ png_compress_IDAT(png_structrp png_ptr,
1377 #endif
1379 if (size > 0)
1380 +#ifdef PNG_WRITE_APNG_SUPPORTED
1382 + if (png_ptr->num_frames_written == 0)
1383 +#endif
1384 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1385 +#ifdef PNG_WRITE_APNG_SUPPORTED
1386 + else
1387 + png_write_fdAT(png_ptr, data, size);
1389 +#endif /* WRITE_APNG */
1391 png_ptr->mode |= PNG_HAVE_IDAT;
1393 png_ptr->zstream.next_out = data;
1394 @@ -1056,7 +1071,17 @@ png_compress_IDAT(png_structrp png_ptr,
1395 #endif
1397 if (size > 0)
1398 +#ifdef PNG_WRITE_APNG_SUPPORTED
1400 + if (png_ptr->num_frames_written == 0)
1401 +#endif
1402 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1403 +#ifdef PNG_WRITE_APNG_SUPPORTED
1404 + else
1405 + png_write_fdAT(png_ptr, data, size);
1407 +#endif /* WRITE_APNG */
1409 png_ptr->zstream.avail_out = 0;
1410 png_ptr->zstream.next_out = NULL;
1411 png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
1412 @@ -1891,6 +1916,82 @@ png_write_tIME(png_structrp png_ptr, png
1414 #endif
1416 +#ifdef PNG_WRITE_APNG_SUPPORTED
1417 +void /* PRIVATE */
1418 +png_write_acTL(png_structp png_ptr,
1419 + png_uint_32 num_frames, png_uint_32 num_plays)
1421 + png_byte buf[8];
1423 + png_debug(1, "in png_write_acTL");
1425 + png_ptr->num_frames_to_write = num_frames;
1427 + if ((png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) != 0)
1428 + num_frames--;
1430 + png_save_uint_32(buf, num_frames);
1431 + png_save_uint_32(buf + 4, num_plays);
1433 + png_write_complete_chunk(png_ptr, png_acTL, buf, (png_size_t)8);
1436 +void /* PRIVATE */
1437 +png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
1438 + png_uint_32 x_offset, png_uint_32 y_offset,
1439 + png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1440 + png_byte blend_op)
1442 + png_byte buf[26];
1444 + png_debug(1, "in png_write_fcTL");
1446 + if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0))
1447 + png_error(png_ptr, "x and/or y offset for the first frame aren't 0");
1448 + if (png_ptr->num_frames_written == 0 &&
1449 + (width != png_ptr->first_frame_width ||
1450 + height != png_ptr->first_frame_height))
1451 + png_error(png_ptr, "width and/or height in the first frame's fcTL "
1452 + "don't match the ones in IHDR");
1454 + /* more error checking */
1455 + png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1456 + delay_num, delay_den, dispose_op, blend_op);
1458 + png_save_uint_32(buf, png_ptr->next_seq_num);
1459 + png_save_uint_32(buf + 4, width);
1460 + png_save_uint_32(buf + 8, height);
1461 + png_save_uint_32(buf + 12, x_offset);
1462 + png_save_uint_32(buf + 16, y_offset);
1463 + png_save_uint_16(buf + 20, delay_num);
1464 + png_save_uint_16(buf + 22, delay_den);
1465 + buf[24] = dispose_op;
1466 + buf[25] = blend_op;
1468 + png_write_complete_chunk(png_ptr, png_fcTL, buf, (png_size_t)26);
1470 + png_ptr->next_seq_num++;
1473 +void /* PRIVATE */
1474 +png_write_fdAT(png_structp png_ptr,
1475 + png_const_bytep data, png_size_t length)
1477 + png_byte buf[4];
1479 + png_write_chunk_header(png_ptr, png_fdAT, (png_uint_32)(4 + length));
1481 + png_save_uint_32(buf, png_ptr->next_seq_num);
1482 + png_write_chunk_data(png_ptr, buf, 4);
1484 + png_write_chunk_data(png_ptr, data, length);
1486 + png_write_chunk_end(png_ptr);
1488 + png_ptr->next_seq_num++;
1490 +#endif /* WRITE_APNG */
1492 /* Initializes the row writing capability of libpng */
1493 void /* PRIVATE */
1494 png_write_start_row(png_structrp png_ptr)
1495 @@ -2784,4 +2885,39 @@ png_write_filtered_row(png_structrp png_
1497 #endif /* WRITE_FLUSH */
1500 +#ifdef PNG_WRITE_APNG_SUPPORTED
1501 +void /* PRIVATE */
1502 +png_write_reset(png_structp png_ptr)
1504 + png_ptr->row_number = 0;
1505 + png_ptr->pass = 0;
1506 + png_ptr->mode &= ~PNG_HAVE_IDAT;
1509 +void /* PRIVATE */
1510 +png_write_reinit(png_structp png_ptr, png_infop info_ptr,
1511 + png_uint_32 width, png_uint_32 height)
1513 + if (png_ptr->num_frames_written == 0 &&
1514 + (width != png_ptr->first_frame_width ||
1515 + height != png_ptr->first_frame_height))
1516 + png_error(png_ptr, "width and/or height in the first frame's fcTL "
1517 + "don't match the ones in IHDR");
1518 + if (width > png_ptr->first_frame_width ||
1519 + height > png_ptr->first_frame_height)
1520 + png_error(png_ptr, "width and/or height for a frame greater than "
1521 + "the ones in IHDR");
1523 + png_set_IHDR(png_ptr, info_ptr, width, height,
1524 + info_ptr->bit_depth, info_ptr->color_type,
1525 + info_ptr->interlace_type, info_ptr->compression_type,
1526 + info_ptr->filter_type);
1528 + png_ptr->width = width;
1529 + png_ptr->height = height;
1530 + png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
1531 + png_ptr->usr_width = png_ptr->width;
1533 +#endif /* WRITE_APNG */
1534 #endif /* WRITE */