2025-07-05 13:28:41 +03:30
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#include <engine/utils/file_manager.hpp>
|
|
|
|
#include <stb_image.h>
|
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
basic_file_handle::basic_file_handle(
|
2025-07-05 13:28:41 +03:30
|
|
|
uint8_t *data,
|
|
|
|
uint32_t size,
|
|
|
|
const std::string &path,
|
|
|
|
const std::string &name,
|
|
|
|
const std::string &extension
|
|
|
|
)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_data(data)
|
|
|
|
, m_size(size)
|
|
|
|
, m_path(path)
|
|
|
|
, m_name(name)
|
|
|
|
, m_extension(extension)
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void basic_file_handle::release()
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
delete m_data;
|
|
|
|
m_data = nullptr;
|
|
|
|
m_size = 0ull;
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
basic_file_handle FileManager::read_text_file(const std::string &path)
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
|
|
|
// parse path info
|
|
|
|
std::string name = path.substr(0, path.find('.') + -1);
|
|
|
|
std::string extension = path.substr(path.find('.') + 1);
|
|
|
|
|
|
|
|
// open file
|
|
|
|
std::ifstream file(path.c_str(), std::ios_base::in | std::ios_base::binary);
|
|
|
|
|
|
|
|
// check
|
|
|
|
if (!file)
|
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(warn, "Failed to load text file: {}", path);
|
2025-07-05 13:28:41 +03:30
|
|
|
file.close();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch file size
|
|
|
|
file.seekg(0, std::ios::end);
|
|
|
|
uint32_t size = file.tellg();
|
|
|
|
file.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
if (!size)
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(warn, "Empty text file: {}", path);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// read file
|
|
|
|
uint8_t *data = new uint8_t[size];
|
|
|
|
file.read(reinterpret_cast<char *>(data), size);
|
|
|
|
|
|
|
|
file.close();
|
2025-07-05 15:36:53 +03:30
|
|
|
return basic_file_handle(data, size, path, name, extension);
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
image_file_handle FileManager::read_image_file(const std::string &path, int32_t desiredComponents)
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
|
|
|
// parse path info
|
|
|
|
std::string name = path.substr(0, path.find('.') + -1);
|
|
|
|
std::string extension = path.substr(path.find('.') + 1);
|
|
|
|
|
|
|
|
// load image
|
|
|
|
int32_t width = 0, height = 0, fetchedComponents = 0;
|
|
|
|
uint8_t *pixels = stbi_load(
|
|
|
|
path.c_str(),
|
|
|
|
&width,
|
|
|
|
&height,
|
|
|
|
&fetchedComponents,
|
|
|
|
desiredComponents
|
|
|
|
);
|
|
|
|
|
|
|
|
// check
|
|
|
|
if (!pixels)
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(warn, "Failed to load image file: <{}>", path);
|
2025-07-05 13:28:41 +03:30
|
|
|
else if (fetchedComponents != desiredComponents)
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(warn,
|
2025-07-05 13:28:41 +03:30
|
|
|
"Mismatch of fetched/desired components: <{}> ({}/{})",
|
|
|
|
name + '.' + extension,
|
|
|
|
fetchedComponents,
|
|
|
|
desiredComponents);
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
return image_file_handle(
|
2025-07-05 13:28:41 +03:30
|
|
|
pixels,
|
|
|
|
width * height,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
extension,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
fetchedComponents,
|
|
|
|
desiredComponents
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void image_file_handle::release()
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
stbi_image_free(reinterpret_cast<void *>(m_data));
|
|
|
|
m_data = nullptr;
|
|
|
|
m_size = 0ull;
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Light
|