PageRenderTime 216ms CodeModel.GetById 121ms app.highlight 10ms RepoModel.GetById 80ms app.codeStats 0ms

/src/away3d/loaders/parsers/ImageParser.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 165 lines | 110 code | 27 blank | 28 comment | 38 complexity | b4f61dacc0736bfe92240148dca92270 MD5 | raw file
  1package away3d.loaders.parsers
  2
  3{
  4	import away3d.arcane;
  5	import away3d.events.AssetEvent;
  6	import away3d.library.assets.BitmapDataAsset;
  7	import away3d.textures.ATFTexture;
  8	import away3d.textures.BitmapTexture;
  9	import away3d.textures.Texture2DBase;
 10	import away3d.tools.utils.TextureUtils;
 11	
 12	import flash.display.Bitmap;
 13	import flash.display.BitmapData;
 14	import flash.display.Loader;
 15	import flash.events.Event;
 16	import flash.utils.ByteArray;
 17	
 18	use namespace arcane;
 19	
 20	/**
 21	 * ImageParser provides a "parser" for natively supported image types (jpg, png). While it simply loads bytes into
 22	 * a loader object, it wraps it in a BitmapDataResource so resource management can happen consistently without
 23	 * exception cases.
 24	 */
 25	public class ImageParser extends ParserBase
 26	{
 27		private var _byteData:ByteArray;
 28		private var _startedParsing:Boolean;
 29		private var _doneParsing:Boolean;
 30		private var _loader:Loader;
 31		
 32		/**
 33		 * Creates a new ImageParser object.
 34		 * @param uri The url or id of the data or file to be parsed.
 35		 * @param extra The holder for extra contextual data that the parser might need.
 36		 */
 37		public function ImageParser()
 38		{
 39			super(ParserDataFormat.BINARY);
 40		}
 41		
 42		/**
 43		 * Indicates whether or not a given file extension is supported by the parser.
 44		 * @param extension The file extension of a potential file to be parsed.
 45		 * @return Whether or not the given file type is supported.
 46		 */
 47		
 48		public static function supportsType(extension:String):Boolean
 49		{
 50			extension = extension.toLowerCase();
 51			return extension == "jpg" || extension == "jpeg" || extension == "png" || extension == "gif" || extension == "bmp" || extension == "atf";
 52		}
 53		
 54		/**
 55		 * Tests whether a data block can be parsed by the parser.
 56		 * @param data The data block to potentially be parsed.
 57		 * @return Whether or not the given data is supported.
 58		 */
 59		public static function supportsData(data:*):Boolean
 60		{
 61			//shortcut if asset is IFlexAsset
 62			if (data is Bitmap)
 63				return true;
 64			
 65			if (data is BitmapData)
 66				return true;
 67			
 68			if (!(data is ByteArray))
 69				return false;
 70			
 71			var ba:ByteArray = data as ByteArray;
 72			ba.position = 0;
 73			if (ba.readUnsignedShort() == 0xffd8)
 74				return true; // JPEG, maybe check for "JFIF" as well?
 75			
 76			ba.position = 0;
 77			if (ba.readShort() == 0x424D)
 78				return true; // BMP
 79			
 80			ba.position = 1;
 81			if (ba.readUTFBytes(3) == 'PNG')
 82				return true;
 83			
 84			ba.position = 0;
 85			if (ba.readUTFBytes(3) == 'GIF' && ba.readShort() == 0x3839 && ba.readByte() == 0x61)
 86				return true;
 87			
 88			ba.position = 0;
 89			if (ba.readUTFBytes(3) == 'ATF')
 90				return true;
 91			
 92			return false;
 93		}
 94		
 95		/**
 96		 * @inheritDoc
 97		 */
 98		protected override function proceedParsing():Boolean
 99		{
100			var asset:Texture2DBase;
101			if (_data is Bitmap) {
102				asset = new BitmapTexture(Bitmap(_data).bitmapData);
103				finalizeAsset(asset, _fileName);
104				return PARSING_DONE;
105			}
106			
107			if (_data is BitmapData) {
108				asset = new BitmapTexture(_data as BitmapData);
109				finalizeAsset(asset, _fileName);
110				return PARSING_DONE;
111			}
112			
113			_byteData = getByteData();
114			if (!_startedParsing) {
115				_byteData.position = 0;
116				if (_byteData.readUTFBytes(3) == 'ATF') {
117					_byteData.position = 0;
118					asset = new ATFTexture(_byteData);
119					finalizeAsset(asset, _fileName);
120					return PARSING_DONE;
121				} else {
122					_loader = new Loader();
123					_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
124					_loader.loadBytes(_byteData);
125					_startedParsing = true;
126				}
127			}
128			
129			return _doneParsing;
130		}
131		
132		/**
133		 * Called when "loading" is complete.
134		 */
135		private function onLoadComplete(event:Event):void
136		{
137			var bmp:BitmapData = Bitmap(_loader.content).bitmapData;
138			var asset:BitmapTexture;
139			
140			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
141			
142			if (!TextureUtils.isBitmapDataValid(bmp)) {
143				var bmdAsset:BitmapDataAsset = new BitmapDataAsset(bmp);
144				bmdAsset.name = _fileName;
145				
146				dispatchEvent(new AssetEvent(AssetEvent.TEXTURE_SIZE_ERROR, bmdAsset));
147				
148				bmp = new BitmapData(8, 8, false, 0x0);
149				
150				//create chekerboard for this texture rather than a new Default Material
151				var i:uint, j:uint;
152				for (i = 0; i < 8; i++) {
153					for (j = 0; j < 8; j++) {
154						if ((j & 1) ^ (i & 1))
155							bmp.setPixel(i, j, 0XFFFFFF);
156					}
157				}
158			}
159			
160			asset = new BitmapTexture(bmp);
161			finalizeAsset(asset, _fileName);
162			_doneParsing = true;
163		}
164	}
165}