summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/image.cpp16
-rw-r--r--gfx/image.h1
2 files changed, 17 insertions, 0 deletions
diff --git a/gfx/image.cpp b/gfx/image.cpp
index e3e3b01..fb86cb6 100644
--- a/gfx/image.cpp
+++ b/gfx/image.cpp
@@ -19,6 +19,22 @@ Image::Image(const char * fileName, int flags) : width {}, height {}, numCompone
data = {bytes, static_cast<size_t>(width * height * numComponents)};
}
+Image::Image(std::span<unsigned char> buffer, int flags)
+{
+ stbi_set_flip_vertically_on_load(1);
+ int w, h, nc;
+ unsigned char * bytes = stbi_load_from_memory(buffer.data(), static_cast<int>(buffer.size()), &w, &h, &nc, flags);
+ width = static_cast<unsigned int>(w);
+ height = static_cast<unsigned int>(h);
+ numComponents = static_cast<unsigned int>(nc);
+
+ if (!bytes) {
+ throw std::runtime_error {"Unable to load image from memory buffer "};
+ }
+
+ data = {bytes, static_cast<size_t>(width * height * numComponents)};
+}
+
Image::~Image()
{
stbi_image_free(data.data());
diff --git a/gfx/image.h b/gfx/image.h
index 47437ca..97c2731 100644
--- a/gfx/image.h
+++ b/gfx/image.h
@@ -8,6 +8,7 @@ class Image {
public:
Image(const char * fileName, int flags);
Image(const std::string & fileName, int flags) : Image(fileName.c_str(), flags) { }
+ Image(std::span<unsigned char> data, int flags);
~Image();
NO_COPY(Image);