如何在 C# 中的位图图像上应用 Color Lut

发布于 2024-07-17 19:54:21 字数 941 浏览 7 评论 0原文

我正在打开不同类型的图像,例如(8 位和 16 位),它们是(单色、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() 方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸 2024-07-24 19:54:21

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

Take a look at the bitmap's Image.Palette property.

云裳 2024-07-24 19:54:21

据我所知,.NET 不支持 ICC 颜色配置文件,因此,如果您打开使用 Adob​​eRGB 颜色配置文件的图像,则颜色会比打开同一图像时显得更暗淡且更“灰”例如,在 Photoshop 或其他支持颜色配置文件的软件中保存文件。

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

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.

断爱 2024-07-24 19:54:21

GetPixel和SetPixel确实很慢。 如果您想对单个像素进行操作,请考虑使用 FastBitmap。 它允许快速为像素着色。 使用这个不安全的位图将大大提高你的速度。

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.

只有一腔孤勇 2024-07-24 19:54:21

我解决了这个问题看看如何。
我在某处读到 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);

谢谢

有人可以有比这更快的代码吗?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文