#include "image.h" #include #include #include #include Image::Image(const char * fileName, int flags) : width {}, height {}, numComponents {} { stbi_set_flip_vertically_on_load(1); int w, h, nc; unsigned char * bytes = stbi_load(fileName, &w, &h, &nc, flags); width = static_cast(w); height = static_cast(h); numComponents = static_cast(nc); if (!bytes) { throw std::runtime_error {std::format("Unable to load image: {}", fileName)}; } data = {bytes, static_cast(width * height * numComponents)}; } Image::Image(std::span 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(buffer.size()), &w, &h, &nc, flags); width = static_cast(w); height = static_cast(h); numComponents = static_cast(nc); if (!bytes) { throw std::runtime_error {std::format("Unable to load image from memory buffer @ {} ({} bytes)", static_cast(buffer.data()), buffer.size_bytes())}; } data = {bytes, static_cast(width * height * numComponents)}; } Image::~Image() { stbi_image_free(data.data()); }