tacraft/image.cpp

32 lines
803 B
C++
Raw Normal View History

2022-12-15 02:28:00 +00:00
#define STB_IMAGE_IMPLEMENTATION
#include "deps/stb_image.h"
#include "image.hpp"
class ImageImpl : public Image {
public:
ImageImpl(unsigned char* data, int x, int y) : data(data), width(x), height(y) {}
ImageImpl(const ImageImpl&) = delete;
~ImageImpl() {
if(data)
stbi_image_free(data);
}
std::pair<int32_t, int32_t> img_size() override {
return std::make_pair(width, height);
}
const unsigned char* img_data() override {
return data;
}
private:
unsigned char* data = nullptr;
int32_t width;
int32_t height;
};
std::shared_ptr<Image> Image::load_from_file(const char* filename) {
int x, y, n;
auto data = stbi_load(filename, &x, &y, &n, STBI_rgb_alpha);
return std::make_shared<ImageImpl>(data, x, y);
}