apng.patch 49 KB

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