 package aerys.qark
 {
+	import flash.display.BitmapData;
 	import flash.utils.ByteArray;
+	import flash.utils.IExternalizable;
 	import flash.utils.describeType;
 	import flash.utils.getDefinitionByName;
 	import flash.utils.getQualifiedClassName;
 
 	public class Qark
 	{
-		private static const FLAG_OBJECT		: int		= 0;
-		private static const FLAG_ARRAY			: int		= 1;
-		private static const FLAG_INTEGER		: int		= 2;
-		private static const FLAG_FLOAT			: int		= 3;
-		private static const FLAG_STRING		: int		= 4;
-		private static const FLAG_UTF_STRING	: int		= 5;
-		private static const FLAG_BYTES			: int		= 6;
-		private static const FLAG_BOOLEAN		: int		= 7;
-		private static const FLAG_CUSTOM		: int		= 8;
-		
-		private static const ENCODERS		: Array		= [encodeObject,
+		private static const FLAG_CUSTOM		: int		= 0;
+		private static const FLAG_OBJECT		: int		= 1;
+		private static const FLAG_ARRAY			: int		= 2;
+		private static const FLAG_INTEGER		: int		= 3;
+		private static const FLAG_FLOAT			: int		= 4;
+		private static const FLAG_STRING		: int		= 5;
+		private static const FLAG_UTF_STRING	: int		= 6;
+		private static const FLAG_BYTES			: int		= 7;
+		private static const FLAG_BOOLEAN		: int		= 8;
+		private static const FLAG_BITMAP_DATA	: int		= 9;
+		
+		
+		private static const ENCODERS		: Array		= [encodeCustomObject,
+														   encodeObject,
 														   encodeArray,
 														   encodeInteger,
 														   encodeFloat,

				return FLAG_BYTES;
			if (source is Boolean)
				return FLAG_BOOLEAN;
			if (getQualifiedClassName(source) == "Object")
				return FLAG_OBJECT;
			

			var className : String = decodeString(source);
			var instance : Object = new (getDefinitionByName(className))();
			
			if (instance is IExternalizable)
				(instance as IExternalizable).readExternal(source);
			else
				decodeObject(source, instance);
			
			return instance;
		}
		
		private static function encodeBitmapData(source : BitmapData, target : ByteArray) : void
		{
			var ba : ByteArray = source.getPixels(source.rect);
			
			target.writeShort(source.width);
			target.writeShort(source.height);
			
			encodeBytes(ba, target);
		}
		
		private static function decodeBitmapData(source : ByteArray) : BitmapData
		{
			var bmp : BitmapData = new BitmapData(source.readShort(),
												  source.readShort());
			
			bmp.setPixels(bmp.rect, decodeBytes(source));
			
			return bmp;
		}
	}
}