UnmanagedBitmap.cs 5.6 KB
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace eyemLib_Sharp
{
    public unsafe class UnmanagedBitmap : IDisposable
    {
        #region 接口
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage tpImage);
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemImageFree(ref EyemImage tpImage);
        #endregion

        private EyemImage image;
        public EyemImage Image
        {
            get { return image; }
        }

        public UnmanagedBitmap()
        {
            image = new EyemImage();
        }

        public UnmanagedBitmap(string fileName)
        {
            eyemImageRead(fileName, -1, out image);
        }

        public UnmanagedBitmap(Bitmap bitmap)
        {
            mustbeDispose = true;
            image = eyemCvtToEyemImage(bitmap);
        }

        private bool mustbeDispose = false;

        ~UnmanagedBitmap()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
            }
            if (mustbeDispose)
            {
                image.iChannels = image.iDepth = image.iHeight = image.iWidth = 0;
                Marshal.FreeHGlobal(image.vpImage);
                image.vpImage = IntPtr.Zero;
            }
            else
            {
                eyemImageFree(ref image);
            }
        }

        #region EyemImageBitmap相互转换
        public static Bitmap eyemCvtToBitmap(EyemImage tpImage)
        {
            if (tpImage.vpImage == IntPtr.Zero || tpImage.iDepth != 0)
                return null;

            PixelFormat format;

            switch (tpImage.iChannels)
            {
                case 1:
                    format = PixelFormat.Format8bppIndexed;
                    break;
                case 3:
                    format = PixelFormat.Format24bppRgb;
                    break;
                case 4:
                    format = PixelFormat.Format32bppArgb;
                    break;
                default:
                    return null;
            }

            Bitmap bitmap = new Bitmap(tpImage.iWidth, tpImage.iHeight, format);

            //对于输出灰度图像
            if (format == PixelFormat.Format8bppIndexed)
            {
                ColorPalette palette = bitmap.Palette;
                for (int i = 0; i < 256; i++)
                {
                    palette.Entries[i] = Color.FromArgb(i, i, i);
                }
                bitmap.Palette = palette;
            }

            //锁定数据区
            BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight),
                ImageLockMode.WriteOnly, format);

            try
            {
                int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;

                long bytesToCopy = tpImage.iWidth * tpImage.iChannels;

                for (int y = 0; y < tpImage.iHeight; y++)
                {
                    long offsetSrc = (y * tpImage.iWidth * tpImage.iChannels);
                    long offsetDst = (y * pd);

                    Buffer.MemoryCopy((byte*)(tpImage.vpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
                }
            }
            finally
            {
                bitmap.UnlockBits(bd);
            }
            return bitmap;
        }

        public static EyemImage eyemCvtToEyemImage(Bitmap bitmap)
        {
            EyemImage tpImage = new EyemImage();
            //锁定数据区
            BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, bitmap.PixelFormat);

            switch (bitmap.PixelFormat)
            {
                case PixelFormat.Format8bppIndexed:
                    tpImage.iChannels = 1;
                    break;
                case PixelFormat.Format24bppRgb:
                    tpImage.iChannels = 3;
                    break;
                case PixelFormat.Format32bppArgb:
                    tpImage.iChannels = 4;
                    break;
                default:
                    throw new Exception("Image formats are not supported");
            }
            //仅支持8位
            tpImage.iDepth = 0;
            //图像尺寸
            tpImage.iWidth = bitmap.Width; tpImage.iHeight = bitmap.Height;
            //分配内存(释放不是用eyemImageFree,用Marshal.FreeHGlobal(tpImage.vpImage))
            tpImage.vpImage = Marshal.AllocHGlobal(bd.Stride * bd.Height);
            try
            {
                int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;

                long bytesToCopy = tpImage.iWidth * tpImage.iChannels;

                for (int y = 0; y < tpImage.iHeight; y++)
                {
                    long offsetSrc = y * pd;
                    long offsetDst = y * tpImage.iWidth * tpImage.iChannels;

                    Buffer.MemoryCopy((byte*)(bd.Scan0.ToPointer()) + offsetSrc, (byte*)(tpImage.vpImage.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
                }
            }
            finally
            {
                bitmap.UnlockBits(bd);
            }
            return tpImage;
        }
        #endregion
    }
}