• Advertisement
Sign in to follow this  

Problems reading texture data back

This topic is 554 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 Guys,

I am having trouble reading texture data back after loading a texture into RAM.

I have loaded a test BMP which is a 1x1 pixel with RGB values of 128, 129, 130, so I know what values to expect back.

The D3DX9 functions are reporting that the image is successfully loaded into RAM, but when I try to read he pixel data back the values are random each time the program is executed.

	hr = D3DXCreateTextureFromFile(d3dDevice, "d:\\1px.bmp", &d3dTexture);

	if (FAILED(hr))
	{
		MessageBox(NULL, "Load bitmap failed", 0, NULL);
		return 1;
	}

	D3DXIMAGE_INFO info;
	D3DXGetImageInfoFromFile("d:\\1px.bmp", &info);

	std::cout << "Image information\r\n\r\n";
	std::cout << "Width: " << info.Width << "\r\n";
	std::cout << "Height: " << info.Height << "\r\n";
	std::cout << "Format: " << info.Format << "\r\n\r\n";

	D3DLOCKED_RECT locked;
	locked.Pitch = info.Width * 3;		// Hard coded (test BMP format is D3DFMT_R8G8B8)
	locked.pBits = { 0 };
	hr = d3dTexture->LockRect(0, &locked, NULL, 0);

	if (FAILED(hr))
	{
		MessageBox(NULL, "Lock rect failed", 0, NULL);
		return 1;
	}


	BYTE test = (BYTE)&locked.pBits+0;		// trying to grab value at offset (0 in this case)
	int test2 = test;
	std::cout << "Element data:" << test2 << "\r\n";      // Always returns a random value in 0-255 range

Any advice on how to read the RGB values back would be greatly appreciated.

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

You bitmap may be D3DFMT_R8G8B8 when on disk, but D3DXCreateTextureFromFile probably converts it to D3DFMT_X8R8G8B8. You should query d3dTexture to find out it's actual format.

 

More importantly though, locked.pBits is the address the pixel data... and you're printing out the address of locked.pBits, which is the address of the address of the pixel data.

Share this post


Link to post
Share on other sites

More importantly though, locked.pBits is the address the pixel data... and you're printing out the address of locked.pBits, which is the address of the address of the pixel data.


I think this is more the case of what is the source of the issue.

How would I get the actual data as opposed to the address of the data?

With and with out & gives the same symptoms (different value each time). If it were a simple format issue, i'd expect the results to remain constant. Edited by DarkRonin

Share this post


Link to post
Share on other sites

BYTE* bytes = (BYTE*)locked.pBits;
BYTE test = bytes[0];


Oh Man! Works perfectly.

As much as I try to understand pointers, they have a habit of biting me. Hopefully one day I'll get there. I really do try.

Thanks again for your help, it is truly appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement