void CGSH_Direct3D9::CopyRenderTargetToBitmap(Framework::CBitmap& outputBitmap, const TexturePtr& renderTarget, uint32 renderTargetWidth, uint32 renderTargetHeight, uint32 width, uint32 height) { HRESULT result = S_OK; SurfacePtr offscreenSurface, renderTargetSurface; result = renderTarget->GetSurfaceLevel(0, &renderTargetSurface); assert(SUCCEEDED(result)); result = m_device->CreateOffscreenPlainSurface(renderTargetWidth, renderTargetHeight, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &offscreenSurface, nullptr); assert(SUCCEEDED(result)); result = m_device->GetRenderTargetData(renderTargetSurface, offscreenSurface); assert(SUCCEEDED(result)); outputBitmap = Framework::CBitmap(width, height, 32); D3DLOCKED_RECT lockedRect = {}; result = offscreenSurface->LockRect(&lockedRect, nullptr, D3DLOCK_READONLY); assert(SUCCEEDED(result)); uint8* srcPtr = reinterpret_cast<uint8*>(lockedRect.pBits); uint8* dstPtr = reinterpret_cast<uint8*>(outputBitmap.GetPixels()); uint32 copyWidth = std::min<uint32>(renderTargetWidth, width); uint32 copyHeight = std::min<uint32>(renderTargetHeight, height); for(unsigned int y = 0; y < copyHeight; y++) { memcpy(dstPtr, srcPtr, copyWidth * 4); dstPtr += outputBitmap.GetPitch(); srcPtr += lockedRect.Pitch; } result = offscreenSurface->UnlockRect(); assert(SUCCEEDED(result)); }
Framework::CBitmap CPixelBufferView::ConvertBGRToRGB(Framework::CBitmap bitmap) { for(int32 y = 0; y < bitmap.GetHeight(); y++) { for(int32 x = 0; x < bitmap.GetWidth(); x++) { auto color = bitmap.GetPixel(x, y); std::swap(color.r, color.b); bitmap.SetPixel(x, y, color); } } return std::move(bitmap); }
CPixelBufferView::TexturePtr CPixelBufferView::CreateTextureFromBitmap(const Framework::CBitmap& bitmap) { TexturePtr texture; if(!bitmap.IsEmpty()) { HRESULT result = S_OK; result = m_device->CreateTexture(bitmap.GetWidth(), bitmap.GetHeight(), 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr); assert(SUCCEEDED(result)); D3DLOCKED_RECT lockedRect = {}; result = texture->LockRect(0, &lockedRect, nullptr, 0); assert(SUCCEEDED(result)); uint32* dstPtr = reinterpret_cast<uint32*>(lockedRect.pBits); uint32* srcPtr = reinterpret_cast<uint32*>(bitmap.GetPixels()); for(unsigned int y = 0; y < bitmap.GetHeight(); y++) { memcpy(dstPtr, srcPtr, bitmap.GetWidth() * sizeof(uint32)); dstPtr += lockedRect.Pitch / sizeof(uint32); srcPtr += bitmap.GetPitch() / sizeof(uint32); } result = texture->UnlockRect(0); assert(SUCCEEDED(result)); } return texture; }
CPixelBufferView::TexturePtr CPixelBufferView::CreateTextureFromBitmap(const Framework::CBitmap& bitmap) { TexturePtr texture; if(!bitmap.IsEmpty()) { D3DFORMAT textureFormat = [bitmap]() { switch(bitmap.GetBitsPerPixel()) { case 8: return D3DFMT_L8; case 16: return D3DFMT_A1R5G5B5; case 32: default: return D3DFMT_A8R8G8B8; } }(); HRESULT result = S_OK; result = m_device->CreateTexture(bitmap.GetWidth(), bitmap.GetHeight(), 1, D3DUSAGE_DYNAMIC, textureFormat, D3DPOOL_DEFAULT, &texture, nullptr); assert(SUCCEEDED(result)); switch(bitmap.GetBitsPerPixel()) { case 8: CopyBitmapToTexture<uint8>(texture, 0, bitmap); break; case 16: CopyBitmapToTexture<uint16>(texture, 0, bitmap); break; case 32: CopyBitmapToTexture<uint32>(texture, 0, bitmap); break; default: assert(false); break; } } return texture; }
void CGSH_Direct3D9::CopyTextureToBitmap(Framework::CBitmap& outputBitmap, const TexturePtr& texture, uint32 width, uint32 height) { outputBitmap = Framework::CBitmap(width, height, 32); HRESULT result = S_OK; D3DLOCKED_RECT lockedRect = {}; result = texture->LockRect(0, &lockedRect, nullptr, D3DLOCK_READONLY); assert(SUCCEEDED(result)); uint8* srcPtr = reinterpret_cast<uint8*>(lockedRect.pBits); uint8* dstPtr = reinterpret_cast<uint8*>(outputBitmap.GetPixels()); for(unsigned int y = 0; y < height; y++) { memcpy(dstPtr, srcPtr, width * 4); dstPtr += outputBitmap.GetPitch(); srcPtr += lockedRect.Pitch; } result = texture->UnlockRect(0); assert(SUCCEEDED(result)); }