--!parent
-- ************************************************************************
-- Animated WebP Encoder based on img2webp
-- https://developers.google.com/speed/webp/docs/img2webp
--
-- @author Valentin Schmidt
-- @version 0.2
-- @requires xtra "ImgXtra", xtra "Shell", img2webp.exe
-- ************************************************************************
property _ix
property _sx
property _tmp_dir
property _tmp_name
property _tmp_type
property _loop
property _frames
----------------------------------------
-- @constructor
----------------------------------------
on new (me)
me._ix = xtra("ImgXtra").new()
me._sx = xtra("Shell").new()
me._tmp_dir = me._sx.shell_getEnvVar("TMP")
return me
end
----------------------------------------
-- Starts a new animation
-- @param {integer} [num_plays=0] - how often to loop, 0 means endless looping (default)
----------------------------------------
on init (me, num_plays)
if voidP(num_plays) then num_plays = 0
me._loop = num_plays
me._tmp_name = random(9999) & "_"
me._frames = []
end
----------------------------------------
-- Adds new frame to animation
-- @param {image} frame_image - the frame as Lingo image object
-- @param {integer} [ms=100] - duration of the frame in milliseconds
-- @param {integer} [qual=0] - compression quality, 0 for lossless
-- @return {bool} success
----------------------------------------
on addFrame (me, frame_image, ms, qual)
if voidP(ms) then ms = 100
if voidP(qual) then qual = 0
-- export image as temporary image file in TMP dir
n = me._frames.count
if n=0 then
if frame_image.useAlpha then
me._tmp_type = ".png" -- slower, but supports alpha channel
else
me._tmp_type = ".bmp"
end if
end if
tmp_file = me._tmp_dir & "\" & me._tmp_name & n & me._tmp_type
ok = me._ix.ix_saveImage(["image":frame_image, "filename":tmp_file])
if ok then me._frames.add([#ms:ms, #q:qual])
return ok
end
----------------------------------------
-- Saves as animated WebP file
-- @param {string} webp_file
-- @return {bool} success
----------------------------------------
on writeFile (me, webp_file)
-- run img2webp.exe
me._sx.shell_setCurrentDir(me._tmp_dir)
params = "-loop " & me._loop
cnt = me._frames.count
repeat with i = 1 to cnt
f = me._frames[i]
put " " & me._tmp_name & (i-1) & me._tmp_type & " -d " & f[#ms] after params
if f[#q]<>0 then -- lossless = default
put " -lossy -q "&f[#q] after params
end if
end repeat
put " -o " & QUOTE & webp_file & QUOTE after params
props = [:]
props["show_cmd"] = 0
props["wait"] = 1
props["parameters"] = params
exit_code = me._sx.shell_execex(the moviePath & "bin\img2webp.exe", props)
ok = (exit_code=0)
-- delete temporary files
cmd = "del /Q " & QUOTE & "%TMP%\" & me._tmp_name & "*" & me._tmp_type & QUOTE
me._sx.shell_cmd(cmd)
return ok
end