Jump to content
  • Advertisement

Archived

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

Seriema

detailed reading an image

This topic is 5978 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

Hi! I just read TANSTAAFLs tip on sprite handling(http://www.gamedev.net/reference/articles/article1122.asp) and I thought "hey! what a great idea!" I know how to read a image into a texture... but how do I read it "detailed"? So that I can find the different colors and stuff in the image?? thanx... }+TITANIUM+{

Share this post


Link to post
Share on other sites
Advertisement
Once you have created a texture and have a valid texture interface you can access the first level surface using the following code. We need to do this to obtain the surface width and height and pixel formats.

----------
LPDIRECT3DTEXTURE8 pTexture .... = valid texture

D3DSURFACE_DESC SurfaceInfo; // Structure to hold surface Info
ZeroMemory(&SurfaceInfo,sizeof(D3DSURFACE_DESC);
LPDIRECT3DSURFACE8 pSurface = NULL;
pTexture->GetSurfaceLevel(0,&pSurface);
pSurface->GetDesc(&SurfaceInfo)

----------

Now that we have the surface info, we know the surface height and width, i.e. the SurfaceInfo.Width value etc. To find a color at a specific point we first need to lock the texture passing 0 as the level:

----------

D3DLOCKED_RECT rcLocked;
pTexture->LockRect(0,&rcLocked,0,0);

----------

And we can cast the bits into a LPDWORD variable:

----------

LPDWORD pBits = (DWORD*)rcLocked.pBits

----------

The Bits are stored in D3DCOLOR values, so to obtain any point in the surface, all we need to do is to access the point in the array . I.e.

----------

DWORD x = 100; (any coordinate in the width range)
DWORD y = 100; (any coordinate in the height range)

D3DCOLOR Color = pBits[SurfaceInfo.Width*y+x];

----------

The surface can be altered in any way and then we need to unlock the texture afterwards using:

pTexture->UnlockRect(0);

----------

P.s In my programs I always have code to retrieve the red green and blue and alpha components from a D3DCOLOR value: these are shown below

D3DCOLOR Clr = D3DCOLOR_ARGB(255,60,70,89); // Some Arbitrary Color

BYTE Alpha = (BYTE)((Clr&0xff000000)>>24);
BYTE Red = (BYTE)((Clr&0x00ff0000)>>16);
BYTE Blue = (BYTE)((Clr&0x000000ff));
BYTE Green = (BYTE)((Clr&0x0000ff00)>>8);



Hope this helps you










Share this post


Link to post
Share on other sites
I do spot one problem with this - texture size issues.

D3D will convert textures to power of 2 sizes thus losing accuracy and potentially having different results on cards which can handle non power of 2 textures, small texture sizes, etc.

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!