繁体   English   中英

如何在C#中将Color Lut应用于位图图像

[英]How apply Color Lut over bitmap image in c#

我正在打开不同类型的图像(例如8位和16位),它们是(Monocrome,RGB,调色板颜色)。

我有这些图像的原始像素数据。 我为8位图像创建这样的位图。

          //for monocrome images i am passing PixelFormat.Format8bppIndexed.
          //for RGB images i am passing PixelFormat.Format24bppRgb
           PixelFormat format = PixelFormat.Format8bppIndexed;
           Bitmap bmp = new Bitmap(Img_Width, Img_Height,format);

           Rectangle rect = new Rectangle(0, 0, Img_Width, Img_Height);

           //locking the bitmap on memory
           BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);

           // copy the managed byte array to the bitmap's image data
           Marshal.Copy(rawPixel, 0, bmpData.Scan0, rawPixel.Length);
           bmp.UnlockBits(bmpData);

问题是当我绘制该bmp图像时,它的颜色与原始颜色不同。 因此,有什么方法可以将lut(查找表)应用于该彩色图像。

我想要任何不安全的代码,因为我尝试过getixel和setPixel,它们非常慢。 我也不想要Image.fromSource()方法。

看一下位图的Image.Palette属性。

据我所知,.NET不支持ICC颜色配置文件,因此,例如,如果打开使用AdobeRGB颜色配置文件的图像,则与打开同一图像相比,这些颜色将显得更暗淡和“灰色”文件,例如Photoshop或其他可识别颜色配置文件的软件。

这篇文章讨论了一些颜色配置文件问题 ; 您可能会在那找到感兴趣的东西。

GetPixel和SetPixel确实非常慢。 如果要对单个像素执行操作,请考虑使用FastBitmap 它可以快速为像素着色。 使用此不安全的位图将大大提高您的速度。

我解决了这个问题,看看如何。 我在某个地方读到GDI +返回的BGR值不是RGB。 所以我颠倒了顺序,一切都很棒。 但这有点慢。

       PixelFormat format = PixelFormat.Format8bppIndexed;
       Bitmap bmp = new Bitmap(Img_Width, Img_Height,format);

       Rectangle rect = new Rectangle(0, 0, Img_Width, Img_Height);

       //locking the bitmap on memory
       BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
       Marshal.Copy(rawPixel, 0, bmpData.Scan0, rawPixel.Length);

       int stride = bmpData.Stride;
       System.IntPtr Scan0 = bmpData.Scan0;

       unsafe
       {
           byte* p = (byte*)(void*)Scan0;
           int nOffset = stride - bmp.Width * SAMPLES_PER_PIXEL ;
           byte red, green, blue;

           for (int y = 0; y < bmp.Height; ++y)
            {
                for (int x = 0; x < bmp.Width; ++x)
                {
                    blue = p[0];
                    green = p[1];
                    red = p[2];
                    p[0] = red;
                    p[1] = green;
                    p[2] = blue;
                    p += 3;
                }
                p += nOffset;
            }
        }

        ////unlockimg the bitmap
        bmp.UnlockBits(bmpData);

谢谢

任何人都可以拥有比这更快的代码吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM