public void SetGainFile(string fn)
        {
            if ((IntPtr)gain_map_ != IntPtr.Zero)
            {
                Marshal.FreeHGlobal((IntPtr)gain_map_);
                Kernel32Interface.CloseHandle((IntPtr)gain_map_);
                gain_map_ = (ushort *)IntPtr.Zero;
            }

            if ((IntPtr)gain_med_map_ != IntPtr.Zero)
            {
                Marshal.FreeHGlobal((IntPtr)gain_med_map_);
                Kernel32Interface.CloseHandle((IntPtr)gain_med_map_);
                gain_med_map_ = (ushort *)IntPtr.Zero;
            }
            if (File.Exists(fn))
            {
                List <ushort[, ]> list = HisObject.OpenFile(fn).dataList;
                gain_seq_num_ = list.Count;
                int count = list[0].Length;
                width_        = list[0].GetLength(1);
                height_       = list[0].GetLength(0);
                gain_map_     = ArrayListToBuffer(list);
                gain_med_map_ = (ushort *)Marshal.AllocHGlobal((int)(gain_seq_num_ * sizeof(short)));
                PKL_Interface.Acquisition_CreateGainMap(gain_map_, gain_med_map_, count, gain_seq_num_);

                offset_map_ = (ushort *)Marshal.AllocHGlobal((int)(width_ * height_ * sizeof(short)));
                for (int i = 0; i < width_ * height_; i++)
                {
                    offset_map_[i] = 0;
                }
            }
        }
        public void SetPixelMapFile(string fn)
        {
            if ((IntPtr)pixel_corr_list_ != IntPtr.Zero)
            {
                Marshal.FreeHGlobal((IntPtr)pixel_corr_list_);
                Kernel32Interface.CloseHandle((IntPtr)pixel_corr_list_);
                pixel_corr_list_ = (int *)IntPtr.Zero;
            }
            if (File.Exists(fn))
            {
                List <ushort[, ]> list = HisObject.OpenFile(fn).dataList;
                int Frames             = list.Count;
                width_  = list[0].GetLength(1);
                height_ = list[0].GetLength(0);
                int     pCorrListSize = 0;
                ushort *pPixelSoure   = ArrayListToBuffer(list);
                PKL_Interface.Acquisition_CreatePixelMap(pPixelSoure, height_, width_, null, ref pCorrListSize);
                pixel_corr_list_ = (int *)Marshal.AllocHGlobal((int)(pCorrListSize * sizeof(int)));
                PKL_Interface.Acquisition_CreatePixelMap(pPixelSoure, height_, width_, pixel_corr_list_, ref pCorrListSize);

                Marshal.FreeHGlobal((IntPtr)pPixelSoure);
                Kernel32Interface.CloseHandle((IntPtr)pPixelSoure);
                pPixelSoure = (ushort *)IntPtr.Zero;
            }
        }
        public void  Apply()
        {
            // offset
            //--------------------------------------------------------------------------------
            if (do_offset_)
            {
                for (int i = 0; i < height_; i++)
                {
                    for (int j = 0; j < width_; j++)
                    {
                        img_[i, j] -= offset_img_[i, j];
                    }
                }
            }


            //////////////////////////////////////////////////////////////////////////
            if ((IntPtr)img_buf_ != IntPtr.Zero)
            {
                Marshal.FreeHGlobal((IntPtr)img_buf_);
                Kernel32Interface.CloseHandle((IntPtr)img_buf_);
                img_buf_ = (ushort *)IntPtr.Zero;
            }

            img_buf_ = ArrayToBuffer(img_);
            //////////////////////////////////////////////////////////////////////////

            // gain
            if (do_gain_)
            {
                PKL_Interface.Acquisition_DoOffsetGainCorrection_Ex(img_buf_, img_buf_, offset_map_, gain_map_, gain_med_map_, width_ * height_, gain_seq_num_);
            }

            //////////////////////////////////////////////////////////////////////////
            // pixel
            if (do_pixel_)
            {
                PKL_Interface.Acquisition_DoPixelCorrection(img_buf_, pixel_corr_list_);
            }

            //////////////////////////////////////////////////////////////////////////
            ushort *p_idx = img_buf_;

            for (int i = 0; i < width_; i++)
            {
                for (int j = 0; j < height_; j++)
                {
                    img_[i, j] = *p_idx;
                    p_idx++;
                }
            }
            Marshal.FreeHGlobal((IntPtr)img_buf_);
            Kernel32Interface.CloseHandle((IntPtr)img_buf_);
            img_buf_ = (ushort *)IntPtr.Zero;
        }