gpt4 book ai didi

c++ - ID2D1Bitmap::CreateBitmap 的 srcData 是什么

转载 作者:行者123 更新时间:2023-11-30 04:44:51 25 4
gpt4 key购买 nike

我需要为我在 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...

第二行和第三行应该是什么样的?还有什么我做错了吗?

最佳答案

Syntax :

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 here

(c) - 这请求 DXGI_FORMAT_UNKNOWN D2D1_ALPHA_MODE_UNKNOWN 并导致位图创建错误。你需要一个真正的格式,比如 DXGI_FORMAT_B8G8R8A8_UNORM(参见 Pixel FormatsSupported 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;
}

关于c++ - ID2D1Bitmap::CreateBitmap 的 srcData 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com