--!movie
global $
----------------------------------------
--
----------------------------------------
on startMovie
-- CONFIG
NUM_COLORS = 128 -- number of colors used by created GIF, in range 3..256
DUR = 50 -- 50 ms = 20 fps
-- libs
$.import("console").show()
-- create an instance of the AGIF lib
agif = $.include($.PATH&"agif.ls").new()
-- load background image from file
m = new(#bitmap)
m.importFileInto($.PATH&"input\lena.bmp")
lena = m.image
lena.setAlpha(255)
lena.useAlpha = TRUE
-- create blue square image
blue_square = image(100, 100, 32)
blue_square.fill(blue_square.rect, rgb(0,0,255))
-- create a circle mask
m = new(#bitmap)
m.importFileInto($.PATH&"input\circle.bmp", [#trimWhitespace: FALSE])
alpha = image(100, 100, 8, #grayscale)
alpha.copyPixels(m.image, alpha.rect, alpha.rect)
circle_mask = alpha.createMask()
-- create a master image to extract an appropriate palette
master = lena.duplicate()
master.copyPixels(blue_square, blue_square.rect.offset(20, 20), blue_square.rect, [#maskImage:circle_mask])
palette_colors = agif.findPalette(master, NUM_COLORS)
ms = the milliseconds
-- initialize AGIF (i.e. start creation of new animated GIF)
agif.init(palette_colors, 0)
-- add static background photo, immediately go to next frame (ms=0)
agif.addFrame(lena, 0)
frame_image = image(100, 100, 24)
repeat with i = 0 to 103
-- create frame image
-- the image will be mapped by AGIF lib to the color list we passed to init()
frame_image.copyPixels(lena, frame_image.rect, frame_image.rect.offset(i*4, i*4))
frame_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
agif.addFrame(frame_image, DUR, i*4, i*4, agif.DISPOSE_OP_PREVIOUS)
end repeat
-- save GIF file
ok = agif.writeFile($.PATH&"animation_circle_"&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