--!movie
global $
----------------------------------------
-- This demo deliberately uses a "dumb" approach by adding full frames instead of using PNG's dispose and blend operations.
-- This creates a rather huge PNG file, which is then optimized in a second step by apngopt.
-- So the purpose of this demo is to demonstrate such post-process optimization - but see demo "demo_circle" for a smarter
-- and faster approach, which doesn't require such post-processing.
----------------------------------------
on startMovie
-- CONFIG
NUM_COLORS = 128 -- number of colors used by created PNG, in range 3..256
DUR = 50 -- 50 ms = 20 fps
-- libs
$.import("console").show()
-- create an instance of the APNG lib
apng = $.include($.PATH&"apng.ls").new()
-- load background image from file
m = new(#bitmap)
m.importFileInto($.PATH&"input\lena.bmp")
lena = m.image
-- 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" for finding an optimzed color palette.
-- In this case we simply copy the blue circle on top of the photo.
-- in other cases - if different frames of your animation use completely different
-- colors - such a master image could be created by assembling various or all frames
-- to a large image.
----------------------------------------
master_image = lena.duplicate()
master_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
palette_colors = apng.findPalette(master_image, NUM_COLORS)
out("Generating APNG file...")
ms = the milliseconds
-- initialize APNG (i.e. start creation of new APNG file)
apng.init(0, palette_colors)
-- add frames in a loop
repeat with i = 0 to 103
-- create full frame image (dumb)
-- this image will be mapped by APNG lib to the color list we passed to init()
frame_image = lena.duplicate()
frame_image.copyPixels(blue_square, blue_square.rect.offset(i*4, i*4), blue_square.rect, [#maskImage:circle_mask])
apng.addFrame(frame_image, DUR)
end repeat
ms = the milliseconds-ms
-- save the APNG as file
ok = apng.writeFile($.PATH&"animation_dumb_"&NUM_COLORS&".png")
if ok then
out("APNG was generated in "&ms&" milliseconds.")
else
out("Error: failed to generate APNG file.")
return
end if
-- optional: optimize
out("")
out("Optimizing APNG with apngopt...")
ms = the milliseconds
ok = apng.optimize_apng($.PATH&"animation_dumb_"&NUM_COLORS&".png", $.PATH&"animation_dumb_"&NUM_COLORS&"_opt.png")
ms = the milliseconds-ms
if ok then
out("Done. APNG was optimized in "&ms&" milliseconds.")
else
out("Error: failed to optimize APNG file.")
return
end if
end