1. --!movie
  2.  
  3. global $
  4.  
  5. ----------------------------------------
  6. -- This demo deliberately uses a "dumb" approach by adding full frames instead of using PNG's dispose and blend operations.
  7. -- This creates a rather huge PNG file, which is then optimized in a second step by apngopt.
  8. -- So the purpose of this demo is to demonstrate such post-process optimization - but see demo "demo_circle" for a smarter
  9. -- and faster approach, which doesn't require such post-processing.
  10. ----------------------------------------
  11. on startMovie
  12.  
  13.     -- CONFIG
  14.     NUM_COLORS = 128 -- number of colors used by created PNG, in range 3..256
  15.     DUR = 50 -- 50 ms = 20 fps
  16.  
  17.     -- libs
  18.     $.import("console").show()
  19.  
  20.     -- create an instance of the APNG lib
  21.     apng = $.include($.PATH&"apng.ls").new()
  22.  
  23.     -- load background image from file
  24.     m = new(#bitmap)
  25.     m.importFileInto($.PATH&"input\lena.bmp")
  26.     lena = m.image
  27.  
  28.     -- create blue square image
  29.     blue_square = image(100, 100, 32)
  30.     blue_square.fill(blue_square.rect, rgb(0,0,255))
  31.  
  32.     -- create a circle mask
  33.     m = new(#bitmap)
  34.     m.importFileInto($.PATH&"input\circle.bmp", [#trimWhitespace: FALSE])
  35.     alpha = image(100, 100, 8, #grayscale)
  36.     alpha.copyPixels(m.image, alpha.rect, alpha.rect)
  37.     circle_mask = alpha.createMask()
  38.  
  39.     ----------------------------------------
  40.     -- Create a "master image" for finding an optimzed color palette.
  41.     -- In this case we simply copy the blue circle on top of the photo.
  42.     -- in other cases - if different frames of your animation use completely different
  43.     -- colors - such a master image could be created by assembling various or all frames
  44.     -- to a large image.
  45.     ----------------------------------------
  46.     master_image = lena.duplicate()
  47.     master_image.copyPixels(blue_square, blue_square.rect, blue_square.rect, [#maskImage:circle_mask])
  48.     palette_colors = apng.findPalette(master_image, NUM_COLORS)
  49.  
  50.     out("Generating APNG file...")
  51.  
  52.     ms = the milliseconds
  53.  
  54.     -- initialize APNG (i.e. start creation of new APNG file)
  55.     apng.init(0, palette_colors)
  56.  
  57.     -- add frames in a loop
  58.     repeat with i = 0 to 103
  59.         -- create full frame image (dumb)
  60.         -- this image will be mapped by APNG lib to the color list we passed to init()
  61.         frame_image = lena.duplicate()
  62.         frame_image.copyPixels(blue_square, blue_square.rect.offset(i*4, i*4), blue_square.rect, [#maskImage:circle_mask])
  63.         apng.addFrame(frame_image, DUR)
  64.     end repeat
  65.  
  66.     ms = the milliseconds-ms
  67.  
  68.     -- save the APNG as file
  69.     ok = apng.writeFile($.PATH&"animation_dumb_"&NUM_COLORS&".png")
  70.     if ok then
  71.         out("APNG was generated in "&ms&" milliseconds.")
  72.     else
  73.         out("Error: failed to generate APNG file.")
  74.         return
  75.     end if
  76.  
  77.     -- optional: optimize
  78.     out("")
  79.     out("Optimizing APNG with apngopt...")
  80.     ms = the milliseconds
  81.     ok = apng.optimize_apng($.PATH&"animation_dumb_"&NUM_COLORS&".png", $.PATH&"animation_dumb_"&NUM_COLORS&"_opt.png")
  82.     ms = the milliseconds-ms
  83.     if ok then
  84.         out("Done. APNG was optimized in "&ms&" milliseconds.")
  85.     else
  86.         out("Error: failed to optimize APNG file.")
  87.         return
  88.     end if
  89. end
  90.  
[raw code]