Skip to main content
GameDev.net gamedev.net
🔒 Locked

bitmap info

Started by GameDev.net Dec 21, 1999 at 4:42 AM 3 replies 950+ views
Original Post
GameDev.net
GameDev.net
I need to find out the color of a single pixel referenced by an x,y co-ordinate in a 8 bit bitmap.
Is there an easy way of doing this with directx?
Gromit
Gromit
The answer is yes. A very easy way I might add. I might even go as far as to say... simple. Very easy indeed.

Do you want to know how?

William Reiach - Human Extrodinaire

Marlene and Me

Aeetes
Aeetes
You can also do it by locking the surface, getting a pointer to the memory, then reading at the appropriate byte offset.

This way isn't more simple than going through a DC, but I believe it is faster (It avoids going through the Win API, and GetDC implicity calls Lock and ReleaseDC calls Unlock).

This is the code that could do it:

code:

char GetPixel(IDirectDrawSurface7 *lpDDSurface, DWORD x, DWORD y) {
DDSURFACEDESC2 ddsd;
ddsd.dwSize = sizeof(DDSURFACEDESC2);
if (FAILED(lpDDSurface->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL))
; // error obtaining surface memory
char nColor = *((char *)((int)ddsd.lpSurface + x + ddsd.lPitch * y));
lpDDSurface->Unlock();
return nColor;
}


------------------
Where's my Golden Fleece?

[This message has been edited by Aeetes (edited December 20, 1999).]

CJ
CJ
It's paletized, so you would need an unsigned char instead oa char (unsigned char = 0 - 255, char = -128 - 127). And I don't think the int is necessary.....


unsigned char nColor = *((unsigned char *)((int)ddsd.lpSurface + x + ddsd.lPitch * y));

BUT this only puts the value of that place in nColor. So...you still need to find out the RGB color (in the palette itself).

I don't know how you load your bitmap. But if you use GetDIBits (I know, I know...it's slow, but only needs to be done once). You allocate enough memory for both the BITMAPINFOHEADER (so.....the size of the BITMAPINFOHEADER AND 256 * sizeof(RGBQUAD)...
and after that call GetDIBits TWICE!!!!!!!!!!!! The second time to load the palette.

If you use this method, you will then only have to put the value of that specific pixel into the palette thingy....and you get the color........

easy huh?

------------------
Dance with me......

http://members.xoom.com/CJdeVos/index.htm

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.