Reputation: 7526
I'm loading a 8bppIndexed greyscale image into memory and reading the pixel values. The problem is that the values I am getting from the pixels do not seem to match the actual image, they are always darker. My image is a simple grey gradient like this:
The bottom right pixel is returning 191 and the top left 0. The top left is actually 64 and bottom right is 255.
Here is how I am loading my image:
Bitmap threshImg = new Bitmap(@"C:\grey.bmp");
Checking the PixelFormat confirms it is in Format8bppIndexed. So I read the bottom right pixel and top left like so:
BitmapData data = threshImg.LockBits(new Rectangle(0, 0, rectWidth, rectHeight), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
unsafe
{
byte* pixel = (byte*)data.Scan0.ToPointer();
int topVal = (int)(byte)pixel[0];
int bottomVal = (int)(byte)pixel[((threshImg.Height * threshImg.Width)) - 1];
}
threshImg.UnlockBits(data);
If I convert the image to 24bppRbg (and adjust the code accordingly) I see the correct colour values in the respective corners.
Anyone know why I'm getting darker values when using an 8bppIndexed image?
Upvotes: 1
Views: 2039
Reputation: 18061
The value in the 8bpp indexed image isn't the color itself (or gray value) but the index. Try to look up the color value in the palette.
Upvotes: 3
Reputation: 137148
With an indexed image there are only a certain number of colours (or in this case shades of grey) available - usually 256. It's probable that there aren't enough to get the full range of shades in the original image.
As having the exact shades is important I'd shift to a 24bpp image.
Upvotes: 1