1. --!movie
  2.  
  3. global $
  4.  
  5. ----------------------------------------
  6. -- converts filmstrip image with alpha channel to animated GIF with transparency
  7. ----------------------------------------
  8. on startMovie
  9.  
  10.     -- CONFIG
  11.     NUM_COLORS = 128 -- number of colors used by created GIF, in range 3..256
  12.     DUR = 50 -- 50 ms = 20 fps
  13.     BG_COLOR = rgb(0,255,255) -- background color used when converting alpha to transparency
  14.  
  15.     -- libs
  16.     $.import("console").show()
  17.  
  18.     -- create an instance of the AGIF lib
  19.     agif = $.include($.PATH&"agif.ls").new()
  20.    
  21.     -- load strip BMP (with alpha channel and 29 frames of 100x100px) into image
  22.     m = new(#bitmap)
  23.     m.importFileInto($.PATH&"input\web_heart_strip_32bit_alpha.bmp", [#trimWhiteSpace: FALSE])
  24.     webHeart = m.image
  25.     webHeartMask = webHeart.extractAlpha().createMask()
  26.  
  27.     -- find palette
  28.     palette_colors = agif.findPalette(webHeart, NUM_COLORS-1)
  29.     palette_colors[NUM_COLORS] = BG_COLOR
  30.  
  31.     ms = the milliseconds
  32.  
  33.     -- initialize AGIF (i.e. start creation of new animated GIF)
  34.     agif.init(palette_colors, 0)
  35.  
  36.     -- add frames in a loop
  37.     w = 100
  38.     h = 100
  39.     r = rect(0, 0, w, h)
  40.     frameImg = image(w, h, 24)
  41.  
  42.     cnt = webHeart.width / w
  43.     repeat with i = 1 to cnt
  44.  
  45.         -- extract next frame from strip
  46.         frameImg.fill(frameImg.rect, BG_COLOR)
  47.         frameImg.copyPixels(webHeart, r, r.offset((i-1)*w, 0), [#maskImage:webHeartMask])
  48.  
  49.         -- add frame
  50.         agif.addFrame(frameImg, DUR, 0, 0, agif.DISPOSE_OP_BACKGROUND, NUM_COLORS)
  51.     end repeat
  52.  
  53.     -- save animated GIF
  54.     ok = agif.writeFile($.PATH&"animation_web_heart_"&NUM_COLORS&".gif")
  55.  
  56.     ms = the milliseconds-ms
  57.  
  58.     if ok then
  59.         out("Done. Animated GIF was generated in "&ms&" milliseconds.")
  60.     else
  61.         out("Error: failed to generate animated GIF file.")
  62.     end if
  63. end
  64.  
[raw code]