简体   繁体   中英

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

I have raw pixel data of these images. I create bitmap like this for 8 bit images.

          //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. So is there any way to apply lut (lookup table) on that colored images.

i want any unsafe code because i tried getixel and setPixel and they are very slow. I also dont want Image.fromSource() methods.

看一下位图的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.

This post discusses some color profile issues ; you may find something of interest there.

GetPixel and SetPixel are indeed very slow. If you want to do operations on single pixels, consider using a 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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