notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FAVORITE BY
:
うにうにーん
:
acid
:
うねうね〜
:
图片水波效果
:
電子ドラッグ
FORKED
  1. // forked from soundkitchen's flash on 2009-10-24
  2. package
  3. {
  4.     import flash.display.Bitmap;
  5.     import flash.display.BitmapData;
  6.     import flash.display.BlendMode;
  7.     import flash.display.DisplayObject;
  8.     import flash.display.Graphics;
  9.     import flash.display.Loader;
  10.     import flash.display.LoaderInfo;
  11.     import flash.display.Sprite;
  12.     import flash.display.StageAlign;
  13.     import flash.display.StageQuality;
  14.     import flash.display.StageScaleMode;
  15.     import flash.events.Event;
  16.     import flash.events.MouseEvent;
  17.     import flash.filters.BitmapFilter;
  18.     import flash.filters.DisplacementMapFilter;
  19.     import flash.geom.Point;
  20.     import flash.net.FileFilter;
  21.     import flash.net.FileReference;
  22.     import flash.text.TextField;
  23.     import flash.text.TextFieldAutoSize;
  24.     import flash.text.TextFormat;
  25.     import flash.utils.ByteArray;
  26.     import com.flashdynamix.utils.SWFProfiler;
  27.     [SWF(width=465, height=465, frameRate=30, backgroundColor=0xffffff)]
  28.     public class Main extends Sprite
  29.     {
  30.         private var _fileRef:FileReference;
  31.         private var _fileFilters:Array = [new FileFilter("Images""*.jpg;*.png;")];
  32.         private var _currentImage:DisplayObject;
  33.         private var _imageFilter:DisplacementMapFilter;
  34.         private var _imageFilterMap:BitmapData;
  35.         private var _imageFilterOffset:Array;
  36.         public function Main()
  37.         {
  38.             addEventListener(Event.ADDED_TO_STAGE, initialize);
  39.         }
  40.         private function initialize(evt:Event):void
  41.         {
  42.             removeEventListener(Event.ADDED_TO_STAGE, initialize);
  43.             //  setup stage.
  44.             stage.align = StageAlign.TOP_LEFT;
  45.             stage.quality = StageQuality.HIGH;
  46.             stage.scaleMode = StageScaleMode.NO_SCALE;
  47.             //  setup debugger.
  48.             SWFProfiler.init(this);
  49.             createUI();
  50.             createFilter();
  51.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  52.         }
  53.         private function createFilter():void
  54.         {
  55.             //_imageFilterMap = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xffffff);
  56.             _imageFilterMap = new BitmapData(600600false, 0xffffff);
  57.             _imageFilter = new DisplacementMapFilter(_imageFilterMap, new Point(), 2200"wrap");
  58.             _imageFilterOffset = [new Point()];
  59.         }
  60.         private function createUI():void
  61.         {
  62.             var ui:Sprite,
  63.                 sp:Sprite,
  64.                 g:Graphics,
  65.                 txt:TextField,
  66.                 fmt:TextFormat,
  67.                 i:uint, pos:Number,
  68.                 labelList:Array,
  69.                 callbackList:Array;
  70.             //  ui container.
  71.             ui = addChild(new AutoHide()) as Sprite;
  72.             fmt = new TextFormat("Arial"12);
  73.             fmt.leftMargin = fmt.rightMargin = 5;
  74.             labelList = [
  75.                 "upload",
  76.                 "save",
  77.             ];
  78.             callbackList = [
  79.                 uploadClickHandler,
  80.                 saveClickHandler,
  81.             ];
  82.             pos = 10;
  83.             while (labelList.length)
  84.             {
  85.                 txt = new TextField();
  86.                 txt.autoSize = TextFieldAutoSize.LEFT;
  87.                 txt.defaultTextFormat = fmt;
  88.                 txt.selectable = false;
  89.                 txt.text = labelList.shift();
  90.                 sp = new Sprite();
  91.                 sp.x = pos;
  92.                 sp.y = 10;
  93.                 sp.mouseChildren = false;
  94.                 sp.buttonMode = true;
  95.                 sp.blendMode = BlendMode.INVERT;
  96.                 sp.addEventListener(MouseEvent.CLICK, callbackList.shift());
  97.                 g = sp.graphics;
  98.                 g.lineStyle(1, 0x666666);
  99.                 g.beginFill(0xd8d8d8, 0);
  100.                 g.drawRect(00, txt.width, txt.height);
  101.                 g.endFill();
  102.                 sp.addChild(txt);
  103.                 ui.addChild(sp);
  104.                 pos = sp.y + sp.width + 10;
  105.             }
  106.         }
  107.         private function enterFrameHandler(evt:Event):void
  108.         {
  109.             if (!_currentImage) return;
  110.             _imageFilterOffset[0].y -= 3;
  111.             _imageFilterMap.perlinNoise(stage.stageWidth >> 1, stage.stageHeight >> 115truetrue7true, _imageFilterOffset);
  112.             _imageFilter.scaleX += 1;
  113.             _imageFilter.scaleY += 1;
  114.             _currentImage.filters = [_imageFilter];
  115.         }
  116.         private function saveClickHandler(evt:MouseEvent):void
  117.         {
  118.             var data:BitmapData,
  119.                 png:ByteArray,
  120.                 file:FileReference;
  121.             if (!_currentImage) return;
  122.             data = new BitmapData(stage.stageWidth, stage.stageHeight, true0);
  123.             data.draw(_currentImage, _currentImage.transform.matrix);
  124.             png = PNGEnc.encode(data);
  125.             file = new FileReference();
  126.             file.save(png, 'hoge.png');
  127.         }
  128.         private function uploadClickHandler(evt:MouseEvent):void
  129.         {
  130.             _fileRef = new FileReference();
  131.             _fileRef.addEventListener(Event.SELECT, uploadSelectHandler);
  132.             _fileRef.browse(_fileFilters);
  133.         }
  134.         private function uploadSelectHandler(evt:Event):void
  135.         {
  136.             _fileRef.removeEventListener(Event.SELECT, uploadSelectHandler);
  137.             _fileRef.addEventListener(Event.COMPLETE, uploadCompleteHandler);
  138.             _fileRef.load();
  139.         }
  140.         private function uploadCompleteHandler(evt:Event):void
  141.         {
  142.             var loader:Loader;
  143.             _fileRef.removeEventListener(Event.COMPLETE, uploadCompleteHandler);
  144.             loader = new Loader();
  145.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
  146.             loader.loadBytes(_fileRef.data);
  147.             _fileRef = null;
  148.         }
  149.         private function loaderCompleteHandler(evt:Event):void
  150.         {
  151.             var info:LoaderInfo,
  152.                 loader:Loader,
  153.                 //bm:Bitmap,
  154.                 //data:BitmapData,
  155.                 scale:Number;
  156.             info = LoaderInfo(evt.target);
  157.             info.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
  158.             loader = info.loader;
  159.             scale = Math.max(stage.stageWidth / loader.width,
  160.                              stage.stageHeight / loader.height);
  161.             loader.scaleX = loader.scaleY = scale;
  162.             loader.x -= (loader.width - stage.stageWidth) >> 1;
  163.             loader.y -= (loader.height - stage.stageHeight) >> 1;
  164.             //data = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
  165.             //data.draw(loader, loader.transform.matrix);
  166.             //bm = new Bitmap(data);
  167.             //bm.smoothing = true;
  168.             if (_currentImage) removeChild(_currentImage);
  169.             _currentImage = addChildAt(loader, 0);
  170.         }
  171.     }
  172. }
  173. import flash.display.BitmapData;
  174. import flash.display.Sprite;
  175. import flash.events.Event;
  176. import flash.events.MouseEvent;
  177. import flash.events.TimerEvent;
  178. import flash.geom.Rectangle;
  179. import flash.utils.ByteArray;
  180. import flash.utils.Timer;
  181. class AutoHide extends Sprite
  182. {
  183.     private var _timer:Timer;
  184.     public function AutoHide()
  185.     {
  186.         _timer = new Timer(30001);
  187.         addEventListener(Event.ADDED_TO_STAGE, initialize);
  188.     }
  189.     private function initialize(evt:Event):void
  190.     {
  191.         removeEventListener(Event.ADDED_TO_STAGE, initialize);
  192.         stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  193.         _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
  194.         _timer.start();
  195.     }
  196.     private function timerCompleteHandler(evt:TimerEvent):void
  197.     {
  198.         visible = false;
  199.     }
  200.     private function mouseMoveHandler(evt:MouseEvent):void
  201.     {
  202.         visible = true;
  203.         _timer.reset();
  204.         _timer.start();
  205.     }
  206. }
  207. // http://www.5etdemi.com/blog/archives/2006/12/as3-png-encoder-faster-better/
  208. // @see Saqoosha http://wonderfl.net/code/21cfc87e11ffb354562c393a6e55666d61e15bf6
  209. class PNGEnc {
  210.     
  211.     public static function encode(img:BitmapData, type:uint = 0):ByteArray {
  212.         
  213.         // Create output byte array
  214.         var png:ByteArray = new ByteArray();
  215.         // Write PNG signature
  216.         png.writeUnsignedInt(0x89504e47);
  217.         png.writeUnsignedInt(0x0D0A1A0A);
  218.         // Build IHDR chunk
  219.         var IHDR:ByteArray = new ByteArray();
  220.         IHDR.writeInt(img.width);
  221.         IHDR.writeInt(img.height);
  222.         if(img.transparent || type == 0)
  223.         {
  224.             IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
  225.         }
  226.         else
  227.         {
  228.             IHDR.writeUnsignedInt(0x08020000); //24bit RGB
  229.         }
  230.         IHDR.writeByte(0);
  231.         writeChunk(png,0x49484452,IHDR);
  232.         // Build IDAT chunk
  233.         var IDAT:ByteArray= new ByteArray();
  234.         
  235.         switch(type)
  236.         {
  237.             case 0:
  238.                 writeRaw(img, IDAT);
  239.                 break;
  240.             case 1:
  241.                 writeSub(img, IDAT);
  242.                 break;
  243.         }
  244.         
  245.         IDAT.compress();
  246.         writeChunk(png,0x49444154,IDAT);
  247.         // Build IEND chunk
  248.         writeChunk(png,0x49454E44,null);
  249.         // return PNG
  250.         
  251.         
  252.         
  253.         return png;
  254.     }
  255.     
  256.     private static function writeRaw(img:BitmapData, IDAT:ByteArray):void
  257.     {
  258.         var h:int = img.height;
  259.         var w:int = img.width;
  260.         var transparent:Boolean = img.transparent;
  261.         
  262.         for(var i:int=0;i < h;i++) {
  263.             // no filter
  264.             if ( !transparent ) {
  265.                 var subImage:ByteArray = img.getPixels(
  266.                     new Rectangle(0, i, w, 1));
  267.                 //Here we overwrite the alpha value of the first pixel
  268.                 //to be the filter 0 flag
  269.                 subImage[0] = 0;
  270.                 IDAT.writeBytes(subImage);
  271.                 //And we add a byte at the end to wrap the alpha values
  272.                 IDAT.writeByte(0xff);
  273.             } else {
  274.                 IDAT.writeByte(0);
  275.                 var p:uint;
  276.                 for(var j:int=0;j < w;j++) {
  277.                     p = img.getPixel32(j,i);
  278.                     IDAT.writeUnsignedInt(
  279.                         uint(((p&0xFFFFFF) << 8)|
  280.                         (p>>>24)));
  281.                 }
  282.             }
  283.         }
  284.     }
  285.     
  286.     private static function writeSub(img:BitmapData, IDAT:ByteArray):void
  287.     {
  288.         var r1:uint;
  289.         var g1:uint;
  290.         var b1:uint;
  291.         var a1:uint;
  292.         
  293.         var r2:uint;
  294.         var g2:uint;
  295.         var b2:uint;
  296.         var a2:uint;
  297.         
  298.         var r3:uint;
  299.         var g3:uint;
  300.         var b3:uint;
  301.         var a3:uint;
  302.         
  303.         var p:uint;
  304.         var h:int = img.height;
  305.         var w:int = img.width;
  306.         
  307.         for(var i:int=0;i < h;i++) {
  308.             // no filter
  309.             IDAT.writeByte(1);
  310.             if ( !img.transparent ) {
  311.                 r1 = 0;
  312.                 g1 = 0;
  313.                 b1 = 0;
  314.                 a1 = 0xff;
  315.                 for(var j:int=0;j < w;j++) {
  316.                     p = img.getPixel(j,i);
  317.                     
  318.                     r2 = p >> 16 & 0xff;
  319.                     g2 = p >> 8  & 0xff;
  320.                     b2 = p & 0xff;
  321.                     
  322.                     r3 = (r2 - r1 + 256) & 0xff;
  323.                     g3 = (g2 - g1 + 256) & 0xff;
  324.                     b3 = (b2 - b1 + 256) & 0xff;
  325.                     
  326.                     IDAT.writeByte(r3);
  327.                     IDAT.writeByte(g3);
  328.                     IDAT.writeByte(b3);
  329.                     
  330.                     r1 = r2;
  331.                     g1 = g2;
  332.                     b1 = b2;
  333.                     a1 = 0;
  334.                 }
  335.             } else {
  336.                 r1 = 0;
  337.                 g1 = 0;
  338.                 b1 = 0;
  339.                 a1 = 0;
  340.                 for(j=0;j < w;j++) {
  341.                     p = img.getPixel32(j,i);
  342.                     
  343.                     a2 = p >> 24 & 0xff;
  344.                     r2 = p >> 16 & 0xff;
  345.                     g2 = p >> 8  & 0xff;
  346.                     b2 = p & 0xff;
  347.                     
  348.                     r3 = (r2 - r1 + 256) & 0xff;
  349.                     g3 = (g2 - g1 + 256) & 0xff;
  350.                     b3 = (b2 - b1 + 256) & 0xff;
  351.                     a3 = (a2 - a1 + 256) & 0xff;
  352.                     
  353.                     IDAT.writeByte(r3);
  354.                     IDAT.writeByte(g3);
  355.                     IDAT.writeByte(b3);
  356.                     IDAT.writeByte(a3);
  357.                     
  358.                     r1 = r2;
  359.                     g1 = g2;
  360.                     b1 = b2;
  361.                     a1 = a2;
  362.                 }
  363.             }
  364.         }
  365.     }
  366.     private static var crcTable:Array;
  367.     private static var crcTableComputed:Boolean = false;
  368.     private static function writeChunk(png:ByteArray, 
  369.             type:uint, data:ByteArray):void {
  370.         var c:uint;
  371.         if (!crcTableComputed) {
  372.             crcTableComputed = true;
  373.             crcTable = [];
  374.             for (var n:uint = 0; n < 256; n++) {
  375.                 c = n;
  376.                 for (var k:uint = 0; k < 8; k++) {
  377.                     if (c & 1) {
  378.                         c = uint(uint(0xedb88320) ^ 
  379.                             uint(c >>> 1));
  380.                     } else {
  381.                         c = uint(c >>> 1);
  382.                     }
  383.                 }
  384.                 crcTable[n] = c;
  385.             }
  386.         }
  387.         var len:uint = 0;
  388.         if (data != null) {
  389.             len = data.length;
  390.         }
  391.         png.writeUnsignedInt(len);
  392.         var p:uint = png.position;
  393.         png.writeUnsignedInt(type);
  394.         if ( data != null ) {
  395.             png.writeBytes(data);
  396.         }
  397.         var e:uint = png.position;
  398.         png.position = p;
  399.         c = 0xffffffff;
  400.         for (var i:int = 0; i < (e-p); i++) {
  401.             c = uint(crcTable[
  402.                 (c ^ png.readUnsignedByte()) & 
  403.                 0xff] ^ (c >>> 8));
  404.         }
  405.         c = uint(c^uint(0xffffffff));
  406.         png.position = e;
  407.         png.writeUnsignedInt(c);
  408.     }
  409. }
noswf
  1. // forked from soundkitchen's flash on 2009-10-24
  2. package
  3. {
  4.     import flash.display.Bitmap;
  5.     import flash.display.BitmapData;
  6.     import flash.display.BlendMode;
  7.     import flash.display.DisplayObject;
  8.     import flash.display.Graphics;
  9.     import flash.display.Loader;
  10.     import flash.display.LoaderInfo;
  11.     import flash.display.Sprite;
  12.     import flash.display.StageAlign;
  13.     import flash.display.StageQuality;
  14.     import flash.display.StageScaleMode;
  15.     import flash.events.Event;
  16.     import flash.events.MouseEvent;
  17.     import flash.filters.BitmapFilter;
  18.     import flash.filters.DisplacementMapFilter;
  19.     import flash.geom.Point;
  20.     import flash.net.FileFilter;
  21.     import flash.net.FileReference;
  22.     import flash.text.TextField;
  23.     import flash.text.TextFieldAutoSize;
  24.     import flash.text.TextFormat;
  25.     import flash.utils.ByteArray;
  26.     import com.flashdynamix.utils.SWFProfiler;
  27.     [SWF(width=465, height=465, frameRate=30, backgroundColor=0xffffff)]
  28.     public class Main extends Sprite
  29.     {
  30.         private var _fileRef:FileReference;
  31.         private var _fileFilters:Array = [new FileFilter("Images""*.jpg;*.png;")];
  32.         private var _currentImage:DisplayObject;
  33.         private var _imageFilter:DisplacementMapFilter;
  34.         private var _imageFilterMap:BitmapData;
  35.         private var _imageFilterOffset:Array;
  36.         public function Main()
  37.         {
  38.             addEventListener(Event.ADDED_TO_STAGE, initialize);
  39.         }
  40.         private function initialize(evt:Event):void
  41.         {
  42.             removeEventListener(Event.ADDED_TO_STAGE, initialize);
  43.             //  setup stage.
  44.             stage.align = StageAlign.TOP_LEFT;
  45.             stage.quality = StageQuality.HIGH;
  46.             stage.scaleMode = StageScaleMode.NO_SCALE;
  47.             //  setup debugger.
  48.             SWFProfiler.init(this);
  49.             createUI();
  50.             createFilter();
  51.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  52.         }
  53.         private function createFilter():void
  54.         {
  55.             //_imageFilterMap = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xffffff);
  56.             _imageFilterMap = new BitmapData(600600false, 0xffffff);
  57.             _imageFilter = new DisplacementMapFilter(_imageFilterMap, new Point(), 2200"wrap");
  58.             _imageFilterOffset = [new Point()];
  59.         }
  60.         private function createUI():void
  61.         {
  62.             var ui:Sprite,
  63.                 sp:Sprite,
  64.                 g:Graphics,
  65.                 txt:TextField,
  66.                 fmt:TextFormat,
  67.                 i:uint, pos:Number,
  68.                 labelList:Array,
  69.                 callbackList:Array;
  70.             //  ui container.
  71.             ui = addChild(new AutoHide()) as Sprite;
  72.             fmt = new TextFormat("Arial"12);
  73.             fmt.leftMargin = fmt.rightMargin = 5;
  74.             labelList = [
  75.                 "upload",
  76.                 "save",
  77.             ];
  78.             callbackList = [
  79.                 uploadClickHandler,
  80.                 saveClickHandler,
  81.             ];
  82.             pos = 10;
  83.             while (labelList.length)
  84.             {
  85.                 txt = new TextField();
  86.                 txt.autoSize = TextFieldAutoSize.LEFT;
  87.                 txt.defaultTextFormat = fmt;
  88.                 txt.selectable = false;
  89.                 txt.text = labelList.shift();
  90.                 sp = new Sprite();
  91.                 sp.x = pos;
  92.                 sp.y = 10;
  93.                 sp.mouseChildren = false;
  94.                 sp.buttonMode = true;
  95.                 sp.blendMode = BlendMode.INVERT;
  96.                 sp.addEventListener(MouseEvent.CLICK, callbackList.shift());
  97.                 g = sp.graphics;
  98.                 g.lineStyle(1, 0x666666);
  99.                 g.beginFill(0xd8d8d8, 0);
  100.                 g.drawRect(00, txt.width, txt.height);
  101.                 g.endFill();
  102.                 sp.addChild(txt);
  103.                 ui.addChild(sp);
  104.                 pos = sp.y + sp.width + 10;
  105.             }
  106.         }
  107.         private function enterFrameHandler(evt:Event):void
  108.         {
  109.             if (!_currentImage) return;
  110.             _imageFilterOffset[0].y -= 3;
  111.             _imageFilterMap.perlinNoise(stage.stageWidth >> 1, stage.stageHeight >> 115truetrue7true, _imageFilterOffset);
  112.             _imageFilter.scaleX += 1;
  113.             _imageFilter.scaleY += 1;
  114.             _currentImage.filters = [_imageFilter];
  115.         }
  116.         private function saveClickHandler(evt:MouseEvent):void
  117.         {
  118.             var data:BitmapData,
  119.                 png:ByteArray,
  120.                 file:FileReference;
  121.             if (!_currentImage) return;
  122.             data = new BitmapData(stage.stageWidth, stage.stageHeight, true0);
  123.             data.draw(_currentImage, _currentImage.transform.matrix);
  124.             png = PNGEnc.encode(data);
  125.             file = new FileReference();
  126.             file.save(png, 'hoge.png');
  127.         }
  128.         private function uploadClickHandler(evt:MouseEvent):void
  129.         {
  130.             _fileRef = new FileReference();
  131.             _fileRef.addEventListener(Event.SELECT, uploadSelectHandler);
  132.             _fileRef.browse(_fileFilters);
  133.         }
  134.         private function uploadSelectHandler(evt:Event):void
  135.         {
  136.             _fileRef.removeEventListener(Event.SELECT, uploadSelectHandler);
  137.             _fileRef.addEventListener(Event.COMPLETE, uploadCompleteHandler);
  138.             _fileRef.load();
  139.         }
  140.         private function uploadCompleteHandler(evt:Event):void
  141.         {
  142.             var loader:Loader;
  143.             _fileRef.removeEventListener(Event.COMPLETE, uploadCompleteHandler);
  144.             loader = new Loader();
  145.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
  146.             loader.loadBytes(_fileRef.data);
  147.             _fileRef = null;
  148.         }
  149.         private function loaderCompleteHandler(evt:Event):void
  150.         {
  151.             var info:LoaderInfo,
  152.                 loader:Loader,
  153.                 //bm:Bitmap,
  154.                 //data:BitmapData,
  155.                 scale:Number;
  156.             info = LoaderInfo(evt.target);
  157.             info.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
  158.             loader = info.loader;
  159.             scale = Math.max(stage.stageWidth / loader.width,
  160.                              stage.stageHeight / loader.height);
  161.             loader.scaleX = loader.scaleY = scale;
  162.             loader.x -= (loader.width - stage.stageWidth) >> 1;
  163.             loader.y -= (loader.height - stage.stageHeight) >> 1;
  164.             //data = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
  165.             //data.draw(loader, loader.transform.matrix);
  166.             //bm = new Bitmap(data);
  167.             //bm.smoothing = true;
  168.             if (_currentImage) removeChild(_currentImage);
  169.             _currentImage = addChildAt(loader, 0);
  170.         }
  171.     }
  172. }
  173. import flash.display.BitmapData;
  174. import flash.display.Sprite;
  175. import flash.events.Event;
  176. import flash.events.MouseEvent;
  177. import flash.events.TimerEvent;
  178. import flash.geom.Rectangle;
  179. import flash.utils.ByteArray;
  180. import flash.utils.Timer;
  181. class AutoHide extends Sprite
  182. {
  183.     private var _timer:Timer;
  184.     public function AutoHide()
  185.     {
  186.         _timer = new Timer(30001);
  187.         addEventListener(Event.ADDED_TO_STAGE, initialize);
  188.     }
  189.     private function initialize(evt:Event):void
  190.     {
  191.         removeEventListener(Event.ADDED_TO_STAGE, initialize);
  192.         stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  193.         _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
  194.         _timer.start();
  195.     }
  196.     private function timerCompleteHandler(evt:TimerEvent):void
  197.     {
  198.         visible = false;
  199.     }
  200.     private function mouseMoveHandler(evt:MouseEvent):void
  201.     {
  202.         visible = true;
  203.         _timer.reset();
  204.         _timer.start();
  205.     }
  206. }
  207. // http://www.5etdemi.com/blog/archives/2006/12/as3-png-encoder-faster-better/
  208. // @see Saqoosha http://wonderfl.net/code/21cfc87e11ffb354562c393a6e55666d61e15bf6
  209. class PNGEnc {
  210.     
  211.     public static function encode(img:BitmapData, type:uint = 0):ByteArray {
  212.         
  213.         // Create output byte array
  214.         var png:ByteArray = new ByteArray();
  215.         // Write PNG signature
  216.         png.writeUnsignedInt(0x89504e47);
  217.         png.writeUnsignedInt(0x0D0A1A0A);
  218.         // Build IHDR chunk
  219.         var IHDR:ByteArray = new ByteArray();
  220.         IHDR.writeInt(img.width);
  221.         IHDR.writeInt(img.height);
  222.         if(img.transparent || type == 0)
  223.         {
  224.             IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
  225.         }
  226.         else
  227.         {
  228.             IHDR.writeUnsignedInt(0x08020000); //24bit RGB
  229.         }
  230.         IHDR.writeByte(0);
  231.         writeChunk(png,0x49484452,IHDR);
  232.         // Build IDAT chunk
  233.         var IDAT:ByteArray= new ByteArray();
  234.         
  235.         switch(type)
  236.         {
  237.             case 0:
  238.                 writeRaw(img, IDAT);
  239.                 break;
  240.             case 1:
  241.                 writeSub(img, IDAT);
  242.                 break;
  243.         }
  244.         
  245.         IDAT.compress();
  246.         writeChunk(png,0x49444154,IDAT);
  247.         // Build IEND chunk
  248.         writeChunk(png,0x49454E44,null);
  249.         // return PNG
  250.         
  251.         
  252.         
  253.         return png;
  254.     }
  255.     
  256.     private static function writeRaw(img:BitmapData, IDAT:ByteArray):void
  257.     {
  258.         var h:int = img.height;
  259.         var w:int = img.width;
  260.         var transparent:Boolean = img.transparent;
  261.         
  262.         for(var i:int=0;i < h;i++) {
  263.             // no filter
  264.             if ( !transparent ) {
  265.                 var subImage:ByteArray = img.getPixels(
  266.                     new Rectangle(0, i, w, 1));
  267.                 //Here we overwrite the alpha value of the first pixel
  268.                 //to be the filter 0 flag
  269.                 subImage[0] = 0;
  270.                 IDAT.writeBytes(subImage);
  271.                 //And we add a byte at the end to wrap the alpha values
  272.                 IDAT.writeByte(0xff);
  273.             } else {
  274.                 IDAT.writeByte(0);
  275.                 var p:uint;
  276.                 for(var j:int=0;j < w;j++) {
  277.                     p = img.getPixel32(j,i);
  278.                     IDAT.writeUnsignedInt(
  279.                         uint(((p&0xFFFFFF) << 8)|
  280.                         (p>>>24)));
  281.                 }
  282.             }
  283.         }
  284.     }
  285.     
  286.     private static function writeSub(img:BitmapData, IDAT:ByteArray):void
  287.     {
  288.         var r1:uint;
  289.         var g1:uint;
  290.         var b1:uint;
  291.         var a1:uint;
  292.         
  293.         var r2:uint;
  294.         var g2:uint;
  295.         var b2:uint;
  296.         var a2:uint;
  297.         
  298.         var r3:uint;
  299.         var g3:uint;
  300.         var b3:uint;
  301.         var a3:uint;
  302.         
  303.         var p:uint;
  304.         var h:int = img.height;
  305.         var w:int = img.width;
  306.         
  307.         for(var i:int=0;i < h;i++) {
  308.             // no filter
  309.             IDAT.writeByte(1);
  310.             if ( !img.transparent ) {
  311.                 r1 = 0;
  312.                 g1 = 0;
  313.                 b1 = 0;
  314.                 a1 = 0xff;
  315.                 for(var j:int=0;j < w;j++) {
  316.                     p = img.getPixel(j,i);
  317.                     
  318.                     r2 = p >> 16 & 0xff;
  319.                     g2 = p >> 8  & 0xff;
  320.                     b2 = p & 0xff;
  321.                     
  322.                     r3 = (r2 - r1 + 256) & 0xff;
  323.                     g3 = (g2 - g1 + 256) & 0xff;
  324.                     b3 = (b2 - b1 + 256) & 0xff;
  325.                     
  326.                     IDAT.writeByte(r3);
  327.                     IDAT.writeByte(g3);
  328.                     IDAT.writeByte(b3);
  329.                     
  330.                     r1 = r2;
  331.                     g1 = g2;
  332.                     b1 = b2;
  333.                     a1 = 0;
  334.                 }
  335.             } else {
  336.                 r1 = 0;
  337.                 g1 = 0;
  338.                 b1 = 0;
  339.                 a1 = 0;
  340.                 for(j=0;j < w;j++) {
  341.                     p = img.getPixel32(j,i);
  342.                     
  343.                     a2 = p >> 24 & 0xff;
  344.                     r2 = p >> 16 & 0xff;
  345.                     g2 = p >> 8  & 0xff;
  346.                     b2 = p & 0xff;
  347.                     
  348.                     r3 = (r2 - r1 + 256) & 0xff;
  349.                     g3 = (g2 - g1 + 256) & 0xff;
  350.                     b3 = (b2 - b1 + 256) & 0xff;
  351.                     a3 = (a2 - a1 + 256) & 0xff;
  352.                     
  353.                     IDAT.writeByte(r3);
  354.                     IDAT.writeByte(g3);
  355.                     IDAT.writeByte(b3);
  356.                     IDAT.writeByte(a3);
  357.                     
  358.                     r1 = r2;
  359.                     g1 = g2;
  360.                     b1 = b2;
  361.                     a1 = a2;
  362.                 }
  363.             }
  364.         }
  365.     }
  366.     private static var crcTable:Array;
  367.     private static var crcTableComputed:Boolean = false;
  368.     private static function writeChunk(png:ByteArray, 
  369.             type:uint, data:ByteArray):void {
  370.         var c:uint;
  371.         if (!crcTableComputed) {
  372.             crcTableComputed = true;
  373.             crcTable = [];
  374.             for (var n:uint = 0; n < 256; n++) {
  375.                 c = n;
  376.                 for (var k:uint = 0; k < 8; k++) {
  377.                     if (c & 1) {
  378.                         c = uint(uint(0xedb88320) ^ 
  379.                             uint(c >>> 1));
  380.                     } else {
  381.                         c = uint(c >>> 1);
  382.                     }
  383.                 }
  384.                 crcTable[n] = c;
  385.             }
  386.         }
  387.         var len:uint = 0;
  388.         if (data != null) {
  389.             len = data.length;
  390.         }
  391.         png.writeUnsignedInt(len);
  392.         var p:uint = png.position;
  393.         png.writeUnsignedInt(type);
  394.         if ( data != null ) {
  395.             png.writeBytes(data);
  396.         }
  397.         var e:uint = png.position;
  398.         png.position = p;
  399.         c = 0xffffffff;
  400.         for (var i:int = 0; i < (e-p); i++) {
  401.             c = uint(crcTable[
  402.                 (c ^ png.readUnsignedByte()) & 
  403.                 0xff] ^ (c >>> 8));
  404.         }
  405.         c = uint(c^uint(0xffffffff));
  406.         png.position = e;
  407.         png.writeUnsignedInt(c);
  408.     }
  409. }
noswf
Get Adobe Flash Player