c++ - ID2D1Bitmap::CreateBitmap 的 srcData 是什么
问题描述
我需要为我在 Direct2D 中工作的光线投射器从像素数组创建位图。但是,我无法理解如何使用CreateBitmap函数。具体来说,我不确定srcData
参数应该是什么。我很确定/希望它是指向像素数组的指针,但我不确定如何设置该数组。它应该是什么样的数组?什么数据类型?等等。
这是我尝试过的:
int width = 400, height = 400;
D2D1::ColorF * arr = (D2D1::ColorF*)calloc(width * height * 4, sizeof(D2D1::ColorF));
for (int i = 0; i < width * height * 4; i++) { arr[i] = D2D1::ColorF(0.0f, 1.0f, 0.0f); }
// Create the bitmap and draw it on the screen
ID2D1Bitmap * bmp;
HRESULT hr;
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr,
width * sizeof(int) * 4,
D2D1::BitmapProperties(),
&bmp);
if (hr != S_OK) { return; } // I've tested and found that hr does not equal S_OK
// Draw the bitmap...
第二行和第三行应该是什么样子?还有什么我做错了吗?
解决方案
语法:
HRESULT CreateBitmap(
D2D1_SIZE_U size,
const void *srcData,
UINT32 pitch,
const D2D1_BITMAP_PROPERTIES & bitmapProperties,
ID2D1Bitmap **bitmap
);
你的代码:
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr, // <<--- Wrong, see (a) below
width * sizeof(int) * 4, // <<--- Close but wrong, see (b) below
D2D1::BitmapProperties(), // <<--- Wrong, see (c) below
&bmp);
(a) - 您应该在这里提供一个像素数据数组,其中格式取决于位图的格式。请注意,这是可选的,您可以在不初始化的情况下创建位图。像素不D2D1::ColorF
完全。如果您请求相应的位图格式,它们可能是 4 字节 RGBA 数据,请参见下面的 (c)。
(b) - 这是行之间的距离(以字节为单位),如果您的像素应该是 32 位值,您通常会Width * 4
在这里想要
(c) - 这请求DXGI_FORMAT_UNKNOWN
D2D1_ALPHA_MODE_UNKNOWN
并导致位图创建错误。您需要一个真实的格式,例如DXGI_FORMAT_B8G8R8A8_UNORM
(参见Pixel Formats以及Supported Pixel Formats and Alpha Modes)
上面的第一个链接显示了内存中的字节如何准确地映射到像素颜色,并且您应该分别准备数据。
UPD
你DXGI_FORMAT_B8G8R8A8_UNORM
的初始化结构是这样的:
UINT8* Data = malloc(Height * Width * 4);
for(UINT Y = 0; Y < Height; Y++)
for(UINT X = 0; X < Width; X++)
{
UINT8* PixelData = Data + ((Y * Width) + X) * 4;
PixelData[0] = unsigned integer blue in range 0..255;
PixelData[1] = unsigned integer red in range 0..255;
PixelData[2] = unsigned integer green in range 0..255;
PixelData[3] = 255;
}
推荐阅读
- ios - 简单 SpriteKit 场景中的资产大小问题
- c# - getJSON 在第二次请求时丢失路径中的控制器
- python - ValueError:无法将大小为 1048576 的数组重塑为形状 (1024,1024,3)
- lbph-algorithm - 边界上的局部二进制模式像素?
- java - 如果进程终止,Redisson Cache Map 不会驱逐
- mysql - 自定义数据库结果 [Symfony 4]
- php - Symfony - 工作流组件 - 无法识别的选项“元数据”
- detection - 检测 AWS Elastic 部署失败
- typescript - MSBuild TypeScript 不重新生成所有 JS 文件
- nativescript - Nativescript - Onesignal 休息 API