• ### What is your GameDev Story?

#### Archived

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

# How to get a pixel from a surface?

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

## Recommended Posts

I want to read a pixel in a surface so I can pass it's color to an array. How do I do that? I'm running in DX7 and C/C++; Edited by - s_cloudx on February 27, 2002 4:06:59 AM

##### Share on other sites
er, is it possible?

##### Share on other sites
You could use the GDI GetPixel method:
  HDC hdc;lpSurface->GetDC(&hdc);//Now you can access the hdc using all the standard GDI functions.//Access colour at 32,32COLORREF cr = GetPixel(hdc,32,32);//Remember to do this!lpSurface->ReleaseDC(hdc);

Should work. I have a feeling some fanatic is about to start flaming me about the evilness of using GDI functions though

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
AdmiralBinary, I thank you! that''s exactly what I was looking for. One last question. How do I extract the rgb of the resultant COLORREF? I''m kinda lost here.
(yes, getdc is slow but it''s on initialization anyways so it won''t matter. )

er, IndirectX, I said DX7. guess you missed that. oh well. thanks for the help anyways. :D

##### Share on other sites
Don''t bother with the GDI (yes, I''ll be that person... but only because there is a much simpler way). Lock the surface, then use either GetLockedArray or GetLockedPixel to get the pixel (use GetLockedArray if you are grabbing lots of pixels and GetLockedPixel if you are only getting one).

Then, use GetPixelFormat to decypher the pixels.

Don''t forget to unlock the surface once you are done.

Trying is the first step towards failure.

##### Share on other sites
Wow, I''m surprised no gave teh best answer yet. In DirectDraw all you do is:

Call IDirectDrawSurface7::Lock() on the surface, and make sure DDLOCK_SURFACEMEMORYPTR is one of the properties bitwise or''ed with the other DDLOCK properties. And then, after the call to lock if it succeeds, the DDSURFACEDESC structure will have the pointer to the memory surface stored in the lpSurface feild.

So in psuedo code it looks like(dont know how to use white boxes):

---------------------------------------------------------
surface->Lock(NULL, &ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);

DWORD * memory = (DWORD *)ddsd.lpSurface;

DWORD * pixel = memory[x + y* ddsd.lPitch]; //This gets a pixel colo
memory[x + y * ddsd.lPitch] = 0xFF00FF00; //Set toBlue in 32-bit mode
---------------------------------------------------------

Basically, ddsd.lpSurface is a void pointer that needs to be casted to another type - depending on the bit depth of the surface. It may be 16(WORD), 32(DWORD), or 8(BYTE) - 24 is DWORD but with 4 bits unused, I think.... I hope that shed some light. As soon as you get that pointer you can read or write to the surface memory as you wish.

--I hope you know what pitch is. If you dont then ask.

##### Share on other sites
quote:
One last question. How do I extract the rgb of the resultant COLORREF? I''m kinda lost here.

I think a couple of ppl have beat me to this one, but I think I do know an easier method

GetRValue(colour)
GetGValue(colour)
GetBValue(colour)

These are just bitshifting macros, but they look neater...
Hope this helps

---------------

I finally got it all together...
...and then forgot where I put it.

##### Share on other sites
ragonastick, according to my SDK, GetLockedPixel and GetLockedArray is visual-basic specific (I might be wrong). sorry

EbonySeraph, you''re right. that is (the way I look at it) much faster. Pitch, I think, is the "extra memory" in the graphic card per line that exceeds the width of the resolution. Correct me if I''m wrong.

I''ll check out all your answers later when I get back to my pc.

##### Share on other sites
Yeah, that would be the pitch.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×