Jump to content
  • Advertisement

Archived

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

s_cloudx

How to get a pixel from a surface?

This topic is 5971 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 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 this post


Link to post
Share on other sites
Advertisement
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,32

COLORREF 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

AdmiralBinary, thanks again! I didn''t realize I already had the answer in my pc!

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

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!