using System;

namespace DM2CompressPic
{
	// 
	public class CGDR
	{
		// 
		public class Entry
		{
			// 
			public System.Byte[] m_Binary;
			// 
			public System.Byte[] Binary
			{
				get { return m_Binary; }
			}
		};
		// 
		public enum ImgType
		{
			Unknown,
			ExtDM1,
			Intrepray,
			U4bpp,
			U320x200x8bpp,
			U8bpp,
		};
		// 
		public class Draw8bppImg
		{
			// 
			private System.Byte[, ] m_pImage;
			// 
			private int m_cx, m_cy;
			// 
			private MaskHint m_pMH;

			// 
			public Draw8bppImg(
				int cx,
				int cy
				)
			{
				m_pImage = new System.Byte[m_cy = cy, m_cx = cx];
				m_pMH = null;
			}
			// 
			public Draw8bppImg(
				int cx,
				int cy,
				MaskHint pMH
				)
			{
				m_pImage = new System.Byte[m_cy = cy, m_cx = cx];
				m_pMH = pMH;
			}
			// 
			public bool HasMaskPalIdx()
			{
				if (m_pMH != null)
				{
					m_pMH.OnDraw8bppImageContext(this);

					return m_pMH.HasMaskPalIdx();
				}
				return false;
			}
			// 
			public System.Byte this[int y, int x]
			{
				get { return m_pImage[y, x]; }
				set { m_pImage[y, x] = value; }
			}
			// 
			public System.Byte[, ] Image
			{
				get { return m_pImage; }
			}
			// 
			public int Width
			{
				get { return m_cx; }
			}
			// 
			public int Height
			{
				get { return m_cy; }
			}
			// 
			public MaskHint MH
			{
				get { return m_pMH; }
			}
		};
		// 
		private class DrawImgEntry
		{
			// 
			private System.Byte[] m_pAlias;
			// Array is subjected in form of [y, x]
			// 0~5  = Selected 6 colors
			// 6~21 = Palette 16 colors
			//22    = Transparent color
			private System.Byte[,] m_pImage;
			// 
			private int m_cx, m_cy;

			// 
			public DrawImgEntry(
				int cx,
				int cy,
				System.Byte[] pMostUsed6,
				System.Byte[] pSelectedPal16
				)
			{
				m_pImage = new System.Byte[m_cy = cy, m_cx = cx];
				m_pAlias = new System.Byte[22];
				int i;
				for (i = 0; i < 6; i++) m_pAlias[i] = pMostUsed6[i];
				for (i = 0; i < 16; i++) m_pAlias[6 + i] = pSelectedPal16[i];
			}
			// 
			public System.Byte this[int y, int x]
			{
				get { return m_pImage[y, x]; }
				set { m_pImage[y, x] = value; }
			}
			// 
			public System.Byte[] Alias
			{
				get { return m_pAlias; }
			}
			// 
			public System.Byte[, ] Image
			{
				get { return m_pImage; }
			}
			// 
			public int Width
			{
				get { return m_cx; }
			}
			// 
			public int Height
			{
				get { return m_cy; }
			}
		};
		// 
		public class DM2Pal256
		{
			// 
			System.Drawing.Color[] m_p256;
			// 
			public DM2Pal256()
			{
				m_p256 = new System.Drawing.Color[] {
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x0C,0x08,0x00),
														System.Drawing.Color.FromArgb(0x1C,0x10,0x00),
														System.Drawing.Color.FromArgb(0x28,0x18,0x00),
														System.Drawing.Color.FromArgb(0x38,0x20,0x04),
														System.Drawing.Color.FromArgb(0x45,0x28,0x04),
														System.Drawing.Color.FromArgb(0x55,0x30,0x04),
														System.Drawing.Color.FromArgb(0x61,0x38,0x08),
														System.Drawing.Color.FromArgb(0x75,0x49,0x10),
														System.Drawing.Color.FromArgb(0x86,0x59,0x18),
														System.Drawing.Color.FromArgb(0x9A,0x65,0x20),
														System.Drawing.Color.FromArgb(0xAA,0x75,0x28),
														System.Drawing.Color.FromArgb(0xBA,0x86,0x30),
														System.Drawing.Color.FromArgb(0xCB,0x96,0x38),
														System.Drawing.Color.FromArgb(0xDB,0xA6,0x41),
														System.Drawing.Color.FromArgb(0xEF,0xB6,0x49),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x08,0x04,0x0C),
														System.Drawing.Color.FromArgb(0x14,0x08,0x18),
														System.Drawing.Color.FromArgb(0x1C,0x0C,0x24),
														System.Drawing.Color.FromArgb(0x28,0x10,0x30),
														System.Drawing.Color.FromArgb(0x34,0x14,0x3C),
														System.Drawing.Color.FromArgb(0x3C,0x18,0x49),
														System.Drawing.Color.FromArgb(0x49,0x20,0x59),
														System.Drawing.Color.FromArgb(0x59,0x24,0x6D),
														System.Drawing.Color.FromArgb(0x6D,0x2C,0x82),
														System.Drawing.Color.FromArgb(0x7D,0x30,0x96),
														System.Drawing.Color.FromArgb(0x92,0x38,0xAA),
														System.Drawing.Color.FromArgb(0xA2,0x41,0xC3),
														System.Drawing.Color.FromArgb(0xB6,0x45,0xD7),
														System.Drawing.Color.FromArgb(0xC7,0x4D,0xEB),
														System.Drawing.Color.FromArgb(0xDB,0x55,0xFF),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x0C),
														System.Drawing.Color.FromArgb(0x00,0x00,0x18),
														System.Drawing.Color.FromArgb(0x00,0x00,0x24),
														System.Drawing.Color.FromArgb(0x00,0x00,0x30),
														System.Drawing.Color.FromArgb(0x00,0x00,0x3C),
														System.Drawing.Color.FromArgb(0x00,0x00,0x49),
														System.Drawing.Color.FromArgb(0x00,0x00,0x55),
														System.Drawing.Color.FromArgb(0x00,0x00,0x69),
														System.Drawing.Color.FromArgb(0x00,0x00,0x7D),
														System.Drawing.Color.FromArgb(0x00,0x00,0x96),
														System.Drawing.Color.FromArgb(0x00,0x00,0xAA),
														System.Drawing.Color.FromArgb(0x00,0x00,0xBE),
														System.Drawing.Color.FromArgb(0x00,0x00,0xD7),
														System.Drawing.Color.FromArgb(0x00,0x00,0xEB),
														System.Drawing.Color.FromArgb(0x00,0x00,0xFF),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x04,0x04,0x10),
														System.Drawing.Color.FromArgb(0x08,0x0C,0x20),
														System.Drawing.Color.FromArgb(0x10,0x14,0x30),
														System.Drawing.Color.FromArgb(0x14,0x1C,0x41),
														System.Drawing.Color.FromArgb(0x1C,0x24,0x51),
														System.Drawing.Color.FromArgb(0x20,0x2C,0x65),
														System.Drawing.Color.FromArgb(0x28,0x34,0x75),
														System.Drawing.Color.FromArgb(0x24,0x4D,0x86),
														System.Drawing.Color.FromArgb(0x1C,0x69,0x96),
														System.Drawing.Color.FromArgb(0x18,0x82,0xAA),
														System.Drawing.Color.FromArgb(0x14,0x9A,0xBA),
														System.Drawing.Color.FromArgb(0x10,0xB2,0xCB),
														System.Drawing.Color.FromArgb(0x08,0xCF,0xDF),
														System.Drawing.Color.FromArgb(0x04,0xE7,0xEF),
														System.Drawing.Color.FromArgb(0x00,0xFF,0xFF),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x0C,0x08),
														System.Drawing.Color.FromArgb(0x20,0x1C,0x14),
														System.Drawing.Color.FromArgb(0x34,0x2C,0x1C),
														System.Drawing.Color.FromArgb(0x45,0x3C,0x28),
														System.Drawing.Color.FromArgb(0x59,0x4D,0x34),
														System.Drawing.Color.FromArgb(0x69,0x5D,0x3C),
														System.Drawing.Color.FromArgb(0x79,0x6D,0x49),
														System.Drawing.Color.FromArgb(0x8E,0x7D,0x55),
														System.Drawing.Color.FromArgb(0x9E,0x8E,0x5D),
														System.Drawing.Color.FromArgb(0xB6,0xAE,0x6D),
														System.Drawing.Color.FromArgb(0xCF,0xCB,0x79),
														System.Drawing.Color.FromArgb(0xDB,0xD7,0x8E),
														System.Drawing.Color.FromArgb(0xE7,0xDF,0x9E),
														System.Drawing.Color.FromArgb(0xF3,0xEB,0xB2),
														System.Drawing.Color.FromArgb(0xFF,0xF3,0xC7),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x08,0x0C,0x08),
														System.Drawing.Color.FromArgb(0x10,0x1C,0x10),
														System.Drawing.Color.FromArgb(0x18,0x2C,0x18),
														System.Drawing.Color.FromArgb(0x20,0x3C,0x20),
														System.Drawing.Color.FromArgb(0x28,0x49,0x28),
														System.Drawing.Color.FromArgb(0x30,0x59,0x30),
														System.Drawing.Color.FromArgb(0x38,0x69,0x38),
														System.Drawing.Color.FromArgb(0x45,0x79,0x45),
														System.Drawing.Color.FromArgb(0x51,0x8E,0x51),
														System.Drawing.Color.FromArgb(0x61,0x9E,0x61),
														System.Drawing.Color.FromArgb(0x71,0xB2,0x71),
														System.Drawing.Color.FromArgb(0x82,0xC7,0x82),
														System.Drawing.Color.FromArgb(0x92,0xDB,0x92),
														System.Drawing.Color.FromArgb(0x9E,0xEB,0x9E),
														System.Drawing.Color.FromArgb(0xAE,0xFF,0xAE),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x04,0x08,0x04),
														System.Drawing.Color.FromArgb(0x08,0x14,0x08),
														System.Drawing.Color.FromArgb(0x0C,0x20,0x0C),
														System.Drawing.Color.FromArgb(0x10,0x2C,0x10),
														System.Drawing.Color.FromArgb(0x14,0x38,0x14),
														System.Drawing.Color.FromArgb(0x20,0x51,0x20),
														System.Drawing.Color.FromArgb(0x14,0x61,0x20),
														System.Drawing.Color.FromArgb(0x08,0x71,0x20),
														System.Drawing.Color.FromArgb(0x00,0x82,0x20),
														System.Drawing.Color.FromArgb(0x04,0x8A,0x38),
														System.Drawing.Color.FromArgb(0x08,0x96,0x55),
														System.Drawing.Color.FromArgb(0x10,0xA2,0x71),
														System.Drawing.Color.FromArgb(0x2C,0xB6,0x7D),
														System.Drawing.Color.FromArgb(0x49,0xCB,0x8A),
														System.Drawing.Color.FromArgb(0x65,0xDF,0x9A),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x00,0x08,0x00),
														System.Drawing.Color.FromArgb(0x00,0x10,0x00),
														System.Drawing.Color.FromArgb(0x00,0x18,0x00),
														System.Drawing.Color.FromArgb(0x00,0x24,0x00),
														System.Drawing.Color.FromArgb(0x00,0x2C,0x00),
														System.Drawing.Color.FromArgb(0x00,0x34,0x00),
														System.Drawing.Color.FromArgb(0x00,0x49,0x00),
														System.Drawing.Color.FromArgb(0x00,0x59,0x00),
														System.Drawing.Color.FromArgb(0x00,0x6D,0x00),
														System.Drawing.Color.FromArgb(0x00,0x7D,0x00),
														System.Drawing.Color.FromArgb(0x00,0x92,0x00),
														System.Drawing.Color.FromArgb(0x00,0xB2,0x00),
														System.Drawing.Color.FromArgb(0x00,0xD3,0x00),
														System.Drawing.Color.FromArgb(0x00,0xE7,0x00),
														System.Drawing.Color.FromArgb(0x00,0xFF,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x10,0x00),
														System.Drawing.Color.FromArgb(0x20,0x20,0x00),
														System.Drawing.Color.FromArgb(0x30,0x30,0x00),
														System.Drawing.Color.FromArgb(0x45,0x45,0x00),
														System.Drawing.Color.FromArgb(0x55,0x55,0x00),
														System.Drawing.Color.FromArgb(0x65,0x65,0x00),
														System.Drawing.Color.FromArgb(0x75,0x75,0x00),
														System.Drawing.Color.FromArgb(0x8A,0x8A,0x00),
														System.Drawing.Color.FromArgb(0x9A,0x9A,0x00),
														System.Drawing.Color.FromArgb(0xAA,0xAA,0x00),
														System.Drawing.Color.FromArgb(0xBA,0xBA,0x00),
														System.Drawing.Color.FromArgb(0xCF,0xCF,0x00),
														System.Drawing.Color.FromArgb(0xDF,0xDF,0x00),
														System.Drawing.Color.FromArgb(0xEF,0xEF,0x00),
														System.Drawing.Color.FromArgb(0xFF,0xFF,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x0C,0x00),
														System.Drawing.Color.FromArgb(0x20,0x18,0x00),
														System.Drawing.Color.FromArgb(0x30,0x24,0x00),
														System.Drawing.Color.FromArgb(0x45,0x30,0x00),
														System.Drawing.Color.FromArgb(0x55,0x3C,0x00),
														System.Drawing.Color.FromArgb(0x65,0x49,0x00),
														System.Drawing.Color.FromArgb(0x75,0x55,0x00),
														System.Drawing.Color.FromArgb(0x8A,0x61,0x00),
														System.Drawing.Color.FromArgb(0x9A,0x71,0x00),
														System.Drawing.Color.FromArgb(0xAA,0x7D,0x00),
														System.Drawing.Color.FromArgb(0xBA,0x8A,0x00),
														System.Drawing.Color.FromArgb(0xCF,0x96,0x00),
														System.Drawing.Color.FromArgb(0xDF,0xA2,0x00),
														System.Drawing.Color.FromArgb(0xEF,0xAE,0x00),
														System.Drawing.Color.FromArgb(0xFF,0xBA,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x08,0x00),
														System.Drawing.Color.FromArgb(0x20,0x10,0x00),
														System.Drawing.Color.FromArgb(0x30,0x18,0x00),
														System.Drawing.Color.FromArgb(0x45,0x24,0x00),
														System.Drawing.Color.FromArgb(0x55,0x2C,0x00),
														System.Drawing.Color.FromArgb(0x65,0x34,0x00),
														System.Drawing.Color.FromArgb(0x75,0x3C,0x00),
														System.Drawing.Color.FromArgb(0x8A,0x49,0x00),
														System.Drawing.Color.FromArgb(0x9A,0x51,0x00),
														System.Drawing.Color.FromArgb(0xAA,0x59,0x00),
														System.Drawing.Color.FromArgb(0xBA,0x61,0x00),
														System.Drawing.Color.FromArgb(0xCF,0x6D,0x00),
														System.Drawing.Color.FromArgb(0xDF,0x75,0x00),
														System.Drawing.Color.FromArgb(0xEF,0x7D,0x00),
														System.Drawing.Color.FromArgb(0xFF,0x8A,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x00,0x00),
														System.Drawing.Color.FromArgb(0x20,0x00,0x00),
														System.Drawing.Color.FromArgb(0x30,0x00,0x00),
														System.Drawing.Color.FromArgb(0x45,0x00,0x00),
														System.Drawing.Color.FromArgb(0x55,0x00,0x00),
														System.Drawing.Color.FromArgb(0x65,0x00,0x00),
														System.Drawing.Color.FromArgb(0x75,0x00,0x00),
														System.Drawing.Color.FromArgb(0x8A,0x00,0x00),
														System.Drawing.Color.FromArgb(0x9A,0x00,0x00),
														System.Drawing.Color.FromArgb(0xAA,0x00,0x00),
														System.Drawing.Color.FromArgb(0xBA,0x00,0x00),
														System.Drawing.Color.FromArgb(0xCF,0x00,0x00),
														System.Drawing.Color.FromArgb(0xDF,0x00,0x00),
														System.Drawing.Color.FromArgb(0xEF,0x00,0x00),
														System.Drawing.Color.FromArgb(0xFF,0x00,0x00),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x04,0x00,0x00),
														System.Drawing.Color.FromArgb(0x0C,0x04,0x00),
														System.Drawing.Color.FromArgb(0x10,0x08,0x00),
														System.Drawing.Color.FromArgb(0x18,0x0C,0x00),
														System.Drawing.Color.FromArgb(0x20,0x0C,0x00),
														System.Drawing.Color.FromArgb(0x2C,0x14,0x04),
														System.Drawing.Color.FromArgb(0x34,0x18,0x04),
														System.Drawing.Color.FromArgb(0x41,0x1C,0x04),
														System.Drawing.Color.FromArgb(0x4D,0x20,0x08),
														System.Drawing.Color.FromArgb(0x59,0x28,0x08),
														System.Drawing.Color.FromArgb(0x65,0x30,0x10),
														System.Drawing.Color.FromArgb(0x6D,0x38,0x14),
														System.Drawing.Color.FromArgb(0x79,0x45,0x1C),
														System.Drawing.Color.FromArgb(0x86,0x4D,0x20),
														System.Drawing.Color.FromArgb(0x92,0x55,0x28),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x10,0x0C,0x08),
														System.Drawing.Color.FromArgb(0x20,0x18,0x10),
														System.Drawing.Color.FromArgb(0x34,0x24,0x1C),
														System.Drawing.Color.FromArgb(0x45,0x30,0x24),
														System.Drawing.Color.FromArgb(0x55,0x3C,0x2C),
														System.Drawing.Color.FromArgb(0x69,0x49,0x38),
														System.Drawing.Color.FromArgb(0x79,0x55,0x41),
														System.Drawing.Color.FromArgb(0x8A,0x61,0x49),
														System.Drawing.Color.FromArgb(0x9E,0x6D,0x55),
														System.Drawing.Color.FromArgb(0xAE,0x79,0x5D),
														System.Drawing.Color.FromArgb(0xBE,0x86,0x65),
														System.Drawing.Color.FromArgb(0xD3,0x92,0x71),
														System.Drawing.Color.FromArgb(0xDF,0xA6,0x7D),
														System.Drawing.Color.FromArgb(0xEF,0xBA,0x8A),
														System.Drawing.Color.FromArgb(0xFF,0xCF,0x9A),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x08,0x08,0x08),
														System.Drawing.Color.FromArgb(0x14,0x14,0x10),
														System.Drawing.Color.FromArgb(0x20,0x20,0x18),
														System.Drawing.Color.FromArgb(0x2C,0x2C,0x24),
														System.Drawing.Color.FromArgb(0x38,0x38,0x2C),
														System.Drawing.Color.FromArgb(0x45,0x45,0x34),
														System.Drawing.Color.FromArgb(0x51,0x51,0x41),
														System.Drawing.Color.FromArgb(0x61,0x61,0x51),
														System.Drawing.Color.FromArgb(0x71,0x71,0x61),
														System.Drawing.Color.FromArgb(0x82,0x82,0x71),
														System.Drawing.Color.FromArgb(0x92,0x92,0x82),
														System.Drawing.Color.FromArgb(0xA2,0xA2,0x92),
														System.Drawing.Color.FromArgb(0xB2,0xB2,0xA2),
														System.Drawing.Color.FromArgb(0xC3,0xC3,0xB2),
														System.Drawing.Color.FromArgb(0xD3,0xD3,0xC3),
														System.Drawing.Color.FromArgb(0x00,0x00,0x00),
														System.Drawing.Color.FromArgb(0x08,0x08,0x08),
														System.Drawing.Color.FromArgb(0x14,0x14,0x14),
														System.Drawing.Color.FromArgb(0x1C,0x1C,0x1C),
														System.Drawing.Color.FromArgb(0x28,0x28,0x28),
														System.Drawing.Color.FromArgb(0x34,0x34,0x34),
														System.Drawing.Color.FromArgb(0x3C,0x3C,0x3C),
														System.Drawing.Color.FromArgb(0x51,0x51,0x51),
														System.Drawing.Color.FromArgb(0x61,0x61,0x61),
														System.Drawing.Color.FromArgb(0x71,0x71,0x71),
														System.Drawing.Color.FromArgb(0x92,0x92,0x92),
														System.Drawing.Color.FromArgb(0xA2,0xA2,0xA2),
														System.Drawing.Color.FromArgb(0xB2,0xB2,0xB2),
														System.Drawing.Color.FromArgb(0xCB,0xCB,0xCB),
														System.Drawing.Color.FromArgb(0xE7,0xE7,0xE7),
														System.Drawing.Color.FromArgb(0xFF,0xFF,0xFF)
													};
			}
			// 
			public System.Drawing.Color this[int i]
			{
				get { return m_p256[i]; }
			}
		}
		// 
		public class MaskHint
		{
			// 
			protected int m_iMaskHint, m_iPalIdx;
			// 
			public MaskHint()
			{
				m_iMaskHint = m_iPalIdx = -1;
			}
			// 
			public MaskHint(int iMaskHint, bool fAbsPal)
			{
				m_iMaskHint = iMaskHint;
				m_iPalIdx = (fAbsPal) ? iMaskHint : -1;
			}
			// 
			public bool HasMaskPalIdx()
			{
				return m_iPalIdx >= 0 ? true : false;
			}
			// 
			public int GetMaskPalIdx()
			{
				return m_iPalIdx;
			}
			// 
			public virtual void OnUncompress(System.Byte[] pBin16)
			{
				if (m_iMaskHint >= 0 && m_iMaskHint < 16)
					m_iPalIdx = pBin16[m_iMaskHint];
			}
			// 
			public virtual void OnDraw8bppImageContext(Draw8bppImg pImg)
			{

			}
		};

		// 
		private bool m_fLittleEndian;
		// 
		private static readonly DM2Pal256 m_DM2AllPal = new DM2Pal256();
		// 
		private static readonly System.Drawing.Color[] m_DM1Pal = new System.Drawing.Color[] {
																								 System.Drawing.Color.FromArgb(  0,  0,  0),//00 00 00//
																								 System.Drawing.Color.FromArgb(108,108,108),//1B 1B 1B//
																								 System.Drawing.Color.FromArgb(144,144,144),//24 24 24//
																								 System.Drawing.Color.FromArgb(108, 36,  0),//1B 09 00//
																								 System.Drawing.Color.FromArgb(  0,216,216),//00 36 36//
																								 System.Drawing.Color.FromArgb(144, 72,  0),//24 12 00//
																								 System.Drawing.Color.FromArgb(  0,144,  0),//00 24 00//
																								 System.Drawing.Color.FromArgb(  0,216,  0),//00 36 00//
																								 System.Drawing.Color.FromArgb(255,  0,  0),//3F 3F 00//
																								 System.Drawing.Color.FromArgb(224,160,  0),//38 28 00//
																								 System.Drawing.Color.FromArgb(224,128,  0),//38 20 00//
																								 System.Drawing.Color.FromArgb(255,255,  0),//3F 3F 00//
																								 System.Drawing.Color.FromArgb( 72, 72, 72),//12 12 12//
																								 System.Drawing.Color.FromArgb(180,180,180),//2D 2D 2D//
																								 System.Drawing.Color.FromArgb(  0,  0,255),//00 00 3F//
																								 System.Drawing.Color.FromArgb(255,255,255) //3F 3F 3F//
																							 };

		// 
		public CGDR(bool fLittleEndian)
		{
			m_fLittleEndian = fLittleEndian;
		}
		// 
		~CGDR()
		{

		}
		// 
		public bool ReadEntryAsImage(
			Entry pEntry,
			MaskHint pMH,
			out System.Drawing.Bitmap pImgFg
			)
		{
			pImgFg = null;
			try
			{
				ImgType which = DecideImgType(pEntry);
				System.Diagnostics.Debug.WriteLine(which.ToString());
				switch (which)
				{
					case ImgType.Unknown:
						return false;
					case ImgType.Intrepray:
						if (true) 
						{
							Draw8bppImg pDrawEntity = null;
							if (!TryToReadAsIntreprayImg(pEntry.m_Binary, pMH, out pDrawEntity))
								return false;
							PlayImgEntryAsIntreprayImg(pDrawEntity, out pImgFg);
							return true;
						}
					case ImgType.U4bpp:
						if (true)
						{
							if (!TryToReadAsU4bppImg(pEntry, out pImgFg))
								return false;
							return true;
						}
					case ImgType.U8bpp:
						if (true)
						{
							if (!TryToReadAsU8bppImg(pEntry, out pImgFg))
								return false;
							return true;
						}
					case ImgType.U320x200x8bpp:
						if (true)
						{
							if (!TryToReadAs320x200x8bppImg(pEntry, out pImgFg))
								return false;
							return true;
						}
					case ImgType.ExtDM1:
						if (true)
						{
							Draw8bppImg pDrawEntity = null;

							DrawImgEntry pEntity;

							TryToReadAsExtDM1Img(
								pEntry.m_Binary,
								false,
								pMH,
								out pEntity
								);

							pDrawEntity = new Draw8bppImg(pEntity.Width, pEntity.Height, pMH);

							CombineImgEntryAsExtDM1(
								pDrawEntity,
								pEntity,
								true
								);

							PlayImgEntryAsExtDM1(
								pDrawEntity,
								out pImgFg
								);

							return true;
						}
				}
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		public static void NormalizeImageToDM2Pal(
			System.Drawing.Bitmap pImg,
			out System.Drawing.Bitmap pNewImg
			)
		{
			int x1, y1, cx = pImg.Width, cy = pImg.Height;
			Draw8bppImg pDrawEntity = new Draw8bppImg(cx, cy);
			for (y1 = 0; y1 < cy; y1++)
			{
				for (x1 = 0; x1 < cx; x1++)
				{
					pDrawEntity.Image[y1, x1] = GetNearestDM2PalIdx(pImg.GetPixel(x1, y1));
				}
			}
			PlayImgEntryAsExtDM1(pDrawEntity, out pNewImg);
		}

		// 
		static System.Byte GetNearestDM2PalIdx(
			System.Drawing.Color pc
			)
		{
			int iLowest = 256 * 3;
			int iLowestPalIdx = 0;
			for (int i = 0; i < 256; i++)
			{
				int v1 = DM2Pal[i].R - pc.R;
				int v2 = DM2Pal[i].G - pc.G;
				int v3 = DM2Pal[i].B - pc.B;
				if (v1 < 0) v1 = -v1;
				if (v2 < 0) v2 = -v2;
				if (v3 < 0) v3 = -v3;
				int vz = v1 + v2 + v3;
				if (vz < iLowest)
				{
					iLowest = vz;
					iLowestPalIdx = i;
				}
			}
			return (System.Byte)iLowestPalIdx;
		}
		// 
		static private bool PlayImgEntryAsExtDM1(
			Draw8bppImg pDrawEntity,
			out System.Drawing.Bitmap pImg
			)
		{
			pImg = null;

			try
			{
				int x, y, cx = pDrawEntity.Width, cy = pDrawEntity.Height;
				pImg = new System.Drawing.Bitmap(
					cx,
					cy,
					System.Drawing.Imaging.PixelFormat.Format8bppIndexed
					);

				int i;
				System.Drawing.Imaging.ColorPalette pPal = pImg.Palette;
				for (i = 0; i < 256; i++)
				{
					pPal.Entries[i] = m_DM2AllPal[i];
				}
				if (pDrawEntity.HasMaskPalIdx())
				{
					i = pDrawEntity.MH.GetMaskPalIdx();
					pPal.Entries[i] = System.Drawing.Color.FromArgb(0, pPal.Entries[i]);
				}
				pImg.Palette = pPal;

				System.Drawing.Imaging.BitmapData pBD = pImg.LockBits(
					new System.Drawing.Rectangle(0, 0, cx, cy),
					System.Drawing.Imaging.ImageLockMode.WriteOnly,
					System.Drawing.Imaging.PixelFormat.Format8bppIndexed
					);

				System.Byte[] pBuffer = new System.Byte[pBD.Stride * cy];
				for (y = 0; y < cy; y++)
				{
					for (x = 0; x < cx; x++)
					{
						pBuffer[pBD.Stride * y + x] = pDrawEntity.Image[y, x];
					}
				}

				System.Runtime.InteropServices.Marshal.Copy(
					pBuffer, 
					0, 
					pBD.Scan0,
					pBuffer.Length
					);
				pImg.UnlockBits(pBD);
				return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private int ReadDistance(BinaryThruRead4Bit ReadIn)
		{
			int Distance;
			System.Byte Lead;
			Lead = ReadIn.Read4Bit();
			if (Lead == 0xf) // F
			{
				Lead = ReadIn.Read4Bit();
				if (Lead == 0xf) // FF
				{
					Lead = ReadIn.Read4Bit();
					if (Lead == 0xf) // FFF
					{
						Lead = ReadIn.Read4Bit(); // 12~15
						Distance = Lead;
						Lead = ReadIn.Read4Bit(); // 08~11
						Distance = (Distance << 4) + Lead;
						Lead = ReadIn.Read4Bit(); // 04~07
						Distance = (Distance << 4) + Lead;
						Lead = ReadIn.Read4Bit(); // 00~03
						Distance = (Distance << 4) + Lead;
					}
					else
					{
						Distance = Lead + 257;
					}
				}
				else
				{
					Distance = Lead;
					Lead = ReadIn.Read4Bit();
					Distance = 17 + (Distance << 4) + Lead;
				}
			}
			else
			{
				Distance = Lead + 2;
			}
			return Distance;
		}
		// 
		private bool Uncompress(
			int cx,
			int cy,
			BinaryThruRead4Bit pThruIn,
			System.Byte[] pMostUsed6,
			System.Byte[] pSelectedPal16,
			out DrawImgEntry pEntity,
			bool fOverlapImage
			)
		{
			pEntity = null;
			if (1 <= cx && 1 <= cy)
			{
				int x1 = 0, y1 = 0, t;
				pEntity = new DrawImgEntry(cx, cy, pMostUsed6, pSelectedPal16);
				System.Byte iLead, iNextLead;
				System.Byte iPalIdx = 0xFF;
				int iDistance;

				try
				{
					while (y1 < cy)
					{
						iLead = pThruIn.Read4Bit();

						switch (iLead & 0x07)
						{
							case 0:
							case 1:
							case 2:
							case 3:
							case 4:
							case 5:
								iPalIdx = (System.Byte)(iLead & 7);
								break;
							case 6:
								iPalIdx = 22;
								break;
							case 7:
								iNextLead = pThruIn.Read4Bit();
								iPalIdx = (System.Byte)(6 + iNextLead);
								break;
						}
						iDistance = 1;
						if ((iLead & 0x08) == 0x08)
						{
							iDistance = ReadDistance(pThruIn);
						}

						while (iDistance > 0)
						{
							while (cx <= x1)
							{
								x1 -= cx;
								y1++;
							}
							if (cy <= y1)
								break;
							if (x1 + iDistance <= cx)
							{
								for (t = 0; t < iDistance; t++, x1++)
								{
									pEntity[y1, x1] = iPalIdx;
								}
								iDistance = 0;
							}
							else
							{
								int n = cx - x1;
								for (t = 0; t < n; t++, x1++)
								{
									pEntity[y1, x1] = iPalIdx;
								}
								iDistance -= n;
								x1 = 0;
								y1++;
							}
						}

						if (x1 == cx)
						{
							x1 = 0; y1++;
						}
					}
					return true;
				}
				catch(System.Exception err)
				{
					System.Diagnostics.Debug.WriteLine(err.Message);
				}
			}
			return false;
		}
		// 
		private bool TryToReadAsIntreprayImg(
			System.Byte[] pBinary,
			MaskHint pMH,
			out Draw8bppImg pDrawEntity
			)
		{
			pDrawEntity = null;
			int nLen = pBinary.Length;
			if (nLen < 8 || !((pBinary[6] == 3 || pBinary[6] == 2) && pBinary[7] == 0))
				return false;
			try
			{
				int cx, cy;
				cx = pBinary[0] | ((pBinary[1]&0x01) * 256);
				cy = pBinary[2] | ((pBinary[3]&0x01) * 256);
				System.IO.MemoryStream pFileIn = new System.IO.MemoryStream(pBinary, 8, nLen - 8, false);
				int iPixel = 0, nPixels = cx * cy, iBit;
				pDrawEntity = new Draw8bppImg(cx, cy, pMH);
				bool fIs3 = pBinary[6] == 3;
				while (iPixel < nPixels)
				{
					int nLead = pFileIn.ReadByte();
					if (nLead < 0) break;
					for (iBit = 0; iBit < 8; iBit++)
					{
						if ((nLead & (1 << iBit)) != 0)
						{
							int v1 = pFileIn.ReadByte();
							if (v1 < 0) break;
							pDrawEntity.Image[iPixel / cx, iPixel % cx] = (System.Byte)v1;
							iPixel++;
						}
						else
						{
							int v1 = pFileIn.ReadByte();
							int v2 = pFileIn.ReadByte();
							if (v1 < 0 || v2 < 0) break;
							int nBack = fIs3
								? ((v1 >> 5) + 8 * v2)
								: ((v1 >> 4) + 16 * v2);
							int nSize = fIs3
								? ((v1 & 0x1F) + 3)
								: ((v1 & 0x0F) + 3);
							for (int v = 0; v < nSize; v++)
							{
								int iPixelFrom = iPixel - nBack;
								pDrawEntity.Image[iPixel / cx, iPixel % cx] = pDrawEntity.Image[iPixelFrom / cx, iPixelFrom % cx];
								iPixel++;
							}
						}
					}
				}
				if (iPixel == nPixels) return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private bool TryToReadAsExtDM1Img(
			System.Byte[] pBinary,
			bool fOverlapImage,
			MaskHint pMH,
			out DrawImgEntry pEntity
			)
		{
			pEntity = null;

			if (23 <= pBinary.Length)
			{
				int cx, cy;
				if (m_fLittleEndian)
				{
					cx = pBinary[0] | ((pBinary[1]&0x01) * 256);
					cy = pBinary[2] | ((pBinary[3]&0x01) * 256);
				}
				else
				{
					cx = ((pBinary[0]&0x01) * 256) | pBinary[1];
					cy = ((pBinary[2]&0x01) * 256) | pBinary[3];
				}
				System.Byte[] pMostUsed6 = new System.Byte[6];
				System.Byte[] pSelectedPal16 = new System.Byte[16];

				for (int iNewPal = 0; iNewPal < 16; iNewPal++)
				{
					pSelectedPal16[iNewPal] = pBinary[pBinary.Length - 16 + iNewPal];
				}

				System.UInt32 iPal2Color = (System.UInt32)(0
					+ pBinary[4] * 256 * 256
					+ pBinary[5] * 256
					+ pBinary[6]
					);
				Pal2ColorIdxer pIdxer = new Pal2ColorIdxer(iPal2Color, fOverlapImage ? 5 : 6);
				pIdxer.GetMostUsed6Colors(pMostUsed6);

				//System.String sMostUsed6 = pIdxer.ToString();
				//System.Diagnostics.Debug.WriteLine("6          :" + sMostUsed6);

				for (int iMostUsed6 = 0; iMostUsed6 < 6; iMostUsed6++)
				{
					pMostUsed6[iMostUsed6] = pSelectedPal16[pMostUsed6[iMostUsed6]];
				}

				System.IO.MemoryStream Mem = new System.IO.MemoryStream(pBinary, 6, pBinary.Length - 6 - 16, false);
				BinaryThruRead4Bit pThruIn = new BinaryThruRead4Bit(Mem);

				pThruIn.Read4Bit();
				if (!fOverlapImage)
					pThruIn.Read4Bit();

				Uncompress(cx, cy, pThruIn, pMostUsed6, pSelectedPal16, out pEntity, fOverlapImage);

				if (pMH != null)
					pMH.OnUncompress(pSelectedPal16);

				return true;
			}
			return false;
		}
		// 
		private ImgType DecideImgType(
			Entry pEntry
			)
		{
			//
			if (TryToDecodeImageAsU4bpp(pEntry))
				return ImgType.U4bpp;
			if (TryToDecodeImageAsU8bpp(pEntry))
				return ImgType.U8bpp;
			if (TryToDecodeImageAs320x200(pEntry))
				return ImgType.U320x200x8bpp;
			//
			if (TryToDecodeImageAsIntrepray(pEntry))
				return ImgType.Intrepray;
			//
			if (TryToDecodeImageAsExtDM1(pEntry))
				return ImgType.ExtDM1;
			//
			return ImgType.Unknown;
		}
		// 
		private bool TryToDecodeImageAsExtDM1(
			Entry pEntry
			)
		{
			System.Byte[] pBinary = pEntry.m_Binary;
			int nLen = pBinary.Length;
			if (nLen <= 7 + 16)
				return false;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			try
			{
				System.IO.MemoryStream pFileIn = new System.IO.MemoryStream(pBinary, 7, nLen - 7 - 16, false);
				BinaryThruRead4Bit pThruIn = new BinaryThruRead4Bit(pFileIn);
				int iPixel = 0, nPixels = cx * cy;
				System.Byte nLead;
				bool fEmpty = false;
				while (iPixel < nPixels)
				{
					nLead = pThruIn.Read4Bit();
					switch (nLead & 7)
					{
						case 0:
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
							fEmpty = false;
							break;
						case 6:
							fEmpty = true;
							break;
						case 7:
							pThruIn.Read4Bit();
							break;
					}
					int nDistance = 1;
					if ((nLead & 8) != 0)
					{
						nDistance = ReadDistance(pThruIn);
					}
					//System.Diagnostics.Debug.WriteLine(string.Format("{0:00},{1:00000},{2:##########}", 0, nDistance, iPixel));
					//System.Diagnostics.Debug.WriteLine("D:" + nDistance.ToString());
					int i;
					for (i = 0; i < nDistance && (!fEmpty || (fEmpty && (cx <= iPixel))); i++, iPixel++);
					if (i != nDistance) break;
				}
				if (iPixel == nPixels && pFileIn.Position == pFileIn.Length) return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private bool TryToDecodeImageAsIntrepray(
			Entry pEntry
			)
		{
			System.Byte[] pBinary = pEntry.m_Binary;
			int nLen = pBinary.Length;
			if (nLen <= 8)
				return false;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			switch (pBinary[6])
			{
				case 2:
				case 3:
					break;
				default:
					return false;
			}
			bool fIs3 = pBinary[6] == 3;
			// System.Diagnostics.Debug.Assert(fIs3, "Type 2");
			try
			{
				System.IO.MemoryStream pFileIn = new System.IO.MemoryStream(pBinary, 8, nLen - 8, false);
				int iPixel = 0, nPixels = cx * cy;
				int nLead, nBit, v1, v2;
				while (iPixel < nPixels)
				{
					nLead = pFileIn.ReadByte();
					if (nLead < 0) break;
					for (nBit = 0; nBit < 8 && iPixel < nPixels; nBit++)
					{
						if ((nLead & (1 << nBit)) != 0)
						{
							v1 = pFileIn.ReadByte();
							if (v1 < 0) break;
							iPixel++;
							//System.Diagnostics.Debug.WriteLine("P:");
						}
						else
						{
							v1 = pFileIn.ReadByte();
							v2 = pFileIn.ReadByte();
							if (v1 < 0 || v2 < 0) break;
							if (fIs3)
							{
								if (iPixel < (v1 >> 5) + 8 * v2) break;
								iPixel += (v1 & 0x1F) + 3;
								//System.Diagnostics.Debug.WriteLine("R:"+((v1 & 0x1F) + 3).ToString());
							}
							else
							{
								if (iPixel < (v1 >> 4) + 16 * v2) break;
								iPixel += (v1 & 0x0F) + 3;
								//System.Diagnostics.Debug.WriteLine("R:"+((v1 & 0x0F) + 3).ToString());
							}
						}
					}
				}
				if (iPixel == nPixels && pFileIn.Position == pFileIn.Length) return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private bool TryToDecodeImageAsU4bpp(
			Entry pEntry
			)
		{
			System.Byte[] pBinary = pEntry.m_Binary;
			int nLen = pBinary.Length;
			if (nLen <= 7 + 16)
				return false;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			int ey = pBinary[m_fLittleEndian ? 3 : 2];
			if (nLen == 10 + (cx + 1) / 2 * cy + 16)
				if ((ey & 0x7C) != 0x7C) // FIXME:KILL BIT
					return true;
			return false;
		}
		// 
		private bool TryToDecodeImageAsU8bpp(
			Entry pEntry
			)
		{
			System.Byte[] pBinary = pEntry.m_Binary;
			int nLen = pBinary.Length;
			if (nLen <= 10)
				return false;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			if (nLen == 10 + cx * cy)
				return true;
			return false;
		}
		// 
		private bool TryToDecodeImageAs320x200(
			Entry pEntry
			)
		{
			if (pEntry.m_Binary.Length == 64000)
				return true;
			return false;
		}
		// 
		private void PlayImgEntryAsIntreprayImg(
			Draw8bppImg pDrawEntity,
			out System.Drawing.Bitmap pImg
			)
		{
			PlayImgEntryAsExtDM1(pDrawEntity, out pImg);
		}
		// 
		private bool TryToReadAs320x200x8bppImg(
			Entry pEntry,
			out System.Drawing.Bitmap pImg
			)
		{
			pImg = null;
			if (pEntry.m_Binary.Length < 64000)
				return false;
			try
			{
				Draw8bppImg pDrawEntity = new Draw8bppImg(320, 200);
				int i = 0;
				for (int y1 = 0; y1 < 200; y1++)
				{
					for (int x1 = 0; x1 < 320; x1++)
					{
						pDrawEntity.Image[y1, x1] = pEntry.m_Binary[i];
						i++;
					}
				}
				PlayImgEntryAsExtDM1(
					pDrawEntity,
					out pImg
					);
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return true;
		}
		// 
		private bool TryToReadAsU4bppImg(
			Entry pEntry,
			out System.Drawing.Bitmap pImg
			)
		{
			pImg = null;
			int nLen = pEntry.m_Binary.Length;
			if (nLen < 10 + 16)
				return false;
			System.Byte[] pBinary = pEntry.m_Binary;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			if (nLen < 10 + (cx + 1) / 2 * cy + 16)
				return false;
			try
			{
				Draw8bppImg pDrawEntity = new Draw8bppImg(cx, cy);
				System.IO.MemoryStream pFileIn = new System.IO.MemoryStream(pEntry.m_Binary, 10, nLen - 10 - 16, false);
				BinaryThruRead4Bit pThruIn = new BinaryThruRead4Bit(pFileIn);
				int iPal, iPalPos = nLen - 16;
				for (int y1 = 0; y1 < cy; y1++)
				{
					for (int x1 = 0; x1 < cx; x1++)
					{
						iPal = pThruIn.Read4Bit();
						iPal = pEntry.m_Binary[iPalPos + iPal];
						pDrawEntity.Image[y1, x1] = (System.Byte)iPal;
					}
					if ((cx & 1) != 0) pThruIn.Read4Bit();
				}
				PlayImgEntryAsExtDM1(
					pDrawEntity,
					out pImg
					);
				return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private bool TryToReadAsU8bppImg(
			Entry pEntry,
			out System.Drawing.Bitmap pImg
			)
		{
			pImg = null;
			int nLen = pEntry.m_Binary.Length;
			if (nLen < 10)
				return false;
			System.Byte[] pBinary = pEntry.Binary;
			int cx = pBinary[m_fLittleEndian ? 0 : 1] | ((pBinary[m_fLittleEndian ? 1 : 0] & 0x01) << 8);
			int cy = pBinary[m_fLittleEndian ? 2 : 3] | ((pBinary[m_fLittleEndian ? 3 : 2] & 0x01) << 8);
			if (nLen < 10 + cx * cy)
				return false;
			try
			{
				Draw8bppImg pDrawEntity = new Draw8bppImg(cx, cy);
				for (int y1 = 0; y1 < cy; y1++)
				{
					for (int x1 = 0; x1 < cx; x1++)
					{
						pDrawEntity.Image[y1, x1] = pBinary[10 + cx * y1 + x1];
					}
				}
				PlayImgEntryAsExtDM1(
					pDrawEntity,
					out pImg
					);
				return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}
			return false;
		}
		// 
		private void CombineImgEntryAsExtDM1(
			Draw8bppImg pDrawEntity,
			DrawImgEntry pEntity,
			bool fFirstImage
			)
		{
			int cx = pEntity.Width, cy = pEntity.Height;
			int x1, y1;
			System.Collections.BitArray pMaskBit = new System.Collections.BitArray(22, true);
			if (!fFirstImage) pMaskBit[5] = false;
			for (y1 = 0; y1 < cy; y1++)
			{
				for (x1 = 0; x1 < cx; x1++)
				{
					if (pEntity[y1, x1] == 22)
					{
						pDrawEntity[y1, x1] = pDrawEntity[y1 - 1, x1];
					}
					else if (pMaskBit[pEntity[y1, x1]])
					{
						pDrawEntity[y1, x1] = pEntity.Alias[pEntity[y1, x1]];
					}
				}
			}
		}
		// 
		static public Entry CreateEntryFromFile(
			System.String sFile
			)
		{
			Entry pEntry = new Entry();
			System.IO.Stream pFileIn = System.IO.File.OpenRead(sFile);
			int nLen = System.Convert.ToInt32(pFileIn.Length);
			if (nLen > 65535)
				throw new System.ApplicationException("File size " + nLen.ToString() + " is Too Large.");
			pEntry.m_Binary = new System.Byte[nLen];
			pFileIn.Read(pEntry.m_Binary, 0, nLen);
			pFileIn.Close();
			return pEntry;
		}
		// 
		static public DM2Pal256 DM2Pal
		{
			get { return m_DM2AllPal; }
		}
	}
}
