Chira Engine
A customizable MIT-licensed game engine.
Image.cpp
1 #include "Image.h"
2 
3 #define STB_IMAGE_IMPLEMENTATION
4 #include <stb_image.h>
5 
6 using namespace chira;
7 
8 Image::~Image() {
9  if (this->image) {
10  stbi_image_free(image);
11  }
12 }
13 
14 Image::Image(std::string identifier_, bool vFlip)
15  : Resource(std::move(identifier_))
16  , verticalFlip(vFlip) {}
17 
18 void Image::compile(const byte buffer[], std::size_t bufferLen) {
19  int w, h, bd;
20  this->image = Image::getUncompressedImage(buffer, static_cast<int>(bufferLen) - 1, &w, &h, &bd, 0, this->isVerticallyFlipped());
21  this->width = w;
22  this->height = h;
23  this->bitDepth = bd;
24 }
25 
26 byte* Image::getUncompressedImage(const byte buffer[], int bufferLen, int* width, int* height, int* fileChannels, int desiredChannels, bool vflip) {
27  stbi_set_flip_vertically_on_load(vflip);
28  return stbi_load_from_memory(buffer, bufferLen, width, height, fileChannels, desiredChannels);
29 }
30 
31 byte* Image::getUncompressedImage(const byte buffer[], int bufferLen, int desiredChannels, bool vflip) {
32  int width, height, fileChannels;
33  return Image::getUncompressedImage(buffer, bufferLen, &width, &height, &fileChannels, desiredChannels, vflip);
34 }
35 
36 byte* Image::getUncompressedImage(std::string_view filepath, int* width, int* height, int* fileChannels, int desiredChannels, bool vflip) {
37  stbi_set_flip_vertically_on_load(vflip);
38  return stbi_load(filepath.data(), width, height, fileChannels, desiredChannels);
39 }
40 
41 byte* Image::getUncompressedImage(std::string_view filepath, int desiredChannels, bool vflip) {
42  int width, height, fileChannels;
43  return Image::getUncompressedImage(filepath, &width, &height, &fileChannels, desiredChannels, vflip);
44 }
45 
46 void Image::deleteUncompressedImage(byte* image) {
47  if (image) {
48  stbi_image_free(image);
49  }
50 }
A chunk of data, usually a file. Is typically cached and shared.
Definition: Resource.h:19