package flur.libs.images
{
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.*;
	
	import org.bytearray.decoder.JPEGDecoder;
	
	
	/**
	 * 
	 * @author UX
	 * 
	 */	
	public class BigJPGLoader extends EventDispatcher
	{
		private var urlLoader:URLLoader;
		private var decoder:JPEGDecoder = new JPEGDecoder();
		private var pixels:Vector.<uint>;
		
		
		private var __width:uint;
		public function get width():uint
		{
			return	__width;
		}
		
		
		private var __height:uint;
		public function get height():uint
		{
			return	__height;
		}
		
		/**
		 * 
		 * @param $request
		 * 
		 */		
		public function BigJPGLoader($request:URLRequest=null)
		{
			urlLoader = new URLLoader($request);
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;;
			urlLoader.addEventListener(Event.COMPLETE, complete);
			urlLoader.addEventListener(Event.OPEN, dispatchEvent);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
			urlLoader.addEventListener(ProgressEvent.PROGRESS, dispatchEvent);
		}
		
		
		/**
		 * 
		 * 
		 */		
		public function destroy():void
		{
			close();
			
			urlLoader.removeEventListener(Event.COMPLETE, complete);
			urlLoader.removeEventListener(Event.OPEN, dispatchEvent);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
			urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
			urlLoader.removeEventListener(ProgressEvent.PROGRESS, dispatchEvent);
		}
		
		
		/**
		 * 
		 * @param $request
		 * 
		 */		
		public function load($request:URLRequest):void
		{
			urlLoader.load($request);
		}
		
		
		/**
		 * 
		 * 
		 */		
		public function close():void
		{
			try
			{
				urlLoader.close();
			}
			catch(error:Error)	{}
			__width = 0;
			__height = 0;
			pixels = null;
		}
		
		
		/**
		 * 
		 * @param $e
		 * 
		 */		
		private function complete($e:Event):void
		{
			decoder.parse(urlLoader.data);
			__width = decoder.width;
			__height = decoder.height;
			pixels = decoder.pixels;
			
			dispatchEvent($e);
		}
		
		
		/**
		 * 
		 * @param $rect
		 * @return 
		 * 
		 */			
		public function getBitmapData($rect:Rectangle):BitmapData
		{
			var w:uint = $rect.width;
			var h:uint = $rect.height;
			if(w > 8192 || h > 8192 || w * h > 16777216)	throw new Error("bitmapdata size Error");
			var bd:BitmapData = new BitmapData(w, h);
			if(!pixels)	return	bd;
			
			var addWidth:uint = width - w;
			var length:uint = pixels.length;
			
			var count:uint = width * $rect.y + $rect.x;
			var y:uint = 0;
			while(y < h)
			{
				var x:uint = 0;
				while(x < w)
				{
					if(x && y)	bd.setPixel(x, y, pixels[count]);
					++ count;
					if(length <= count)	break;
					++ x;
				}
				count += addWidth;
				if(length <= count)	break;
				++ y;
			}
			
			return	bd;
		}
	}
}
