简体   繁体   English

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

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

I am opening different types of images like (8 and 16 bit) and they are (Monocrome, RGB ,palette color). 我正在打开不同类型的图像(例如8位和16位),它们是(Monocrome,RGB,调色板颜色)。

I have raw pixel data of these images. 我有这些图像的原始像素数据。 I create bitmap like this for 8 bit images. 我为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);

The problem is that when i draw that bmp image then it differs in color than original. 问题是当我绘制该bmp图像时,它的颜色与原始颜色不同。 So is there any way to apply lut (lookup table) on that colored images. 因此,有什么方法可以将lut(查找表)应用于该彩色图像。

i want any unsafe code because i tried getixel and setPixel and they are very slow. 我想要任何不安全的代码,因为我尝试过getixel和setPixel,它们非常慢。 I also dont want Image.fromSource() methods. 我也不想要Image.fromSource()方法。

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

As far as I know, .NET does not support ICC color profiles, so if you for instance open an image that is using the AdobeRGB color profile, the colours will appear a bit duller and more "greyish" than if you open the same image file in, say, Photoshop or another color-profile-aware software. 据我所知,.NET不支持ICC颜色配置文件,因此,例如,如果打开使用AdobeRGB颜色配置文件的图像,则与打开同一图像相比,这些颜色将显得更暗淡和“灰色”文件,例如Photoshop或其他可识别颜色配置文件的软件。

This post discusses some color profile issues ; 这篇文章讨论了一些颜色配置文件问题 ; you may find something of interest there. 您可能会在那找到感兴趣的东西。

GetPixel and SetPixel are indeed very slow. GetPixel和SetPixel确实非常慢。 If you want to do operations on single pixels, consider using a FastBitmap . 如果要对单个像素执行操作,请考虑使用FastBitmap It allows to quickly color pixels. 它可以快速为像素着色。 Using this unsafe bitmap will greatly improve your speed. 使用此不安全的位图将大大提高您的速度。

i solved this problem see how. 我解决了这个问题,看看如何。 Somewhere i read that GDI+ return BGR value not the RGB. 我在某个地方读到GDI +返回的BGR值不是RGB。 So i reverse the order and amazing everything fine. 所以我颠倒了顺序,一切都很棒。 But it is little bit slow. 但这有点慢。

       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);

thanks 谢谢

Can anybody is having some faster code than that. 任何人都可以拥有比这更快的代码吗?

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

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