summaryrefslogtreecommitdiff
path: root/gfx/image.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-30 20:36:42 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-04 19:29:49 +0000
commitbde4a9f40bb4af39270a124d5ef9571a83305d0a (patch)
tree536719df20d8f18d4c40e893c2f93b4fa45fb2d5 /gfx/image.cpp
parentAdd some tests over collection (diff)
downloadilt-bde4a9f40bb4af39270a124d5ef9571a83305d0a.tar.bz2
ilt-bde4a9f40bb4af39270a124d5ef9571a83305d0a.tar.xz
ilt-bde4a9f40bb4af39270a124d5ef9571a83305d0a.zip
Restructure to allow a resource path and testing
Diffstat (limited to 'gfx/image.cpp')
-rw-r--r--gfx/image.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/gfx/image.cpp b/gfx/image.cpp
new file mode 100644
index 0000000..20c862f
--- /dev/null
+++ b/gfx/image.cpp
@@ -0,0 +1,20 @@
+#include "image.h"
+#include <cstddef>
+#include <stb_image.h>
+#include <stdexcept>
+
+Image::Image(const char * fileName, int flags) : width {}, height {}, numComponents {}
+{
+ unsigned char * bytes = stbi_load(fileName, &width, &height, &numComponents, flags);
+
+ if (!bytes) {
+ throw std::runtime_error {std::string {"Unable to load image: "} + fileName};
+ }
+
+ data = {bytes, static_cast<size_t>(width * height * numComponents)};
+}
+
+Image::~Image()
+{
+ stbi_image_free(data.data());
+}