Archived

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

reading pixels from texture surface?!

This topic is 5598 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 trying to move pixels from a texture in video memory, to an array and save to disk. Somehow something goes wrong. When checking out the bytes in the array I saved to disk, it looks completely wrong, and loading the picture as .raw in an imageeditor looks wrong too, just a pixel mess. Please help out, I'm going crazy! First I loaded the texture into memory like so if(FAILED(D3DXCreateTextureFromFileEx( pD3DDevice, lpszFileName, // filename D3DX_DEFAULT, // width D3DX_DEFAULT, // height D3DX_DEFAULT, // miplevels 0, // usage D3DFMT_A8R8G8B8, // color format D3DX_FILTER_NONE, // filter for texture D3DX_FILTER_NONE, // mip filter D3DCOLOR_RGBA(255,255,255,255), // color key NULL, // pSrcInfo NULL, // pPalette entry &pTexture // texture to create ))) And later.. the code for getting the pixels from videoRam: (temp is the target array to save to,pTexture is the videosurface) DWORD* temp=new DWORD[iTextureWidth*iTextureHeight]; D3DLOCKED_RECT lockData; DWORD* pPixels; int pitch; if(SUCCEEDED( pTexture->LockRect (0,&lockData, NULL,0))) { pitch=lockData.Pitch; pPixels=(DWORD*)lockData.pBits; long index=0; for(int y=0;yUnlockRect(0); [edited by - brewski on August 15, 2002 12:17:25 PM] [edited by - brewski on August 15, 2002 12:18:42 PM]

Share this post


Link to post
Share on other sites
First of all, you should use the code tags so your formatting and angular brackets don''t get spoilt by HTML.

Anyway, I''ve not looked at your code in detail, but what will be happening is that your texture is being stored as 16 or 32 bit in memory. I don''t know if your image editor expects that or not.

Also, do your D3D function calls succeed?


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Sorry bout the code-formatting, dont know the correct tag.

Anyhow, the locking of the surface is ok, no errors.

I''m unsure how the pixel data is stored in video ram, perhaps they are stored in different order ( the argb bytes ). Also I don''t know if I use the pitch correctly.

I just want to store the data in my array like aarrggbb.
I see clearly when viewing the data I saved to disk in hex, that it''s wrong.

Share this post


Link to post
Share on other sites
I checked the color format after the texture was loaded into video, with GetLevelDesc();
The format was D3DFMT_A8R8G8B8 as when I loaded it into memory.
So it should be alfa8,red8,green8,blue8.

So it''s not 16bits.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Tip of the day NEVER EVER EVER EVER EVER read from video memory. It is VERY VERY VERY SLOW!

Share this post


Link to post
Share on other sites
I know. I just want to create bitmasks from the textures, it''s not for realtime. I like to use the d3dx functions for loading images, instead of linking a image-library.

Share this post


Link to post
Share on other sites
When I had to do a similar task with ARGB format colors (but in VB, it may be slightly different for C++?), I declared a type like this:

Type COLOR_ARGB
Blue as Byte
Green as Byte
Red as Byte
Alpha as Byte
End Type

In other words, in reverse order (BGRA). After that I found that it worked. I think this is something to do with the word "endian" - search for some info on that.

Share this post


Link to post
Share on other sites
also most raw image readers cant handle an alpha channel so you have to store it in 24bit. seems to me using dx in this manner is a very roundabout and poor way of doing this. why are you not using an image editor to do this? also whats wrong with an image library to load the data? i use one coded myself from the jpeg reference library, png reference library, the bmp file spec, and the tga file spec. makes things much easier and faster. plus no worries about unintended conversions, filtering, etc. dx may resize yoru image to a power of two so becareful. read th docs and check what dx may do to your image.

you ware using the pitch correctly assuming you are using the width as the terminating condition for the loop.

Share this post


Link to post
Share on other sites