|
|
如何用DX9得到当前屏幕的指定坐标的RGB值?
有代码如下,但是。。。不能得到正确的RGB值。
RGBQUAD rgb;
g_pd3dDevice- >GetFrontBufferData(0, g_pSurface);
// Get the surface description.
D3DSURFACE_DESC surfaceDesc;
g_pSurface- >GetDesc(&surfaceDesc);
D3DLOCKED_RECT lockedRect;
if(FAILED(g_pSurface- >LockRect(&lockedRect,NULL,D3DLOCK_NO_DIRTY_UPDATE &brvbarD3DLOCK_NOSYSLOCK &brvbarD3DLOCK_READONLY)))
{
ErrorMessage("Unable to Lock Front Buffer Surface"); return RGBQUAD();
}
// Iterate through each pixel in the surface and set it to red.
DWORD* imageData = (DWORD*)lockedRect.pBits;
//for (unsigned int i = 0; i < surfaceDesc.Height; i++)
//{
// for (unsigned int j = 0; j < surfaceDesc.Width; j++)
// {
// // index into texture, note we use the pitch and divide by
// // four since the pitch is given in bytes and there are
// // 4 bytes per DWORD.
// int index = i * lockedRect.Pitch / 4 + j;
// imageData[index] = 0xffff0000; // red
// }
//}
// 得到指定坐标的颜色信息
int index = x * lockedRect.Pitch / 4 + y;
std::bitset <32 > color(imageData[index]);
std::string s;
// R
s.clear();
for (int j = 8; j < 16; ++j)
{
if (color[j])
{
s.push_back( '1 ');
}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > r(s);
rgb.rgbRed = (unsigned char)r.to_ulong();
// G
s.clear();
for (int j = 16; j < 24; ++j)
{
if (color[j])
{
s.push_back( '1 ');
}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > g(s);
rgb.rgbGreen = (unsigned char)g.to_ulong();
// B
s.clear();
for (int j = 24; j < 32; ++j)
{
if (color[j])
{
s.push_back( '1 ');
}
else
{
s.push_back( '0 ');
}
}
std::bitset <8 > b(s);
rgb.rgbBlue = (unsigned char)b.to_ulong();
//for(int i=0;i <gScreenRect.bottom;i++)
//{
// memcpy((BYTE*)pBits+(gScreenRect.bottom-i-1)*gScreenRect.right*BITSPERPIXEL/8,
// (BYTE*)lockedRect.pBits+i*lockedRect.Pitch,
// gScreenRect.right*BITSPERPIXEL/8);//g_d3dpp.BackBufferHeight*g_d3dpp.BackBufferWidth*4);
//}
g_pSurface- >UnlockRect();
return rgb; |
|