ummmm
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include <lunasvg.h>
|
#include <lunasvg.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -21,13 +22,30 @@
|
|||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "filesystem_.h"
|
|
||||||
#include "math_.h"
|
#include "math_.h"
|
||||||
|
|
||||||
using namespace anm2ed::resource::texture;
|
using namespace anm2ed::resource::texture;
|
||||||
using namespace anm2ed::util::math;
|
using namespace anm2ed::util::math;
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
namespace filesystem = anm2ed::util::filesystem;
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> file_read_all(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
if (!file) return {};
|
||||||
|
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
auto size = file.tellg();
|
||||||
|
if (size <= 0) return {};
|
||||||
|
|
||||||
|
std::vector<uint8_t> buffer(static_cast<std::size_t>(size));
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
if (!file.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(size))) return {};
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace anm2ed::resource
|
namespace anm2ed::resource
|
||||||
{
|
{
|
||||||
@@ -118,8 +136,13 @@ namespace anm2ed::resource
|
|||||||
|
|
||||||
Texture::Texture(const std::filesystem::path& pngPath)
|
Texture::Texture(const std::filesystem::path& pngPath)
|
||||||
{
|
{
|
||||||
auto pngPathUtf8 = filesystem::path_to_utf8(pngPath);
|
auto fileData = file_read_all(pngPath);
|
||||||
if (const uint8_t* data = stbi_load(pngPathUtf8.c_str(), &size.x, &size.y, nullptr, CHANNELS); data)
|
if (fileData.empty()) return;
|
||||||
|
|
||||||
|
if (const uint8_t* data =
|
||||||
|
stbi_load_from_memory(fileData.data(), static_cast<int>(fileData.size()), &size.x, &size.y, nullptr,
|
||||||
|
CHANNELS);
|
||||||
|
data)
|
||||||
{
|
{
|
||||||
upload(data);
|
upload(data);
|
||||||
stbi_image_free((void*)data);
|
stbi_image_free((void*)data);
|
||||||
@@ -128,8 +151,20 @@ namespace anm2ed::resource
|
|||||||
|
|
||||||
bool Texture::write_png(const std::filesystem::path& path)
|
bool Texture::write_png(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
auto pathUtf8 = filesystem::path_to_utf8(path);
|
if (pixels.empty()) return false;
|
||||||
return stbi_write_png(pathUtf8.c_str(), size.x, size.y, CHANNELS, this->pixels.data(), size.x * CHANNELS);
|
|
||||||
|
std::ofstream file(path, std::ios::binary);
|
||||||
|
if (!file) return false;
|
||||||
|
|
||||||
|
auto write_func = [](void* context, void* data, int size) {
|
||||||
|
auto* stream = static_cast<std::ofstream*>(context);
|
||||||
|
stream->write(static_cast<char*>(data), size);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto result = stbi_write_png_to_func(write_func, &file, size.x, size.y, CHANNELS, this->pixels.data(),
|
||||||
|
size.x * CHANNELS);
|
||||||
|
file.flush();
|
||||||
|
return result != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 Texture::pixel_read(vec2 position) const
|
vec4 Texture::pixel_read(vec2 position) const
|
||||||
|
|||||||
Reference in New Issue
Block a user