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"
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
#include <memory>
|
2020-11-26 03:34:51 +00:00
|
|
|
#include <string>
|
2016-12-27 15:32:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
#include <spng.h>
|
2018-08-18 13:37:20 +00:00
|
|
|
|
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/Logging/Log.h"
|
|
|
|
#include "Common/Timer.h"
|
2016-12-27 15:32:32 +00:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
2022-07-24 09:55:57 +00:00
|
|
|
static void spng_free(spng_ctx* ctx)
|
|
|
|
{
|
|
|
|
if (ctx)
|
|
|
|
spng_ctx_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto make_spng_ctx(int flags)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<spng_ctx, decltype(&spng_free)>(spng_ctx_new(flags), spng_free);
|
|
|
|
}
|
|
|
|
|
2016-12-27 15:32:32 +00:00
|
|
|
bool LoadPNG(const std::vector<u8>& input, std::vector<u8>* data_out, u32* width_out,
|
|
|
|
u32* height_out)
|
|
|
|
{
|
2022-07-24 09:55:57 +00:00
|
|
|
auto ctx = make_spng_ctx(0);
|
|
|
|
if (!ctx)
|
|
|
|
return false;
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
if (spng_set_png_buffer(ctx.get(), input.data(), input.size()))
|
2016-12-27 15:32:32 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
spng_ihdr ihdr{};
|
|
|
|
if (spng_get_ihdr(ctx.get(), &ihdr))
|
|
|
|
return false;
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
const int format = SPNG_FMT_RGBA8;
|
|
|
|
size_t decoded_len = 0;
|
|
|
|
if (spng_decoded_image_size(ctx.get(), format, &decoded_len))
|
2016-12-27 15:32:32 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
data_out->resize(decoded_len);
|
|
|
|
if (spng_decode_image(ctx.get(), data_out->data(), decoded_len, format, SPNG_DECODE_TRNS))
|
|
|
|
return false;
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
*width_out = ihdr.width;
|
|
|
|
*height_out = ihdr.height;
|
2016-12-27 15:32:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-26 03:34:51 +00:00
|
|
|
|
|
|
|
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
2022-07-24 09:55:57 +00:00
|
|
|
u32 height, u32 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
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
spng_color_type color_type;
|
2020-11-26 03:34:51 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case ImageByteFormat::RGB:
|
2022-07-24 09:55:57 +00:00
|
|
|
color_type = SPNG_COLOR_TYPE_TRUECOLOR;
|
2020-11-26 03:34:51 +00:00
|
|
|
break;
|
|
|
|
case ImageByteFormat::RGBA:
|
2022-07-24 09:55:57 +00:00
|
|
|
color_type = SPNG_COLOR_TYPE_TRUECOLOR_ALPHA;
|
2020-11-26 03:34:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-01-13 00:47:26 +00:00
|
|
|
ASSERT_MSG(FRAMEDUMP, false, "Invalid format {}", static_cast<int>(format));
|
2020-11-26 03:34:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
auto ctx = make_spng_ctx(SPNG_CTX_ENCODER);
|
|
|
|
if (!ctx)
|
|
|
|
return false;
|
2020-11-26 03:34:51 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
auto outfile = File::IOFile(path, "wb");
|
|
|
|
if (spng_set_png_file(ctx.get(), outfile.GetHandle()))
|
|
|
|
return false;
|
2021-10-02 00:56:46 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
if (spng_set_option(ctx.get(), SPNG_IMG_COMPRESSION_LEVEL, level))
|
|
|
|
return false;
|
2021-10-02 00:56:46 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
spng_ihdr ihdr{};
|
|
|
|
ihdr.width = width;
|
|
|
|
ihdr.height = height;
|
|
|
|
ihdr.color_type = color_type;
|
|
|
|
ihdr.bit_depth = 8;
|
|
|
|
if (spng_set_ihdr(ctx.get(), &ihdr))
|
|
|
|
return false;
|
2021-10-02 00:56:46 +00:00
|
|
|
|
2022-07-28 04:03:01 +00:00
|
|
|
if (spng_encode_image(ctx.get(), nullptr, 0, SPNG_FMT_PNG,
|
|
|
|
SPNG_ENCODE_PROGRESSIVE | SPNG_ENCODE_FINALIZE))
|
|
|
|
{
|
2022-07-24 09:55:57 +00:00
|
|
|
return false;
|
2022-07-28 04:03:01 +00:00
|
|
|
}
|
2022-07-24 09:55:57 +00:00
|
|
|
for (u32 row = 0; row < height; row++)
|
|
|
|
{
|
|
|
|
const int err = spng_encode_row(ctx.get(), &input[row * stride], stride);
|
|
|
|
if (err == SPNG_EOI)
|
|
|
|
break;
|
|
|
|
if (err)
|
2021-11-24 22:19:40 +00:00
|
|
|
{
|
2022-07-24 09:55:57 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Failed to save {} by {} image to {} at level {}: error {}", width,
|
|
|
|
height, path, level, err);
|
|
|
|
return false;
|
2021-11-24 22:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 14:34:50 +00:00
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
size_t image_len = 0;
|
|
|
|
spng_decoded_image_size(ctx.get(), SPNG_FMT_PNG, &image_len);
|
2022-09-26 22:04:15 +00:00
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {} ms", image_len,
|
|
|
|
width, height, path, level, timer.ElapsedMs());
|
2022-07-24 09:55:57 +00:00
|
|
|
return true;
|
2020-12-16 14:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 09:55:57 +00:00
|
|
|
static std::vector<u8> RGBAToRGB(const u8* input, u32 width, u32 height, u32 row_stride)
|
2020-12-16 14:34:50 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2022-07-24 09:55:57 +00:00
|
|
|
|
|
|
|
bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height,
|
|
|
|
u32 stride, int level)
|
|
|
|
{
|
|
|
|
const std::vector<u8> data = RGBAToRGB(input, width, height, stride);
|
|
|
|
return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height, width * 3, level);
|
|
|
|
}
|
2016-12-27 15:32:32 +00:00
|
|
|
} // namespace Common
|