diff options
author | crupest <crupest@outlook.com> | 2022-05-15 14:08:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-05-15 14:08:06 +0800 |
commit | 8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6 (patch) | |
tree | 77e41cc14264060517c0f7ed95837012afb8342e /src/platform/graphics/quartz/Image.cpp | |
parent | 9e0c9d3499bc50c3534b4dc500d8b5d0b5f22752 (diff) | |
download | cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.tar.gz cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.tar.bz2 cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.zip |
...
Diffstat (limited to 'src/platform/graphics/quartz/Image.cpp')
-rw-r--r-- | src/platform/graphics/quartz/Image.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/platform/graphics/quartz/Image.cpp b/src/platform/graphics/quartz/Image.cpp new file mode 100644 index 00000000..3fa40937 --- /dev/null +++ b/src/platform/graphics/quartz/Image.cpp @@ -0,0 +1,58 @@ +#include "cru/platform/graphics/quartz/Image.h" +#include "cru/common/Exception.h" +#include "cru/platform/graphics/quartz/Convert.h" +#include "cru/platform/graphics/quartz/Painter.h" + +namespace cru::platform::graphics::quartz { +QuartzImage::QuartzImage(IGraphicsFactory* graphics_factory, + IImageFactory* image_factory, CGImageRef image, + bool auto_release, unsigned char* buffer) + : OsxQuartzResource(graphics_factory), + image_factory_(image_factory), + image_(image), + auto_release_(auto_release), + buffer_(buffer) { + Expects(image); +} + +QuartzImage::~QuartzImage() { + if (auto_release_) { + CGImageRelease(image_); + } +} + +float QuartzImage::GetWidth() { return CGImageGetWidth(image_); } + +float QuartzImage::GetHeight() { return CGImageGetHeight(image_); } + +std::unique_ptr<IImage> QuartzImage::CreateWithRect(const Rect& rect) { + auto new_cg_image = CGImageCreateWithImageInRect(image_, Convert(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::quartz |