diff options
author | crupest <crupest@outlook.com> | 2022-05-10 11:33:22 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-05-10 11:33:22 +0800 |
commit | 5947b34096c7162bad4e17091f2a798c788b231b (patch) | |
tree | 2cb7949e5eee17223383e37e45f817d2c8ee87ee /src | |
parent | 58e37eee64b13df9cf24cdfc97906e19b2898869 (diff) | |
download | cru-5947b34096c7162bad4e17091f2a798c788b231b.tar.gz cru-5947b34096c7162bad4e17091f2a798c788b231b.tar.bz2 cru-5947b34096c7162bad4e17091f2a798c788b231b.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/win/graphics/direct/Image.cpp | 6 | ||||
-rw-r--r-- | src/win/graphics/direct/ImageFactory.cpp | 18 |
2 files changed, 20 insertions, 4 deletions
diff --git a/src/win/graphics/direct/Image.cpp b/src/win/graphics/direct/Image.cpp index c8230fd5..518b9516 100644 --- a/src/win/graphics/direct/Image.cpp +++ b/src/win/graphics/direct/Image.cpp @@ -1,4 +1,5 @@ #include "cru/win/graphics/direct/Image.h" +#include <d2d1_1.h> #include "cru/common/platform/win/Exception.h" #include "cru/win/graphics/direct/ConvertUtil.h" #include "cru/win/graphics/direct/Exception.h" @@ -24,7 +25,10 @@ std::unique_ptr<IImage> Direct2DImage::CreateWithRect(const Rect& rect) { Microsoft::WRL::ComPtr<ID2D1Bitmap1> bitmap; ThrowIfFailed(device_context->CreateBitmap( D2D1::SizeU(rect.width, rect.height), nullptr, 0, - D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET), &bitmap)); + D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, + D2D1_ALPHA_MODE_PREMULTIPLIED)), + &bitmap)); device_context->SetTarget(bitmap.Get()); device_context->BeginDraw(); device_context->DrawBitmap(d2d_bitmap_.Get(), Convert(rect)); diff --git a/src/win/graphics/direct/ImageFactory.cpp b/src/win/graphics/direct/ImageFactory.cpp index 4cbecc36..0e8eb1b6 100644 --- a/src/win/graphics/direct/ImageFactory.cpp +++ b/src/win/graphics/direct/ImageFactory.cpp @@ -6,6 +6,8 @@ #include "cru/win/graphics/direct/Factory.h" #include "cru/win/graphics/direct/Image.h" +#include <d2d1_1.h> +#include <d2d1_1helper.h> #include <wincodec.h> #include <wrl/client.h> @@ -78,22 +80,32 @@ void WinImageFactory::EncodeToStream(IImage* image, io::Stream* stream, platform::win::ConvertStreamToComStream(stream)); auto d2d_bitmap = direct_image->GetD2DBitmap(); - auto size = d2d_bitmap->GetSize(); + auto size = d2d_bitmap->GetPixelSize(); FLOAT dpi_x, dpi_y; d2d_bitmap->GetDpi(&dpi_x, &dpi_y); auto pixel_format = d2d_bitmap->GetPixelFormat(); Ensures(pixel_format.format == DXGI_FORMAT_B8G8R8A8_UNORM); Ensures(pixel_format.alphaMode == D2D1_ALPHA_MODE_PREMULTIPLIED); + Microsoft::WRL::ComPtr<ID2D1Bitmap1> cpu_bitmap; + ThrowIfFailed(GetDirectFactory()->GetDefaultD2D1DeviceContext()->CreateBitmap( + size, nullptr, 0, + D2D1::BitmapProperties1( + D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_CPU_READ, + pixel_format, dpi_x, dpi_y), + &cpu_bitmap)); + + ThrowIfFailed(cpu_bitmap->CopyFromBitmap(nullptr, d2d_bitmap.Get(), nullptr)); + D2D1_MAPPED_RECT mapped_rect; - ThrowIfFailed(d2d_bitmap->Map(D2D1_MAP_OPTIONS_READ, &mapped_rect)); + ThrowIfFailed(cpu_bitmap->Map(D2D1_MAP_OPTIONS_READ, &mapped_rect)); Microsoft::WRL::ComPtr<IWICBitmap> wic_bitmap; ThrowIfFailed(wic_imaging_factory_->CreateBitmapFromMemory( size.width, size.height, GUID_WICPixelFormat32bppPBGRA, mapped_rect.pitch, mapped_rect.pitch * size.height, mapped_rect.bits, &wic_bitmap)); - ThrowIfFailed(d2d_bitmap->Unmap()); + ThrowIfFailed(cpu_bitmap->Unmap()); Microsoft::WRL::ComPtr<IWICBitmapEncoder> wic_bitmap_encoder; |