diff options
author | crupest <crupest@outlook.com> | 2022-05-05 20:05:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-05-05 20:05:35 +0800 |
commit | 56a8047c4093c928b8551bbacda796382984512e (patch) | |
tree | 8891fef971686184511af6b98d111f1b68c13fd0 /src/osx/graphics/quartz | |
parent | 7b4b334e777e573040569801d3b4ab28a606a127 (diff) | |
download | cru-56a8047c4093c928b8551bbacda796382984512e.tar.gz cru-56a8047c4093c928b8551bbacda796382984512e.tar.bz2 cru-56a8047c4093c928b8551bbacda796382984512e.zip |
...
Diffstat (limited to 'src/osx/graphics/quartz')
-rw-r--r-- | src/osx/graphics/quartz/Image.cpp | 31 | ||||
-rw-r--r-- | src/osx/graphics/quartz/ImageFactory.cpp | 11 |
2 files changed, 39 insertions, 3 deletions
diff --git a/src/osx/graphics/quartz/Image.cpp b/src/osx/graphics/quartz/Image.cpp index db10da76..feddad8c 100644 --- a/src/osx/graphics/quartz/Image.cpp +++ b/src/osx/graphics/quartz/Image.cpp @@ -1,14 +1,17 @@ #include "cru/osx/graphics/quartz/Image.h" +#include "cru/common/Exception.h" #include "cru/osx/graphics/quartz/Convert.h" +#include "cru/osx/graphics/quartz/Painter.h" namespace cru::platform::graphics::osx::quartz { QuartzImage::QuartzImage(IGraphicsFactory* graphics_factory, IImageFactory* image_factory, CGImageRef image, - bool auto_release) + bool auto_release, unsigned char* buffer) : OsxQuartzResource(graphics_factory), image_factory_(image_factory), image_(image), - auto_release_(auto_release) {} + auto_release_(auto_release), + buffer_(buffer) {} QuartzImage::~QuartzImage() { if (auto_release_) { @@ -26,4 +29,28 @@ std::unique_ptr<IImage> QuartzImage::CreateWithRect(const Rect& rect) { return std::make_unique<QuartzImage>(GetGraphicsFactory(), image_factory_, new_cg_image, true); } + +std::unique_ptr<IPainter> QuartzImage::CreatePainter() { + if (!buffer_) + throw Exception( + u"Failed to create painter for image because failed to get its " + u"buffer."); + + auto width = CGImageGetWidth(image_); + auto height = CGImageGetHeight(image_); + auto bits_per_component = CGImageGetBitsPerComponent(image_); + auto bytes_per_row = CGImageGetBytesPerRow(image_); + auto color_space = CGImageGetColorSpace(image_); + auto bitmap_info = CGImageGetBitmapInfo(image_); + + auto cg_context = + CGBitmapContextCreate(buffer_, width, height, bits_per_component, + bytes_per_row, color_space, bitmap_info); + + return std::make_unique<QuartzCGContextPainter>( + GetGraphicsFactory(), cg_context, true, Size(width, height), + [](QuartzCGContextPainter* painter) { + + }); +} } // namespace cru::platform::graphics::osx::quartz diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp index 95315e1a..a8c17719 100644 --- a/src/osx/graphics/quartz/ImageFactory.cpp +++ b/src/osx/graphics/quartz/ImageFactory.cpp @@ -35,6 +35,15 @@ std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width, CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); + const auto buffer_size = width * height * 4; + auto buffer = new unsigned char[buffer_size]; + + auto cg_data_provider = CGDataProviderCreateWithData( + nullptr, buffer, buffer_size, + [](void* info, const void* data, size_t size) { + delete[] static_cast<const unsigned char*>(data); + }); + auto cg_image = CGImageCreate(width, height, 8, 32, 4 * width, color_space, kCGImageAlphaLast, nullptr, nullptr, true, kCGRenderingIntentDefault); @@ -42,6 +51,6 @@ std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width, CGColorSpaceRelease(color_space); return std::unique_ptr<IImage>( - new QuartzImage(GetGraphicsFactory(), this, cg_image, true)); + new QuartzImage(GetGraphicsFactory(), this, cg_image, true, buffer)); } } // namespace cru::platform::graphics::osx::quartz |