Duckstation/src/common/image.cpp

97 lines
3 KiB
C++
Raw Normal View History

2020-06-30 14:34:22 +00:00
#include "image.h"
2020-09-14 19:27:22 +00:00
#include "file_system.h"
2020-06-30 14:34:22 +00:00
#include "log.h"
#include "stb_image.h"
#include "stb_image_write.h"
#include "string_util.h"
2020-06-30 14:34:22 +00:00
Log_SetChannel(Common::Image);
namespace Common {
bool LoadImageFromFile(Common::RGBA8Image* image, const char* filename)
{
2020-09-14 19:27:22 +00:00
auto fp = FileSystem::OpenManagedCFile(filename, "rb");
if (!fp)
{
return false;
}
2020-06-30 14:34:22 +00:00
int width, height, file_channels;
2020-09-14 19:27:22 +00:00
u8* pixel_data = stbi_load_from_file(fp.get(), &width, &height, &file_channels, 4);
2020-06-30 14:34:22 +00:00
if (!pixel_data)
{
const char* error_reason = stbi_failure_reason();
Log_ErrorPrintf("Failed to load image from '%s': %s", filename, error_reason ? error_reason : "unknown error");
return false;
}
image->SetPixels(static_cast<u32>(width), static_cast<u32>(height), reinterpret_cast<const u32*>(pixel_data));
stbi_image_free(pixel_data);
return true;
}
bool LoadImageFromBuffer(Common::RGBA8Image* image, const void* buffer, std::size_t buffer_size)
{
int width, height, file_channels;
u8* pixel_data = stbi_load_from_memory(static_cast<const stbi_uc*>(buffer), static_cast<int>(buffer_size), &width,
&height, &file_channels, 4);
if (!pixel_data)
{
const char* error_reason = stbi_failure_reason();
Log_ErrorPrintf("Failed to load image from memory: %s", error_reason ? error_reason : "unknown error");
return false;
}
image->SetPixels(static_cast<u32>(width), static_cast<u32>(height), reinterpret_cast<const u32*>(pixel_data));
stbi_image_free(pixel_data);
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;
}
2020-06-30 14:34:22 +00:00
} // namespace Common