libpng-1.6.37-apng.patch 55 KB

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