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