aboutsummaryrefslogtreecommitdiff
path: root/src/osx
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-01-30 20:40:59 +0800
committercrupest <crupest@outlook.com>2022-01-30 20:40:59 +0800
commitc1f8ea13524f398f8d1720e5f03a17dd66352ebf (patch)
tree0e27ed132cd82d1a690aa9534df38beec0c77d45 /src/osx
parenta6dd823a94f0e6338545a4885e9d428e83a56593 (diff)
downloadcru-c1f8ea13524f398f8d1720e5f03a17dd66352ebf.tar.gz
cru-c1f8ea13524f398f8d1720e5f03a17dd66352ebf.tar.bz2
cru-c1f8ea13524f398f8d1720e5f03a17dd66352ebf.zip
...
Diffstat (limited to 'src/osx')
-rw-r--r--src/osx/graphics/quartz/CMakeLists.txt1
-rw-r--r--src/osx/graphics/quartz/Convert.cpp21
-rw-r--r--src/osx/graphics/quartz/Factory.cpp10
-rw-r--r--src/osx/graphics/quartz/ImageFactory.cpp31
4 files changed, 63 insertions, 0 deletions
diff --git a/src/osx/graphics/quartz/CMakeLists.txt b/src/osx/graphics/quartz/CMakeLists.txt
index f5aae675..d5260896 100644
--- a/src/osx/graphics/quartz/CMakeLists.txt
+++ b/src/osx/graphics/quartz/CMakeLists.txt
@@ -4,6 +4,7 @@ add_library(cru_osx_graphics_quartz SHARED
Factory.cpp
Font.cpp
Geometry.cpp
+ ImageFactory.cpp
Painter.cpp
Resource.cpp
TextLayout.cpp
diff --git a/src/osx/graphics/quartz/Convert.cpp b/src/osx/graphics/quartz/Convert.cpp
index af054e5a..1cd7ea1d 100644
--- a/src/osx/graphics/quartz/Convert.cpp
+++ b/src/osx/graphics/quartz/Convert.cpp
@@ -28,4 +28,25 @@ Rect Convert(const CGRect& rect) {
static_cast<float>(rect.size.width),
static_cast<float>(rect.size.height)};
}
+
+const CGDataProviderSequentialCallbacks kStreamToCGDataProviderCallbacks{
+ 1,
+ [](void* stream, void* buffer, size_t size) -> size_t {
+ return static_cast<io::Stream*>(stream)->Read(
+ static_cast<std::byte*>(buffer), size);
+ },
+ [](void* stream, off_t offset) -> off_t {
+ auto s = static_cast<io::Stream*>(stream);
+ auto current_position = s->Tell();
+ s->Seek(offset, io::Stream::SeekOrigin::Current);
+ return s->Tell() - current_position;
+ },
+ [](void* stream) { static_cast<io::Stream*>(stream)->Rewind(); },
+ [](void* stream) {}};
+
+CGDataProviderRef ConvertStreamToCGDataProvider(io::Stream* stream) {
+ return CGDataProviderCreateSequential(stream,
+ &kStreamToCGDataProviderCallbacks);
+}
+
} // namespace cru::platform::graphics::osx::quartz
diff --git a/src/osx/graphics/quartz/Factory.cpp b/src/osx/graphics/quartz/Factory.cpp
index 7aef9d7e..570fd099 100644
--- a/src/osx/graphics/quartz/Factory.cpp
+++ b/src/osx/graphics/quartz/Factory.cpp
@@ -3,12 +3,19 @@
#include "cru/osx/graphics/quartz/Brush.hpp"
#include "cru/osx/graphics/quartz/Font.hpp"
#include "cru/osx/graphics/quartz/Geometry.hpp"
+#include "cru/osx/graphics/quartz/ImageFactory.hpp"
#include "cru/osx/graphics/quartz/TextLayout.hpp"
#include "cru/platform/Check.hpp"
+#include "cru/platform/graphics/ImageFactory.hpp"
#include <memory>
namespace cru::platform::graphics::osx::quartz {
+QuartzGraphicsFactory::QuartzGraphicsFactory()
+ : OsxQuartzResource(this), image_factory_(new QuartzImageFactory(this)) {}
+
+QuartzGraphicsFactory::~QuartzGraphicsFactory() {}
+
std::unique_ptr<ISolidColorBrush>
QuartzGraphicsFactory::CreateSolidColorBrush() {
return std::make_unique<QuartzSolidColorBrush>(this, colors::black);
@@ -30,4 +37,7 @@ std::unique_ptr<ITextLayout> QuartzGraphicsFactory::CreateTextLayout(
return std::make_unique<OsxCTTextLayout>(this, f, text);
}
+IImageFactory* QuartzGraphicsFactory::GetImageFactory() {
+ return image_factory_.get();
+}
} // namespace cru::platform::graphics::osx::quartz
diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp
new file mode 100644
index 00000000..c59ff126
--- /dev/null
+++ b/src/osx/graphics/quartz/ImageFactory.cpp
@@ -0,0 +1,31 @@
+#include "cru/osx/graphics/quartz/ImageFactory.hpp"
+#include "cru/osx/graphics/quartz/Convert.hpp"
+#include "cru/osx/graphics/quartz/Image.hpp"
+#include "cru/platform/graphics/Image.hpp"
+
+#include <ImageIO/ImageIO.h>
+
+namespace cru::platform::graphics::osx::quartz {
+QuartzImageFactory::QuartzImageFactory(IGraphicsFactory* graphics_factory)
+ : OsxQuartzResource(graphics_factory) {}
+
+QuartzImageFactory::~QuartzImageFactory() {}
+
+std::unique_ptr<IImage> QuartzImageFactory::DecodeFromStream(
+ io::Stream* stream) {
+ CGDataProviderRef data_provider = ConvertStreamToCGDataProvider(stream);
+ CGImageSourceRef image_source =
+ CGImageSourceCreateWithDataProvider(data_provider, nullptr);
+
+ CGImageRef cg_image =
+ CGImageSourceCreateImageAtIndex(image_source, 0, nullptr);
+
+ QuartzImage* image =
+ new QuartzImage(GetGraphicsFactory(), this, cg_image, true);
+
+ CFRelease(cg_image);
+ CGDataProviderRelease(data_provider);
+
+ return std::unique_ptr<IImage>(image);
+}
+} // namespace cru::platform::graphics::osx::quartz