i'm trying to edit the pixels of a 8bpp. Since this PixelFormat is indexed i'm aware that it uses a Color Table to map the pixel values. Even though I can edit the bitmap by converting it to 24bpp, 8bpp editing is much faster (13ms vs 3ms). But, changing each value when accessing the 8bpp bitmap results in some random rgb colors even though the PixelFormat remains 8bpp.
I'm currently developing in c# and the algorithm is as follows:
(C#)
1- Load original Bitmap at 8bpp
2- Create Empty temp Bitmap with 8bpp with the same size as the original
3-LockBits of both bitmaps and, using P/Invoke, calling c++ method where I pass the Scan0 of each BitmapData object. (I used a c++ method as it offers better performance when iterating through the Bitmap's pixels)
(C++)
4- Create a int[256] palette according to some parameters and edit the temp bitmap bytes by passing the original's pixel values through the palette.
(C#)
5- UnlockBits.
My question is how can I edit the pixel values without having the strange rgb colors, or even better, edit the 8bpp bitmap's Color Table?
There is no need to move into C++ land or use P/Invoke at all; C# supports pointers and unsafe code perfectly fine; I may even hazard a guess that this is causing your problems.
The colors are likely coming from the default Palette. You'll find that changing the Palette shouldn't be slow at all. This is what I do to create a greyscale palette:
Have you tried loading a
System.Drawing.Image
instead? That class gives you access to the colour palette. You can then wrap theSystem.Drawing.Image
up as aSystem.Drawing.Bitmap
once the palette is set.I'm not sure how
System.Drawing.BitMap.SetPixel()
works with indexed coloured images. It could be it tries to map it to the closest colour in the palette.Imagine that your indexed 8ppp graysacle are stored in a linear array dataB (for speed)
Try this code: