using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DM2CompressPic
{
	// 
	public interface ICommitRecompile
	{
		// 
		void SetCompressionResultBasis(
			DM2CompressionResultBasis pRB
			);
	}
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form, ICommitRecompile
	{
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemLoadFile;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItemClose;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItemEditCopy;
		private System.Windows.Forms.MenuItem menuItemEditPaste;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItemDeletePreviewImg;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItemLE;
		private System.Windows.Forms.MenuItem menuItemBE;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ToolBar toolBar2;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox textBoxExtDM1Stat;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button buttonExtDM1Pal;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button buttonSaveExtDM1To;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button buttonEncodeExtDM1;
		private System.Windows.Forms.PictureBox pictureBoxPreview;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button buttonSaveIntreprayTo;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textBoxIntreprayStat;
		private System.Windows.Forms.Button buttonEncodeIntrepray;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItemNormPal;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemLoadFile = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItemClose = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItemNormPal = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItemEditCopy = new System.Windows.Forms.MenuItem();
			this.menuItemEditPaste = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItemDeletePreviewImg = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItemLE = new System.Windows.Forms.MenuItem();
			this.menuItemBE = new System.Windows.Forms.MenuItem();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.toolBar2 = new System.Windows.Forms.ToolBar();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.panel2 = new System.Windows.Forms.Panel();
			this.textBoxIntreprayStat = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.buttonSaveIntreprayTo = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.buttonEncodeIntrepray = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.textBoxExtDM1Stat = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.buttonExtDM1Pal = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.buttonSaveExtDM1To = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.buttonEncodeExtDM1 = new System.Windows.Forms.Button();
			this.pictureBoxPreview = new System.Windows.Forms.PictureBox();
			this.panel2.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 251);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(608, 22);
			this.statusBar1.TabIndex = 0;
			// 
			// toolBar1
			// 
			this.toolBar1.ButtonSize = new System.Drawing.Size(39, 2);
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(608, 5);
			this.toolBar1.TabIndex = 1;
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Filter = "All (*.*)|*.*";
			// 
			// label1
			// 
			this.label1.Dock = System.Windows.Forms.DockStyle.Top;
			this.label1.Location = new System.Drawing.Point(0, 5);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(608, 19);
			this.label1.TabIndex = 6;
			this.label1.Text = "Preview:";
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemLoadFile,
																					  this.menuItem5,
																					  this.menuItemClose});
			this.menuItem1.Text = "&File";
			// 
			// menuItemLoadFile
			// 
			this.menuItemLoadFile.Index = 0;
			this.menuItemLoadFile.Shortcut = System.Windows.Forms.Shortcut.CtrlL;
			this.menuItemLoadFile.Text = "&Load";
			this.menuItemLoadFile.Click += new System.EventHandler(this.menuItemLoadFile_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "-";
			// 
			// menuItemClose
			// 
			this.menuItemClose.Index = 2;
			this.menuItemClose.Text = "&Close";
			this.menuItemClose.Click += new System.EventHandler(this.menuItemClose_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemNormPal,
																					  this.menuItem6,
																					  this.menuItemEditCopy,
																					  this.menuItemEditPaste,
																					  this.menuItem3,
																					  this.menuItemDeletePreviewImg});
			this.menuItem2.Text = "&Edit";
			// 
			// menuItemNormPal
			// 
			this.menuItemNormPal.Index = 0;
			this.menuItemNormPal.Text = "&Normalize to DM2 Palette";
			this.menuItemNormPal.Click += new System.EventHandler(this.menuItemNormPal_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 1;
			this.menuItem6.Text = "-";
			// 
			// menuItemEditCopy
			// 
			this.menuItemEditCopy.Index = 2;
			this.menuItemEditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.menuItemEditCopy.Text = "&Copy";
			this.menuItemEditCopy.Click += new System.EventHandler(this.menuItemEditCopy_Click);
			// 
			// menuItemEditPaste
			// 
			this.menuItemEditPaste.Index = 3;
			this.menuItemEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
			this.menuItemEditPaste.Text = "&Paste";
			this.menuItemEditPaste.Click += new System.EventHandler(this.menuItemEditPaste_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 4;
			this.menuItem3.Text = "-";
			// 
			// menuItemDeletePreviewImg
			// 
			this.menuItemDeletePreviewImg.Index = 5;
			this.menuItemDeletePreviewImg.Shortcut = System.Windows.Forms.Shortcut.CtrlDel;
			this.menuItemDeletePreviewImg.Text = "&Erase Preview image";
			this.menuItemDeletePreviewImg.Click += new System.EventHandler(this.menuItemDeletePreviewImg_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemLE,
																					  this.menuItemBE});
			this.menuItem4.Text = "&Options";
			// 
			// menuItemLE
			// 
			this.menuItemLE.Index = 0;
			this.menuItemLE.RadioCheck = true;
			this.menuItemLE.Text = "Load/Save ExtDM1 as LITTLE ENDIAN";
			this.menuItemLE.Click += new System.EventHandler(this.menuItemLE_Click);
			// 
			// menuItemBE
			// 
			this.menuItemBE.Index = 1;
			this.menuItemBE.RadioCheck = true;
			this.menuItemBE.Text = "Load/Save ExtDM1 as BIG ENDIAN";
			this.menuItemBE.Click += new System.EventHandler(this.menuItemLE_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2,
																					  this.menuItem4});
			// 
			// toolBar2
			// 
			this.toolBar2.ButtonSize = new System.Drawing.Size(39, 2);
			this.toolBar2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.toolBar2.DropDownArrows = true;
			this.toolBar2.Location = new System.Drawing.Point(0, 246);
			this.toolBar2.Name = "toolBar2";
			this.toolBar2.ShowToolTips = true;
			this.toolBar2.Size = new System.Drawing.Size(608, 5);
			this.toolBar2.TabIndex = 10;
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.Filter = "BIN (*.bin)|*.bin|All (*.*)|*.*";
			// 
			// panel2
			// 
			this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.textBoxIntreprayStat,
																				 this.label6,
																				 this.buttonSaveIntreprayTo,
																				 this.label5,
																				 this.buttonEncodeIntrepray});
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.DockPadding.Bottom = 2;
			this.panel2.DockPadding.Top = 2;
			this.panel2.Location = new System.Drawing.Point(0, 223);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(608, 23);
			this.panel2.TabIndex = 14;
			// 
			// textBoxIntreprayStat
			// 
			this.textBoxIntreprayStat.Dock = System.Windows.Forms.DockStyle.Fill;
			this.textBoxIntreprayStat.Location = new System.Drawing.Point(184, 2);
			this.textBoxIntreprayStat.Name = "textBoxIntreprayStat";
			this.textBoxIntreprayStat.ReadOnly = true;
			this.textBoxIntreprayStat.Size = new System.Drawing.Size(341, 19);
			this.textBoxIntreprayStat.TabIndex = 1;
			this.textBoxIntreprayStat.Text = "No Data Available for Saving";
			// 
			// label6
			// 
			this.label6.Dock = System.Windows.Forms.DockStyle.Right;
			this.label6.Location = new System.Drawing.Point(525, 2);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(3, 19);
			this.label6.TabIndex = 4;
			// 
			// buttonSaveIntreprayTo
			// 
			this.buttonSaveIntreprayTo.Dock = System.Windows.Forms.DockStyle.Right;
			this.buttonSaveIntreprayTo.Location = new System.Drawing.Point(528, 2);
			this.buttonSaveIntreprayTo.Name = "buttonSaveIntreprayTo";
			this.buttonSaveIntreprayTo.Size = new System.Drawing.Size(80, 19);
			this.buttonSaveIntreprayTo.TabIndex = 2;
			this.buttonSaveIntreprayTo.Text = "Save To...";
			this.buttonSaveIntreprayTo.Click += new System.EventHandler(this.buttonSaveIntreprayTo_Click);
			// 
			// label5
			// 
			this.label5.Dock = System.Windows.Forms.DockStyle.Left;
			this.label5.Location = new System.Drawing.Point(136, 2);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 19);
			this.label5.TabIndex = 2;
			this.label5.Text = " Status:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// buttonEncodeIntrepray
			// 
			this.buttonEncodeIntrepray.Dock = System.Windows.Forms.DockStyle.Left;
			this.buttonEncodeIntrepray.Location = new System.Drawing.Point(0, 2);
			this.buttonEncodeIntrepray.Name = "buttonEncodeIntrepray";
			this.buttonEncodeIntrepray.Size = new System.Drawing.Size(136, 19);
			this.buttonEncodeIntrepray.TabIndex = 0;
			this.buttonEncodeIntrepray.Text = "Encode to DM2PC";
			this.buttonEncodeIntrepray.Click += new System.EventHandler(this.buttonEncodeIntrepray_Click);
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.textBoxExtDM1Stat,
																				 this.label4,
																				 this.buttonExtDM1Pal,
																				 this.label3,
																				 this.buttonSaveExtDM1To,
																				 this.label2,
																				 this.buttonEncodeExtDM1});
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.DockPadding.Bottom = 2;
			this.panel1.DockPadding.Top = 2;
			this.panel1.Location = new System.Drawing.Point(0, 200);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(608, 23);
			this.panel1.TabIndex = 13;
			// 
			// textBoxExtDM1Stat
			// 
			this.textBoxExtDM1Stat.Dock = System.Windows.Forms.DockStyle.Fill;
			this.textBoxExtDM1Stat.Location = new System.Drawing.Point(184, 2);
			this.textBoxExtDM1Stat.Name = "textBoxExtDM1Stat";
			this.textBoxExtDM1Stat.ReadOnly = true;
			this.textBoxExtDM1Stat.Size = new System.Drawing.Size(274, 19);
			this.textBoxExtDM1Stat.TabIndex = 1;
			this.textBoxExtDM1Stat.Text = "No Data Available for Saving";
			// 
			// label4
			// 
			this.label4.Dock = System.Windows.Forms.DockStyle.Right;
			this.label4.Location = new System.Drawing.Point(458, 2);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(3, 19);
			this.label4.TabIndex = 6;
			// 
			// buttonExtDM1Pal
			// 
			this.buttonExtDM1Pal.Dock = System.Windows.Forms.DockStyle.Right;
			this.buttonExtDM1Pal.Location = new System.Drawing.Point(461, 2);
			this.buttonExtDM1Pal.Name = "buttonExtDM1Pal";
			this.buttonExtDM1Pal.Size = new System.Drawing.Size(64, 19);
			this.buttonExtDM1Pal.TabIndex = 2;
			this.buttonExtDM1Pal.Text = "Palette...";
			this.buttonExtDM1Pal.Click += new System.EventHandler(this.buttonExtDM1Pal_Click);
			// 
			// label3
			// 
			this.label3.Dock = System.Windows.Forms.DockStyle.Right;
			this.label3.Location = new System.Drawing.Point(525, 2);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(3, 19);
			this.label3.TabIndex = 3;
			// 
			// buttonSaveExtDM1To
			// 
			this.buttonSaveExtDM1To.Dock = System.Windows.Forms.DockStyle.Right;
			this.buttonSaveExtDM1To.Location = new System.Drawing.Point(528, 2);
			this.buttonSaveExtDM1To.Name = "buttonSaveExtDM1To";
			this.buttonSaveExtDM1To.Size = new System.Drawing.Size(80, 19);
			this.buttonSaveExtDM1To.TabIndex = 3;
			this.buttonSaveExtDM1To.Text = "Save To...";
			this.buttonSaveExtDM1To.Click += new System.EventHandler(this.buttonSaveExtDM1To_Click);
			// 
			// label2
			// 
			this.label2.Dock = System.Windows.Forms.DockStyle.Left;
			this.label2.Location = new System.Drawing.Point(136, 2);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 19);
			this.label2.TabIndex = 1;
			this.label2.Text = " Status:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// buttonEncodeExtDM1
			// 
			this.buttonEncodeExtDM1.Dock = System.Windows.Forms.DockStyle.Left;
			this.buttonEncodeExtDM1.Location = new System.Drawing.Point(0, 2);
			this.buttonEncodeExtDM1.Name = "buttonEncodeExtDM1";
			this.buttonEncodeExtDM1.Size = new System.Drawing.Size(136, 19);
			this.buttonEncodeExtDM1.TabIndex = 0;
			this.buttonEncodeExtDM1.Text = "Encode to ExtDM1";
			this.buttonEncodeExtDM1.Click += new System.EventHandler(this.buttonEncodeExtDM1_Click);
			// 
			// pictureBoxPreview
			// 
			this.pictureBoxPreview.BackColor = System.Drawing.Color.Black;
			this.pictureBoxPreview.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pictureBoxPreview.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pictureBoxPreview.Location = new System.Drawing.Point(0, 24);
			this.pictureBoxPreview.Name = "pictureBoxPreview";
			this.pictureBoxPreview.Size = new System.Drawing.Size(608, 176);
			this.pictureBoxPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pictureBoxPreview.TabIndex = 15;
			this.pictureBoxPreview.TabStop = false;
			this.pictureBoxPreview.DragEnter += new System.Windows.Forms.DragEventHandler(this.pictureBoxPreview_DragEnter);
			this.pictureBoxPreview.DragDrop += new System.Windows.Forms.DragEventHandler(this.pictureBoxPreview_DragDrop);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(608, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBoxPreview,
																		  this.panel1,
																		  this.panel2,
																		  this.toolBar2,
																		  this.label1,
																		  this.toolBar1,
																		  this.statusBar1});
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "DM2CompressPic - DM2 Image Encoder/Decoder Rev.0";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.panel2.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		System.Drawing.Bitmap m_pImg = null;
		System.Boolean m_fLittleEndian = true;
		DM2CompressionResultBasis m_pRB = null;
		Form2 m_pForm2 = null;
		CGDR.Draw8bppImg m_pExtDM1DrawEntity = null;
		System.Byte[] m_pIntreprayBinary = null;

		public void SetCompressionResultBasis(
			DM2CompressionResultBasis pRB
			)
		{
			m_pRB = pRB;
			if (m_pRB == null)
			{
				textBoxExtDM1Stat.Text = "No Data Available for Saving";
				return;
			}
			textBoxExtDM1Stat.Text = "Comression Done: Length is " + m_pRB.Binary.Length.ToString();
		}

		void SetResultIntrepray(
			System.Byte[] pBinary
			)
		{
			m_pIntreprayBinary = pBinary;

			if (m_pIntreprayBinary == null)
			{
				textBoxIntreprayStat.Text = "No Data Available for Saving";
				return;
			}
			textBoxIntreprayStat.Text = "Comression Done: Length is " + m_pIntreprayBinary.Length.ToString();
		}

		void SetPreviewImage(System.Drawing.Bitmap pImg, bool fRedraw)
		{
			m_pImg = pImg;

			if (fRedraw) pictureBoxPreview.Image = m_pImg;
		}

		bool LoadUnknownImage(System.String sFile)
		{
			statusBar1.Text = "";

			try
			{
				System.Drawing.Bitmap pImg = new System.Drawing.Bitmap(sFile);

				SetPreviewImage(pImg, true);
				return true;
			}
			catch (System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}

			try
			{
				CGDR pGDR = new CGDR(m_fLittleEndian);
				CGDR.Entry pEntry = CGDR.CreateEntryFromFile(sFile);
				System.Drawing.Bitmap pImg;
				if (!pGDR.ReadEntryAsImage(pEntry, null, out pImg))
					throw (new System.ApplicationException());

				SetPreviewImage(pImg, true);
				return true;
			}
			catch (System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
			}

			statusBar1.Text = "ERR";
			return false;
		}

		void CreateDrawEntityFrom(
			System.Drawing.Bitmap pBmp,
			out CGDR.Draw8bppImg pDrawEntity
			)
		{
			if (pBmp == null)
				throw new System.ApplicationException("No Image Supplied");
			System.Drawing.Bitmap pImg = pBmp.Clone() as System.Drawing.Bitmap;
			pImg.SelectActiveFrame(new System.Drawing.Imaging.FrameDimension(pImg.FrameDimensionsList[0]), 0);
			pDrawEntity = null;
			int cx = pImg.Width;
			int cy = pImg.Height;
			if (cx >= 512 || cy >= 512)
				throw new System.ApplicationException("Image size (" + cx.ToString() + "," + cy.ToString() + ") is Too Large.");
			pDrawEntity = new CGDR.Draw8bppImg(cx, cy);
			int x1, y1, cp;
			System.Drawing.Color c1;
			for (y1 = 0; y1 < cy; y1++)
			{
				for (x1 = 0; x1 < cx; x1++)
				{
					c1 = pImg.GetPixel(x1, y1);
					for (cp = 0; cp < 256; cp++)
					{
						if (c1 == CGDR.DM2Pal[cp])
							break;
					}
					if (cp < 256)
					{
						pDrawEntity.Image[y1, x1] = System.Convert.ToByte(cp);
						continue;
					}
					if (cp == 256)
					{
						throw new System.ApplicationException("Color \"" + c1.ToString() + "\" at (" + x1.ToString() + "," + y1.ToString() + ") seems not to be part of color in DM2 Palette.");
					}
				}
			}
		}

		private void menuItemClose_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void menuItemEditCopy_Click(object sender, System.EventArgs e)
		{
			if (m_pImg == null) return;

			System.Windows.Forms.Clipboard.SetDataObject(m_pImg, true);
		}

		private void menuItemEditPaste_Click(object sender, System.EventArgs e)
		{
			statusBar1.Text = "";
			try
			{
				System.Windows.Forms.IDataObject pDataObj = System.Windows.Forms.Clipboard.GetDataObject();

				SetPreviewImage(pDataObj.GetData(System.Windows.Forms.DataFormats.Bitmap) as System.Drawing.Bitmap, true);
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
		}

		private void menuItemDeletePreviewImg_Click(object sender, System.EventArgs e)
		{
			SetPreviewImage(null, true);
		}

		private void menuItemLE_Click(object sender, System.EventArgs e)
		{
			if (sender == menuItemLE) m_fLittleEndian = true;
			if (sender == menuItemBE) m_fLittleEndian = false;

			menuItemLE.Checked = (m_fLittleEndian);
			menuItemBE.Checked = (!m_fLittleEndian);
		}

		private void pictureBoxPreview_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			System.Windows.Forms.IDataObject pDataObj = e.Data;
			e.Effect = System.Windows.Forms.DragDropEffects.None;
			if (pDataObj.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
			{
				e.Effect = System.Windows.Forms.DragDropEffects.Copy;
			}
		}

		private void pictureBoxPreview_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			Cursor = System.Windows.Forms.Cursors.WaitCursor;

			try
			{
				System.Windows.Forms.IDataObject pDataObj = e.Data;
				if (pDataObj.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
				{
					System.String[] pFileDrop = pDataObj.GetData(System.Windows.Forms.DataFormats.FileDrop) as System.String[];

						if (pFileDrop.Length > 0)
						{
							LoadUnknownImage(pFileDrop[0]);
						}
				}
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			menuItemLE.Checked = true;

			pictureBoxPreview.AllowDrop = true;
		}

		private void menuItemLoadFile_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.FileName = "";
			switch (openFileDialog1.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					LoadUnknownImage(openFileDialog1.FileName);
					return;
			}
		}

		private void buttonEncodeExtDM1_Click(object sender, System.EventArgs e)
		{
			statusBar1.Text = "";

			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				CGDR.Draw8bppImg pDrawEntity;
				CreateDrawEntityFrom(m_pImg, out pDrawEntity);
				DM2DE pDE = DM2IC.CreateDrawEntity(m_pExtDM1DrawEntity = pDrawEntity);
				System.IO.MemoryStream pFileInto = new System.IO.MemoryStream(10000);
				DM2IC.Compress(pDE, pFileInto, m_fLittleEndian, 0x89ABCD);

				DM2CompressionResultBasis pRB = new DM2CompressionResultBasis(pDE, pFileInto.ToArray());
				SetCompressionResultBasis(pRB);
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		private void buttonSaveExtDM1To_Click(object sender, System.EventArgs e)
		{
			if (m_pRB == null) return;

			switch (saveFileDialog1.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					string sPathName = saveFileDialog1.FileName;
					System.IO.Stream pFileInto = System.IO.File.OpenWrite(sPathName);
					int nLen = m_pRB.Binary.Length;
					pFileInto.Write(m_pRB.Binary, 0, nLen);
					pFileInto.SetLength(nLen);
					pFileInto.Close();
					return;
			}
		}

		private void buttonExtDM1Pal_Click(object sender, System.EventArgs e)
		{
			if (m_pRB == null) return;
			if (m_pForm2 == null || m_pForm2.IsDisposed)
			{
				m_pForm2 = new Form2();
				m_pForm2.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
				m_pForm2.Location = new System.Drawing.Point(
					this.Left,
					this.Top + this.Height
					);
				m_pForm2.Show();
			}
			else
			{
				switch (m_pForm2.WindowState)
				{
					case System.Windows.Forms.FormWindowState.Minimized:
						m_pForm2.WindowState = System.Windows.Forms.FormWindowState.Normal;
						break;
				}
				m_pForm2.Activate();
			}
			m_pForm2.Init(this, m_pRB.DrawEntity.SelectedPalette, m_fLittleEndian, m_pExtDM1DrawEntity);
		}

		private void buttonEncodeIntrepray_Click(object sender, System.EventArgs e)
		{
			statusBar1.Text = "";

			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				CGDR.Draw8bppImg pDrawEntity;
				CreateDrawEntityFrom(m_pImg, out pDrawEntity);
				System.IO.MemoryStream pFileType3 = new System.IO.MemoryStream(20000);
				DM2IC.Compress8(pDrawEntity, pFileType3, true, true);
				System.IO.MemoryStream pFileType2 = new System.IO.MemoryStream(20000);
				DM2IC.Compress8(pDrawEntity, pFileType2, true, false);
				if (pFileType2.Length > pFileType3.Length)
				{
					SetResultIntrepray(pFileType3.ToArray());
				}
				else
				{
					SetResultIntrepray(pFileType2.ToArray());
				}
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		private void buttonSaveIntreprayTo_Click(object sender, System.EventArgs e)
		{
			if (m_pIntreprayBinary == null) return;

			switch (saveFileDialog1.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					string sPathName = saveFileDialog1.FileName;
					System.IO.Stream pFileInto = System.IO.File.OpenWrite(sPathName);
					int nLen = m_pIntreprayBinary.Length;
					pFileInto.Write(m_pIntreprayBinary, 0, nLen);
					pFileInto.SetLength(nLen);
					pFileInto.Close();
					return;
			}
		}

		private void menuItemNormPal_Click(object sender, System.EventArgs e)
		{
			if (m_pImg == null) return;

			statusBar1.Text = "";

			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				System.Drawing.Bitmap pImg;
				CGDR.NormalizeImageToDM2Pal(m_pImg, out pImg);

				SetPreviewImage(pImg, true);
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}
	}
}
