using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Euresys.Open_eVision_2_11;

namespace Project
{
	public partial class fMain
	{
		Int32[] _hDevice = new Int32[] { 0, 0, 0 };
		Int32[] _width = new Int32[] { 0, 0, 0 };
		Int32[] _height = new Int32[] { 0, 0, 0 };
		Int32[] _stride = new Int32[] { 0, 0, 0 };
		Int32[] _bufferSize = new Int32[] { 0, 0, 0 };
		Boolean[] _isCrevisOpen = new bool[] { false, false, false };
		Boolean[] _isCrevisACQ = new bool[] { false, false, false };
		IntPtr[] _pImage = new IntPtr[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero };
		//IntPtr[] _vImage = new IntPtr[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero };
		arCtl.ImageBox[] ivs;
		Bitmap[] OrgBitmap = null;
		Image<Gray, byte>[] OrgImage = null;
		//EImageBW8[] OrgEImage = null;
		//Boolean bOpenEvisionReady = false;

	
		void MakeBlankImage(int vIdx, int width=1024, int height=768, string title = "")
		{
			_width[vIdx] = width;
			_height[vIdx] = height;

			OrgBitmap[vIdx] = new Bitmap(_width[vIdx], _height[vIdx], PixelFormat.Format8bppIndexed);
			SetGrayscalePalette(OrgBitmap[vIdx]);

			var lockbit = OrgBitmap[vIdx].LockBits(new Rectangle(0, 0, _width[vIdx], _height[vIdx]), ImageLockMode.ReadOnly, OrgBitmap[vIdx].PixelFormat);
			_pImage[vIdx] = lockbit.Scan0;
			_stride[vIdx] = lockbit.Stride;
			OrgBitmap[vIdx].UnlockBits(lockbit);

			//if (bOpenEvisionReady)
			//{
			//	//OrgEImage[vIdx] = new EImageBW8();
			//	//OrgEImage[vIdx].SetImagePtr(_width[vIdx], _height[vIdx], _pImage[vIdx]);
			//}

			//openCv 이미지 처리
			OrgImage[vIdx] = new Image<Gray, byte>(_width[vIdx], _height[vIdx], _stride[vIdx], _pImage[vIdx]);
			if (title.Equals("") == false)
			{
				CvInvoke.PutText(OrgImage[vIdx], title, new Point(50, 100), FontFace.HersheyDuplex, 2.0, new Gray(255).MCvScalar, 2);
			}
		}


		private void SetGrayscalePalette(Bitmap bitmap)
		{
			ColorPalette GrayscalePalette = bitmap.Palette;
			for (int i = 0; i < 255; i++)
			{
				GrayscalePalette.Entries[i] = Color.FromArgb(i, i, i);
			}
			bitmap.Palette = GrayscalePalette;
		}
	}
}
