BitmapDataをByteArrayに入れて圧縮して使いたかった

by yasurageruheya
解凍遅すぎて使えなかった
♥0 | Line 78 | Modified 2011-05-27 18:20:59 | MIT License
play

ActionScript3 source code

/**
 * Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/d7s8
 */

package {
	/**
	 * 
	 * 毛
	 */
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	import flash.system.System;
	public class Main extends Sprite
	{
		private var _txt1:TextField;
		private var _txt2:TextField;
		private var _txt3:TextField;
		
		private const TEST_COUNT:int = 10000;
		
		private var bmpdata:BitmapData;
		private var bmp:Bitmap;
		
		private var compress:ByteArray;
		
		private const w:int = 100;
		private const h:int = 100;
		
		public function Main():void
		{
			_txt1 = new TextField();
			_txt2 = new TextField();
			_txt3 = new TextField();
			
			_txt1.autoSize = _txt2.autoSize = "left";
			
			_txt2.y = 20;
			_txt3.y = 40;
			
			addChild(_txt1);
			addChild(_txt2);
			addChild(_txt3);
			
			addEventListener(MouseEvent.CLICK, test);
			
			//test();
			
			bmpdata = new BitmapData(w, h, true, 0x0);
			bmpdata.noise(1);
			
			bmp = new Bitmap(bmpdata);
			
			compress = bmpdata.getPixels(bmpdata.rect);
			
			_txt1.text = String(compress.length);
			
			compress.compress();
			
			_txt2.text = String(compress.length);
		}
		
		private function test(e:MouseEvent = null):void
		{
			var i:int = TEST_COUNT;
			
			var tmpdata:BitmapData = new BitmapData(w, h, true, 0x0);
			
			var time:int = getTimer();
			
			while (i--)
			{
				tmpdata.draw(bmp);
			}
			
			_txt1.text = String(getTimer() - time);
			
			i = TEST_COUNT;
			var ucomdata:BitmapData = new BitmapData(w, h, true, 0x0);
			var pt:Point = new Point();
			var ucombyte:ByteArray = new ByteArray();
			
			time = getTimer();
			
			while (i--)
			{
				compress.position = 0;
				ucombyte.writeBytes(compress);
				ucombyte.position = 0;
				ucombyte.uncompress();
				ucomdata.setPixels(ucomdata.rect, ucombyte);
				ucombyte.clear();
				ucombyte.position = 0;
				tmpdata.copyPixels(ucomdata, ucomdata.rect, pt);
			}
			
			_txt2.text = String(getTimer() - time);
			
			i = TEST_COUNT;
			time = getTimer();
			
			while (i--)
			{
				tmpdata.copyPixels(bmpdata, bmpdata.rect, pt);
			}
			
			_txt3.text = String(getTimer() - time);
		}
	}
}