--!movie
global $
----------------------------------------
-- This demo converts a filmstrip image with alpha channel to an APNG file with alpha channel.
----------------------------------------
on startMovie
-- CONFIG
DUR = 50 -- 50 ms = 20 fps
-- libs
$.import("console").show()
-- load strip BMP (with alpha channel and 29 frames of 100x100px) into image
m = new(#bitmap)
m.importFileInto($.PATH&"input\web_heart_strip_32bit_alpha.bmp", [#trimWhiteSpace: FALSE])
webHeart = m.image
webHeartMask = webHeart.extractAlpha().createMask()
-- create an instance of the APNG lib
apng = $.include($.PATH&"apng.ls").new()
ms = the milliseconds
-- initialize APNG (i.e. start creation of new APNG file)
apng.init(0) -- 0 = loop forever
-- add frames in a loop
w = 100
h = 100
r = rect(0, 0, w, h)
frameImg = image(w, h, 32)
frameImg.setAlpha(0)
frameImg.useAlpha = 1
cnt = webHeart.width / w
repeat with i = 1 to cnt
-- extract next frame from strip
frameImg.fill(frameImg.rect, rgb(0,0,0))
frameImg.setAlpha(0)
frameImg.copyPixels(webHeart, r, r.offset((i-1)*w, 0), [#maskImage:webHeartMask])
-- add frame to APNG
apng.addFrame(frameImg, DUR)
end repeat
ms = the milliseconds-ms
-- save the APNG as file
apng.writeFile($.PATH&"animation_web_heart.png")
out("Done. APNG was generated in "&ms&" milliseconds.")
end