A small OpenGL 3+ renderer and game engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
1.3 KiB

#include <cstring>
#include "stb_image.h"
#include "dumbLog.h"
#include "util_image.h"
util_image parseSTBResult(util_image img);
util_image
utilLoadImagePath(const char* full_path)
{
LOG(Info) << "Loading Image: " << full_path << "\n";
util_image image;
stbi_set_flip_vertically_on_load(1);
return parseSTBResult(image);
}
util_image
utilLoadImageBytes(const uint8* bytes, uint length)
{
LOG(Info) << "Loading binary image data\n";
util_image image;
stbi_set_flip_vertically_on_load(1);
image.pixels = stbi_load_from_memory(bytes, length, &image.w, &image.h, &image.num_channels, 0);
return parseSTBResult(image);
}
void
utilFreeImage(util_image image)
{
image.w = image.h = image.data_len = 0;
image.bits_per_channel = 8;
image.num_channels = 4;
std::memset(image.file_path, 0, std::strlen(image.file_path) + 1);
utilSafeFree(image.pixels);
}
util_image
parseSTBResult(util_image image)
{
image.bits_per_channel = 8;
image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel
if (image.pixels == 0) {
LOG(Error) << stbi_failure_reason() << "\n";
utilFreeImage(image);
return image;
}
LOG(Info) << "Image properties, data_len: " << image.data_len
<< ", width: " << image.w << ", height: " << image.h << "\n";
return image;
}