Skip to main content
gamedev.net
🔒 Locked

Accessing pixels in bitmap

Started by Kieran Jan 14, 2004 at 3:07 PM 3 replies 400+ views
Original Post
Kieran
Kieran
I''m using C++. I want to load a bitmap into memory (always 24 bit, always 50x50 pixels). I then want to go through each of the pixels reading their RGB values. Basically, the bitmap is a heightmap for the land in my DX game. I will be using only the red component for this. The code I am using to load the bitmap into memory is this:
// handle to bitmap
HBITMAP hbm;
// load bitmap
hbm = (HBITMAP)LoadImage(NULL, "..\\maps/map001.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE); 
I think this is correct? Now how do I access a pixel and read its RGB value? I was thinking of using GetPixel (even though its slow), but it seems that you need a device context, and as this is just stored in memory I dont have one (or do I and Im being dumb?). Thanks in advance Kieran
porthios
porthios
Look into GetDIBits and SetDIBits.
Kieran
Kieran
Thanks, Ill look into that now. I wonder why I didnt get an email telling me I had a reply to this?
Kieran
Kieran
Well, in the end I didnt use your suggestion. This is how I did it. Please excuse the messy source, its a work-in-progress.

HRESULT CGraphics::AddHills()
{
HRESULT hr = S_OK;
// tweak the terrain vertices to add some bumpy terrain
if (m_pLand)
{
// get bitmap from either of the directories
HBITMAP hbm = NULL;
hbm = (HBITMAP)LoadImage(NULL, "..\\maps/map001.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE);
if (hbm == NULL)
hbm = (HBITMAP)LoadImage(NULL, "maps/map001.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE);
// create memory device context
HDC MyDC = CreateCompatibleDC(NULL);
// select bitmap to be in DC
SelectObject(MyDC, hbm);
// get access to the mesh vertices
LPDIRECT3DVERTEXBUFFER9 pVB;
LANDVERTEX* pVertices;
DWORD NumVertices = m_pLand->GetNumVertices();
m_pLand->GetVertexBuffer(&pVB);
pVB->Lock(0, 0, (void**)&pVertices, 0);
// go through the vertices adjusting
for (DWORD i=0; i {
// temp values
FLOAT dX = 1.0f/(m_n-1);
FLOAT dZ = 1.0f/(m_m-1);
// set new height
pVertices.p.y = (FLOAT)(GetRValue(
GetPixel(MyDC,
(INT)(((pVertices[i].p.x + 50.0f) / (m_scale*dX)) + 0.5f),
(INT)(((pVertices[i].p.z + 50.0f) / (m_scale*dZ)) + 0.5f))
)-128.0f)/10.0f;
}
// unlock then release vertex buffer
pVB->Unlock();
pVB->Release();
// delete DC
DeleteDC(MyDC);
// release the bitmap
DeleteObject(hbm);
}
return hr;
}
AndreTheGiant
AndreTheGiant
I wrote some windows independant code for loading data from a bitmap if your interested.

Actually, you have to include windows.h, but this is only for the declaration of the 3 bitmap structs (BITMAPINFOHEADER, BITMAPFILEHEADER... etc). If you really wanted to do without the windows.h file, you can declare these yourself, which I also did as an option.

Let me know if your interested. Sounds like youve already got something that works though.

[edited by - AndreTheGiant on January 15, 2004 2:21:26 PM]

[edited by - AndreTheGiant on January 15, 2004 2:21:49 PM]

Topic Locked

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

Sign in to reply to this topic.