2016-12-27 16:32:32 +01:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "Common/Image.h"
|
|
|
|
|
2020-11-26 04:34:51 +01:00
|
|
|
#include <string>
|
2016-12-27 16:32:32 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-18 15:37:20 +02:00
|
|
|
#include <png.h>
|
|
|
|
|
2016-12-27 16:32:32 +01:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-12-19 17:21:58 +01:00
|
|
|
#include "Common/File.h"
|
2016-12-27 16:32:32 +01:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
bool LoadPNG(const std::vector<u8>& input, std::vector<u8>* data_out, u32* width_out,
|
|
|
|
u32* height_out)
|
|
|
|
{
|
2018-08-18 15:37:20 +02:00
|
|
|
// Using the 'Simplified API' of libpng; see section V in the libpng manual.
|
2016-12-27 16:32:32 +01:00
|
|
|
|
2018-08-18 15:37:20 +02: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 16:32:32 +01:00
|
|
|
return false;
|
|
|
|
|
2018-08-18 15:37:20 +02:00
|
|
|
// Prepare output vector
|
2018-08-19 17:35:15 +02:00
|
|
|
png.format = PNG_FORMAT_RGBA;
|
2018-08-18 15:37:20 +02:00
|
|
|
size_t png_size = PNG_IMAGE_SIZE(png);
|
|
|
|
data_out->resize(png_size);
|
2016-12-27 16:32:32 +01:00
|
|
|
|
2018-08-18 15:37:20 +02:00
|
|
|
// Convert to RGBA and write into output vector
|
|
|
|
if (!png_image_finish_read(&png, nullptr, data_out->data(), 0, nullptr))
|
2016-12-27 16:32:32 +01:00
|
|
|
return false;
|
|
|
|
|
2018-08-18 15:37:20 +02:00
|
|
|
*width_out = png.width;
|
|
|
|
*height_out = png.height;
|
2016-12-27 16:32:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-26 04:34:51 +01:00
|
|
|
|
|
|
|
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
|
|
|
u32 height, int stride)
|
|
|
|
{
|
|
|
|
png_image png = {};
|
|
|
|
png.version = PNG_IMAGE_VERSION;
|
|
|
|
png.width = width;
|
|
|
|
png.height = height;
|
|
|
|
|
2020-12-19 17:21:58 +01:00
|
|
|
size_t byte_per_pixel;
|
2020-11-26 04:34:51 +01:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case ImageByteFormat::RGB:
|
|
|
|
png.format = PNG_FORMAT_RGB;
|
2020-12-19 17:21:58 +01:00
|
|
|
byte_per_pixel = 3;
|
2020-11-26 04:34:51 +01:00
|
|
|
break;
|
|
|
|
case ImageByteFormat::RGBA:
|
|
|
|
png.format = PNG_FORMAT_RGBA;
|
2020-12-19 17:21:58 +01:00
|
|
|
byte_per_pixel = 4;
|
2020-11-26 04:34:51 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-19 17:21:58 +01:00
|
|
|
// libpng doesn't handle non-ASCII characters in path, so write in two steps:
|
|
|
|
// first to memory, then to file
|
|
|
|
std::vector<u8> buffer(byte_per_pixel * width * height);
|
|
|
|
png_alloc_size_t size = buffer.size();
|
|
|
|
int success = png_image_write_to_memory(&png, buffer.data(), &size, 0, input, stride, nullptr);
|
|
|
|
if (!success && size > buffer.size())
|
|
|
|
{
|
|
|
|
// initial buffer size guess was too small, set to the now-known size and retry
|
|
|
|
buffer.resize(size);
|
|
|
|
png.warning_or_error = 0;
|
|
|
|
success = png_image_write_to_memory(&png, buffer.data(), &size, 0, input, stride, nullptr);
|
|
|
|
}
|
|
|
|
if (!success || (png.warning_or_error & PNG_IMAGE_ERROR) != 0)
|
2020-11-26 04:34:51 +01:00
|
|
|
return false;
|
|
|
|
|
2020-12-19 17:21:58 +01:00
|
|
|
File::IOFile outfile(path, "wb");
|
|
|
|
if (!outfile)
|
|
|
|
return false;
|
|
|
|
return outfile.WriteBytes(buffer.data(), size);
|
2020-11-26 04:34:51 +01:00
|
|
|
}
|
2020-12-16 15:34:50 +01:00
|
|
|
|
|
|
|
bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height,
|
|
|
|
int stride)
|
|
|
|
{
|
|
|
|
const std::vector<u8> data = RGBAToRGB(input, width, height, stride);
|
|
|
|
return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:32:32 +01:00
|
|
|
} // namespace Common
|