--!movie
global $
----------------------------------------
-- converts filmstrip image with alpha channel to animated GIF with transparency
----------------------------------------
on startMovie
-- CONFIG
NUM_COLORS = 128 -- number of colors used by created GIF, in range 3..256
DUR = 50 -- 50 ms = 20 fps
BG_COLOR = rgb(0,255,255) -- background color used when converting alpha to transparency
-- libs
$.import("console").show()
-- create an instance of the AGIF lib
agif = $.include($.PATH&"agif.ls").new()
-- 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()
-- find palette
palette_colors = agif.findPalette(webHeart, NUM_COLORS-1)
palette_colors[NUM_COLORS] = BG_COLOR
ms = the milliseconds
-- initialize AGIF (i.e. start creation of new animated GIF)
agif.init(palette_colors, 0)
-- add frames in a loop
w = 100
h = 100
r = rect(0, 0, w, h)
frameImg = image(w, h, 24)
cnt = webHeart.width / w
repeat with i = 1 to cnt
-- extract next frame from strip
frameImg.fill(frameImg.rect, BG_COLOR)
frameImg.copyPixels(webHeart, r, r.offset((i-1)*w, 0), [#maskImage:webHeartMask])
-- add frame
agif.addFrame(frameImg, DUR, 0, 0, agif.DISPOSE_OP_BACKGROUND, NUM_COLORS)
end repeat
-- save animated GIF
ok = agif.writeFile($.PATH&"animation_web_heart_"&NUM_COLORS&".gif")
ms = the milliseconds-ms
if ok then
out("Done. Animated GIF was generated in "&ms&" milliseconds.")
else
out("Error: failed to generate animated GIF file.")
end if
end