mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2025-01-18 22:35:39 +00:00
Common/Image: Add save helpers, fix errors
This commit is contained in:
parent
34dace4506
commit
f6c0156eea
|
@ -2,6 +2,8 @@
|
||||||
#include "file_system.h"
|
#include "file_system.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
#include "string_util.h"
|
||||||
Log_SetChannel(Common::Image);
|
Log_SetChannel(Common::Image);
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
@ -43,4 +45,53 @@ bool LoadImageFromBuffer(Common::RGBA8Image* image, const void* buffer, std::siz
|
||||||
stbi_image_free(pixel_data);
|
stbi_image_free(pixel_data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool WriteImageToFile(const RGBA8Image& image, const char* filename)
|
||||||
|
{
|
||||||
|
const char* extension = std::strrchr(filename, '.');
|
||||||
|
if (!extension)
|
||||||
|
{
|
||||||
|
Log_ErrorPrintf("Unable to determine file extension for '%s'", filename);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto fp = FileSystem::OpenManagedCFile(filename, "wb");
|
||||||
|
if (!fp)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const auto write_func = [](void* context, void* data, int size) {
|
||||||
|
std::fwrite(data, 1, size, static_cast<std::FILE*>(context));
|
||||||
|
};
|
||||||
|
|
||||||
|
bool result = false;
|
||||||
|
if (StringUtil::Strcasecmp(extension, ".png") == 0)
|
||||||
|
{
|
||||||
|
result = (stbi_write_png_to_func(write_func, fp.get(), image.GetWidth(), image.GetHeight(), 4, image.GetPixels(),
|
||||||
|
image.GetByteStride()) != 0);
|
||||||
|
}
|
||||||
|
else if (StringUtil::Strcasecmp(extension, ".jpg") == 0)
|
||||||
|
{
|
||||||
|
result = (stbi_write_jpg_to_func(write_func, fp.get(), image.GetWidth(), image.GetHeight(), 4, image.GetPixels(),
|
||||||
|
95) != 0);
|
||||||
|
}
|
||||||
|
else if (StringUtil::Strcasecmp(extension, ".tga") == 0)
|
||||||
|
{
|
||||||
|
result =
|
||||||
|
(stbi_write_tga_to_func(write_func, fp.get(), image.GetWidth(), image.GetHeight(), 4, image.GetPixels()) != 0);
|
||||||
|
}
|
||||||
|
else if (StringUtil::Strcasecmp(extension, ".bmp") == 0)
|
||||||
|
{
|
||||||
|
result =
|
||||||
|
(stbi_write_bmp_to_func(write_func, fp.get(), image.GetWidth(), image.GetHeight(), 4, image.GetPixels()) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
Log_ErrorPrintf("Unknown extension in filename '%s' or save error: '%s'", filename, extension);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
|
@ -23,7 +23,7 @@ public:
|
||||||
{
|
{
|
||||||
m_width = move.m_width;
|
m_width = move.m_width;
|
||||||
m_height = move.m_height;
|
m_height = move.m_height;
|
||||||
m_pixels = std::move(move.m_width);
|
m_pixels = std::move(move.m_pixels);
|
||||||
move.m_width = 0;
|
move.m_width = 0;
|
||||||
move.m_height = 0;
|
move.m_height = 0;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public:
|
||||||
{
|
{
|
||||||
m_width = move.m_width;
|
m_width = move.m_width;
|
||||||
m_height = move.m_height;
|
m_height = move.m_height;
|
||||||
m_pixels = std::move(move.m_width);
|
m_pixels = std::move(move.m_pixels);
|
||||||
move.m_width = 0;
|
move.m_width = 0;
|
||||||
move.m_height = 0;
|
move.m_height = 0;
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -94,5 +94,6 @@ using RGBA8Image = Image<u32>;
|
||||||
|
|
||||||
bool LoadImageFromFile(Common::RGBA8Image* image, const char* filename);
|
bool LoadImageFromFile(Common::RGBA8Image* image, const char* filename);
|
||||||
bool LoadImageFromBuffer(Common::RGBA8Image* image, const void* buffer, std::size_t buffer_size);
|
bool LoadImageFromBuffer(Common::RGBA8Image* image, const void* buffer, std::size_t buffer_size);
|
||||||
|
bool WriteImageToFile(const Common::RGBA8Image& image, const char* filename);
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
Loading…
Reference in a new issue