AS3的JPGDecoder

不知道爲什麼,使用flex開發 IOs下的應用,碰到了Loader.loadBytes無法使用的情況,也不能Loader.load

被逼無奈,只好找了一個JPGDecoder類,自動解碼使用FileStream讀取的圖片二進制

使用Alchemy開發的,效率一般,沒有as3自己的Loader類效率高,特別是大尺寸圖片的時候,是同步解碼的,先就湊合用一下吧

準備打算用純as3來開發IOs應用了,感覺Flex的效率太低了

/**
 * __    ___  __ ___   ___                   _           
   \ \  / _ \/__\ _ \ /   \___  ___ ___   __| | ___ _ __ 
    \ \/ /_)/_\/ /_\// /\ / _ \/ __/ _ \ / _` |/ _ \ '__|
 /\_/ / ___//__ /_\\/ /_//  __/ (__ (_) | (_| |  __/ |   
 \___/\/   \__\____/___,' \___|\___\___/ \__,_|\___|_|
 
 * This class lets you decode a JPEG stream in ActionScript 3 in Flash Player 10
 * @author Thibault Imbert (bytearray.org)
 * @version 0.2 - Removed unuseful setters.
 * @version 0.3 - If needed, stream can now be passed when JPEGDecoder is instanciated.
 * @version 0.4 - Check file header before processing it.
 */

package org.bytearray.decoder
{
	import cmodule.jpegdecoder.CLibInit;
	
	import flash.utils.ByteArray;
	
	public final class JPEGDecoder
	{
		private var loader:CLibInit;
		private var lib:Object;
		private var ns:Namespace;
		private var memory:ByteArray;
		private var infos:Array;
		private var position:uint;
		private var length:uint;
		private var buffer:ByteArray = new ByteArray();
		
		private var _pixels:Vector.<uint>;
		private var _width:uint;
		private var _height:uint;
		private var _numComponents:uint;
		private var _colorComponents:uint;
		
		public static const HEADER:int = 0xFFD8;
		
		public function JPEGDecoder ( stream:ByteArray=null )
		{
			if ( stream != null ) parse( stream );
		}
		
		/**
		 * Allows you to inject a JPEG stream to decode it. 
		 * @param stream
		 * @return 
		 */		
		public function parse ( stream:ByteArray ):Vector.<uint>
		{
			stream.position = 0;
			if ( stream.readUnsignedShort() != JPEGDecoder.HEADER )
				throw new Error ("Not a valid JPEG file.");
			loader = new CLibInit();
			loader.supplyFile("stream", stream);
			lib = loader.init();
			
			ns = new Namespace("cmodule.jpegdecoder");
			memory = (ns::gstate).ds;
			infos = lib.parseJPG ("stream");
			
			_width = infos[0];
			_height = infos[1];
			_numComponents = infos[2];
			_colorComponents = infos[3];
			position = infos[4];
			length = width*height*3;
			
			buffer.length = 0;
			buffer.writeBytes(memory, position, length);
			buffer.position = 0;
			
			var lng:uint = buffer.length;
			_pixels = new Vector.<uint>(lng/3, true);
			var count:int;
			
			for ( var i:int = 0; i< lng; i+=3 )
				pixels[int(count++)] = (255 << 24 | buffer[i] << 16 | buffer[int(i+1)] << 8 | buffer[int(i+2)]);
			
			return pixels;
		}

		public function get pixels():Vector.<uint>
		{
			return _pixels;
		}

		public function get colorComponents():uint
		{
			return _colorComponents;
		}

		public function get numComponents():uint
		{
			return _numComponents;
		}

		public function get height():uint
		{
			return _height;
		}

		public function get width():uint
		{
			return _width;
		}
	}
}


下載地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章