Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kieran

Accessing pixels in bitmap

This topic is 5329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Thanks, Ill look into that now. I wonder why I didnt get an email telling me I had a reply to this?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!