| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Image.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <string> |
| 8 | |
| 9 | #include <spng.h> |
| 10 | |
| 11 | #include "Common/Assert.h" |
| 12 | #include "Common/CommonTypes.h" |
| 13 | #include "Common/IOFile.h" |
| 14 | #include "Common/Logging/Log.h" |
| 15 | #include "Common/Timer.h" |
| 16 | |
| 17 | namespace Common |
| 18 | { |
| 19 | static void spng_free(spng_ctx* ctx) |
| 20 | { |
| 21 | if (ctx) |
| 22 | spng_ctx_free(ctx); |
| 23 | } |
| 24 | |
| 25 | static auto make_spng_ctx(int flags) |
| 26 | { |
| 27 | return std::unique_ptr<spng_ctx, decltype(&spng_free)>(spng_ctx_new(flags), spng_free); |
| 28 | } |
| 29 | |
| 30 | bool LoadPNG(std::span<const u8> input, Common::UniqueBuffer<u8>* data_out, u32* width_out, |
| 31 | u32* height_out) |
| 32 | { |
| 33 | auto ctx = make_spng_ctx(0); |
| 34 | if (!ctx) |
| 35 | return false; |
| 36 | |
| 37 | if (spng_set_png_buffer(ctx.get(), input.data(), input.size())) |
| 38 | return false; |
| 39 | |
| 40 | spng_ihdr ihdr{}; |
| 41 | if (spng_get_ihdr(ctx.get(), &ihdr)) |
| 42 | return false; |
| 43 | |
| 44 | const int format = SPNG_FMT_RGBA8; |
| 45 | size_t decoded_len = 0; |
| 46 | if (spng_decoded_image_size(ctx.get(), format, &decoded_len)) |
| 47 | return false; |
| 48 | |
| 49 | data_out->reset(decoded_len); |
| 50 | if (spng_decode_image(ctx.get(), data_out->data(), decoded_len, format, SPNG_DECODE_TRNS)) |
| 51 | return false; |
| 52 | |
| 53 | *width_out = ihdr.width; |
| 54 | *height_out = ihdr.height; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width, |
| 59 | u32 height, u32 stride, int level) |
| 60 | { |
| 61 | Common::Timer timer; |
| 62 | timer.Start(); |
| 63 | |
| 64 | spng_color_type color_type; |
| 65 | switch (format) |
| 66 | { |
| 67 | case ImageByteFormat::RGB: |
| 68 | color_type = SPNG_COLOR_TYPE_TRUECOLOR; |
| 69 | break; |
| 70 | case ImageByteFormat::RGBA: |
| 71 | color_type = SPNG_COLOR_TYPE_TRUECOLOR_ALPHA; |
| 72 | break; |
| 73 | default: |
| 74 | ASSERT_MSG(FRAMEDUMP, false, "Invalid format {}", static_cast<int>(format)); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | auto ctx = make_spng_ctx(SPNG_CTX_ENCODER); |
| 79 | if (!ctx) |
| 80 | return false; |
| 81 | |
| 82 | auto outfile = File::IOFile(path, "wb"); |
| 83 | if (spng_set_png_file(ctx.get(), outfile.GetHandle())) |
| 84 | return false; |
| 85 | |
| 86 | if (spng_set_option(ctx.get(), SPNG_IMG_COMPRESSION_LEVEL, level)) |
| 87 | return false; |
| 88 | |
| 89 | spng_ihdr ihdr{}; |
| 90 | ihdr.width = width; |
| 91 | ihdr.height = height; |
| 92 | ihdr.color_type = color_type; |
| 93 | ihdr.bit_depth = 8; |
| 94 | if (spng_set_ihdr(ctx.get(), &ihdr)) |
| 95 | return false; |
| 96 | |
| 97 | if (spng_encode_image(ctx.get(), nullptr, 0, SPNG_FMT_PNG, |
| 98 | SPNG_ENCODE_PROGRESSIVE | SPNG_ENCODE_FINALIZE)) |
| 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | for (u32 row = 0; row < height; row++) |
| 103 | { |
| 104 | const int err = spng_encode_row(ctx.get(), &input[row * stride], stride); |
| 105 | if (err == SPNG_EOI) |
| 106 | break; |
| 107 | if (err) |
| 108 | { |
| 109 | ERROR_LOG_FMT(FRAMEDUMP, "Failed to save {} by {} image to {} at level {}: error {}", width, |
| 110 | height, path, level, err); |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | size_t image_len = 0; |
| 116 | spng_decoded_image_size(ctx.get(), SPNG_FMT_PNG, &image_len); |
| 117 | INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {} ms", image_len, |
| 118 | width, height, path, level, timer.ElapsedMs()); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | static Common::UniqueBuffer<u8> RGBAToRGB(const u8* input, u32 width, u32 height, u32 row_stride) |
| 123 | { |
| 124 | Common::UniqueBuffer<u8> buffer; |
| 125 | buffer.reset(width * height * 3); |
| 126 | |
| 127 | std::size_t buffer_index = 0; |
| 128 | for (u32 y = 0; y < height; ++y) |
| 129 | { |
| 130 | const u8* pos = input + y * row_stride; |
| 131 | for (u32 x = 0; x < width; ++x) |
| 132 | { |
| 133 | buffer[buffer_index++] = pos[x * 4]; |
| 134 | buffer[buffer_index++] = pos[x * 4 + 1]; |
| 135 | buffer[buffer_index++] = pos[x * 4 + 2]; |
| 136 | } |
| 137 | } |
| 138 | return buffer; |
| 139 | } |
| 140 | |
| 141 | bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height, |
| 142 | u32 stride, int level) |
| 143 | { |
| 144 | const auto data = RGBAToRGB(input, width, height, stride); |
| 145 | return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height, width * 3, level); |
| 146 | } |
| 147 | } // namespace Common |