3

As the subject says, I have a .bmp image and I need to write a code which will be able to get the color of any pixel of the image. It is a 1bpp (indexed) image, so the colour will be either black or white. Here is the code I currently have:

    //This method locks the bits of line of pixels
    private BitmapData LockLine(Bitmap bmp, int y)
    {
        Rectangle lineRect = new Rectangle(0, y, bmp.Width, 1);
        BitmapData line = bmp.LockBits(lineRect,
                                       ImageLockMode.ReadWrite,
                                       bmp.PixelFormat);
        return line;
    }
    //This method takes the BitmapData of a line of pixels
    //and returns the color of one which has the needed x coordinate
    private Color GetPixelColor(BitmapData data, int x)
    {
        //I am not sure if this line is correct
        IntPtr pPixel = data.Scan0 + x; 
        //The following code works for the 24bpp image:
        byte[] rgbValues = new byte[3];
        System.Runtime.InteropServices.Marshal.Copy(pPixel, rgbValues, 0, 3);
        return Color.FromArgb(rgbValues[2], rgbValues[1], rgbValues[0]);
    }

But how can I make it work for a 1bpp image? If I read only one byte from the pointer it always has the 255 value, so I assume, I am doing something wrong.
Please, do not suggest to use the System.Drawing.Bitmap.GetPixel method, because it works too slow and I want the code to work as fast as possible. Thanks in advance.

EDIT: Here is the code that works fine, just in case someone needs this:

    private Color GetPixelColor(BitmapData data, int x)
    {
        int byteIndex = x / 8;
        int bitIndex = x % 8;
        IntPtr pFirstPixel = data.Scan0+byteIndex;
        byte[] color = new byte[1];
        System.Runtime.InteropServices.Marshal.Copy(pFirstPixel, color, 0, 1);
        BitArray bits = new BitArray(color);
        return bits.Get(bitIndex) ? Color.Black : Color.White;
    }
Cracker
  • 912
  • 2
  • 14
  • 26
  • 1
    Is the value 255 even if the pixel is black? – VoidStar Aug 25 '12 at 12:53
  • 2
    There is no point in locking the bits when you only want one pixel operation. You should simply use the GetPixel method of the bmp, without locking any bits or anything. – SimpleVar Aug 25 '12 at 12:55
  • 2
    If you actually want more than one pixel, then you should lock the whole area that contains all the pixels you want a single time, and access those pixels using indexing logic of Scan0. – SimpleVar Aug 25 '12 at 12:57
  • 1
    For a single pixel-get/set you SHOULD, however, use the GetPixel/SetPixel because it IS faster for a single use. By the way, locking a 1x1 rectangle might be problematic, so you can try locking 2x2, even though you only use the first pixel from that rect (remember to handle right-bottom edges of the bmp if you do that). – SimpleVar Aug 25 '12 at 13:04
  • Your image is 1bit per pixel or 1 byte per pixel? Because you talk about 24bpp and then 1bpp, but seems like 1bpp actually means 8bpp, or not? – Marcelo De Zen Aug 25 '12 at 13:45
  • @VoidStar Yes, it is 255 even if the pixel is black. – Cracker Aug 25 '12 at 13:53
  • @YoryeNathan Yes, I am sorry, I want to get colors of a line (or how do I call it correctly?) of pixels. I have edited the question to emphasise that. – Cracker Aug 25 '12 at 13:54
  • @devundef Of course it is one *bit* per pixel, not byte. – Cracker Aug 25 '12 at 13:55
  • Well, is not "of course" because your code is all about 24bpp which do not has one single line that manipulates bits... – Marcelo De Zen Aug 25 '12 at 14:04
  • @devundef And what is wrong with that? 24bpp is 3 bytes, it has nothing to do with bits. – Cracker Aug 25 '12 at 14:21
  • @Cracker that is: nothing to do with bits, that's why I thought you maybe could not be talking about bits but bytes. – Marcelo De Zen Aug 25 '12 at 14:22
  • @devundef But the unit of color depth is bits per pixel and talking about computer graphics "bpp" will always mean "bits per pixel". Also, there is no point in manipulating with bits when the picture's color depth is 24bpp, because I have 3 bytes indicating red, green and blue, and these values can easily be converted to color. – Cracker Aug 25 '12 at 14:36

2 Answers2

2

Ok, got it! You need to read the bits from the BitmapData and apply a mask to the bit you want extract the color:

var bm = new Bitmap...

//lock all image bits
var bitmapData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

// this  will return the pixel index in the color pallete
// since is 1bpp it will return 0 or 1
int pixelColorIndex = GetIndexedPixel(50, 30, bitmapData);

// read the color from pallete
Color pixelColor = bm.Pallete.Entries[pixelColorIndex];

And here is the method:

// x, y relative to the locked area
private int GetIndexedPixel(int x, int y, BitmapData bitmapData)
{
    var index = y * bitmapData.Stride + (x >> 3);
    var chunk = Marshal.ReadByte(bitmapData.Scan0, index);

    var mask = (byte)(0x80 >> (x & 0x7));
    return (chunk & mask) == mask ? 1 : 0;
}

The pixel position is calculated in 2 rounds:

1) Find the byte where pixel in 'x' is (x / 8): each byte holds 8 pixels, to find the byte divide x/8 rounding down: 58 >> 3 = 7, the pixel is on the byte 7 of the current row (stride)

2) Find the bit on the current byte (x % 8): Do x & 0x7 to get only the 3 leftmost bits (x % 8)

Example:

x = 58 
// x / 8 - the pixel is on byte 7
byte = 58 >> 3 = 58 / 8 = 7 

// x % 8 - byte 7, bit 2
bitPosition = 58 & 0x7 = 2 

// the pixels are read from left to right, so we start with 0x80 and then shift right. 
mask = 0x80 >> bitPosition = 1000 0000b >> 2 =  0010 0000b 
Marcelo De Zen
  • 9,439
  • 3
  • 37
  • 50
  • Works perfectly! Thanks a lot. Let me ask you how did you find out the way to calculate the mask? – Cracker Aug 25 '12 at 17:58
1

First of all, if you need to read a single pixel in one operation, then GetPixel will be equivalent in performance. The expensive operation is locking the bits, ie. you should hold on to the BitmapData for doing all the reading you need, and only close it at the end - but remember to close it !

There seems to be some confusion about your pixel format, but let's assume it is correct 1bpp. Then each pixel will occupy one bit, and there will be data for 8 pixels in a byte. Therefore, your indexing calculation is incorrect. The location of the byte would be in x/8, then you need to take bit x%8.

driis
  • 161,458
  • 45
  • 265
  • 341
  • Thanks for your answer! I have mentioned that the image is 1bpp *indexed*. Does this matter? I was just confused by this fact, because if all the bits are indicating the pixel color, where is the index stored? Or am I misundestanding something? – Cracker Aug 25 '12 at 14:32
  • 1
    Indexed just means that the color values are indices into a color table. But since 1 bit leaves only 2 possibilities, you are safe to assume that those two colors are black and white. – driis Aug 25 '12 at 14:42
  • 1
    Actually you can have *any* two colors on the pallete, not necessarily black/white. It can be blue/red, green/yellow etc... Bitmap.Pallete.Entries holds the pallete colors for the bitmap. – Marcelo De Zen Aug 25 '12 at 15:28