aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/common/platform/osx/Convert.hpp2
-rw-r--r--include/cru/osx/graphics/quartz/Convert.hpp3
-rw-r--r--include/cru/osx/graphics/quartz/Factory.hpp11
-rw-r--r--include/cru/osx/graphics/quartz/Image.hpp24
-rw-r--r--include/cru/osx/graphics/quartz/ImageFactory.hpp19
-rw-r--r--include/cru/platform/graphics/Factory.hpp4
-rw-r--r--include/cru/platform/graphics/Image.hpp6
-rw-r--r--include/cru/platform/graphics/ImageFactory.hpp11
-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
-rw-r--r--src/platform/graphics/ForDllExport.cpp2
13 files changed, 142 insertions, 3 deletions
diff --git a/include/cru/common/platform/osx/Convert.hpp b/include/cru/common/platform/osx/Convert.hpp
index a583e8da..a2d8beae 100644
--- a/include/cru/common/platform/osx/Convert.hpp
+++ b/include/cru/common/platform/osx/Convert.hpp
@@ -4,7 +4,7 @@
#include "../../String.hpp"
-#include <CoreFoundation/CFString.h>
+#include <CoreFoundation/CoreFoundation.h>
namespace cru::platform::osx {
CFStringRef Convert(const String& string);
diff --git a/include/cru/osx/graphics/quartz/Convert.hpp b/include/cru/osx/graphics/quartz/Convert.hpp
index 1b5a186a..ba2b1be1 100644
--- a/include/cru/osx/graphics/quartz/Convert.hpp
+++ b/include/cru/osx/graphics/quartz/Convert.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "cru/common/Range.hpp"
#include "cru/common/String.hpp"
+#include "cru/common/io/Stream.hpp"
#include "cru/platform/Matrix.hpp"
#include <CoreGraphics/CoreGraphics.h>
@@ -17,4 +18,6 @@ Size Convert(const CGSize& size);
CGRect Convert(const Rect& rect);
Rect Convert(const CGRect& rect);
+
+CGDataProviderRef ConvertStreamToCGDataProvider(io::Stream* stream);
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/osx/graphics/quartz/Factory.hpp b/include/cru/osx/graphics/quartz/Factory.hpp
index 454b2388..59433b54 100644
--- a/include/cru/osx/graphics/quartz/Factory.hpp
+++ b/include/cru/osx/graphics/quartz/Factory.hpp
@@ -1,18 +1,20 @@
#pragma once
#include "Resource.hpp"
#include "cru/common/Base.hpp"
+#include "cru/osx/graphics/quartz/ImageFactory.hpp"
#include "cru/platform/graphics/Factory.hpp"
+#include "cru/platform/graphics/ImageFactory.hpp"
namespace cru::platform::graphics::osx::quartz {
class QuartzGraphicsFactory : public OsxQuartzResource,
public virtual IGraphicsFactory {
public:
- QuartzGraphicsFactory() : OsxQuartzResource(this) {}
+ QuartzGraphicsFactory();
CRU_DELETE_COPY(QuartzGraphicsFactory)
CRU_DELETE_MOVE(QuartzGraphicsFactory)
- ~QuartzGraphicsFactory() override = default;
+ ~QuartzGraphicsFactory() override;
public:
std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush() override;
@@ -24,5 +26,10 @@ class QuartzGraphicsFactory : public OsxQuartzResource,
std::unique_ptr<ITextLayout> CreateTextLayout(std::shared_ptr<IFont> font,
String text) override;
+
+ IImageFactory* GetImageFactory() override;
+
+ private:
+ std::unique_ptr<QuartzImageFactory> image_factory_;
};
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/osx/graphics/quartz/Image.hpp b/include/cru/osx/graphics/quartz/Image.hpp
new file mode 100644
index 00000000..66d7dca0
--- /dev/null
+++ b/include/cru/osx/graphics/quartz/Image.hpp
@@ -0,0 +1,24 @@
+#pragma once
+#include "Resource.hpp"
+#include "cru/platform/graphics/Image.hpp"
+#include "cru/platform/graphics/ImageFactory.hpp"
+
+#include <CoreGraphics/CoreGraphics.h>
+
+namespace cru::platform::graphics::osx::quartz {
+class QuartzImage : public OsxQuartzResource, public virtual IImage {
+ public:
+ QuartzImage(IGraphicsFactory* graphics_factory, IImageFactory* image_factory,
+ CGImageRef image, bool auto_release);
+
+ CRU_DELETE_COPY(QuartzImage)
+ CRU_DELETE_MOVE(QuartzImage)
+
+ ~QuartzImage() override;
+
+ public:
+ private:
+ CGImageRef image_;
+ bool auto_release_ = false;
+};
+} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/osx/graphics/quartz/ImageFactory.hpp b/include/cru/osx/graphics/quartz/ImageFactory.hpp
new file mode 100644
index 00000000..027f3f2a
--- /dev/null
+++ b/include/cru/osx/graphics/quartz/ImageFactory.hpp
@@ -0,0 +1,19 @@
+#pragma once
+#include "Resource.hpp"
+#include "cru/platform/graphics/ImageFactory.hpp"
+
+namespace cru::platform::graphics::osx::quartz {
+class QuartzImageFactory : public OsxQuartzResource,
+ public virtual IImageFactory {
+ public:
+ explicit QuartzImageFactory(IGraphicsFactory* graphics_factory);
+
+ CRU_DELETE_COPY(QuartzImageFactory)
+ CRU_DELETE_MOVE(QuartzImageFactory)
+
+ ~QuartzImageFactory() override;
+
+ public:
+ std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) override;
+};
+} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/platform/graphics/Factory.hpp b/include/cru/platform/graphics/Factory.hpp
index f3802651..7aa88ebd 100644
--- a/include/cru/platform/graphics/Factory.hpp
+++ b/include/cru/platform/graphics/Factory.hpp
@@ -4,6 +4,8 @@
#include "Brush.hpp"
#include "Font.hpp"
#include "Geometry.hpp"
+#include "Image.hpp"
+#include "ImageFactory.hpp"
#include "TextLayout.hpp"
namespace cru::platform::graphics {
@@ -24,5 +26,7 @@ struct CRU_PLATFORM_GRAPHICS_API IGraphicsFactory : virtual IPlatformResource {
brush->SetColor(color);
return brush;
}
+
+ virtual IImageFactory* GetImageFactory() = 0;
};
} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Image.hpp b/include/cru/platform/graphics/Image.hpp
new file mode 100644
index 00000000..bf1e8545
--- /dev/null
+++ b/include/cru/platform/graphics/Image.hpp
@@ -0,0 +1,6 @@
+#pragma once
+#include "Resource.hpp"
+
+namespace cru::platform::graphics {
+struct CRU_PLATFORM_GRAPHICS_API IImage : public virtual IGraphicsResource {};
+} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/ImageFactory.hpp b/include/cru/platform/graphics/ImageFactory.hpp
new file mode 100644
index 00000000..3997f783
--- /dev/null
+++ b/include/cru/platform/graphics/ImageFactory.hpp
@@ -0,0 +1,11 @@
+#pragma once
+#include "Image.hpp"
+#include "Resource.hpp"
+#include "cru/common/io/Stream.hpp"
+
+namespace cru::platform::graphics {
+struct CRU_PLATFORM_GRAPHICS_API IImageFactory
+ : public virtual IGraphicsResource {
+ virtual std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) = 0;
+};
+} // namespace cru::platform::graphics
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
diff --git a/src/platform/graphics/ForDllExport.cpp b/src/platform/graphics/ForDllExport.cpp
index 1a571365..a67030b8 100644
--- a/src/platform/graphics/ForDllExport.cpp
+++ b/src/platform/graphics/ForDllExport.cpp
@@ -2,6 +2,8 @@
#include "cru/platform/graphics/Factory.hpp"
#include "cru/platform/graphics/Font.hpp"
#include "cru/platform/graphics/Geometry.hpp"
+#include "cru/platform/graphics/Image.hpp"
+#include "cru/platform/graphics/ImageFactory.hpp"
#include "cru/platform/graphics/Painter.hpp"
#include "cru/platform/graphics/Resource.hpp"
#include "cru/platform/graphics/TextLayout.hpp"