Accessing pixels in bitmap

Started by
3 comments, last by Kieran 20 years, 3 months ago
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
Advertisement
Look into GetDIBits and SetDIBits.
Thanks, Ill look into that now. I wonder why I didnt get an email telling me I had a reply to this?
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.p.x + 50.0f) / (m_scale*dX)) + 0.5f),<br>					(INT)(((pVertices.p.z + 50.0f) / (m_scale*dZ)) + 0.5f))<br>					)-128.0f)/10.0f;<br>		}<br>		// unlock then release vertex buffer<br>		pVB->Unlock();<br>		pVB->Release();<br>		// delete DC<br>		DeleteDC(MyDC);<br>		// release the bitmap<br>		DeleteObject(hbm);<br>	}<br>	return hr;<br>} </pre>   </i>  
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]

This topic is closed to new replies.

Advertisement