我在另一个线程读取,我可以读取Texture.Lock /解锁单个像素,但我需要阅读它们之后的像素写回的质感,这是我到目前为止的代码
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;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
- How to write the pixels back to the texture?
- How to get the ARGB values from this unsigned int?
Run Code Online (Sandbox Code Playgroud)
((BYTE)x >> 8/16/24)对我没用(函数的返回值是688)
1)一种方法是使用memcpy:
memcpy( &bits[rect.Pitch * y + 4 * x]), &pixel, 4 );
Run Code Online (Sandbox Code Playgroud)
2)有许多不同的方法.最简单的是定义一个结构如下:
struct ARGB
{
char b;
char g;
char r;
char a;
};
Run Code Online (Sandbox Code Playgroud)
然后将像素加载代码更改为以下内容:
ARGB pixel;
memcpy( &pixel, &bits[rect.Pitch * y + 4 * x]), 4 );
char red = pixel.r;
Run Code Online (Sandbox Code Playgroud)
您还可以使用蒙版和移位获取所有值.例如
unsigned char a = (intPixel >> 24);
unsigned char r = (intPixel >> 16) & 0xff;
unsigned char g = (intPixel >> 8) & 0xff;
unsigned char b = (intPixel) & 0xff;
Run Code Online (Sandbox Code Playgroud)