Re: Grabbing the pixels of a bmp
"Jack" <jl@knight.com> wrote:
The problem I am having now is the bitmap pointer is byte-based.
I want to read 24 to 32 bit bitmaps
Any comments?
"Pointer is byte-based" doesn't really mean anything. Every bitmap is just
a string of bytes. It just so happens that, in a 24-bit DIB, a pixel is
spread across three bytes.
doesn't work either
pBitmap->GetBitmap(&bmpX);
BYTE* bmpBuffer = (BYTE *)GlobalAlloc (GPTR, bmpX.bmWidth*bmpX.bmHeight);
You don't need to allocate any space. The BITMAP structure contains a
pointer to the bytes of the bitmap.
for (int i = 0; i < ds.dsBm.bmHeight; i) {
for (int j = 0; i < ds.dsBm.bmWidth; j) {
How much C experience do you have? Both of those for loops are infinite
loops. You aren't bumping your loop counter. Plus, in the second one,
you're testing the wrong variable.
if (bmpBuffer[i*ds.dsBm.bmHeight] == RGB(255,255,0))
AfxMessageBox ("Hit it", MB_OK, NULL);
}
}
}
Every pixel in a 24-bit DIB occupies three bytes. So, pixel X,Y is at
((Y * width) + X) * 3
However, there may be padding at the end of each scanline. The actual
distance from the start of one scanline to the start of the next is in
bmWidthBytes.
pBitmap->GetBitmap(&bmpX);
BYTE* bmpBuffer = bmpX.bmBits;
for (int i = 0; i < bmpX.bmHeight; i++) {
for (int j = 0; j < bmpX.bmWidth; j++) {
int iPixelOffset =
j * bmpX.bmWidthBytes +
i * bmpX.bmBitsPixel / 8;
unsigned long pixel = *(unsigned long *)(bmpBuffer + iPixelOffset) &
0xffffff;
if (pixel == RGB(255,255,0))
AfxMessageBox ("Hit it", MB_OK, NULL);
}
}
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.