summaryrefslogtreecommitdiff
path: root/gfx/image.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-10 18:36:53 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-10 18:36:53 +0100
commit9aa09c55b0af4b8c4c14a4ac50fcb21f7eba492d (patch)
tree401ca6f622182b26bfffe4114ced625a431d5fc0 /gfx/image.cpp
parentPlants are world objects (diff)
downloadilt-9aa09c55b0af4b8c4c14a4ac50fcb21f7eba492d.tar.bz2
ilt-9aa09c55b0af4b8c4c14a4ac50fcb21f7eba492d.tar.xz
ilt-9aa09c55b0af4b8c4c14a4ac50fcb21f7eba492d.zip
Support loading textures from an in memory buffer
Diffstat (limited to 'gfx/image.cpp')
-rw-r--r--gfx/image.cpp16
1 files changed, 16 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());