fix: create texture from memory by WIC

This commit is contained in:
Nomango 2023-09-27 17:44:11 +08:00
parent 524ddd055a
commit 6842e34713
1 changed files with 66 additions and 0 deletions

View File

@ -52,6 +52,21 @@ inline DXGI_FORMAT ConvertPixelFormat(PixelFormat format, UINT32& pitch)
}
}
inline const GUID& ConvertPixelFormat2WIC(PixelFormat format, UINT& stride)
{
switch (format)
{
case PixelFormat::Bpp32RGBA:
stride = 4;
return GUID_WICPixelFormat32bppRGBA;
case PixelFormat::Bpp32BGRA:
stride = 4;
return GUID_WICPixelFormat32bppBGRA;
default:
return GUID_WICPixelFormatDontCare;
}
}
Renderer& Renderer::GetInstance()
{
return RendererImpl::GetInstance();
@ -316,6 +331,56 @@ void RendererImpl::CreateTexture(Texture& texture, const BinaryData& data)
KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture failed");
}
void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const BinaryData& data, PixelFormat format)
{
HRESULT hr = S_OK;
if (!d2d_res_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
hr = data.IsValid() ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
UINT stride = 0;
const auto& wicFormat = ConvertPixelFormat2WIC(format, stride);
ComPtr<IWICBitmapSource> source;
hr = d2d_res_->CreateBitmapSourceFromMemory(source, UINT(size.x), UINT(size.y), UINT(size.x) * stride,
UINT(data.size), reinterpret_cast<BYTE*>(data.buffer),
wicFormat);
if (SUCCEEDED(hr))
{
ComPtr<IWICFormatConverter> converter;
hr = d2d_res_->CreateBitmapConverter(converter, source, GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone, nullptr, 0.f,
WICBitmapPaletteTypeMedianCut);
if (SUCCEEDED(hr))
{
ComPtr<ID2D1Bitmap> bitmap;
hr = d2d_res_->CreateBitmapFromConverter(bitmap, nullptr, converter);
if (SUCCEEDED(hr))
{
ComPolicy::Set(texture, bitmap);
texture.SetSize({ bitmap->GetSize().width, bitmap->GetSize().height });
texture.SetSizeInPixels({ bitmap->GetPixelSize().width, bitmap->GetPixelSize().height });
}
}
}
}
}
KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture from memory failed");
}
/*
void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const BinaryData& data, PixelFormat format)
{
HRESULT hr = S_OK;
@ -351,6 +416,7 @@ void RendererImpl::CreateTexture(Texture& texture, const PixelSize& size, const
KGE_SET_STATUS_IF_FAILED(hr, texture, "Load texture from memory failed");
}
*/
void RendererImpl::CreateGifImage(GifImage& gif, const String& file_path)
{