diff options
author | crupest <crupest@outlook.com> | 2022-05-05 19:31:18 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-05-05 19:31:18 +0800 |
commit | 7b4b334e777e573040569801d3b4ab28a606a127 (patch) | |
tree | 91db9cf17dd1936e44dc21c2e86d61642129bd41 | |
parent | 5bc684dcc1d121bf6e02d0800174c7977c72d101 (diff) | |
download | cru-7b4b334e777e573040569801d3b4ab28a606a127.tar.gz cru-7b4b334e777e573040569801d3b4ab28a606a127.tar.bz2 cru-7b4b334e777e573040569801d3b4ab28a606a127.zip |
...
-rw-r--r-- | include/cru/osx/graphics/quartz/ImageFactory.h | 1 | ||||
-rw-r--r-- | include/cru/platform/graphics/ImageFactory.h | 7 | ||||
-rw-r--r-- | src/osx/graphics/quartz/ImageFactory.cpp | 26 |
3 files changed, 29 insertions, 5 deletions
diff --git a/include/cru/osx/graphics/quartz/ImageFactory.h b/include/cru/osx/graphics/quartz/ImageFactory.h index e9854738..19e9c8c1 100644 --- a/include/cru/osx/graphics/quartz/ImageFactory.h +++ b/include/cru/osx/graphics/quartz/ImageFactory.h @@ -15,5 +15,6 @@ class QuartzImageFactory : public OsxQuartzResource, public: std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) override; + std::unique_ptr<IImage> CreateBitmap(int width, int height) override; }; } // namespace cru::platform::graphics::osx::quartz diff --git a/include/cru/platform/graphics/ImageFactory.h b/include/cru/platform/graphics/ImageFactory.h index 2a7902b2..e74f57e0 100644 --- a/include/cru/platform/graphics/ImageFactory.h +++ b/include/cru/platform/graphics/ImageFactory.h @@ -7,5 +7,12 @@ namespace cru::platform::graphics { struct CRU_PLATFORM_GRAPHICS_API IImageFactory : public virtual IGraphicsResource { virtual std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) = 0; + + /** + * \brief Create an empty bitmap with given width and height. + * \remarks Implementation should ensure that the bitmap supports alpha + * channel. It had better be in 32-bit rgba format. + */ + virtual std::unique_ptr<IImage> CreateBitmap(int width, int height) = 0; }; } // namespace cru::platform::graphics diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp index 7796ef3a..95315e1a 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,27 @@ 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(); + + 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)); } } // namespace cru::platform::graphics::osx::quartz |