1. --!parent
  2.  
  3. -- ************************************************************************
  4. -- Animated WebP Encoder based on img2webp
  5. -- https://developers.google.com/speed/webp/docs/img2webp
  6. --
  7. -- @author Valentin Schmidt
  8. -- @version 0.2
  9. -- @requires xtra "ImgXtra", xtra "Shell", img2webp.exe
  10. -- ************************************************************************
  11.  
  12. property _ix
  13. property _sx
  14. property _tmp_dir
  15. property _tmp_name
  16. property _tmp_type
  17. property _loop
  18. property _frames
  19.  
  20. ----------------------------------------
  21. -- @constructor
  22. ----------------------------------------
  23. on new (me)
  24.     me._ix = xtra("ImgXtra").new()
  25.     me._sx = xtra("Shell").new()
  26.     me._tmp_dir = me._sx.shell_getEnvVar("TMP")
  27.     return me
  28. end
  29.  
  30. ----------------------------------------
  31. -- Starts a new animation
  32. -- @param {integer} [num_plays=0] - how often to loop, 0 means endless looping (default)
  33. ----------------------------------------
  34. on init (me, num_plays)
  35.     if voidP(num_plays) then num_plays = 0
  36.     me._loop = num_plays
  37.     me._tmp_name = random(9999) & "_"
  38.     me._frames = []
  39. end
  40.  
  41. ----------------------------------------
  42. -- Adds new frame to animation
  43. -- @param {image} frame_image - the frame as Lingo image object
  44. -- @param {integer} [ms=100] - duration of the frame in milliseconds
  45. -- @param {integer} [qual=0] - compression quality, 0 for lossless
  46. -- @return {bool} success
  47. ----------------------------------------
  48. on addFrame (me, frame_image, ms, qual)
  49.     if voidP(ms) then ms = 100
  50.     if voidP(qual) then qual = 0
  51.  
  52.     -- export image as temporary image file in TMP dir
  53.     n = me._frames.count
  54.     if n=0 then
  55.         if frame_image.useAlpha then
  56.             me._tmp_type = ".png" -- slower, but supports alpha channel
  57.         else
  58.             me._tmp_type = ".bmp"
  59.         end if
  60.     end if
  61.     tmp_file = me._tmp_dir & "\" & me._tmp_name & n & me._tmp_type
  62.     ok = me._ix.ix_saveImage(["image":frame_image, "filename":tmp_file])
  63.     if ok then me._frames.add([#ms:ms, #q:qual])
  64.     return ok
  65. end
  66.  
  67. ----------------------------------------
  68. -- Saves as animated WebP file
  69. -- @param {string} webp_file
  70. -- @return {bool} success
  71. ----------------------------------------
  72. on writeFile (me, webp_file)
  73.  
  74.     -- run img2webp.exe
  75.     me._sx.shell_setCurrentDir(me._tmp_dir)
  76.     params = "-loop " & me._loop
  77.     cnt = me._frames.count
  78.     repeat with i = 1 to cnt
  79.         f = me._frames[i]
  80.         put " " & me._tmp_name & (i-1) & me._tmp_type & " -d " & f[#ms] after params
  81.         if f[#q]<>0 then -- lossless = default
  82.             put " -lossy -q "&f[#q] after params
  83.         end if
  84.     end repeat
  85.     put " -o " & QUOTE & webp_file & QUOTE after params
  86.     props = [:]
  87.     props["show_cmd"] = 0
  88.     props["wait"] = 1
  89.     props["parameters"] = params
  90.     exit_code = me._sx.shell_execex(the moviePath & "bin\img2webp.exe", props)
  91.     ok = (exit_code=0)
  92.  
  93.     -- delete temporary files
  94.     cmd = "del /Q " & QUOTE & "%TMP%\" & me._tmp_name & "*" & me._tmp_type & QUOTE
  95.     me._sx.shell_cmd(cmd)
  96.  
  97.     return ok
  98. end
  99.  
[raw code]