[英]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.