7 changed files with 88 additions and 112 deletions
@ -0,0 +1,30 @@ |
|||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "util.h" |
||||||
|
|
||||||
|
|
||||||
|
// NOTE: wrapper for stb_image
|
||||||
|
struct util_image |
||||||
|
{ |
||||||
|
int32 w; |
||||||
|
int32 h; |
||||||
|
int32 bits_per_channel; |
||||||
|
int32 num_channels; |
||||||
|
uint data_len; |
||||||
|
uint8* pixels; |
||||||
|
char file_path[256]; |
||||||
|
}; |
||||||
|
|
||||||
|
struct util_RGBA |
||||||
|
{ |
||||||
|
real32 R; |
||||||
|
real32 G; |
||||||
|
real32 B; |
||||||
|
real32 A; |
||||||
|
}; |
||||||
|
|
||||||
|
util_image utilLoadImage(const char* base_dir, const char* filename); |
||||||
|
|
||||||
|
void utilFreeImage(util_image image); |
||||||
|
|
||||||
@ -0,0 +1,52 @@ |
|||||||
|
|
||||||
|
#include <cstring> |
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION |
||||||
|
#include "stb_image.h" |
||||||
|
|
||||||
|
#include "dumbLog.h" |
||||||
|
#include "util_image.h" |
||||||
|
|
||||||
|
|
||||||
|
util_image |
||||||
|
utilLoadImage(const char* base_dir, const char* filename) |
||||||
|
{ |
||||||
|
const uint max_path = 256; // NOTE: util_image.file_path is char[256]
|
||||||
|
const char* base_name = utilBaseName(filename); |
||||||
|
// NOTE: +1 for '/' char, +1 for null
|
||||||
|
uint l = std::strlen(base_dir) + std::strlen(base_name) + 2; |
||||||
|
assert(l < max_path); |
||||||
|
char full_path[l]; |
||||||
|
std::memcpy(full_path, base_dir, std::strlen(base_dir) + 1); |
||||||
|
std::strcat(std::strcat(full_path, "/"), base_name); |
||||||
|
LOG(Info) << "Loading Image: " << full_path << "\n"; |
||||||
|
|
||||||
|
util_image image; |
||||||
|
std::memcpy(image.file_path, full_path, std::strlen(full_path) + 1); |
||||||
|
stbi_set_flip_vertically_on_load(1); |
||||||
|
image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0); |
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
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); |
||||||
|
} |
||||||
|
|
||||||
Loading…
Reference in new issue