DirectX sets the color of a single pixel

I read in another thread that I can read one pixel with Texture.Lock / Unlock, but I need to write the pixels back to the texture after reading them, this is my code so far

unsigned int readPixel(LPDIRECT3DTEXTURE9 pTexture, UINT x, UINT y) { D3DLOCKED_RECT rect; ZeroMemory(&rect, sizeof(D3DLOCKED_RECT)); pTexture->LockRect(0, &rect, NULL, D3DLOCK_READONLY); unsigned char *bits = (unsigned char *)rect.pBits; unsigned int pixel = (unsigned int)&bits[rect.Pitch * y + 4 * x]; pTexture->UnlockRect(0); return pixel; } 

So my questions are:

 - How to write the pixels back to the texture? - How to get the ARGB values from this unsigned int? 

((BYTE) x β†’ 8/16/24) did not work for me (the return value from the function was 688)

+6
directx
source share
1 answer

1) One way is to use memcpy:

 memcpy( &bits[rect.Pitch * y + 4 * x]), &pixel, 4 ); 

2) There are several different ways. The simplest is to define the structure as follows:

 struct ARGB { char b; char g; char r; char a; }; 

Then change the pixel load code to the following:

 ARGB pixel; memcpy( &pixel, &bits[rect.Pitch * y + 4 * x]), 4 ); char red = pixel.r; 

You can also get all values ​​using masks and shifts. eg

 unsigned char a = (intPixel >> 24); unsigned char r = (intPixel >> 16) & 0xff; unsigned char g = (intPixel >> 8) & 0xff; unsigned char b = (intPixel) & 0xff; 
+5
source share

All Articles