From 2de3b12e9d832278921e296d277a3511b4316a7d Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 26 Nov 2020 04:34:51 +0100 Subject: [PATCH] Common: Add SavePNG() function that writes PNGs using the simplified libpng API. --- Source/Core/Common/Image.cpp | 28 ++++++++++++++++++++++++++++ Source/Core/Common/Image.h | 12 +++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Source/Core/Common/Image.cpp b/Source/Core/Common/Image.cpp index 91d1bc68e0..678a64db8c 100644 --- a/Source/Core/Common/Image.cpp +++ b/Source/Core/Common/Image.cpp @@ -4,6 +4,7 @@ #include "Common/Image.h" +#include #include #include @@ -37,4 +38,31 @@ bool LoadPNG(const std::vector& input, std::vector* data_out, u32* width return true; } + +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; + + switch (format) + { + case ImageByteFormat::RGB: + png.format = PNG_FORMAT_RGB; + break; + case ImageByteFormat::RGBA: + png.format = PNG_FORMAT_RGBA; + break; + default: + return false; + } + + png_image_write_to_file(&png, path.c_str(), 0, input, stride, nullptr); + if (png.warning_or_error & PNG_IMAGE_ERROR) + return false; + + return true; +} } // namespace Common diff --git a/Source/Core/Common/Image.h b/Source/Core/Common/Image.h index d1354b8e0b..9ef399138e 100644 --- a/Source/Core/Common/Image.h +++ b/Source/Core/Common/Image.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "Common/CommonTypes.h" @@ -12,4 +13,13 @@ namespace Common { bool LoadPNG(const std::vector& input, std::vector* data_out, u32* width_out, u32* height_out); -} + +enum class ImageByteFormat +{ + RGB, + RGBA, +}; + +bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width, + u32 height, int stride = 0); +} // namespace Common