diff options
author | crupest <crupest@outlook.com> | 2022-05-07 20:53:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-07 20:53:57 +0800 |
commit | ee5aa17e44cb36b386e89032cab96caf87b5b524 (patch) | |
tree | a7cef6c60f55e3870900016e0f1a4efe578efbf0 /src/osx/graphics/quartz/ImageFactory.cpp | |
parent | 5bc684dcc1d121bf6e02d0800174c7977c72d101 (diff) | |
parent | cb850a6d6d13fc5b2c0cdd8773e368e23252c459 (diff) | |
download | cru-ee5aa17e44cb36b386e89032cab96caf87b5b524.tar.gz cru-ee5aa17e44cb36b386e89032cab96caf87b5b524.tar.bz2 cru-ee5aa17e44cb36b386e89032cab96caf87b5b524.zip |
Merge pull request #54 from crupest/create-image
Diffstat (limited to 'src/osx/graphics/quartz/ImageFactory.cpp')
-rw-r--r-- | src/osx/graphics/quartz/ImageFactory.cpp | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp index 7796ef3a..a8c17719 100644 --- a/src/osx/graphics/quartz/ImageFactory.cpp +++ b/src/osx/graphics/quartz/ImageFactory.cpp @@ -1,4 +1,5 @@ #include "cru/osx/graphics/quartz/ImageFactory.h" +#include "cru/common/Exception.h" #include "cru/osx/graphics/quartz/Convert.h" #include "cru/osx/graphics/quartz/Image.h" #include "cru/platform/graphics/Image.h" @@ -20,12 +21,36 @@ std::unique_ptr<IImage> QuartzImageFactory::DecodeFromStream( CGImageRef cg_image = CGImageSourceCreateImageAtIndex(image_source, 0, nullptr); - QuartzImage* image = - new QuartzImage(GetGraphicsFactory(), this, cg_image, true); - - CFRelease(cg_image); + CFRelease(image_source); CGDataProviderRelease(data_provider); - return std::unique_ptr<IImage>(image); + return std::unique_ptr<IImage>( + new QuartzImage(GetGraphicsFactory(), this, cg_image, true)); +} + +std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width, + int height) { + if (width <= 0) throw Exception(u"Image width should be greater than 0."); + if (height <= 0) throw Exception(u"Image height should be greater than 0."); + + 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); + + CGColorSpaceRelease(color_space); + + return std::unique_ptr<IImage>( + new QuartzImage(GetGraphicsFactory(), this, cg_image, true, buffer)); } } // namespace cru::platform::graphics::osx::quartz |