M
Morten Nielsen
I'm trying to manipulate a 1bpp image.
I have found this example to set a pixel :
private unsafe void SetIndexedPixel(int x,int y,BitmapData bmd, bool pixel)
{
byte* p = (byte*)bmd.Scan0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));
if (pixel)
p[index]|=mask;
else
p[index]&=(byte)(mask^0xff);
}
....which seems to work ok, but I haven't been able to find an example of
doing a GetIndexedPixel(), so I tried myself :
protected unsafe bool GetIndexedPixel(int x,int y,BitmapData bmd)
{
byte* p=(byte*)bmd.Scan0.ToPointer();
int index=y*bmd.Stride+(x>>3);
byte mask=(byte)(0x80>>(x&0x7));
return (p[index]==mask);
}
But this only works for less than 1% of the pixels! There's a lot of
assignents and byte-manipulation in the SetIndexedPixel method that I don't
really understand, so I'm not sure if my GetIndexedPixel is correct at all
(something tells me its not). Can anyone spot the error?
Regards
/Morten Nielsen
http://www.iter.dk
I have found this example to set a pixel :
private unsafe void SetIndexedPixel(int x,int y,BitmapData bmd, bool pixel)
{
byte* p = (byte*)bmd.Scan0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));
if (pixel)
p[index]|=mask;
else
p[index]&=(byte)(mask^0xff);
}
....which seems to work ok, but I haven't been able to find an example of
doing a GetIndexedPixel(), so I tried myself :
protected unsafe bool GetIndexedPixel(int x,int y,BitmapData bmd)
{
byte* p=(byte*)bmd.Scan0.ToPointer();
int index=y*bmd.Stride+(x>>3);
byte mask=(byte)(0x80>>(x&0x7));
return (p[index]==mask);
}
But this only works for less than 1% of the pixels! There's a lot of
assignents and byte-manipulation in the SetIndexedPixel method that I don't
really understand, so I'm not sure if my GetIndexedPixel is correct at all
(something tells me its not). Can anyone spot the error?
Regards
/Morten Nielsen
http://www.iter.dk