2016-12-27 15:32:32 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-12-27 15:32:32 +00:00
|
|
|
|
|
|
|
#include "Common/Image.h"
|
|
|
|
|
2020-11-26 03:34:51 +00:00
|
|
|
#include <string>
|
2016-12-27 15:32:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-18 13:37:20 +00:00
|
|
|
#include <png.h>
|
|
|
|
|
2021-11-24 22:19:40 +00:00
|
|
|
#include "Common/Assert.h"
|
2016-12-27 15:32:32 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2021-10-02 00:56:46 +00:00
|
|
|
#include "Common/ImageC.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Timer.h"
|
2016-12-27 15:32:32 +00:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
bool LoadPNG(const std::vector<u8>& input, std::vector<u8>* data_out, u32* width_out,
|
|
|
|
u32* height_out)
|
|
|
|
{
|
2018-08-18 13:37:20 +00:00
|
|
|
// Using the 'Simplified API' of libpng; see section V in the libpng manual.
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2018-08-18 13:37:20 +00:00
|
|
|
// Read header
|
|
|
|
png_image png = {};
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
if (!png_image_begin_read_from_memory(&png, input.data(), input.size()))
|
2016-12-27 15:32:32 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-18 13:37:20 +00:00
|
|
|
// Prepare output vector
|
2018-08-19 15:35:15 +00:00
|
|
|
png.format = PNG_FORMAT_RGBA;
|
2018-08-18 13:37:20 +00:00
|
|
|
size_t png_size = PNG_IMAGE_SIZE(png);
|
|
|
|
data_out->resize(png_size);
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2018-08-18 13:37:20 +00:00
|
|
|
// Convert to RGBA and write into output vector
|
|
|
|
if (!png_image_finish_read(&png, nullptr, data_out->data(), 0, nullptr))
|
2016-12-27 15:32:32 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-18 13:37:20 +00:00
|
|
|
*width_out = png.width;
|
|
|
|
*height_out = png.height;
|
2016-12-27 15:32:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-26 03:34:51 +00:00
|
|
|
|
2021-10-02 00:56:46 +00:00
|
|
|
static void WriteCallback(png_structp png_ptr, png_bytep data, size_t length)
|
|
|
|
{
|
|
|
|
std::vector<u8>* buffer = static_cast<std::vector<u8>*>(png_get_io_ptr(png_ptr));
|
|
|
|
buffer->insert(buffer->end(), data, data + length);
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:19:40 +00:00
|
|
|
static void ErrorCallback(ErrorHandler* self, const char* msg)
|
|
|
|
{
|
|
|
|
std::vector<std::string>* errors = static_cast<std::vector<std::string>*>(self->error_list);
|
|
|
|
errors->emplace_back(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WarningCallback(ErrorHandler* self, const char* msg)
|
|
|
|
{
|
|
|
|
std::vector<std::string>* warnings = static_cast<std::vector<std::string>*>(self->warning_list);
|
|
|
|
warnings->emplace_back(msg);
|
|
|
|
}
|
|
|
|
|
2020-11-26 03:34:51 +00:00
|
|
|
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
2021-10-02 00:56:46 +00:00
|
|
|
u32 height, int stride, int level)
|
2020-11-26 03:34:51 +00:00
|
|
|
{
|
2021-10-02 00:56:46 +00:00
|
|
|
Common::Timer timer;
|
|
|
|
timer.Start();
|
2020-11-26 03:34:51 +00:00
|
|
|
|
2020-12-19 16:21:58 +00:00
|
|
|
size_t byte_per_pixel;
|
2021-11-24 22:39:07 +00:00
|
|
|
int color_type;
|
2020-11-26 03:34:51 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case ImageByteFormat::RGB:
|
2021-11-24 22:39:07 +00:00
|
|
|
color_type = PNG_COLOR_TYPE_RGB;
|
2020-12-19 16:21:58 +00:00
|
|
|
byte_per_pixel = 3;
|
2020-11-26 03:34:51 +00:00
|
|
|
break;
|
|
|
|
case ImageByteFormat::RGBA:
|
2021-11-24 22:39:07 +00:00
|
|
|
color_type = PNG_COLOR_TYPE_RGBA;
|
2020-12-19 16:21:58 +00:00
|
|
|
byte_per_pixel = 4;
|
2020-11-26 03:34:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-11-10 22:42:49 +00:00
|
|
|
ASSERT_MSG(FRAMEDUMP, false, "Invalid format {}", format);
|
2020-11-26 03:34:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-19 16:21:58 +00:00
|
|
|
// libpng doesn't handle non-ASCII characters in path, so write in two steps:
|
|
|
|
// first to memory, then to file
|
2021-10-02 00:56:46 +00:00
|
|
|
std::vector<u8> buffer;
|
|
|
|
buffer.reserve(byte_per_pixel * width * height);
|
|
|
|
|
2021-11-24 22:19:40 +00:00
|
|
|
std::vector<std::string> warnings;
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
ErrorHandler error_handler;
|
|
|
|
error_handler.error_list = &errors;
|
|
|
|
error_handler.warning_list = &warnings;
|
|
|
|
error_handler.StoreError = ErrorCallback;
|
|
|
|
error_handler.StoreWarning = WarningCallback;
|
|
|
|
|
2021-10-02 00:56:46 +00:00
|
|
|
std::vector<const u8*> rows;
|
|
|
|
rows.reserve(height);
|
|
|
|
for (u32 row = 0; row < height; row++)
|
2020-12-19 16:21:58 +00:00
|
|
|
{
|
2021-10-02 00:56:46 +00:00
|
|
|
rows.push_back(&input[row * stride]);
|
2020-12-19 16:21:58 +00:00
|
|
|
}
|
2020-11-26 03:34:51 +00:00
|
|
|
|
2021-11-24 22:19:40 +00:00
|
|
|
png_structp png_ptr =
|
|
|
|
png_create_write_struct(PNG_LIBPNG_VER_STRING, &error_handler, PngError, PngWarning);
|
2021-10-02 00:56:46 +00:00
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
if (png_ptr != nullptr && info_ptr != nullptr)
|
|
|
|
{
|
2021-11-24 22:39:07 +00:00
|
|
|
success = SavePNG0(png_ptr, info_ptr, color_type, width, height, level, &buffer, WriteCallback,
|
2021-10-02 00:56:46 +00:00
|
|
|
const_cast<u8**>(rows.data()));
|
|
|
|
}
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
File::IOFile outfile(path, "wb");
|
|
|
|
if (!outfile)
|
|
|
|
return false;
|
|
|
|
success = outfile.WriteBytes(buffer.data(), buffer.size());
|
|
|
|
|
|
|
|
timer.Stop();
|
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {}", buffer.size(),
|
|
|
|
width, height, path, level, timer.GetTimeElapsedFormatted());
|
2021-11-24 22:19:40 +00:00
|
|
|
ASSERT(errors.size() == 0);
|
|
|
|
if (warnings.size() != 0)
|
|
|
|
{
|
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Saved with {} warnings:", warnings.size());
|
|
|
|
for (auto& warning : warnings)
|
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "libpng warning: {}", warning);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(FRAMEDUMP,
|
|
|
|
"Failed to save {} by {} image to {} at level {}: {} warnings, {} errors", width,
|
|
|
|
height, path, level, warnings.size(), errors.size());
|
|
|
|
for (auto& error : errors)
|
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "libpng error: {}", error);
|
|
|
|
for (auto& warning : warnings)
|
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "libpng warning: {}", warning);
|
2021-10-02 00:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2020-11-26 03:34:51 +00:00
|
|
|
}
|
2020-12-16 14:34:50 +00:00
|
|
|
|
|
|
|
bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height,
|
2021-10-02 00:56:46 +00:00
|
|
|
int stride, int level)
|
2020-12-16 14:34:50 +00:00
|
|
|
{
|
|
|
|
const std::vector<u8> data = RGBAToRGB(input, width, height, stride);
|
2021-10-02 00:56:46 +00:00
|
|
|
return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height, width * 3, level);
|
2020-12-16 14:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> RGBAToRGB(const u8* input, u32 width, u32 height, int row_stride)
|
|
|
|
{
|
|
|
|
std::vector<u8> buffer;
|
|
|
|
buffer.reserve(width * height * 3);
|
|
|
|
|
|
|
|
for (u32 y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
const u8* pos = input + y * row_stride;
|
|
|
|
for (u32 x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
buffer.push_back(pos[x * 4]);
|
|
|
|
buffer.push_back(pos[x * 4 + 1]);
|
|
|
|
buffer.push_back(pos[x * 4 + 2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
2016-12-27 15:32:32 +00:00
|
|
|
} // namespace Common
|