aboutsummaryrefslogtreecommitdiff
path: root/src/win/graphics/direct/ImageFactory.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-05-10 11:33:22 +0800
committercrupest <crupest@outlook.com>2022-05-10 11:33:22 +0800
commit5947b34096c7162bad4e17091f2a798c788b231b (patch)
tree2cb7949e5eee17223383e37e45f817d2c8ee87ee /src/win/graphics/direct/ImageFactory.cpp
parent58e37eee64b13df9cf24cdfc97906e19b2898869 (diff)
downloadcru-5947b34096c7162bad4e17091f2a798c788b231b.tar.gz
cru-5947b34096c7162bad4e17091f2a798c788b231b.tar.bz2
cru-5947b34096c7162bad4e17091f2a798c788b231b.zip
...
Diffstat (limited to 'src/win/graphics/direct/ImageFactory.cpp')
-rw-r--r--src/win/graphics/direct/ImageFactory.cpp18
1 files changed, 15 insertions, 3 deletions
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;