using irisHelper;
using irisRefactor.IrisScoket;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace irisRefactor.IrisThread
{
    public class SaveImageTh
    {

        private static readonly SaveImageTh saveImageTh = new SaveImageTh();
        private SaveImageTh() { }
        public static SaveImageTh GetInstance
        {
            get => saveImageTh;

        }
        //图象采集回调函数
        //pData [out] 保存图像数据的缓存地址
        //pFrameInfo [out] 获取到的帧信息，包括宽、高、像素格式等
        //pUser [out] 用户数据
        public void SaveImage(IntPtr pData)
        {
            try
            {
                // 1.取出采集到的图像
                int ylength = 1280 * 960;
                byte[] imgBytes = new byte[ylength];
                Marshal.Copy(pData, imgBytes, 0, ylength);
                Image image = Raw8BitByteArrayToImage(imgBytes, 1280, 960);

                if (image == null) return;

                //2.将图像存入堆栈中
                // 条件：未在待机状态
                if (ProMemory.isWait == false)
                {
                    try
                    {
                        // 将图片放入堆栈1
                        lock (ProMemory.irisConfig.QueueFace)
                        {
                            ProMemory.irisConfig.QueueFace.Push(imgBytes);
                            // LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "Q1.Push：" + ProMemory.irisConfig.QueueFace.Count);
                        }
                    }
                    catch (Exception ee)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "errorerrorerror ： 采集线程push q1 ：" + ee);
                    }
                }

                //释放内存
                //imgBytes = null;
                //image = null;
                try
                {
                    lock (ProMemory.irisConfig.QueueFace)
                    {
                        if (ProMemory.irisConfig.QueueFace.Count >= 30)
                        {
                            ProMemory.irisConfig.QueueFace.Clear(); //手动控制堆栈容量
                        }
                    }
                }
                catch (Exception ee)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "errorerrorerror ： 采集线程clear q1 ：" + ee);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "图像采集线程CatchError:" + ex);
            }
            
        }

        private Image Raw8BitByteArrayToImage(byte[] byteArray, int width, int height)
        {
            if ((byteArray == null) || (width <= 0) || (height <= 0))
                return null;

            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            ColorPalette palette = bmp.Palette;

            for (int index = 0; index < palette.Entries.Length; index++)
                palette.Entries[index] = Color.FromArgb(index, index, index);

            bmp.Palette = palette;

            BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
                                            ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            ///水平镜像处理
            byte temp;
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width / 2; j++)
                {
                    //以水平中轴线为对称轴，两边像素值交换
                    temp = byteArray[i * width + j];
                    byteArray[i * width + j] = byteArray[(i + 1) * width - 1 - j];
                    byteArray[(i + 1) * width - 1 - j] = temp;
                }

            }
            ///水平镜像处理
            
            // Copy the bytes to the bitmap object
            Marshal.Copy(byteArray, 0, bData.Scan0, byteArray.Length);

            bmp.UnlockBits(bData);

            return bmp;
        }
        
        private Bitmap RevPic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
            int x, y, z;//x,y是循环次数,z是用来记录像素点的x坐标的变化的
            Color pixel;

            for (y = height - 1; y >= 0; y--)
            {
                for (x = width - 1, z = 0; x >= 0; x--)
                {
                    pixel = mybm.GetPixel(x, y);//获取当前像素的值
                    bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
                }
            }

            return bm;//返回经过翻转后的图片
        }

    }
}