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 16:07:51 +03:30
|
|
|
BasicFileHandle::BasicFileHandle(
|
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 16:07:51 +03:30
|
|
|
void BasicFileHandle::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-06 14:02:50 +03:30
|
|
|
auto FileManager::read_text_file(const std::string &path) -> BasicFileHandle
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
|
|
|
// parse path info
|
2025-07-06 14:02:50 +03:30
|
|
|
auto name = path.substr(0, path.find('.') + -1);
|
|
|
|
auto extension = path.substr(path.find('.') + 1);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// open file
|
2025-07-06 14:02:50 +03:30
|
|
|
auto file = std::ifstream { path.c_str(), std::ios_base::in | std::ios_base::binary };
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// 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);
|
2025-07-06 14:02:50 +03:30
|
|
|
auto size = file.tellg();
|
2025-07-05 13:28:41 +03:30
|
|
|
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
|
2025-07-06 14:02:50 +03:30
|
|
|
auto *data = new uint8_t[size];
|
2025-07-05 13:28:41 +03:30
|
|
|
file.read(reinterpret_cast<char *>(data), size);
|
|
|
|
|
|
|
|
file.close();
|
2025-07-06 14:02:50 +03:30
|
|
|
return { data, static_cast<unsigned int>(size), path, name, extension };
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
2025-07-06 14:02:50 +03:30
|
|
|
auto FileManager::read_image_file(const std::string &path, int32_t desiredComponents)
|
|
|
|
-> ImageFileHandle
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
|
|
|
// parse path info
|
2025-07-06 14:02:50 +03:30
|
|
|
auto name = path.substr(0, path.find('.') + -1);
|
|
|
|
auto extension = path.substr(path.find('.') + 1);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// load image
|
2025-07-06 14:02:50 +03:30
|
|
|
auto width = 0;
|
|
|
|
auto height = 0;
|
|
|
|
auto fetchedComponents = 0;
|
|
|
|
auto *pixels = stbi_load(path.c_str(), &width, &height, &fetchedComponents, desiredComponents);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// 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-06 14:02:50 +03:30
|
|
|
lt_log(
|
|
|
|
warn,
|
2025-07-05 13:28:41 +03:30
|
|
|
"Mismatch of fetched/desired components: <{}> ({}/{})",
|
|
|
|
name + '.' + extension,
|
|
|
|
fetchedComponents,
|
2025-07-06 14:02:50 +03:30
|
|
|
desiredComponents
|
|
|
|
);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
2025-07-05 16:07:51 +03:30
|
|
|
return ImageFileHandle(
|
2025-07-05 13:28:41 +03:30
|
|
|
pixels,
|
|
|
|
width * height,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
extension,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
fetchedComponents,
|
|
|
|
desiredComponents
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-07-05 16:07:51 +03:30
|
|
|
void ImageFileHandle::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
|