如何创建32位红色纹理字节缓冲区

时间:2018-12-23 06:09:19

标签: c++ image-processing gdi+ directx-11

我要制作红色纹理图像缓冲区。谁能帮助我以正确的方式做到这一点。我尝试了以下操作,但无法将缓冲区复制到ID3D11Texture2D中。我需要帮助:

std::vector<BYTE> redTexture(w*h*4);
const auto stride = w * 4;
BYTE* buf = redTexture.data();

for (int i = 0; i < h; ++i)
{
    const auto redValue = Gdiplus::Color::Red;
    memcpy(buf, &redValue, stride);
    buf += stride;
}

2 个答案:

答案 0 :(得分:0)

如果纹理的DXGI_FORMAT是R8G8B8A8_UNORM,则可以这样操作;

std::vector<BYTE> redTexture(w*h*4);

for(int i=0; i<redTexture.size(); i++)
{
    if(i%4==0)
    {
        redTexture[i]=255;
    }
    else if(i%4==3)
    {
        redTexture[i]=255;
    }
    else
    {
        redTexture[i]=0;
    }
}

由于每个像素都由RGBA 4字节值组成,因此您应该将第一个和最后一个字节分配给255,将其他字节分配给0以得到红色。

答案 1 :(得分:0)

感谢@HMD。我已通过以下操作解决了该问题:

    CImage m_cImage;
    // create a test image
    m_cImage.Create(w, -h, 8 * 4); // 8 bpp * 4 channel
    auto hdc = m_cImage.GetDC();
    Gdiplus::Graphics graphics(hdc);

    // Create a SolidBrush object.
    Gdiplus::SolidBrush redBrush(Gdiplus::Color::Red);

    // Fill the rectangle.
    Gdiplus::Status status = graphics.FillRectangle(&redBrush, 0, 0, w, h);
    TRY_CONDITION(status == Gdiplus::Status::Ok);
    ....
    // Then saved the m_cImage.GetBits() to bmp file using Gdiplus::Bitmap
    // and my expected texture is found